primitive-admin 1.1.0-alpha.44 → 1.1.0-alpha.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/primitive.js +0 -0
- package/dist/src/commands/blob-buckets.js +20 -7
- package/dist/src/commands/blob-buckets.js.map +1 -1
- package/dist/src/commands/sync.js +113 -28
- package/dist/src/commands/sync.js.map +1 -1
- package/dist/src/commands/workflows.js +185 -20
- package/dist/src/commands/workflows.js.map +1 -1
- package/dist/src/lib/api-client.d.ts +1 -0
- package/dist/src/lib/api-client.js +4 -0
- package/dist/src/lib/api-client.js.map +1 -1
- package/dist/src/lib/codegen-shared/generatedFiles.d.ts +61 -0
- package/dist/src/lib/codegen-shared/generatedFiles.js +127 -0
- package/dist/src/lib/codegen-shared/generatedFiles.js.map +1 -0
- package/dist/src/lib/db-codegen/dbGenerator.d.ts +30 -5
- package/dist/src/lib/db-codegen/dbGenerator.js +227 -165
- package/dist/src/lib/db-codegen/dbGenerator.js.map +1 -1
- package/dist/src/lib/db-codegen/dbTemplates.d.ts +11 -23
- package/dist/src/lib/db-codegen/dbTemplates.js +21 -41
- package/dist/src/lib/db-codegen/dbTemplates.js.map +1 -1
- package/dist/src/lib/db-codegen/dbTsTypes.d.ts +7 -3
- package/dist/src/lib/db-codegen/dbTsTypes.js +23 -1
- package/dist/src/lib/db-codegen/dbTsTypes.js.map +1 -1
- package/dist/src/lib/generated-allowlist.js +13 -0
- package/dist/src/lib/generated-allowlist.js.map +1 -1
- package/dist/src/lib/workflow-codegen/generated-schema-descriptor.d.ts +96 -0
- package/dist/src/lib/workflow-codegen/generated-schema-descriptor.js +112 -0
- package/dist/src/lib/workflow-codegen/generated-schema-descriptor.js.map +1 -0
- package/dist/src/lib/workflow-codegen/generator.d.ts +70 -0
- package/dist/src/lib/workflow-codegen/generator.js +186 -0
- package/dist/src/lib/workflow-codegen/generator.js.map +1 -0
- package/dist/src/lib/workflow-codegen/naming.d.ts +33 -0
- package/dist/src/lib/workflow-codegen/naming.js +81 -0
- package/dist/src/lib/workflow-codegen/naming.js.map +1 -0
- package/dist/src/lib/workflow-codegen/schemaToTs.d.ts +55 -0
- package/dist/src/lib/workflow-codegen/schemaToTs.js +237 -0
- package/dist/src/lib/workflow-codegen/schemaToTs.js.map +1 -0
- package/package.json +4 -2
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `workflows codegen` orchestrator (issue #1442).
|
|
3
|
+
*
|
|
4
|
+
* Reads synced workflow TOMLs (`.primitive/sync/<env>/<appId>/workflows/*.toml`)
|
|
5
|
+
* and emits ONE generated file per workflow —
|
|
6
|
+
* `workflows/generated/<key>.generated.ts` — each containing the workflow's
|
|
7
|
+
* `<Key>Input`/`<Key>Output` types plus a typed `<key>(client)` invoker factory
|
|
8
|
+
* returning `{ runSync?, start }`. This mirrors `databases codegen`'s per-type
|
|
9
|
+
* `<Type>.generated.ts` layout (#1441's `<type>Ops`): per-file artifacts keep
|
|
10
|
+
* team merges conflict-free and let a `[workflow-key]` filter regenerate exactly
|
|
11
|
+
* one file.
|
|
12
|
+
*
|
|
13
|
+
* Design decisions (see the plan of record on #1442, post-pivot):
|
|
14
|
+
* - Schemas are interpreted through the SHARED descriptor
|
|
15
|
+
* (`generated-schema-descriptor.ts`, a vendored copy of the server's
|
|
16
|
+
* `schema-validation` descriptor) so the generated types match runtime
|
|
17
|
+
* validation exactly. A malformed schema throws (fail-and-exit).
|
|
18
|
+
* - `.runSync` is emitted only for `syncCallable: true` workflows (the server
|
|
19
|
+
* rejects run-sync otherwise); `.start` for every non-reserved workflow.
|
|
20
|
+
* - `input` is a REQUIRED option iff the schema rejects `{}` (the client's
|
|
21
|
+
* `input ?? {}` fallback would fail validation); otherwise `input?`.
|
|
22
|
+
* - The runtime call always passes the ORIGINAL key string; only identifiers
|
|
23
|
+
* are sanitized. `workflowKey` is set AFTER spreading `opts`, so a caller
|
|
24
|
+
* cannot override it.
|
|
25
|
+
* - Disk lifecycle (write / `--check` / suffix-based cleanup) delegates to the
|
|
26
|
+
* shared `writeOrCheckGeneratedFiles`, exactly like `databases codegen`.
|
|
27
|
+
*/
|
|
28
|
+
import * as path from "path";
|
|
29
|
+
import { parseConfigToml } from "../config-toml.js";
|
|
30
|
+
import { safeJsonParse } from "../workflow-payload.js";
|
|
31
|
+
import { writeOrCheckGeneratedFiles, } from "../codegen-shared/generatedFiles.js";
|
|
32
|
+
import { fingerprintToml } from "../db-codegen/dbFingerprint.js";
|
|
33
|
+
import { schemaToTsType, schemaRejectsEmptyObject } from "./schemaToTs.js";
|
|
34
|
+
import { workflowInputTypeName, workflowOutputTypeName, workflowFactoryName, } from "./naming.js";
|
|
35
|
+
/** Prefix marking a platform-owned workflow key (mirrors the server's
|
|
36
|
+
* `INTERNAL_WORKFLOW_KEY_PREFIX`; inlined because the CLI cannot import from
|
|
37
|
+
* `src/`). Such workflows are never client-invocable and are skipped. */
|
|
38
|
+
const INTERNAL_WORKFLOW_KEY_PREFIX = "__internal.";
|
|
39
|
+
/** Suffix identifying a generated file (shared with `databases codegen`). */
|
|
40
|
+
export const GENERATED_FILE_SUFFIX = ".generated.ts";
|
|
41
|
+
/** The client package the generated file imports its types from. */
|
|
42
|
+
const CLIENT_PACKAGE = "js-bao-wss-client";
|
|
43
|
+
/** True for a platform-owned workflow key. */
|
|
44
|
+
function isReservedKey(key) {
|
|
45
|
+
return (typeof key === "string" &&
|
|
46
|
+
key.trim().toLowerCase().startsWith(INTERNAL_WORKFLOW_KEY_PREFIX));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Render a top-level `<Name>Input`/`<Name>Output` declaration. An absent schema
|
|
50
|
+
* → `unknown`. An object type → an `interface`; anything else → a `type` alias.
|
|
51
|
+
*/
|
|
52
|
+
function renderTypeDecl(name, schema) {
|
|
53
|
+
if (schema === undefined) {
|
|
54
|
+
return `export type ${name} = unknown;`;
|
|
55
|
+
}
|
|
56
|
+
const expr = schemaToTsType(schema, 0);
|
|
57
|
+
if (expr.startsWith("{")) {
|
|
58
|
+
return `export interface ${name} ${expr}`;
|
|
59
|
+
}
|
|
60
|
+
return `export type ${name} = ${expr};`;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parse a single workflow TOML into the data the emitter needs. Throws on
|
|
64
|
+
* malformed schema JSON (fail-and-exit) — the honest `unknown` fallback is for
|
|
65
|
+
* valid-but-untypeable schemas only. Returns null for a reserved workflow.
|
|
66
|
+
*/
|
|
67
|
+
function parseWorkflow(input) {
|
|
68
|
+
// #1464 removed `@iarna/toml`; #1446 makes `sync pull` emit schemas as native
|
|
69
|
+
// TOML tables (incl. mixed-type enums like `enum = ["ok", 1]`, TOML 1.0). The
|
|
70
|
+
// `parseConfigToml` facade parses both. `safeJsonParse` still owns the
|
|
71
|
+
// string-vs-inline-table schema handling.
|
|
72
|
+
const data = parseConfigToml(input.tomlContent);
|
|
73
|
+
const workflow = data?.workflow ?? {};
|
|
74
|
+
const key = typeof workflow.key === "string" && workflow.key.length > 0
|
|
75
|
+
? workflow.key
|
|
76
|
+
: input.fileStem;
|
|
77
|
+
if (isReservedKey(key))
|
|
78
|
+
return null;
|
|
79
|
+
// safeJsonParse throws on invalid JSON in a schema string — let it bubble.
|
|
80
|
+
const inputSchema = safeJsonParse(workflow.inputSchema);
|
|
81
|
+
const outputSchema = safeJsonParse(workflow.outputSchema);
|
|
82
|
+
const inputTypeName = workflowInputTypeName(key);
|
|
83
|
+
const outputTypeName = workflowOutputTypeName(key);
|
|
84
|
+
return {
|
|
85
|
+
key,
|
|
86
|
+
syncCallable: workflow.syncCallable === true,
|
|
87
|
+
inputTypeName,
|
|
88
|
+
outputTypeName,
|
|
89
|
+
factoryName: workflowFactoryName(key),
|
|
90
|
+
inputDecl: renderTypeDecl(inputTypeName, inputSchema),
|
|
91
|
+
outputDecl: renderTypeDecl(outputTypeName, outputSchema),
|
|
92
|
+
// `input` is required iff the schema rejects the client's `{}` fallback.
|
|
93
|
+
// A schema-less workflow (`undefined`) accepts anything → optional.
|
|
94
|
+
inputRequired: inputSchema !== undefined && schemaRejectsEmptyObject(inputSchema),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** Render the typed invoker factory for one workflow. */
|
|
98
|
+
function renderFactory(wf) {
|
|
99
|
+
const keyLiteral = JSON.stringify(wf.key);
|
|
100
|
+
const inputMember = wf.inputRequired
|
|
101
|
+
? `input: ${wf.inputTypeName}`
|
|
102
|
+
: `input?: ${wf.inputTypeName}`;
|
|
103
|
+
const lines = [
|
|
104
|
+
`export function ${wf.factoryName}(client: JsBaoClient) {`,
|
|
105
|
+
` return {`,
|
|
106
|
+
];
|
|
107
|
+
// Spread `opts` FIRST, then set `workflowKey` LAST, so the factory's intended
|
|
108
|
+
// key always wins. `Omit<..., "workflowKey">` only rejects an excess
|
|
109
|
+
// `workflowKey` on object literals; a caller passing an options *variable*
|
|
110
|
+
// carrying an extra `workflowKey` (or any untyped JS caller) would otherwise
|
|
111
|
+
// override the key and invoke the wrong workflow if the spread came last.
|
|
112
|
+
if (wf.syncCallable) {
|
|
113
|
+
lines.push(` runSync: (\n` +
|
|
114
|
+
` opts: { ${inputMember} } & Omit<RunSyncWorkflowOptions, "workflowKey" | "input">\n` +
|
|
115
|
+
` ) =>\n` +
|
|
116
|
+
` client.workflows.runSync<${wf.inputTypeName}, ${wf.outputTypeName}>({\n` +
|
|
117
|
+
` ...opts,\n` +
|
|
118
|
+
` workflowKey: ${keyLiteral},\n` +
|
|
119
|
+
` }),`);
|
|
120
|
+
}
|
|
121
|
+
lines.push(` start: (\n` +
|
|
122
|
+
` opts: { ${inputMember} } & Omit<StartWorkflowOptions, "workflowKey" | "input">\n` +
|
|
123
|
+
` ) =>\n` +
|
|
124
|
+
` client.workflows.start<${wf.inputTypeName}>({\n` +
|
|
125
|
+
` ...opts,\n` +
|
|
126
|
+
` workflowKey: ${keyLiteral},\n` +
|
|
127
|
+
` }),`);
|
|
128
|
+
lines.push(` };`, `}`);
|
|
129
|
+
return lines.join("\n");
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Render the full `<key>.generated.ts` content for a single workflow TOML input.
|
|
133
|
+
* Returns null for a reserved (`__internal.*`) workflow. Pure — exported so
|
|
134
|
+
* tests can assert on the string directly.
|
|
135
|
+
*/
|
|
136
|
+
export function renderWorkflowFile(input) {
|
|
137
|
+
const wf = parseWorkflow(input);
|
|
138
|
+
if (!wf)
|
|
139
|
+
return null;
|
|
140
|
+
const fingerprint = fingerprintToml(input.tomlContent);
|
|
141
|
+
// Only import the option types actually referenced, so the generated file
|
|
142
|
+
// compiles under a consumer's `noUnusedLocals`.
|
|
143
|
+
const imports = ["JsBaoClient", "StartWorkflowOptions"];
|
|
144
|
+
if (wf.syncCallable)
|
|
145
|
+
imports.push("RunSyncWorkflowOptions");
|
|
146
|
+
imports.sort();
|
|
147
|
+
const blocks = [];
|
|
148
|
+
blocks.push([
|
|
149
|
+
`// AUTO-GENERATED FROM workflows/${wf.key}.toml — DO NOT EDIT.`,
|
|
150
|
+
"// Run `primitive workflows codegen` to regenerate.",
|
|
151
|
+
`// fingerprint: ${fingerprint}`,
|
|
152
|
+
].join("\n"));
|
|
153
|
+
blocks.push(`import type {\n${imports
|
|
154
|
+
.map((i) => ` ${i},`)
|
|
155
|
+
.join("\n")}\n} from "${CLIENT_PACKAGE}";`);
|
|
156
|
+
blocks.push(wf.inputDecl);
|
|
157
|
+
blocks.push(wf.outputDecl);
|
|
158
|
+
blocks.push(renderFactory(wf));
|
|
159
|
+
return { key: wf.key, content: blocks.join("\n\n") + "\n" };
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Run workflow codegen end-to-end. One `<key>.generated.ts` per workflow;
|
|
163
|
+
* delegates the disk lifecycle to the shared writer/checker, exactly like
|
|
164
|
+
* `databases codegen`.
|
|
165
|
+
*/
|
|
166
|
+
export async function generateWorkflowTypes(options) {
|
|
167
|
+
const outAbs = path.resolve(options.outputDir);
|
|
168
|
+
const filesToWrite = new Map();
|
|
169
|
+
for (const input of options.inputs) {
|
|
170
|
+
const rendered = renderWorkflowFile(input);
|
|
171
|
+
if (!rendered)
|
|
172
|
+
continue; // reserved workflow — no file
|
|
173
|
+
const filePath = path.join(outAbs, `${rendered.key}${GENERATED_FILE_SUFFIX}`);
|
|
174
|
+
filesToWrite.set(filePath, rendered.content);
|
|
175
|
+
}
|
|
176
|
+
return await writeOrCheckGeneratedFiles({
|
|
177
|
+
outputDir: outAbs,
|
|
178
|
+
filesToWrite,
|
|
179
|
+
check: options.check,
|
|
180
|
+
// A single-workflow filter run owns only a subset of the generated files,
|
|
181
|
+
// so orphan cleanup / stale detection must be scoped to the filtered file.
|
|
182
|
+
fullOwnership: options.singleWorkflow !== true,
|
|
183
|
+
generatedSuffix: GENERATED_FILE_SUFFIX,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../../../../src/lib/workflow-codegen/generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EACL,0BAA0B,GAE3B,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAC3E,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AAErB;;yEAEyE;AACzE,MAAM,4BAA4B,GAAG,aAAa,CAAC;AAEnD,6EAA6E;AAC7E,MAAM,CAAC,MAAM,qBAAqB,GAA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CAAC;IAC3B,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC,CAAC;IAE/B,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC;AAC9D,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,OAAqC;IAErC,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAE/C,MAAM,YAAY,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC/C,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QACnC,MAAM,QAAQ,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,QAAQ;YAAE,SAAS,CAAC,8BAA8B;QACvD,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CACxB,MAAM,EACN,GAAG,QAAQ,CAAC,GAAG,GAAG,qBAAqB,EAAE,CAC1C,CAAC;QACF,YAAY,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,MAAM,0BAA0B,CAAC;QACtC,SAAS,EAAE,MAAM;QACjB,YAAY;QACZ,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,0EAA0E;QAC1E,2EAA2E;QAC3E,aAAa,EAAE,OAAO,CAAC,cAAc,KAAK,IAAI;QAC9C,eAAe,EAAE,qBAAqB;KACvC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifier-naming helpers for `workflows codegen` (issue #1442).
|
|
3
|
+
*
|
|
4
|
+
* Workflow keys resolve case-insensitively server-side and may contain
|
|
5
|
+
* characters that aren't valid TS identifiers (`.`, `-`). Only the emitted
|
|
6
|
+
* identifiers are sanitized — the runtime call always passes the ORIGINAL key
|
|
7
|
+
* string.
|
|
8
|
+
*
|
|
9
|
+
* Type names: `pascalCase(key) + "Input" | "Output"` (reuses `pascalCase` from
|
|
10
|
+
* the database codegen so the two commands share one PascalCase rule).
|
|
11
|
+
* Factory property: a camelCase form of the key.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* `create-checkout-session` / `createCheckoutSession` → `CreateCheckoutSession`.
|
|
15
|
+
* A digit-leading key (`123-job` → `_123Job`) is prefixed with `_` so the
|
|
16
|
+
* emitted type name is a valid TS identifier.
|
|
17
|
+
*/
|
|
18
|
+
export declare function workflowTypeBaseName(key: string): string;
|
|
19
|
+
/** PascalCase base + `Input` (e.g. `CreateCheckoutSessionInput`). */
|
|
20
|
+
export declare function workflowInputTypeName(key: string): string;
|
|
21
|
+
/** PascalCase base + `Output` (e.g. `CreateCheckoutSessionOutput`). */
|
|
22
|
+
export declare function workflowOutputTypeName(key: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* camelCase factory function name for a workflow key: PascalCase, then lowercase
|
|
25
|
+
* the first character (`create-checkout-session` → `createCheckoutSession`,
|
|
26
|
+
* `refresh-sheet` → `refreshSheet`). Used as the top-level exported factory
|
|
27
|
+
* function name in the per-workflow generated file, so it must be a legal
|
|
28
|
+
* function identifier: a digit-leading key (`123-job` → `_123Job`) is
|
|
29
|
+
* underscore-prefixed, and a key that lands on a JS reserved word (`delete`,
|
|
30
|
+
* `new`, `import`) is underscore-suffixed (`delete_`). Only the emitted
|
|
31
|
+
* identifier changes; the runtime call still passes the ORIGINAL key string.
|
|
32
|
+
*/
|
|
33
|
+
export declare function workflowFactoryName(key: string): string;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifier-naming helpers for `workflows codegen` (issue #1442).
|
|
3
|
+
*
|
|
4
|
+
* Workflow keys resolve case-insensitively server-side and may contain
|
|
5
|
+
* characters that aren't valid TS identifiers (`.`, `-`). Only the emitted
|
|
6
|
+
* identifiers are sanitized — the runtime call always passes the ORIGINAL key
|
|
7
|
+
* string.
|
|
8
|
+
*
|
|
9
|
+
* Type names: `pascalCase(key) + "Input" | "Output"` (reuses `pascalCase` from
|
|
10
|
+
* the database codegen so the two commands share one PascalCase rule).
|
|
11
|
+
* Factory property: a camelCase form of the key.
|
|
12
|
+
*/
|
|
13
|
+
import { pascalCase } from "../db-codegen/dbNaming.js";
|
|
14
|
+
/**
|
|
15
|
+
* Ensure an identifier is a valid TS identifier start. The server accepts
|
|
16
|
+
* workflow keys matching `^[a-z0-9][a-z0-9-_]{2,}$`, so a digit-leading key
|
|
17
|
+
* like `123-job` PascalCases to `123Job` — not a legal TS type name or
|
|
18
|
+
* unquoted object property, since identifiers can't start with a digit.
|
|
19
|
+
* Prefix an underscore in that case (`123Job` → `_123Job`). Only the emitted
|
|
20
|
+
* identifier changes; the runtime call still passes the ORIGINAL key string.
|
|
21
|
+
*/
|
|
22
|
+
function ensureValidIdentifierStart(name) {
|
|
23
|
+
const first = name.charAt(0);
|
|
24
|
+
if (first >= "0" && first <= "9") {
|
|
25
|
+
return `_${name}`;
|
|
26
|
+
}
|
|
27
|
+
return name;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* `create-checkout-session` / `createCheckoutSession` → `CreateCheckoutSession`.
|
|
31
|
+
* A digit-leading key (`123-job` → `_123Job`) is prefixed with `_` so the
|
|
32
|
+
* emitted type name is a valid TS identifier.
|
|
33
|
+
*/
|
|
34
|
+
export function workflowTypeBaseName(key) {
|
|
35
|
+
return ensureValidIdentifierStart(pascalCase(key));
|
|
36
|
+
}
|
|
37
|
+
/** PascalCase base + `Input` (e.g. `CreateCheckoutSessionInput`). */
|
|
38
|
+
export function workflowInputTypeName(key) {
|
|
39
|
+
return `${workflowTypeBaseName(key)}Input`;
|
|
40
|
+
}
|
|
41
|
+
/** PascalCase base + `Output` (e.g. `CreateCheckoutSessionOutput`). */
|
|
42
|
+
export function workflowOutputTypeName(key) {
|
|
43
|
+
return `${workflowTypeBaseName(key)}Output`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Reserved words that cannot be used as a bare `function <name>` declaration.
|
|
47
|
+
* The per-workflow factory is now a top-level exported function (not an
|
|
48
|
+
* object-literal property), so a key that camelCases to one of these must be
|
|
49
|
+
* escaped or `export function delete(client) {}` is a syntax error. Covers ES
|
|
50
|
+
* reserved words plus the strict-mode/future set — a workflow key resolves case
|
|
51
|
+
* -insensitively and matches `^[a-z0-9][a-z0-9-_]{2,}$`, so only the lowercase
|
|
52
|
+
* single-word forms can actually collide.
|
|
53
|
+
*/
|
|
54
|
+
const RESERVED_WORDS = new Set([
|
|
55
|
+
"break", "case", "catch", "class", "const", "continue", "debugger",
|
|
56
|
+
"default", "delete", "do", "else", "enum", "export", "extends", "false",
|
|
57
|
+
"finally", "for", "function", "if", "import", "in", "instanceof", "new",
|
|
58
|
+
"null", "return", "super", "switch", "this", "throw", "true", "try",
|
|
59
|
+
"typeof", "var", "void", "while", "with", "yield", "let", "static",
|
|
60
|
+
"implements", "interface", "package", "private", "protected", "public",
|
|
61
|
+
"await",
|
|
62
|
+
]);
|
|
63
|
+
/**
|
|
64
|
+
* camelCase factory function name for a workflow key: PascalCase, then lowercase
|
|
65
|
+
* the first character (`create-checkout-session` → `createCheckoutSession`,
|
|
66
|
+
* `refresh-sheet` → `refreshSheet`). Used as the top-level exported factory
|
|
67
|
+
* function name in the per-workflow generated file, so it must be a legal
|
|
68
|
+
* function identifier: a digit-leading key (`123-job` → `_123Job`) is
|
|
69
|
+
* underscore-prefixed, and a key that lands on a JS reserved word (`delete`,
|
|
70
|
+
* `new`, `import`) is underscore-suffixed (`delete_`). Only the emitted
|
|
71
|
+
* identifier changes; the runtime call still passes the ORIGINAL key string.
|
|
72
|
+
*/
|
|
73
|
+
export function workflowFactoryName(key) {
|
|
74
|
+
const pascal = pascalCase(key);
|
|
75
|
+
if (pascal.length === 0)
|
|
76
|
+
return pascal;
|
|
77
|
+
const camel = pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
|
78
|
+
const safeStart = ensureValidIdentifierStart(camel);
|
|
79
|
+
return RESERVED_WORDS.has(safeStart) ? `${safeStart}_` : safeStart;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=naming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"naming.js","sourceRoot":"","sources":["../../../../src/lib/workflow-codegen/naming.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAEvD;;;;;;;GAOG;AACH,SAAS,0BAA0B,CAAC,IAAY;IAC9C,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC7B,IAAI,KAAK,IAAI,GAAG,IAAI,KAAK,IAAI,GAAG,EAAE,CAAC;QACjC,OAAO,IAAI,IAAI,EAAE,CAAC;IACpB,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,GAAW;IAC9C,OAAO,0BAA0B,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;AACrD,CAAC;AAED,qEAAqE;AACrE,MAAM,UAAU,qBAAqB,CAAC,GAAW;IAC/C,OAAO,GAAG,oBAAoB,CAAC,GAAG,CAAC,OAAO,CAAC;AAC7C,CAAC;AAED,uEAAuE;AACvE,MAAM,UAAU,sBAAsB,CAAC,GAAW;IAChD,OAAO,GAAG,oBAAoB,CAAC,GAAG,CAAC,QAAQ,CAAC;AAC9C,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC;IAC7B,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU;IAClE,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO;IACvE,SAAS,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK;IACvE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK;IACnE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ;IAClE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ;IACtE,OAAO;CACR,CAAC,CAAC;AAEH;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAW;IAC7C,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;IAC/B,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC;IACvC,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC/D,MAAM,SAAS,GAAG,0BAA0B,CAAC,KAAK,CAAC,CAAC;IACpD,OAAO,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC;AACrE,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-Schema → TypeScript converter for `workflows codegen` (issue #1442).
|
|
3
|
+
*
|
|
4
|
+
* Workflow `inputSchema`/`outputSchema` are real JSON Schema. This converter
|
|
5
|
+
* types only the subset the server's validator descends into — and it reads
|
|
6
|
+
* that interpretation from the SAME descriptor the server validator consumes,
|
|
7
|
+
* vendored into the CLI as `generated-schema-descriptor.ts` (a byte copy of
|
|
8
|
+
* `src/workflows/schema-descriptor.ts`, kept fresh by
|
|
9
|
+
* `cli/scripts/gen-workflow-schema-descriptor.mjs` + its drift guard). So the
|
|
10
|
+
* generated type and the runtime validation can never disagree on:
|
|
11
|
+
*
|
|
12
|
+
* - `type` single scalar (`string`/`number`/`integer`/`boolean`/`null`) →
|
|
13
|
+
* the mapped TS scalar.
|
|
14
|
+
* - `type` array-union — only when every member is a scalar/`null` → a TS
|
|
15
|
+
* union. A union that contains `object` or `array` renders `unknown`,
|
|
16
|
+
* because the server only walks `properties`/`items` for the bare string
|
|
17
|
+
* `"object"`/`"array"` — a union is never descended, so a precise-looking
|
|
18
|
+
* type would over-promise.
|
|
19
|
+
* - `enum` → a scalar-literal union, intersected with the declared `type`
|
|
20
|
+
* first (the server gates on `type` before checking `enum`).
|
|
21
|
+
* - `type: "object"` with `properties` → a nested object type; `required[]`
|
|
22
|
+
* drives `name:` vs `name?:`. An OPEN object (no `additionalProperties:
|
|
23
|
+
* false`) also emits `[key: string]: unknown` so extra keys the runtime
|
|
24
|
+
* validator accepts don't hit TS excess-property errors; a CLOSED object
|
|
25
|
+
* (`additionalProperties: false`) omits the index signature.
|
|
26
|
+
* - `type: "array"` with `items` → `T[]`.
|
|
27
|
+
*
|
|
28
|
+
* Everything the server ignores (`$ref`, `oneOf`/`anyOf`/`allOf`, `format`,
|
|
29
|
+
* `patternProperties`, numeric constraints, tuple `items`) renders `unknown`.
|
|
30
|
+
*
|
|
31
|
+
* Errors are NOT swallowed here — a malformed schema fails upstream at parse
|
|
32
|
+
* time; this walker only sees an already-parsed schema and returns `unknown`
|
|
33
|
+
* for anything it does not recognize.
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* Convert a parsed JSON Schema node to a TypeScript type expression. Object
|
|
37
|
+
* types render multi-line (2-space indent per level); everything else renders
|
|
38
|
+
* on a single line. `indent` is the current indentation depth.
|
|
39
|
+
*/
|
|
40
|
+
export declare function schemaToTsType(schema: any, indent?: number): string;
|
|
41
|
+
/**
|
|
42
|
+
* True when the schema REJECTS an empty object `{}` — i.e. the client's
|
|
43
|
+
* `rootInput: options.input ?? {}` fallback would fail validation, so `input`
|
|
44
|
+
* must be a compile-time-required option (issue #1442, P4). Derived from the
|
|
45
|
+
* shared descriptor so it matches the server validator exactly:
|
|
46
|
+
*
|
|
47
|
+
* - a declared top-level `type` that `{}` does not satisfy (anything but
|
|
48
|
+
* `object`, e.g. `string`/`array`, or a union with no `object` arm);
|
|
49
|
+
* - a top-level `enum` (a fresh `{}` is never an enum member);
|
|
50
|
+
* - `type: "object"` with a non-empty `required[]` (a missing required key).
|
|
51
|
+
*
|
|
52
|
+
* An all-optional object schema, or a schema with no top-level constraints,
|
|
53
|
+
* accepts `{}` → `input` is optional.
|
|
54
|
+
*/
|
|
55
|
+
export declare function schemaRejectsEmptyObject(schema: any): boolean;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-Schema → TypeScript converter for `workflows codegen` (issue #1442).
|
|
3
|
+
*
|
|
4
|
+
* Workflow `inputSchema`/`outputSchema` are real JSON Schema. This converter
|
|
5
|
+
* types only the subset the server's validator descends into — and it reads
|
|
6
|
+
* that interpretation from the SAME descriptor the server validator consumes,
|
|
7
|
+
* vendored into the CLI as `generated-schema-descriptor.ts` (a byte copy of
|
|
8
|
+
* `src/workflows/schema-descriptor.ts`, kept fresh by
|
|
9
|
+
* `cli/scripts/gen-workflow-schema-descriptor.mjs` + its drift guard). So the
|
|
10
|
+
* generated type and the runtime validation can never disagree on:
|
|
11
|
+
*
|
|
12
|
+
* - `type` single scalar (`string`/`number`/`integer`/`boolean`/`null`) →
|
|
13
|
+
* the mapped TS scalar.
|
|
14
|
+
* - `type` array-union — only when every member is a scalar/`null` → a TS
|
|
15
|
+
* union. A union that contains `object` or `array` renders `unknown`,
|
|
16
|
+
* because the server only walks `properties`/`items` for the bare string
|
|
17
|
+
* `"object"`/`"array"` — a union is never descended, so a precise-looking
|
|
18
|
+
* type would over-promise.
|
|
19
|
+
* - `enum` → a scalar-literal union, intersected with the declared `type`
|
|
20
|
+
* first (the server gates on `type` before checking `enum`).
|
|
21
|
+
* - `type: "object"` with `properties` → a nested object type; `required[]`
|
|
22
|
+
* drives `name:` vs `name?:`. An OPEN object (no `additionalProperties:
|
|
23
|
+
* false`) also emits `[key: string]: unknown` so extra keys the runtime
|
|
24
|
+
* validator accepts don't hit TS excess-property errors; a CLOSED object
|
|
25
|
+
* (`additionalProperties: false`) omits the index signature.
|
|
26
|
+
* - `type: "array"` with `items` → `T[]`.
|
|
27
|
+
*
|
|
28
|
+
* Everything the server ignores (`$ref`, `oneOf`/`anyOf`/`allOf`, `format`,
|
|
29
|
+
* `patternProperties`, numeric constraints, tuple `items`) renders `unknown`.
|
|
30
|
+
*
|
|
31
|
+
* Errors are NOT swallowed here — a malformed schema fails upstream at parse
|
|
32
|
+
* time; this walker only sees an already-parsed schema and returns `unknown`
|
|
33
|
+
* for anything it does not recognize.
|
|
34
|
+
*/
|
|
35
|
+
import { describeSchema, isType, } from "./generated-schema-descriptor.js";
|
|
36
|
+
/** Scalar JSON-schema types this converter maps to a concrete TS scalar. */
|
|
37
|
+
const SCALAR_TS = {
|
|
38
|
+
string: "string",
|
|
39
|
+
number: "number",
|
|
40
|
+
integer: "number",
|
|
41
|
+
boolean: "boolean",
|
|
42
|
+
null: "null",
|
|
43
|
+
};
|
|
44
|
+
/** A shared empty-object reference for the `{}`-rejection probe below. */
|
|
45
|
+
const EMPTY_OBJECT = Object.freeze({});
|
|
46
|
+
/**
|
|
47
|
+
* Render a scalar-literal union from an `enum` array. `JSON.stringify` emits a
|
|
48
|
+
* valid TS literal for a string, number, boolean, or `null`. An object/array
|
|
49
|
+
* member has no TS literal form, so the whole enum falls back to `unknown`
|
|
50
|
+
* (returns `null` here).
|
|
51
|
+
*/
|
|
52
|
+
function tsUnionFromScalarEnum(values) {
|
|
53
|
+
if (!Array.isArray(values) || values.length === 0)
|
|
54
|
+
return null;
|
|
55
|
+
const literals = [];
|
|
56
|
+
const seen = new Set();
|
|
57
|
+
for (const v of values) {
|
|
58
|
+
if (v !== null && (typeof v === "object" || Array.isArray(v))) {
|
|
59
|
+
// Objects/arrays are not representable as TS literals.
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const literal = JSON.stringify(v);
|
|
63
|
+
if (literal === undefined)
|
|
64
|
+
return null; // e.g. `undefined` in the enum
|
|
65
|
+
if (!seen.has(literal)) {
|
|
66
|
+
seen.add(literal);
|
|
67
|
+
literals.push(literal);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return literals.length > 0 ? literals.join(" | ") : null;
|
|
71
|
+
}
|
|
72
|
+
/** True when a rendered type is a bare identifier safe to suffix with `[]`. */
|
|
73
|
+
function isSimpleTypeExpr(expr) {
|
|
74
|
+
return /^[A-Za-z0-9_.]+(\[\])*$/.test(expr);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Convert a parsed JSON Schema node to a TypeScript type expression. Object
|
|
78
|
+
* types render multi-line (2-space indent per level); everything else renders
|
|
79
|
+
* on a single line. `indent` is the current indentation depth.
|
|
80
|
+
*/
|
|
81
|
+
export function schemaToTsType(schema, indent = 0) {
|
|
82
|
+
const d = describeSchema(schema);
|
|
83
|
+
// `enum` is the most specific constraint — the server checks it AFTER the
|
|
84
|
+
// type gate, so when both are present only the enum members that satisfy the
|
|
85
|
+
// declared type(s) are reachable. Emit just those (intersect with `type`
|
|
86
|
+
// via the shared `isType`). If the type filter leaves nothing representable,
|
|
87
|
+
// fall through to plain `type` rendering rather than over-promising.
|
|
88
|
+
if (d.enumValues) {
|
|
89
|
+
const allowedTypes = d.types;
|
|
90
|
+
const candidates = allowedTypes.length > 0
|
|
91
|
+
? d.enumValues.filter((v) => allowedTypes.some((t) => isType(v, t)))
|
|
92
|
+
: d.enumValues;
|
|
93
|
+
if (candidates.length === 0) {
|
|
94
|
+
// No enum member satisfies the declared type gate (or the enum is empty),
|
|
95
|
+
// so the runtime rejects EVERY value: the type gate lets some values
|
|
96
|
+
// through, then `enum.includes(value)` fails them all. The reachable set
|
|
97
|
+
// is empty → an uninhabitable `never`. Widening to the declared type here
|
|
98
|
+
// (e.g. `{type:"string", enum:[1]}` → `string`) would let typed callers
|
|
99
|
+
// pass values the server always rejects.
|
|
100
|
+
return "never";
|
|
101
|
+
}
|
|
102
|
+
const union = tsUnionFromScalarEnum(candidates);
|
|
103
|
+
if (union !== null)
|
|
104
|
+
return union;
|
|
105
|
+
// Candidates survive the type gate but aren't representable as TS literals
|
|
106
|
+
// (object/array enum members). With no declared type there is nothing to
|
|
107
|
+
// fall back on → `unknown`; otherwise fall through to plain `type`.
|
|
108
|
+
if (allowedTypes.length === 0)
|
|
109
|
+
return "unknown";
|
|
110
|
+
}
|
|
111
|
+
// Array-union type (e.g. `["string", "null"]`). Only scalar/null members get
|
|
112
|
+
// a precise union — the server never descends `properties`/`items` for a
|
|
113
|
+
// union, so an object/array member forces `unknown`.
|
|
114
|
+
if (d.isUnion) {
|
|
115
|
+
const parts = [];
|
|
116
|
+
const seen = new Set();
|
|
117
|
+
for (const t of d.types) {
|
|
118
|
+
const mapped = SCALAR_TS[t];
|
|
119
|
+
if (mapped === undefined)
|
|
120
|
+
return "unknown"; // object/array/unknown member
|
|
121
|
+
if (!seen.has(mapped)) {
|
|
122
|
+
seen.add(mapped);
|
|
123
|
+
parts.push(mapped);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return parts.length > 0 ? parts.join(" | ") : "unknown";
|
|
127
|
+
}
|
|
128
|
+
if (d.singularType === null) {
|
|
129
|
+
// No usable single type and no enum — the server enforces nothing.
|
|
130
|
+
return "unknown";
|
|
131
|
+
}
|
|
132
|
+
// Object: the server walks `properties`/`required` ONLY when the type is
|
|
133
|
+
// exactly `"object"`. `d.object` is non-null only in that case with declared
|
|
134
|
+
// properties; a typed object with no properties is open → Record.
|
|
135
|
+
if (d.singularType === "object") {
|
|
136
|
+
if (d.object)
|
|
137
|
+
return renderObjectType(d.object, indent);
|
|
138
|
+
return "Record<string, unknown>";
|
|
139
|
+
}
|
|
140
|
+
// Array: the server walks `items` ONLY when the type is exactly `"array"`.
|
|
141
|
+
if (d.singularType === "array") {
|
|
142
|
+
const items = d.array ? d.array.items : undefined;
|
|
143
|
+
// Missing or tuple `items` (an array of schemas) is ignored → unknown[].
|
|
144
|
+
if (!items || typeof items !== "object" || Array.isArray(items)) {
|
|
145
|
+
return "unknown[]";
|
|
146
|
+
}
|
|
147
|
+
const elem = schemaToTsType(items, indent);
|
|
148
|
+
return isSimpleTypeExpr(elem) ? `${elem}[]` : `(${elem})[]`;
|
|
149
|
+
}
|
|
150
|
+
// Single scalar type.
|
|
151
|
+
const scalar = SCALAR_TS[d.singularType];
|
|
152
|
+
return scalar ?? "unknown";
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Render an object descriptor (`describeSchema(...).object`) as a TS object
|
|
156
|
+
* type. `required[]` drives optionality. An OPEN object (not
|
|
157
|
+
* `additionalProperties: false`) emits `[key: string]: unknown` so extra keys
|
|
158
|
+
* the runtime accepts don't trip TS excess-property checks; a CLOSED object
|
|
159
|
+
* omits it.
|
|
160
|
+
*/
|
|
161
|
+
function renderObjectType(objectDesc, indent) {
|
|
162
|
+
const { properties, required, additionalPropertiesClosed } = objectDesc;
|
|
163
|
+
const propNames = Object.keys(properties);
|
|
164
|
+
const requiredSet = new Set(Array.isArray(required)
|
|
165
|
+
? required.filter((k) => typeof k === "string")
|
|
166
|
+
: []);
|
|
167
|
+
const pad = " ".repeat(indent + 1);
|
|
168
|
+
const closePad = " ".repeat(indent);
|
|
169
|
+
const propNameSet = new Set(propNames);
|
|
170
|
+
const lines = ["{"];
|
|
171
|
+
for (const name of propNames) {
|
|
172
|
+
const childType = schemaToTsType(properties[name], indent + 1);
|
|
173
|
+
const optional = requiredSet.has(name) ? "" : "?";
|
|
174
|
+
lines.push(`${pad}${quotePropIfNeeded(name)}${optional}: ${childType};`);
|
|
175
|
+
}
|
|
176
|
+
// A key listed in `required` but absent from `properties` has no declared
|
|
177
|
+
// child schema, yet the validator still rejects an object missing it. Emit it
|
|
178
|
+
// as a required `unknown` member so the generated type demands the key too —
|
|
179
|
+
// otherwise `{properties:{}, required:["id"]}` would accept `{}`, which the
|
|
180
|
+
// runtime rejects.
|
|
181
|
+
for (const name of requiredSet) {
|
|
182
|
+
if (!propNameSet.has(name)) {
|
|
183
|
+
lines.push(`${pad}${quotePropIfNeeded(name)}: unknown;`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// Open objects accept extra keys at runtime; model that with an index
|
|
187
|
+
// signature so a call site passing an unlisted key still compiles.
|
|
188
|
+
if (!additionalPropertiesClosed) {
|
|
189
|
+
lines.push(`${pad}[key: string]: unknown;`);
|
|
190
|
+
}
|
|
191
|
+
lines.push(`${closePad}}`);
|
|
192
|
+
return lines.join("\n");
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Quote an object-property name when it is not a plain identifier (so keys with
|
|
196
|
+
* hyphens, dots, or other characters render as valid TS).
|
|
197
|
+
*/
|
|
198
|
+
function quotePropIfNeeded(name) {
|
|
199
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name))
|
|
200
|
+
return name;
|
|
201
|
+
return JSON.stringify(name);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* True when the schema REJECTS an empty object `{}` — i.e. the client's
|
|
205
|
+
* `rootInput: options.input ?? {}` fallback would fail validation, so `input`
|
|
206
|
+
* must be a compile-time-required option (issue #1442, P4). Derived from the
|
|
207
|
+
* shared descriptor so it matches the server validator exactly:
|
|
208
|
+
*
|
|
209
|
+
* - a declared top-level `type` that `{}` does not satisfy (anything but
|
|
210
|
+
* `object`, e.g. `string`/`array`, or a union with no `object` arm);
|
|
211
|
+
* - a top-level `enum` (a fresh `{}` is never an enum member);
|
|
212
|
+
* - `type: "object"` with a non-empty `required[]` (a missing required key).
|
|
213
|
+
*
|
|
214
|
+
* An all-optional object schema, or a schema with no top-level constraints,
|
|
215
|
+
* accepts `{}` → `input` is optional.
|
|
216
|
+
*/
|
|
217
|
+
export function schemaRejectsEmptyObject(schema) {
|
|
218
|
+
const d = describeSchema(schema);
|
|
219
|
+
// 1. Type gate: `{}` must satisfy at least one declared type.
|
|
220
|
+
if (d.types.length && !d.types.some((t) => isType(EMPTY_OBJECT, t))) {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
// 2. Enum gate: a JSON enum never contains a value reference-equal to `{}`,
|
|
224
|
+
// so any enum rejects it (mirrors `enum.includes(value)` at runtime).
|
|
225
|
+
if (d.enumValues && !d.enumValues.includes(EMPTY_OBJECT)) {
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
// 3. Object required: any required key is absent from `{}`.
|
|
229
|
+
if (d.object) {
|
|
230
|
+
for (const key of d.object.required) {
|
|
231
|
+
if (!(key in EMPTY_OBJECT))
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=schemaToTs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaToTs.js","sourceRoot":"","sources":["../../../../src/lib/workflow-codegen/schemaToTs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EACL,cAAc,EACd,MAAM,GAEP,MAAM,kCAAkC,CAAC;AAE1C,4EAA4E;AAC5E,MAAM,SAAS,GAA2B;IACxC,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,QAAQ;IACjB,OAAO,EAAE,SAAS;IAClB,IAAI,EAAE,MAAM;CACb,CAAC;AAEF,0EAA0E;AAC1E,MAAM,YAAY,GAA0B,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAE9D;;;;;GAKG;AACH,SAAS,qBAAqB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AAM,CAAC,QAAQ,EAAE,CAAC;YACpC,IAAI,CAAC,CAAC,GAAG,IAAI,YAAY,CAAC;gBAAE,OAAO,IAAI,CAAC;QAC1C,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "primitive-admin",
|
|
3
|
-
"version": "1.1.0-alpha.
|
|
3
|
+
"version": "1.1.0-alpha.45",
|
|
4
4
|
"description": "CLI for administering Primitive applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"gen:allowlist": "node scripts/gen-allowlist.mjs",
|
|
22
22
|
"gen:allowlist:check": "node scripts/gen-allowlist.mjs --check",
|
|
23
|
-
"
|
|
23
|
+
"gen:workflow-descriptor": "node scripts/gen-workflow-schema-descriptor.mjs",
|
|
24
|
+
"gen:workflow-descriptor:check": "node scripts/gen-workflow-schema-descriptor.mjs --check",
|
|
25
|
+
"prebuild": "node scripts/gen-allowlist.mjs && node scripts/gen-workflow-schema-descriptor.mjs",
|
|
24
26
|
"build": "tsc",
|
|
25
27
|
"typecheck": "tsc --noEmit",
|
|
26
28
|
"prepublishOnly": "pnpm run build",
|