@sdk-it/rpc 0.46.11 → 0.47.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/README.md +21 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +199 -899
- package/dist/index.js.map +4 -4
- package/dist/lib/rpc.d.ts +8 -10
- package/dist/lib/rpc.d.ts.map +1 -1
- package/dist/lib/zod.d.ts +5 -5
- package/dist/lib/zod.d.ts.map +1 -1
- package/package.json +20 -11
- package/dist/lib/http/dispatcher.d.ts +0 -33
- package/dist/lib/http/dispatcher.d.ts.map +0 -1
- package/dist/lib/http/interceptors.d.ts +0 -8
- package/dist/lib/http/interceptors.d.ts.map +0 -1
- package/dist/lib/http/parse-response.d.ts +0 -4
- package/dist/lib/http/parse-response.d.ts.map +0 -1
- package/dist/lib/http/parser.d.ts +0 -7
- package/dist/lib/http/parser.d.ts.map +0 -1
- package/dist/lib/http/request.d.ts +0 -55
- package/dist/lib/http/request.d.ts.map +0 -1
- package/dist/lib/http/response.d.ts +0 -176
- package/dist/lib/http/response.d.ts.map +0 -1
- package/dist/lib/http/sse.d.ts +0 -3
- package/dist/lib/http/sse.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,719 +1,40 @@
|
|
|
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/http/dispatcher.ts
|
|
8
|
-
import z from "zod";
|
|
9
|
-
|
|
10
|
-
// packages/rpc/src/lib/http/interceptors.ts
|
|
11
|
-
var createHeadersInterceptor = (headers, requestHeaders) => {
|
|
12
|
-
return {
|
|
13
|
-
before({ init, url }) {
|
|
14
|
-
for (const [key, value] of new Headers(requestHeaders)) {
|
|
15
|
-
if (value !== void 0 && !init.headers.has(key)) {
|
|
16
|
-
init.headers.set(key, value);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
20
|
-
if (value !== void 0 && !init.headers.has(key)) {
|
|
21
|
-
init.headers.set(key, value);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return { init, url };
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
var createBaseUrlInterceptor = (baseUrl) => {
|
|
29
|
-
return {
|
|
30
|
-
before({ init, url }) {
|
|
31
|
-
if (url.protocol === "local:") {
|
|
32
|
-
return {
|
|
33
|
-
init,
|
|
34
|
-
url: new URL(url.href.replace("local://", baseUrl))
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
return { init, url };
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// packages/rpc/src/lib/http/request.ts
|
|
43
|
-
function template(templateString, templateVariables) {
|
|
44
|
-
const nargs = /{([0-9a-zA-Z_]+)}/g;
|
|
45
|
-
return templateString.replace(nargs, (match, key, index) => {
|
|
46
|
-
if (templateString[index - 1] === "{" && templateString[index + match.length] === "}") {
|
|
47
|
-
return key;
|
|
48
|
-
}
|
|
49
|
-
const result = key in templateVariables ? templateVariables[key] : null;
|
|
50
|
-
return result === null || result === void 0 ? "" : encodeURIComponent(String(result));
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
function toWireValue(value) {
|
|
54
|
-
return value instanceof Date ? value.toISOString() : String(value);
|
|
55
|
-
}
|
|
56
|
-
var Serializer = class {
|
|
57
|
-
input;
|
|
58
|
-
props;
|
|
59
|
-
constructor(input, props) {
|
|
60
|
-
this.input = input;
|
|
61
|
-
this.props = props;
|
|
62
|
-
}
|
|
63
|
-
serialize(path) {
|
|
64
|
-
const params = this.props.inputParams.reduce(
|
|
65
|
-
(acc, key) => {
|
|
66
|
-
acc[key] = this.input[key];
|
|
67
|
-
return acc;
|
|
68
|
-
},
|
|
69
|
-
{}
|
|
70
|
-
);
|
|
71
|
-
const url = new URL(template(path, params), `local://`);
|
|
72
|
-
const headers = { ...this.getHeaders() };
|
|
73
|
-
for (const header of this.props.inputHeaders) {
|
|
74
|
-
const value = this.input[header];
|
|
75
|
-
if (value !== void 0) {
|
|
76
|
-
headers[header] = toWireValue(value);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
for (const key of this.props.inputQuery) {
|
|
80
|
-
const value = this.input[key];
|
|
81
|
-
if (value !== void 0) {
|
|
82
|
-
if (Array.isArray(value)) {
|
|
83
|
-
for (const item of value) {
|
|
84
|
-
url.searchParams.append(key, toWireValue(item));
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
url.searchParams.set(key, toWireValue(value));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return {
|
|
92
|
-
body: this.getBody(),
|
|
93
|
-
url,
|
|
94
|
-
headers
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
var JsonSerializer = class extends Serializer {
|
|
99
|
-
getBody() {
|
|
100
|
-
const body = {};
|
|
101
|
-
if (this.props.inputBody.length === 1 && this.props.inputBody[0] === "$body") {
|
|
102
|
-
return JSON.stringify(this.input.$body);
|
|
103
|
-
}
|
|
104
|
-
for (const prop of this.props.inputBody) {
|
|
105
|
-
body[prop] = this.input[prop];
|
|
106
|
-
}
|
|
107
|
-
return JSON.stringify(body);
|
|
108
|
-
}
|
|
109
|
-
getHeaders() {
|
|
110
|
-
return {
|
|
111
|
-
"Content-Type": "application/json",
|
|
112
|
-
Accept: "application/json"
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
var UrlencodedSerializer = class extends Serializer {
|
|
117
|
-
getBody() {
|
|
118
|
-
const body = new URLSearchParams();
|
|
119
|
-
for (const prop of this.props.inputBody) {
|
|
120
|
-
body.set(prop, toWireValue(this.input[prop]));
|
|
121
|
-
}
|
|
122
|
-
return body;
|
|
123
|
-
}
|
|
124
|
-
getHeaders() {
|
|
125
|
-
return {
|
|
126
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
127
|
-
Accept: "application/json"
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
var EmptySerializer = class extends Serializer {
|
|
132
|
-
getBody() {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
getHeaders() {
|
|
136
|
-
return {};
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
var FormDataSerializer = class extends Serializer {
|
|
140
|
-
getBody() {
|
|
141
|
-
const body = new FormData();
|
|
142
|
-
for (const prop of this.props.inputBody) {
|
|
143
|
-
body.append(prop, this.input[prop]);
|
|
144
|
-
}
|
|
145
|
-
return body;
|
|
146
|
-
}
|
|
147
|
-
getHeaders() {
|
|
148
|
-
return {
|
|
149
|
-
Accept: "application/json"
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
function json(input, props) {
|
|
154
|
-
return new JsonSerializer(input, props);
|
|
155
|
-
}
|
|
156
|
-
function urlencoded(input, props) {
|
|
157
|
-
return new UrlencodedSerializer(input, props);
|
|
158
|
-
}
|
|
159
|
-
function empty(input, props) {
|
|
160
|
-
return new EmptySerializer(input, props);
|
|
161
|
-
}
|
|
162
|
-
function formdata(input, props) {
|
|
163
|
-
return new FormDataSerializer(input, props);
|
|
164
|
-
}
|
|
165
|
-
function toRequest(endpoint, serializer) {
|
|
166
|
-
const [method, path] = endpoint.split(" ");
|
|
167
|
-
const input = serializer.serialize(path);
|
|
168
|
-
return {
|
|
169
|
-
url: input.url,
|
|
170
|
-
init: {
|
|
171
|
-
method,
|
|
172
|
-
headers: new Headers(input.headers),
|
|
173
|
-
body: method === "GET" ? void 0 : input.body
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// packages/rpc/src/lib/http/parse-response.ts
|
|
179
|
-
import { parse } from "fast-content-type-parse";
|
|
180
|
-
function isBinaryContentType(contentType) {
|
|
181
|
-
const type = contentType.toLowerCase();
|
|
182
|
-
if (type.startsWith("image/")) {
|
|
183
|
-
return true;
|
|
184
|
-
}
|
|
185
|
-
if (type.startsWith("audio/")) {
|
|
186
|
-
return true;
|
|
187
|
-
}
|
|
188
|
-
if (type.startsWith("video/")) {
|
|
189
|
-
return true;
|
|
190
|
-
}
|
|
191
|
-
switch (type) {
|
|
192
|
-
case "application/pdf":
|
|
193
|
-
case "application/zip":
|
|
194
|
-
case "application/gzip":
|
|
195
|
-
case "application/x-7z-compressed":
|
|
196
|
-
case "application/x-tar":
|
|
197
|
-
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
|
198
|
-
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
|
199
|
-
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
|
|
200
|
-
case "application/vnd.ms-excel":
|
|
201
|
-
case "application/vnd.ms-powerpoint":
|
|
202
|
-
case "application/msword":
|
|
203
|
-
case "application/octet-stream":
|
|
204
|
-
return true;
|
|
205
|
-
default:
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
function chunked(response) {
|
|
210
|
-
return response.body;
|
|
211
|
-
}
|
|
212
|
-
async function bodyless(response) {
|
|
213
|
-
await response.body?.cancel();
|
|
214
|
-
return null;
|
|
215
|
-
}
|
|
216
|
-
async function buffered(response) {
|
|
217
|
-
if (response.status === 204 || response.status === 205 || response.status === 304) {
|
|
218
|
-
return null;
|
|
219
|
-
}
|
|
220
|
-
const contentType = response.headers.get("Content-Type");
|
|
221
|
-
if (!contentType) {
|
|
222
|
-
throw new Error("Content-Type header is missing");
|
|
223
|
-
}
|
|
224
|
-
const { type } = parse(contentType);
|
|
225
|
-
if (isBinaryContentType(type)) {
|
|
226
|
-
return response.blob();
|
|
227
|
-
}
|
|
228
|
-
if (type.startsWith("text/")) {
|
|
229
|
-
return response.text();
|
|
230
|
-
}
|
|
231
|
-
switch (type) {
|
|
232
|
-
case "application/json":
|
|
233
|
-
return response.json();
|
|
234
|
-
case "application/xml":
|
|
235
|
-
return response.text();
|
|
236
|
-
case "application/x-www-form-urlencoded": {
|
|
237
|
-
const text = await response.text();
|
|
238
|
-
return Object.fromEntries(new URLSearchParams(text));
|
|
239
|
-
}
|
|
240
|
-
case "multipart/form-data":
|
|
241
|
-
return response.formData();
|
|
242
|
-
default:
|
|
243
|
-
throw new Error(`Unsupported content type: ${contentType}`);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// packages/rpc/src/lib/http/response.ts
|
|
248
|
-
var response_exports = {};
|
|
249
|
-
__export(response_exports, {
|
|
250
|
-
APIError: () => APIError,
|
|
251
|
-
APIResponse: () => APIResponse,
|
|
252
|
-
Accepted: () => Accepted,
|
|
253
|
-
BadGateway: () => BadGateway,
|
|
254
|
-
BadRequest: () => BadRequest,
|
|
255
|
-
Conflict: () => Conflict,
|
|
256
|
-
Created: () => Created,
|
|
257
|
-
Forbidden: () => Forbidden,
|
|
258
|
-
GatewayTimeout: () => GatewayTimeout,
|
|
259
|
-
Gone: () => Gone,
|
|
260
|
-
InternalServerError: () => InternalServerError,
|
|
261
|
-
MethodNotAllowed: () => MethodNotAllowed,
|
|
262
|
-
NoContent: () => NoContent,
|
|
263
|
-
NotAcceptable: () => NotAcceptable,
|
|
264
|
-
NotFound: () => NotFound,
|
|
265
|
-
NotImplemented: () => NotImplemented,
|
|
266
|
-
Ok: () => Ok,
|
|
267
|
-
PayloadTooLarge: () => PayloadTooLarge,
|
|
268
|
-
PaymentRequired: () => PaymentRequired,
|
|
269
|
-
PreconditionFailed: () => PreconditionFailed,
|
|
270
|
-
ServiceUnavailable: () => ServiceUnavailable,
|
|
271
|
-
TooManyRequests: () => TooManyRequests,
|
|
272
|
-
Unauthorized: () => Unauthorized,
|
|
273
|
-
UnprocessableEntity: () => UnprocessableEntity,
|
|
274
|
-
UnsupportedMediaType: () => UnsupportedMediaType
|
|
275
|
-
});
|
|
276
|
-
var APIResponse = class {
|
|
277
|
-
static status;
|
|
278
|
-
status;
|
|
279
|
-
data;
|
|
280
|
-
headers;
|
|
281
|
-
constructor(status, headers, data) {
|
|
282
|
-
this.status = status;
|
|
283
|
-
this.headers = headers;
|
|
284
|
-
this.data = data;
|
|
285
|
-
}
|
|
286
|
-
static create(status, headers, data) {
|
|
287
|
-
return new this(status, headers, data);
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
var APIError = class extends Error {
|
|
291
|
-
static status;
|
|
292
|
-
status;
|
|
293
|
-
data;
|
|
294
|
-
headers;
|
|
295
|
-
constructor(status, headers, data) {
|
|
296
|
-
super(failureMessage(status, data));
|
|
297
|
-
this.name = "APIError";
|
|
298
|
-
this.status = status;
|
|
299
|
-
this.headers = headers;
|
|
300
|
-
this.data = data;
|
|
301
|
-
}
|
|
302
|
-
static create(status, headers, data) {
|
|
303
|
-
return new this(status, headers, data);
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
function failureMessage(status, data) {
|
|
307
|
-
const summary = bodySummary(data);
|
|
308
|
-
return summary ? `Request failed with status code ${status}: ${summary}` : `Request failed with status code ${status}`;
|
|
309
|
-
}
|
|
310
|
-
function bodySummary(data) {
|
|
311
|
-
const tag = Object.prototype.toString.call(data);
|
|
312
|
-
let text = "";
|
|
313
|
-
if (typeof data === "string") {
|
|
314
|
-
text = data;
|
|
315
|
-
} else if (tag === "[object Object]" || tag === "[object Array]") {
|
|
316
|
-
text = JSON.stringify(data);
|
|
317
|
-
}
|
|
318
|
-
const line = text.replace(/\s+/g, " ").trim();
|
|
319
|
-
return line.length > 200 ? `${line.slice(0, 200)}\u2026` : line;
|
|
320
|
-
}
|
|
321
|
-
var Ok = class _Ok extends APIResponse {
|
|
322
|
-
static status = 200;
|
|
323
|
-
constructor(headers, data) {
|
|
324
|
-
super(_Ok.status, headers, data);
|
|
325
|
-
}
|
|
326
|
-
static create(status, headers, data) {
|
|
327
|
-
return new this(headers, data);
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
var Created = class _Created extends APIResponse {
|
|
331
|
-
static status = 201;
|
|
332
|
-
constructor(headers, data) {
|
|
333
|
-
super(_Created.status, headers, data);
|
|
334
|
-
}
|
|
335
|
-
static create(status, headers, data) {
|
|
336
|
-
return new this(headers, data);
|
|
337
|
-
}
|
|
338
|
-
};
|
|
339
|
-
var Accepted = class _Accepted extends APIResponse {
|
|
340
|
-
static status = 202;
|
|
341
|
-
constructor(headers, data) {
|
|
342
|
-
super(_Accepted.status, headers, data);
|
|
343
|
-
}
|
|
344
|
-
static create(status, headers, data) {
|
|
345
|
-
return new this(headers, data);
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
var NoContent = class _NoContent extends APIResponse {
|
|
349
|
-
static status = 204;
|
|
350
|
-
constructor(headers) {
|
|
351
|
-
super(_NoContent.status, headers, null);
|
|
352
|
-
}
|
|
353
|
-
static create(status, headers, _data) {
|
|
354
|
-
return new this(headers);
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
var BadRequest = class _BadRequest extends APIError {
|
|
358
|
-
static status = 400;
|
|
359
|
-
constructor(headers, data) {
|
|
360
|
-
super(_BadRequest.status, headers, data);
|
|
361
|
-
}
|
|
362
|
-
static create(status, headers, data) {
|
|
363
|
-
return new this(headers, data);
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
var Unauthorized = class _Unauthorized extends APIError {
|
|
367
|
-
static status = 401;
|
|
368
|
-
constructor(headers, data) {
|
|
369
|
-
super(_Unauthorized.status, headers, data);
|
|
370
|
-
}
|
|
371
|
-
static create(status, headers, data) {
|
|
372
|
-
return new this(headers, data);
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
var PaymentRequired = class _PaymentRequired extends APIError {
|
|
376
|
-
static status = 402;
|
|
377
|
-
constructor(headers, data) {
|
|
378
|
-
super(_PaymentRequired.status, headers, data);
|
|
379
|
-
}
|
|
380
|
-
static create(status, headers, data) {
|
|
381
|
-
return new this(headers, data);
|
|
382
|
-
}
|
|
383
|
-
};
|
|
384
|
-
var Forbidden = class _Forbidden extends APIError {
|
|
385
|
-
static status = 403;
|
|
386
|
-
constructor(headers, data) {
|
|
387
|
-
super(_Forbidden.status, headers, data);
|
|
388
|
-
}
|
|
389
|
-
static create(status, headers, data) {
|
|
390
|
-
return new this(headers, data);
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
var NotFound = class _NotFound extends APIError {
|
|
394
|
-
static status = 404;
|
|
395
|
-
constructor(headers, data) {
|
|
396
|
-
super(_NotFound.status, headers, data);
|
|
397
|
-
}
|
|
398
|
-
static create(status, headers, data) {
|
|
399
|
-
return new this(headers, data);
|
|
400
|
-
}
|
|
401
|
-
};
|
|
402
|
-
var MethodNotAllowed = class _MethodNotAllowed extends APIError {
|
|
403
|
-
static status = 405;
|
|
404
|
-
constructor(headers, data) {
|
|
405
|
-
super(_MethodNotAllowed.status, headers, data);
|
|
406
|
-
}
|
|
407
|
-
static create(status, headers, data) {
|
|
408
|
-
return new this(headers, data);
|
|
409
|
-
}
|
|
410
|
-
};
|
|
411
|
-
var NotAcceptable = class _NotAcceptable extends APIError {
|
|
412
|
-
static status = 406;
|
|
413
|
-
constructor(headers, data) {
|
|
414
|
-
super(_NotAcceptable.status, headers, data);
|
|
415
|
-
}
|
|
416
|
-
static create(status, headers, data) {
|
|
417
|
-
return new this(headers, data);
|
|
418
|
-
}
|
|
419
|
-
};
|
|
420
|
-
var Conflict = class _Conflict extends APIError {
|
|
421
|
-
static status = 409;
|
|
422
|
-
constructor(headers, data) {
|
|
423
|
-
super(_Conflict.status, headers, data);
|
|
424
|
-
}
|
|
425
|
-
static create(status, headers, data) {
|
|
426
|
-
return new this(headers, data);
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
var Gone = class _Gone extends APIError {
|
|
430
|
-
static status = 410;
|
|
431
|
-
constructor(headers, data) {
|
|
432
|
-
super(_Gone.status, headers, data);
|
|
433
|
-
}
|
|
434
|
-
static create(status, headers, data) {
|
|
435
|
-
return new this(headers, data);
|
|
436
|
-
}
|
|
437
|
-
};
|
|
438
|
-
var PreconditionFailed = class _PreconditionFailed extends APIError {
|
|
439
|
-
static status = 412;
|
|
440
|
-
constructor(headers, data) {
|
|
441
|
-
super(_PreconditionFailed.status, headers, data);
|
|
442
|
-
}
|
|
443
|
-
static create(status, headers, data) {
|
|
444
|
-
return new this(headers, data);
|
|
445
|
-
}
|
|
446
|
-
};
|
|
447
|
-
var UnprocessableEntity = class _UnprocessableEntity extends APIError {
|
|
448
|
-
static status = 422;
|
|
449
|
-
constructor(headers, data) {
|
|
450
|
-
super(_UnprocessableEntity.status, headers, data);
|
|
451
|
-
}
|
|
452
|
-
static create(status, headers, data) {
|
|
453
|
-
return new this(headers, data);
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
var TooManyRequests = class _TooManyRequests extends APIError {
|
|
457
|
-
static status = 429;
|
|
458
|
-
constructor(headers, data) {
|
|
459
|
-
super(_TooManyRequests.status, headers, data);
|
|
460
|
-
}
|
|
461
|
-
static create(status, headers, data) {
|
|
462
|
-
return new this(headers, data);
|
|
463
|
-
}
|
|
464
|
-
};
|
|
465
|
-
var PayloadTooLarge = class _PayloadTooLarge extends APIError {
|
|
466
|
-
static status = 413;
|
|
467
|
-
constructor(headers, data) {
|
|
468
|
-
super(_PayloadTooLarge.status, headers, data);
|
|
469
|
-
}
|
|
470
|
-
static create(status, headers, data) {
|
|
471
|
-
return new this(headers, data);
|
|
472
|
-
}
|
|
473
|
-
};
|
|
474
|
-
var UnsupportedMediaType = class _UnsupportedMediaType extends APIError {
|
|
475
|
-
static status = 415;
|
|
476
|
-
constructor(headers, data) {
|
|
477
|
-
super(_UnsupportedMediaType.status, headers, data);
|
|
478
|
-
}
|
|
479
|
-
static create(status, headers, data) {
|
|
480
|
-
return new this(headers, data);
|
|
481
|
-
}
|
|
482
|
-
};
|
|
483
|
-
var InternalServerError = class _InternalServerError extends APIError {
|
|
484
|
-
static status = 500;
|
|
485
|
-
constructor(headers, data) {
|
|
486
|
-
super(_InternalServerError.status, headers, data);
|
|
487
|
-
}
|
|
488
|
-
static create(status, headers, data) {
|
|
489
|
-
return new this(headers, data);
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
var NotImplemented = class _NotImplemented extends APIError {
|
|
493
|
-
static status = 501;
|
|
494
|
-
constructor(headers, data) {
|
|
495
|
-
super(_NotImplemented.status, headers, data);
|
|
496
|
-
}
|
|
497
|
-
static create(status, headers, data) {
|
|
498
|
-
return new this(headers, data);
|
|
499
|
-
}
|
|
500
|
-
};
|
|
501
|
-
var BadGateway = class _BadGateway extends APIError {
|
|
502
|
-
static status = 502;
|
|
503
|
-
constructor(headers, data) {
|
|
504
|
-
super(_BadGateway.status, headers, data);
|
|
505
|
-
}
|
|
506
|
-
static create(status, headers, data) {
|
|
507
|
-
return new this(headers, data);
|
|
508
|
-
}
|
|
509
|
-
};
|
|
510
|
-
var ServiceUnavailable = class _ServiceUnavailable extends APIError {
|
|
511
|
-
static status = 503;
|
|
512
|
-
constructor(headers, data) {
|
|
513
|
-
super(_ServiceUnavailable.status, headers, data);
|
|
514
|
-
}
|
|
515
|
-
static create(status, headers, data) {
|
|
516
|
-
return new this(headers, data);
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
var GatewayTimeout = class _GatewayTimeout extends APIError {
|
|
520
|
-
static status = 504;
|
|
521
|
-
constructor(headers, data) {
|
|
522
|
-
super(_GatewayTimeout.status, headers, data);
|
|
523
|
-
}
|
|
524
|
-
static create(status, headers, data) {
|
|
525
|
-
return new this(headers, data);
|
|
526
|
-
}
|
|
527
|
-
};
|
|
528
|
-
|
|
529
|
-
// packages/rpc/src/lib/http/sse.ts
|
|
530
|
-
function sse(response) {
|
|
531
|
-
const subscribers = /* @__PURE__ */ new Map();
|
|
532
|
-
let started = false;
|
|
533
|
-
let streamDone = false;
|
|
534
|
-
let streamFailure = null;
|
|
535
|
-
function dispatch(eventType, data) {
|
|
536
|
-
const sub = subscribers.get(eventType);
|
|
537
|
-
if (sub) {
|
|
538
|
-
sub.queue.push(data);
|
|
539
|
-
sub.resolve?.();
|
|
540
|
-
sub.resolve = null;
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
function endAll() {
|
|
544
|
-
streamDone = true;
|
|
545
|
-
for (const sub of subscribers.values()) {
|
|
546
|
-
sub.resolve?.();
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
function startReading() {
|
|
550
|
-
if (started) return;
|
|
551
|
-
started = true;
|
|
552
|
-
const decoder = new TextDecoder();
|
|
553
|
-
let buffer = "";
|
|
554
|
-
(async () => {
|
|
555
|
-
try {
|
|
556
|
-
for await (const value of response.body) {
|
|
557
|
-
buffer += decoder.decode(value, { stream: true });
|
|
558
|
-
const parts = buffer.split("\n\n");
|
|
559
|
-
buffer = parts.pop();
|
|
560
|
-
for (const part of parts) {
|
|
561
|
-
if (!part.trim()) continue;
|
|
562
|
-
let eventType = "message";
|
|
563
|
-
let data = "";
|
|
564
|
-
for (const line of part.split("\n")) {
|
|
565
|
-
if (line.startsWith("event:")) {
|
|
566
|
-
eventType = line.slice(6).trim();
|
|
567
|
-
} else if (line.startsWith("data:")) {
|
|
568
|
-
data += (data ? "\n" : "") + line.slice(5).trim();
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
if (data) dispatch(eventType, data);
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
} catch (error) {
|
|
575
|
-
streamFailure = { error };
|
|
576
|
-
}
|
|
577
|
-
endAll();
|
|
578
|
-
})();
|
|
579
|
-
}
|
|
580
|
-
return function listen(eventType) {
|
|
581
|
-
if (!subscribers.has(eventType)) {
|
|
582
|
-
subscribers.set(eventType, { queue: [], resolve: null });
|
|
583
|
-
}
|
|
584
|
-
const sub = subscribers.get(eventType);
|
|
585
|
-
startReading();
|
|
586
|
-
return {
|
|
587
|
-
[Symbol.asyncIterator]() {
|
|
588
|
-
return {
|
|
589
|
-
async next() {
|
|
590
|
-
while (sub.queue.length === 0) {
|
|
591
|
-
if (streamFailure) throw streamFailure.error;
|
|
592
|
-
if (streamDone) return { value: void 0, done: true };
|
|
593
|
-
const { promise, resolve } = Promise.withResolvers();
|
|
594
|
-
sub.resolve = resolve;
|
|
595
|
-
await promise;
|
|
596
|
-
}
|
|
597
|
-
return { value: sub.queue.shift(), done: false };
|
|
598
|
-
},
|
|
599
|
-
async return() {
|
|
600
|
-
return { value: void 0, done: true };
|
|
601
|
-
}
|
|
602
|
-
};
|
|
603
|
-
}
|
|
604
|
-
};
|
|
605
|
-
};
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
// packages/rpc/src/lib/http/dispatcher.ts
|
|
609
|
-
var fetchType = z.function({
|
|
610
|
-
input: [z.custom()],
|
|
611
|
-
output: z.custom()
|
|
612
|
-
}).optional();
|
|
613
|
-
async function parse2(outputs, response, mapper) {
|
|
614
|
-
let output = null;
|
|
615
|
-
let parser = buffered;
|
|
616
|
-
for (const outputType of outputs) {
|
|
617
|
-
const type = "parser" in outputType ? outputType.type : outputType;
|
|
618
|
-
if ((isTypeOf(type, APIResponse) || isTypeOf(type, APIError)) && response.status === type.status) {
|
|
619
|
-
output = type;
|
|
620
|
-
if ("parser" in outputType) {
|
|
621
|
-
parser = outputType.parser;
|
|
622
|
-
}
|
|
623
|
-
break;
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
if (response.ok) {
|
|
627
|
-
const data = await parser(response);
|
|
628
|
-
const mapped = mapper ? mapper(data) : data;
|
|
629
|
-
const apiresponse = (output || APIResponse).create(
|
|
630
|
-
response.status,
|
|
631
|
-
response.headers,
|
|
632
|
-
mapped
|
|
633
|
-
);
|
|
634
|
-
return apiresponse;
|
|
635
|
-
}
|
|
636
|
-
const failure = output && isTypeOf(output, APIError) ? output : APIError;
|
|
637
|
-
throw failure.create(
|
|
638
|
-
response.status,
|
|
639
|
-
response.headers,
|
|
640
|
-
await parser(response)
|
|
641
|
-
);
|
|
642
|
-
}
|
|
643
|
-
function isTypeOf(instance, baseType) {
|
|
644
|
-
if (instance === baseType) {
|
|
645
|
-
return true;
|
|
646
|
-
}
|
|
647
|
-
const prototype = Object.getPrototypeOf(instance);
|
|
648
|
-
if (prototype === null) {
|
|
649
|
-
return false;
|
|
650
|
-
}
|
|
651
|
-
return isTypeOf(prototype, baseType);
|
|
652
|
-
}
|
|
653
|
-
var Dispatcher = class {
|
|
654
|
-
#interceptors = [];
|
|
655
|
-
#fetch;
|
|
656
|
-
constructor(interceptors, fetch2) {
|
|
657
|
-
this.#interceptors = interceptors;
|
|
658
|
-
this.#fetch = fetch2;
|
|
659
|
-
}
|
|
660
|
-
async send(config, outputs, signal, mapper) {
|
|
661
|
-
for (const interceptor of this.#interceptors) {
|
|
662
|
-
if (interceptor.before) {
|
|
663
|
-
config = await interceptor.before(config);
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
const init = signal === void 0 ? config.init : { ...config.init, signal };
|
|
667
|
-
let response = await (this.#fetch ?? fetch)(new Request(config.url, init));
|
|
668
|
-
for (let i = this.#interceptors.length - 1; i >= 0; i--) {
|
|
669
|
-
const interceptor = this.#interceptors[i];
|
|
670
|
-
if (interceptor.after) {
|
|
671
|
-
response = await interceptor.after(response.clone());
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
if (mapper) {
|
|
675
|
-
return await parse2(outputs, response, mapper);
|
|
676
|
-
}
|
|
677
|
-
return await parse2(outputs, response);
|
|
678
|
-
}
|
|
679
|
-
};
|
|
680
|
-
|
|
681
|
-
// packages/rpc/src/lib/http/parser.ts
|
|
682
|
-
import { z as z2 } from "zod";
|
|
683
|
-
var ParseError = class extends Error {
|
|
684
|
-
data;
|
|
685
|
-
constructor(data) {
|
|
686
|
-
super("Validation failed");
|
|
687
|
-
this.name = "ParseError";
|
|
688
|
-
this.data = data;
|
|
689
|
-
}
|
|
690
|
-
};
|
|
691
|
-
function parseInput(schema, input) {
|
|
692
|
-
const result = schema.safeParse(input);
|
|
693
|
-
if (!result.success) {
|
|
694
|
-
const error = z2.flattenError(result.error, (issue) => issue);
|
|
695
|
-
throw new ParseError(error);
|
|
696
|
-
}
|
|
697
|
-
return result.data;
|
|
698
|
-
}
|
|
699
|
-
|
|
700
1
|
// packages/rpc/src/lib/rpc.ts
|
|
701
2
|
import { jsonSchema, tool } from "ai";
|
|
702
|
-
import { z as
|
|
3
|
+
import { z as z2 } from "zod";
|
|
4
|
+
import * as http from "@sdk-it/runtime";
|
|
5
|
+
import {
|
|
6
|
+
Dispatcher,
|
|
7
|
+
binary,
|
|
8
|
+
bodyless,
|
|
9
|
+
buffered,
|
|
10
|
+
chunked,
|
|
11
|
+
createHeadersInterceptor,
|
|
12
|
+
empty,
|
|
13
|
+
fetchType,
|
|
14
|
+
formdata,
|
|
15
|
+
json,
|
|
16
|
+
parseInput,
|
|
17
|
+
sse,
|
|
18
|
+
text,
|
|
19
|
+
toRequest,
|
|
20
|
+
urlencoded
|
|
21
|
+
} from "@sdk-it/runtime";
|
|
703
22
|
import {
|
|
23
|
+
carriesBinary,
|
|
704
24
|
forEachOperation,
|
|
25
|
+
inputEndpoint,
|
|
26
|
+
inputName,
|
|
705
27
|
loadSpec,
|
|
28
|
+
operationInputs,
|
|
29
|
+
statusRange,
|
|
706
30
|
toIR
|
|
707
31
|
} from "@sdk-it/spec";
|
|
708
|
-
import {
|
|
709
|
-
buildInput,
|
|
710
|
-
operationSchema
|
|
711
|
-
} from "@sdk-it/typescript";
|
|
712
32
|
|
|
713
33
|
// packages/rpc/src/lib/zod.ts
|
|
714
|
-
import { z
|
|
34
|
+
import { z } from "zod";
|
|
715
35
|
import { followRef, isEmpty, isRef } from "@sdk-it/core";
|
|
716
|
-
|
|
36
|
+
import { isRecursiveRef } from "@sdk-it/spec";
|
|
37
|
+
var isoTime = z.string().regex(
|
|
717
38
|
/^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d(\.\d+)?)?(Z|[+-]([01]\d|2[0-3]):[0-5]\d)?$/
|
|
718
39
|
);
|
|
719
40
|
var RuntimeZodConverter = class {
|
|
@@ -729,13 +50,13 @@ var RuntimeZodConverter = class {
|
|
|
729
50
|
const isRequired = required.includes(key);
|
|
730
51
|
shape[key] = this.handle(propSchema, isRequired);
|
|
731
52
|
}
|
|
732
|
-
let result =
|
|
53
|
+
let result = z.object(shape);
|
|
733
54
|
if (schema.additionalProperties) {
|
|
734
55
|
if (typeof schema.additionalProperties === "object") {
|
|
735
56
|
const addPropSchema = this.handle(schema.additionalProperties, true);
|
|
736
57
|
result = result.catchall(addPropSchema);
|
|
737
58
|
} else if (schema.additionalProperties === true) {
|
|
738
|
-
result = result.catchall(
|
|
59
|
+
result = result.catchall(z.unknown());
|
|
739
60
|
}
|
|
740
61
|
}
|
|
741
62
|
return result;
|
|
@@ -743,39 +64,47 @@ var RuntimeZodConverter = class {
|
|
|
743
64
|
#array(schema) {
|
|
744
65
|
const { items } = schema;
|
|
745
66
|
if (!items) {
|
|
746
|
-
let result2 =
|
|
67
|
+
let result2 = z.array(z.unknown());
|
|
747
68
|
if (schema.default !== void 0) {
|
|
748
|
-
result2 =
|
|
69
|
+
result2 = z.array(z.unknown()).default(schema.default);
|
|
749
70
|
}
|
|
750
71
|
return result2;
|
|
751
72
|
}
|
|
752
73
|
if (Array.isArray(items)) {
|
|
753
|
-
const
|
|
754
|
-
return
|
|
74
|
+
const [first, ...rest] = items.map((sub) => this.handle(sub, true));
|
|
75
|
+
return first ? z.tuple([first, ...rest]) : z.tuple([]);
|
|
755
76
|
}
|
|
756
77
|
const itemsSchema = this.handle(items, true);
|
|
757
|
-
let result =
|
|
78
|
+
let result = z.array(itemsSchema);
|
|
758
79
|
if (schema.default !== void 0) {
|
|
759
|
-
result =
|
|
80
|
+
result = z.array(itemsSchema).default(schema.default);
|
|
760
81
|
}
|
|
761
82
|
return result;
|
|
762
83
|
}
|
|
84
|
+
/** One deferred schema per recursive component, shared by every reference to it. */
|
|
85
|
+
#recursive = /* @__PURE__ */ new Map();
|
|
763
86
|
#ref($ref) {
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
87
|
+
if (!isRecursiveRef(this.#spec, $ref)) {
|
|
88
|
+
return this.handle(followRef(this.#spec, $ref), true);
|
|
89
|
+
}
|
|
90
|
+
return this.#recursive.getOrInsertComputed($ref, () => {
|
|
91
|
+
let resolved;
|
|
92
|
+
return z.lazy(
|
|
93
|
+
() => resolved ??= this.handle(followRef(this.#spec, $ref), true)
|
|
94
|
+
);
|
|
95
|
+
});
|
|
767
96
|
}
|
|
768
97
|
#toIntersection(schemas) {
|
|
769
98
|
const [left, ...right] = schemas;
|
|
770
99
|
if (!right.length) {
|
|
771
100
|
return left;
|
|
772
101
|
}
|
|
773
|
-
return
|
|
102
|
+
return z.intersection(left, this.#toIntersection(right));
|
|
774
103
|
}
|
|
775
104
|
allOf(schemas) {
|
|
776
105
|
const allOfSchemas = schemas.map((sub) => this.handle(sub, true));
|
|
777
106
|
if (allOfSchemas.length === 0) {
|
|
778
|
-
return
|
|
107
|
+
return z.unknown();
|
|
779
108
|
}
|
|
780
109
|
if (allOfSchemas.length === 1) {
|
|
781
110
|
return allOfSchemas[0];
|
|
@@ -787,75 +116,76 @@ var RuntimeZodConverter = class {
|
|
|
787
116
|
if (anyOfSchemas.length === 1) {
|
|
788
117
|
return anyOfSchemas[0];
|
|
789
118
|
}
|
|
790
|
-
return
|
|
119
|
+
return z.union(anyOfSchemas);
|
|
791
120
|
}
|
|
792
121
|
oneOf(schemas) {
|
|
793
122
|
const oneOfSchemas = schemas.map((sub) => this.handle(sub, true));
|
|
794
123
|
if (oneOfSchemas.length === 1) {
|
|
795
124
|
return oneOfSchemas[0];
|
|
796
125
|
}
|
|
797
|
-
return
|
|
126
|
+
return z.xor(oneOfSchemas);
|
|
798
127
|
}
|
|
799
128
|
enum(type, values) {
|
|
800
129
|
if (values.length === 1) {
|
|
801
|
-
return
|
|
130
|
+
return z.literal(values[0]);
|
|
802
131
|
}
|
|
803
132
|
if (values.every((value) => typeof value === "string")) {
|
|
804
|
-
return
|
|
133
|
+
return z.enum(values);
|
|
805
134
|
}
|
|
806
|
-
return
|
|
135
|
+
return z.literal(values);
|
|
807
136
|
}
|
|
808
137
|
/**
|
|
809
138
|
* Handle a `string` schema with possible format keywords (JSON Schema).
|
|
810
139
|
*/
|
|
811
140
|
string(schema) {
|
|
812
141
|
if (schema.contentEncoding === "binary") {
|
|
813
|
-
return
|
|
142
|
+
return z.custom();
|
|
814
143
|
}
|
|
815
144
|
const coerced = schema["x-zod-type"] === "coerce-string";
|
|
816
|
-
const base = coerced ?
|
|
817
|
-
const
|
|
145
|
+
const base = coerced ? z.coerce.string() : z.string();
|
|
146
|
+
const limits = stringLimits(schema);
|
|
147
|
+
const withFormat = (format) => coerced ? base.pipe(format.check(...limits)) : format.check(...limits);
|
|
818
148
|
switch (schema.format) {
|
|
819
149
|
case "date-time":
|
|
820
150
|
case "datetime":
|
|
821
151
|
if (schema["x-zod-type"] === "coerce-date") {
|
|
822
|
-
return
|
|
152
|
+
return z.coerce.date();
|
|
823
153
|
} else if (schema["x-zod-type"] === "date") {
|
|
824
|
-
return
|
|
154
|
+
return z.date();
|
|
825
155
|
} else {
|
|
826
|
-
return withFormat(
|
|
156
|
+
return withFormat(z.iso.datetime({ offset: true }));
|
|
827
157
|
}
|
|
828
158
|
case "date":
|
|
829
|
-
return withFormat(
|
|
159
|
+
return withFormat(z.iso.date());
|
|
830
160
|
case "time":
|
|
831
161
|
return withFormat(isoTime);
|
|
832
162
|
case "email":
|
|
833
|
-
return withFormat(
|
|
163
|
+
return withFormat(z.email());
|
|
834
164
|
case "uuid":
|
|
835
|
-
return withFormat(
|
|
165
|
+
return withFormat(z.guid());
|
|
836
166
|
case "url":
|
|
837
167
|
case "uri":
|
|
838
|
-
return withFormat(
|
|
168
|
+
return withFormat(z.url());
|
|
839
169
|
case "ipv4":
|
|
840
|
-
return withFormat(
|
|
170
|
+
return withFormat(z.ipv4());
|
|
841
171
|
case "ipv6":
|
|
842
|
-
return withFormat(
|
|
172
|
+
return withFormat(z.ipv6());
|
|
843
173
|
case "cidrv4":
|
|
844
|
-
return withFormat(
|
|
174
|
+
return withFormat(z.cidrv4());
|
|
845
175
|
case "cidrv6":
|
|
846
|
-
return withFormat(
|
|
176
|
+
return withFormat(z.cidrv6());
|
|
847
177
|
case "byte":
|
|
848
178
|
case "binary":
|
|
849
|
-
return
|
|
179
|
+
return z.custom();
|
|
850
180
|
default:
|
|
851
|
-
return base;
|
|
181
|
+
return base.check(...limits);
|
|
852
182
|
}
|
|
853
183
|
}
|
|
854
184
|
/**
|
|
855
185
|
* Handle number/integer constraints from OpenAPI/JSON Schema.
|
|
856
186
|
*/
|
|
857
187
|
#number(schema) {
|
|
858
|
-
let base = schema["x-zod-type"] === "coerce-number" ?
|
|
188
|
+
let base = schema["x-zod-type"] === "coerce-number" ? z.coerce.number() : z.number();
|
|
859
189
|
if (schema.type === "integer" || schema.format === "int32") {
|
|
860
190
|
base = base.int();
|
|
861
191
|
}
|
|
@@ -894,7 +224,7 @@ var RuntimeZodConverter = class {
|
|
|
894
224
|
base = this.#number(schema);
|
|
895
225
|
break;
|
|
896
226
|
case "boolean":
|
|
897
|
-
base = schema["x-zod-type"] === "coerce-boolean" ?
|
|
227
|
+
base = schema["x-zod-type"] === "coerce-boolean" ? z.union([z.boolean(), z.stringbool()]) : z.boolean();
|
|
898
228
|
break;
|
|
899
229
|
case "object":
|
|
900
230
|
base = this.#object(schema);
|
|
@@ -903,10 +233,10 @@ var RuntimeZodConverter = class {
|
|
|
903
233
|
base = this.#array(schema);
|
|
904
234
|
break;
|
|
905
235
|
case "null":
|
|
906
|
-
base =
|
|
236
|
+
base = z.null();
|
|
907
237
|
break;
|
|
908
238
|
default:
|
|
909
|
-
base =
|
|
239
|
+
base = z.unknown();
|
|
910
240
|
break;
|
|
911
241
|
}
|
|
912
242
|
if (nullable) {
|
|
@@ -932,9 +262,9 @@ var RuntimeZodConverter = class {
|
|
|
932
262
|
}
|
|
933
263
|
return result2;
|
|
934
264
|
}
|
|
935
|
-
let result
|
|
265
|
+
let result;
|
|
936
266
|
if (schema.not && isEmpty(schema.not)) {
|
|
937
|
-
result = required ?
|
|
267
|
+
result = required ? z.never() : z.never().optional();
|
|
938
268
|
} else if (schema.allOf && Array.isArray(schema.allOf)) {
|
|
939
269
|
result = this.allOf(schema.allOf);
|
|
940
270
|
if (!required) {
|
|
@@ -966,13 +296,13 @@ var RuntimeZodConverter = class {
|
|
|
966
296
|
types.push("null");
|
|
967
297
|
}
|
|
968
298
|
if (!types.length) {
|
|
969
|
-
result = required ?
|
|
299
|
+
result = required ? z.unknown() : z.unknown().optional();
|
|
970
300
|
} else if (types.length > 1) {
|
|
971
301
|
const realTypes = types.filter((t) => t !== "null");
|
|
972
302
|
if (realTypes.length === 1 && types.includes("null")) {
|
|
973
303
|
result = this.normal(realTypes[0], schema, required, true);
|
|
974
304
|
} else {
|
|
975
|
-
result =
|
|
305
|
+
result = z.union(types.map((t) => this.normal(t, schema, true)));
|
|
976
306
|
if (!required) {
|
|
977
307
|
result = result.optional();
|
|
978
308
|
}
|
|
@@ -991,41 +321,74 @@ function schemaToZod(schema, spec, options) {
|
|
|
991
321
|
const converter = new RuntimeZodConverter(spec);
|
|
992
322
|
return converter.handle(schema, options?.required ?? false);
|
|
993
323
|
}
|
|
324
|
+
function stringLimits(schema) {
|
|
325
|
+
const limits = [];
|
|
326
|
+
if (typeof schema.minLength === "number") {
|
|
327
|
+
limits.push(z.minLength(schema.minLength));
|
|
328
|
+
}
|
|
329
|
+
if (typeof schema.maxLength === "number") {
|
|
330
|
+
limits.push(z.maxLength(schema.maxLength));
|
|
331
|
+
}
|
|
332
|
+
if (typeof schema.pattern === "string") {
|
|
333
|
+
limits.push(z.regex(new RegExp(schema.pattern)));
|
|
334
|
+
}
|
|
335
|
+
return limits;
|
|
336
|
+
}
|
|
994
337
|
|
|
995
338
|
// packages/rpc/src/lib/rpc.ts
|
|
996
339
|
var bodyParser = {
|
|
997
|
-
json: buffered,
|
|
998
|
-
text: buffered,
|
|
999
|
-
binary: buffered,
|
|
340
|
+
json: buffered("json"),
|
|
341
|
+
text: buffered("text"),
|
|
342
|
+
binary: buffered("binary"),
|
|
1000
343
|
empty: bodyless,
|
|
1001
344
|
stream: chunked,
|
|
1002
345
|
sse
|
|
1003
346
|
};
|
|
1004
|
-
var statusClasses = Object.values(
|
|
1005
|
-
(value) => typeof value.status === "number"
|
|
347
|
+
var statusClasses = Object.values(http).filter(
|
|
348
|
+
(value) => "status" in value && typeof value.status === "number"
|
|
1006
349
|
);
|
|
1007
350
|
function toOutputs(operation) {
|
|
1008
|
-
return Object.entries(operation.responses).flatMap(
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
351
|
+
return Object.entries(operation.responses).flatMap(
|
|
352
|
+
([status, response]) => {
|
|
353
|
+
const parser = bodyParser[response["x-body"]];
|
|
354
|
+
const range = statusRange(status);
|
|
355
|
+
if (range) {
|
|
356
|
+
const type2 = range === "2XX" ? http.APIResponse : http.APIError;
|
|
357
|
+
return [{ type: type2, parser, range }];
|
|
358
|
+
}
|
|
359
|
+
const type = statusClasses.find((it) => it.status === Number(status));
|
|
360
|
+
return type ? [{ type, parser }] : [];
|
|
361
|
+
}
|
|
362
|
+
);
|
|
1012
363
|
}
|
|
1013
|
-
var callable =
|
|
364
|
+
var callable = z2.custom(
|
|
1014
365
|
(value) => typeof value === "function"
|
|
1015
366
|
);
|
|
1016
|
-
var baseUrlSchema =
|
|
367
|
+
var baseUrlSchema = z2.union([z2.string(), callable]).transform(async (baseUrl, ctx) => {
|
|
1017
368
|
const value = typeof baseUrl === "function" ? await baseUrl() : baseUrl;
|
|
1018
369
|
if (typeof value !== "string") {
|
|
1019
370
|
ctx.addIssue({
|
|
1020
371
|
code: "custom",
|
|
1021
372
|
message: "baseUrl must resolve to a string"
|
|
1022
373
|
});
|
|
1023
|
-
return
|
|
374
|
+
return z2.NEVER;
|
|
1024
375
|
}
|
|
1025
|
-
|
|
376
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(value)) {
|
|
377
|
+
return value;
|
|
378
|
+
}
|
|
379
|
+
const page = Reflect.get(
|
|
380
|
+
globalThis,
|
|
381
|
+
"location"
|
|
382
|
+
);
|
|
383
|
+
if (page === void 0) {
|
|
384
|
+
throw new TypeError(
|
|
385
|
+
`baseUrl "${value}" is relative, and there is no page location to resolve it against.`
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
return new URL(value, page.href).href;
|
|
1026
389
|
});
|
|
1027
|
-
var optionsSchema =
|
|
1028
|
-
token:
|
|
390
|
+
var optionsSchema = z2.object({
|
|
391
|
+
token: z2.union([z2.string(), callable]).optional().transform(async (token, ctx) => {
|
|
1029
392
|
if (!token) return void 0;
|
|
1030
393
|
const value = typeof token === "function" ? await token() : token;
|
|
1031
394
|
if (typeof value !== "string") {
|
|
@@ -1033,43 +396,14 @@ var optionsSchema = z4.object({
|
|
|
1033
396
|
code: "custom",
|
|
1034
397
|
message: "token must resolve to a string"
|
|
1035
398
|
});
|
|
1036
|
-
return
|
|
399
|
+
return z2.NEVER;
|
|
1037
400
|
}
|
|
1038
401
|
return `Bearer ${value}`;
|
|
1039
402
|
}),
|
|
1040
403
|
fetch: fetchType,
|
|
1041
404
|
baseUrl: baseUrlSchema,
|
|
1042
|
-
headers:
|
|
405
|
+
headers: z2.record(z2.string(), z2.string()).optional()
|
|
1043
406
|
});
|
|
1044
|
-
function inputToPath(operation, inputs) {
|
|
1045
|
-
const inputHeaders = [];
|
|
1046
|
-
const inputQuery = [];
|
|
1047
|
-
const inputBody = [];
|
|
1048
|
-
const inputParams = [];
|
|
1049
|
-
for (const [name, prop] of Object.entries(inputs)) {
|
|
1050
|
-
if (prop.in === "headers" || prop.in === "header") {
|
|
1051
|
-
inputHeaders.push(name);
|
|
1052
|
-
} else if (prop.in === "query") {
|
|
1053
|
-
inputQuery.push(name);
|
|
1054
|
-
} else if (prop.in === "body") {
|
|
1055
|
-
inputBody.push(name);
|
|
1056
|
-
} else if (prop.in === "path") {
|
|
1057
|
-
inputParams.push(name);
|
|
1058
|
-
} else {
|
|
1059
|
-
throw new Error(
|
|
1060
|
-
`Unknown source ${prop.in} in ${name} ${JSON.stringify(
|
|
1061
|
-
prop
|
|
1062
|
-
)} in ${operation.operationId}`
|
|
1063
|
-
);
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
return {
|
|
1067
|
-
inputHeaders,
|
|
1068
|
-
inputQuery,
|
|
1069
|
-
inputBody,
|
|
1070
|
-
inputParams
|
|
1071
|
-
};
|
|
1072
|
-
}
|
|
1073
407
|
var Client = class {
|
|
1074
408
|
options;
|
|
1075
409
|
schemas;
|
|
@@ -1084,12 +418,12 @@ var Client = class {
|
|
|
1084
418
|
const clientOptions = await optionsSchema.parseAsync(this.options);
|
|
1085
419
|
const result = await route.dispatch(parsedInput, {
|
|
1086
420
|
fetch: clientOptions.fetch,
|
|
421
|
+
baseUrl: clientOptions.baseUrl,
|
|
1087
422
|
interceptors: [
|
|
1088
423
|
createHeadersInterceptor(
|
|
1089
424
|
await this.#defaultHeaders(),
|
|
1090
425
|
options?.headers ?? {}
|
|
1091
|
-
)
|
|
1092
|
-
createBaseUrlInterceptor(clientOptions.baseUrl)
|
|
426
|
+
)
|
|
1093
427
|
],
|
|
1094
428
|
signal: options?.signal
|
|
1095
429
|
});
|
|
@@ -1112,36 +446,53 @@ var Client = class {
|
|
|
1112
446
|
};
|
|
1113
447
|
}
|
|
1114
448
|
};
|
|
449
|
+
var httpMethods = {
|
|
450
|
+
get: "GET",
|
|
451
|
+
post: "POST",
|
|
452
|
+
put: "PUT",
|
|
453
|
+
patch: "PATCH",
|
|
454
|
+
delete: "DELETE",
|
|
455
|
+
trace: "TRACE",
|
|
456
|
+
head: "HEAD"
|
|
457
|
+
};
|
|
458
|
+
var serializers = {
|
|
459
|
+
json,
|
|
460
|
+
urlencoded,
|
|
461
|
+
formdata,
|
|
462
|
+
text,
|
|
463
|
+
binary,
|
|
464
|
+
empty
|
|
465
|
+
};
|
|
1115
466
|
function createRpc(ir, options = {}) {
|
|
1116
467
|
const schemas = {};
|
|
1117
468
|
forEachOperation(ir, (entry, operation) => {
|
|
1118
|
-
const
|
|
1119
|
-
const
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
469
|
+
const method = httpMethods[entry.method];
|
|
470
|
+
for (const sent of operationInputs(ir, operation)) {
|
|
471
|
+
schemas[inputEndpoint(`${method} ${entry.path}`, sent)] = {
|
|
472
|
+
operationId: operation.operationId,
|
|
473
|
+
output: toOutputs(operation),
|
|
474
|
+
schema: schemaToZod(sent.schema, ir, { required: true }),
|
|
475
|
+
async dispatch(input, options2) {
|
|
476
|
+
const dispatcher = new Dispatcher(
|
|
477
|
+
options2.interceptors,
|
|
478
|
+
options2.fetch
|
|
479
|
+
);
|
|
480
|
+
const request = toRequest(
|
|
481
|
+
method,
|
|
482
|
+
entry.path,
|
|
483
|
+
serializers[sent.encoding](input, {
|
|
484
|
+
contentType: sent.contentType,
|
|
485
|
+
inputHeaders: sent.header,
|
|
486
|
+
inputQuery: sent.query,
|
|
487
|
+
inputBody: sent.body,
|
|
488
|
+
inputParams: sent.path
|
|
489
|
+
}),
|
|
490
|
+
options2.baseUrl
|
|
491
|
+
);
|
|
492
|
+
return dispatcher.send(request, this.output, options2.signal);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
}
|
|
1145
496
|
});
|
|
1146
497
|
const baseUrl = options.baseUrl ?? ir.servers.at(0)?.url;
|
|
1147
498
|
if (baseUrl === void 0) {
|
|
@@ -1150,7 +501,7 @@ function createRpc(ir, options = {}) {
|
|
|
1150
501
|
return new Client({ ...options, baseUrl }, schemas);
|
|
1151
502
|
}
|
|
1152
503
|
async function rpc(openapi, options) {
|
|
1153
|
-
const spec = await loadSpec(openapi);
|
|
504
|
+
const { spec } = await loadSpec(openapi);
|
|
1154
505
|
const ir = await toIR({
|
|
1155
506
|
spec,
|
|
1156
507
|
responses: { flattenErrorResponses: true }
|
|
@@ -1158,7 +509,7 @@ async function rpc(openapi, options) {
|
|
|
1158
509
|
return createRpc(ir, options);
|
|
1159
510
|
}
|
|
1160
511
|
async function toAgents(openapi, options) {
|
|
1161
|
-
const spec = await loadSpec(openapi);
|
|
512
|
+
const { spec } = await loadSpec(openapi);
|
|
1162
513
|
const ir = await toIR({
|
|
1163
514
|
spec,
|
|
1164
515
|
responses: { flattenErrorResponses: true }
|
|
@@ -1168,16 +519,9 @@ async function toAgents(openapi, options) {
|
|
|
1168
519
|
forEachOperation(ir, (entry, operation) => {
|
|
1169
520
|
const tagDef = ir.tags.find((tag) => tag.name === entry.tag);
|
|
1170
521
|
if (!tagDef) {
|
|
1171
|
-
|
|
1172
|
-
return;
|
|
522
|
+
throw new Error(`The IR defines no tag ${entry.tag}`);
|
|
1173
523
|
}
|
|
1174
|
-
groups[entry.tag] ??= {
|
|
1175
|
-
tools: {},
|
|
1176
|
-
instructions: "",
|
|
1177
|
-
displayName: "",
|
|
1178
|
-
name: "",
|
|
1179
|
-
handoffDescription: ""
|
|
1180
|
-
};
|
|
524
|
+
groups[entry.tag] ??= { tools: {}, definition: tagDef };
|
|
1181
525
|
const endpoint = `${entry.method.toUpperCase()} ${entry.path}`;
|
|
1182
526
|
const toolInfo = operation["x-tool"];
|
|
1183
527
|
let includeTool = true;
|
|
@@ -1185,37 +529,32 @@ async function toAgents(openapi, options) {
|
|
|
1185
529
|
includeTool = !!toolInfo;
|
|
1186
530
|
}
|
|
1187
531
|
if (includeTool) {
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
execute: async (input) => {
|
|
1193
|
-
const response = await client.request(endpoint, input);
|
|
1194
|
-
return JSON.stringify(response);
|
|
532
|
+
const name = toolInfo?.name || operation["x-fn-name"];
|
|
533
|
+
for (const sent of operationInputs(ir, operation)) {
|
|
534
|
+
if (carriesBinary(ir, sent)) {
|
|
535
|
+
continue;
|
|
1195
536
|
}
|
|
1196
|
-
|
|
537
|
+
const key = inputEndpoint(endpoint, sent);
|
|
538
|
+
groups[entry.tag].tools[inputName(name, sent)] = tool({
|
|
539
|
+
type: "function",
|
|
540
|
+
description: toolInfo?.description || operation.description || operation.summary,
|
|
541
|
+
inputSchema: toToolSchema(client.schemas[key].schema),
|
|
542
|
+
execute: async (input) => {
|
|
543
|
+
const response = await client.request(key, input);
|
|
544
|
+
return JSON.stringify(response);
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
}
|
|
1197
548
|
}
|
|
1198
|
-
groups[entry.tag].handoffDescription = tagDef["x-handoff-description"];
|
|
1199
|
-
groups[entry.tag].instructions = tagDef["x-instructions"];
|
|
1200
|
-
groups[entry.tag].name = tagDef.name;
|
|
1201
|
-
groups[entry.tag].displayName = tagDef["x-name"];
|
|
1202
549
|
});
|
|
1203
550
|
const agents = {};
|
|
1204
|
-
for (const [
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
agents[agentName] = {
|
|
1209
|
-
name: displayName,
|
|
1210
|
-
instructions,
|
|
1211
|
-
tools
|
|
1212
|
-
};
|
|
551
|
+
for (const [agentName, { tools, definition }] of Object.entries(groups)) {
|
|
552
|
+
const id = definition["x-name"] ?? definition.summary ?? definition.name;
|
|
553
|
+
const instructions = definition["x-instructions"] ?? definition.description;
|
|
554
|
+
agents[agentName] = instructions === void 0 ? { id, tools } : { id, instructions, tools };
|
|
1213
555
|
}
|
|
1214
556
|
return agents;
|
|
1215
557
|
}
|
|
1216
|
-
function defaultSerializer(ct) {
|
|
1217
|
-
throw new Error(`Unsupported content type: ${ct}`);
|
|
1218
|
-
}
|
|
1219
558
|
var TOOL_COERCE_MARKER = "x-sdkit-tool-coerce";
|
|
1220
559
|
function coerceToolValue(value, schema) {
|
|
1221
560
|
if (!schema || typeof schema !== "object" || value === null || value === void 0) {
|
|
@@ -1256,7 +595,7 @@ function coerceToolValue(value, schema) {
|
|
|
1256
595
|
return value;
|
|
1257
596
|
}
|
|
1258
597
|
function toToolSchema(schema) {
|
|
1259
|
-
const marked =
|
|
598
|
+
const marked = z2.toJSONSchema(schema, {
|
|
1260
599
|
target: "draft-7",
|
|
1261
600
|
io: "input",
|
|
1262
601
|
unrepresentable: "any",
|
|
@@ -1285,50 +624,11 @@ function toToolSchema(schema) {
|
|
|
1285
624
|
});
|
|
1286
625
|
}
|
|
1287
626
|
export {
|
|
1288
|
-
APIError,
|
|
1289
|
-
APIResponse,
|
|
1290
|
-
Accepted,
|
|
1291
|
-
BadGateway,
|
|
1292
|
-
BadRequest,
|
|
1293
627
|
Client,
|
|
1294
|
-
Conflict,
|
|
1295
|
-
Created,
|
|
1296
|
-
Dispatcher,
|
|
1297
|
-
Forbidden,
|
|
1298
|
-
GatewayTimeout,
|
|
1299
|
-
Gone,
|
|
1300
|
-
InternalServerError,
|
|
1301
|
-
MethodNotAllowed,
|
|
1302
|
-
NoContent,
|
|
1303
|
-
NotAcceptable,
|
|
1304
|
-
NotFound,
|
|
1305
|
-
NotImplemented,
|
|
1306
|
-
Ok,
|
|
1307
|
-
ParseError,
|
|
1308
|
-
PayloadTooLarge,
|
|
1309
|
-
PaymentRequired,
|
|
1310
|
-
PreconditionFailed,
|
|
1311
628
|
RuntimeZodConverter,
|
|
1312
|
-
ServiceUnavailable,
|
|
1313
|
-
TooManyRequests,
|
|
1314
|
-
Unauthorized,
|
|
1315
|
-
UnprocessableEntity,
|
|
1316
|
-
UnsupportedMediaType,
|
|
1317
|
-
createBaseUrlInterceptor,
|
|
1318
|
-
createHeadersInterceptor,
|
|
1319
629
|
createRpc,
|
|
1320
|
-
empty,
|
|
1321
|
-
fetchType,
|
|
1322
|
-
formdata,
|
|
1323
|
-
inputToPath,
|
|
1324
|
-
isTypeOf,
|
|
1325
|
-
json,
|
|
1326
|
-
parse2 as parse,
|
|
1327
|
-
parseInput,
|
|
1328
630
|
rpc,
|
|
1329
631
|
schemaToZod,
|
|
1330
|
-
toAgents
|
|
1331
|
-
toRequest,
|
|
1332
|
-
urlencoded
|
|
632
|
+
toAgents
|
|
1333
633
|
};
|
|
1334
634
|
//# sourceMappingURL=index.js.map
|