@sdk-it/runtime 0.0.0-stage → 0.47.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/dist/http/client.d.ts +130 -0
- package/dist/http/client.d.ts.map +1 -0
- package/dist/http/dispatcher.d.ts +35 -0
- package/dist/http/dispatcher.d.ts.map +1 -0
- package/dist/http/interceptors.d.ts +7 -0
- package/dist/http/interceptors.d.ts.map +1 -0
- package/dist/http/page.d.ts +69 -0
- package/dist/http/page.d.ts.map +1 -0
- package/dist/http/parse-response.d.ts +11 -0
- package/dist/http/parse-response.d.ts.map +1 -0
- package/dist/http/parser.d.ts +7 -0
- package/dist/http/parser.d.ts.map +1 -0
- package/dist/http/request.d.ts +62 -0
- package/dist/http/request.d.ts.map +1 -0
- package/dist/http/response.d.ts +183 -0
- package/dist/http/response.d.ts.map +1 -0
- package/dist/http/security.d.ts +45 -0
- package/dist/http/security.d.ts.map +1 -0
- package/dist/http/sse.d.ts +3 -0
- package/dist/http/sse.d.ts.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1133 -0
- package/dist/index.js.map +7 -0
- package/package.json +38 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,1133 @@
|
|
|
1
|
+
// packages/runtime/src/http/client.ts
|
|
2
|
+
import { z as z4 } from "zod";
|
|
3
|
+
|
|
4
|
+
// packages/runtime/src/http/dispatcher.ts
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
|
|
7
|
+
// packages/runtime/src/http/interceptors.ts
|
|
8
|
+
var createHeadersInterceptor = (headers, requestHeaders) => {
|
|
9
|
+
return {
|
|
10
|
+
before({ init, url }) {
|
|
11
|
+
for (const [key, value] of new Headers(requestHeaders)) {
|
|
12
|
+
if (value !== void 0 && !init.headers.has(key)) {
|
|
13
|
+
init.headers.set(key, value);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
17
|
+
if (value !== void 0 && !init.headers.has(key)) {
|
|
18
|
+
init.headers.set(key, value);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return { init, url };
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// packages/runtime/src/http/parse-response.ts
|
|
27
|
+
import { parse } from "fast-content-type-parse";
|
|
28
|
+
function isBinaryContentType(contentType) {
|
|
29
|
+
const type = contentType.toLowerCase();
|
|
30
|
+
if (type.startsWith("image/")) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
if (type.startsWith("audio/")) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (type.startsWith("video/")) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
switch (type) {
|
|
40
|
+
case "application/pdf":
|
|
41
|
+
case "application/zip":
|
|
42
|
+
case "application/gzip":
|
|
43
|
+
case "application/x-7z-compressed":
|
|
44
|
+
case "application/x-tar":
|
|
45
|
+
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
|
46
|
+
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
|
47
|
+
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
|
|
48
|
+
case "application/vnd.ms-excel":
|
|
49
|
+
case "application/vnd.ms-powerpoint":
|
|
50
|
+
case "application/msword":
|
|
51
|
+
case "application/octet-stream":
|
|
52
|
+
return true;
|
|
53
|
+
default:
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function chunked(response) {
|
|
58
|
+
return response.body;
|
|
59
|
+
}
|
|
60
|
+
async function bodyless(response) {
|
|
61
|
+
await response.body?.cancel();
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
function buffered(declared) {
|
|
65
|
+
return async (response) => {
|
|
66
|
+
if (response.status === 204 || response.status === 205 || response.status === 304) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const contentType = response.headers.get("Content-Type");
|
|
70
|
+
if (contentType) {
|
|
71
|
+
return byContentType(response, contentType);
|
|
72
|
+
}
|
|
73
|
+
const bytes = await response.bytes();
|
|
74
|
+
if (bytes.byteLength === 0) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
switch (declared) {
|
|
78
|
+
case "json":
|
|
79
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
80
|
+
case "text":
|
|
81
|
+
return new TextDecoder().decode(bytes);
|
|
82
|
+
default:
|
|
83
|
+
return new Blob([bytes], { type: "application/octet-stream" });
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
async function byContentType(response, contentType) {
|
|
88
|
+
const { type } = parse(contentType);
|
|
89
|
+
if (isBinaryContentType(type)) {
|
|
90
|
+
return response.blob();
|
|
91
|
+
}
|
|
92
|
+
if (type.startsWith("text/")) {
|
|
93
|
+
return response.text();
|
|
94
|
+
}
|
|
95
|
+
if (type === "application/json" || type.endsWith("+json")) {
|
|
96
|
+
return response.json();
|
|
97
|
+
}
|
|
98
|
+
switch (type) {
|
|
99
|
+
case "application/xml":
|
|
100
|
+
return response.text();
|
|
101
|
+
case "application/x-www-form-urlencoded": {
|
|
102
|
+
const text2 = await response.text();
|
|
103
|
+
return Object.fromEntries(new URLSearchParams(text2));
|
|
104
|
+
}
|
|
105
|
+
case "multipart/form-data":
|
|
106
|
+
return response.formData();
|
|
107
|
+
default:
|
|
108
|
+
throw new Error(`Unsupported content type: ${contentType}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// packages/runtime/src/http/request.ts
|
|
113
|
+
function template(templateString, templateVariables) {
|
|
114
|
+
const nargs = /{([0-9a-zA-Z_]+)}/g;
|
|
115
|
+
return templateString.replace(nargs, (match2, key, index) => {
|
|
116
|
+
if (templateString[index - 1] === "{" && templateString[index + match2.length] === "}") {
|
|
117
|
+
return key;
|
|
118
|
+
}
|
|
119
|
+
const result = key in templateVariables ? templateVariables[key] : null;
|
|
120
|
+
return result === null || result === void 0 ? "" : encodeURIComponent(String(result));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function toWireValue(value) {
|
|
124
|
+
return value instanceof Date ? value.toISOString() : String(value);
|
|
125
|
+
}
|
|
126
|
+
function wireItems(value) {
|
|
127
|
+
if (value === void 0) {
|
|
128
|
+
return [];
|
|
129
|
+
}
|
|
130
|
+
return Array.isArray(value) ? value : [value];
|
|
131
|
+
}
|
|
132
|
+
var Serializer = class {
|
|
133
|
+
input;
|
|
134
|
+
props;
|
|
135
|
+
constructor(input, props) {
|
|
136
|
+
this.input = input;
|
|
137
|
+
this.props = props;
|
|
138
|
+
}
|
|
139
|
+
serialize(path, baseUrl) {
|
|
140
|
+
const params = this.props.inputParams.reduce(
|
|
141
|
+
(acc, key) => {
|
|
142
|
+
acc[key] = this.input[key];
|
|
143
|
+
return acc;
|
|
144
|
+
},
|
|
145
|
+
{}
|
|
146
|
+
);
|
|
147
|
+
const url = new URL(`${baseUrl}${template(path, params)}`);
|
|
148
|
+
const headers = { ...this.getHeaders() };
|
|
149
|
+
for (const header of this.props.inputHeaders) {
|
|
150
|
+
const value = this.input[header];
|
|
151
|
+
if (value !== void 0) {
|
|
152
|
+
headers[header] = toWireValue(value);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const key of this.props.inputQuery) {
|
|
156
|
+
for (const item of wireItems(this.input[key])) {
|
|
157
|
+
url.searchParams.append(key, toWireValue(item));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
body: this.getBody(),
|
|
162
|
+
url,
|
|
163
|
+
headers
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
var JsonSerializer = class extends Serializer {
|
|
168
|
+
getBody() {
|
|
169
|
+
const body = {};
|
|
170
|
+
if (this.props.inputBody.length === 1 && this.props.inputBody[0] === "$body") {
|
|
171
|
+
return JSON.stringify(this.input.$body);
|
|
172
|
+
}
|
|
173
|
+
for (const prop of this.props.inputBody) {
|
|
174
|
+
body[prop] = this.input[prop];
|
|
175
|
+
}
|
|
176
|
+
return JSON.stringify(body);
|
|
177
|
+
}
|
|
178
|
+
getHeaders() {
|
|
179
|
+
return {
|
|
180
|
+
"Content-Type": this.props.contentType,
|
|
181
|
+
Accept: "application/json"
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
var UrlencodedSerializer = class extends Serializer {
|
|
186
|
+
getBody() {
|
|
187
|
+
const body = new URLSearchParams();
|
|
188
|
+
for (const prop of this.props.inputBody) {
|
|
189
|
+
for (const item of wireItems(this.input[prop])) {
|
|
190
|
+
body.append(prop, toWireValue(item));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return body;
|
|
194
|
+
}
|
|
195
|
+
getHeaders() {
|
|
196
|
+
return {
|
|
197
|
+
"Content-Type": this.props.contentType,
|
|
198
|
+
Accept: "application/json"
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
var EmptySerializer = class extends Serializer {
|
|
203
|
+
getBody() {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
getHeaders() {
|
|
207
|
+
return {};
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
var FormDataSerializer = class extends Serializer {
|
|
211
|
+
getBody() {
|
|
212
|
+
const body = new FormData();
|
|
213
|
+
for (const prop of this.props.inputBody) {
|
|
214
|
+
for (const item of wireItems(this.input[prop])) {
|
|
215
|
+
body.append(prop, item);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return body;
|
|
219
|
+
}
|
|
220
|
+
getHeaders() {
|
|
221
|
+
return {
|
|
222
|
+
Accept: "application/json"
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
var RawSerializer = class extends Serializer {
|
|
227
|
+
getBody() {
|
|
228
|
+
return this.input.$body;
|
|
229
|
+
}
|
|
230
|
+
getHeaders() {
|
|
231
|
+
return {
|
|
232
|
+
"Content-Type": this.props.contentType
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
function json(input, props) {
|
|
237
|
+
return new JsonSerializer(input, props);
|
|
238
|
+
}
|
|
239
|
+
function urlencoded(input, props) {
|
|
240
|
+
return new UrlencodedSerializer(input, props);
|
|
241
|
+
}
|
|
242
|
+
function empty(input, props) {
|
|
243
|
+
return new EmptySerializer(input, props);
|
|
244
|
+
}
|
|
245
|
+
function formdata(input, props) {
|
|
246
|
+
return new FormDataSerializer(input, props);
|
|
247
|
+
}
|
|
248
|
+
function text(input, props) {
|
|
249
|
+
return new RawSerializer(input, props);
|
|
250
|
+
}
|
|
251
|
+
function binary(input, props) {
|
|
252
|
+
return new RawSerializer(input, props);
|
|
253
|
+
}
|
|
254
|
+
function toRequest(method, path, serializer, baseUrl) {
|
|
255
|
+
const input = serializer.serialize(path, baseUrl);
|
|
256
|
+
return {
|
|
257
|
+
url: input.url,
|
|
258
|
+
init: {
|
|
259
|
+
method,
|
|
260
|
+
headers: new Headers(input.headers),
|
|
261
|
+
...method === "GET" ? {} : { body: input.body }
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// packages/runtime/src/http/response.ts
|
|
267
|
+
var APIResponse = class {
|
|
268
|
+
static status;
|
|
269
|
+
status;
|
|
270
|
+
data;
|
|
271
|
+
headers;
|
|
272
|
+
constructor(status, headers, data) {
|
|
273
|
+
this.status = status;
|
|
274
|
+
this.headers = headers;
|
|
275
|
+
this.data = data;
|
|
276
|
+
}
|
|
277
|
+
static create(status, headers, data) {
|
|
278
|
+
return new this(status, headers, data);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
var APIError = class extends Error {
|
|
282
|
+
static status;
|
|
283
|
+
status;
|
|
284
|
+
data;
|
|
285
|
+
headers;
|
|
286
|
+
constructor(status, headers, data) {
|
|
287
|
+
super(failureMessage(status, data));
|
|
288
|
+
this.name = "APIError";
|
|
289
|
+
this.status = status;
|
|
290
|
+
this.headers = headers;
|
|
291
|
+
this.data = data;
|
|
292
|
+
}
|
|
293
|
+
static create(status, headers, data) {
|
|
294
|
+
return new this(status, headers, data);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
function failureMessage(status, data) {
|
|
298
|
+
const summary = bodySummary(data);
|
|
299
|
+
return summary ? `Request failed with status code ${status}: ${summary}` : `Request failed with status code ${status}`;
|
|
300
|
+
}
|
|
301
|
+
function bodySummary(data) {
|
|
302
|
+
const tag = Object.prototype.toString.call(data);
|
|
303
|
+
let text2 = "";
|
|
304
|
+
if (typeof data === "string") {
|
|
305
|
+
text2 = data;
|
|
306
|
+
} else if (tag === "[object Object]" || tag === "[object Array]") {
|
|
307
|
+
text2 = JSON.stringify(data);
|
|
308
|
+
}
|
|
309
|
+
const line = text2.replace(/\s+/g, " ").trim();
|
|
310
|
+
return line.length > 200 ? `${line.slice(0, 200)}\u2026` : line;
|
|
311
|
+
}
|
|
312
|
+
var Ok = class _Ok extends APIResponse {
|
|
313
|
+
static status = 200;
|
|
314
|
+
constructor(headers, data) {
|
|
315
|
+
super(_Ok.status, headers, data);
|
|
316
|
+
}
|
|
317
|
+
static create(status, headers, data) {
|
|
318
|
+
return new this(headers, data);
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
var Created = class _Created extends APIResponse {
|
|
322
|
+
static status = 201;
|
|
323
|
+
constructor(headers, data) {
|
|
324
|
+
super(_Created.status, headers, data);
|
|
325
|
+
}
|
|
326
|
+
static create(status, headers, data) {
|
|
327
|
+
return new this(headers, data);
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
var Accepted = class _Accepted extends APIResponse {
|
|
331
|
+
static status = 202;
|
|
332
|
+
constructor(headers, data) {
|
|
333
|
+
super(_Accepted.status, headers, data);
|
|
334
|
+
}
|
|
335
|
+
static create(status, headers, data) {
|
|
336
|
+
return new this(headers, data);
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
var NoContent = class _NoContent extends APIResponse {
|
|
340
|
+
static status = 204;
|
|
341
|
+
constructor(headers) {
|
|
342
|
+
super(_NoContent.status, headers, null);
|
|
343
|
+
}
|
|
344
|
+
static create(status, headers) {
|
|
345
|
+
return new this(headers);
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
var BadRequest = class _BadRequest extends APIError {
|
|
349
|
+
static status = 400;
|
|
350
|
+
constructor(headers, data) {
|
|
351
|
+
super(_BadRequest.status, headers, data);
|
|
352
|
+
}
|
|
353
|
+
static create(status, headers, data) {
|
|
354
|
+
return new this(headers, data);
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
var Unauthorized = class _Unauthorized extends APIError {
|
|
358
|
+
static status = 401;
|
|
359
|
+
constructor(headers, data) {
|
|
360
|
+
super(_Unauthorized.status, headers, data);
|
|
361
|
+
}
|
|
362
|
+
static create(status, headers, data) {
|
|
363
|
+
return new this(headers, data);
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
var PaymentRequired = class _PaymentRequired extends APIError {
|
|
367
|
+
static status = 402;
|
|
368
|
+
constructor(headers, data) {
|
|
369
|
+
super(_PaymentRequired.status, headers, data);
|
|
370
|
+
}
|
|
371
|
+
static create(status, headers, data) {
|
|
372
|
+
return new this(headers, data);
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
var Forbidden = class _Forbidden extends APIError {
|
|
376
|
+
static status = 403;
|
|
377
|
+
constructor(headers, data) {
|
|
378
|
+
super(_Forbidden.status, headers, data);
|
|
379
|
+
}
|
|
380
|
+
static create(status, headers, data) {
|
|
381
|
+
return new this(headers, data);
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
var NotFound = class _NotFound extends APIError {
|
|
385
|
+
static status = 404;
|
|
386
|
+
constructor(headers, data) {
|
|
387
|
+
super(_NotFound.status, headers, data);
|
|
388
|
+
}
|
|
389
|
+
static create(status, headers, data) {
|
|
390
|
+
return new this(headers, data);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
var MethodNotAllowed = class _MethodNotAllowed extends APIError {
|
|
394
|
+
static status = 405;
|
|
395
|
+
constructor(headers, data) {
|
|
396
|
+
super(_MethodNotAllowed.status, headers, data);
|
|
397
|
+
}
|
|
398
|
+
static create(status, headers, data) {
|
|
399
|
+
return new this(headers, data);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
var NotAcceptable = class _NotAcceptable extends APIError {
|
|
403
|
+
static status = 406;
|
|
404
|
+
constructor(headers, data) {
|
|
405
|
+
super(_NotAcceptable.status, headers, data);
|
|
406
|
+
}
|
|
407
|
+
static create(status, headers, data) {
|
|
408
|
+
return new this(headers, data);
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
var Conflict = class _Conflict extends APIError {
|
|
412
|
+
static status = 409;
|
|
413
|
+
constructor(headers, data) {
|
|
414
|
+
super(_Conflict.status, headers, data);
|
|
415
|
+
}
|
|
416
|
+
static create(status, headers, data) {
|
|
417
|
+
return new this(headers, data);
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
var Gone = class _Gone extends APIError {
|
|
421
|
+
static status = 410;
|
|
422
|
+
constructor(headers, data) {
|
|
423
|
+
super(_Gone.status, headers, data);
|
|
424
|
+
}
|
|
425
|
+
static create(status, headers, data) {
|
|
426
|
+
return new this(headers, data);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
var PreconditionFailed = class _PreconditionFailed extends APIError {
|
|
430
|
+
static status = 412;
|
|
431
|
+
constructor(headers, data) {
|
|
432
|
+
super(_PreconditionFailed.status, headers, data);
|
|
433
|
+
}
|
|
434
|
+
static create(status, headers, data) {
|
|
435
|
+
return new this(headers, data);
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
var UnprocessableEntity = class _UnprocessableEntity extends APIError {
|
|
439
|
+
static status = 422;
|
|
440
|
+
constructor(headers, data) {
|
|
441
|
+
super(_UnprocessableEntity.status, headers, data);
|
|
442
|
+
}
|
|
443
|
+
static create(status, headers, data) {
|
|
444
|
+
return new this(headers, data);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
var TooManyRequests = class _TooManyRequests extends APIError {
|
|
448
|
+
static status = 429;
|
|
449
|
+
constructor(headers, data) {
|
|
450
|
+
super(_TooManyRequests.status, headers, data);
|
|
451
|
+
}
|
|
452
|
+
static create(status, headers, data) {
|
|
453
|
+
return new this(headers, data);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
var PayloadTooLarge = class _PayloadTooLarge extends APIError {
|
|
457
|
+
static status = 413;
|
|
458
|
+
constructor(headers, data) {
|
|
459
|
+
super(_PayloadTooLarge.status, headers, data);
|
|
460
|
+
}
|
|
461
|
+
static create(status, headers, data) {
|
|
462
|
+
return new this(headers, data);
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
var UnsupportedMediaType = class _UnsupportedMediaType extends APIError {
|
|
466
|
+
static status = 415;
|
|
467
|
+
constructor(headers, data) {
|
|
468
|
+
super(_UnsupportedMediaType.status, headers, data);
|
|
469
|
+
}
|
|
470
|
+
static create(status, headers, data) {
|
|
471
|
+
return new this(headers, data);
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
var InternalServerError = class _InternalServerError extends APIError {
|
|
475
|
+
static status = 500;
|
|
476
|
+
constructor(headers, data) {
|
|
477
|
+
super(_InternalServerError.status, headers, data);
|
|
478
|
+
}
|
|
479
|
+
static create(status, headers, data) {
|
|
480
|
+
return new this(headers, data);
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
var NotImplemented = class _NotImplemented extends APIError {
|
|
484
|
+
static status = 501;
|
|
485
|
+
constructor(headers, data) {
|
|
486
|
+
super(_NotImplemented.status, headers, data);
|
|
487
|
+
}
|
|
488
|
+
static create(status, headers, data) {
|
|
489
|
+
return new this(headers, data);
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
var BadGateway = class _BadGateway extends APIError {
|
|
493
|
+
static status = 502;
|
|
494
|
+
constructor(headers, data) {
|
|
495
|
+
super(_BadGateway.status, headers, data);
|
|
496
|
+
}
|
|
497
|
+
static create(status, headers, data) {
|
|
498
|
+
return new this(headers, data);
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
var ServiceUnavailable = class _ServiceUnavailable extends APIError {
|
|
502
|
+
static status = 503;
|
|
503
|
+
constructor(headers, data) {
|
|
504
|
+
super(_ServiceUnavailable.status, headers, data);
|
|
505
|
+
}
|
|
506
|
+
static create(status, headers, data) {
|
|
507
|
+
return new this(headers, data);
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
var GatewayTimeout = class _GatewayTimeout extends APIError {
|
|
511
|
+
static status = 504;
|
|
512
|
+
constructor(headers, data) {
|
|
513
|
+
super(_GatewayTimeout.status, headers, data);
|
|
514
|
+
}
|
|
515
|
+
static create(status, headers, data) {
|
|
516
|
+
return new this(headers, data);
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// packages/runtime/src/http/sse.ts
|
|
521
|
+
function sse(response) {
|
|
522
|
+
const subscribers = /* @__PURE__ */ new Map();
|
|
523
|
+
let started = false;
|
|
524
|
+
let streamDone = false;
|
|
525
|
+
let streamFailure = null;
|
|
526
|
+
function wake(waiters) {
|
|
527
|
+
for (const resolve of waiters) resolve();
|
|
528
|
+
waiters.clear();
|
|
529
|
+
}
|
|
530
|
+
function dispatch(eventType, data) {
|
|
531
|
+
const sub = subscribers.get(eventType);
|
|
532
|
+
if (sub) {
|
|
533
|
+
sub.queue.push(data);
|
|
534
|
+
wake(sub.waiters);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
function endAll() {
|
|
538
|
+
streamDone = true;
|
|
539
|
+
for (const sub of subscribers.values()) {
|
|
540
|
+
wake(sub.waiters);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
function startReading() {
|
|
544
|
+
if (started) return;
|
|
545
|
+
started = true;
|
|
546
|
+
const decoder = new TextDecoder();
|
|
547
|
+
let buffer = "";
|
|
548
|
+
void (async () => {
|
|
549
|
+
try {
|
|
550
|
+
for await (const value of response.body) {
|
|
551
|
+
buffer += decoder.decode(value, { stream: true });
|
|
552
|
+
const parts = buffer.split("\n\n");
|
|
553
|
+
buffer = parts.pop();
|
|
554
|
+
for (const part of parts) {
|
|
555
|
+
if (!part.trim()) continue;
|
|
556
|
+
let eventType = "message";
|
|
557
|
+
let data = "";
|
|
558
|
+
for (const line of part.split("\n")) {
|
|
559
|
+
if (line.startsWith("event:")) {
|
|
560
|
+
eventType = line.slice(6).trim();
|
|
561
|
+
} else if (line.startsWith("data:")) {
|
|
562
|
+
data += (data ? "\n" : "") + line.slice(5).trim();
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (data) dispatch(eventType, data);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
} catch (error) {
|
|
569
|
+
streamFailure = { error };
|
|
570
|
+
}
|
|
571
|
+
endAll();
|
|
572
|
+
})();
|
|
573
|
+
}
|
|
574
|
+
return function listen(eventType) {
|
|
575
|
+
if (!subscribers.has(eventType)) {
|
|
576
|
+
subscribers.set(eventType, { queue: [], waiters: /* @__PURE__ */ new Set() });
|
|
577
|
+
}
|
|
578
|
+
const sub = subscribers.get(eventType);
|
|
579
|
+
startReading();
|
|
580
|
+
return {
|
|
581
|
+
[Symbol.asyncIterator]() {
|
|
582
|
+
return {
|
|
583
|
+
async next() {
|
|
584
|
+
while (sub.queue.length === 0) {
|
|
585
|
+
if (streamFailure) throw streamFailure.error;
|
|
586
|
+
if (streamDone) return { value: void 0, done: true };
|
|
587
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
588
|
+
sub.waiters.add(resolve);
|
|
589
|
+
await promise;
|
|
590
|
+
}
|
|
591
|
+
return { value: sub.queue.shift(), done: false };
|
|
592
|
+
},
|
|
593
|
+
async return() {
|
|
594
|
+
return { value: void 0, done: true };
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// packages/runtime/src/http/dispatcher.ts
|
|
603
|
+
var fetchType = z.function({
|
|
604
|
+
input: [z.custom()],
|
|
605
|
+
output: z.custom()
|
|
606
|
+
}).optional();
|
|
607
|
+
var undeclared = buffered();
|
|
608
|
+
async function parse2(outputs, response, mapper) {
|
|
609
|
+
const declared = match(outputs, response.status);
|
|
610
|
+
const output = declared ? typeOf(declared) : null;
|
|
611
|
+
const parser = declared && "parser" in declared ? declared.parser : undeclared;
|
|
612
|
+
if (response.ok) {
|
|
613
|
+
const data = await parser(response);
|
|
614
|
+
const mapped = mapper ? mapper(data) : data;
|
|
615
|
+
const success = output && isTypeOf(output, APIResponse) ? output : APIResponse;
|
|
616
|
+
return success.create(response.status, response.headers, mapped);
|
|
617
|
+
}
|
|
618
|
+
const failure = output && isTypeOf(output, APIError) ? output : APIError;
|
|
619
|
+
throw failure.create(
|
|
620
|
+
response.status,
|
|
621
|
+
response.headers,
|
|
622
|
+
await parser(response)
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
function match(outputs, status) {
|
|
626
|
+
const range = `${Math.trunc(status / 100)}XX`;
|
|
627
|
+
return outputs.find(
|
|
628
|
+
(output) => !("range" in output) && statusOf(output) === status
|
|
629
|
+
) ?? outputs.find((output) => "range" in output && output.range === range);
|
|
630
|
+
}
|
|
631
|
+
function typeOf(output) {
|
|
632
|
+
return "parser" in output ? output.type : output;
|
|
633
|
+
}
|
|
634
|
+
function statusOf(output) {
|
|
635
|
+
const type = typeOf(output);
|
|
636
|
+
return isTypeOf(type, APIResponse) || isTypeOf(type, APIError) ? type.status : void 0;
|
|
637
|
+
}
|
|
638
|
+
function isTypeOf(instance, baseType) {
|
|
639
|
+
return instance === baseType || Object.prototype.isPrototypeOf.call(baseType, instance);
|
|
640
|
+
}
|
|
641
|
+
var Dispatcher = class {
|
|
642
|
+
#interceptors = [];
|
|
643
|
+
#fetch;
|
|
644
|
+
constructor(interceptors, fetch2) {
|
|
645
|
+
this.#interceptors = interceptors;
|
|
646
|
+
this.#fetch = fetch2;
|
|
647
|
+
}
|
|
648
|
+
async send(config, outputs, signal, mapper) {
|
|
649
|
+
for (const interceptor of this.#interceptors) {
|
|
650
|
+
if (interceptor.before) {
|
|
651
|
+
config = await interceptor.before(config);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
const init = signal === void 0 ? config.init : { ...config.init, signal };
|
|
655
|
+
let response = await (this.#fetch ?? fetch)(new Request(config.url, init));
|
|
656
|
+
for (const interceptor of this.#interceptors.toReversed()) {
|
|
657
|
+
if (interceptor.after) {
|
|
658
|
+
response = await interceptor.after(response.clone());
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
if (mapper) {
|
|
662
|
+
return await parse2(outputs, response, mapper);
|
|
663
|
+
}
|
|
664
|
+
return await parse2(outputs, response);
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
// packages/runtime/src/http/page.ts
|
|
669
|
+
function at(data, path) {
|
|
670
|
+
let value = data;
|
|
671
|
+
for (const key of path.split(".")) {
|
|
672
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
673
|
+
value = Reflect.get(value, key);
|
|
674
|
+
}
|
|
675
|
+
return value;
|
|
676
|
+
}
|
|
677
|
+
function position(sent, start, first) {
|
|
678
|
+
if (typeof sent === "number") return sent;
|
|
679
|
+
return start ?? first;
|
|
680
|
+
}
|
|
681
|
+
var ResponsePage = class {
|
|
682
|
+
#input;
|
|
683
|
+
#pagination;
|
|
684
|
+
#fetchPage;
|
|
685
|
+
#items;
|
|
686
|
+
constructor(data, input, pagination, fetchPage) {
|
|
687
|
+
Object.assign(this, data);
|
|
688
|
+
this.#input = input;
|
|
689
|
+
this.#pagination = pagination;
|
|
690
|
+
this.#fetchPage = fetchPage;
|
|
691
|
+
const items = at(data, pagination.items);
|
|
692
|
+
this.#items = Array.isArray(items) ? items : [];
|
|
693
|
+
}
|
|
694
|
+
hasNextPage() {
|
|
695
|
+
return Boolean(at(this, this.#pagination.hasMore));
|
|
696
|
+
}
|
|
697
|
+
async getNextPage(options) {
|
|
698
|
+
return this.#fetchPage(this.#nextInput(), options);
|
|
699
|
+
}
|
|
700
|
+
async *iterPages(options) {
|
|
701
|
+
yield this;
|
|
702
|
+
let page = this.hasNextPage() ? await this.getNextPage(options) : void 0;
|
|
703
|
+
while (page) {
|
|
704
|
+
yield page;
|
|
705
|
+
page = page.hasNextPage() ? await page.getNextPage(options) : void 0;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
async *[Symbol.asyncIterator]() {
|
|
709
|
+
for await (const page of this.iterPages()) {
|
|
710
|
+
yield* page.#items;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
#nextInput() {
|
|
714
|
+
const pagination = this.#pagination;
|
|
715
|
+
switch (pagination.type) {
|
|
716
|
+
case "offset": {
|
|
717
|
+
const offset = position(
|
|
718
|
+
this.#input[pagination.offsetParamName],
|
|
719
|
+
pagination.start,
|
|
720
|
+
0
|
|
721
|
+
);
|
|
722
|
+
return {
|
|
723
|
+
...this.#input,
|
|
724
|
+
[pagination.offsetParamName]: offset + this.#items.length
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
case "page": {
|
|
728
|
+
const page = position(
|
|
729
|
+
this.#input[pagination.pageNumberParamName],
|
|
730
|
+
pagination.start,
|
|
731
|
+
1
|
|
732
|
+
);
|
|
733
|
+
return { ...this.#input, [pagination.pageNumberParamName]: page + 1 };
|
|
734
|
+
}
|
|
735
|
+
case "cursor": {
|
|
736
|
+
const cursor = pagination.nextCursor ? at(this, pagination.nextCursor) : void 0;
|
|
737
|
+
if (typeof cursor !== "string") {
|
|
738
|
+
throw new Error(
|
|
739
|
+
"The response says more pages exist but names no next cursor"
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
return { ...this.#input, [pagination.cursorParamName]: cursor };
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
function createPage(data, input, pagination, fetchPage) {
|
|
748
|
+
return new ResponsePage(data, input, pagination, fetchPage);
|
|
749
|
+
}
|
|
750
|
+
var PagePromise = class extends Promise {
|
|
751
|
+
static get [Symbol.species]() {
|
|
752
|
+
return Promise;
|
|
753
|
+
}
|
|
754
|
+
constructor(page) {
|
|
755
|
+
super((resolve, reject) => {
|
|
756
|
+
page.then(resolve, reject);
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
async *[Symbol.asyncIterator]() {
|
|
760
|
+
yield* await this;
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
// packages/runtime/src/http/parser.ts
|
|
765
|
+
import { z as z2 } from "zod";
|
|
766
|
+
var ParseError = class extends Error {
|
|
767
|
+
data;
|
|
768
|
+
constructor(data) {
|
|
769
|
+
super("Validation failed");
|
|
770
|
+
this.name = "ParseError";
|
|
771
|
+
this.data = data;
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
function parseInput(schema, input) {
|
|
775
|
+
const result = schema.safeParse(input);
|
|
776
|
+
if (!result.success) {
|
|
777
|
+
const error = z2.flattenError(result.error, (issue) => issue);
|
|
778
|
+
throw new ParseError(error);
|
|
779
|
+
}
|
|
780
|
+
return result.data;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// packages/runtime/src/http/security.ts
|
|
784
|
+
import { z as z3 } from "zod";
|
|
785
|
+
var providerSchema = () => z3.custom(
|
|
786
|
+
(value) => typeof value === "function"
|
|
787
|
+
);
|
|
788
|
+
var stringCredentialSchema = z3.union([
|
|
789
|
+
z3.string(),
|
|
790
|
+
z3.literal(true),
|
|
791
|
+
providerSchema()
|
|
792
|
+
]);
|
|
793
|
+
var basicCredentialValueSchema = z3.object({
|
|
794
|
+
username: z3.string(),
|
|
795
|
+
password: z3.string()
|
|
796
|
+
});
|
|
797
|
+
var basicCredentialSchema = z3.union([
|
|
798
|
+
basicCredentialValueSchema,
|
|
799
|
+
z3.literal(true),
|
|
800
|
+
providerSchema()
|
|
801
|
+
]);
|
|
802
|
+
var mutualTlsCredentialSchema = z3.union([
|
|
803
|
+
z3.literal(true),
|
|
804
|
+
providerSchema()
|
|
805
|
+
]);
|
|
806
|
+
function createSecurityInterceptor(requirements, credentials, schemes) {
|
|
807
|
+
return {
|
|
808
|
+
async before(config) {
|
|
809
|
+
if (requirements.length === 0) return config;
|
|
810
|
+
const secured = requirements.filter(
|
|
811
|
+
(requirement) => Object.keys(requirement).length > 0
|
|
812
|
+
);
|
|
813
|
+
const selected = secured.find(
|
|
814
|
+
(requirement) => Object.keys(requirement).every(
|
|
815
|
+
(name) => credentials?.[name] !== void 0
|
|
816
|
+
)
|
|
817
|
+
);
|
|
818
|
+
if (!selected) {
|
|
819
|
+
if (requirements.some(
|
|
820
|
+
(requirement) => Object.keys(requirement).length === 0
|
|
821
|
+
))
|
|
822
|
+
return config;
|
|
823
|
+
throw new Error(
|
|
824
|
+
`Missing credentials for security requirements: ${secured.map((requirement) => Object.keys(requirement).join(" + ")).join(" or ")}`
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
for (const [name, values] of Object.entries(selected)) {
|
|
828
|
+
const scheme = schemes[name];
|
|
829
|
+
if (!scheme) {
|
|
830
|
+
throw new Error(`Unsupported external security scheme: ${name}`);
|
|
831
|
+
}
|
|
832
|
+
const configured = credentials?.[name];
|
|
833
|
+
const isOAuth = scheme.type === "oauth2" || scheme.type === "openIdConnect";
|
|
834
|
+
const credential = typeof configured === "function" ? await configured({
|
|
835
|
+
scheme: name,
|
|
836
|
+
scopes: isOAuth ? values : [],
|
|
837
|
+
roles: isOAuth ? [] : values
|
|
838
|
+
}) : configured;
|
|
839
|
+
applyCredential(config, scheme, credential);
|
|
840
|
+
}
|
|
841
|
+
return config;
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
function applyCredential(config, scheme, credential) {
|
|
846
|
+
if (credential === void 0) {
|
|
847
|
+
throw new Error("Security credential provider returned no credential");
|
|
848
|
+
}
|
|
849
|
+
if (credential === true) return;
|
|
850
|
+
if (scheme.type === "apiKey") {
|
|
851
|
+
if (typeof credential !== "string") {
|
|
852
|
+
throw new TypeError("API key credentials must be strings");
|
|
853
|
+
}
|
|
854
|
+
if (scheme.in === "header") {
|
|
855
|
+
if (!config.init.headers.has(scheme.name)) {
|
|
856
|
+
config.init.headers.set(scheme.name, credential);
|
|
857
|
+
}
|
|
858
|
+
} else if (scheme.in === "query") {
|
|
859
|
+
if (!config.url.searchParams.has(scheme.name)) {
|
|
860
|
+
config.url.searchParams.set(scheme.name, credential);
|
|
861
|
+
}
|
|
862
|
+
} else if (scheme.in === "cookie") {
|
|
863
|
+
if ("document" in globalThis) {
|
|
864
|
+
throw new Error(
|
|
865
|
+
`Cannot send the ${scheme.name} cookie credential: browsers forbid setting the Cookie header. Let the browser send the cookie and pass a fetch that sets credentials: 'include'.`
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
const cookie = `${scheme.name}=${encodeURIComponent(credential)}`;
|
|
869
|
+
const existing = config.init.headers.get("Cookie");
|
|
870
|
+
if (!existing?.split(";").some((part) => part.trim().startsWith(`${scheme.name}=`))) {
|
|
871
|
+
config.init.headers.set(
|
|
872
|
+
"Cookie",
|
|
873
|
+
existing ? `${existing}; ${cookie}` : cookie
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
} else {
|
|
877
|
+
throw new TypeError(`Unsupported apiKey location: ${String(scheme.in)}`);
|
|
878
|
+
}
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
if (scheme.type === "mutualTLS") {
|
|
882
|
+
throw new TypeError(
|
|
883
|
+
"mutualTLS credentials must be true when the custom fetch owns the client certificate"
|
|
884
|
+
);
|
|
885
|
+
}
|
|
886
|
+
if (scheme.type === "http" && scheme.scheme.toLowerCase() === "basic") {
|
|
887
|
+
if (typeof credential !== "object" || !("username" in credential) || !("password" in credential)) {
|
|
888
|
+
throw new TypeError("Basic credentials require username and password");
|
|
889
|
+
}
|
|
890
|
+
const bytes = new TextEncoder().encode(
|
|
891
|
+
`${credential.username}:${credential.password}`
|
|
892
|
+
);
|
|
893
|
+
let binary2 = "";
|
|
894
|
+
for (const byte of bytes) binary2 += String.fromCharCode(byte);
|
|
895
|
+
if (!config.init.headers.has("Authorization")) {
|
|
896
|
+
config.init.headers.set("Authorization", `Basic ${btoa(binary2)}`);
|
|
897
|
+
}
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
if (typeof credential !== "string") {
|
|
901
|
+
throw new TypeError(`${scheme.type} credentials must be strings`);
|
|
902
|
+
}
|
|
903
|
+
const prefix = scheme.type === "http" ? authorizationScheme(scheme.scheme) : "Bearer";
|
|
904
|
+
if (!config.init.headers.has("Authorization")) {
|
|
905
|
+
config.init.headers.set("Authorization", `${prefix} ${credential}`);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
var authorizationSchemes = {
|
|
909
|
+
basic: "Basic",
|
|
910
|
+
bearer: "Bearer",
|
|
911
|
+
digest: "Digest",
|
|
912
|
+
hoba: "HOBA",
|
|
913
|
+
mutual: "Mutual",
|
|
914
|
+
negotiate: "Negotiate",
|
|
915
|
+
oauth: "OAuth",
|
|
916
|
+
"scram-sha-1": "SCRAM-SHA-1",
|
|
917
|
+
"scram-sha-256": "SCRAM-SHA-256"
|
|
918
|
+
};
|
|
919
|
+
function authorizationScheme(scheme) {
|
|
920
|
+
return authorizationSchemes[scheme.toLowerCase()] ?? scheme;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
// packages/runtime/src/http/client.ts
|
|
924
|
+
var baseUrlSchema = z4.union([
|
|
925
|
+
z4.string(),
|
|
926
|
+
z4.custom(
|
|
927
|
+
(value) => typeof value === "function"
|
|
928
|
+
)
|
|
929
|
+
]).transform(async (baseUrl, ctx) => {
|
|
930
|
+
const value = typeof baseUrl === "function" ? await baseUrl() : baseUrl;
|
|
931
|
+
if (typeof value !== "string") {
|
|
932
|
+
ctx.addIssue({
|
|
933
|
+
code: "custom",
|
|
934
|
+
message: "baseUrl must resolve to a string"
|
|
935
|
+
});
|
|
936
|
+
return z4.NEVER;
|
|
937
|
+
}
|
|
938
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(value)) {
|
|
939
|
+
return value;
|
|
940
|
+
}
|
|
941
|
+
const page = Reflect.get(
|
|
942
|
+
globalThis,
|
|
943
|
+
"location"
|
|
944
|
+
);
|
|
945
|
+
if (page === void 0) {
|
|
946
|
+
throw new TypeError(
|
|
947
|
+
`baseUrl "${value}" is relative, and there is no page location to resolve it against.`
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
return new URL(value, page.href).href;
|
|
951
|
+
});
|
|
952
|
+
function settingsSchema(api) {
|
|
953
|
+
const [server, ...others] = api.servers;
|
|
954
|
+
return z4.object({
|
|
955
|
+
// OpenAPI 3.2: a spec without servers is served from "/".
|
|
956
|
+
baseUrl: others.length > 0 ? baseUrlSchema : baseUrlSchema.prefault(server ?? "/"),
|
|
957
|
+
headers: z4.record(z4.string(), z4.string()).optional(),
|
|
958
|
+
fetch: fetchType,
|
|
959
|
+
skipValidation: z4.boolean().optional(),
|
|
960
|
+
credentials: api.credentials.optional()
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
var Client = class {
|
|
964
|
+
api;
|
|
965
|
+
options;
|
|
966
|
+
#settings;
|
|
967
|
+
constructor(api, options) {
|
|
968
|
+
this.api = api;
|
|
969
|
+
this.options = options;
|
|
970
|
+
this.#settings = settingsSchema(api);
|
|
971
|
+
}
|
|
972
|
+
/** The settings, validated, with baseUrl resolved. */
|
|
973
|
+
async settings() {
|
|
974
|
+
return this.#settings.parseAsync(this.options);
|
|
975
|
+
}
|
|
976
|
+
request(endpoint, input, options) {
|
|
977
|
+
const pagination = this.api.routes[endpoint]?.pagination;
|
|
978
|
+
if (pagination) {
|
|
979
|
+
return new PagePromise(
|
|
980
|
+
firstPage(this, endpoint, input, pagination, options)
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
return send(this, endpoint, input, options).then(
|
|
984
|
+
(result) => result instanceof APIResponse ? result.data : result
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
/** Builds a ready-to-send request without sending it. */
|
|
988
|
+
async prepare(endpoint, input, options) {
|
|
989
|
+
return prepare(this, endpoint, input, options);
|
|
990
|
+
}
|
|
991
|
+
async defaultHeaders() {
|
|
992
|
+
const settings = await this.settings();
|
|
993
|
+
return { ...settings.headers };
|
|
994
|
+
}
|
|
995
|
+
setOptions(options) {
|
|
996
|
+
this.options = { ...this.options, ...options };
|
|
997
|
+
}
|
|
998
|
+
};
|
|
999
|
+
function createClient(api, settings) {
|
|
1000
|
+
return new Client(api, settings);
|
|
1001
|
+
}
|
|
1002
|
+
async function firstPage(client, endpoint, input, pagination, options) {
|
|
1003
|
+
if (typeof input !== "object" || input === null) {
|
|
1004
|
+
throw new TypeError(
|
|
1005
|
+
`${endpoint} pages through its input, so the input must be an object`
|
|
1006
|
+
);
|
|
1007
|
+
}
|
|
1008
|
+
const fetchPage = async (pageInput, pageOptions) => {
|
|
1009
|
+
const result = await send(client, endpoint, pageInput, {
|
|
1010
|
+
...options,
|
|
1011
|
+
...pageOptions
|
|
1012
|
+
});
|
|
1013
|
+
if (!(result instanceof APIResponse) || result.status !== pagination.statusCode) {
|
|
1014
|
+
throw result;
|
|
1015
|
+
}
|
|
1016
|
+
const data = result.data;
|
|
1017
|
+
if (typeof data !== "object" || data === null) {
|
|
1018
|
+
throw new TypeError(`${endpoint} answered a page that is not an object`);
|
|
1019
|
+
}
|
|
1020
|
+
return createPage(data, pageInput, pagination, fetchPage);
|
|
1021
|
+
};
|
|
1022
|
+
return fetchPage(Object.fromEntries(Object.entries(input)), void 0);
|
|
1023
|
+
}
|
|
1024
|
+
function interceptorsFor(route, settings, api, options) {
|
|
1025
|
+
return [
|
|
1026
|
+
createHeadersInterceptor({ ...settings.headers }, options?.headers ?? {}),
|
|
1027
|
+
createSecurityInterceptor(
|
|
1028
|
+
route.security,
|
|
1029
|
+
settings.credentials,
|
|
1030
|
+
api.securitySchemes
|
|
1031
|
+
)
|
|
1032
|
+
];
|
|
1033
|
+
}
|
|
1034
|
+
async function request(client, endpoint, input, options) {
|
|
1035
|
+
return send(client, endpoint, input, options);
|
|
1036
|
+
}
|
|
1037
|
+
function routeOf(api, endpoint) {
|
|
1038
|
+
const route = api.routes[endpoint];
|
|
1039
|
+
if (route === void 0) {
|
|
1040
|
+
throw new Error(`The api has no endpoint '${endpoint}'`);
|
|
1041
|
+
}
|
|
1042
|
+
return route;
|
|
1043
|
+
}
|
|
1044
|
+
async function send(client, endpoint, input, options) {
|
|
1045
|
+
const route = routeOf(client.api, endpoint);
|
|
1046
|
+
const settings = await client.settings();
|
|
1047
|
+
const parsedInput = settings.skipValidation ? input : parseInput(route.schema, input);
|
|
1048
|
+
return route.dispatch(parsedInput, {
|
|
1049
|
+
fetch: settings.fetch,
|
|
1050
|
+
baseUrl: settings.baseUrl,
|
|
1051
|
+
interceptors: interceptorsFor(route, settings, client.api, options),
|
|
1052
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
async function prepare(client, endpoint, input, options) {
|
|
1056
|
+
const route = routeOf(client.api, endpoint);
|
|
1057
|
+
const settings = await client.settings();
|
|
1058
|
+
const parsedInput = settings.skipValidation ? input : parseInput(route.schema, input);
|
|
1059
|
+
let config = route.toRequest(parsedInput, settings.baseUrl);
|
|
1060
|
+
if (options?.signal) {
|
|
1061
|
+
config = { ...config, init: { ...config.init, signal: options.signal } };
|
|
1062
|
+
}
|
|
1063
|
+
for (const interceptor of interceptorsFor(
|
|
1064
|
+
route,
|
|
1065
|
+
settings,
|
|
1066
|
+
client.api,
|
|
1067
|
+
options
|
|
1068
|
+
)) {
|
|
1069
|
+
if (interceptor.before) {
|
|
1070
|
+
config = await interceptor.before(config);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
return {
|
|
1074
|
+
...config,
|
|
1075
|
+
parse: (response) => parse2(route.output, response)
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
export {
|
|
1079
|
+
APIError,
|
|
1080
|
+
APIResponse,
|
|
1081
|
+
Accepted,
|
|
1082
|
+
BadGateway,
|
|
1083
|
+
BadRequest,
|
|
1084
|
+
Client,
|
|
1085
|
+
Conflict,
|
|
1086
|
+
Created,
|
|
1087
|
+
Dispatcher,
|
|
1088
|
+
Forbidden,
|
|
1089
|
+
GatewayTimeout,
|
|
1090
|
+
Gone,
|
|
1091
|
+
InternalServerError,
|
|
1092
|
+
MethodNotAllowed,
|
|
1093
|
+
NoContent,
|
|
1094
|
+
NotAcceptable,
|
|
1095
|
+
NotFound,
|
|
1096
|
+
NotImplemented,
|
|
1097
|
+
Ok,
|
|
1098
|
+
PagePromise,
|
|
1099
|
+
ParseError,
|
|
1100
|
+
PayloadTooLarge,
|
|
1101
|
+
PaymentRequired,
|
|
1102
|
+
PreconditionFailed,
|
|
1103
|
+
ServiceUnavailable,
|
|
1104
|
+
TooManyRequests,
|
|
1105
|
+
Unauthorized,
|
|
1106
|
+
UnprocessableEntity,
|
|
1107
|
+
UnsupportedMediaType,
|
|
1108
|
+
basicCredentialSchema,
|
|
1109
|
+
binary,
|
|
1110
|
+
bodyless,
|
|
1111
|
+
buffered,
|
|
1112
|
+
chunked,
|
|
1113
|
+
createClient,
|
|
1114
|
+
createHeadersInterceptor,
|
|
1115
|
+
createPage,
|
|
1116
|
+
createSecurityInterceptor,
|
|
1117
|
+
empty,
|
|
1118
|
+
fetchType,
|
|
1119
|
+
formdata,
|
|
1120
|
+
isTypeOf,
|
|
1121
|
+
json,
|
|
1122
|
+
mutualTlsCredentialSchema,
|
|
1123
|
+
parse2 as parse,
|
|
1124
|
+
parseInput,
|
|
1125
|
+
prepare,
|
|
1126
|
+
request,
|
|
1127
|
+
sse,
|
|
1128
|
+
stringCredentialSchema,
|
|
1129
|
+
text,
|
|
1130
|
+
toRequest,
|
|
1131
|
+
urlencoded
|
|
1132
|
+
};
|
|
1133
|
+
//# sourceMappingURL=index.js.map
|