@workerstack/clients 1.0.0-rc.28 → 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 +403 -21
- 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 +39 -13
- 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 +18 -8
- 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 +8 -4
- package/dist/emit/csharp/errors.js +4 -4
- package/dist/emit/csharp/json.js +8 -8
- package/dist/emit/csharp/realtime.d.ts +1 -8
- package/dist/emit/csharp/realtime.js +26 -26
- package/dist/emit/csharp/sdk-answer.d.ts +10 -2
- 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 +19 -8
- 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 -13
- package/dist/emit/csharp/sse.js +92 -11
- package/dist/emit/csharp/transport.js +9 -9
- package/dist/emit/csharp/types-enum.js +3 -1
- package/dist/emit/csharp/types-messages.d.ts +47 -0
- package/dist/emit/csharp/types-messages.js +172 -0
- package/dist/emit/csharp/types-names.js +13 -14
- 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.d.ts +0 -15
- package/dist/emit/csharp/ws.js +26 -5
- 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.d.ts +24 -1
- 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.d.ts +3 -0
- package/dist/emit/go/sdk-method.js +18 -12
- 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.d.ts +2 -10
- package/dist/emit/go/sse.js +88 -13
- 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-union.d.ts +3 -2
- package/dist/emit/go/types-union.js +2 -0
- package/dist/emit/go/types.d.ts +6 -0
- package/dist/emit/go/types.js +20 -9
- package/dist/emit/go/ws.d.ts +4 -11
- package/dist/emit/go/ws.js +215 -59
- package/dist/emit/java/gradle.d.ts +7 -9
- package/dist/emit/java/gradle.js +8 -8
- package/dist/emit/java/json.d.ts +3 -0
- package/dist/emit/java/json.js +1 -1
- package/dist/emit/java/layout-ast.d.ts +4 -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.d.ts +2 -0
- package/dist/emit/java/models-names.js +6 -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 +13 -2
- package/dist/emit/java/models-union.js +12 -6
- package/dist/emit/java/models.d.ts +5 -1
- package/dist/emit/java/models.js +44 -20
- package/dist/emit/java/realtime.d.ts +34 -26
- package/dist/emit/java/realtime.js +45 -10
- 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 +7 -6
- 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 +9 -9
- 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 +12 -13
- package/dist/emit/java/sse.js +105 -6
- package/dist/emit/java/ws.d.ts +48 -9
- package/dist/emit/java/ws.js +88 -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.d.ts +4 -2
- package/dist/emit/mcp/output-schema.js +58 -14
- 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.d.ts +9 -6
- package/dist/emit/python/realtime.js +22 -20
- package/dist/emit/python/sdk-answer.d.ts +8 -8
- 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.d.ts +1 -1
- package/dist/emit/python/sdk-method.js +38 -11
- 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 +28 -8
- package/dist/emit/python/sse.d.ts +0 -7
- package/dist/emit/python/sse.js +36 -6
- 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.d.ts +7 -0
- package/dist/emit/python/types.js +23 -6
- package/dist/emit/python/ws.d.ts +21 -10
- package/dist/emit/python/ws.js +69 -3
- package/dist/emit/rust/errors.js +3 -3
- package/dist/emit/rust/http.js +4 -4
- package/dist/emit/rust/realtime.d.ts +7 -5
- package/dist/emit/rust/realtime.js +15 -18
- 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 +3 -3
- 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 +11 -12
- package/dist/emit/rust/sse.js +54 -8
- 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 +10 -2
- package/dist/emit/rust/types-union.js +4 -0
- package/dist/emit/rust/types.d.ts +9 -0
- package/dist/emit/rust/types.js +20 -12
- package/dist/emit/rust/ws.d.ts +22 -15
- package/dist/emit/rust/ws.js +60 -10
- 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 +6 -2
- package/dist/emit/ts/sdk-types.js +59 -20
- package/dist/emit/ts/sdk.js +6 -6
- package/dist/emit/ts/service-map.d.ts +10 -3
- package/dist/emit/ts/service-map.js +30 -2
- package/dist/emit/ts/tanstack-query.js +6 -6
- package/dist/emit/ts/type-gen.js +36 -21
- 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 +12 -5
- package/dist/ir/load.js +9 -10
- package/dist/ir/model.d.ts +16 -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.d.ts +25 -1
- package/dist/ir/sdk-model.js +38 -8
- package/dist/ir/slots.d.ts +1 -1
- package/dist/ir/slots.js +22 -11
- package/dist/ir/streams.d.ts +16 -0
- package/dist/ir/streams.js +40 -0
- 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/cli/socket.go +107 -13
- package/dist/runtime/cli/stream.go +18 -11
- package/dist/runtime/csharp/Adapters.cs +12 -11
- package/dist/runtime/csharp/Events.cs +22 -17
- package/dist/runtime/csharp/Realtime.cs +50 -23
- package/dist/runtime/csharp/RealtimeLoop.cs +1 -1
- package/dist/runtime/csharp/TransportSse.cs +3 -3
- package/dist/runtime/csharp/Ws.cs +1 -1
- package/dist/runtime/csharp/WsDial.cs +19 -1
- package/dist/runtime/csharp/WsTyped.cs +109 -0
- package/dist/runtime/go/realtime.go +125 -41
- package/dist/runtime/go/realtime_loop.go +21 -18
- package/dist/runtime/go/result.go +2 -1
- package/dist/runtime/go/sse.go +11 -0
- package/dist/runtime/go/stream.go +29 -13
- package/dist/runtime/go/transport.go +15 -21
- package/dist/runtime/go/transport_longpoll.go +3 -4
- package/dist/runtime/go/transport_sse.go +6 -6
- package/dist/runtime/go/ws.go +157 -61
- package/dist/runtime/java/EventStream.java +47 -29
- package/dist/runtime/java/Exchange.java +5 -5
- package/dist/runtime/java/Flight.java +3 -3
- package/dist/runtime/java/Realtime.java +45 -11
- package/dist/runtime/java/ResumableConnection.java +77 -32
- package/dist/runtime/java/Socket.java +8 -4
- package/dist/runtime/java/SseAdapter.java +1 -1
- package/dist/runtime/java/TypedWebSocket.java +81 -24
- package/dist/runtime/java/WsAdapter.java +4 -2
- package/dist/runtime/java/WsDial.java +12 -5
- 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/python/events.py +25 -11
- package/dist/runtime/python/operation.py +6 -1
- package/dist/runtime/python/realtime.py +47 -33
- package/dist/runtime/python/send_async.py +1 -0
- package/dist/runtime/python/transport_sse.py +2 -2
- package/dist/runtime/python/transport_ws.py +1 -1
- package/dist/runtime/python/ws.py +100 -39
- package/dist/runtime/rust/events.rs +64 -41
- package/dist/runtime/rust/realtime.rs +80 -34
- package/dist/runtime/rust/realtime_loop.rs +37 -12
- package/dist/runtime/rust/sse.rs +3 -2
- package/dist/runtime/rust/transport_sse.rs +1 -1
- package/dist/runtime/rust/transport_ws.rs +3 -2
- package/dist/runtime/rust/ws.rs +122 -50
- package/dist/runtime/ts/realtime-connection.ts +22 -0
- package/dist/runtime/ts/realtime-transports.ts +13 -4
- package/dist/runtime/ts/realtime.ts +27 -3
- package/dist/runtime/ts/sdk-realtime.ts +1 -1
- package/dist/runtime/ts/sdk-sse.ts +31 -14
- package/dist/runtime/ts/sdk-ws.ts +2 -1
- package/dist/runtime/ts/sdk.ts +9 -2
- package/dist/runtime/ts/sse.ts +8 -0
- package/dist/runtime/ts/ws.ts +88 -50
- package/dist/runtime/ts-tanstack-query/query.ts +0 -2
- package/package.json +12 -40
- 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
|
@@ -22,9 +22,11 @@ export const RESERVED = [
|
|
|
22
22
|
"Result",
|
|
23
23
|
"SDKConfig",
|
|
24
24
|
"SSEEvent",
|
|
25
|
+
"SSEEventOf",
|
|
25
26
|
"SseOptions",
|
|
26
27
|
"TypedWebSocket",
|
|
27
28
|
"Uint8Array",
|
|
29
|
+
"WsFrame",
|
|
28
30
|
"WsOptions",
|
|
29
31
|
];
|
|
30
32
|
/** What the types file may import from the runtime module, in the order the import lists them. */
|
|
@@ -34,7 +36,9 @@ const RUNTIME_TYPES = [
|
|
|
34
36
|
"ResumableConnection",
|
|
35
37
|
"SDKConfig",
|
|
36
38
|
"SSEEvent",
|
|
39
|
+
"SSEEventOf",
|
|
37
40
|
"TypedWebSocket",
|
|
41
|
+
"WsFrame",
|
|
38
42
|
];
|
|
39
43
|
/** The input fields every call takes, by transport. */
|
|
40
44
|
const OPTIONS = `export type HttpOptions = {
|
|
@@ -48,7 +52,7 @@ const OPTIONS = `export type HttpOptions = {
|
|
|
48
52
|
export type SseOptions = Omit<HttpOptions, "idempotencyKey"> & { lastEventId?: string }
|
|
49
53
|
export type WsOptions = { protocols?: string | string[]; reconnectToken?: string }
|
|
50
54
|
`;
|
|
51
|
-
|
|
55
|
+
// the error envelope, the call and stream shapes, and the safe mirror's opening
|
|
52
56
|
const CALLS = `
|
|
53
57
|
type ErrorField = { error_key: string; message: string; path: string }
|
|
54
58
|
|
|
@@ -84,7 +88,7 @@ type HttpStream<I, V, E = never> = {
|
|
|
84
88
|
/** \`client.safe\`: every REST and stream call answering a Result, never throwing an API answer; a connection as is. */
|
|
85
89
|
export type HttpSafe<T> = {
|
|
86
90
|
[K in keyof T]: `;
|
|
87
|
-
|
|
91
|
+
// the safe mirror's REST and stream arms
|
|
88
92
|
const SAFE_ARMS = ` ? {
|
|
89
93
|
(input: I & HttpOptions & { raw: true }): Promise<Result<Response, E>>
|
|
90
94
|
(...input: HttpArgs<I, HttpOptions & { raw?: false }>): Promise<Result<D, E, H>>
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { irErrorEnvelope, isJsonMedia, mediaEssence, sdkModel, sseEventSchema } from "../../ir/sdk-model.js";
|
|
1
|
+
import { dispatchKey, irErrorEnvelope, isJsonMedia, mediaEssence, sdkModel, sseEventSchema, sseEvents, } from "../../ir/sdk-model.js";
|
|
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,18 +55,20 @@ 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
|
+
// a browser WebSocket sends no headers and sets no cookies, so a socket call takes neither
|
|
62
|
+
const socket = op.stream === "ws";
|
|
61
63
|
const parts = [
|
|
62
64
|
paramsPart("params", pathParams, (p) => irToTs(p.schema, types)),
|
|
63
65
|
paramsPart("search", op.query, (p) => irToTs(p.schema, types)),
|
|
64
|
-
paramsPart("headers", op.headers, text),
|
|
65
|
-
paramsPart("cookies", op.ir.params.cookie ?? [], text),
|
|
66
|
+
socket ? null : paramsPart("headers", op.headers, text),
|
|
67
|
+
socket ? null : paramsPart("cookies", op.ir.params.cookie ?? [], text),
|
|
66
68
|
...bodyParts(op, ctx),
|
|
67
69
|
].filter((part) => part !== null);
|
|
68
70
|
const ordered = [...parts.filter((p) => p.required), ...parts.filter((p) => !p.required)];
|
|
69
|
-
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)}`));
|
|
70
72
|
return { required: parts.some((p) => p.required), type: objectText(fields) };
|
|
71
73
|
}
|
|
72
74
|
/** The data a successful call resolves to: JSON from its schema, text as a string, bytes (A1), else null. */
|
|
@@ -102,6 +104,46 @@ function headersOf(op) {
|
|
|
102
104
|
const fields = [...byName].sort(([a], [b]) => compareCodeUnits(a, b));
|
|
103
105
|
return objectText(fields.map(([name, required]) => `${quoteKey(name)}${required ? "" : "?"}: string`));
|
|
104
106
|
}
|
|
107
|
+
/** A WebSocket op's socket: typed by its declared messages, `In` what the server receives, `Out` what it sends (D9). */
|
|
108
|
+
function socketType(op, ctx) {
|
|
109
|
+
const messages = op.ir.messages;
|
|
110
|
+
if (messages === undefined)
|
|
111
|
+
return "TypedWebSocket";
|
|
112
|
+
const { receive, send } = messages;
|
|
113
|
+
const key = send === undefined ? undefined : dispatchKey(send, ctx.resolve);
|
|
114
|
+
const args = [
|
|
115
|
+
receive === undefined ? "WsFrame" : irToTs(receive, ctx.types),
|
|
116
|
+
send === undefined ? "string" : irToTs(send, ctx.types),
|
|
117
|
+
...(key === undefined ? [] : [tsString(key)]),
|
|
118
|
+
];
|
|
119
|
+
while (args.length > 0 && args.at(-1) === ["WsFrame", "string"][args.length - 1])
|
|
120
|
+
args.pop();
|
|
121
|
+
return args.length === 0 ? "TypedWebSocket" : `TypedWebSocket<${args.join(", ")}>`;
|
|
122
|
+
}
|
|
123
|
+
/** A realtime op's connection: what it yields (the messages it hears, else its event data), sends, and dispatches on. */
|
|
124
|
+
function connectionType(op, ctx) {
|
|
125
|
+
const { receive, send } = op.ir.messages ?? {};
|
|
126
|
+
const yields = send ?? sseEventSchema(op.ir, ctx.resolve);
|
|
127
|
+
const key = send === undefined ? undefined : dispatchKey(send, ctx.resolve);
|
|
128
|
+
const args = [
|
|
129
|
+
yields === undefined ? "unknown" : irToTs(yields, ctx.types),
|
|
130
|
+
receive === undefined ? "unknown" : irToTs(receive, ctx.types),
|
|
131
|
+
...(key === undefined ? [] : [tsString(key)]),
|
|
132
|
+
];
|
|
133
|
+
while (args.length > 1 && args.at(-1) === "unknown")
|
|
134
|
+
args.pop();
|
|
135
|
+
return `ResumableConnection<${args.join(", ")}>`;
|
|
136
|
+
}
|
|
137
|
+
/** What an SSE op's stream yields: `SSEEventOf` its named events (D7), else `SSEEvent` of its one data schema. */
|
|
138
|
+
function sseEventType(op, ctx) {
|
|
139
|
+
const named = sseEvents(op.ir);
|
|
140
|
+
if (named !== undefined) {
|
|
141
|
+
const fields = named.map(({ data, name }) => `${quoteKey(name)}: ${irToTs(data, ctx.types)}`);
|
|
142
|
+
return `SSEEventOf<${objectText(fields)}>`;
|
|
143
|
+
}
|
|
144
|
+
const events = sseEventSchema(op.ir, ctx.resolve);
|
|
145
|
+
return events === undefined ? "SSEEvent" : `SSEEvent<${irToTs(events, ctx.types)}>`;
|
|
146
|
+
}
|
|
105
147
|
/** `op`'s member line `name`: a REST call a HttpCall, a stream a HttpStream, defaults dropped; a connection its method. */
|
|
106
148
|
function signature(name, op, ctx) {
|
|
107
149
|
const input = inputOf(op, ctx);
|
|
@@ -110,25 +152,22 @@ function signature(name, op, ctx) {
|
|
|
110
152
|
return `${input.required ? "input" : "input?"}: ${type}`;
|
|
111
153
|
};
|
|
112
154
|
const key = quoteKey(name);
|
|
113
|
-
|
|
155
|
+
// a realtime op is one resumable connection of its event data; any other is typed by its transport
|
|
114
156
|
const { realtime, sse, websocket } = op.ir.extensions;
|
|
115
|
-
if (realtime)
|
|
116
|
-
|
|
117
|
-
return `${key}(${arg("RealtimeCallOptions")}): ResumableConnection<${events ? irToTs(events, ctx.types) : "unknown"}>`;
|
|
118
|
-
}
|
|
157
|
+
if (realtime)
|
|
158
|
+
return `${key}(${arg("RealtimeCallOptions")}): ${connectionType(op, ctx)}`;
|
|
119
159
|
if (websocket)
|
|
120
|
-
return `${key}(${arg("WsOptions")}):
|
|
160
|
+
return `${key}(${arg("WsOptions")}): ${socketType(op, ctx)}`;
|
|
121
161
|
const errors = errorsOf(op, ctx.types, ctx.resolve);
|
|
122
162
|
if (sse) {
|
|
123
|
-
const
|
|
124
|
-
const event = events === undefined ? "SSEEvent" : `SSEEvent<${irToTs(events, ctx.types)}>`;
|
|
163
|
+
const event = sseEventType(op, ctx);
|
|
125
164
|
return `${key}: HttpStream<${[input.type, event, ...(errors === null ? [] : [errors])].join(", ")}>`;
|
|
126
165
|
}
|
|
127
166
|
const headers = headersOf(op);
|
|
128
167
|
const tail = headers === null ? (errors === null ? [] : [errors]) : [errors ?? "never", headers];
|
|
129
168
|
return `${key}: HttpCall<${[input.type, dataOf(op, ctx.types), ...tail].join(", ")}>`;
|
|
130
169
|
}
|
|
131
|
-
|
|
170
|
+
// every line break a comment splits at, as the shared comment writers split (escape.ts)
|
|
132
171
|
const LINE_BREAKS = /\r\n|\r|\n|\u2028|\u2029/;
|
|
133
172
|
/**
|
|
134
173
|
* `op`'s summary and description as a JSDoc block above its overloads at `indent`, a paragraph each, as Go, Rust,
|
|
@@ -183,7 +222,7 @@ function configText(name, server, runtimeConfig) {
|
|
|
183
222
|
/** The `${stem}.types.gen.ts` text of `ir` for the SDK `name`, importing the runtime module `runtime`. */
|
|
184
223
|
export function sdkTypesText(ir, name, runtime) {
|
|
185
224
|
const model = sdkModel(ir);
|
|
186
|
-
|
|
225
|
+
// the SDK named `SDK` declares `SDKConfig`, so the runtime's own is imported under another name
|
|
187
226
|
const aliases = name === "SDK" ? { SDKConfig: "HttpSDKConfig" } : {};
|
|
188
227
|
const runtimeConfig = aliases["SDKConfig"] ?? "SDKConfig";
|
|
189
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",
|
|
@@ -63,8 +63,8 @@ const RUNTIME_TYPES = {
|
|
|
63
63
|
"TransportConnection",
|
|
64
64
|
"TransportOpts",
|
|
65
65
|
],
|
|
66
|
-
sse: ["SSEEvent"],
|
|
67
|
-
ws: ["TypedWebSocket"],
|
|
66
|
+
sse: ["SSEEvent", "SSEEventOf"],
|
|
67
|
+
ws: ["TypedWebSocket", "WsFrame"],
|
|
68
68
|
};
|
|
69
69
|
/** The names of `lists` a runtime with `features` ships, ignoring case in their order. */
|
|
70
70
|
function shipped(lists, features) {
|
|
@@ -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;
|
|
@@ -15,10 +15,16 @@ export type ServiceEntry = {
|
|
|
15
15
|
params?: string[];
|
|
16
16
|
path: string;
|
|
17
17
|
realtime?: true;
|
|
18
|
-
/** an event stream; `"json"` when its declared event schema is not a string */
|
|
19
|
-
sse?: "json" | true;
|
|
18
|
+
/** an event stream; `"json"` when its declared event schema is not a string, its JSON events' names when it names them */
|
|
19
|
+
sse?: "json" | string[] | true;
|
|
20
20
|
wildcard?: string;
|
|
21
21
|
ws?: true;
|
|
22
|
+
/** the server declares what it sends: the socket parses each text frame */
|
|
23
|
+
decode?: true;
|
|
24
|
+
/** the field a socket's message union is keyed on (D9) */
|
|
25
|
+
dispatch?: string;
|
|
26
|
+
/** the server declares what it receives: the socket sends JSON */
|
|
27
|
+
encode?: true;
|
|
22
28
|
};
|
|
23
29
|
export type ServiceMapNode = ServiceEntry | {
|
|
24
30
|
[key: string]: ServiceMapNode;
|
|
@@ -38,3 +44,4 @@ export declare function sdkMemberPaths(ir: IR): Map<string, string[]>;
|
|
|
38
44
|
export declare function serviceMapOf(ir: IR): Record<string, ServiceMapNode>;
|
|
39
45
|
/** The `${stem}.map.gen.ts` text: the service map as a `const`, importing the adapters its entries name. */
|
|
40
46
|
export declare function serviceMapText(ir: IR, runtime: string): string;
|
|
47
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isJsonMedia, mediaEssence, sdkModel, sseEventSchema, } from "../../ir/sdk-model.js";
|
|
1
|
+
import { isJsonMedia, mediaEssence, sdkModel, dispatchKey, isJsonEventData, sseEventSchema, sseEvents, } from "../../ir/sdk-model.js";
|
|
2
2
|
import { openApiPathParams } from "../../ir/slots.js";
|
|
3
3
|
import { tsString } from "../shared/escape.js";
|
|
4
4
|
import { compareCodeUnits } from "../shared/lang.js";
|
|
@@ -69,6 +69,33 @@ function headersOf(op, status) {
|
|
|
69
69
|
const declared = op.responses[status]?.headers ?? [];
|
|
70
70
|
return [...new Set(declared.map(({ name }) => name.toLowerCase()))].sort(compareCodeUnits);
|
|
71
71
|
}
|
|
72
|
+
/** How the runtime reads `op`'s events: the names of its JSON events when it names them (D7), else all or none. */
|
|
73
|
+
function sseKind(op, resolve) {
|
|
74
|
+
const named = sseEvents(op);
|
|
75
|
+
if (named !== undefined)
|
|
76
|
+
return named.filter(({ data }) => isJsonEventData(data, resolve)).map(({ name }) => name);
|
|
77
|
+
return sseEventSchema(op, resolve) === undefined ? true : "json";
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* How a socket op's declared messages read on the wire: a WebSocket's sends JSON when the server declares `receive`
|
|
81
|
+
* and its frames parsed when it declares `send`; either kind's messages dispatched on `send`'s key (D9).
|
|
82
|
+
*/
|
|
83
|
+
function socketEntry(op, resolve) {
|
|
84
|
+
const { messages } = op;
|
|
85
|
+
if (messages === undefined)
|
|
86
|
+
return {};
|
|
87
|
+
const out = {};
|
|
88
|
+
if (!op.extensions.realtime) {
|
|
89
|
+
if (messages.receive !== undefined)
|
|
90
|
+
out.encode = true;
|
|
91
|
+
if (messages.send !== undefined)
|
|
92
|
+
out.decode = true;
|
|
93
|
+
}
|
|
94
|
+
const key = messages.send === undefined ? undefined : dispatchKey(messages.send, resolve);
|
|
95
|
+
if (key !== undefined)
|
|
96
|
+
out.dispatch = key;
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
72
99
|
/** The service entry of `op`; `success` is its success answer as the SDK model reads it. */
|
|
73
100
|
function entryOf(op, success, resolve) {
|
|
74
101
|
const entry = { method: op.method, path: op.path };
|
|
@@ -79,11 +106,12 @@ function entryOf(op, success, resolve) {
|
|
|
79
106
|
if (wildcard)
|
|
80
107
|
entry.wildcard = wildcard.name;
|
|
81
108
|
if (op.extensions.sse)
|
|
82
|
-
entry.sse =
|
|
109
|
+
entry.sse = sseKind(op, resolve);
|
|
83
110
|
if (op.extensions.websocket)
|
|
84
111
|
entry.ws = true;
|
|
85
112
|
if (op.extensions.realtime)
|
|
86
113
|
entry.realtime = true;
|
|
114
|
+
Object.assign(entry, socketEntry(op, resolve));
|
|
87
115
|
if (op.extensions.idempotencyKey)
|
|
88
116
|
entry.idempotent = true;
|
|
89
117
|
const invalidate = op.extensions.invalidates;
|
|
@@ -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 = [];
|
|
@@ -204,13 +210,15 @@ function baseCtx(extracted, name) {
|
|
|
204
210
|
}
|
|
205
211
|
/**
|
|
206
212
|
* A WebSocket route's ctx, as core's `WSRouteCtx` types its callbacks: the base with the pattern's params, and the
|
|
207
|
-
* search
|
|
213
|
+
* search, headers and cookies `.input()` declares. Extracted additions are keyed by HTTP routes only, so none join
|
|
214
|
+
* here.
|
|
208
215
|
*/
|
|
209
216
|
function wsCtx(op, id, options, ctx) {
|
|
210
217
|
const own = [];
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
218
|
+
// the upgrade request's validated search, headers and cookies, as core's `WithInput` holds them
|
|
219
|
+
const input = wireInput(op, ctx).filter(([key]) => key === "search" || key === "headers" || key === "cookies");
|
|
220
|
+
if (input.length > 0)
|
|
221
|
+
own.push(["readonly input", objectText(input)]);
|
|
214
222
|
const params = ctxParams(options.extracted, id, pathOf(id));
|
|
215
223
|
if (params !== "{}")
|
|
216
224
|
own.push(["readonly params", params]);
|
|
@@ -273,15 +281,21 @@ export function generateTypes(options) {
|
|
|
273
281
|
const ir = toIR(options.document, { deriveOperationIds: true });
|
|
274
282
|
const routes = routesOf(ir, options.report);
|
|
275
283
|
const extra = Object.keys(options.extracted?.routes ?? {}).filter((id) => !routes.has(id));
|
|
276
|
-
for (const id of extra.sort(compareCodeUnits))
|
|
284
|
+
for (const id of extra.sort(compareCodeUnits)) {
|
|
277
285
|
options.report(`${id}: extracted ctx additions match no documented operation (internal and filtered routes are not documented); not typed`);
|
|
286
|
+
}
|
|
278
287
|
const name = options.baseCtxName ?? "BaseCtx";
|
|
279
288
|
const names = { refName: componentNames(ir, [...RESERVED, name]) };
|
|
280
289
|
const ctx = { names, resolve: (schema) => irToTs(schema, names) };
|
|
281
290
|
const hasErrors = [...routes.values()].some((op) => Object.keys(op.extensions.errors ?? {}).length > 0);
|
|
282
|
-
|
|
291
|
+
// HttpMeta once a route's ctx reads it: with anything extracted, the ctx's meta is built from its keys
|
|
283
292
|
const readsMeta = options.extracted !== null && options.extracted !== undefined && routes.size > 0;
|
|
284
|
-
const imports = [
|
|
293
|
+
const imports = [
|
|
294
|
+
"HttpContext",
|
|
295
|
+
"WithOutput",
|
|
296
|
+
...(hasErrors ? ["HttpError"] : []),
|
|
297
|
+
...(readsMeta ? ["HttpMeta"] : []),
|
|
298
|
+
];
|
|
285
299
|
const out = [`import type { ${imports.join(", ")} } from "@workerstack/backend/http"`];
|
|
286
300
|
out.push(baseCtx(options.extracted, name));
|
|
287
301
|
const slot = operationSlot(ir, options, ctx);
|
|
@@ -290,7 +304,8 @@ export function generateTypes(options) {
|
|
|
290
304
|
for (const schema of Object.keys(ir.schemas).sort(compareCodeUnits))
|
|
291
305
|
out.push(`type ${names.refName?.(schema)} = ${ctx.resolve(ir.schemas[schema])}`);
|
|
292
306
|
out.push(routesType(routes, options, ctx));
|
|
293
|
-
for (const [type, field] of EXTRACTORS)
|
|
307
|
+
for (const [type, field] of EXTRACTORS) {
|
|
294
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
|
+
}
|
|
295
310
|
return `${out.join("\n\n")}\n`;
|
|
296
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;
|