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

View File

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

View File

@@ -1,9 +1,11 @@
import { Effects, ExpectedExports, ExportedAction } from "../types" import { Effects, ExpectedExports } from "../types"
import { ActionMetaData } from "../types" import { createUtils } from "../util"
import { once } from "../util/once" import { once } from "../util/once"
import { CreatedAction } from "./createAction" import { CreatedAction } from "./createAction"
export function setupActions(...createdActions: CreatedAction<any, any>[]) { export function setupActions<Store>(
...createdActions: CreatedAction<any, any>[]
) {
const myActions = once(() => { const myActions = once(() => {
const actions: Record<string, CreatedAction<any, any>> = {} const actions: Record<string, CreatedAction<any, any>> = {}
for (const action of createdActions) { for (const action of createdActions) {
@@ -15,8 +17,14 @@ export function setupActions(...createdActions: CreatedAction<any, any>[]) {
get actions() { get actions() {
return myActions() return myActions()
}, },
get actionsMetadata() { async actionMetaData({ effects }: { effects: Effects }) {
return createdActions.map((x) => x.myMetaData) 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> containers: Record<string, Container>
/** This denotes any data, asset, or pointer volumes that should be connected when the "docker run" command is invoked */ /** This denotes any data, asset, or pointer volumes that should be connected when the "docker run" command is invoked */
volumes: Record<string, "data" | "assets"> volumes: Record<string, "data" | "assets">
actions: Array<ActionMetaData>
alerts: { alerts: {
install: string | null install: string | null
update: 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 * 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. * package represents, like running a bitcoind in a bitcoind-wrapper.
@@ -154,6 +158,7 @@ export type ActionMetaData = {
name: string name: string
description: string description: string
id: string id: string
input: InputSpec
allowedStatuses: "only-running" | "only-stopped" | "any" 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 * So the ordering of the actions is by alphabetical order of the group, then followed by the alphabetical of the actions