@orpc/openapi 2.0.0-beta.49 → 2.0.0-beta.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/aws-lambda/index.d.ts +3 -15
- package/dist/adapters/aws-lambda/index.js +1 -1
- package/dist/adapters/aws-lambda/openapi-handler.d.ts +8 -9
- package/dist/adapters/aws-lambda/openapi-handler.js +19 -17
- package/dist/adapters/fastify/index.d.ts +2 -1
- package/dist/adapters/fastify/index.js +1 -1
- package/dist/adapters/fastify/openapi-handler.d.ts +8 -9
- package/dist/adapters/fastify/openapi-handler.js +16 -14
- package/dist/adapters/fetch/index.d.ts +3 -2
- package/dist/adapters/fetch/index.js +2 -2
- package/dist/adapters/fetch/openapi-handler.d.ts +8 -9
- package/dist/adapters/fetch/openapi-handler.js +16 -14
- package/dist/adapters/fetch/openapi-link.d.ts +9 -10
- package/dist/adapters/fetch/openapi-link.js +21 -19
- package/dist/adapters/node/index.d.ts +2 -1
- package/dist/adapters/node/index.js +1 -1
- package/dist/adapters/node/openapi-handler.d.ts +8 -9
- package/dist/adapters/node/openapi-handler.js +16 -14
- package/dist/adapters/standard/index.d.ts +4 -3
- package/dist/adapters/standard/index.js +3 -3
- package/dist/adapters/standard/openapi-handler-codec.d.ts +56 -57
- package/dist/adapters/standard/openapi-handler-codec.js +143 -205
- package/dist/adapters/standard/openapi-link-codec.d.ts +55 -59
- package/dist/adapters/standard/openapi-link-codec.js +262 -377
- package/dist/adapters/standard/openapi-matcher.d.ts +26 -26
- package/dist/adapters/standard/openapi-matcher.js +78 -108
- package/dist/adapters/standard/utils.js +28 -44
- package/dist/bracket-notation.d.ts +22 -22
- package/dist/bracket-notation.js +123 -173
- package/dist/client-factory.d.ts +12 -12
- package/dist/client-factory.js +13 -11
- package/dist/constants.d.ts +1 -4
- package/dist/constants.js +5 -4
- package/dist/extensions/route.d.ts +36 -36
- package/dist/extensions/route.js +7 -6
- package/dist/helpers/form-data.d.ts +1 -1
- package/dist/helpers/form-data.js +74 -86
- package/dist/helpers/index.d.ts +2 -1
- package/dist/helpers/index.js +1 -1
- package/dist/index.d.ts +15 -13
- package/dist/index.js +11 -11
- package/dist/meta.d.ts +298 -298
- package/dist/meta.js +54 -72
- package/dist/openapi-generator-components.d.ts +2 -31
- package/dist/openapi-generator-components.js +200 -248
- package/dist/openapi-generator-operation.d.ts +12 -33
- package/dist/openapi-generator-operation.js +369 -437
- package/dist/openapi-generator.d.ts +75 -80
- package/dist/openapi-generator.js +114 -142
- package/dist/openapi-json-serializer.d.ts +65 -65
- package/dist/openapi-json-serializer.js +218 -257
- package/dist/openapi-serializer.d.ts +31 -32
- package/dist/openapi-serializer.js +68 -92
- package/dist/plugins/index.d.ts +2 -1
- package/dist/plugins/index.js +1 -1
- package/dist/plugins/openapi-reference.d.ts +99 -102
- package/dist/plugins/openapi-reference.js +109 -132
- package/dist/router-utils.d.ts +7 -10
- package/dist/router-utils.js +30 -34
- package/dist/types.d.ts +8 -12
- package/dist/utils.d.ts +5 -10
- package/dist/utils.js +39 -42
- package/package.json +12 -12
- package/dist/adapters/standard/utils.d.ts +0 -4
- package/dist/types.js +0 -1
|
@@ -1,89 +1,78 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
|
|
1
|
+
import "../../constants.js";
|
|
2
|
+
import { getOpenAPIMeta } from "../../meta.js";
|
|
3
|
+
import { OpenAPISerializer } from "../../openapi-serializer.js";
|
|
4
|
+
import { getDynamicPathParams, isBodylessMethod } from "../../utils.js";
|
|
5
|
+
import { serializeHeaders } from "./utils.js";
|
|
6
|
+
import { isNonPrimitive, mergeHttpPath, pathToHttpPath, safeEncodeURIComponent, stringifyJSON, value } from "@orpc/shared";
|
|
7
|
+
import { ProcedureContract, getRouterContract } from "@orpc/contract";
|
|
8
|
+
import { MalformedResponseError, ResponseError, createORPCErrorFromJson, isORPCErrorJson } from "@orpc/client";
|
|
9
|
+
import { unlazy } from "@orpc/server";
|
|
10
|
+
import { flattenStandardHeader, mergeStandardHeaders, parseStandardUrl } from "@standard-server/core";
|
|
11
|
+
import { toStandardHeaders } from "@standard-server/fetch";
|
|
12
|
+
// src/adapters/standard/openapi-link-codec.ts
|
|
13
13
|
const DOT_SEGMENT_REGEX = /(?:^|\/)\.{1,2}(?:\/|$)/;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
url,
|
|
78
|
-
method,
|
|
79
|
-
headers,
|
|
80
|
-
body: this.serializer.serialize(data),
|
|
81
|
-
signal: options.signal,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
if (!isValidDetailedInput(input)) {
|
|
85
|
-
throw new TypeError(`
|
|
86
|
-
Invalid "detailed" input structure in call to procedure (${path.join('.')}):
|
|
14
|
+
var OpenAPILinkCodec = class {
|
|
15
|
+
router;
|
|
16
|
+
baseUrl;
|
|
17
|
+
headers;
|
|
18
|
+
serializer;
|
|
19
|
+
errorResponseBodyDecoder;
|
|
20
|
+
errorResponseMediaType;
|
|
21
|
+
constructor(router, options = {}) {
|
|
22
|
+
this.router = router;
|
|
23
|
+
this.baseUrl = options.url ?? "/";
|
|
24
|
+
this.headers = options.headers ?? {};
|
|
25
|
+
this.serializer = options.serializer ?? new OpenAPISerializer();
|
|
26
|
+
this.errorResponseBodyDecoder = options.errorResponseBodyDecoder ?? ((_body, _response, defaultError) => defaultError());
|
|
27
|
+
this.errorResponseMediaType = toMediaType(options.errorResponseContentType);
|
|
28
|
+
}
|
|
29
|
+
async encodeInput(input, path, options) {
|
|
30
|
+
let headers = toResolvedStandardHeaders(await value(this.headers, options, path, input));
|
|
31
|
+
if (options.lastEventId !== void 0) headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
32
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
33
|
+
const procedure = await this.resolveProcedure(path);
|
|
34
|
+
const meta = getOpenAPIMeta(procedure);
|
|
35
|
+
const method = meta?.method ?? "POST";
|
|
36
|
+
const inputStructure = meta?.inputStructure ?? "compact";
|
|
37
|
+
let pathname = meta?.path ?? pathToHttpPath(path);
|
|
38
|
+
if (meta?.prefix) pathname = mergeHttpPath(meta.prefix, pathname);
|
|
39
|
+
const [basePathname, baseSearch, baseHash] = parseStandardUrl(baseUrl);
|
|
40
|
+
const dynamicParams = getDynamicPathParams(pathname);
|
|
41
|
+
if (inputStructure === "compact") {
|
|
42
|
+
let data = input;
|
|
43
|
+
if (dynamicParams?.length) {
|
|
44
|
+
if (!isNonPrimitive(input)) throw new TypeError(`Input must be an object with "compact" input structure when the path has dynamic params (${dynamicParams.map((p) => p.parameterName).join(", ")}) in call to procedure (${path.join(".")}).`);
|
|
45
|
+
const remaining = { ...input };
|
|
46
|
+
for (let i = dynamicParams.length - 1; i >= 0; i--) {
|
|
47
|
+
const param = dynamicParams[i];
|
|
48
|
+
const encoded = this.encodePathParam(input[param.parameterName], param, meta?.paramsStyles?.[param.parameterName], path);
|
|
49
|
+
pathname = `${pathname.slice(0, param.startIndex)}${encoded}${pathname.slice(param.startIndex + param.segment.length)}`;
|
|
50
|
+
delete remaining[param.parameterName];
|
|
51
|
+
}
|
|
52
|
+
data = Object.keys(remaining).length > 0 ? remaining : void 0;
|
|
53
|
+
}
|
|
54
|
+
pathname = mergeHttpPath(basePathname, pathname);
|
|
55
|
+
if (isBodylessMethod(method)) {
|
|
56
|
+
const search = combineSearch(baseSearch, this.serializeQueryString(data, meta?.queryStyles));
|
|
57
|
+
const url = `${pathname}${search ?? ""}${baseHash ?? ""}`;
|
|
58
|
+
return {
|
|
59
|
+
body: void 0,
|
|
60
|
+
method,
|
|
61
|
+
headers,
|
|
62
|
+
url,
|
|
63
|
+
signal: options.signal
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
url: `${pathname}${baseSearch ?? ""}${baseHash ?? ""}`,
|
|
68
|
+
method,
|
|
69
|
+
headers,
|
|
70
|
+
body: this.serializer.serialize(data),
|
|
71
|
+
signal: options.signal
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (!isValidDetailedInput(input)) throw new TypeError(`
|
|
75
|
+
Invalid "detailed" input structure in call to procedure (${path.join(".")}):
|
|
87
76
|
• Expected an object or undefined with optional properties:
|
|
88
77
|
- params (object, required when the path has dynamic params)
|
|
89
78
|
- query (object)
|
|
@@ -93,313 +82,209 @@ export class OpenAPILinkCodec {
|
|
|
93
82
|
Actual value:
|
|
94
83
|
${stringifyJSON(input)}
|
|
95
84
|
`);
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
const serialized = this.serializeURLValue(value);
|
|
255
|
-
if (serialized !== undefined && serialized !== null) {
|
|
256
|
-
// oxlint-disable-next-line typescript/no-base-to-string
|
|
257
|
-
query += `&${encodeURLSearchParamComponent(key)}=${encodeURLSearchParamComponent(String(serialized))}`;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
const form = this.serializer.serialize(remaining, { asFormData: true });
|
|
262
|
-
query = `${toURLSearchParams(form).toString()}${query}`;
|
|
263
|
-
if (query.startsWith('&')) {
|
|
264
|
-
query = query.slice(1);
|
|
265
|
-
}
|
|
266
|
-
return query || undefined;
|
|
267
|
-
}
|
|
268
|
-
async decodeResponse(response, path, _options) {
|
|
269
|
-
const isOk = response.status < 400;
|
|
270
|
-
const procedure = await this.resolveProcedure(path);
|
|
271
|
-
const meta = getOpenAPIMeta(procedure);
|
|
272
|
-
const errorBodyHint = !isOk &&
|
|
273
|
-
this.errorResponseMediaType !== undefined &&
|
|
274
|
-
toMediaType(flattenStandardHeader(response.headers['content-type'])) ===
|
|
275
|
-
this.errorResponseMediaType
|
|
276
|
-
? 'json'
|
|
277
|
-
: undefined;
|
|
278
|
-
const body = await response.resolveBody(isOk ? meta?.responseBodyHint : errorBodyHint);
|
|
279
|
-
const deserialized = await (async () => {
|
|
280
|
-
try {
|
|
281
|
-
return this.serializer.deserialize(body);
|
|
282
|
-
}
|
|
283
|
-
catch (error) {
|
|
284
|
-
throw new MalformedResponseError({
|
|
285
|
-
message: 'Invalid OpenAPI response format.',
|
|
286
|
-
response: { status: response.status, headers: response.headers, body },
|
|
287
|
-
cause: error,
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
})();
|
|
291
|
-
if (!isOk) {
|
|
292
|
-
const defaultError = () => {
|
|
293
|
-
if (isORPCErrorJson(deserialized)) {
|
|
294
|
-
return createORPCErrorFromJson(deserialized, {
|
|
295
|
-
cause: new ResponseError({
|
|
296
|
-
response: { status: response.status, headers: response.headers, body },
|
|
297
|
-
}),
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
return new MalformedResponseError({
|
|
301
|
-
response: { headers: response.headers, status: response.status, body },
|
|
302
|
-
});
|
|
303
|
-
};
|
|
304
|
-
return {
|
|
305
|
-
kind: 'error',
|
|
306
|
-
error: this.errorResponseBodyDecoder(deserialized, response, defaultError),
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
const outputStructure = meta?.outputStructure ?? DEFAULT_OPENAPI_OUTPUT_STRUCTURE;
|
|
310
|
-
return outputStructure === 'compact'
|
|
311
|
-
? { kind: 'output', output: deserialized }
|
|
312
|
-
: {
|
|
313
|
-
kind: 'output',
|
|
314
|
-
output: {
|
|
315
|
-
status: response.status,
|
|
316
|
-
headers: response.headers,
|
|
317
|
-
body: deserialized,
|
|
318
|
-
},
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
async resolveProcedure(path) {
|
|
322
|
-
const { default: maybeProcedure } = await unlazy(getRouterContract(this.router, path));
|
|
323
|
-
if (!(maybeProcedure instanceof ProcedureContract)) {
|
|
324
|
-
throw new TypeError(`Expected a procedure or contract at path (${path.join('.')})`);
|
|
325
|
-
}
|
|
326
|
-
return maybeProcedure;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
85
|
+
if (dynamicParams?.length) {
|
|
86
|
+
if (!input?.params) throw new TypeError(`The "params" property is required for "detailed" input when the path has dynamic params (${dynamicParams.map((p) => p.parameterName).join(", ")}) in call to procedure (${path.join(".")}).`);
|
|
87
|
+
for (let i = dynamicParams.length - 1; i >= 0; i--) {
|
|
88
|
+
const param = dynamicParams[i];
|
|
89
|
+
const val = input.params[param.parameterName];
|
|
90
|
+
const encoded = this.encodePathParam(val, param, meta?.paramsStyles?.[param.parameterName], path);
|
|
91
|
+
pathname = `${pathname.slice(0, param.startIndex)}${encoded}${pathname.slice(param.startIndex + param.segment.length)}`;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (input?.headers) headers = mergeStandardHeaders(headers, serializeHeaders(input.headers, this.serializer));
|
|
95
|
+
pathname = mergeHttpPath(basePathname, pathname);
|
|
96
|
+
const search = combineSearch(baseSearch, this.serializeQueryString(input?.query, meta?.queryStyles));
|
|
97
|
+
const url = `${pathname}${search ?? ""}${baseHash ?? ""}`;
|
|
98
|
+
if (isBodylessMethod(method)) return {
|
|
99
|
+
body: void 0,
|
|
100
|
+
method,
|
|
101
|
+
headers,
|
|
102
|
+
url,
|
|
103
|
+
signal: options.signal
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
url,
|
|
107
|
+
method,
|
|
108
|
+
headers,
|
|
109
|
+
body: this.serializer.serialize(input?.body),
|
|
110
|
+
signal: options.signal
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
encodePathParam(val, param, style, path) {
|
|
114
|
+
let encoded;
|
|
115
|
+
if (style === "comma-delimited-array" && Array.isArray(val)) encoded = val.map((val) => this.serializeURLValue(val)).filter((val) => val !== void 0 && val !== null).map((val) => safeEncodeURIComponent(String(val))).join(",");
|
|
116
|
+
else if (style === "comma-delimited-object" && isNonPrimitive(val)) encoded = Object.entries(val).map(([key, val]) => [key, this.serializeURLValue(val)]).filter(([, val]) => val !== void 0 && val !== null).map(([key, val]) => `${safeEncodeURIComponent(String(key))},${safeEncodeURIComponent(String(val))}`).join(",");
|
|
117
|
+
else {
|
|
118
|
+
const serialized = this.serializeURLValue(val);
|
|
119
|
+
if (serialized !== void 0 && serialized !== null) {
|
|
120
|
+
if (param.allowsSlash) encoded = String(serialized).split("/").map(safeEncodeURIComponent).join("/");
|
|
121
|
+
else encoded = safeEncodeURIComponent(String(serialized));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (!encoded) throw new TypeError(`Path param "${param.parameterName}" cannot be empty in call to procedure (${path.join(".")}).`);
|
|
125
|
+
if (DOT_SEGMENT_REGEX.test(encoded)) throw new TypeError(`Path param "${param.parameterName}" cannot contain "." or ".." segments in call to procedure (${path.join(".")}).`);
|
|
126
|
+
if (encoded.startsWith("/")) encoded = `%2F${encoded.slice(1)}`;
|
|
127
|
+
return encoded;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Serializes a single path/query value, like header values, so body-level
|
|
131
|
+
* serialize defaults (e.g. `asFormData`) never apply to URLs.
|
|
132
|
+
*/
|
|
133
|
+
serializeURLValue(value) {
|
|
134
|
+
return value === void 0 ? void 0 : this.serializer.serializeJson(value).json;
|
|
135
|
+
}
|
|
136
|
+
serializeQueryString(data, queryStyles) {
|
|
137
|
+
if (!queryStyles || !isNonPrimitive(data)) return toURLSearchParams(this.serializer.serialize(data, { asFormData: true })).toString();
|
|
138
|
+
const remaining = { ...data };
|
|
139
|
+
let query = "";
|
|
140
|
+
Object.entries(queryStyles).forEach(([key, style]) => {
|
|
141
|
+
if (style === void 0) return;
|
|
142
|
+
const value = remaining[key];
|
|
143
|
+
delete remaining[key];
|
|
144
|
+
if (style === "primitive") {
|
|
145
|
+
const serialized = this.serializeURLValue(value);
|
|
146
|
+
if (serialized !== void 0 && serialized !== null) query += `&${encodeURLSearchParamComponent(key)}=${encodeURLSearchParamComponent(String(serialized))}`;
|
|
147
|
+
} else if (style === "array" && Array.isArray(value)) {
|
|
148
|
+
const encodedKey = encodeURLSearchParamComponent(key);
|
|
149
|
+
value.forEach((v) => {
|
|
150
|
+
const s = this.serializeURLValue(v);
|
|
151
|
+
if (s !== void 0 && s !== null) query += `&${encodedKey}=${encodeURLSearchParamComponent(String(s))}`;
|
|
152
|
+
});
|
|
153
|
+
} else if (style === "json") {
|
|
154
|
+
const serialized = this.serializeURLValue(value);
|
|
155
|
+
if (serialized !== void 0) query += `&${encodeURLSearchParamComponent(key)}=${encodeURLSearchParamComponent(stringifyJSON(serialized))}`;
|
|
156
|
+
} else if (style === "comma-delimited-array" && Array.isArray(value)) {
|
|
157
|
+
const encodedValue = encodeDelimitedArray(value.map((v) => this.serializeURLValue(v)), ",");
|
|
158
|
+
if (encodedValue !== void 0) query += `&${encodeURLSearchParamComponent(key)}=${encodedValue}`;
|
|
159
|
+
} else if (style === "comma-delimited-object" && isNonPrimitive(value)) {
|
|
160
|
+
const encodedValue = encodeDelimitedObject(Object.entries(value).map(([key, value]) => [key, this.serializeURLValue(value)]), ",");
|
|
161
|
+
if (encodedValue !== void 0) query += `&${encodeURLSearchParamComponent(key)}=${encodedValue}`;
|
|
162
|
+
} else if (style === "pipe-delimited-array" && Array.isArray(value)) {
|
|
163
|
+
const encodedValue = encodeDelimitedArray(value.map((v) => this.serializeURLValue(v)), "%7C");
|
|
164
|
+
if (encodedValue !== void 0) query += `&${encodeURLSearchParamComponent(key)}=${encodedValue}`;
|
|
165
|
+
} else if (style === "pipe-delimited-object" && isNonPrimitive(value)) {
|
|
166
|
+
const encodedValue = encodeDelimitedObject(Object.entries(value).map(([key, value]) => [key, this.serializeURLValue(value)]), "%7C");
|
|
167
|
+
if (encodedValue !== void 0) query += `&${encodeURLSearchParamComponent(key)}=${encodedValue}`;
|
|
168
|
+
} else if (style === "space-delimited-array" && Array.isArray(value)) {
|
|
169
|
+
const encodedValue = encodeDelimitedArray(value.map((v) => this.serializeURLValue(v)), "%20");
|
|
170
|
+
if (encodedValue !== void 0) query += `&${encodeURLSearchParamComponent(key)}=${encodedValue}`;
|
|
171
|
+
} else if (style === "space-delimited-object" && isNonPrimitive(value)) {
|
|
172
|
+
const encodedValue = encodeDelimitedObject(Object.entries(value).map(([key, value]) => [key, this.serializeURLValue(value)]), "%20");
|
|
173
|
+
if (encodedValue !== void 0) query += `&${encodeURLSearchParamComponent(key)}=${encodedValue}`;
|
|
174
|
+
} else {
|
|
175
|
+
const serialized = this.serializeURLValue(value);
|
|
176
|
+
if (serialized !== void 0 && serialized !== null) query += `&${encodeURLSearchParamComponent(key)}=${encodeURLSearchParamComponent(String(serialized))}`;
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
query = `${toURLSearchParams(this.serializer.serialize(remaining, { asFormData: true })).toString()}${query}`;
|
|
180
|
+
if (query.startsWith("&")) query = query.slice(1);
|
|
181
|
+
return query || void 0;
|
|
182
|
+
}
|
|
183
|
+
async decodeResponse(response, path, _options) {
|
|
184
|
+
const isOk = response.status < 400;
|
|
185
|
+
const procedure = await this.resolveProcedure(path);
|
|
186
|
+
const meta = getOpenAPIMeta(procedure);
|
|
187
|
+
const errorBodyHint = !isOk && this.errorResponseMediaType !== void 0 && toMediaType(flattenStandardHeader(response.headers["content-type"])) === this.errorResponseMediaType ? "json" : void 0;
|
|
188
|
+
const body = await response.resolveBody(isOk ? meta?.responseBodyHint : errorBodyHint);
|
|
189
|
+
const deserialized = await (async () => {
|
|
190
|
+
try {
|
|
191
|
+
return this.serializer.deserialize(body);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
throw new MalformedResponseError({
|
|
194
|
+
message: "Invalid OpenAPI response format.",
|
|
195
|
+
response: {
|
|
196
|
+
status: response.status,
|
|
197
|
+
headers: response.headers,
|
|
198
|
+
body
|
|
199
|
+
},
|
|
200
|
+
cause: error
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
})();
|
|
204
|
+
if (!isOk) {
|
|
205
|
+
const defaultError = () => {
|
|
206
|
+
if (isORPCErrorJson(deserialized)) return createORPCErrorFromJson(deserialized, { cause: new ResponseError({ response: {
|
|
207
|
+
status: response.status,
|
|
208
|
+
headers: response.headers,
|
|
209
|
+
body
|
|
210
|
+
} }) });
|
|
211
|
+
return new MalformedResponseError({ response: {
|
|
212
|
+
headers: response.headers,
|
|
213
|
+
status: response.status,
|
|
214
|
+
body
|
|
215
|
+
} });
|
|
216
|
+
};
|
|
217
|
+
return {
|
|
218
|
+
kind: "error",
|
|
219
|
+
error: this.errorResponseBodyDecoder(deserialized, response, defaultError)
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
return (meta?.outputStructure ?? "compact") === "compact" ? {
|
|
223
|
+
kind: "output",
|
|
224
|
+
output: deserialized
|
|
225
|
+
} : {
|
|
226
|
+
kind: "output",
|
|
227
|
+
output: {
|
|
228
|
+
status: response.status,
|
|
229
|
+
headers: response.headers,
|
|
230
|
+
body: deserialized
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
async resolveProcedure(path) {
|
|
235
|
+
const { default: maybeProcedure } = await unlazy(getRouterContract(this.router, path));
|
|
236
|
+
if (!(maybeProcedure instanceof ProcedureContract)) throw new TypeError(`Expected a procedure or contract at path (${path.join(".")})`);
|
|
237
|
+
return maybeProcedure;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
329
240
|
function combineSearch(baseSearch, additionalSearch) {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
return baseSearch;
|
|
335
|
-
}
|
|
336
|
-
if (!baseSearch) {
|
|
337
|
-
return `?${additionalSearch}`;
|
|
338
|
-
}
|
|
339
|
-
return `${baseSearch}&${additionalSearch}`;
|
|
241
|
+
if (!baseSearch && !additionalSearch) return;
|
|
242
|
+
if (!additionalSearch) return baseSearch;
|
|
243
|
+
if (!baseSearch) return `?${additionalSearch}`;
|
|
244
|
+
return `${baseSearch}&${additionalSearch}`;
|
|
340
245
|
}
|
|
341
246
|
/**
|
|
342
|
-
|
|
343
|
-
|
|
247
|
+
* Strips parameters and normalizes case, e.g. `Application/Problem+JSON; charset=utf-8` -> `application/problem+json`.
|
|
248
|
+
*/
|
|
344
249
|
function toMediaType(contentType) {
|
|
345
|
-
|
|
250
|
+
return contentType?.split(";")[0]?.trim().toLowerCase();
|
|
346
251
|
}
|
|
347
252
|
function toResolvedStandardHeaders(headers) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
356
|
-
return headers;
|
|
253
|
+
/**
|
|
254
|
+
* Headers class might not be available in some environments,
|
|
255
|
+
* so we check for the existence of `forEach` and `get`
|
|
256
|
+
* methods to determine if it's a Headers instance.
|
|
257
|
+
*/
|
|
258
|
+
if (typeof headers.forEach === "function") return toStandardHeaders(headers);
|
|
259
|
+
return headers;
|
|
357
260
|
}
|
|
358
261
|
function isValidDetailedInput(input) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
if (input.query !== undefined && !isNonPrimitive(input.query)) {
|
|
366
|
-
return false;
|
|
367
|
-
}
|
|
368
|
-
if (input.headers !== undefined && !isNonPrimitive(input.headers)) {
|
|
369
|
-
return false;
|
|
370
|
-
}
|
|
371
|
-
return true;
|
|
262
|
+
if (!isNonPrimitive(input)) return input === void 0;
|
|
263
|
+
if (input.params !== void 0 && !isNonPrimitive(input.params)) return false;
|
|
264
|
+
if (input.query !== void 0 && !isNonPrimitive(input.query)) return false;
|
|
265
|
+
if (input.headers !== void 0 && !isNonPrimitive(input.headers)) return false;
|
|
266
|
+
return true;
|
|
372
267
|
}
|
|
373
268
|
/**
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
269
|
+
* Encode a query parameter value using URLSearchParams semantics.
|
|
270
|
+
* Prefer this over safeEncodeURIComponent for query-string values.
|
|
271
|
+
*/
|
|
377
272
|
function encodeURLSearchParamComponent(value) {
|
|
378
|
-
|
|
273
|
+
return new URLSearchParams({ "": value }).toString().slice(1);
|
|
379
274
|
}
|
|
380
275
|
function toURLSearchParams(form) {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
params.append(key, String(value));
|
|
385
|
-
}
|
|
386
|
-
return params;
|
|
276
|
+
const params = new URLSearchParams();
|
|
277
|
+
for (const [key, value] of form) params.append(key, String(value));
|
|
278
|
+
return params;
|
|
387
279
|
}
|
|
388
280
|
function encodeDelimitedArray(serializedValues, encodedDelimiter) {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
return strings.map(encodeURLSearchParamComponent).join(encodedDelimiter);
|
|
281
|
+
const strings = serializedValues.filter((v) => v !== null && v !== void 0).map(String);
|
|
282
|
+
if (!strings.length) return;
|
|
283
|
+
return strings.map(encodeURLSearchParamComponent).join(encodedDelimiter);
|
|
394
284
|
}
|
|
395
285
|
function encodeDelimitedObject(entries, encodedDelimiter) {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
if (!strings.length) {
|
|
400
|
-
return undefined;
|
|
401
|
-
}
|
|
402
|
-
return strings
|
|
403
|
-
.map(([key, value]) => `${encodeURLSearchParamComponent(key)}${encodedDelimiter}${encodeURLSearchParamComponent(value)}`)
|
|
404
|
-
.join(encodedDelimiter);
|
|
286
|
+
const strings = entries.filter(([, v]) => v !== null && v !== void 0).map(([k, v]) => [k, String(v)]);
|
|
287
|
+
if (!strings.length) return;
|
|
288
|
+
return strings.map(([key, value]) => `${encodeURLSearchParamComponent(key)}${encodedDelimiter}${encodeURLSearchParamComponent(value)}`).join(encodedDelimiter);
|
|
405
289
|
}
|
|
290
|
+
export { OpenAPILinkCodec };
|