feat: New actions

This commit is contained in:
BluJ
2023-05-03 13:15:51 -06:00
parent f4c93438f8
commit a71b2f9f82
5 changed files with 57 additions and 24 deletions

View File

@@ -11,16 +11,15 @@ export class CreatedAction<
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
> {
private constructor(
public readonly myMetaData: Omit<ActionMetaData, "input"> & {
input: Config<Type, WrapperData>
},
public readonly myMetaData: ActionMetaData,
readonly fn: (options: {
effects: Effects
utils: Utils<WrapperData>
input: Type
}) => Promise<ActionResult>,
readonly input: Config<Type, WrapperData> | Config<Type, never>,
) {}
private validator = this.myMetaData.input.validator
public validator = this.input.validator
static of<
WrapperData,
@@ -36,7 +35,8 @@ export class CreatedAction<
input: Type
}) => Promise<ActionResult>,
) {
return new CreatedAction<WrapperData, ConfigType, Type>(metaData, fn)
const { input, ...rest } = metaData
return new CreatedAction<WrapperData, ConfigType, Type>(rest, fn, input)
}
exportedAction: ExportedAction = ({ effects, input }) => {
@@ -47,16 +47,19 @@ export class CreatedAction<
})
}
async exportAction(effects: Effects) {
const myUtils = utils<WrapperData>(effects)
const metaData = {
...this.myMetaData,
input: await this.myMetaData.input.build({
effects,
utils: myUtils,
}),
}
await effects.exportAction(metaData)
run = async ({ effects, input }: { effects: Effects; input?: Type }) => {
return this.fn({
effects,
utils: utils<WrapperData>(effects),
input: this.validator.unsafeCast(input),
})
}
async getConfig({ effects }: { effects: Effects }) {
return this.input.build({
effects,
utils: utils<WrapperData>(effects) as any,
})
}
}

View File

@@ -1,15 +1,19 @@
import { Effects, ExpectedExports, ExportedAction } from "../types"
import { ActionMetaData } from "../types"
import { once } from "../util/once"
import { CreatedAction } from "./createAction"
export function setupActions(...createdActions: CreatedAction<any, any>[]) {
const myActions = once(() => {
const actions: Record<string, CreatedAction<any, any>> = {}
for (const action of createdActions) {
actions[action.myMetaData.id] = action
}
return actions
})
return {
get actions() {
const actions: Record<string, ExportedAction> = {}
for (const action of createdActions) {
actions[action.myMetaData.id] = action.exportedAction
}
return actions
return myActions()
},
get actionsMetadata() {
return createdActions.map((x) => x.myMetaData)

View File

@@ -627,12 +627,15 @@ export class Value<Type, WD> {
static filteredUnion<WrapperData = never>(
getDisabledFn: LazyBuild<WrapperData, string[]>,
) {
return <Type extends Record<string, any>>(
return <
Required extends RequiredDefault<string>,
Type extends Record<string, any>,
>(
a: {
name: string
description?: string | null
warning?: string | null
required: RequiredDefault<string>
required: Required
default?: string | null
},
aVariants: Variants<Type, WrapperData> | Variants<Type, never>,

View File

@@ -59,3 +59,4 @@ export const smtpConfig = Value.filteredUnion(async ({ effects, utils }) => {
},
}),
)
type test = typeof smtpConfig.validator._TYPE

View File

@@ -43,7 +43,10 @@ export namespace ExpectedExports {
* service starting, and that file would indicate that it would rescan all the data.
*/
export type actions = {
[id: string]: ExportedAction
[id: string]: {
run: ExportedAction
getConfig: (options: { effects: Effects }) => Promise<InputSpec>
}
}
/**
@@ -155,7 +158,6 @@ export type ActionMetaData = {
name: string
description: string
id: string
input: null | InputSpec
runningOnly: boolean
/**
* So the ordering of the actions is by alphabetical order of the group, then followed by the alphabetical of the actions
@@ -424,6 +426,26 @@ export type Effects = {
exists(packageId: PackageId): Promise<boolean>
/** Exists could be useful during the runtime to know if some service is running, option dep */
running(packageId: PackageId): Promise<boolean>
/** Instead of creating proxies with nginx, we have a utility to create and maintain a proxy in the lifetime of this running. */
reverseProxy(options: {
bind: {
/** Optional, default is 0.0.0.0 */
ip?: string
port: number
ssl: boolean
}
dst: {
/** Optional: default is 127.0.0.1 */
ip?: string // optional, default 127.0.0.1
port: number
ssl: boolean
}
http: {
// optional, will do TCP layer proxy only if not present
headers: (headers: Record<string, string>) => Record<string, string>
}
}): Promise<{ stop(): Promise<void> }>
restart(): void
shutdown(): void
}