openapi-typesafe-fetch 0.0.0 → 1.0.0-rc.0
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/LICENSE +22 -0
- package/README.md +52 -1
- package/dist/index.cjs +611 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +344 -0
- package/dist/index.d.mts +342 -0
- package/dist/index.d.ts +344 -0
- package/dist/index.mjs +595 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +87 -4
- package/src/index.d.ts +352 -0
- package/src/index.js +797 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
const PATH_PARAM_RE = /\{[^{}]+\}/g;
|
|
2
|
+
const DOT_SEGMENT_RE = /(^|\/)\.\.?(\/|$)/;
|
|
3
|
+
const JSON_MEDIA_TYPE_RE = /json/i;
|
|
4
|
+
const FORM_MEDIA_TYPE = "application/x-www-form-urlencoded";
|
|
5
|
+
const MULTIPART_MEDIA_TYPE_RE = /^multipart\/form-data(;|$)/i;
|
|
6
|
+
const NESTED_FORM_VALUE_MESSAGE = "Nested form values need your own `bodySerializer()`.";
|
|
7
|
+
const BOUNDARY_PARAMETER_RE = /;\s*boundary=/i;
|
|
8
|
+
const supportsRequestInitExt = () => {
|
|
9
|
+
return typeof process === "object" && process.versions?.undici;
|
|
10
|
+
};
|
|
11
|
+
let requestIdCount = 0;
|
|
12
|
+
function randomID() {
|
|
13
|
+
requestIdCount += 1;
|
|
14
|
+
return requestIdCount.toString(36);
|
|
15
|
+
}
|
|
16
|
+
function createClient(clientOptions) {
|
|
17
|
+
let {
|
|
18
|
+
baseUrl = "",
|
|
19
|
+
Request: CustomRequest = globalThis.Request,
|
|
20
|
+
fetch: baseFetch = globalThis.fetch,
|
|
21
|
+
querySerializer: globalQuerySerializer,
|
|
22
|
+
bodySerializer: globalBodySerializer,
|
|
23
|
+
pathSerializer: globalPathSerializer,
|
|
24
|
+
headers: baseHeaders,
|
|
25
|
+
requestInitExt = void 0,
|
|
26
|
+
...baseOptions
|
|
27
|
+
} = { ...clientOptions };
|
|
28
|
+
requestInitExt = supportsRequestInitExt() ? requestInitExt : void 0;
|
|
29
|
+
baseUrl = removeTrailingSlash(baseUrl);
|
|
30
|
+
const globalMiddlewares = [];
|
|
31
|
+
async function coreFetch(schemaPath, fetchOptions) {
|
|
32
|
+
const {
|
|
33
|
+
baseUrl: localBaseUrl,
|
|
34
|
+
fetch = baseFetch,
|
|
35
|
+
Request = CustomRequest,
|
|
36
|
+
headers,
|
|
37
|
+
params = {},
|
|
38
|
+
parseAs = "json",
|
|
39
|
+
querySerializer: requestQuerySerializer,
|
|
40
|
+
bodySerializer = globalBodySerializer ?? defaultBodySerializer,
|
|
41
|
+
pathSerializer: requestPathSerializer,
|
|
42
|
+
body,
|
|
43
|
+
middleware: requestMiddlewares = [],
|
|
44
|
+
...init
|
|
45
|
+
} = fetchOptions || {};
|
|
46
|
+
let finalBaseUrl = baseUrl;
|
|
47
|
+
if (localBaseUrl) {
|
|
48
|
+
finalBaseUrl = removeTrailingSlash(localBaseUrl) ?? baseUrl;
|
|
49
|
+
}
|
|
50
|
+
let querySerializer = typeof globalQuerySerializer === "function" ? globalQuerySerializer : createQuerySerializer(globalQuerySerializer);
|
|
51
|
+
if (requestQuerySerializer) {
|
|
52
|
+
querySerializer = typeof requestQuerySerializer === "function" ? requestQuerySerializer : createQuerySerializer({
|
|
53
|
+
...typeof globalQuerySerializer === "object" ? globalQuerySerializer : {},
|
|
54
|
+
...requestQuerySerializer
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const pathSerializer = requestPathSerializer || globalPathSerializer || defaultPathSerializer;
|
|
58
|
+
const serializedBody = body === void 0 ? void 0 : bodySerializer(
|
|
59
|
+
body,
|
|
60
|
+
// Note: we declare mergeHeaders() both here and below because it’s a bit of a chicken-or-egg situation:
|
|
61
|
+
// bodySerializer() needs all headers so we aren’t dropping ones set by the user, however,
|
|
62
|
+
// the result of this ALSO sets the lowest-priority content-type header. So we re-merge below,
|
|
63
|
+
// setting the content-type at the very beginning to be overwritten.
|
|
64
|
+
// Lastly, based on the way headers work, it’s not a simple “present-or-not” check becauase null intentionally un-sets headers.
|
|
65
|
+
mergeHeaders(baseHeaders, headers, params.header)
|
|
66
|
+
);
|
|
67
|
+
const finalHeaders = mergeHeaders(
|
|
68
|
+
// with no body, we should not to set Content-Type
|
|
69
|
+
serializedBody === void 0 || // a raw body (FormData, Blob, ...) gets its Content-Type from the platform, or none
|
|
70
|
+
isRawBody(serializedBody) ? {} : {
|
|
71
|
+
"Content-Type": "application/json"
|
|
72
|
+
},
|
|
73
|
+
baseHeaders,
|
|
74
|
+
headers,
|
|
75
|
+
params.header
|
|
76
|
+
);
|
|
77
|
+
if (serializedBody instanceof FormData && !BOUNDARY_PARAMETER_RE.test(finalHeaders.get("Content-Type"))) {
|
|
78
|
+
finalHeaders.delete("Content-Type");
|
|
79
|
+
}
|
|
80
|
+
const finalMiddlewares = [...globalMiddlewares, ...requestMiddlewares];
|
|
81
|
+
const requestInit = {
|
|
82
|
+
redirect: "follow",
|
|
83
|
+
...baseOptions,
|
|
84
|
+
...init,
|
|
85
|
+
body: serializedBody,
|
|
86
|
+
headers: finalHeaders
|
|
87
|
+
};
|
|
88
|
+
let id;
|
|
89
|
+
let options;
|
|
90
|
+
let request = new Request(
|
|
91
|
+
createFinalURL(schemaPath, { baseUrl: finalBaseUrl, params, querySerializer, pathSerializer }),
|
|
92
|
+
requestInit
|
|
93
|
+
);
|
|
94
|
+
let response;
|
|
95
|
+
for (const key in init) {
|
|
96
|
+
if (!(key in request)) {
|
|
97
|
+
request[key] = init[key];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (finalMiddlewares.length) {
|
|
101
|
+
id = randomID();
|
|
102
|
+
options = Object.freeze({
|
|
103
|
+
baseUrl: finalBaseUrl,
|
|
104
|
+
fetch,
|
|
105
|
+
parseAs,
|
|
106
|
+
querySerializer,
|
|
107
|
+
bodySerializer,
|
|
108
|
+
pathSerializer
|
|
109
|
+
});
|
|
110
|
+
for (const m of finalMiddlewares) {
|
|
111
|
+
if (m && typeof m === "object" && typeof m.onRequest === "function") {
|
|
112
|
+
const result = await m.onRequest({
|
|
113
|
+
request,
|
|
114
|
+
schemaPath,
|
|
115
|
+
params,
|
|
116
|
+
options,
|
|
117
|
+
id
|
|
118
|
+
});
|
|
119
|
+
if (result) {
|
|
120
|
+
if (result instanceof Request) {
|
|
121
|
+
request = result;
|
|
122
|
+
} else if (result instanceof Response) {
|
|
123
|
+
response = result;
|
|
124
|
+
break;
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error("onRequest: must return new Request() or Response() when modifying the request");
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (!response) {
|
|
133
|
+
try {
|
|
134
|
+
response = await fetch(request, requestInitExt);
|
|
135
|
+
} catch (error2) {
|
|
136
|
+
let errorAfterMiddleware = error2;
|
|
137
|
+
if (finalMiddlewares.length) {
|
|
138
|
+
for (let i = finalMiddlewares.length - 1; i >= 0; i--) {
|
|
139
|
+
const m = finalMiddlewares[i];
|
|
140
|
+
if (m && typeof m === "object" && typeof m.onError === "function") {
|
|
141
|
+
const result = await m.onError({
|
|
142
|
+
request,
|
|
143
|
+
error: errorAfterMiddleware,
|
|
144
|
+
schemaPath,
|
|
145
|
+
params,
|
|
146
|
+
options,
|
|
147
|
+
id
|
|
148
|
+
});
|
|
149
|
+
if (result) {
|
|
150
|
+
if (result instanceof Response) {
|
|
151
|
+
errorAfterMiddleware = void 0;
|
|
152
|
+
response = result;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
if (result instanceof Error) {
|
|
156
|
+
errorAfterMiddleware = result;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
throw new Error("onError: must return new Response() or instance of Error");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (errorAfterMiddleware) {
|
|
165
|
+
throw errorAfterMiddleware;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (finalMiddlewares.length) {
|
|
169
|
+
for (let i = finalMiddlewares.length - 1; i >= 0; i--) {
|
|
170
|
+
const m = finalMiddlewares[i];
|
|
171
|
+
if (m && typeof m === "object" && typeof m.onResponse === "function") {
|
|
172
|
+
const result = await m.onResponse({
|
|
173
|
+
request,
|
|
174
|
+
response,
|
|
175
|
+
schemaPath,
|
|
176
|
+
params,
|
|
177
|
+
options,
|
|
178
|
+
id
|
|
179
|
+
});
|
|
180
|
+
if (result) {
|
|
181
|
+
if (!(result instanceof Response)) {
|
|
182
|
+
throw new Error("onResponse: must return new Response() when modifying the response");
|
|
183
|
+
}
|
|
184
|
+
response = result;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const contentLength = response.headers.get("Content-Length");
|
|
191
|
+
if (response.status === 204 || request.method === "HEAD" || contentLength === "0" && !response.headers.get("Transfer-Encoding")?.includes("chunked")) {
|
|
192
|
+
return response.ok ? { data: void 0, response } : { error: void 0, response };
|
|
193
|
+
}
|
|
194
|
+
if (response.ok) {
|
|
195
|
+
const getResponseData = async () => {
|
|
196
|
+
if (parseAs === "stream") {
|
|
197
|
+
return response.body;
|
|
198
|
+
}
|
|
199
|
+
if (parseAs === "json" && !contentLength) {
|
|
200
|
+
const raw2 = await response.text();
|
|
201
|
+
return raw2 ? JSON.parse(raw2) : void 0;
|
|
202
|
+
}
|
|
203
|
+
return await response[parseAs]();
|
|
204
|
+
};
|
|
205
|
+
return { data: await getResponseData(), response };
|
|
206
|
+
}
|
|
207
|
+
const raw = await response.text();
|
|
208
|
+
if (!raw) {
|
|
209
|
+
return { error: void 0, response };
|
|
210
|
+
}
|
|
211
|
+
let error = raw;
|
|
212
|
+
try {
|
|
213
|
+
error = JSON.parse(raw);
|
|
214
|
+
} catch {
|
|
215
|
+
}
|
|
216
|
+
return { error, response };
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
request(method, url, init) {
|
|
220
|
+
return coreFetch(url, { ...init, method: method.toUpperCase() });
|
|
221
|
+
},
|
|
222
|
+
/** Call a GET endpoint */
|
|
223
|
+
GET(url, init) {
|
|
224
|
+
return coreFetch(url, { ...init, method: "GET" });
|
|
225
|
+
},
|
|
226
|
+
/** Call a PUT endpoint */
|
|
227
|
+
PUT(url, init) {
|
|
228
|
+
return coreFetch(url, { ...init, method: "PUT" });
|
|
229
|
+
},
|
|
230
|
+
/** Call a POST endpoint */
|
|
231
|
+
POST(url, init) {
|
|
232
|
+
return coreFetch(url, { ...init, method: "POST" });
|
|
233
|
+
},
|
|
234
|
+
/** Call a DELETE endpoint */
|
|
235
|
+
DELETE(url, init) {
|
|
236
|
+
return coreFetch(url, { ...init, method: "DELETE" });
|
|
237
|
+
},
|
|
238
|
+
/** Call a OPTIONS endpoint */
|
|
239
|
+
OPTIONS(url, init) {
|
|
240
|
+
return coreFetch(url, { ...init, method: "OPTIONS" });
|
|
241
|
+
},
|
|
242
|
+
/** Call a HEAD endpoint */
|
|
243
|
+
HEAD(url, init) {
|
|
244
|
+
return coreFetch(url, { ...init, method: "HEAD" });
|
|
245
|
+
},
|
|
246
|
+
/** Call a PATCH endpoint */
|
|
247
|
+
PATCH(url, init) {
|
|
248
|
+
return coreFetch(url, { ...init, method: "PATCH" });
|
|
249
|
+
},
|
|
250
|
+
/** Call a TRACE endpoint */
|
|
251
|
+
TRACE(url, init) {
|
|
252
|
+
return coreFetch(url, { ...init, method: "TRACE" });
|
|
253
|
+
},
|
|
254
|
+
/** Call a QUERY endpoint (OpenAPI 3.2) */
|
|
255
|
+
QUERY(url, init) {
|
|
256
|
+
return coreFetch(url, { ...init, method: "QUERY" });
|
|
257
|
+
},
|
|
258
|
+
/** Register middleware */
|
|
259
|
+
use(...middleware) {
|
|
260
|
+
for (const m of middleware) {
|
|
261
|
+
if (!m) {
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (typeof m !== "object" || !("onRequest" in m || "onResponse" in m || "onError" in m)) {
|
|
265
|
+
throw new Error("Middleware must be an object with one of `onRequest()`, `onResponse()` or `onError()`");
|
|
266
|
+
}
|
|
267
|
+
globalMiddlewares.push(m);
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
/** Unregister middleware */
|
|
271
|
+
eject(...middleware) {
|
|
272
|
+
for (const m of middleware) {
|
|
273
|
+
const i = globalMiddlewares.indexOf(m);
|
|
274
|
+
if (i !== -1) {
|
|
275
|
+
globalMiddlewares.splice(i, 1);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
class PathCallForwarder {
|
|
282
|
+
constructor(client, url) {
|
|
283
|
+
this.client = client;
|
|
284
|
+
this.url = url;
|
|
285
|
+
}
|
|
286
|
+
GET = (init) => {
|
|
287
|
+
return this.client.GET(this.url, init);
|
|
288
|
+
};
|
|
289
|
+
PUT = (init) => {
|
|
290
|
+
return this.client.PUT(this.url, init);
|
|
291
|
+
};
|
|
292
|
+
POST = (init) => {
|
|
293
|
+
return this.client.POST(this.url, init);
|
|
294
|
+
};
|
|
295
|
+
DELETE = (init) => {
|
|
296
|
+
return this.client.DELETE(this.url, init);
|
|
297
|
+
};
|
|
298
|
+
OPTIONS = (init) => {
|
|
299
|
+
return this.client.OPTIONS(this.url, init);
|
|
300
|
+
};
|
|
301
|
+
HEAD = (init) => {
|
|
302
|
+
return this.client.HEAD(this.url, init);
|
|
303
|
+
};
|
|
304
|
+
PATCH = (init) => {
|
|
305
|
+
return this.client.PATCH(this.url, init);
|
|
306
|
+
};
|
|
307
|
+
TRACE = (init) => {
|
|
308
|
+
return this.client.TRACE(this.url, init);
|
|
309
|
+
};
|
|
310
|
+
QUERY = (init) => {
|
|
311
|
+
return this.client.QUERY(this.url, init);
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
class PathClientProxyHandler {
|
|
315
|
+
constructor() {
|
|
316
|
+
this.client = null;
|
|
317
|
+
}
|
|
318
|
+
// Assume the property is an URL.
|
|
319
|
+
get(coreClient, url) {
|
|
320
|
+
const forwarder = new PathCallForwarder(coreClient, url);
|
|
321
|
+
this.client[url] = forwarder;
|
|
322
|
+
return forwarder;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function wrapAsPathBasedClient(coreClient) {
|
|
326
|
+
const handler = new PathClientProxyHandler();
|
|
327
|
+
const proxy = new Proxy(coreClient, handler);
|
|
328
|
+
function Client() {
|
|
329
|
+
}
|
|
330
|
+
Client.prototype = proxy;
|
|
331
|
+
const client = new Client();
|
|
332
|
+
handler.client = client;
|
|
333
|
+
return client;
|
|
334
|
+
}
|
|
335
|
+
function createPathBasedClient(clientOptions) {
|
|
336
|
+
return wrapAsPathBasedClient(createClient(clientOptions));
|
|
337
|
+
}
|
|
338
|
+
const KEPT_NAME_ESCAPES = /%(?:5B|5D|24|3A|40|2C)/g;
|
|
339
|
+
function encodeName(name) {
|
|
340
|
+
return encodeURIComponent(name).replace(KEPT_NAME_ESCAPES, decodeURIComponent);
|
|
341
|
+
}
|
|
342
|
+
function serializePrimitiveParam(name, value, options) {
|
|
343
|
+
if (value === void 0 || value === null) {
|
|
344
|
+
return "";
|
|
345
|
+
}
|
|
346
|
+
if (typeof value === "object") {
|
|
347
|
+
throw new Error(
|
|
348
|
+
"Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these."
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
return `${name}=${options?.allowReserved === true ? value : encodeURIComponent(value)}`;
|
|
352
|
+
}
|
|
353
|
+
function serializeObjectParam(name, value, options) {
|
|
354
|
+
if (!value || typeof value !== "object") {
|
|
355
|
+
return "";
|
|
356
|
+
}
|
|
357
|
+
const values = [];
|
|
358
|
+
const joiner = {
|
|
359
|
+
simple: ",",
|
|
360
|
+
label: ".",
|
|
361
|
+
matrix: ";"
|
|
362
|
+
}[options.style] || "&";
|
|
363
|
+
if (options.style !== "deepObject" && options.explode === false) {
|
|
364
|
+
for (const k in value) {
|
|
365
|
+
values.push(encodeName(k), options.allowReserved === true ? value[k] : encodeURIComponent(value[k]));
|
|
366
|
+
}
|
|
367
|
+
const final2 = values.join(",");
|
|
368
|
+
switch (options.style) {
|
|
369
|
+
case "form": {
|
|
370
|
+
return `${name}=${final2}`;
|
|
371
|
+
}
|
|
372
|
+
case "label": {
|
|
373
|
+
return `.${final2}`;
|
|
374
|
+
}
|
|
375
|
+
case "matrix": {
|
|
376
|
+
return `;${name}=${final2}`;
|
|
377
|
+
}
|
|
378
|
+
default: {
|
|
379
|
+
return final2;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
for (const k in value) {
|
|
384
|
+
const finalName = options.style === "deepObject" ? `${name}[${encodeName(k)}]` : encodeName(k);
|
|
385
|
+
values.push(serializePrimitiveParam(finalName, value[k], options));
|
|
386
|
+
}
|
|
387
|
+
const final = values.join(joiner);
|
|
388
|
+
return options.style === "label" || options.style === "matrix" ? `${joiner}${final}` : final;
|
|
389
|
+
}
|
|
390
|
+
function serializeArrayParam(name, value, options) {
|
|
391
|
+
if (!Array.isArray(value)) {
|
|
392
|
+
return "";
|
|
393
|
+
}
|
|
394
|
+
if (options.explode === false) {
|
|
395
|
+
const joiner2 = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[options.style] || ",";
|
|
396
|
+
const final = (options.allowReserved === true ? value : value.map((v) => encodeURIComponent(v))).join(joiner2);
|
|
397
|
+
switch (options.style) {
|
|
398
|
+
case "simple": {
|
|
399
|
+
return final;
|
|
400
|
+
}
|
|
401
|
+
case "label": {
|
|
402
|
+
return `.${final}`;
|
|
403
|
+
}
|
|
404
|
+
case "matrix": {
|
|
405
|
+
return `;${name}=${final}`;
|
|
406
|
+
}
|
|
407
|
+
// case "spaceDelimited":
|
|
408
|
+
// case "pipeDelimited":
|
|
409
|
+
default: {
|
|
410
|
+
return `${name}=${final}`;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const joiner = { simple: ",", label: ".", matrix: ";" }[options.style] || "&";
|
|
415
|
+
const values = [];
|
|
416
|
+
for (const v of value) {
|
|
417
|
+
if (options.style === "simple" || options.style === "label") {
|
|
418
|
+
values.push(options.allowReserved === true ? v : encodeURIComponent(v));
|
|
419
|
+
} else {
|
|
420
|
+
values.push(serializePrimitiveParam(name, v, options));
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return options.style === "label" || options.style === "matrix" ? `${joiner}${values.join(joiner)}` : values.join(joiner);
|
|
424
|
+
}
|
|
425
|
+
function createQuerySerializer(options) {
|
|
426
|
+
return function querySerializer(queryParams) {
|
|
427
|
+
const search = [];
|
|
428
|
+
if (queryParams && typeof queryParams === "object") {
|
|
429
|
+
for (const key in queryParams) {
|
|
430
|
+
const value = queryParams[key];
|
|
431
|
+
const name = encodeName(key);
|
|
432
|
+
if (value === void 0 || value === null) {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (Array.isArray(value)) {
|
|
436
|
+
if (value.length === 0) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
search.push(
|
|
440
|
+
serializeArrayParam(name, value, {
|
|
441
|
+
style: "form",
|
|
442
|
+
explode: true,
|
|
443
|
+
...options?.array,
|
|
444
|
+
allowReserved: options?.allowReserved || false
|
|
445
|
+
})
|
|
446
|
+
);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (typeof value === "object") {
|
|
450
|
+
search.push(
|
|
451
|
+
serializeObjectParam(name, value, {
|
|
452
|
+
style: "deepObject",
|
|
453
|
+
explode: true,
|
|
454
|
+
...options?.object,
|
|
455
|
+
allowReserved: options?.allowReserved || false
|
|
456
|
+
})
|
|
457
|
+
);
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
search.push(serializePrimitiveParam(name, value, options));
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return search.join("&");
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
function defaultPathSerializer(pathname, pathParams) {
|
|
467
|
+
let nextURL = pathname;
|
|
468
|
+
for (const match of pathname.match(PATH_PARAM_RE) ?? []) {
|
|
469
|
+
let name = match.substring(1, match.length - 1);
|
|
470
|
+
let explode = false;
|
|
471
|
+
let style = "simple";
|
|
472
|
+
if (name.endsWith("*")) {
|
|
473
|
+
explode = true;
|
|
474
|
+
name = name.substring(0, name.length - 1);
|
|
475
|
+
}
|
|
476
|
+
if (name.startsWith(".")) {
|
|
477
|
+
style = "label";
|
|
478
|
+
name = name.substring(1);
|
|
479
|
+
} else if (name.startsWith(";")) {
|
|
480
|
+
style = "matrix";
|
|
481
|
+
name = name.substring(1);
|
|
482
|
+
}
|
|
483
|
+
if (!pathParams || pathParams[name] === void 0 || pathParams[name] === null) {
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
const value = pathParams[name];
|
|
487
|
+
if (Array.isArray(value)) {
|
|
488
|
+
nextURL = nextURL.replace(match, serializeArrayParam(name, value, { style, explode }));
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
if (typeof value === "object") {
|
|
492
|
+
nextURL = nextURL.replace(match, serializeObjectParam(name, value, { style, explode }));
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (style === "matrix") {
|
|
496
|
+
nextURL = nextURL.replace(match, `;${serializePrimitiveParam(name, value)}`);
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
nextURL = nextURL.replace(match, style === "label" ? `.${encodeURIComponent(value)}` : encodeURIComponent(value));
|
|
500
|
+
}
|
|
501
|
+
if (DOT_SEGMENT_RE.test(nextURL)) {
|
|
502
|
+
throw new Error(`Dot path segment in ${nextURL}`);
|
|
503
|
+
}
|
|
504
|
+
return nextURL;
|
|
505
|
+
}
|
|
506
|
+
function isRawBody(body) {
|
|
507
|
+
return body instanceof FormData || body instanceof Blob || body instanceof URLSearchParams || body instanceof ArrayBuffer || ArrayBuffer.isView(body);
|
|
508
|
+
}
|
|
509
|
+
function formBody(body) {
|
|
510
|
+
for (const value of Object.values(body)) {
|
|
511
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
512
|
+
throw new Error(NESTED_FORM_VALUE_MESSAGE);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return new URLSearchParams(body).toString();
|
|
516
|
+
}
|
|
517
|
+
function multipartBody(body) {
|
|
518
|
+
const formData = new FormData();
|
|
519
|
+
for (const [name, value] of Object.entries(body)) {
|
|
520
|
+
for (const item of [value].flat()) {
|
|
521
|
+
if (item instanceof Blob) {
|
|
522
|
+
formData.append(name, item);
|
|
523
|
+
} else if (item && typeof item === "object") {
|
|
524
|
+
throw new Error(NESTED_FORM_VALUE_MESSAGE);
|
|
525
|
+
} else if (item != null) {
|
|
526
|
+
formData.append(name, String(item));
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
return formData;
|
|
531
|
+
}
|
|
532
|
+
function defaultBodySerializer(body, headers) {
|
|
533
|
+
if (isRawBody(body)) {
|
|
534
|
+
return body;
|
|
535
|
+
}
|
|
536
|
+
const contentType = headers ? (
|
|
537
|
+
// `typeof`, not `instanceof Function`: headers from another realm (iframe, `node:vm`) have a
|
|
538
|
+
// `get` that is not an instance of this realm's Function (upstream #2637).
|
|
539
|
+
typeof headers.get === "function" ? headers.get("Content-Type") ?? headers.get("content-type") : headers["Content-Type"] ?? headers["content-type"]
|
|
540
|
+
) : void 0;
|
|
541
|
+
if (contentType === FORM_MEDIA_TYPE) {
|
|
542
|
+
return formBody(body);
|
|
543
|
+
}
|
|
544
|
+
if (body && typeof body === "object" && MULTIPART_MEDIA_TYPE_RE.test(contentType)) {
|
|
545
|
+
return multipartBody(body);
|
|
546
|
+
}
|
|
547
|
+
if (typeof body === "string" && contentType && !JSON_MEDIA_TYPE_RE.test(contentType)) {
|
|
548
|
+
return body;
|
|
549
|
+
}
|
|
550
|
+
return JSON.stringify(body);
|
|
551
|
+
}
|
|
552
|
+
function createFinalURL(pathname, options) {
|
|
553
|
+
let finalURL = `${options.baseUrl}${pathname}`;
|
|
554
|
+
if (options.params?.path) {
|
|
555
|
+
finalURL = options.pathSerializer(finalURL, options.params.path);
|
|
556
|
+
}
|
|
557
|
+
let search = options.querySerializer(options.params.query ?? {});
|
|
558
|
+
if (search.startsWith("?")) {
|
|
559
|
+
search = search.substring(1);
|
|
560
|
+
}
|
|
561
|
+
if (search) {
|
|
562
|
+
finalURL += `${finalURL.includes("?") ? "&" : "?"}${search}`;
|
|
563
|
+
}
|
|
564
|
+
return finalURL;
|
|
565
|
+
}
|
|
566
|
+
function mergeHeaders(...allHeaders) {
|
|
567
|
+
const finalHeaders = new Headers();
|
|
568
|
+
for (const h of allHeaders) {
|
|
569
|
+
if (!h || typeof h !== "object") {
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
const iterator = h instanceof Headers ? h.entries() : Object.entries(h);
|
|
573
|
+
for (const [k, v] of iterator) {
|
|
574
|
+
if (v === null) {
|
|
575
|
+
finalHeaders.delete(k);
|
|
576
|
+
} else if (Array.isArray(v)) {
|
|
577
|
+
for (const v2 of v) {
|
|
578
|
+
finalHeaders.append(k, v2);
|
|
579
|
+
}
|
|
580
|
+
} else if (v !== void 0) {
|
|
581
|
+
finalHeaders.set(k, v);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return finalHeaders;
|
|
586
|
+
}
|
|
587
|
+
function removeTrailingSlash(url) {
|
|
588
|
+
if (url.endsWith("/")) {
|
|
589
|
+
return url.substring(0, url.length - 1);
|
|
590
|
+
}
|
|
591
|
+
return url;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
export { createFinalURL, createPathBasedClient, createQuerySerializer, createClient as default, defaultBodySerializer, defaultPathSerializer, mergeHeaders, randomID, removeTrailingSlash, serializeArrayParam, serializeObjectParam, serializePrimitiveParam, wrapAsPathBasedClient };
|
|
595
|
+
//# sourceMappingURL=index.mjs.map
|