@workerstack/clients 1.0.0-rc.29 → 1.0.0-rc.30
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/README.md +373 -14
- package/assets/installers/examples/github.config.ts +17 -19
- package/assets/installers/examples/goreleaser.config.ts +16 -18
- package/assets/installers/examples/raw.config.ts +13 -15
- package/bin/clients.js +87 -66
- package/bin/installgen.js +19 -10
- package/dist/cli.d.ts +4 -4
- package/dist/cli.js +18 -12
- package/dist/emit/cli/channels.d.ts +2 -2
- package/dist/emit/cli/channels.js +18 -13
- package/dist/emit/cli/commands.d.ts +1 -1
- package/dist/emit/cli/commands.js +14 -8
- package/dist/emit/cli/discovery.d.ts +0 -6
- package/dist/emit/cli/discovery.js +9 -7
- package/dist/emit/cli/flags.d.ts +1 -3
- package/dist/emit/cli/flags.js +11 -5
- package/dist/emit/cli/install.d.ts +0 -8
- package/dist/emit/cli/install.js +3 -3
- package/dist/emit/cli/layout.d.ts +1 -1
- package/dist/emit/cli/layout.js +13 -10
- package/dist/emit/cli/module.d.ts +6 -6
- package/dist/emit/cli/module.js +10 -10
- package/dist/emit/cli/release.d.ts +0 -11
- package/dist/emit/cli/release.js +4 -4
- package/dist/emit/cli/skills.js +3 -3
- package/dist/emit/csharp/errors.js +4 -4
- package/dist/emit/csharp/json.js +8 -8
- package/dist/emit/csharp/realtime.js +3 -3
- package/dist/emit/csharp/sdk-answer.js +4 -4
- package/dist/emit/csharp/sdk-body.js +8 -6
- package/dist/emit/csharp/sdk-client.d.ts +3 -3
- package/dist/emit/csharp/sdk-client.js +2 -2
- package/dist/emit/csharp/sdk-csproj.d.ts +7 -9
- package/dist/emit/csharp/sdk-csproj.js +10 -10
- package/dist/emit/csharp/sdk-doc.js +1 -1
- package/dist/emit/csharp/sdk-method.d.ts +0 -1
- package/dist/emit/csharp/sdk-method.js +10 -6
- package/dist/emit/csharp/sdk-plan.js +0 -1
- package/dist/emit/csharp/sdk.js +3 -3
- package/dist/emit/csharp/sse.d.ts +0 -2
- package/dist/emit/csharp/sse.js +7 -7
- package/dist/emit/csharp/transport.js +7 -7
- package/dist/emit/csharp/types-enum.js +3 -1
- package/dist/emit/csharp/types-messages.js +3 -3
- package/dist/emit/csharp/types-names.js +10 -12
- package/dist/emit/csharp/types-record.js +3 -3
- package/dist/emit/csharp/types-union.d.ts +1 -1
- package/dist/emit/csharp/types.d.ts +0 -1
- package/dist/emit/csharp/types.js +3 -3
- package/dist/emit/csharp/ws.js +2 -2
- package/dist/emit/go/errors.js +1 -1
- package/dist/emit/go/http.js +2 -2
- package/dist/emit/go/module-path.js +2 -2
- package/dist/emit/go/sdk-answer.js +1 -1
- package/dist/emit/go/sdk-body.js +11 -7
- package/dist/emit/go/sdk-client.js +1 -1
- package/dist/emit/go/sdk-method.js +2 -2
- package/dist/emit/go/sdk-mirror.js +2 -2
- package/dist/emit/go/sdk-opts.d.ts +0 -2
- package/dist/emit/go/sdk-opts.js +3 -3
- package/dist/emit/go/sdk.d.ts +1 -1
- package/dist/emit/go/sdk.js +9 -9
- package/dist/emit/go/sse.js +1 -1
- package/dist/emit/go/types-names.js +5 -0
- package/dist/emit/go/types-struct.d.ts +2 -1
- package/dist/emit/go/types-struct.js +1 -1
- package/dist/emit/go/types.js +9 -9
- package/dist/emit/go/ws.js +6 -6
- package/dist/emit/java/gradle.d.ts +7 -9
- package/dist/emit/java/gradle.js +8 -8
- package/dist/emit/java/json.js +1 -1
- package/dist/emit/java/layout-ast.js +1 -1
- package/dist/emit/java/layout-decl.js +5 -4
- package/dist/emit/java/layout-doc.js +13 -7
- package/dist/emit/java/layout-dot.js +1 -1
- package/dist/emit/java/layout-strings.js +4 -5
- package/dist/emit/java/layout-types.js +7 -5
- package/dist/emit/java/layout-visitor.js +4 -3
- package/dist/emit/java/layout.js +5 -4
- package/dist/emit/java/models-names.js +5 -4
- package/dist/emit/java/models-record.d.ts +0 -1
- package/dist/emit/java/models-record.js +3 -3
- package/dist/emit/java/models-union.d.ts +1 -1
- package/dist/emit/java/models-union.js +1 -1
- package/dist/emit/java/models.d.ts +1 -2
- package/dist/emit/java/models.js +25 -16
- package/dist/emit/java/realtime.d.ts +5 -17
- package/dist/emit/java/realtime.js +1 -1
- package/dist/emit/java/sdk-client.d.ts +6 -6
- package/dist/emit/java/sdk-client.js +1 -1
- package/dist/emit/java/sdk-decl.js +2 -2
- package/dist/emit/java/sdk-errors.js +1 -1
- package/dist/emit/java/sdk-headers.js +2 -2
- package/dist/emit/java/sdk-mirror.js +2 -2
- package/dist/emit/java/sdk-models.d.ts +1 -1
- package/dist/emit/java/sdk-models.js +7 -7
- package/dist/emit/java/sdk-op.d.ts +1 -10
- package/dist/emit/java/sdk-op.js +8 -8
- package/dist/emit/java/sdk-params.d.ts +5 -5
- package/dist/emit/java/sdk-params.js +3 -2
- package/dist/emit/java/sdk-plan.js +0 -1
- package/dist/emit/java/sdk.d.ts +1 -1
- package/dist/emit/java/sdk.js +14 -13
- package/dist/emit/java/sse.d.ts +2 -4
- package/dist/emit/java/sse.js +6 -6
- package/dist/emit/java/ws.d.ts +2 -2
- package/dist/emit/java/ws.js +3 -4
- package/dist/emit/mcp/annotations.js +8 -4
- package/dist/emit/mcp/files.js +9 -9
- package/dist/emit/mcp/mounted.js +2 -2
- package/dist/emit/mcp/output-schema.js +3 -3
- package/dist/emit/mcp/registry.d.ts +0 -2
- package/dist/emit/mcp/registry.js +5 -4
- package/dist/emit/mcp/runtime.js +2 -2
- package/dist/emit/mcp/schema.js +22 -11
- package/dist/emit/mcp/server.d.ts +7 -7
- package/dist/emit/mcp/server.js +7 -6
- package/dist/emit/mcp/tools.js +7 -6
- package/dist/emit/python/errors.js +3 -3
- package/dist/emit/python/realtime.js +2 -2
- package/dist/emit/python/sdk-answer.d.ts +3 -7
- package/dist/emit/python/sdk-answer.js +2 -2
- package/dist/emit/python/sdk-body.js +4 -5
- package/dist/emit/python/sdk-client.js +5 -5
- package/dist/emit/python/sdk-doc.js +2 -2
- package/dist/emit/python/sdk-method.js +5 -5
- package/dist/emit/python/sdk-plan.js +0 -1
- package/dist/emit/python/sdk-pyproject.d.ts +0 -2
- package/dist/emit/python/sdk-pyproject.js +8 -8
- package/dist/emit/python/sdk.d.ts +1 -1
- package/dist/emit/python/sdk.js +7 -7
- package/dist/emit/python/sse.d.ts +0 -2
- package/dist/emit/python/sse.js +2 -2
- package/dist/emit/python/transport.js +4 -4
- package/dist/emit/python/types-dict.d.ts +2 -2
- package/dist/emit/python/types-dict.js +1 -1
- package/dist/emit/python/types-names.js +2 -2
- package/dist/emit/python/types-union.js +1 -1
- package/dist/emit/python/types.js +3 -3
- package/dist/emit/python/ws.d.ts +2 -5
- package/dist/emit/python/ws.js +4 -4
- package/dist/emit/rust/errors.js +3 -3
- package/dist/emit/rust/http.js +4 -4
- package/dist/emit/rust/realtime.js +2 -2
- package/dist/emit/rust/sdk-body.js +8 -5
- package/dist/emit/rust/sdk-cargo.d.ts +16 -18
- package/dist/emit/rust/sdk-cargo.js +12 -12
- package/dist/emit/rust/sdk-client.d.ts +1 -1
- package/dist/emit/rust/sdk-client.js +3 -3
- package/dist/emit/rust/sdk-method.js +2 -2
- package/dist/emit/rust/sdk-opts.d.ts +2 -1
- package/dist/emit/rust/sdk-opts.js +2 -2
- package/dist/emit/rust/sdk-plan.js +0 -1
- package/dist/emit/rust/sdk.d.ts +1 -1
- package/dist/emit/rust/sdk.js +5 -5
- package/dist/emit/rust/sse.d.ts +0 -5
- package/dist/emit/rust/sse.js +2 -2
- package/dist/emit/rust/types-names.js +3 -3
- package/dist/emit/rust/types-struct.js +1 -1
- package/dist/emit/rust/types-union.d.ts +1 -1
- package/dist/emit/rust/types.js +7 -5
- package/dist/emit/rust/ws.d.ts +0 -11
- package/dist/emit/rust/ws.js +5 -5
- package/dist/emit/shared/escape.js +17 -17
- package/dist/emit/shared/format.js +1 -1
- package/dist/emit/shared/gofmt.js +1 -1
- package/dist/emit/shared/lang.js +10 -10
- package/dist/emit/shared/literals.d.ts +5 -0
- package/dist/emit/shared/literals.js +8 -0
- package/dist/emit/shared/metadata.d.ts +0 -6
- package/dist/emit/shared/metadata.js +8 -7
- package/dist/emit/shared/ruff.js +1 -1
- package/dist/emit/shared/rustfmt.js +1 -1
- package/dist/emit/shared/type-emitter.js +37 -20
- package/dist/emit/shared/unknown-type.constants.d.ts +2 -0
- package/dist/emit/shared/unknown-type.constants.js +2 -0
- package/dist/emit/ts/http.d.ts +0 -2
- package/dist/emit/ts/http.js +4 -4
- package/dist/emit/ts/package.d.ts +14 -12
- package/dist/emit/ts/package.js +9 -8
- package/dist/emit/ts/route-tree.js +2 -2
- package/dist/emit/ts/sdk-types-preamble.d.ts +1 -1
- package/dist/emit/ts/sdk-types-preamble.js +2 -2
- package/dist/emit/ts/sdk-types.js +11 -11
- package/dist/emit/ts/sdk.js +4 -4
- package/dist/emit/ts/service-map.d.ts +2 -1
- package/dist/emit/ts/tanstack-query.js +6 -6
- package/dist/emit/ts/type-gen.js +31 -18
- package/dist/emit/ts/webhooks.d.ts +0 -2
- package/dist/emit/ts/webhooks.js +3 -3
- package/dist/extract/chain.js +5 -4
- package/dist/extract/extract.js +5 -5
- package/dist/extract/serialize.d.ts +0 -2
- package/dist/extract/serialize.js +14 -15
- package/dist/extract/walk.js +15 -12
- package/dist/gen/args.js +9 -5
- package/dist/gen/formatters.js +5 -3
- package/dist/gen/shipped.js +2 -2
- package/dist/gen/targets.d.ts +2 -4
- package/dist/gen/targets.js +8 -4
- package/dist/gen/write.js +4 -2
- package/dist/index.js +2 -2
- package/dist/installers/cli.d.ts +4 -4
- package/dist/installers/cli.js +24 -24
- package/dist/installers/commands/build.d.ts +4 -4
- package/dist/installers/commands/build.js +2 -3
- package/dist/installers/commands/check.d.ts +2 -2
- package/dist/installers/commands/check.js +11 -10
- package/dist/installers/commands/init.d.ts +2 -2
- package/dist/installers/commands/init.js +1 -2
- package/dist/installers/config/define-config.d.ts +16 -16
- package/dist/installers/config/define-config.js +1 -1
- package/dist/installers/config/hooks/bun.d.ts +6 -6
- package/dist/installers/config/hooks/node.js +1 -1
- package/dist/installers/config/load.d.ts +6 -8
- package/dist/installers/config/load.js +7 -8
- package/dist/installers/config/resolve.d.ts +1 -2
- package/dist/installers/config/resolve.js +28 -33
- package/dist/installers/config/types.d.ts +6 -6
- package/dist/installers/config/types.js +0 -1
- package/dist/installers/render/quote.js +3 -5
- package/dist/installers/render/render.d.ts +6 -6
- package/dist/installers/render/render.js +26 -26
- package/dist/installers/ui.d.ts +4 -4
- package/dist/installers/ui.js +3 -3
- package/dist/ir/build.d.ts +3 -3
- package/dist/ir/build.js +8 -5
- package/dist/ir/load.js +9 -10
- package/dist/ir/model.d.ts +2 -1
- package/dist/ir/model.js +0 -1
- package/dist/ir/resolve-refs.js +2 -2
- package/dist/ir/resource-tree.js +2 -1
- package/dist/ir/route-tree-intern.js +1 -1
- package/dist/ir/schema.js +3 -3
- package/dist/ir/sdk-model.js +3 -3
- package/dist/ir/slots.js +13 -8
- package/dist/ir/streams.js +1 -1
- package/dist/release/bootstrap.d.ts +3 -3
- package/dist/release/bootstrap.js +7 -7
- package/dist/release/bump-args.js +4 -4
- package/dist/release/bump-command.d.ts +1 -1
- package/dist/release/bump-command.js +9 -10
- package/dist/release/bump.d.ts +5 -4
- package/dist/release/bump.js +2 -2
- package/dist/release/cli.d.ts +4 -4
- package/dist/release/cli.js +4 -4
- package/dist/release/codegen.js +1 -1
- package/dist/release/digest.js +5 -7
- package/dist/release/git.js +5 -8
- package/dist/release/load.js +3 -3
- package/dist/release/manifest.js +9 -10
- package/dist/release/oasdiff.d.ts +10 -15
- package/dist/release/oasdiff.js +5 -6
- package/dist/release/registries.d.ts +0 -11
- package/dist/release/registries.js +17 -17
- package/dist/release/release-state.d.ts +16 -13
- package/dist/release/release-workflow.js +5 -7
- package/dist/release/repo-settings.d.ts +2 -1
- package/dist/release/spec-gate.d.ts +0 -2
- package/dist/release/spec-gate.js +6 -10
- package/dist/release/sync-command.d.ts +2 -2
- package/dist/release/sync-command.js +1 -1
- package/dist/release/sync-tree.js +9 -13
- package/dist/release/sync.d.ts +6 -6
- package/dist/release/sync.js +12 -13
- package/dist/release/types.d.ts +22 -22
- package/dist/release/types.js +0 -1
- package/dist/release/version-files.d.ts +3 -4
- package/dist/release/version-files.js +4 -5
- package/dist/runtime/.oxfmtrc.jsonc +8 -0
- package/dist/runtime/mcp/files.ts +1 -1
- package/dist/runtime/mcp/http.ts +6 -2
- package/dist/runtime/mcp/listen.ts +1 -1
- package/dist/runtime/mcp/mount.ts +2 -2
- package/dist/runtime/mcp/protocol.ts +1 -1
- package/dist/runtime/ts-tanstack-query/query.ts +0 -2
- package/package.json +10 -7
- package/dist/emit/cli/index.d.ts +0 -1
- package/dist/emit/cli/index.js +0 -1
- package/dist/emit/csharp/index.d.ts +0 -1
- package/dist/emit/csharp/index.js +0 -1
- package/dist/emit/go/index.d.ts +0 -1
- package/dist/emit/go/index.js +0 -1
- package/dist/emit/java/index.d.ts +0 -2
- package/dist/emit/java/index.js +0 -2
- package/dist/emit/mcp/index.d.ts +0 -3
- package/dist/emit/mcp/index.js +0 -3
- package/dist/emit/python/index.d.ts +0 -1
- package/dist/emit/python/index.js +0 -1
- package/dist/emit/rust/index.d.ts +0 -1
- package/dist/emit/rust/index.js +0 -1
package/dist/emit/ts/package.js
CHANGED
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
import { transformSync } from "oxc-transform";
|
|
12
12
|
import { checked, issuesUrl, LICENSE, NPM_NAME, packageOn, README, SEMVER, shippedOf, } from "../shared/metadata.js";
|
|
13
13
|
import { generateSdk } from "./sdk.js";
|
|
14
|
-
|
|
14
|
+
// the optional peer the TanStack adapter's types import, as npm and JSR range it
|
|
15
15
|
const QUERY_CORE = "@tanstack/query-core";
|
|
16
16
|
const QUERY_CORE_RANGE = "^5.0.0";
|
|
17
|
-
|
|
17
|
+
// each importable file's kind and the subpath it is exported at; the others are reached through the index
|
|
18
18
|
const SUBPATHS = {
|
|
19
19
|
index: ".",
|
|
20
20
|
"tanstack-query": "./tanstack-query",
|
|
@@ -41,13 +41,14 @@ function exportsOf(files, stem, target) {
|
|
|
41
41
|
/** The package.json fields the metadata fills, each only when given: the TS SDK's and the MCP server's. */
|
|
42
42
|
export function npmFields(metadata) {
|
|
43
43
|
const { authors, description, homepage, keywords, license, repository } = metadata;
|
|
44
|
+
const author = authors?.[0];
|
|
44
45
|
return {
|
|
45
46
|
...(description === undefined ? {} : { description }),
|
|
46
47
|
...(keywords === undefined ? {} : { keywords }),
|
|
47
48
|
...(homepage === undefined ? {} : { homepage }),
|
|
48
49
|
...(repository === undefined ? {} : { bugs: issuesUrl(repository) }),
|
|
49
50
|
...(license === undefined ? {} : { license }),
|
|
50
|
-
...(
|
|
51
|
+
...(author === undefined ? {} : { author }),
|
|
51
52
|
...(repository === undefined
|
|
52
53
|
? {}
|
|
53
54
|
: { repository: { type: "git", url: `git+${repository.replace(/\.git$/, "")}.git` } }),
|
|
@@ -61,7 +62,7 @@ function packageJson(sdk, name, version, metadata) {
|
|
|
61
62
|
version,
|
|
62
63
|
...npmFields(metadata),
|
|
63
64
|
type: "module",
|
|
64
|
-
exports: exportsOf(sdk.files, sdk.stem, (file) => ({
|
|
65
|
+
exports: exportsOf(sdk.files, sdk.stem, (file) => ({ default: `./dist/${file}.js`, types: `./dist/${file}.d.ts` })),
|
|
65
66
|
files: ["dist"],
|
|
66
67
|
sideEffects: false,
|
|
67
68
|
engines: { node: ">=22" },
|
|
@@ -83,12 +84,12 @@ function jsrJson(sdk, name, version, metadata) {
|
|
|
83
84
|
...(metadata.license === undefined ? {} : { license: metadata.license }),
|
|
84
85
|
exports: exportsOf(sdk.files, sdk.stem, (file) => `./${file}.ts`),
|
|
85
86
|
...(tanstack ? { imports: { [QUERY_CORE]: `npm:${QUERY_CORE}@${QUERY_CORE_RANGE}` } } : {}),
|
|
86
|
-
|
|
87
|
+
// the runtime reads browser globals (FileList, WebSocket) where they exist: JSR checks it with them declared
|
|
87
88
|
compilerOptions: { lib: ["esnext", "dom", "dom.iterable"] },
|
|
88
|
-
|
|
89
|
+
// the sources' `.js` imports name their `.ts` files
|
|
89
90
|
unstable: ["sloppy-imports"],
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
// the README JSR shows and the license file beside the sources, each when the run ships it; npm packs both
|
|
92
|
+
// whatever `files` says
|
|
92
93
|
publish: { include: ["*.gen.ts", ...shippedNames(metadata)] },
|
|
93
94
|
};
|
|
94
95
|
return `${JSON.stringify(config, null, "\t")}\n`;
|
|
@@ -100,7 +100,7 @@ function emittedRoutes(tree) {
|
|
|
100
100
|
.filter((id) => leaves.has(id))
|
|
101
101
|
.map((id) => {
|
|
102
102
|
const entry = tree.routes[id];
|
|
103
|
-
|
|
103
|
+
// core's snapshot writes `mt: {}` for every route without meta: no key, no meta
|
|
104
104
|
const mt = entry.mt && Object.keys(entry.mt).length > 0 ? entry.mt : null;
|
|
105
105
|
if (mt !== null)
|
|
106
106
|
assertJsonMeta(id, mt);
|
|
@@ -218,7 +218,7 @@ export function generateRouteTree(tree) {
|
|
|
218
218
|
HEAD,
|
|
219
219
|
consts.length > 0 ? `${consts.join("\n")}\n` : "",
|
|
220
220
|
`export const tree: TreeNode = ${nodeText(tree.root, intern)}\n`,
|
|
221
|
-
|
|
221
|
+
// S typed by its entry: inferred from the literals, the union of entries would give each es the others' keys
|
|
222
222
|
`export const routes: RouteTree["routes"] = S<RouteTree["routes"][string]>({\n${routes.map((route) => routeLine(route, intern)).join(",\n")}\n})\n`,
|
|
223
223
|
shape ?? "",
|
|
224
224
|
meta.length > 0 ? `export const meta: ${metaType} = S({\n${meta.join(",\n")}\n})\n` : "",
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* document becomes those types.
|
|
6
6
|
*/
|
|
7
7
|
import type { TsRuntimeFeatures } from "./http.ts";
|
|
8
|
-
export declare const RESERVED: string[];
|
|
8
|
+
export declare const RESERVED: readonly string[];
|
|
9
9
|
/** The input fields every call takes, by transport, and the safe twin's types, for an API with `features`. */
|
|
10
10
|
export declare function shared(features: TsRuntimeFeatures): string;
|
|
11
11
|
/** The runtime types this text names, in order: a document with no realtime route uses neither
|
|
@@ -52,7 +52,7 @@ const OPTIONS = `export type HttpOptions = {
|
|
|
52
52
|
export type SseOptions = Omit<HttpOptions, "idempotencyKey"> & { lastEventId?: string }
|
|
53
53
|
export type WsOptions = { protocols?: string | string[]; reconnectToken?: string }
|
|
54
54
|
`;
|
|
55
|
-
|
|
55
|
+
// the error envelope, the call and stream shapes, and the safe mirror's opening
|
|
56
56
|
const CALLS = `
|
|
57
57
|
type ErrorField = { error_key: string; message: string; path: string }
|
|
58
58
|
|
|
@@ -88,7 +88,7 @@ type HttpStream<I, V, E = never> = {
|
|
|
88
88
|
/** \`client.safe\`: every REST and stream call answering a Result, never throwing an API answer; a connection as is. */
|
|
89
89
|
export type HttpSafe<T> = {
|
|
90
90
|
[K in keyof T]: `;
|
|
91
|
-
|
|
91
|
+
// the safe mirror's REST and stream arms
|
|
92
92
|
const SAFE_ARMS = ` ? {
|
|
93
93
|
(input: I & HttpOptions & { raw: true }): Promise<Result<Response, E>>
|
|
94
94
|
(...input: HttpArgs<I, HttpOptions & { raw?: false }>): Promise<Result<D, E, H>>
|
|
@@ -2,7 +2,7 @@ import { dispatchKey, irErrorEnvelope, isJsonMedia, mediaEssence, sdkModel, sseE
|
|
|
2
2
|
import { bodiesOf } from "../../ir/slots.js";
|
|
3
3
|
import { blockCommentText, tsString } from "../shared/escape.js";
|
|
4
4
|
import { compareCodeUnits, NameTable, pascalWords } from "../shared/lang.js";
|
|
5
|
-
import { quoteKey } from "../shared/literals.js";
|
|
5
|
+
import { optionalType, quoteKey } from "../shared/literals.js";
|
|
6
6
|
import { irToTs } from "../shared/type-emitter.js";
|
|
7
7
|
import { runtimeFeatures } from "./http.js";
|
|
8
8
|
import { importText, RESERVED, shared } from "./sdk-types-preamble.js";
|
|
@@ -15,7 +15,7 @@ function paramsPart(field, params, value) {
|
|
|
15
15
|
return null;
|
|
16
16
|
const entries = [...params]
|
|
17
17
|
.sort((a, b) => compareCodeUnits(a.name, b.name))
|
|
18
|
-
.map((p) => `${quoteKey(p.name)}${p
|
|
18
|
+
.map((p) => p.required === true ? `${quoteKey(p.name)}: ${value(p)}` : `${quoteKey(p.name)}?: ${optionalType(value(p))}`);
|
|
19
19
|
return { field, required: params.some((p) => p.required === true), type: objectText(entries) };
|
|
20
20
|
}
|
|
21
21
|
/** The input field a request body fills, by its media type. */
|
|
@@ -41,9 +41,9 @@ function bodyParts(op, ctx) {
|
|
|
41
41
|
const body = byField.get(field);
|
|
42
42
|
if (body === undefined)
|
|
43
43
|
return [];
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
// a multipart file part is a Blob, so a File passes; in JSON a binary field stays a string. the form's own
|
|
45
|
+
// shape is emitted, never its component's name: one named type cannot be `string` for JSON and `Blob` here,
|
|
46
|
+
// and a name is what hid the Blob until bit 22 b
|
|
47
47
|
const type = body.kind === "stream"
|
|
48
48
|
? "ArrayBuffer | Blob | ReadableStream<Uint8Array> | Uint8Array"
|
|
49
49
|
: field === "form"
|
|
@@ -55,10 +55,10 @@ function bodyParts(op, ctx) {
|
|
|
55
55
|
/** A call's input object, and whether it must be passed. */
|
|
56
56
|
function inputOf(op, ctx) {
|
|
57
57
|
const { types } = ctx;
|
|
58
|
-
|
|
58
|
+
// a header or cookie is text on the wire: an enum keeps its values, anything else is a string
|
|
59
59
|
const text = (p) => (p.schema.kind === "scalar" && p.schema.enum ? irToTs(p.schema, types) : "string");
|
|
60
60
|
const pathParams = op.pathParams.map((p) => ({ ...p, required: true }));
|
|
61
|
-
|
|
61
|
+
// a browser WebSocket sends no headers and sets no cookies, so a socket call takes neither
|
|
62
62
|
const socket = op.stream === "ws";
|
|
63
63
|
const parts = [
|
|
64
64
|
paramsPart("params", pathParams, (p) => irToTs(p.schema, types)),
|
|
@@ -68,7 +68,7 @@ function inputOf(op, ctx) {
|
|
|
68
68
|
...bodyParts(op, ctx),
|
|
69
69
|
].filter((part) => part !== null);
|
|
70
70
|
const ordered = [...parts.filter((p) => p.required), ...parts.filter((p) => !p.required)];
|
|
71
|
-
const fields = ordered.map((p) => `${p.field}${p.
|
|
71
|
+
const fields = ordered.map((p) => (p.required ? `${p.field}: ${p.type}` : `${p.field}?: ${optionalType(p.type)}`));
|
|
72
72
|
return { required: parts.some((p) => p.required), type: objectText(fields) };
|
|
73
73
|
}
|
|
74
74
|
/** The data a successful call resolves to: JSON from its schema, text as a string, bytes (A1), else null. */
|
|
@@ -152,7 +152,7 @@ function signature(name, op, ctx) {
|
|
|
152
152
|
return `${input.required ? "input" : "input?"}: ${type}`;
|
|
153
153
|
};
|
|
154
154
|
const key = quoteKey(name);
|
|
155
|
-
|
|
155
|
+
// a realtime op is one resumable connection of its event data; any other is typed by its transport
|
|
156
156
|
const { realtime, sse, websocket } = op.ir.extensions;
|
|
157
157
|
if (realtime)
|
|
158
158
|
return `${key}(${arg("RealtimeCallOptions")}): ${connectionType(op, ctx)}`;
|
|
@@ -167,7 +167,7 @@ function signature(name, op, ctx) {
|
|
|
167
167
|
const tail = headers === null ? (errors === null ? [] : [errors]) : [errors ?? "never", headers];
|
|
168
168
|
return `${key}: HttpCall<${[input.type, dataOf(op, ctx.types), ...tail].join(", ")}>`;
|
|
169
169
|
}
|
|
170
|
-
|
|
170
|
+
// every line break a comment splits at, as the shared comment writers split (escape.ts)
|
|
171
171
|
const LINE_BREAKS = /\r\n|\r|\n|\u2028|\u2029/;
|
|
172
172
|
/**
|
|
173
173
|
* `op`'s summary and description as a JSDoc block above its overloads at `indent`, a paragraph each, as Go, Rust,
|
|
@@ -222,7 +222,7 @@ function configText(name, server, runtimeConfig) {
|
|
|
222
222
|
/** The `${stem}.types.gen.ts` text of `ir` for the SDK `name`, importing the runtime module `runtime`. */
|
|
223
223
|
export function sdkTypesText(ir, name, runtime) {
|
|
224
224
|
const model = sdkModel(ir);
|
|
225
|
-
|
|
225
|
+
// the SDK named `SDK` declares `SDKConfig`, so the runtime's own is imported under another name
|
|
226
226
|
const aliases = name === "SDK" ? { SDKConfig: "HttpSDKConfig" } : {};
|
|
227
227
|
const runtimeConfig = aliases["SDKConfig"] ?? "SDKConfig";
|
|
228
228
|
const own = [
|
package/dist/emit/ts/sdk.js
CHANGED
|
@@ -11,12 +11,12 @@ import { MIRROR, mirrorNotice } from "../shared/mirror.js";
|
|
|
11
11
|
import { runtimeFeatures, runtimeSource } from "./http.js";
|
|
12
12
|
import { RESERVED } from "./sdk-types-preamble.js";
|
|
13
13
|
import { sdkTypesText } from "./sdk-types.js";
|
|
14
|
-
import { tanstackQueryText } from "./tanstack-query.js";
|
|
15
14
|
import { memberTree, serviceMapText } from "./service-map.js";
|
|
15
|
+
import { tanstackQueryText } from "./tanstack-query.js";
|
|
16
16
|
import { webhooksText, webhookTypeNames } from "./webhooks.js";
|
|
17
17
|
export { sdkMemberPaths, serviceMapOf } from "./service-map.js";
|
|
18
18
|
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
19
|
-
|
|
19
|
+
// the error classes and helpers a consumer imports from the SDK beside the class, by the transport that ships them
|
|
20
20
|
const RUNTIME_EXPORTS = {
|
|
21
21
|
core: [
|
|
22
22
|
"APIConnectionError",
|
|
@@ -153,7 +153,7 @@ export function generateSdk(document, options) {
|
|
|
153
153
|
refuseClash(name, (own) => (events.has(own) ? "the document's webhook events" : undefined));
|
|
154
154
|
const runtime = `${stem}.runtime.gen`;
|
|
155
155
|
const features = runtimeFeatures(ir);
|
|
156
|
-
|
|
156
|
+
// memberTree emits the root groups in code-unit order, each renamed in place
|
|
157
157
|
const emitted = [...memberTree(ir.tree).entries.keys()];
|
|
158
158
|
const groups = [...ir.tree.entries.keys()].sort(compareCodeUnits);
|
|
159
159
|
const notices = groups.flatMap((group, i) => mirrorNotice("ts", MIRROR, group, emitted[i]) ?? []);
|
|
@@ -166,7 +166,7 @@ export function generateSdk(document, options) {
|
|
|
166
166
|
};
|
|
167
167
|
if (ir.webhooks.some((webhook) => webhook.event))
|
|
168
168
|
files[`${stem}.webhook.gen.ts`] = webhooksText(ir, stem);
|
|
169
|
-
|
|
169
|
+
// the index never names it: its text must not vary by option, and it must never name query-core
|
|
170
170
|
if (options.tanstackQuery === true)
|
|
171
171
|
files[`${stem}.tanstack-query.gen.ts`] = tanstackQueryText(ir, name, stem);
|
|
172
172
|
return { files, notices, stem, version: ir.info.version };
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { IR, IRNamespace } from "../../ir/model.ts";
|
|
6
6
|
/** One operation as the runtime's service map holds it (`runtime/ts/sdk.ts` `ServiceEntry`). */
|
|
7
|
-
|
|
7
|
+
type ServiceEntry = {
|
|
8
8
|
accept?: string;
|
|
9
9
|
/** the operation declares no success body: a 2xx reads as null, its body only through `raw` */
|
|
10
10
|
empty?: true;
|
|
@@ -44,3 +44,4 @@ export declare function sdkMemberPaths(ir: IR): Map<string, string[]>;
|
|
|
44
44
|
export declare function serviceMapOf(ir: IR): Record<string, ServiceMapNode>;
|
|
45
45
|
/** The `${stem}.map.gen.ts` text: the service map as a `const`, importing the adapters its entries name. */
|
|
46
46
|
export declare function serviceMapText(ir: IR, runtime: string): string;
|
|
47
|
+
export {};
|
|
@@ -14,11 +14,11 @@ import { quoteKey } from "../shared/literals.js";
|
|
|
14
14
|
import { memberTree } from "./service-map.js";
|
|
15
15
|
/** The adapter's modules, each after the ones it imports, as `RUNTIME_MODULES` is ordered. */
|
|
16
16
|
const QUERY_MODULES = ["invalidate", "query"];
|
|
17
|
-
|
|
17
|
+
// an import of a module beside this one: now the same file, so the names are already in scope
|
|
18
18
|
const SIBLING_IMPORT = /^import\s+(?:type\s+)?\{[^}]*\}\s+from\s+"\.\/[\w-]+\.ts"\n/gm;
|
|
19
|
-
|
|
19
|
+
// an import of clients' own runtime, which the emitted file reads from `<stem>.runtime.gen`
|
|
20
20
|
const RUNTIME_IMPORT = /^import\s+(type\s+)?\{([^}]*)\}\s+from\s+"\.\.\/ts\/[\w-]+\.ts"\n/gm;
|
|
21
|
-
|
|
21
|
+
// query-core, which stays exactly as written: type-only, so nothing has to resolve at run time
|
|
22
22
|
const QUERY_CORE_IMPORT = /^import\s+type\s+\{([^}]*)\}\s+from\s+"@tanstack\/query-core"\n/gm;
|
|
23
23
|
/** The names a `{ a, type b }` clause lists, in code-unit order and once each. */
|
|
24
24
|
function namesOf(clauses) {
|
|
@@ -40,15 +40,15 @@ export function tanstackQuerySource(runtime) {
|
|
|
40
40
|
const bodies = QUERY_MODULES.map((name) => {
|
|
41
41
|
const text = readFileSync(new URL(`../../runtime/ts-tanstack-query/${name}.ts`, import.meta.url), "utf8");
|
|
42
42
|
return text
|
|
43
|
-
.
|
|
43
|
+
.replaceAll(QUERY_CORE_IMPORT, (_, named) => {
|
|
44
44
|
core.push(named);
|
|
45
45
|
return "";
|
|
46
46
|
})
|
|
47
|
-
.
|
|
47
|
+
.replaceAll(RUNTIME_IMPORT, (_, __, named) => {
|
|
48
48
|
runtimeNames.push(named);
|
|
49
49
|
return "";
|
|
50
50
|
})
|
|
51
|
-
.
|
|
51
|
+
.replaceAll(SIBLING_IMPORT, "")
|
|
52
52
|
.trim();
|
|
53
53
|
});
|
|
54
54
|
const imports = [
|
package/dist/emit/ts/type-gen.js
CHANGED
|
@@ -3,13 +3,13 @@ import { isJsonMedia, mediaEssence } from "../../ir/sdk-model.js";
|
|
|
3
3
|
import { bodiesOf } from "../../ir/slots.js";
|
|
4
4
|
import { tsString } from "../shared/escape.js";
|
|
5
5
|
import { compareCodeUnits } from "../shared/lang.js";
|
|
6
|
-
import { literalType, quoteKey } from "../shared/literals.js";
|
|
6
|
+
import { literalType, optionalType, quoteKey } from "../shared/literals.js";
|
|
7
7
|
import { statusKeyOf } from "../shared/status-keys.js";
|
|
8
8
|
import { irToTs } from "../shared/type-emitter.js";
|
|
9
9
|
import { componentNames } from "./sdk-types.js";
|
|
10
|
-
|
|
10
|
+
// the names the file declares, which a component schema never takes
|
|
11
11
|
const RESERVED = ["HttpContext", "HttpError", "HttpMeta", "Omit", "Partial", "Readonly", "Record", "Routes"];
|
|
12
|
-
|
|
12
|
+
// the v1 error-factory signature of a key without a schema (core errors.ts ErrorFactoryOpts)
|
|
13
13
|
const STANDARD_FACTORY = "(opts?: { cause?: unknown; fields?: Record<string, { error_key: string; message: string; path: string }[]>; headers?: Record<string, string>; vars?: Record<string, number | string> }) => HttpError";
|
|
14
14
|
const EXTRACTORS = [
|
|
15
15
|
["RouteInput", "input"],
|
|
@@ -32,7 +32,7 @@ function routesOf(ir, report) {
|
|
|
32
32
|
continue;
|
|
33
33
|
if (id === undefined)
|
|
34
34
|
missing.push(`${op.method} ${op.path}: no x-backend-route; left out of Routes`);
|
|
35
|
-
|
|
35
|
+
// `all()` and an optional param document one route as several operations: the first speaks for it
|
|
36
36
|
if (id !== undefined && !routes.has(id))
|
|
37
37
|
routes.set(id, op);
|
|
38
38
|
}
|
|
@@ -49,18 +49,24 @@ function paramsOf(pattern) {
|
|
|
49
49
|
function wireInput(op, ctx) {
|
|
50
50
|
const group = (params) => objectText([...params]
|
|
51
51
|
.sort((a, b) => compareCodeUnits(a.name, b.name))
|
|
52
|
-
.map((p) =>
|
|
52
|
+
.map((p) => p.required === true
|
|
53
|
+
? [quoteKey(p.name), ctx.resolve(p.schema)]
|
|
54
|
+
: [`${quoteKey(p.name)}?`, optionalType(ctx.resolve(p.schema))]));
|
|
53
55
|
const fields = [];
|
|
54
56
|
if (op.params.query.length > 0)
|
|
55
57
|
fields.push(["search", group(op.params.query)]);
|
|
56
58
|
if (op.params.header.length > 0)
|
|
57
59
|
fields.push(["headers", group(op.params.header)]);
|
|
58
|
-
|
|
60
|
+
// the IR sets `cookie` only when the operation declares one
|
|
59
61
|
if (op.params.cookie !== undefined)
|
|
60
62
|
fields.push(["cookies", group(op.params.cookie)]);
|
|
61
63
|
const body = bodiesOf(op)[0];
|
|
62
|
-
if (body?.kind === "stream")
|
|
63
|
-
fields.push([
|
|
64
|
+
if (body?.kind === "stream") {
|
|
65
|
+
fields.push([
|
|
66
|
+
"binary",
|
|
67
|
+
`{ readonly bytes: Uint8Array<ArrayBuffer>; readonly type: ${tsString(body.contentType)} }`,
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
64
70
|
else if (body !== undefined) {
|
|
65
71
|
const essence = mediaEssence(body.contentType);
|
|
66
72
|
const field = isJsonMedia(body.contentType) ? "json" : "form";
|
|
@@ -136,12 +142,12 @@ function errorsOf(op, ctx) {
|
|
|
136
142
|
* Without an extracted declaration (no ts-morph, or an app that declares no meta) the literals are all there is.
|
|
137
143
|
*/
|
|
138
144
|
function ctxMeta(extracted, literal) {
|
|
139
|
-
|
|
145
|
+
// with nothing extracted, the tree's literals are all there is
|
|
140
146
|
if (extracted === null || extracted === undefined)
|
|
141
147
|
return literal;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
148
|
+
// the OpenAPI keys the declared meta does not redefine: a consumer that types `security` its own way holds
|
|
149
|
+
// that type in the handler, and intersecting backend's would conflict with it. An app that declares none
|
|
150
|
+
// holds them all, each widened by \`.meta()\`
|
|
145
151
|
const declared = extracted.meta;
|
|
146
152
|
return declared === null ? "Partial<HttpMeta>" : `Partial<Omit<HttpMeta, keyof ${declared}>> & ${declared}`;
|
|
147
153
|
}
|
|
@@ -168,7 +174,7 @@ function routeEntry(id, op, options, ctx) {
|
|
|
168
174
|
const params = ctxParams(extracted, id, pattern);
|
|
169
175
|
const { factories, fields } = errorsOf(op, ctx);
|
|
170
176
|
const mt = options.tree.routes[id]?.mt;
|
|
171
|
-
|
|
177
|
+
// the values the tree carries, as literals: what a consumer reads a route's configuration from
|
|
172
178
|
const literal = mt && Object.keys(mt).length > 0 ? literalType(mt) : "{}";
|
|
173
179
|
const output = outputOf(op, ctx);
|
|
174
180
|
const own = [];
|
|
@@ -209,7 +215,7 @@ function baseCtx(extracted, name) {
|
|
|
209
215
|
*/
|
|
210
216
|
function wsCtx(op, id, options, ctx) {
|
|
211
217
|
const own = [];
|
|
212
|
-
|
|
218
|
+
// the upgrade request's validated search, headers and cookies, as core's `WithInput` holds them
|
|
213
219
|
const input = wireInput(op, ctx).filter(([key]) => key === "search" || key === "headers" || key === "cookies");
|
|
214
220
|
if (input.length > 0)
|
|
215
221
|
own.push(["readonly input", objectText(input)]);
|
|
@@ -275,15 +281,21 @@ export function generateTypes(options) {
|
|
|
275
281
|
const ir = toIR(options.document, { deriveOperationIds: true });
|
|
276
282
|
const routes = routesOf(ir, options.report);
|
|
277
283
|
const extra = Object.keys(options.extracted?.routes ?? {}).filter((id) => !routes.has(id));
|
|
278
|
-
for (const id of extra.sort(compareCodeUnits))
|
|
284
|
+
for (const id of extra.sort(compareCodeUnits)) {
|
|
279
285
|
options.report(`${id}: extracted ctx additions match no documented operation (internal and filtered routes are not documented); not typed`);
|
|
286
|
+
}
|
|
280
287
|
const name = options.baseCtxName ?? "BaseCtx";
|
|
281
288
|
const names = { refName: componentNames(ir, [...RESERVED, name]) };
|
|
282
289
|
const ctx = { names, resolve: (schema) => irToTs(schema, names) };
|
|
283
290
|
const hasErrors = [...routes.values()].some((op) => Object.keys(op.extensions.errors ?? {}).length > 0);
|
|
284
|
-
|
|
291
|
+
// HttpMeta once a route's ctx reads it: with anything extracted, the ctx's meta is built from its keys
|
|
285
292
|
const readsMeta = options.extracted !== null && options.extracted !== undefined && routes.size > 0;
|
|
286
|
-
const imports = [
|
|
293
|
+
const imports = [
|
|
294
|
+
"HttpContext",
|
|
295
|
+
"WithOutput",
|
|
296
|
+
...(hasErrors ? ["HttpError"] : []),
|
|
297
|
+
...(readsMeta ? ["HttpMeta"] : []),
|
|
298
|
+
];
|
|
287
299
|
const out = [`import type { ${imports.join(", ")} } from "@workerstack/backend/http"`];
|
|
288
300
|
out.push(baseCtx(options.extracted, name));
|
|
289
301
|
const slot = operationSlot(ir, options, ctx);
|
|
@@ -292,7 +304,8 @@ export function generateTypes(options) {
|
|
|
292
304
|
for (const schema of Object.keys(ir.schemas).sort(compareCodeUnits))
|
|
293
305
|
out.push(`type ${names.refName?.(schema)} = ${ctx.resolve(ir.schemas[schema])}`);
|
|
294
306
|
out.push(routesType(routes, options, ctx));
|
|
295
|
-
for (const [type, field] of EXTRACTORS)
|
|
307
|
+
for (const [type, field] of EXTRACTORS) {
|
|
296
308
|
out.push(`export type ${type}<TPath extends keyof Routes, TMethod extends keyof Routes[TPath] & string> = Routes[TPath][TMethod] extends { ${field}: infer T } ? T : never`);
|
|
309
|
+
}
|
|
297
310
|
return `${out.join("\n\n")}\n`;
|
|
298
311
|
}
|
|
@@ -5,8 +5,6 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { IR } from "../../ir/model.ts";
|
|
7
7
|
import { type TypeOptions } from "../shared/type-emitter.ts";
|
|
8
|
-
/** The catch-all event and the union, declared beside the events. */
|
|
9
|
-
export declare const WEBHOOK_TYPES: readonly ["UnknownWebhookEvent", "WebhookEvent"];
|
|
10
8
|
/** The type names `ir`'s events declare, for the component names to stay clear of; none without events. */
|
|
11
9
|
export declare function webhookTypeNames(ir: IR): string[];
|
|
12
10
|
/** The event types and their union, for `types.gen.ts`; empty for a document without events. */
|
package/dist/emit/ts/webhooks.js
CHANGED
|
@@ -3,7 +3,7 @@ import { compareCodeUnits, pascalWords } from "../shared/lang.js";
|
|
|
3
3
|
import { irToTs } from "../shared/type-emitter.js";
|
|
4
4
|
import { webhookSource } from "./http.js";
|
|
5
5
|
/** The catch-all event and the union, declared beside the events. */
|
|
6
|
-
|
|
6
|
+
const WEBHOOK_TYPES = ["UnknownWebhookEvent", "WebhookEvent"];
|
|
7
7
|
/** `ir`'s events in code-unit order of their type, each named; two that name one type throw, naming both. */
|
|
8
8
|
function namedEvents(ir) {
|
|
9
9
|
const named = ir.webhooks
|
|
@@ -45,7 +45,7 @@ export type WebhookEvent = ${[...named.map(({ name }) => name), "UnknownWebhookE
|
|
|
45
45
|
|
|
46
46
|
`;
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
// the runtime verifier's untyped core, which `verifyWebhook` wraps: kept inside the file, out of a receiver's imports
|
|
49
49
|
const INTERNAL = /^export ((?:async function verifyStandardWebhook|type VerifiedWebhook)\b)/gm;
|
|
50
50
|
/**
|
|
51
51
|
* The `${stem}.webhook.gen.ts` text of `ir`: the runtime's verifier, its untyped core unexported, then `verifyWebhook`
|
|
@@ -60,7 +60,7 @@ export function webhooksText(ir, stem) {
|
|
|
60
60
|
|
|
61
61
|
export type { ${types.join(", ")} } from "./${stem}.types.gen.js"
|
|
62
62
|
|
|
63
|
-
${webhookSource().
|
|
63
|
+
${webhookSource().replaceAll(INTERNAL, "$1")}
|
|
64
64
|
|
|
65
65
|
const KNOWN: ReadonlySet<string> = new Set([${known.join(", ")}])
|
|
66
66
|
|
package/dist/extract/chain.js
CHANGED
|
@@ -16,7 +16,7 @@ function declaredRef(decl, ser) {
|
|
|
16
16
|
const variable = checker.getShorthandAssignmentValueSymbol(decl)?.valueDeclaration;
|
|
17
17
|
if (variable === undefined || !compiler.isBindingElement(variable))
|
|
18
18
|
return ser.refFromDeclaration(variable);
|
|
19
|
-
|
|
19
|
+
// `const { db } = open()`: the pattern's own declaration; a parameter or a nested pattern names nothing
|
|
20
20
|
const holder = variable.parent.parent;
|
|
21
21
|
const init = compiler.isVariableDeclaration(holder) ? holder.initializer : undefined;
|
|
22
22
|
const source = init === undefined ? undefined : checker.getTypeAtLocation(init).getProperty(decl.name.text);
|
|
@@ -28,7 +28,7 @@ function declaredRef(decl, ser) {
|
|
|
28
28
|
/** The props of `ctx` the framework class does not own, in the ctx's order. */
|
|
29
29
|
export function addedProps(ctx, owner, ser) {
|
|
30
30
|
const { checker, compiler } = ser;
|
|
31
|
-
|
|
31
|
+
// a prop with no declaration (a mapped type's) is no member of anything
|
|
32
32
|
const owned = (decls) => owner !== undefined && decls.length > 0 && decls.every((d) => d.parent === owner);
|
|
33
33
|
return ctx
|
|
34
34
|
.getProperties()
|
|
@@ -36,7 +36,7 @@ export function addedProps(ctx, owner, ser) {
|
|
|
36
36
|
.map((p) => {
|
|
37
37
|
const decls = p.declarations ?? [];
|
|
38
38
|
const optional = (p.flags & compiler.SymbolFlags.Optional) !== 0;
|
|
39
|
-
|
|
39
|
+
// a prop several middleware add is their intersection: only the checker's type says it
|
|
40
40
|
const ref = decls.length === 1 ? declaredRef(decls[0], ser) : undefined;
|
|
41
41
|
const type = checker.getTypeOfSymbol(p);
|
|
42
42
|
return { name: p.getName(), optional, type: ref ?? (optional ? ser.optional(type) : ser.text(type)) };
|
|
@@ -54,8 +54,9 @@ export function routeAdditions(found, ser, options) {
|
|
|
54
54
|
const key = JSON.stringify(props);
|
|
55
55
|
for (const id of ids) {
|
|
56
56
|
const seen = byId.get(id);
|
|
57
|
-
if (seen === undefined)
|
|
57
|
+
if (seen === undefined) {
|
|
58
58
|
byId.set(id, { key, props });
|
|
59
|
+
}
|
|
59
60
|
else if (seen.key !== key && !conflicted.has(id)) {
|
|
60
61
|
conflicted.add(id);
|
|
61
62
|
report(`${JSON.stringify(id)} is registered with different middleware; ${SKIPPED}`);
|
package/dist/extract/extract.js
CHANGED
|
@@ -18,9 +18,9 @@ import { compareCodeUnits } from "../emit/shared/lang.js";
|
|
|
18
18
|
import { addedProps, frameworkClass, routeAdditions, SKIPPED } from "./chain.js";
|
|
19
19
|
import { Serializer } from "./serialize.js";
|
|
20
20
|
import { loadEntry, registrations } from "./walk.js";
|
|
21
|
-
|
|
21
|
+
// the type-only members every backend app carries
|
|
22
22
|
const APP_MEMBERS = ["$ctx", "$env"];
|
|
23
|
-
|
|
23
|
+
// the declared meta, read when the app carries it: an app from an older backend has no $meta and still extracts
|
|
24
24
|
const META_MEMBER = "$meta";
|
|
25
25
|
/** The nearest named package.json above `file` (one without a name only marks a module type). */
|
|
26
26
|
function nearestPackage(file, cache) {
|
|
@@ -111,7 +111,7 @@ function moduleSpecifier(file, outputDir, cache) {
|
|
|
111
111
|
}
|
|
112
112
|
/** `import("/abs/file.ts").X` → a specifier the output resolves (lib types are never referenced: they are global). */
|
|
113
113
|
function resolvable(text, outputDir, cache) {
|
|
114
|
-
return text.
|
|
114
|
+
return text.replaceAll(/import\("([^"]+)"\)\./g, (_, file) => `import("${moduleSpecifier(file, outputDir, cache)}").`);
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
117
|
* Each registration with its route ids, methods by paths. A registration whose path is no literal, or a path the
|
|
@@ -187,8 +187,8 @@ export function extractTypes(options) {
|
|
|
187
187
|
const base = new Set(middleware.map((p) => p.name));
|
|
188
188
|
const found = routed(registrations(entry, report), routeId, report);
|
|
189
189
|
const routes = routeAdditions(found, ser, { base, owner, report });
|
|
190
|
-
|
|
191
|
-
|
|
190
|
+
// what `metaType<AppMeta>()` declared, written out: the ctx's meta is this and the OpenAPI keys, never the tree's
|
|
191
|
+
// literal values, so a handler may pass its own ctx to a function typed with the generated ctx (bit 22 a)
|
|
192
192
|
const meta = metaType === undefined ? "{}" : ser.shape(metaType);
|
|
193
193
|
const cache = new Map();
|
|
194
194
|
const text = (t) => resolvable(t, options.outputDir, cache);
|
|
@@ -11,8 +11,6 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import type { ts } from "ts-morph";
|
|
13
13
|
export type Compiler = typeof ts;
|
|
14
|
-
/** Lib types every program has in scope: written by name, never imported. */
|
|
15
|
-
export declare function isLibFile(fileName: string): boolean;
|
|
16
14
|
export declare class Serializer {
|
|
17
15
|
#private;
|
|
18
16
|
readonly checker: ts.TypeChecker;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { quoteKey } from "../emit/shared/literals.js";
|
|
1
|
+
import { optionalType, quoteKey } from "../emit/shared/literals.js";
|
|
2
2
|
import { arrayOf, intersectionOf, unionOf } from "../emit/shared/type-emitter.js";
|
|
3
3
|
/** Lib types every program has in scope: written by name, never imported. */
|
|
4
|
-
|
|
4
|
+
function isLibFile(fileName) {
|
|
5
5
|
return /(?:^|[\\/])lib\.[\w.-]*\.d\.ts$/.test(fileName) || /[\\/]typescript[\\/]lib[\\/]/.test(fileName);
|
|
6
6
|
}
|
|
7
7
|
/** Whether a declaring module exports `sym` (a declaration file's types are always reachable). */
|
|
@@ -15,7 +15,7 @@ function isExported(sym, file, checker) {
|
|
|
15
15
|
function isScript(file) {
|
|
16
16
|
return file.externalModuleIndicator === undefined;
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
// checked in order: `boolean` is a union of its literals, so it is named before unions are split
|
|
19
19
|
const KEYWORDS = [
|
|
20
20
|
["String", "string"],
|
|
21
21
|
["Number", "number"],
|
|
@@ -30,7 +30,7 @@ const KEYWORDS = [
|
|
|
30
30
|
["Unknown", "unknown"],
|
|
31
31
|
["Any", "unknown"],
|
|
32
32
|
["NonPrimitive", "object"],
|
|
33
|
-
|
|
33
|
+
// deferred generic forms (`T extends U ? X : Y`, `T[K]`, `keyof T`) are written as their bound
|
|
34
34
|
["StringMapping", "string"],
|
|
35
35
|
["Conditional", "unknown"],
|
|
36
36
|
["Substitution", "unknown"],
|
|
@@ -41,7 +41,7 @@ export class Serializer {
|
|
|
41
41
|
checker;
|
|
42
42
|
compiler;
|
|
43
43
|
#active = new Set();
|
|
44
|
-
|
|
44
|
+
// type parameters of the signatures being written: inside them a parameter keeps its name
|
|
45
45
|
#bound = new Set();
|
|
46
46
|
constructor(checker, compiler) {
|
|
47
47
|
this.checker = checker;
|
|
@@ -57,9 +57,8 @@ export class Serializer {
|
|
|
57
57
|
while (sym !== undefined && sym.flags & compiler.SymbolFlags.Alias)
|
|
58
58
|
sym = checker.getAliasedSymbol(sym);
|
|
59
59
|
const file = sym?.declarations?.[0]?.getSourceFile();
|
|
60
|
-
if (sym === undefined || file === undefined || isLibFile(file.fileName) || !isExported(sym, file, checker))
|
|
60
|
+
if (sym === undefined || file === undefined || isLibFile(file.fileName) || !isExported(sym, file, checker))
|
|
61
61
|
return undefined;
|
|
62
|
-
}
|
|
63
62
|
const args = (typeRef.typeArguments ?? []).map((a) => this.text(checker.getTypeFromTypeNode(a)));
|
|
64
63
|
return `import("${file.fileName}").${sym.getName()}${args.length > 0 ? `<${args.join(", ")}>` : ""}`;
|
|
65
64
|
}
|
|
@@ -79,7 +78,7 @@ export class Serializer {
|
|
|
79
78
|
const globalName = this.#globalName(sym, decl);
|
|
80
79
|
if (globalName !== null)
|
|
81
80
|
return this.#withArgs(globalName, type, decl);
|
|
82
|
-
|
|
81
|
+
// an ambient module's member has no import() path here, and import() of an unexported type is `any`
|
|
83
82
|
if (isScript(file) || !isExported(sym, file, this.checker))
|
|
84
83
|
return null;
|
|
85
84
|
return this.#withArgs(`import("${file.fileName}").${sym.getName()}`, type, decl);
|
|
@@ -108,7 +107,7 @@ export class Serializer {
|
|
|
108
107
|
if (compiler.isSourceFile(n))
|
|
109
108
|
global = isScript(n);
|
|
110
109
|
}
|
|
111
|
-
|
|
110
|
+
// a `declare global` member is qualified as `global.Name`; in scope it is `Name`
|
|
112
111
|
const name = this.checker.getFullyQualifiedName(sym).replace(/^global\./, "");
|
|
113
112
|
return global && /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(name) ? name : null;
|
|
114
113
|
}
|
|
@@ -126,7 +125,7 @@ export class Serializer {
|
|
|
126
125
|
}
|
|
127
126
|
#serialize(type) {
|
|
128
127
|
const { compiler } = this;
|
|
129
|
-
|
|
128
|
+
// an enum is nominal: its name when it can be referenced
|
|
130
129
|
const enumRef = (type.getSymbol()?.flags ?? 0) & compiler.SymbolFlags.Enum ? this.asRef(type) : null;
|
|
131
130
|
return (enumRef ??
|
|
132
131
|
this.#keyword(type) ??
|
|
@@ -157,7 +156,7 @@ export class Serializer {
|
|
|
157
156
|
/** A template literal type as written: its text escaped where a template would read it, each hole serialized. */
|
|
158
157
|
#template(type) {
|
|
159
158
|
const holes = type.types.map((hole) => `\${${this.serialize(hole)}}`);
|
|
160
|
-
const texts = type.texts.map((text) => text.
|
|
159
|
+
const texts = type.texts.map((text) => text.replaceAll(/[`\\]|\$(?=\{)/g, (c) => `\\${c}`));
|
|
161
160
|
return `\`${texts.map((text, i) => text + (holes[i] ?? "")).join("")}\``;
|
|
162
161
|
}
|
|
163
162
|
/** By name inside the signature that declares it, else its constraint, else unknown. */
|
|
@@ -218,20 +217,20 @@ export class Serializer {
|
|
|
218
217
|
const { checker, compiler } = this;
|
|
219
218
|
const index = type.getStringIndexType();
|
|
220
219
|
const props = type.getProperties().filter((p) => isPublicName(p.getName()));
|
|
221
|
-
if (index !== undefined && props.length === 0 && type.getCallSignatures().length === 0)
|
|
220
|
+
if (index !== undefined && props.length === 0 && type.getCallSignatures().length === 0)
|
|
222
221
|
return `Record<string, ${this.serialize(index)}>`;
|
|
223
|
-
}
|
|
224
222
|
const M = compiler.ModifierFlags;
|
|
225
223
|
const entries = [];
|
|
226
224
|
for (const p of props) {
|
|
227
|
-
|
|
225
|
+
// a mapped type's prop has no declaration
|
|
228
226
|
const decls = p.getDeclarations() ?? [];
|
|
229
227
|
if (decls.some((d) => compiler.getCombinedModifierFlags(d) & (M.Private | M.Protected)))
|
|
230
228
|
continue;
|
|
231
229
|
const readonly = decls.some((d) => compiler.getCombinedModifierFlags(d) & M.Readonly);
|
|
232
230
|
const optional = (p.flags & compiler.SymbolFlags.Optional) !== 0;
|
|
233
231
|
const member = checker.getTypeOfSymbol(p);
|
|
234
|
-
|
|
232
|
+
// `| undefined` said out loud: under exactOptionalPropertyTypes a bare `k?: T` refuses the undefined zod infers
|
|
233
|
+
const value = optional ? optionalType(this.optional(member)) : this.serialize(member);
|
|
235
234
|
entries.push(`${readonly ? "readonly " : ""}${quoteKey(p.getName())}${optional ? "?" : ""}: ${value}`);
|
|
236
235
|
}
|
|
237
236
|
if (index !== undefined)
|