@sdk-it/rpc 0.29.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/dist/index.js ADDED
@@ -0,0 +1,756 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // packages/rpc/src/lib/rpc.ts
8
+ import { z as z2 } from "zod";
9
+ import { augmentSpec, forEachOperation, loadSpec } from "@sdk-it/spec";
10
+ import { buildInput, inputToPath, toHttpOutput } from "@sdk-it/typescript";
11
+
12
+ // packages/rpc/src/lib/http/dispatcher.ts
13
+ import z from "zod";
14
+
15
+ // packages/rpc/src/lib/http/interceptors.ts
16
+ var createHeadersInterceptor = (defaultHeaders, requestHeaders) => {
17
+ return {
18
+ before({ init, url }) {
19
+ const headers = defaultHeaders();
20
+ for (const [key, value] of new Headers(requestHeaders)) {
21
+ if (value !== void 0 && !init.headers.has(key)) {
22
+ init.headers.set(key, value);
23
+ }
24
+ }
25
+ for (const [key, value] of Object.entries(headers)) {
26
+ if (value !== void 0 && !init.headers.has(key)) {
27
+ init.headers.set(key, value);
28
+ }
29
+ }
30
+ return { init, url };
31
+ }
32
+ };
33
+ };
34
+ var createBaseUrlInterceptor = (getBaseUrl) => {
35
+ return {
36
+ before({ init, url }) {
37
+ const baseUrl = getBaseUrl();
38
+ if (url.protocol === "local:") {
39
+ return {
40
+ init,
41
+ url: new URL(url.href.replace("local://", baseUrl))
42
+ };
43
+ }
44
+ return { init, url };
45
+ }
46
+ };
47
+ };
48
+
49
+ // packages/rpc/src/lib/http/request.ts
50
+ function createUrl(path, query) {
51
+ const url = new URL(path, `local://`);
52
+ url.search = query.toString();
53
+ return url;
54
+ }
55
+ function template(templateString, templateVariables) {
56
+ const nargs = /{([0-9a-zA-Z_]+)}/g;
57
+ return templateString.replace(nargs, (match, key, index) => {
58
+ if (templateString[index - 1] === "{" && templateString[index + match.length] === "}") {
59
+ return key;
60
+ }
61
+ const result = key in templateVariables ? templateVariables[key] : null;
62
+ return result === null || result === void 0 ? "" : String(result);
63
+ });
64
+ }
65
+ var Serializer = class {
66
+ input;
67
+ props;
68
+ constructor(input, props) {
69
+ this.input = input;
70
+ this.props = props;
71
+ }
72
+ serialize() {
73
+ const headers = new Headers({});
74
+ for (const header of this.props.inputHeaders) {
75
+ headers.set(header, this.input[header]);
76
+ }
77
+ const query = new URLSearchParams();
78
+ for (const key of this.props.inputQuery) {
79
+ const value = this.input[key];
80
+ if (value !== void 0) {
81
+ query.set(key, String(value));
82
+ }
83
+ }
84
+ const params = this.props.inputParams.reduce(
85
+ (acc, key) => {
86
+ acc[key] = this.input[key];
87
+ return acc;
88
+ },
89
+ {}
90
+ );
91
+ return {
92
+ body: this.getBody(),
93
+ query,
94
+ params,
95
+ headers: this.getHeaders()
96
+ };
97
+ }
98
+ };
99
+ var JsonSerializer = class extends Serializer {
100
+ getBody() {
101
+ const body = {};
102
+ if (this.props.inputBody.length === 1 && this.props.inputBody[0] === "$body") {
103
+ return JSON.stringify(this.input.$body);
104
+ }
105
+ for (const prop of this.props.inputBody) {
106
+ body[prop] = this.input[prop];
107
+ }
108
+ return JSON.stringify(body);
109
+ }
110
+ getHeaders() {
111
+ return {
112
+ "Content-Type": "application/json",
113
+ Accept: "application/json"
114
+ };
115
+ }
116
+ };
117
+ var UrlencodedSerializer = class extends Serializer {
118
+ getBody() {
119
+ const body = new URLSearchParams();
120
+ for (const prop of this.props.inputBody) {
121
+ body.set(prop, this.input[prop]);
122
+ }
123
+ return body;
124
+ }
125
+ getHeaders() {
126
+ return {
127
+ "Content-Type": "application/x-www-form-urlencoded",
128
+ Accept: "application/json"
129
+ };
130
+ }
131
+ };
132
+ var EmptySerializer = class extends Serializer {
133
+ getBody() {
134
+ return null;
135
+ }
136
+ getHeaders() {
137
+ return {};
138
+ }
139
+ };
140
+ var FormDataSerializer = class extends Serializer {
141
+ getBody() {
142
+ const body = new FormData();
143
+ for (const prop of this.props.inputBody) {
144
+ body.append(prop, this.input[prop]);
145
+ }
146
+ return body;
147
+ }
148
+ getHeaders() {
149
+ return {
150
+ Accept: "application/json"
151
+ };
152
+ }
153
+ };
154
+ function json(input, props) {
155
+ return new JsonSerializer(input, props).serialize();
156
+ }
157
+ function urlencoded(input, props) {
158
+ return new UrlencodedSerializer(input, props).serialize();
159
+ }
160
+ function empty(input, props) {
161
+ return new EmptySerializer(input, props).serialize();
162
+ }
163
+ function formdata(input, props) {
164
+ return new FormDataSerializer(input, props).serialize();
165
+ }
166
+ function toRequest(endpoint, input) {
167
+ const [method, path] = endpoint.split(" ");
168
+ const pathVariable = template(path, input.params);
169
+ return {
170
+ url: createUrl(pathVariable, input.query),
171
+ init: {
172
+ method,
173
+ headers: new Headers(input.headers),
174
+ body: method === "GET" ? void 0 : input.body
175
+ }
176
+ };
177
+ }
178
+
179
+ // packages/rpc/src/lib/http/parse-response.ts
180
+ import { parse } from "fast-content-type-parse";
181
+ async function buffered(response) {
182
+ const contentType = response.headers.get("Content-Type");
183
+ if (!contentType) {
184
+ throw new Error("Content-Type header is missing");
185
+ }
186
+ if (response.status === 204) {
187
+ return null;
188
+ }
189
+ const { type } = parse(contentType);
190
+ switch (type) {
191
+ case "application/json":
192
+ return response.json();
193
+ case "text/plain":
194
+ return response.text();
195
+ case "text/html":
196
+ return response.text();
197
+ case "text/xml":
198
+ case "application/xml":
199
+ return response.text();
200
+ case "application/x-www-form-urlencoded": {
201
+ const text = await response.text();
202
+ return Object.fromEntries(new URLSearchParams(text));
203
+ }
204
+ case "multipart/form-data":
205
+ return response.formData();
206
+ default:
207
+ throw new Error(`Unsupported content type: ${contentType}`);
208
+ }
209
+ }
210
+
211
+ // packages/rpc/src/lib/http/response.ts
212
+ var response_exports = {};
213
+ __export(response_exports, {
214
+ APIError: () => APIError,
215
+ APIResponse: () => APIResponse,
216
+ Accepted: () => Accepted,
217
+ BadGateway: () => BadGateway,
218
+ BadRequest: () => BadRequest,
219
+ Conflict: () => Conflict,
220
+ Created: () => Created,
221
+ Forbidden: () => Forbidden,
222
+ GatewayTimeout: () => GatewayTimeout,
223
+ Gone: () => Gone,
224
+ InternalServerError: () => InternalServerError,
225
+ KIND: () => KIND,
226
+ MethodNotAllowed: () => MethodNotAllowed,
227
+ NoContent: () => NoContent,
228
+ NotAcceptable: () => NotAcceptable,
229
+ NotFound: () => NotFound,
230
+ NotImplemented: () => NotImplemented,
231
+ Ok: () => Ok,
232
+ PayloadTooLarge: () => PayloadTooLarge,
233
+ PaymentRequired: () => PaymentRequired,
234
+ PreconditionFailed: () => PreconditionFailed,
235
+ ServiceUnavailable: () => ServiceUnavailable,
236
+ TooManyRequests: () => TooManyRequests,
237
+ Unauthorized: () => Unauthorized,
238
+ UnprocessableEntity: () => UnprocessableEntity,
239
+ UnsupportedMediaType: () => UnsupportedMediaType
240
+ });
241
+ var KIND = Symbol("APIDATA");
242
+ var APIResponse = class {
243
+ static status;
244
+ static kind = Symbol.for("APIResponse");
245
+ status;
246
+ data;
247
+ constructor(status, data) {
248
+ this.status = status;
249
+ this.data = data;
250
+ }
251
+ static create(status, data) {
252
+ return new this(status, data);
253
+ }
254
+ };
255
+ var APIError = class extends APIResponse {
256
+ static create(status, data) {
257
+ return new this(status, data);
258
+ }
259
+ };
260
+ var Ok = class _Ok extends APIResponse {
261
+ static kind = Symbol.for("Ok");
262
+ static status = 200;
263
+ constructor(data) {
264
+ super(_Ok.status, data);
265
+ }
266
+ static create(status, data) {
267
+ Object.defineProperty(data, KIND, { value: this.kind });
268
+ return new this(data);
269
+ }
270
+ static is(value) {
271
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
272
+ }
273
+ };
274
+ var Created = class _Created extends APIResponse {
275
+ static kind = Symbol.for("Created");
276
+ static status = 201;
277
+ constructor(data) {
278
+ super(_Created.status, data);
279
+ }
280
+ static create(status, data) {
281
+ Object.defineProperty(data, KIND, { value: this.kind });
282
+ return new this(data);
283
+ }
284
+ static is(value) {
285
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
286
+ }
287
+ };
288
+ var Accepted = class _Accepted extends APIResponse {
289
+ static kind = Symbol.for("Accepted");
290
+ static status = 202;
291
+ constructor(data) {
292
+ super(_Accepted.status, data);
293
+ }
294
+ static create(status, data) {
295
+ Object.defineProperty(data, KIND, { value: this.kind });
296
+ return new this(data);
297
+ }
298
+ static is(value) {
299
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
300
+ }
301
+ };
302
+ var NoContent = class _NoContent extends APIResponse {
303
+ static kind = Symbol.for("NoContent");
304
+ static status = 204;
305
+ constructor() {
306
+ super(_NoContent.status, null);
307
+ }
308
+ static create(status, data) {
309
+ return new this();
310
+ }
311
+ static is(value) {
312
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
313
+ }
314
+ };
315
+ var BadRequest = class _BadRequest extends APIError {
316
+ static kind = Symbol.for("BadRequest");
317
+ static status = 400;
318
+ constructor(data) {
319
+ super(_BadRequest.status, data);
320
+ }
321
+ static create(status, data) {
322
+ Object.defineProperty(data, KIND, { value: this.kind });
323
+ return new this(data);
324
+ }
325
+ static is(value) {
326
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
327
+ }
328
+ };
329
+ var Unauthorized = class _Unauthorized extends APIError {
330
+ static kind = Symbol.for("Unauthorized");
331
+ static status = 401;
332
+ constructor(data) {
333
+ super(_Unauthorized.status, data);
334
+ }
335
+ static create(status, data) {
336
+ Object.defineProperty(data, KIND, { value: this.kind });
337
+ return new this(data);
338
+ }
339
+ static is(value) {
340
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
341
+ }
342
+ };
343
+ var PaymentRequired = class _PaymentRequired extends APIError {
344
+ static kind = Symbol.for("PaymentRequired");
345
+ static status = 402;
346
+ constructor(data) {
347
+ super(_PaymentRequired.status, data);
348
+ }
349
+ static create(status, data) {
350
+ Object.defineProperty(data, KIND, { value: this.kind });
351
+ return new this(data);
352
+ }
353
+ static is(value) {
354
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
355
+ }
356
+ };
357
+ var Forbidden = class _Forbidden extends APIError {
358
+ static kind = Symbol.for("Forbidden");
359
+ static status = 403;
360
+ constructor(data) {
361
+ super(_Forbidden.status, data);
362
+ }
363
+ static create(status, data) {
364
+ Object.defineProperty(data, KIND, { value: this.kind });
365
+ return new this(data);
366
+ }
367
+ static is(value) {
368
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
369
+ }
370
+ };
371
+ var NotFound = class _NotFound extends APIError {
372
+ static kind = Symbol.for("NotFound");
373
+ static status = 404;
374
+ constructor(data) {
375
+ super(_NotFound.status, data);
376
+ }
377
+ static create(status, data) {
378
+ Object.defineProperty(data, KIND, { value: this.kind });
379
+ return new this(data);
380
+ }
381
+ static is(value) {
382
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
383
+ }
384
+ };
385
+ var MethodNotAllowed = class _MethodNotAllowed extends APIError {
386
+ static kind = Symbol.for("MethodNotAllowed");
387
+ static status = 405;
388
+ constructor(data) {
389
+ super(_MethodNotAllowed.status, data);
390
+ }
391
+ static create(status, data) {
392
+ Object.defineProperty(data, KIND, { value: this.kind });
393
+ return new this(data);
394
+ }
395
+ static is(value) {
396
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
397
+ }
398
+ };
399
+ var NotAcceptable = class _NotAcceptable extends APIError {
400
+ static kind = Symbol.for("NotAcceptable");
401
+ static status = 406;
402
+ constructor(data) {
403
+ super(_NotAcceptable.status, data);
404
+ }
405
+ static create(status, data) {
406
+ Object.defineProperty(data, KIND, { value: this.kind });
407
+ return new this(data);
408
+ }
409
+ static is(value) {
410
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
411
+ }
412
+ };
413
+ var Conflict = class _Conflict extends APIError {
414
+ static kind = Symbol.for("Conflict");
415
+ static status = 409;
416
+ constructor(data) {
417
+ super(_Conflict.status, data);
418
+ }
419
+ static create(status, data) {
420
+ Object.defineProperty(data, KIND, { value: this.kind });
421
+ return new this(data);
422
+ }
423
+ static is(value) {
424
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
425
+ }
426
+ };
427
+ var Gone = class _Gone extends APIError {
428
+ static kind = Symbol.for("Gone");
429
+ static status = 410;
430
+ constructor(data) {
431
+ super(_Gone.status, data);
432
+ }
433
+ static create(status, data) {
434
+ Object.defineProperty(data, KIND, { value: this.kind });
435
+ return new this(data);
436
+ }
437
+ static is(value) {
438
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
439
+ }
440
+ };
441
+ var PreconditionFailed = class _PreconditionFailed extends APIError {
442
+ static kind = Symbol.for("PreconditionFailed");
443
+ static status = 412;
444
+ constructor(data) {
445
+ super(_PreconditionFailed.status, data);
446
+ }
447
+ static create(status, data) {
448
+ Object.defineProperty(data, KIND, { value: this.kind });
449
+ return new this(data);
450
+ }
451
+ static is(value) {
452
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
453
+ }
454
+ };
455
+ var UnprocessableEntity = class _UnprocessableEntity extends APIError {
456
+ static kind = Symbol.for("UnprocessableEntity");
457
+ static status = 422;
458
+ constructor(data) {
459
+ super(_UnprocessableEntity.status, data);
460
+ }
461
+ static create(status, data) {
462
+ Object.defineProperty(data, KIND, { value: this.kind });
463
+ return new this(data);
464
+ }
465
+ static is(value) {
466
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
467
+ }
468
+ };
469
+ var TooManyRequests = class _TooManyRequests extends APIError {
470
+ static kind = Symbol.for("TooManyRequests");
471
+ static status = 429;
472
+ constructor(data) {
473
+ super(_TooManyRequests.status, data);
474
+ }
475
+ static create(status, data) {
476
+ Object.defineProperty(data, KIND, { value: this.kind });
477
+ return new this(data);
478
+ }
479
+ static is(value) {
480
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
481
+ }
482
+ };
483
+ var PayloadTooLarge = class _PayloadTooLarge extends APIError {
484
+ static kind = Symbol.for("PayloadTooLarge");
485
+ static status = 413;
486
+ constructor(data) {
487
+ super(_PayloadTooLarge.status, data);
488
+ }
489
+ static create(status, data) {
490
+ Object.defineProperty(data, KIND, { value: this.kind });
491
+ return new this(data);
492
+ }
493
+ static is(value) {
494
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
495
+ }
496
+ };
497
+ var UnsupportedMediaType = class _UnsupportedMediaType extends APIError {
498
+ static kind = Symbol.for("UnsupportedMediaType");
499
+ static status = 415;
500
+ constructor(data) {
501
+ super(_UnsupportedMediaType.status, data);
502
+ }
503
+ static create(status, data) {
504
+ Object.defineProperty(data, KIND, { value: this.kind });
505
+ return new this(data);
506
+ }
507
+ static is(value) {
508
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
509
+ }
510
+ };
511
+ var InternalServerError = class _InternalServerError extends APIError {
512
+ static kind = Symbol.for("InternalServerError");
513
+ static status = 500;
514
+ constructor(data) {
515
+ super(_InternalServerError.status, data);
516
+ }
517
+ static create(status, data) {
518
+ Object.defineProperty(data, KIND, { value: this.kind });
519
+ return new this(data);
520
+ }
521
+ static is(value) {
522
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
523
+ }
524
+ };
525
+ var NotImplemented = class _NotImplemented extends APIError {
526
+ static kind = Symbol.for("NotImplemented");
527
+ static status = 501;
528
+ constructor(data) {
529
+ super(_NotImplemented.status, data);
530
+ }
531
+ static create(status, data) {
532
+ Object.defineProperty(data, KIND, { value: this.kind });
533
+ return new this(data);
534
+ }
535
+ static is(value) {
536
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
537
+ }
538
+ };
539
+ var BadGateway = class _BadGateway extends APIError {
540
+ static kind = Symbol.for("BadGateway");
541
+ static status = 502;
542
+ constructor(data) {
543
+ super(_BadGateway.status, data);
544
+ }
545
+ static create(status, data) {
546
+ Object.defineProperty(data, KIND, { value: this.kind });
547
+ return new this(data);
548
+ }
549
+ static is(value) {
550
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
551
+ }
552
+ };
553
+ var ServiceUnavailable = class _ServiceUnavailable extends APIError {
554
+ static kind = Symbol.for("ServiceUnavailable");
555
+ static status = 503;
556
+ constructor(data) {
557
+ super(_ServiceUnavailable.status, data);
558
+ }
559
+ static create(status, data) {
560
+ Object.defineProperty(data, KIND, { value: this.kind });
561
+ return new this(data);
562
+ }
563
+ static is(value) {
564
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
565
+ }
566
+ };
567
+ var GatewayTimeout = class _GatewayTimeout extends APIError {
568
+ static kind = Symbol.for("GatewayTimeout");
569
+ static status = 504;
570
+ constructor(data) {
571
+ super(_GatewayTimeout.status, data);
572
+ }
573
+ static create(status, data) {
574
+ Object.defineProperty(data, KIND, { value: this.kind });
575
+ return new this(data);
576
+ }
577
+ static is(value) {
578
+ return typeof value === "object" && value !== null && KIND in value && value[KIND] === this.kind;
579
+ }
580
+ };
581
+
582
+ // packages/rpc/src/lib/http/dispatcher.ts
583
+ var fetchType = z.function().args(z.instanceof(Request)).returns(z.promise(z.instanceof(Response))).optional();
584
+ async function parse2(outputs, response) {
585
+ let output = null;
586
+ let parser = buffered;
587
+ for (const outputType of outputs) {
588
+ if ("parser" in outputType) {
589
+ parser = outputType.parser;
590
+ if (isTypeOf(outputType.type, APIResponse)) {
591
+ if (response.status === outputType.type.status) {
592
+ output = outputType.type;
593
+ break;
594
+ }
595
+ }
596
+ } else if (isTypeOf(outputType, APIResponse)) {
597
+ if (response.status === outputType.status) {
598
+ output = outputType;
599
+ break;
600
+ }
601
+ }
602
+ }
603
+ if (response.ok) {
604
+ const apiresponse = (output || APIResponse).create(
605
+ response.status,
606
+ await parser(response)
607
+ );
608
+ return apiresponse;
609
+ }
610
+ throw (output || APIError).create(response.status, await parser(response));
611
+ }
612
+ function isTypeOf(instance, baseType) {
613
+ if (instance === baseType) {
614
+ return true;
615
+ }
616
+ const prototype = Object.getPrototypeOf(instance);
617
+ if (prototype === null) {
618
+ return false;
619
+ }
620
+ return isTypeOf(prototype, baseType);
621
+ }
622
+ var Dispatcher = class {
623
+ #interceptors = [];
624
+ #fetch;
625
+ constructor(interceptors, fetch2) {
626
+ this.#interceptors = interceptors;
627
+ this.#fetch = fetch2;
628
+ }
629
+ async send(config, outputs, signal) {
630
+ for (const interceptor of this.#interceptors) {
631
+ if (interceptor.before) {
632
+ config = await interceptor.before(config);
633
+ }
634
+ }
635
+ let response = await (this.#fetch ?? fetch)(
636
+ new Request(config.url, config.init),
637
+ {
638
+ ...config.init,
639
+ signal
640
+ }
641
+ );
642
+ for (let i = this.#interceptors.length - 1; i >= 0; i--) {
643
+ const interceptor = this.#interceptors[i];
644
+ if (interceptor.after) {
645
+ response = await interceptor.after(response.clone());
646
+ }
647
+ }
648
+ return await parse2(outputs, response);
649
+ }
650
+ };
651
+
652
+ // packages/rpc/src/lib/rpc.ts
653
+ var optionsSchema = z2.object({
654
+ token: z2.string().optional().transform((val) => val ? `Bearer ${val}` : void 0),
655
+ fetch: fetchType,
656
+ baseUrl: z2.string()
657
+ });
658
+ var Client = class {
659
+ options;
660
+ schemas;
661
+ constructor(options, schemas) {
662
+ this.options = optionsSchema.parse(options);
663
+ this.schemas = schemas;
664
+ }
665
+ async request(endpoint, input, options) {
666
+ const route = this.schemas[endpoint];
667
+ console.log(route);
668
+ const withDefaultInputs = Object.assign({}, this.#defaultInputs, input);
669
+ const parsedInput = input;
670
+ const result = await route.dispatch(parsedInput, {
671
+ fetch: this.options.fetch,
672
+ interceptors: [
673
+ createHeadersInterceptor(
674
+ () => this.defaultHeaders,
675
+ options?.headers ?? {}
676
+ ),
677
+ createBaseUrlInterceptor(() => this.options.baseUrl)
678
+ ],
679
+ signal: options?.signal
680
+ });
681
+ return result;
682
+ }
683
+ get defaultHeaders() {
684
+ return { authorization: this.options["token"] };
685
+ }
686
+ get #defaultInputs() {
687
+ return {};
688
+ }
689
+ setOptions(options) {
690
+ const validated = optionsSchema.partial().parse(options);
691
+ for (const key of Object.keys(validated)) {
692
+ if (validated[key] !== void 0) {
693
+ this.options[key] = validated[key];
694
+ }
695
+ }
696
+ }
697
+ };
698
+ async function rpc(openapi, options) {
699
+ const spec = await loadSpec(openapi);
700
+ const ir = augmentSpec({ spec, responses: { flattenErrorResponses: true } });
701
+ const schemas = {};
702
+ forEachOperation(ir, (entry, operation) => {
703
+ const endpoint = `${entry.method.toUpperCase()} ${entry.path}`;
704
+ const details = buildInput(ir, operation);
705
+ const contentTypeMap = {
706
+ json,
707
+ urlencoded,
708
+ formdata,
709
+ empty
710
+ };
711
+ console.log(operation.responses);
712
+ const outputs = Object.keys(operation.responses).flatMap(
713
+ (status) => toHttpOutput(
714
+ spec,
715
+ operation.operationId,
716
+ status,
717
+ operation.responses[status],
718
+ false
719
+ )
720
+ );
721
+ schemas[endpoint] = {
722
+ output: outputs.map((it) => response_exports[it.replace("http.", "")]),
723
+ toRequest(input) {
724
+ const serializer = contentTypeMap[details.outgoingContentType] || defaultSerializer(details.outgoingContentType);
725
+ return toRequest(
726
+ endpoint,
727
+ serializer(input, inputToPath(operation, details.inputs))
728
+ );
729
+ },
730
+ async dispatch(input, options2) {
731
+ const dispatcher = new Dispatcher(options2.interceptors, options2.fetch);
732
+ const result = await dispatcher.send(
733
+ this.toRequest(input),
734
+ this.output
735
+ );
736
+ return result;
737
+ }
738
+ };
739
+ });
740
+ console.log(schemas);
741
+ return new Client(
742
+ {
743
+ ...options,
744
+ baseUrl: options?.baseUrl ?? ir.servers[0].url
745
+ },
746
+ schemas
747
+ );
748
+ }
749
+ function defaultSerializer(ct) {
750
+ throw new Error(`Unsupported content type: ${ct}`);
751
+ }
752
+ export {
753
+ Client,
754
+ rpc
755
+ };
756
+ //# sourceMappingURL=index.js.map