Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk input spec improvements #2785

Merged
merged 12 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ exports[`transformConfigSpec transformConfigSpec(bitcoind) 1`] = `
"disabled": false,
"immutable": false,
"name": "Pruning Mode",
"required": true,
"type": "union",
"variants": {
"automatic": {
Expand Down Expand Up @@ -524,7 +523,6 @@ exports[`transformConfigSpec transformConfigSpec(embassyPages) 1`] = `
"disabled": false,
"immutable": false,
"name": "Type",
"required": true,
"type": "union",
"variants": {
"index": {
Expand Down Expand Up @@ -589,7 +587,6 @@ exports[`transformConfigSpec transformConfigSpec(embassyPages) 1`] = `
"disabled": false,
"immutable": false,
"name": "Folder Location",
"required": false,
"type": "select",
"values": {
"filebrowser": "filebrowser",
Expand Down Expand Up @@ -644,7 +641,6 @@ exports[`transformConfigSpec transformConfigSpec(embassyPages) 1`] = `
"disabled": false,
"immutable": false,
"name": "Type",
"required": true,
"type": "union",
"variants": {
"redirect": {
Expand Down Expand Up @@ -705,7 +701,6 @@ exports[`transformConfigSpec transformConfigSpec(embassyPages) 1`] = `
"disabled": false,
"immutable": false,
"name": "Folder Location",
"required": false,
"type": "select",
"values": {
"filebrowser": "filebrowser",
Expand Down Expand Up @@ -758,7 +753,6 @@ exports[`transformConfigSpec transformConfigSpec(nostr2) 1`] = `
"disabled": false,
"immutable": false,
"name": "Relay Type",
"required": true,
"type": "union",
"variants": {
"private": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export function transformConfigSpec(oldSpec: OldConfigSpec): IST.InputSpec {
}),
{},
),
required: false,
disabled: false,
immutable: false,
}
Expand Down Expand Up @@ -127,7 +126,6 @@ export function transformConfigSpec(oldSpec: OldConfigSpec): IST.InputSpec {
{} as Record<string, { name: string; spec: IST.InputSpec }>,
),
disabled: false,
required: true,
default: oldVal.default,
immutable: false,
}
Expand Down
45 changes: 20 additions & 25 deletions sdk/base/lib/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as T from "../types"
import * as IST from "../actions/input/inputSpecTypes"
import { Action } from "./setupActions"
import { ExtractInputSpecType } from "./input/builder/inputSpec"

export type RunActionInput<Input> =
| Input
Expand Down Expand Up @@ -44,46 +45,40 @@ export const runAction = async <
})
}
}
type GetActionInputType<
A extends Action<T.ActionId, any, any, Record<string, unknown>>,
> = A extends Action<T.ActionId, any, any, infer I> ? I : never
type GetActionInputType<A extends Action<T.ActionId, any, any>> =
A extends Action<T.ActionId, any, infer I> ? ExtractInputSpecType<I> : never

type ActionRequestBase = {
reason?: string
replayId?: string
}
type ActionRequestInput<
T extends Action<T.ActionId, any, any, Record<string, unknown>>,
> = {
type ActionRequestInput<T extends Action<T.ActionId, any, any>> = {
kind: "partial"
value: Partial<GetActionInputType<T>>
}
export type ActionRequestOptions<
T extends Action<T.ActionId, any, any, Record<string, unknown>>,
> = ActionRequestBase &
(
| {
when?: Exclude<
T.ActionRequestTrigger,
{ condition: "input-not-matches" }
>
input?: ActionRequestInput<T>
}
| {
when: T.ActionRequestTrigger & { condition: "input-not-matches" }
input: ActionRequestInput<T>
}
)
export type ActionRequestOptions<T extends Action<T.ActionId, any, any>> =
ActionRequestBase &
(
| {
when?: Exclude<
T.ActionRequestTrigger,
{ condition: "input-not-matches" }
>
input?: ActionRequestInput<T>
}
| {
when: T.ActionRequestTrigger & { condition: "input-not-matches" }
input: ActionRequestInput<T>
}
)

const _validate: T.ActionRequest = {} as ActionRequestOptions<any> & {
actionId: string
packageId: string
severity: T.ActionSeverity
}

export const requestAction = <
T extends Action<T.ActionId, any, any, Record<string, unknown>>,
>(options: {
export const requestAction = <T extends Action<T.ActionId, any, any>>(options: {
effects: T.Effects
packageId: T.PackageId
action: T
Expand Down
13 changes: 12 additions & 1 deletion sdk/base/lib/actions/input/builder/inputSpec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ValueSpec } from "../inputSpecTypes"
import { Value } from "./value"
import { PartialValue, Value } from "./value"
import { _ } from "../../../util"
import { Effects } from "../../../Effects"
import { Parser, object } from "ts-matches"
Expand All @@ -16,6 +16,15 @@ export type ExtractInputSpecType<A extends Record<string, any> | InputSpec<Recor
A extends InputSpec<infer B, any> | InputSpec<infer B, never> ? B :
A

export type ExtractPartialInputSpecType<
A extends
| Record<string, any>
| InputSpec<Record<string, any>, any>
| InputSpec<Record<string, any>, never>,
> = A extends InputSpec<infer B, any> | InputSpec<infer B, never>
? PartialValue<B>
: PartialValue<A>

export type InputSpecOf<A extends Record<string, any>, Store = never> = {
[K in keyof A]: Value<A[K], Store>
}
Expand Down Expand Up @@ -84,6 +93,8 @@ export class InputSpec<Type extends Record<string, any>, Store = never> {
},
public validator: Parser<unknown, Type>,
) {}
_TYPE: Type = null as any as Type
_PARTIAL: PartialValue<Type> = null as any as PartialValue<Type>
async build(options: LazyBuildOptions<Store>) {
const answer = {} as {
[K in keyof Type]: ValueSpec
Expand Down
Loading