@distilled.cloud/core 1.0.0-rc.15 → 1.0.0-rc.16
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/lib/api.d.ts +118 -13
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +755 -147
- package/lib/api.js.map +1 -1
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +146 -146
- package/lib/category.js.map +1 -1
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +11 -0
- package/lib/chunks.js.map +1 -0
- package/lib/codegen/boolean-string-enums.js +36 -36
- package/lib/codegen/chunk.d.ts +7 -0
- package/lib/codegen/chunk.d.ts.map +1 -0
- package/lib/codegen/chunk.js +139 -0
- package/lib/codegen/chunk.js.map +1 -0
- package/lib/codegen/cli.d.ts.map +1 -1
- package/lib/codegen/cli.js +28 -14
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/datafy.d.ts +14 -0
- package/lib/codegen/datafy.d.ts.map +1 -0
- package/lib/codegen/datafy.js +853 -0
- package/lib/codegen/datafy.js.map +1 -0
- package/lib/codegen/dedupe.d.ts +5 -0
- package/lib/codegen/dedupe.d.ts.map +1 -0
- package/lib/codegen/dedupe.js +153 -0
- package/lib/codegen/dedupe.js.map +1 -0
- package/lib/codegen/emit.js +56 -56
- package/lib/codegen/format.js +18 -18
- package/lib/codegen/generated-at.js +29 -29
- package/lib/codegen/generator.d.ts.map +1 -1
- package/lib/codegen/generator.js +92 -84
- package/lib/codegen/generator.js.map +1 -1
- package/lib/codegen/graph.js +36 -36
- package/lib/codegen/graphql-client.d.ts +2 -0
- package/lib/codegen/graphql-client.d.ts.map +1 -1
- package/lib/codegen/graphql-client.js +55 -26
- package/lib/codegen/graphql-client.js.map +1 -1
- package/lib/codegen/graphql.js +113 -113
- package/lib/codegen/latin1.d.ts +3 -0
- package/lib/codegen/latin1.d.ts.map +1 -0
- package/lib/codegen/latin1.js +126 -0
- package/lib/codegen/latin1.js.map +1 -0
- package/lib/codegen/members.d.ts +7 -0
- package/lib/codegen/members.d.ts.map +1 -1
- package/lib/codegen/members.js +26 -19
- package/lib/codegen/members.js.map +1 -1
- package/lib/codegen/model-diff.js +6 -6
- package/lib/codegen/naming.js +19 -19
- package/lib/codegen/openapi-cli.js +15 -15
- package/lib/codegen/openapi.d.ts +17 -0
- package/lib/codegen/openapi.d.ts.map +1 -1
- package/lib/codegen/openapi.js +323 -169
- package/lib/codegen/openapi.js.map +1 -1
- package/lib/codegen/operations.js +14 -14
- package/lib/codegen/pagination.js +11 -11
- package/lib/codegen/patch-audit.js +57 -57
- package/lib/codegen/patches.js +79 -79
- package/lib/codegen/prelude.js +19 -19
- package/lib/codegen/privatize.d.ts +21 -0
- package/lib/codegen/privatize.d.ts.map +1 -0
- package/lib/codegen/privatize.js +176 -0
- package/lib/codegen/privatize.js.map +1 -0
- package/lib/codegen/proto.js +25 -25
- package/lib/codegen/pure.d.ts +6 -0
- package/lib/codegen/pure.d.ts.map +1 -0
- package/lib/codegen/pure.js +91 -0
- package/lib/codegen/pure.js.map +1 -0
- package/lib/codegen/rewrite-operation-ids.js +108 -108
- package/lib/codegen/spec-path.js +57 -57
- package/lib/codegen/table.d.ts +5 -0
- package/lib/codegen/table.d.ts.map +1 -0
- package/lib/codegen/table.js +249 -0
- package/lib/codegen/table.js.map +1 -0
- package/lib/config.d.ts +27 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +161 -0
- package/lib/config.js.map +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +130 -0
- package/lib/crypto.js.map +1 -0
- package/lib/debug.d.ts +2 -0
- package/lib/debug.d.ts.map +1 -0
- package/lib/debug.js +8 -0
- package/lib/debug.js.map +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +7 -6
- package/lib/error-category.js.map +1 -1
- package/lib/errors.d.ts +62 -62
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +117 -114
- package/lib/errors.js.map +1 -1
- package/lib/graphql.d.ts +31 -20
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.js +150 -119
- package/lib/graphql.js.map +1 -1
- package/lib/graphql.types.js +4 -3
- package/lib/graphql.types.js.map +1 -1
- package/lib/index.d.ts +3 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +15 -14
- package/lib/index.js.map +1 -1
- package/lib/json-patch.d.ts +2 -2
- package/lib/json-patch.js +28 -28
- package/lib/pagination.d.ts +9 -25
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +95 -90
- package/lib/pagination.js.map +1 -1
- package/lib/plain-values.d.ts +10 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +40 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protocol-http.d.ts +50 -14
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +351 -178
- package/lib/protocol-http.js.map +1 -1
- package/lib/protocol-rest.d.ts +38 -25
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +238 -130
- package/lib/protocol-rest.js.map +1 -1
- package/lib/query.js +16 -16
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +284 -0
- package/lib/response-validation-strict.js.map +1 -0
- package/lib/response-validation.d.ts +43 -25
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/response-validation.js +112 -70
- package/lib/response-validation.js.map +1 -1
- package/lib/retry-after.d.ts +3 -3
- package/lib/retry-after.js +49 -49
- package/lib/retry.d.ts +48 -29
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +142 -154
- package/lib/retry.js.map +1 -1
- package/lib/schema-lite-impl.d.ts +158 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +341 -0
- package/lib/schema-lite-impl.js.map +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +27 -0
- package/lib/schema-lite-typed.js.map +1 -0
- package/lib/schema-lite.d.ts +40 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +31 -0
- package/lib/schema-lite.js.map +1 -0
- package/lib/schema.d.ts +20 -6
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +202 -30
- package/lib/schema.js.map +1 -1
- package/lib/testing.d.ts +3 -2
- package/lib/testing.d.ts.map +1 -1
- package/lib/testing.js +22 -21
- package/lib/testing.js.map +1 -1
- package/lib/trait.d.ts +13 -13
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +80 -80
- package/lib/trait.js.map +1 -1
- package/lib/xml.d.ts +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/lib/xml.js +38 -26
- package/lib/xml.js.map +1 -1
- package/package.json +9 -7
- package/src/api.ts +1079 -180
- package/src/category.ts +33 -33
- package/src/chunks.ts +30 -0
- package/src/codegen/chunk.ts +147 -0
- package/src/codegen/cli.ts +27 -1
- package/src/codegen/datafy.ts +874 -0
- package/src/codegen/dedupe.ts +173 -0
- package/src/codegen/generator.ts +10 -3
- package/src/codegen/graphql-client.ts +49 -15
- package/src/codegen/latin1.ts +131 -0
- package/src/codegen/members.ts +8 -0
- package/src/codegen/openapi.ts +194 -3
- package/src/codegen/privatize.ts +200 -0
- package/src/codegen/pure.ts +94 -0
- package/src/codegen/table.ts +254 -0
- package/src/config.ts +216 -0
- package/src/crypto.ts +202 -0
- package/src/debug.ts +8 -0
- package/src/error-category.ts +2 -1
- package/src/errors.ts +65 -56
- package/src/graphql.ts +109 -62
- package/src/graphql.types.ts +9 -7
- package/src/index.ts +3 -2
- package/src/json-patch.ts +2 -2
- package/src/pagination.ts +37 -18
- package/src/plain-values.ts +41 -0
- package/src/protocol-http.ts +378 -145
- package/src/protocol-rest.ts +337 -169
- package/src/response-validation-strict.ts +327 -0
- package/src/response-validation.ts +101 -50
- package/src/retry-after.ts +3 -3
- package/src/retry.ts +94 -93
- package/src/schema-lite-impl.ts +550 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +44 -0
- package/src/schema.ts +258 -14
- package/src/testing.ts +4 -2
- package/src/trait.ts +36 -28
- package/src/xml.ts +31 -17
- package/lib/codegen/boolean-string-enums.test.d.ts +0 -2
- package/lib/codegen/boolean-string-enums.test.d.ts.map +0 -1
- package/lib/codegen/boolean-string-enums.test.js +0 -275
- package/lib/codegen/boolean-string-enums.test.js.map +0 -1
- package/lib/codegen/defer-naming.test.d.ts +0 -2
- package/lib/codegen/defer-naming.test.d.ts.map +0 -1
- package/lib/codegen/defer-naming.test.js +0 -105
- package/lib/codegen/defer-naming.test.js.map +0 -1
- package/lib/codegen/generated-at.test.d.ts +0 -2
- package/lib/codegen/generated-at.test.d.ts.map +0 -1
- package/lib/codegen/generated-at.test.js +0 -81
- package/lib/codegen/generated-at.test.js.map +0 -1
- package/lib/codegen/generator.test.d.ts +0 -2
- package/lib/codegen/generator.test.d.ts.map +0 -1
- package/lib/codegen/generator.test.js +0 -121
- package/lib/codegen/generator.test.js.map +0 -1
- package/lib/codegen/graphql-client.test.d.ts +0 -2
- package/lib/codegen/graphql-client.test.d.ts.map +0 -1
- package/lib/codegen/graphql-client.test.js +0 -171
- package/lib/codegen/graphql-client.test.js.map +0 -1
- package/lib/codegen/openapi-binary.test.d.ts +0 -2
- package/lib/codegen/openapi-binary.test.d.ts.map +0 -1
- package/lib/codegen/openapi-binary.test.js +0 -67
- package/lib/codegen/openapi-binary.test.js.map +0 -1
- package/lib/codegen/patch-audit.test.d.ts +0 -2
- package/lib/codegen/patch-audit.test.d.ts.map +0 -1
- package/lib/codegen/patch-audit.test.js +0 -141
- package/lib/codegen/patch-audit.test.js.map +0 -1
- package/lib/codegen/patches.test.d.ts +0 -2
- package/lib/codegen/patches.test.d.ts.map +0 -1
- package/lib/codegen/patches.test.js +0 -155
- package/lib/codegen/patches.test.js.map +0 -1
- package/lib/codegen/protocol-imports.test.d.ts +0 -2
- package/lib/codegen/protocol-imports.test.d.ts.map +0 -1
- package/lib/codegen/protocol-imports.test.js +0 -36
- package/lib/codegen/protocol-imports.test.js.map +0 -1
- package/lib/codegen/rewrite-operation-ids.test.d.ts +0 -2
- package/lib/codegen/rewrite-operation-ids.test.d.ts.map +0 -1
- package/lib/codegen/rewrite-operation-ids.test.js +0 -451
- package/lib/codegen/rewrite-operation-ids.test.js.map +0 -1
- package/lib/codegen/untagged-unions.test.d.ts +0 -2
- package/lib/codegen/untagged-unions.test.d.ts.map +0 -1
- package/lib/codegen/untagged-unions.test.js +0 -80
- package/lib/codegen/untagged-unions.test.js.map +0 -1
- package/lib/fixtures/xml/upstream-validator.json +0 -397
- package/lib/graphql.test.d.ts +0 -2
- package/lib/graphql.test.d.ts.map +0 -1
- package/lib/graphql.test.js +0 -151
- package/lib/graphql.test.js.map +0 -1
- package/lib/json-patch.test.d.ts +0 -2
- package/lib/json-patch.test.d.ts.map +0 -1
- package/lib/json-patch.test.js +0 -65
- package/lib/json-patch.test.js.map +0 -1
- package/lib/pagination.test.d.ts +0 -2
- package/lib/pagination.test.d.ts.map +0 -1
- package/lib/pagination.test.js +0 -131
- package/lib/pagination.test.js.map +0 -1
- package/lib/protocol-http.test.d.ts +0 -2
- package/lib/protocol-http.test.d.ts.map +0 -1
- package/lib/protocol-http.test.js +0 -228
- package/lib/protocol-http.test.js.map +0 -1
- package/lib/protocol-rest.test.d.ts +0 -2
- package/lib/protocol-rest.test.d.ts.map +0 -1
- package/lib/protocol-rest.test.js +0 -498
- package/lib/protocol-rest.test.js.map +0 -1
- package/lib/response-validation.test.d.ts +0 -2
- package/lib/response-validation.test.d.ts.map +0 -1
- package/lib/response-validation.test.js +0 -72
- package/lib/response-validation.test.js.map +0 -1
- package/lib/retry-after.test.d.ts +0 -2
- package/lib/retry-after.test.d.ts.map +0 -1
- package/lib/retry-after.test.js +0 -204
- package/lib/retry-after.test.js.map +0 -1
- package/lib/retry.test.d.ts +0 -2
- package/lib/retry.test.d.ts.map +0 -1
- package/lib/retry.test.js +0 -407
- package/lib/retry.test.js.map +0 -1
- package/lib/sdks.test.d.ts +0 -2
- package/lib/sdks.test.d.ts.map +0 -1
- package/lib/sdks.test.js +0 -76
- package/lib/sdks.test.js.map +0 -1
- package/lib/xml.test.d.ts +0 -2
- package/lib/xml.test.d.ts.map +0 -1
- package/lib/xml.test.js +0 -210
- package/lib/xml.test.js.map +0 -1
- package/src/codegen/boolean-string-enums.test.ts +0 -299
- package/src/codegen/defer-naming.test.ts +0 -113
- package/src/codegen/generated-at.test.ts +0 -89
- package/src/codegen/generator.test.ts +0 -137
- package/src/codegen/graphql-client.test.ts +0 -189
- package/src/codegen/openapi-binary.test.ts +0 -91
- package/src/codegen/patch-audit.test.ts +0 -160
- package/src/codegen/patches.test.ts +0 -188
- package/src/codegen/protocol-imports.test.ts +0 -39
- package/src/codegen/rewrite-operation-ids.test.ts +0 -472
- package/src/codegen/untagged-unions.test.ts +0 -85
- package/src/fixtures/xml/upstream-validator.json +0 -397
- package/src/graphql.test.ts +0 -198
- package/src/json-patch.test.ts +0 -77
- package/src/pagination.test.ts +0 -169
- package/src/protocol-http.test.ts +0 -276
- package/src/protocol-rest.test.ts +0 -703
- package/src/response-validation.test.ts +0 -89
- package/src/retry-after.test.ts +0 -267
- package/src/retry.test.ts +0 -566
- package/src/sdks.test.ts +0 -87
- package/src/xml.test.ts +0 -251
package/src/protocol-http.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
import * as Headers from "effect/http/Headers";
|
|
1
2
|
import * as HttpBody from "effect/http/HttpBody";
|
|
2
3
|
import * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
3
4
|
/**
|
|
4
5
|
* Generic REST protocol machinery, shared by SDK protocol layers.
|
|
5
6
|
*
|
|
6
7
|
* Everything here is driven purely by the trait annotations in
|
|
7
|
-
* `core/trait.ts`
|
|
8
|
+
* `core/trait.ts` -- nothing is provider-specific. An SDK protocol (e.g.
|
|
8
9
|
* cloudflare's) supplies credentials/base-URL, calls {@link buildRequest} to
|
|
9
10
|
* encode, and layers its own response-envelope handling on top of the
|
|
10
11
|
* decode-side helpers ({@link getProps}/{@link mapKeys}/
|
|
11
12
|
* {@link matchTypedError}).
|
|
12
13
|
*/
|
|
13
14
|
import type * as AST from "effect/SchemaAST";
|
|
15
|
+
import { debugHttp } from "./debug.ts";
|
|
16
|
+
import { isOpaqueValue } from "./plain-values.ts";
|
|
14
17
|
import {
|
|
15
18
|
bodySymbol,
|
|
16
19
|
deepQuerySymbol,
|
|
@@ -46,7 +49,7 @@ export const getAnn = (ast: AST.AST, symbol: symbol): unknown => {
|
|
|
46
49
|
if (direct !== undefined) return direct;
|
|
47
50
|
if (ast._tag === "Suspend") return getAnn(ast.thunk(), symbol);
|
|
48
51
|
if (ast.encoding && ast.encoding.length > 0) return getAnn(ast.encoding[0]!.to, symbol);
|
|
49
|
-
// S.optional
|
|
52
|
+
// S.optional -> Union[self, Undefined]; descend into the single real member.
|
|
50
53
|
if (ast._tag === "Union") {
|
|
51
54
|
const real = (ast as AST.Union).types.filter(
|
|
52
55
|
(t) => t._tag !== "Undefined" && !(t._tag === "Literal" && (t as any).literal === null),
|
|
@@ -82,15 +85,67 @@ export const resolveNode = (ast: AST.AST): AST.AST => {
|
|
|
82
85
|
};
|
|
83
86
|
//#endregion
|
|
84
87
|
|
|
88
|
+
//#region Response headers on outputs
|
|
89
|
+
|
|
90
|
+
/** An output member read from a response header (`T.Header`). */
|
|
91
|
+
export interface ResponseHeaderMember {
|
|
92
|
+
readonly key: string;
|
|
93
|
+
/** Lowercase header name. */
|
|
94
|
+
readonly wire: string;
|
|
95
|
+
/** The member is a number; the header's text is parsed. */
|
|
96
|
+
readonly number: boolean;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const noHeaderMembers: ReadonlyArray<ResponseHeaderMember> = [];
|
|
100
|
+
const headerMemberCache = /*@__PURE__*/ new WeakMap<AST.AST, ReadonlyArray<ResponseHeaderMember>>();
|
|
101
|
+
|
|
102
|
+
/** The output's header-bound members, read from the schema once. */
|
|
103
|
+
export const responseHeaderMembers = (outputAst: AST.AST): ReadonlyArray<ResponseHeaderMember> => {
|
|
104
|
+
let members = headerMemberCache.get(outputAst);
|
|
105
|
+
if (members !== undefined) return members;
|
|
106
|
+
const found: ResponseHeaderMember[] = [];
|
|
107
|
+
for (const prop of getProps(outputAst)) {
|
|
108
|
+
if (!hasPropAnn(prop, headerSymbol)) continue;
|
|
109
|
+
found.push({
|
|
110
|
+
key: String(prop.name),
|
|
111
|
+
wire: nameOf(prop, headerSymbol).toLowerCase(),
|
|
112
|
+
number: resolveNode(prop.type)._tag === "Number",
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
members = found.length === 0 ? noHeaderMembers : found;
|
|
116
|
+
headerMemberCache.set(outputAst, members);
|
|
117
|
+
return members;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Copy the response headers an output declares onto its decoded body. A
|
|
122
|
+
* number member gets the header parsed; text that is not a number is kept
|
|
123
|
+
* as sent, so strict validation reports it. A body that is not an object
|
|
124
|
+
* (a raw root) is returned as is -- generators bind no headers there.
|
|
125
|
+
*/
|
|
126
|
+
export const withResponseHeaders = (
|
|
127
|
+
members: ReadonlyArray<ResponseHeaderMember>,
|
|
128
|
+
body: unknown,
|
|
129
|
+
headers: Record<string, string | undefined>,
|
|
130
|
+
): unknown => {
|
|
131
|
+
if (members.length === 0 || body === null || typeof body !== "object" || Array.isArray(body)) {
|
|
132
|
+
return body;
|
|
133
|
+
}
|
|
134
|
+
let result: Record<string, unknown> | undefined;
|
|
135
|
+
for (const m of members) {
|
|
136
|
+
const raw = headers[m.wire];
|
|
137
|
+
if (raw === undefined) continue;
|
|
138
|
+
result ??= { ...(body as Record<string, unknown>) };
|
|
139
|
+
const parsed = m.number && raw.trim() !== "" ? Number(raw) : Number.NaN;
|
|
140
|
+
result[m.key] = Number.isNaN(parsed) ? raw : parsed;
|
|
141
|
+
}
|
|
142
|
+
return result ?? body;
|
|
143
|
+
};
|
|
144
|
+
//#endregion
|
|
145
|
+
|
|
85
146
|
//#region Recursive wire-name mapping
|
|
86
147
|
|
|
87
|
-
|
|
88
|
-
export const isOpaqueValue = (v: unknown): boolean =>
|
|
89
|
-
v instanceof Blob ||
|
|
90
|
-
v instanceof File ||
|
|
91
|
-
v instanceof ArrayBuffer ||
|
|
92
|
-
v instanceof Uint8Array ||
|
|
93
|
-
v instanceof Date;
|
|
148
|
+
export { isOpaqueValue };
|
|
94
149
|
|
|
95
150
|
/**
|
|
96
151
|
* Deep-rename keys via a plain dictionary (see `T.KeyDictionary`). An entry
|
|
@@ -132,7 +187,7 @@ export const mapKeysByDictionary = (
|
|
|
132
187
|
/**
|
|
133
188
|
* Recursively rename keys between TS names and wire names, per the schema.
|
|
134
189
|
*
|
|
135
|
-
* `fallback` is the nearest ancestor `T.KeyDictionary`
|
|
190
|
+
* `fallback` is the nearest ancestor `T.KeyDictionary` -- schema-known members
|
|
136
191
|
* always map via their own annotations, but opaque values (Document members,
|
|
137
192
|
* keys the schema doesn't know) deep-rename via the dictionary so nested
|
|
138
193
|
* TS-cased content still reaches the wire in wire case. Content with no
|
|
@@ -140,12 +195,66 @@ export const mapKeysByDictionary = (
|
|
|
140
195
|
*/
|
|
141
196
|
/**
|
|
142
197
|
* Value form for a `StringEncoded()` member: the string spelling of the
|
|
143
|
-
* value, element-wise for lists. `null` stays `null`
|
|
198
|
+
* value, element-wise for lists. `null` stays `null` -- an API that models a
|
|
144
199
|
* flag as `"true" | "false"` still means "unset" by null, not `"null"`.
|
|
145
200
|
*/
|
|
146
201
|
const stringEncode = (value: unknown): unknown =>
|
|
147
202
|
value === null ? null : Array.isArray(value) ? value.map(stringEncode) : String(value);
|
|
148
203
|
|
|
204
|
+
/**
|
|
205
|
+
* Whether {@link mapKeys} returns every value of `root` unchanged (as a deep
|
|
206
|
+
* copy) in `direction` with no fallback dictionary: no member is renamed,
|
|
207
|
+
* string-encoded (encode) or picked by `T.UnionCases` (decode), and no
|
|
208
|
+
* `T.KeyDictionary` is in scope anywhere below `root`. Protocols use it to
|
|
209
|
+
* skip the walk. Computed once per schema node and direction.
|
|
210
|
+
*/
|
|
211
|
+
export const mapsVerbatim = (root: AST.AST, direction: "encode" | "decode"): boolean => {
|
|
212
|
+
const cache = direction === "encode" ? verbatimEncode : verbatimDecode;
|
|
213
|
+
let result = cache.get(root);
|
|
214
|
+
if (result !== undefined) return result;
|
|
215
|
+
result = true;
|
|
216
|
+
const seen = new Set<AST.AST>();
|
|
217
|
+
const stack: AST.AST[] = [root];
|
|
218
|
+
walk: while (stack.length > 0) {
|
|
219
|
+
const ast = stack.pop()!;
|
|
220
|
+
if (seen.has(ast)) continue;
|
|
221
|
+
seen.add(ast);
|
|
222
|
+
if (getAnn(ast, keyDictionarySymbol) !== undefined) {
|
|
223
|
+
result = false;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
if (direction === "decode" && getAnn(ast, unionCasesSymbol) !== undefined) {
|
|
227
|
+
result = false;
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
const node = resolveNode(ast);
|
|
231
|
+
if (node._tag === "Union") {
|
|
232
|
+
for (const t of (node as AST.Union).types) stack.push(resolveNode(t));
|
|
233
|
+
} else if (node._tag === "Arrays") {
|
|
234
|
+
const elem = (node as any).rest?.[0] as AST.AST | undefined;
|
|
235
|
+
if (elem) stack.push(elem);
|
|
236
|
+
} else if (node._tag === "Objects") {
|
|
237
|
+
for (const p of (node as any).propertySignatures as readonly AST.PropertySignature[]) {
|
|
238
|
+
if (
|
|
239
|
+
nameOf(p, bodySymbol) !== String(p.name) ||
|
|
240
|
+
(direction === "encode" && hasPropAnn(p, stringEncodedSymbol))
|
|
241
|
+
) {
|
|
242
|
+
result = false;
|
|
243
|
+
break walk;
|
|
244
|
+
}
|
|
245
|
+
stack.push(p.type);
|
|
246
|
+
}
|
|
247
|
+
for (const s of ((node as any).indexSignatures ?? []) as readonly { type: AST.AST }[]) {
|
|
248
|
+
stack.push(s.type);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
cache.set(root, result);
|
|
253
|
+
return result;
|
|
254
|
+
};
|
|
255
|
+
const verbatimEncode = /*@__PURE__*/ new WeakMap<AST.AST, boolean>();
|
|
256
|
+
const verbatimDecode = /*@__PURE__*/ new WeakMap<AST.AST, boolean>();
|
|
257
|
+
|
|
149
258
|
export const mapKeys = (
|
|
150
259
|
ast: AST.AST,
|
|
151
260
|
value: unknown,
|
|
@@ -160,10 +269,10 @@ export const mapKeys = (
|
|
|
160
269
|
// Discriminated union whose cases the API returns merged (every case's
|
|
161
270
|
// keys present, `null` for the inactive ones). Map wire names, then keep
|
|
162
271
|
// only the active case's keys. The case is read off the union's
|
|
163
|
-
// discriminator when it has one
|
|
272
|
+
// discriminator when it has one -- cases that share a key set (every
|
|
164
273
|
// resource kind of `resource_tagging`, every page-rule action) are
|
|
165
274
|
// indistinguishable by keys alone. Without one, a case does NOT need every
|
|
166
|
-
// key present
|
|
275
|
+
// key present -- arms carry optional members the wire may omit -- so the
|
|
167
276
|
// active case is the one that best EXPLAINS the value: most present,
|
|
168
277
|
// non-null keys covered, minus present keys the case cannot explain (ties
|
|
169
278
|
// break by coverage, then declaration order).
|
|
@@ -214,12 +323,12 @@ export const mapKeys = (
|
|
|
214
323
|
const node = resolveNode(ast);
|
|
215
324
|
|
|
216
325
|
// Schema-level union member (a merged response struct whose arms disagree
|
|
217
|
-
// on a member's type
|
|
326
|
+
// on a member's type -- see the codegen's response-union merge): pick the
|
|
218
327
|
// arm that explains the value best and map with it. Object values score
|
|
219
328
|
// arms by how many of their declared keys are present; array values take
|
|
220
329
|
// the first array arm. Anything unmatched deep-renames via the dictionary
|
|
221
|
-
// fallback, like other opaque content. (Single-real-member unions
|
|
222
|
-
// optional/null wrappers
|
|
330
|
+
// fallback, like other opaque content. (Single-real-member unions --
|
|
331
|
+
// optional/null wrappers -- were already collapsed by resolveNode; the
|
|
223
332
|
// opaque `T.UnionCases` mechanism never produces a Union node and is
|
|
224
333
|
// handled above.)
|
|
225
334
|
if (node._tag === "Union") {
|
|
@@ -261,7 +370,7 @@ export const mapKeys = (
|
|
|
261
370
|
const props = (node as any).propertySignatures as readonly AST.PropertySignature[];
|
|
262
371
|
const isigs = (node as any).indexSignatures as readonly { type: AST.AST }[] | undefined;
|
|
263
372
|
if (props.length === 0 && !(isigs && isigs.length)) {
|
|
264
|
-
// opaque object
|
|
373
|
+
// opaque object -- dictionary fallback or verbatim
|
|
265
374
|
return dict ? mapKeysByDictionary(dict, value, direction) : value;
|
|
266
375
|
}
|
|
267
376
|
const out: Record<string, unknown> = {};
|
|
@@ -275,7 +384,7 @@ export const mapKeys = (
|
|
|
275
384
|
const v = (value as Record<string, unknown>)[from];
|
|
276
385
|
if (v === undefined) continue;
|
|
277
386
|
// A `StringEncoded()` member nested in a body struct (e.g. Azure's
|
|
278
|
-
// `hardwareProfile.dynamicMemoryEnabled`) stringifies here
|
|
387
|
+
// `hardwareProfile.dynamicMemoryEnabled`) stringifies here -- the
|
|
279
388
|
// top-level pass in `buildRequest` only sees the outermost member.
|
|
280
389
|
out[to] =
|
|
281
390
|
direction === "encode" && hasPropAnn(p, stringEncodedSymbol)
|
|
@@ -300,7 +409,7 @@ export const mapKeys = (
|
|
|
300
409
|
return out;
|
|
301
410
|
}
|
|
302
411
|
|
|
303
|
-
// Scalar-typed schema node holding object content (spec drift)
|
|
412
|
+
// Scalar-typed schema node holding object content (spec drift) --
|
|
304
413
|
// dictionary fallback or verbatim.
|
|
305
414
|
return dict ? mapKeysByDictionary(dict, value, direction) : value;
|
|
306
415
|
};
|
|
@@ -308,16 +417,16 @@ export const mapKeys = (
|
|
|
308
417
|
|
|
309
418
|
//#region Request building
|
|
310
419
|
|
|
311
|
-
const BODYLESS = new Set(["GET", "HEAD"]);
|
|
420
|
+
const BODYLESS = /*@__PURE__*/ new Set(["GET", "HEAD"]);
|
|
312
421
|
|
|
313
422
|
/**
|
|
314
423
|
* Serialize one query member. Scalars go as `k=v`, arrays as repeated `k=v`
|
|
315
|
-
* pairs, and nested plain objects recurse as DOTTED params
|
|
424
|
+
* pairs, and nested plain objects recurse as DOTTED params -- several list
|
|
316
425
|
* endpoints model their filters as structs (e.g. Cloudflare DNS listRecords
|
|
317
426
|
* takes `name: { exact, contains, ... }`) that must go over the wire as
|
|
318
427
|
* `name.exact=value`. Without this they fell through to `String(value)` and
|
|
319
428
|
* were sent as `name=[object Object]`, which servers happily treat as a
|
|
320
|
-
* filter matching nothing
|
|
429
|
+
* filter matching nothing -- the call "succeeds" with zero results and the
|
|
321
430
|
* bug is invisible to the caller.
|
|
322
431
|
*/
|
|
323
432
|
const appendQuery = (query: URLSearchParams, name: string, value: unknown): void => {
|
|
@@ -345,25 +454,13 @@ export interface BuildRequestOptions {
|
|
|
345
454
|
*/
|
|
346
455
|
readonly mapMemberHeader?: (name: string, value: string) => string;
|
|
347
456
|
/**
|
|
348
|
-
* Wire name for input keys the schema doesn't declare (spec drift
|
|
457
|
+
* Wire name for input keys the schema doesn't declare (spec drift --
|
|
349
458
|
* unknown keys pass through as body fields rather than being dropped).
|
|
350
459
|
* Defaults to the key verbatim.
|
|
351
460
|
*/
|
|
352
461
|
readonly unknownKeyToWire?: (key: string) => string;
|
|
353
462
|
}
|
|
354
463
|
|
|
355
|
-
/**
|
|
356
|
-
* Build an HTTP request from an operation input and its schema, driven by
|
|
357
|
-
* the trait annotations: `Http()` supplies method + URI template, `Label()`
|
|
358
|
-
* members fill URI placeholders, `Header()`/`Query()` members bind to
|
|
359
|
-
* headers/query params, `DeepQuery()` struct members expand into dotted
|
|
360
|
-
* query params, `HttpBody()` sends the member as the whole body,
|
|
361
|
-
* `FormDataFile()` members become multipart file parts, and everything else
|
|
362
|
-
* is a JSON body field (wire-named via `Body()` / `KeyDictionary`).
|
|
363
|
-
*
|
|
364
|
-
* Throws when the input schema is missing the `Http()` trait — a codegen
|
|
365
|
-
* bug, surfaced as a defect by the calling protocol's Effect context.
|
|
366
|
-
*/
|
|
367
464
|
/**
|
|
368
465
|
* Encode a greedy `{name+}` label: the value may span several path
|
|
369
466
|
* segments (e.g. an Azure scope `/subscriptions/{id}/resourceGroups/{rg}`),
|
|
@@ -402,6 +499,131 @@ const flattenToFormPairs = (obj: Record<string, unknown>, prefix = ""): Array<[s
|
|
|
402
499
|
return pairs;
|
|
403
500
|
};
|
|
404
501
|
|
|
502
|
+
/** How an input member binds to the request. */
|
|
503
|
+
type Bind = "label" | "header" | "query" | "deepQuery" | "file" | "httpBody" | "body";
|
|
504
|
+
|
|
505
|
+
/** One input member, with its binding read from the schema once. */
|
|
506
|
+
interface MemberPlan {
|
|
507
|
+
readonly key: string;
|
|
508
|
+
readonly bind: Bind;
|
|
509
|
+
/** Label token, lower-cased header name, query name or body wire name. */
|
|
510
|
+
readonly name: string;
|
|
511
|
+
readonly type: AST.AST;
|
|
512
|
+
/** `LabelEncoding` characters kept unescaped. */
|
|
513
|
+
readonly preserve: string | undefined;
|
|
514
|
+
/** `{name+}` placeholder. */
|
|
515
|
+
readonly greedy: boolean;
|
|
516
|
+
readonly stringEncoded: boolean;
|
|
517
|
+
/** `mapKeys(type, value, "encode", rootDict)` returns the value unchanged. */
|
|
518
|
+
readonly verbatim: boolean;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/** What `buildRequest` reads from an input schema, computed once per schema. */
|
|
522
|
+
interface RequestPlan {
|
|
523
|
+
readonly http: HttpTrait;
|
|
524
|
+
readonly rootDict: KeyDictionaryEntries | undefined;
|
|
525
|
+
readonly members: ReadonlyArray<MemberPlan>;
|
|
526
|
+
readonly known: ReadonlySet<string>;
|
|
527
|
+
/** The schema declares a member that is sent in the body. */
|
|
528
|
+
readonly hasBodyMembers: boolean;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const plans = /*@__PURE__*/ new WeakMap<AST.AST, RequestPlan>();
|
|
532
|
+
|
|
533
|
+
const planFor = (inputAst: AST.AST): RequestPlan => {
|
|
534
|
+
let plan = plans.get(inputAst);
|
|
535
|
+
if (plan !== undefined) return plan;
|
|
536
|
+
const http = getAnn(inputAst, httpSymbol) as HttpTrait | undefined;
|
|
537
|
+
if (!http) {
|
|
538
|
+
throw new Error("operation input is missing the Http() trait");
|
|
539
|
+
}
|
|
540
|
+
const rootDict = getAnn(inputAst, keyDictionarySymbol) as KeyDictionaryEntries | undefined;
|
|
541
|
+
const members: MemberPlan[] = [];
|
|
542
|
+
const known = new Set<string>();
|
|
543
|
+
let hasBodyMembers = false;
|
|
544
|
+
for (const prop of getProps(inputAst)) {
|
|
545
|
+
const key = String(prop.name);
|
|
546
|
+
known.add(key);
|
|
547
|
+
let bind: Bind;
|
|
548
|
+
let name: string;
|
|
549
|
+
let greedy = false;
|
|
550
|
+
if (hasPropAnn(prop, labelSymbol)) {
|
|
551
|
+
bind = "label";
|
|
552
|
+
name = nameOf(prop, labelSymbol);
|
|
553
|
+
greedy = http.uri.includes(`{${name}+}`);
|
|
554
|
+
} else if (hasPropAnn(prop, headerSymbol)) {
|
|
555
|
+
bind = "header";
|
|
556
|
+
name = nameOf(prop, headerSymbol).toLowerCase();
|
|
557
|
+
} else if (hasPropAnn(prop, querySymbol)) {
|
|
558
|
+
bind = "query";
|
|
559
|
+
name = nameOf(prop, querySymbol);
|
|
560
|
+
} else if (hasPropAnn(prop, deepQuerySymbol)) {
|
|
561
|
+
bind = "deepQuery";
|
|
562
|
+
name = nameOf(prop, deepQuerySymbol);
|
|
563
|
+
} else {
|
|
564
|
+
hasBodyMembers = true;
|
|
565
|
+
bind = hasPropAnn(prop, formDataFileSymbol)
|
|
566
|
+
? "file"
|
|
567
|
+
: hasPropAnn(prop, httpBodySymbol)
|
|
568
|
+
? "httpBody"
|
|
569
|
+
: "body";
|
|
570
|
+
name = nameOf(prop, bodySymbol);
|
|
571
|
+
}
|
|
572
|
+
const preserve = getPropAnn(prop, labelEncodingSymbol);
|
|
573
|
+
members.push({
|
|
574
|
+
key,
|
|
575
|
+
bind,
|
|
576
|
+
name,
|
|
577
|
+
type: prop.type,
|
|
578
|
+
preserve: typeof preserve === "string" ? preserve : undefined,
|
|
579
|
+
greedy,
|
|
580
|
+
stringEncoded: hasPropAnn(prop, stringEncodedSymbol),
|
|
581
|
+
verbatim: rootDict === undefined && mapsVerbatim(prop.type, "encode"),
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
plan = { http, rootDict, members, known, hasBodyMembers };
|
|
585
|
+
plans.set(inputAst, plan);
|
|
586
|
+
return plan;
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const emptyRequest = /*@__PURE__*/ HttpClientRequest.get("");
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* `HttpClientRequest.make(method)(url)` with `headers` set, in one object:
|
|
593
|
+
* the same request as `make(...).pipe(setHeaders(headers))` without the
|
|
594
|
+
* intermediate copies. `url` may carry a query string.
|
|
595
|
+
*/
|
|
596
|
+
export const makeRequest = (
|
|
597
|
+
method: HttpTrait["method"],
|
|
598
|
+
url: string,
|
|
599
|
+
headers: Record<string, string>,
|
|
600
|
+
): HttpClientRequest.HttpClientRequest =>
|
|
601
|
+
HttpClientRequest.makeWith(
|
|
602
|
+
method,
|
|
603
|
+
url,
|
|
604
|
+
emptyRequest.urlParams,
|
|
605
|
+
emptyRequest.hash,
|
|
606
|
+
Headers.fromInput(headers),
|
|
607
|
+
HttpBody.empty,
|
|
608
|
+
);
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Build an HTTP request from an operation input and its schema, driven by
|
|
612
|
+
* the trait annotations: `Http()` supplies method + URI template, `Label()`
|
|
613
|
+
* members fill URI placeholders, `Header()`/`Query()` members bind to
|
|
614
|
+
* headers/query params, `DeepQuery()` struct members expand into dotted
|
|
615
|
+
* query params, `HttpBody()` sends the member as the whole body,
|
|
616
|
+
* `FormDataFile()` members become multipart file parts, and everything else
|
|
617
|
+
* is a JSON body field (wire-named via `Body()` / `KeyDictionary`).
|
|
618
|
+
*
|
|
619
|
+
* The schema is read once per input schema (see `planFor`); later calls only
|
|
620
|
+
* walk the input value.
|
|
621
|
+
*
|
|
622
|
+
* Throws when the input schema is missing the `Http()` trait -- a codegen
|
|
623
|
+
* bug -- and, before anything is sent, when a label is `.` or `..` (for a
|
|
624
|
+
* greedy `{name+}` label, when any of its segments is). Each surfaces as a
|
|
625
|
+
* defect by the calling protocol's Effect context.
|
|
626
|
+
*/
|
|
405
627
|
export const buildRequest = ({
|
|
406
628
|
input,
|
|
407
629
|
inputAst,
|
|
@@ -411,112 +633,123 @@ export const buildRequest = ({
|
|
|
411
633
|
unknownKeyToWire,
|
|
412
634
|
}: BuildRequestOptions): HttpClientRequest.HttpClientRequest => {
|
|
413
635
|
const inputObj = (input ?? {}) as Record<string, unknown>;
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
// Root key dictionary: fallback wire mapping for opaque/unknown content
|
|
419
|
-
// the schema doesn't model.
|
|
420
|
-
const rootDict = getAnn(inputAst, keyDictionarySymbol) as KeyDictionaryEntries | undefined;
|
|
636
|
+
const plan = planFor(inputAst);
|
|
637
|
+
const { http, rootDict, members, known } = plan;
|
|
638
|
+
let hasBodyMembers = plan.hasBodyMembers;
|
|
421
639
|
|
|
422
640
|
const headers: Record<string, string> = { ...baseHeaders };
|
|
423
641
|
const body: Record<string, unknown> = {};
|
|
424
|
-
let
|
|
425
|
-
|
|
426
|
-
|
|
642
|
+
let bodyKeys = 0;
|
|
643
|
+
let rawBody: unknown; // whole-body member (T.HttpBody) -- sent as-is
|
|
644
|
+
let files: Array<Blob | File> | undefined;
|
|
645
|
+
let query: URLSearchParams | undefined;
|
|
427
646
|
let uri = http.uri;
|
|
428
|
-
const consumed = new Set<string>();
|
|
429
|
-
let hasBodyMembers = false;
|
|
430
647
|
// Value of a modeled `Content-Type` header member, e.g. R2 putObject's
|
|
431
648
|
// `contentType`. A raw body is sent under it (see below).
|
|
432
649
|
let memberContentType: string | undefined;
|
|
433
650
|
|
|
434
|
-
for (
|
|
435
|
-
const
|
|
436
|
-
|
|
437
|
-
const value = inputObj[key];
|
|
438
|
-
const isBodyMember =
|
|
439
|
-
!hasPropAnn(prop, labelSymbol) &&
|
|
440
|
-
!hasPropAnn(prop, headerSymbol) &&
|
|
441
|
-
!hasPropAnn(prop, querySymbol) &&
|
|
442
|
-
!hasPropAnn(prop, deepQuerySymbol);
|
|
443
|
-
if (isBodyMember) hasBodyMembers = true;
|
|
651
|
+
for (let i = 0; i < members.length; i++) {
|
|
652
|
+
const m = members[i]!;
|
|
653
|
+
const value = inputObj[m.key];
|
|
444
654
|
if (value === undefined) continue;
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
?
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
:
|
|
457
|
-
uri = uri.includes(`{${token}+}`)
|
|
458
|
-
? uri.replace(`{${token}+}`, () => encodeGreedyLabel(String(value)))
|
|
459
|
-
: uri.replace(`{${token}}`, () => label);
|
|
460
|
-
} else if (hasPropAnn(prop, headerSymbol)) {
|
|
461
|
-
const hName = nameOf(prop, headerSymbol).toLowerCase();
|
|
462
|
-
const hVal = String(value);
|
|
463
|
-
headers[hName] = mapMemberHeader ? mapMemberHeader(hName, hVal) : hVal;
|
|
464
|
-
if (hName === "content-type") memberContentType = headers[hName];
|
|
465
|
-
} else if (hasPropAnn(prop, querySymbol)) {
|
|
466
|
-
appendQuery(query, nameOf(prop, querySymbol), value);
|
|
467
|
-
} else if (hasPropAnn(prop, deepQuerySymbol)) {
|
|
468
|
-
// Deep-object query member (T.DeepQuery): a nested struct expands into
|
|
469
|
-
// dotted query keys (`account.id=…`) under the member's wire name.
|
|
470
|
-
// Wire-name the nested content via the schema first, skip null/
|
|
471
|
-
// undefined entries; deeper nesting recurses through appendQuery.
|
|
472
|
-
const base = nameOf(prop, deepQuerySymbol);
|
|
473
|
-
const mapped = mapKeys(prop.type, value, "encode", rootDict);
|
|
474
|
-
if (mapped !== null && typeof mapped === "object") {
|
|
475
|
-
for (const [k, v] of Object.entries(mapped as Record<string, unknown>)) {
|
|
476
|
-
if (v !== undefined && v !== null) {
|
|
477
|
-
appendQuery(query, `${base}.${k}`, v);
|
|
478
|
-
}
|
|
655
|
+
switch (m.bind) {
|
|
656
|
+
case "label": {
|
|
657
|
+
// The URL parser resolves a `.` or `..` segment, percent-encoded or
|
|
658
|
+
// not, so no encoding can carry one: it would address another path
|
|
659
|
+
// (`/projects/p1/branches/..` -> `/projects/p1/`). A greedy label keeps
|
|
660
|
+
// its `/`, so each of its segments counts.
|
|
661
|
+
if (
|
|
662
|
+
(m.greedy ? String(value).split("/") : [String(value)]).some(
|
|
663
|
+
(s) => s === "." || s === "..",
|
|
664
|
+
)
|
|
665
|
+
) {
|
|
666
|
+
throw new Error(`invalid value for input HTTP label: ${m.key}`);
|
|
479
667
|
}
|
|
668
|
+
const encoded = encodeURIComponent(String(value));
|
|
669
|
+
const preserve = m.preserve;
|
|
670
|
+
const label =
|
|
671
|
+
preserve !== undefined
|
|
672
|
+
? encoded.replace(/%[0-9A-F]{2}/g, (escape) => {
|
|
673
|
+
const character = String.fromCharCode(Number.parseInt(escape.slice(1), 16));
|
|
674
|
+
return preserve.includes(character) ? character : escape;
|
|
675
|
+
})
|
|
676
|
+
: encoded;
|
|
677
|
+
uri = m.greedy
|
|
678
|
+
? uri.replace(`{${m.name}+}`, () => encodeGreedyLabel(String(value)))
|
|
679
|
+
: uri.replace(`{${m.name}}`, () => label);
|
|
680
|
+
break;
|
|
480
681
|
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
682
|
+
case "header": {
|
|
683
|
+
const hVal = String(value);
|
|
684
|
+
headers[m.name] = mapMemberHeader ? mapMemberHeader(m.name, hVal) : hVal;
|
|
685
|
+
if (m.name === "content-type") memberContentType = headers[m.name];
|
|
686
|
+
break;
|
|
484
687
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
?
|
|
494
|
-
|
|
688
|
+
case "query":
|
|
689
|
+
appendQuery((query ??= new URLSearchParams()), m.name, value);
|
|
690
|
+
break;
|
|
691
|
+
case "deepQuery": {
|
|
692
|
+
// Deep-object query member (T.DeepQuery): a nested struct expands into
|
|
693
|
+
// dotted query keys (`account.id=...`) under the member's wire name.
|
|
694
|
+
// Wire-name the nested content via the schema first, skip null/
|
|
695
|
+
// undefined entries; deeper nesting recurses through appendQuery.
|
|
696
|
+
const mapped = m.verbatim ? value : mapKeys(m.type, value, "encode", rootDict);
|
|
697
|
+
if (mapped !== null && typeof mapped === "object") {
|
|
698
|
+
for (const [k, v] of Object.entries(mapped as Record<string, unknown>)) {
|
|
699
|
+
if (v !== undefined && v !== null) {
|
|
700
|
+
appendQuery((query ??= new URLSearchParams()), `${m.name}.${k}`, v);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
break;
|
|
705
|
+
}
|
|
706
|
+
case "file":
|
|
707
|
+
files ??= [];
|
|
708
|
+
for (const f of Array.isArray(value) ? value : [value]) {
|
|
709
|
+
files.push(f as Blob | File);
|
|
710
|
+
}
|
|
711
|
+
break;
|
|
712
|
+
case "httpBody":
|
|
713
|
+
rawBody = m.verbatim ? value : mapKeys(m.type, value, "encode", rootDict);
|
|
714
|
+
break;
|
|
715
|
+
default:
|
|
716
|
+
// T.StringEncoded(): the API takes this member's value only as its
|
|
717
|
+
// string spelling (`true` -> `"true"`), while the TS surface keeps the
|
|
718
|
+
// natural type. Explicit here so a JSON body carries the string too,
|
|
719
|
+
// rather than relying on the multipart encoder's own `String()`.
|
|
720
|
+
body[m.name] = m.stringEncoded
|
|
721
|
+
? stringEncode(value)
|
|
722
|
+
: m.verbatim
|
|
723
|
+
? value
|
|
724
|
+
: mapKeys(m.type, value, "encode", rootDict);
|
|
725
|
+
bodyKeys++;
|
|
495
726
|
}
|
|
496
727
|
}
|
|
497
728
|
|
|
498
|
-
// Input keys the schema doesn't know pass through as body fields
|
|
729
|
+
// Input keys the schema doesn't know pass through as body fields -- the
|
|
499
730
|
// generated schemas can lag the real API, and silently dropping them
|
|
500
731
|
// would break working callers. The key dictionary maps them when it knows
|
|
501
732
|
// the key, otherwise `unknownKeyToWire` decides the wire name.
|
|
502
|
-
for (const
|
|
503
|
-
if (
|
|
733
|
+
for (const key in inputObj) {
|
|
734
|
+
if (known.has(key) || !Object.hasOwn(inputObj, key)) continue;
|
|
735
|
+
const value = inputObj[key];
|
|
736
|
+
if (value === undefined) continue;
|
|
504
737
|
const wire = rootDict?.[key] ?? (unknownKeyToWire ? unknownKeyToWire(key) : key);
|
|
505
738
|
body[typeof wire === "string" ? wire : wire[0]] = rootDict
|
|
506
739
|
? mapKeysByDictionary(rootDict, value, "encode")
|
|
507
740
|
: value;
|
|
741
|
+
bodyKeys++;
|
|
508
742
|
hasBodyMembers = true;
|
|
509
743
|
}
|
|
510
744
|
|
|
511
|
-
const qs = query.toString();
|
|
512
|
-
const url = `${baseUrl}${uri}
|
|
513
|
-
if (
|
|
745
|
+
const qs = query === undefined ? "" : query.toString();
|
|
746
|
+
const url = qs ? `${baseUrl}${uri}?${qs}` : `${baseUrl}${uri}`;
|
|
747
|
+
if (debugHttp()) {
|
|
514
748
|
console.error(`[distilled] -> ${http.method} ${http.uri}`);
|
|
515
749
|
}
|
|
516
750
|
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
);
|
|
751
|
+
const request = makeRequest(http.method, url, headers);
|
|
752
|
+
const bodied = !BODYLESS.has(http.method);
|
|
520
753
|
if (http.contentType === "multipart") {
|
|
521
754
|
// Multipart upload: each body member is a form part (objects
|
|
522
755
|
// JSON-encoded under their wire name), each file appends under its own
|
|
@@ -552,28 +785,29 @@ export const buildRequest = ({
|
|
|
552
785
|
form.append(key, String(value));
|
|
553
786
|
}
|
|
554
787
|
}
|
|
555
|
-
for (const f of files) {
|
|
788
|
+
for (const f of files ?? []) {
|
|
556
789
|
const filename = (f as File).name ?? "file";
|
|
557
790
|
form.append(filename, f, filename);
|
|
558
791
|
}
|
|
559
|
-
|
|
560
|
-
}
|
|
792
|
+
return HttpClientRequest.setBody(request, HttpBody.formData(form));
|
|
793
|
+
}
|
|
794
|
+
if (http.contentType === "form-urlencoded" && bodied) {
|
|
561
795
|
// application/x-www-form-urlencoded with Stripe-style deepObject bracket
|
|
562
|
-
// notation: { shipping: { address: { city } } }
|
|
563
|
-
// arrays indexed ({ expand: ["a"] }
|
|
796
|
+
// notation: { shipping: { address: { city } } } -> shipping[address][city],
|
|
797
|
+
// arrays indexed ({ expand: ["a"] } -> expand[0]=a).
|
|
564
798
|
const source =
|
|
565
799
|
rawBody !== undefined && typeof rawBody === "object"
|
|
566
800
|
? (rawBody as Record<string, unknown>)
|
|
567
801
|
: body;
|
|
568
802
|
const params = new URLSearchParams();
|
|
569
803
|
for (const [k, v] of flattenToFormPairs(source)) params.append(k, v);
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
),
|
|
804
|
+
return HttpClientRequest.setBody(
|
|
805
|
+
request,
|
|
806
|
+
HttpBody.text(params.toString(), "application/x-www-form-urlencoded"),
|
|
574
807
|
);
|
|
575
|
-
}
|
|
576
|
-
|
|
808
|
+
}
|
|
809
|
+
if (rawBody !== undefined && bodied) {
|
|
810
|
+
// Whole-body member (raw arrays/scalars) -- sent as the body itself.
|
|
577
811
|
// Binary payloads (Blob / ArrayBuffer / Uint8Array) send verbatim
|
|
578
812
|
// (raw object uploads). With a bodyMediaType, the member is a
|
|
579
813
|
// preserialized payload (string / bytes) sent verbatim; otherwise it's
|
|
@@ -586,38 +820,37 @@ export const buildRequest = ({
|
|
|
586
820
|
const mediaType = memberContentType ?? http.bodyMediaType;
|
|
587
821
|
if (rawBody instanceof Blob || rawBody instanceof ArrayBuffer) {
|
|
588
822
|
// With no media type at all, fetch falls back to the Blob's own type.
|
|
589
|
-
request
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
),
|
|
823
|
+
return HttpClientRequest.setBody(request, HttpBody.raw(rawBody, { contentType: mediaType }));
|
|
824
|
+
}
|
|
825
|
+
if (rawBody instanceof Uint8Array) {
|
|
826
|
+
return HttpClientRequest.setBody(request, HttpBody.uint8Array(rawBody, mediaType));
|
|
827
|
+
}
|
|
828
|
+
if (http.bodyMediaType) {
|
|
829
|
+
return HttpClientRequest.setBody(
|
|
830
|
+
request,
|
|
831
|
+
typeof rawBody === "string"
|
|
832
|
+
? HttpBody.text(rawBody, mediaType)
|
|
833
|
+
: HttpBody.uint8Array(rawBody as Uint8Array, mediaType),
|
|
601
834
|
);
|
|
602
|
-
} else {
|
|
603
|
-
request = request.pipe(HttpClientRequest.bodyJsonUnsafe(rawBody));
|
|
604
835
|
}
|
|
605
|
-
|
|
836
|
+
return HttpClientRequest.setBody(request, HttpBody.jsonUnsafe(rawBody));
|
|
837
|
+
}
|
|
838
|
+
if (
|
|
606
839
|
// A GET/HEAD sends a body only when body members actually carry values.
|
|
607
840
|
// Some APIs really do document GET-with-body: MongoDB Atlas's
|
|
608
|
-
//
|
|
841
|
+
// `.../lineItems:search` declares a `requestBody` on `get`, and AWS EFS's
|
|
609
842
|
// DescribeAccountPreferences takes `{ MaxResults, NextToken }` on a GET.
|
|
610
|
-
// Suppressing it outright dropped those members silently
|
|
843
|
+
// Suppressing it outright dropped those members silently -- the request
|
|
611
844
|
// succeeded, unfiltered, with nothing on the wire to show why.
|
|
612
845
|
//
|
|
613
846
|
// The `{}`-when-empty rule below stays limited to methods that expect a
|
|
614
847
|
// body, so a bodyless method with nothing set still sends nothing.
|
|
615
|
-
|
|
616
|
-
(hasBodyMembers &&
|
|
848
|
+
bodyKeys > 0 ||
|
|
849
|
+
(hasBodyMembers && bodied && http.method !== "DELETE")
|
|
617
850
|
) {
|
|
618
|
-
// Send `{}` rather than no body when the schema declares body members
|
|
851
|
+
// Send `{}` rather than no body when the schema declares body members --
|
|
619
852
|
// some endpoints reject a missing JSON body outright.
|
|
620
|
-
|
|
853
|
+
return HttpClientRequest.setBody(request, HttpBody.jsonUnsafe(body));
|
|
621
854
|
}
|
|
622
855
|
return request;
|
|
623
856
|
};
|