feat: add in the action meta metadata

This commit is contained in:
BluJ
2023-05-09 14:03:07 -06:00
parent 715ddff896
commit bc13fe06fe
5 changed files with 38 additions and 11 deletions

View File

@@ -12,7 +12,7 @@ import {
ValueSpecText,
} from "./config/configTypes"
import { Variants } from "./config/builder/variants"
import { createAction } from "./actions/createAction"
import { CreatedAction, createAction } from "./actions/createAction"
import {
ActionMetaData,
Effects,
@@ -200,7 +200,8 @@ export class StartSDK<Manifest extends SDKManifest, Store> {
}) => Promise<void>
}) => Migration.of<Store, Version>(options),
},
setupActions,
setupActions: (...createdActions: CreatedAction<any, any>[]) =>
setupActions<Store>(...createdActions),
setupAutoConfig: <
Input,
NestedConfigs extends {

View File

@@ -12,13 +12,13 @@ export class CreatedAction<
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
> {
private constructor(
public readonly myMetaData: ActionMetaData,
public readonly myMetaData: Omit<ActionMetaData, "input">,
readonly fn: (options: {
effects: Effects
utils: Utils<Store>
input: Type
}) => Promise<ActionResult>,
readonly input: Config<Type, Store> | Config<Type, never>,
readonly input: Config<Type, Store>,
) {}
public validator = this.input.validator
@@ -40,7 +40,11 @@ export class CreatedAction<
}) => Promise<ActionResult>,
) {
const { input, ...rest } = metaData
return new CreatedAction<Store, ConfigType, Type>(rest, fn, input)
return new CreatedAction<Store, ConfigType, Type>(
rest,
fn,
input as Config<Type, Store>,
)
}
exportedAction: ExportedAction = ({ effects, input }) => {
@@ -59,6 +63,16 @@ export class CreatedAction<
})
}
async actionMetaData(options: {
effects: Effects
utils: Utils<Store>
}): Promise<ActionMetaData> {
return {
...this.myMetaData,
input: await this.input.build(options),
}
}
async getConfig({ effects }: { effects: Effects }) {
return this.input.build({
effects,

View File

@@ -1,9 +1,11 @@
import { Effects, ExpectedExports, ExportedAction } from "../types"
import { ActionMetaData } from "../types"
import { Effects, ExpectedExports } from "../types"
import { createUtils } from "../util"
import { once } from "../util/once"
import { CreatedAction } from "./createAction"
export function setupActions(...createdActions: CreatedAction<any, any>[]) {
export function setupActions<Store>(
...createdActions: CreatedAction<any, any>[]
) {
const myActions = once(() => {
const actions: Record<string, CreatedAction<any, any>> = {}
for (const action of createdActions) {
@@ -15,8 +17,14 @@ export function setupActions(...createdActions: CreatedAction<any, any>[]) {
get actions() {
return myActions()
},
get actionsMetadata() {
return createdActions.map((x) => x.myMetaData)
async actionMetaData({ effects }: { effects: Effects }) {
const utils = createUtils<Store>(effects)
return Promise.all(
createdActions.map((x) => x.actionMetaData({ effects, utils })),
)
},
} satisfies {
actions: ExpectedExports.actions
actionMetaData: ExpectedExports.actionMetaData
}
}

View File

@@ -63,7 +63,6 @@ export interface SDKManifest {
containers: Record<string, Container>
/** This denotes any data, asset, or pointer volumes that should be connected when the "docker run" command is invoked */
volumes: Record<string, "data" | "assets">
actions: Array<ActionMetaData>
alerts: {
install: string | null
update: string | null

View File

@@ -45,6 +45,10 @@ export namespace ExpectedExports {
}
}
export type actionMetaData = (options: {
effects: Effects
}) => Promise<Array<ActionMetaData>>
/**
* This is the entrypoint for the main container. Used to start up something like the service that the
* package represents, like running a bitcoind in a bitcoind-wrapper.
@@ -154,6 +158,7 @@ export type ActionMetaData = {
name: string
description: string
id: string
input: InputSpec
allowedStatuses: "only-running" | "only-stopped" | "any"
/**
* So the ordering of the actions is by alphabetical order of the group, then followed by the alphabetical of the actions