@silverfish-app/sdk 1.0.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.
@@ -0,0 +1,1006 @@
1
+ 'use strict';
2
+
3
+ // src/core/bodySerializer.gen.ts
4
+ var serializeFormDataPair = (data, key, value) => {
5
+ if (typeof value === "string" || value instanceof Blob) {
6
+ data.append(key, value);
7
+ } else if (value instanceof Date) {
8
+ data.append(key, value.toISOString());
9
+ } else {
10
+ data.append(key, JSON.stringify(value));
11
+ }
12
+ };
13
+ var serializeUrlSearchParamsPair = (data, key, value) => {
14
+ if (typeof value === "string") {
15
+ data.append(key, value);
16
+ } else {
17
+ data.append(key, JSON.stringify(value));
18
+ }
19
+ };
20
+ var formDataBodySerializer = {
21
+ bodySerializer: (body) => {
22
+ const data = new FormData();
23
+ Object.entries(body).forEach(([key, value]) => {
24
+ if (value === void 0 || value === null) {
25
+ return;
26
+ }
27
+ if (Array.isArray(value)) {
28
+ value.forEach((v) => serializeFormDataPair(data, key, v));
29
+ } else {
30
+ serializeFormDataPair(data, key, value);
31
+ }
32
+ });
33
+ return data;
34
+ }
35
+ };
36
+ var jsonBodySerializer = {
37
+ bodySerializer: (body) => JSON.stringify(body, (_key, value) => typeof value === "bigint" ? value.toString() : value)
38
+ };
39
+ var urlSearchParamsBodySerializer = {
40
+ bodySerializer: (body) => {
41
+ const data = new URLSearchParams();
42
+ Object.entries(body).forEach(([key, value]) => {
43
+ if (value === void 0 || value === null) {
44
+ return;
45
+ }
46
+ if (Array.isArray(value)) {
47
+ value.forEach((v) => serializeUrlSearchParamsPair(data, key, v));
48
+ } else {
49
+ serializeUrlSearchParamsPair(data, key, value);
50
+ }
51
+ });
52
+ return data.toString();
53
+ }
54
+ };
55
+
56
+ // src/core/params.gen.ts
57
+ var extraPrefixesMap = {
58
+ $body_: "body",
59
+ $headers_: "headers",
60
+ $path_: "path",
61
+ $query_: "query"
62
+ };
63
+ var extraPrefixes = Object.entries(extraPrefixesMap);
64
+ function buildKeyMap(fields, map) {
65
+ if (!map) {
66
+ map = /* @__PURE__ */ new Map();
67
+ }
68
+ for (const config of fields) {
69
+ if ("in" in config) {
70
+ if (config.key) {
71
+ map.set(config.key, {
72
+ in: config.in,
73
+ map: config.map
74
+ });
75
+ }
76
+ } else if ("key" in config) {
77
+ map.set(config.key, {
78
+ map: config.map
79
+ });
80
+ } else if (config.args) {
81
+ buildKeyMap(config.args, map);
82
+ }
83
+ }
84
+ return map;
85
+ }
86
+ function stripEmptySlots(params) {
87
+ for (const [slot, value] of Object.entries(params)) {
88
+ if (value && typeof value === "object" && !Array.isArray(value) && !Object.keys(value).length) {
89
+ delete params[slot];
90
+ }
91
+ }
92
+ }
93
+ function buildClientParams(args, fields) {
94
+ const params = {
95
+ body: /* @__PURE__ */ Object.create(null),
96
+ headers: /* @__PURE__ */ Object.create(null),
97
+ path: /* @__PURE__ */ Object.create(null),
98
+ query: /* @__PURE__ */ Object.create(null)
99
+ };
100
+ const map = buildKeyMap(fields);
101
+ let config;
102
+ for (const [index, arg] of args.entries()) {
103
+ if (fields[index]) {
104
+ config = fields[index];
105
+ }
106
+ if (!config) {
107
+ continue;
108
+ }
109
+ if ("in" in config) {
110
+ if (config.key) {
111
+ const field = map.get(config.key);
112
+ const name = field.map || config.key;
113
+ if (field.in) {
114
+ params[field.in][name] = arg;
115
+ }
116
+ } else {
117
+ params.body = arg;
118
+ }
119
+ } else {
120
+ for (const [key, value] of Object.entries(arg ?? {})) {
121
+ const field = map.get(key);
122
+ if (field) {
123
+ if (field.in) {
124
+ const name = field.map || key;
125
+ params[field.in][name] = value;
126
+ } else {
127
+ params[field.map] = value;
128
+ }
129
+ } else {
130
+ const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix));
131
+ if (extra) {
132
+ const [prefix, slot] = extra;
133
+ params[slot][key.slice(prefix.length)] = value;
134
+ } else if ("allowExtra" in config && config.allowExtra) {
135
+ for (const [slot, allowed] of Object.entries(config.allowExtra)) {
136
+ if (allowed) {
137
+ params[slot][key] = value;
138
+ break;
139
+ }
140
+ }
141
+ }
142
+ }
143
+ }
144
+ }
145
+ }
146
+ stripEmptySlots(params);
147
+ return params;
148
+ }
149
+
150
+ // src/core/queryKeySerializer.gen.ts
151
+ var queryKeyJsonReplacer = (_key, value) => {
152
+ if (value === void 0 || typeof value === "function" || typeof value === "symbol") {
153
+ return void 0;
154
+ }
155
+ if (typeof value === "bigint") {
156
+ return value.toString();
157
+ }
158
+ if (value instanceof Date) {
159
+ return value.toISOString();
160
+ }
161
+ return value;
162
+ };
163
+ var stringifyToJsonValue = (input) => {
164
+ try {
165
+ const json = JSON.stringify(input, queryKeyJsonReplacer);
166
+ if (json === void 0) {
167
+ return void 0;
168
+ }
169
+ return JSON.parse(json);
170
+ } catch {
171
+ return void 0;
172
+ }
173
+ };
174
+ var isPlainObject = (value) => {
175
+ if (value === null || typeof value !== "object") {
176
+ return false;
177
+ }
178
+ const prototype = Object.getPrototypeOf(value);
179
+ return prototype === Object.prototype || prototype === null;
180
+ };
181
+ var serializeSearchParams = (params) => {
182
+ const entries = Array.from(params.entries()).sort(([a], [b]) => a.localeCompare(b));
183
+ const result = {};
184
+ for (const [key, value] of entries) {
185
+ const existing = result[key];
186
+ if (existing === void 0) {
187
+ result[key] = value;
188
+ continue;
189
+ }
190
+ if (Array.isArray(existing)) {
191
+ existing.push(value);
192
+ } else {
193
+ result[key] = [existing, value];
194
+ }
195
+ }
196
+ return result;
197
+ };
198
+ var serializeQueryKeyValue = (value) => {
199
+ if (value === null) {
200
+ return null;
201
+ }
202
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
203
+ return value;
204
+ }
205
+ if (value === void 0 || typeof value === "function" || typeof value === "symbol") {
206
+ return void 0;
207
+ }
208
+ if (typeof value === "bigint") {
209
+ return value.toString();
210
+ }
211
+ if (value instanceof Date) {
212
+ return value.toISOString();
213
+ }
214
+ if (Array.isArray(value)) {
215
+ return stringifyToJsonValue(value);
216
+ }
217
+ if (typeof URLSearchParams !== "undefined" && value instanceof URLSearchParams) {
218
+ return serializeSearchParams(value);
219
+ }
220
+ if (isPlainObject(value)) {
221
+ return stringifyToJsonValue(value);
222
+ }
223
+ return void 0;
224
+ };
225
+
226
+ // src/core/serverSentEvents.gen.ts
227
+ function createSseClient({
228
+ onRequest,
229
+ onSseError,
230
+ onSseEvent,
231
+ responseTransformer,
232
+ responseValidator,
233
+ sseDefaultRetryDelay,
234
+ sseMaxRetryAttempts,
235
+ sseMaxRetryDelay,
236
+ sseSleepFn,
237
+ url,
238
+ ...options
239
+ }) {
240
+ let lastEventId;
241
+ const sleep = sseSleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
242
+ const createStream = async function* () {
243
+ let retryDelay = sseDefaultRetryDelay ?? 3e3;
244
+ let attempt = 0;
245
+ const signal = options.signal ?? new AbortController().signal;
246
+ while (true) {
247
+ if (signal.aborted) break;
248
+ attempt++;
249
+ const headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers);
250
+ if (lastEventId !== void 0) {
251
+ headers.set("Last-Event-ID", lastEventId);
252
+ }
253
+ try {
254
+ const requestInit = {
255
+ redirect: "follow",
256
+ ...options,
257
+ body: options.serializedBody,
258
+ headers,
259
+ signal
260
+ };
261
+ let request = new Request(url, requestInit);
262
+ if (onRequest) {
263
+ request = await onRequest(url, requestInit);
264
+ }
265
+ const _fetch = options.fetch ?? globalThis.fetch;
266
+ const response = await _fetch(request);
267
+ if (!response.ok) throw new Error(`SSE failed: ${response.status} ${response.statusText}`);
268
+ if (!response.body) throw new Error("No body in SSE response");
269
+ const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
270
+ let buffer = "";
271
+ const abortHandler = () => {
272
+ try {
273
+ reader.cancel();
274
+ } catch {
275
+ }
276
+ };
277
+ signal.addEventListener("abort", abortHandler);
278
+ try {
279
+ while (true) {
280
+ const { done, value } = await reader.read();
281
+ if (done) break;
282
+ buffer += value;
283
+ buffer = buffer.replace(/\r\n?/g, "\n");
284
+ const chunks = buffer.split("\n\n");
285
+ buffer = chunks.pop() ?? "";
286
+ for (const chunk of chunks) {
287
+ const lines = chunk.split("\n");
288
+ const dataLines = [];
289
+ let eventName;
290
+ for (const line of lines) {
291
+ if (line.startsWith("data:")) {
292
+ dataLines.push(line.replace(/^data:\s*/, ""));
293
+ } else if (line.startsWith("event:")) {
294
+ eventName = line.replace(/^event:\s*/, "");
295
+ } else if (line.startsWith("id:")) {
296
+ lastEventId = line.replace(/^id:\s*/, "");
297
+ } else if (line.startsWith("retry:")) {
298
+ const parsed = Number.parseInt(line.replace(/^retry:\s*/, ""), 10);
299
+ if (!Number.isNaN(parsed)) {
300
+ retryDelay = parsed;
301
+ }
302
+ }
303
+ }
304
+ let data;
305
+ let parsedJson = false;
306
+ if (dataLines.length) {
307
+ const rawData = dataLines.join("\n");
308
+ try {
309
+ data = JSON.parse(rawData);
310
+ parsedJson = true;
311
+ } catch {
312
+ data = rawData;
313
+ }
314
+ }
315
+ if (parsedJson) {
316
+ if (responseValidator) {
317
+ await responseValidator(data);
318
+ }
319
+ if (responseTransformer) {
320
+ data = await responseTransformer(data);
321
+ }
322
+ }
323
+ onSseEvent?.({
324
+ data,
325
+ event: eventName,
326
+ id: lastEventId,
327
+ retry: retryDelay
328
+ });
329
+ if (dataLines.length) {
330
+ yield data;
331
+ }
332
+ }
333
+ }
334
+ } finally {
335
+ signal.removeEventListener("abort", abortHandler);
336
+ reader.releaseLock();
337
+ }
338
+ break;
339
+ } catch (error) {
340
+ onSseError?.(error);
341
+ if (sseMaxRetryAttempts !== void 0 && attempt >= sseMaxRetryAttempts) {
342
+ break;
343
+ }
344
+ const backoff = Math.min(retryDelay * 2 ** (attempt - 1), sseMaxRetryDelay ?? 3e4);
345
+ await sleep(backoff);
346
+ }
347
+ }
348
+ };
349
+ const stream = createStream();
350
+ return { stream };
351
+ }
352
+
353
+ // src/core/pathSerializer.gen.ts
354
+ var separatorArrayExplode = (style) => {
355
+ switch (style) {
356
+ case "label":
357
+ return ".";
358
+ case "matrix":
359
+ return ";";
360
+ case "simple":
361
+ return ",";
362
+ default:
363
+ return "&";
364
+ }
365
+ };
366
+ var separatorArrayNoExplode = (style) => {
367
+ switch (style) {
368
+ case "form":
369
+ return ",";
370
+ case "pipeDelimited":
371
+ return "|";
372
+ case "spaceDelimited":
373
+ return "%20";
374
+ default:
375
+ return ",";
376
+ }
377
+ };
378
+ var separatorObjectExplode = (style) => {
379
+ switch (style) {
380
+ case "label":
381
+ return ".";
382
+ case "matrix":
383
+ return ";";
384
+ case "simple":
385
+ return ",";
386
+ default:
387
+ return "&";
388
+ }
389
+ };
390
+ var serializeArrayParam = ({
391
+ allowReserved,
392
+ explode,
393
+ name,
394
+ style,
395
+ value
396
+ }) => {
397
+ if (!explode) {
398
+ const joinedValues2 = (allowReserved ? value : value.map((v) => encodeURIComponent(v))).join(separatorArrayNoExplode(style));
399
+ switch (style) {
400
+ case "label":
401
+ return `.${joinedValues2}`;
402
+ case "matrix":
403
+ return `;${name}=${joinedValues2}`;
404
+ case "simple":
405
+ return joinedValues2;
406
+ default:
407
+ return `${name}=${joinedValues2}`;
408
+ }
409
+ }
410
+ const separator = separatorArrayExplode(style);
411
+ const joinedValues = value.map((v) => {
412
+ if (style === "label" || style === "simple") {
413
+ return allowReserved ? v : encodeURIComponent(v);
414
+ }
415
+ return serializePrimitiveParam({
416
+ allowReserved,
417
+ name,
418
+ value: v
419
+ });
420
+ }).join(separator);
421
+ return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues;
422
+ };
423
+ var serializePrimitiveParam = ({
424
+ allowReserved,
425
+ name,
426
+ value
427
+ }) => {
428
+ if (value === void 0 || value === null) {
429
+ return "";
430
+ }
431
+ if (typeof value === "object") {
432
+ throw new Error(
433
+ "Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these."
434
+ );
435
+ }
436
+ return `${name}=${allowReserved ? value : encodeURIComponent(value)}`;
437
+ };
438
+ var serializeObjectParam = ({
439
+ allowReserved,
440
+ explode,
441
+ name,
442
+ style,
443
+ value,
444
+ valueOnly
445
+ }) => {
446
+ if (value instanceof Date) {
447
+ return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`;
448
+ }
449
+ if (style !== "deepObject" && !explode) {
450
+ let values = [];
451
+ Object.entries(value).forEach(([key, v]) => {
452
+ values = [...values, key, allowReserved ? v : encodeURIComponent(v)];
453
+ });
454
+ const joinedValues2 = values.join(",");
455
+ switch (style) {
456
+ case "form":
457
+ return `${name}=${joinedValues2}`;
458
+ case "label":
459
+ return `.${joinedValues2}`;
460
+ case "matrix":
461
+ return `;${name}=${joinedValues2}`;
462
+ default:
463
+ return joinedValues2;
464
+ }
465
+ }
466
+ const separator = separatorObjectExplode(style);
467
+ const joinedValues = Object.entries(value).map(
468
+ ([key, v]) => serializePrimitiveParam({
469
+ allowReserved,
470
+ name: style === "deepObject" ? `${name}[${key}]` : key,
471
+ value: v
472
+ })
473
+ ).join(separator);
474
+ return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues;
475
+ };
476
+
477
+ // src/core/utils.gen.ts
478
+ var PATH_PARAM_RE = /\{[^{}]+\}/g;
479
+ var defaultPathSerializer = ({ path, url: _url }) => {
480
+ let url = _url;
481
+ const matches = _url.match(PATH_PARAM_RE);
482
+ if (matches) {
483
+ for (const match of matches) {
484
+ let explode = false;
485
+ let name = match.substring(1, match.length - 1);
486
+ let style = "simple";
487
+ if (name.endsWith("*")) {
488
+ explode = true;
489
+ name = name.substring(0, name.length - 1);
490
+ }
491
+ if (name.startsWith(".")) {
492
+ name = name.substring(1);
493
+ style = "label";
494
+ } else if (name.startsWith(";")) {
495
+ name = name.substring(1);
496
+ style = "matrix";
497
+ }
498
+ const value = path[name];
499
+ if (value === void 0 || value === null) {
500
+ continue;
501
+ }
502
+ if (Array.isArray(value)) {
503
+ url = url.replace(match, serializeArrayParam({ explode, name, style, value }));
504
+ continue;
505
+ }
506
+ if (typeof value === "object") {
507
+ url = url.replace(
508
+ match,
509
+ serializeObjectParam({
510
+ explode,
511
+ name,
512
+ style,
513
+ value,
514
+ valueOnly: true
515
+ })
516
+ );
517
+ continue;
518
+ }
519
+ if (style === "matrix") {
520
+ url = url.replace(
521
+ match,
522
+ `;${serializePrimitiveParam({
523
+ name,
524
+ value
525
+ })}`
526
+ );
527
+ continue;
528
+ }
529
+ const replaceValue = encodeURIComponent(
530
+ style === "label" ? `.${value}` : value
531
+ );
532
+ url = url.replace(match, replaceValue);
533
+ }
534
+ }
535
+ return url;
536
+ };
537
+ var getUrl = ({
538
+ baseUrl,
539
+ path,
540
+ query,
541
+ querySerializer,
542
+ url: _url
543
+ }) => {
544
+ const pathUrl = _url.startsWith("/") ? _url : `/${_url}`;
545
+ let url = (baseUrl ?? "") + pathUrl;
546
+ if (path) {
547
+ url = defaultPathSerializer({ path, url });
548
+ }
549
+ let search = query ? querySerializer(query) : "";
550
+ if (search.startsWith("?")) {
551
+ search = search.substring(1);
552
+ }
553
+ if (search) {
554
+ url += `?${search}`;
555
+ }
556
+ return url;
557
+ };
558
+ function getValidRequestBody(options) {
559
+ const hasBody = options.body !== void 0;
560
+ const isSerializedBody = hasBody && options.bodySerializer;
561
+ if (isSerializedBody) {
562
+ if ("serializedBody" in options) {
563
+ const hasSerializedBody = options.serializedBody !== void 0 && options.serializedBody !== "";
564
+ return hasSerializedBody ? options.serializedBody : null;
565
+ }
566
+ return options.body !== "" ? options.body : null;
567
+ }
568
+ if (hasBody) {
569
+ return options.body;
570
+ }
571
+ return void 0;
572
+ }
573
+
574
+ // src/core/auth.gen.ts
575
+ var getAuthToken = async (auth, callback) => {
576
+ const token = typeof callback === "function" ? await callback(auth) : callback;
577
+ if (!token) {
578
+ return;
579
+ }
580
+ if (auth.scheme === "bearer") {
581
+ return `Bearer ${token}`;
582
+ }
583
+ if (auth.scheme === "basic") {
584
+ return `Basic ${btoa(token)}`;
585
+ }
586
+ return token;
587
+ };
588
+
589
+ // src/client/utils.gen.ts
590
+ var createQuerySerializer = ({
591
+ parameters = {},
592
+ ...args
593
+ } = {}) => {
594
+ const querySerializer = (queryParams) => {
595
+ const search = [];
596
+ if (queryParams && typeof queryParams === "object") {
597
+ for (const name in queryParams) {
598
+ const value = queryParams[name];
599
+ if (value === void 0 || value === null) {
600
+ continue;
601
+ }
602
+ const options = parameters[name] || args;
603
+ if (Array.isArray(value)) {
604
+ const serializedArray = serializeArrayParam({
605
+ allowReserved: options.allowReserved,
606
+ explode: true,
607
+ name,
608
+ style: "form",
609
+ value,
610
+ ...options.array
611
+ });
612
+ if (serializedArray) search.push(serializedArray);
613
+ } else if (typeof value === "object") {
614
+ const serializedObject = serializeObjectParam({
615
+ allowReserved: options.allowReserved,
616
+ explode: true,
617
+ name,
618
+ style: "deepObject",
619
+ value,
620
+ ...options.object
621
+ });
622
+ if (serializedObject) search.push(serializedObject);
623
+ } else {
624
+ const serializedPrimitive = serializePrimitiveParam({
625
+ allowReserved: options.allowReserved,
626
+ name,
627
+ value
628
+ });
629
+ if (serializedPrimitive) search.push(serializedPrimitive);
630
+ }
631
+ }
632
+ }
633
+ return search.join("&");
634
+ };
635
+ return querySerializer;
636
+ };
637
+ var getParseAs = (contentType) => {
638
+ if (!contentType) {
639
+ return "stream";
640
+ }
641
+ const cleanContent = contentType.split(";")[0]?.trim();
642
+ if (!cleanContent) {
643
+ return;
644
+ }
645
+ if (cleanContent.startsWith("application/json") || cleanContent.endsWith("+json")) {
646
+ return "json";
647
+ }
648
+ if (cleanContent === "multipart/form-data") {
649
+ return "formData";
650
+ }
651
+ if (["application/", "audio/", "image/", "video/"].some((type) => cleanContent.startsWith(type))) {
652
+ return "blob";
653
+ }
654
+ if (cleanContent.startsWith("text/")) {
655
+ return "text";
656
+ }
657
+ return;
658
+ };
659
+ var checkForExistence = (options, name) => {
660
+ if (!name) {
661
+ return false;
662
+ }
663
+ if (options.headers.has(name) || options.query?.[name] || options.headers.get("Cookie")?.includes(`${name}=`)) {
664
+ return true;
665
+ }
666
+ return false;
667
+ };
668
+ async function setAuthParams(options) {
669
+ for (const auth of options.security ?? []) {
670
+ if (checkForExistence(options, auth.name)) {
671
+ continue;
672
+ }
673
+ const token = await getAuthToken(auth, options.auth);
674
+ if (!token) {
675
+ continue;
676
+ }
677
+ const name = auth.name ?? "Authorization";
678
+ switch (auth.in) {
679
+ case "query":
680
+ if (!options.query) {
681
+ options.query = {};
682
+ }
683
+ options.query[name] = token;
684
+ break;
685
+ case "cookie":
686
+ options.headers.append("Cookie", `${name}=${token}`);
687
+ break;
688
+ case "header":
689
+ default:
690
+ options.headers.set(name, token);
691
+ break;
692
+ }
693
+ }
694
+ }
695
+ var buildUrl = (options) => getUrl({
696
+ baseUrl: options.baseUrl,
697
+ path: options.path,
698
+ query: options.query,
699
+ querySerializer: typeof options.querySerializer === "function" ? options.querySerializer : createQuerySerializer(options.querySerializer),
700
+ url: options.url
701
+ });
702
+ var mergeConfigs = (a, b) => {
703
+ const config = { ...a, ...b };
704
+ if (config.baseUrl?.endsWith("/")) {
705
+ config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
706
+ }
707
+ config.headers = mergeHeaders(a.headers, b.headers);
708
+ return config;
709
+ };
710
+ var headersEntries = (headers) => {
711
+ const entries = [];
712
+ headers.forEach((value, key) => {
713
+ entries.push([key, value]);
714
+ });
715
+ return entries;
716
+ };
717
+ var mergeHeaders = (...headers) => {
718
+ const mergedHeaders = new Headers();
719
+ for (const header of headers) {
720
+ if (!header) {
721
+ continue;
722
+ }
723
+ const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header);
724
+ for (const [key, value] of iterator) {
725
+ if (value === null) {
726
+ mergedHeaders.delete(key);
727
+ } else if (Array.isArray(value)) {
728
+ for (const v of value) {
729
+ mergedHeaders.append(key, v);
730
+ }
731
+ } else if (value !== void 0) {
732
+ mergedHeaders.set(
733
+ key,
734
+ typeof value === "object" ? JSON.stringify(value) : value
735
+ );
736
+ }
737
+ }
738
+ }
739
+ return mergedHeaders;
740
+ };
741
+ var Interceptors = class {
742
+ fns = [];
743
+ clear() {
744
+ this.fns = [];
745
+ }
746
+ eject(id) {
747
+ const index = this.getInterceptorIndex(id);
748
+ if (this.fns[index]) {
749
+ this.fns[index] = null;
750
+ }
751
+ }
752
+ exists(id) {
753
+ const index = this.getInterceptorIndex(id);
754
+ return Boolean(this.fns[index]);
755
+ }
756
+ getInterceptorIndex(id) {
757
+ if (typeof id === "number") {
758
+ return this.fns[id] ? id : -1;
759
+ }
760
+ return this.fns.indexOf(id);
761
+ }
762
+ update(id, fn) {
763
+ const index = this.getInterceptorIndex(id);
764
+ if (this.fns[index]) {
765
+ this.fns[index] = fn;
766
+ return id;
767
+ }
768
+ return false;
769
+ }
770
+ use(fn) {
771
+ this.fns.push(fn);
772
+ return this.fns.length - 1;
773
+ }
774
+ };
775
+ var createInterceptors = () => ({
776
+ error: new Interceptors(),
777
+ request: new Interceptors(),
778
+ response: new Interceptors()
779
+ });
780
+ var defaultQuerySerializer = createQuerySerializer({
781
+ allowReserved: false,
782
+ array: {
783
+ explode: true,
784
+ style: "form"
785
+ },
786
+ object: {
787
+ explode: true,
788
+ style: "deepObject"
789
+ }
790
+ });
791
+ var defaultHeaders = {
792
+ "Content-Type": "application/json"
793
+ };
794
+ var createConfig = (override = {}) => ({
795
+ ...jsonBodySerializer,
796
+ headers: defaultHeaders,
797
+ parseAs: "auto",
798
+ querySerializer: defaultQuerySerializer,
799
+ ...override
800
+ });
801
+
802
+ // src/client/client.gen.ts
803
+ var createClient = (config = {}) => {
804
+ let _config = mergeConfigs(createConfig(), config);
805
+ const getConfig = () => ({ ..._config });
806
+ const setConfig = (config2) => {
807
+ _config = mergeConfigs(_config, config2);
808
+ return getConfig();
809
+ };
810
+ const interceptors = createInterceptors();
811
+ const beforeRequest = async (options) => {
812
+ const opts = {
813
+ ..._config,
814
+ ...options,
815
+ fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
816
+ headers: mergeHeaders(_config.headers, options.headers),
817
+ serializedBody: void 0
818
+ };
819
+ if (opts.security) {
820
+ await setAuthParams(opts);
821
+ }
822
+ if (opts.requestValidator) {
823
+ await opts.requestValidator(opts);
824
+ }
825
+ if (opts.body !== void 0 && opts.bodySerializer) {
826
+ opts.serializedBody = opts.bodySerializer(opts.body);
827
+ }
828
+ if (opts.body === void 0 || opts.serializedBody === "") {
829
+ opts.headers.delete("Content-Type");
830
+ }
831
+ const resolvedOpts = opts;
832
+ const url = buildUrl(resolvedOpts);
833
+ return { opts: resolvedOpts, url };
834
+ };
835
+ const request = async (options) => {
836
+ const throwOnError = options.throwOnError ?? _config.throwOnError;
837
+ const responseStyle = options.responseStyle ?? _config.responseStyle;
838
+ let request2;
839
+ let response;
840
+ try {
841
+ const { opts, url } = await beforeRequest(options);
842
+ const requestInit = {
843
+ redirect: "follow",
844
+ ...opts,
845
+ body: getValidRequestBody(opts)
846
+ };
847
+ request2 = new Request(url, requestInit);
848
+ for (const fn of interceptors.request.fns) {
849
+ if (fn) {
850
+ request2 = await fn(request2, opts);
851
+ }
852
+ }
853
+ const _fetch = opts.fetch;
854
+ response = await _fetch(request2);
855
+ for (const fn of interceptors.response.fns) {
856
+ if (fn) {
857
+ response = await fn(response, request2, opts);
858
+ }
859
+ }
860
+ const result = {
861
+ request: request2,
862
+ response
863
+ };
864
+ if (response.ok) {
865
+ const parseAs = (opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json";
866
+ if (response.status === 204 || response.headers.get("Content-Length") === "0") {
867
+ let emptyData;
868
+ switch (parseAs) {
869
+ case "arrayBuffer":
870
+ case "blob":
871
+ case "text":
872
+ emptyData = await response[parseAs]();
873
+ break;
874
+ case "formData":
875
+ emptyData = new FormData();
876
+ break;
877
+ case "stream":
878
+ emptyData = response.body;
879
+ break;
880
+ case "json":
881
+ default:
882
+ emptyData = {};
883
+ break;
884
+ }
885
+ return opts.responseStyle === "data" ? emptyData : {
886
+ data: emptyData,
887
+ ...result
888
+ };
889
+ }
890
+ let data;
891
+ switch (parseAs) {
892
+ case "arrayBuffer":
893
+ case "blob":
894
+ case "formData":
895
+ case "text":
896
+ data = await response[parseAs]();
897
+ break;
898
+ case "json": {
899
+ const text = await response.text();
900
+ data = text ? JSON.parse(text) : {};
901
+ break;
902
+ }
903
+ case "stream":
904
+ return opts.responseStyle === "data" ? response.body : {
905
+ data: response.body,
906
+ ...result
907
+ };
908
+ }
909
+ if (parseAs === "json") {
910
+ if (opts.responseValidator) {
911
+ await opts.responseValidator(data);
912
+ }
913
+ if (opts.responseTransformer) {
914
+ data = await opts.responseTransformer(data);
915
+ }
916
+ }
917
+ return opts.responseStyle === "data" ? data : {
918
+ data,
919
+ ...result
920
+ };
921
+ }
922
+ const textError = await response.text();
923
+ let jsonError;
924
+ try {
925
+ jsonError = JSON.parse(textError);
926
+ } catch {
927
+ }
928
+ throw jsonError ?? textError;
929
+ } catch (error) {
930
+ let finalError = error;
931
+ for (const fn of interceptors.error.fns) {
932
+ if (fn) {
933
+ finalError = await fn(finalError, response, request2, options);
934
+ }
935
+ }
936
+ finalError = finalError || {};
937
+ if (throwOnError) {
938
+ throw finalError;
939
+ }
940
+ return responseStyle === "data" ? void 0 : {
941
+ error: finalError,
942
+ request: request2,
943
+ response
944
+ };
945
+ }
946
+ };
947
+ const makeMethodFn = (method) => (options) => request({ ...options, method });
948
+ const makeSseFn = (method) => async (options) => {
949
+ const { opts, url } = await beforeRequest(options);
950
+ return createSseClient({
951
+ ...opts,
952
+ body: opts.body,
953
+ method,
954
+ onRequest: async (url2, init) => {
955
+ let request2 = new Request(url2, init);
956
+ for (const fn of interceptors.request.fns) {
957
+ if (fn) {
958
+ request2 = await fn(request2, opts);
959
+ }
960
+ }
961
+ return request2;
962
+ },
963
+ serializedBody: getValidRequestBody(opts),
964
+ url
965
+ });
966
+ };
967
+ const _buildUrl = (options) => buildUrl({ ..._config, ...options });
968
+ return {
969
+ buildUrl: _buildUrl,
970
+ connect: makeMethodFn("CONNECT"),
971
+ delete: makeMethodFn("DELETE"),
972
+ get: makeMethodFn("GET"),
973
+ getConfig,
974
+ head: makeMethodFn("HEAD"),
975
+ interceptors,
976
+ options: makeMethodFn("OPTIONS"),
977
+ patch: makeMethodFn("PATCH"),
978
+ post: makeMethodFn("POST"),
979
+ put: makeMethodFn("PUT"),
980
+ request,
981
+ setConfig,
982
+ sse: {
983
+ connect: makeSseFn("CONNECT"),
984
+ delete: makeSseFn("DELETE"),
985
+ get: makeSseFn("GET"),
986
+ head: makeSseFn("HEAD"),
987
+ options: makeSseFn("OPTIONS"),
988
+ patch: makeSseFn("PATCH"),
989
+ post: makeSseFn("POST"),
990
+ put: makeSseFn("PUT"),
991
+ trace: makeSseFn("TRACE")
992
+ },
993
+ trace: makeMethodFn("TRACE")
994
+ };
995
+ };
996
+
997
+ exports.buildClientParams = buildClientParams;
998
+ exports.createClient = createClient;
999
+ exports.createConfig = createConfig;
1000
+ exports.formDataBodySerializer = formDataBodySerializer;
1001
+ exports.jsonBodySerializer = jsonBodySerializer;
1002
+ exports.mergeHeaders = mergeHeaders;
1003
+ exports.serializeQueryKeyValue = serializeQueryKeyValue;
1004
+ exports.urlSearchParamsBodySerializer = urlSearchParamsBodySerializer;
1005
+ //# sourceMappingURL=index.cjs.map
1006
+ //# sourceMappingURL=index.cjs.map