celitech-sdk 1.3.13 → 1.3.23
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 +4 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.js +182 -42
- package/dist/index.mjs +182 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
# Celitech TypeScript SDK 1.3.
|
|
1
|
+
# Celitech TypeScript SDK 1.3.23
|
|
2
2
|
|
|
3
3
|
Welcome to the Celitech SDK documentation. This guide will help you get started with integrating and using the Celitech SDK in your project.
|
|
4
4
|
|
|
5
|
+
[](https://liblab.com/?utm_source=readme)
|
|
6
|
+
|
|
5
7
|
## Versions
|
|
6
8
|
|
|
7
9
|
- API version: `1.1.0`
|
|
8
|
-
- SDK version: `1.3.
|
|
10
|
+
- SDK version: `1.3.23`
|
|
9
11
|
|
|
10
12
|
## About the API
|
|
11
13
|
|
package/dist/index.d.ts
CHANGED
|
@@ -155,7 +155,8 @@ declare enum ContentType {
|
|
|
155
155
|
Binary = "binary",
|
|
156
156
|
FormUrlEncoded = "form",
|
|
157
157
|
Text = "text",
|
|
158
|
-
MultipartFormData = "multipartFormData"
|
|
158
|
+
MultipartFormData = "multipartFormData",
|
|
159
|
+
EventStream = "eventStream"
|
|
159
160
|
}
|
|
160
161
|
interface RequestConfig {
|
|
161
162
|
retry?: RetryOptions;
|
|
@@ -187,6 +188,7 @@ declare class HttpClient {
|
|
|
187
188
|
private readonly requestHandlerChain;
|
|
188
189
|
constructor(config: SdkConfig, hook?: CustomHook);
|
|
189
190
|
call<T>(request: Request<T>): Promise<HttpResponse<T>>;
|
|
191
|
+
stream<T>(request: Request<T>): AsyncGenerator<HttpResponse<T>>;
|
|
190
192
|
callPaginated<FullResponse, Page>(request: Request<FullResponse, Page>): Promise<HttpResponse<Page>>;
|
|
191
193
|
setBaseUrl(url: string): void;
|
|
192
194
|
setConfig(config: SdkConfig): void;
|
package/dist/index.js
CHANGED
|
@@ -58,6 +58,12 @@ var RequestHandlerChain = class {
|
|
|
58
58
|
}
|
|
59
59
|
return this.handlers[0].handle(request);
|
|
60
60
|
}
|
|
61
|
+
async *streamChain(request) {
|
|
62
|
+
if (!this.handlers.length) {
|
|
63
|
+
throw new Error("No handlers added to the chain");
|
|
64
|
+
}
|
|
65
|
+
yield* this.handlers[0].stream(request);
|
|
66
|
+
}
|
|
61
67
|
};
|
|
62
68
|
|
|
63
69
|
// src/http/error.ts
|
|
@@ -256,6 +262,22 @@ var HookHandler = class {
|
|
|
256
262
|
}
|
|
257
263
|
throw await hook.onError(nextRequest, response, hookParams);
|
|
258
264
|
}
|
|
265
|
+
async *stream(request) {
|
|
266
|
+
if (!this.next) {
|
|
267
|
+
throw new Error("No next handler set in hook handler.");
|
|
268
|
+
}
|
|
269
|
+
const hook = new TransportHookAdapter();
|
|
270
|
+
const hookParams = this.getHookParams(request);
|
|
271
|
+
const nextRequest = await hook.beforeRequest(request, hookParams);
|
|
272
|
+
const stream = this.next.stream(nextRequest);
|
|
273
|
+
for await (const response of stream) {
|
|
274
|
+
if (response.metadata.status < 400) {
|
|
275
|
+
yield await hook.afterResponse(nextRequest, response, hookParams);
|
|
276
|
+
} else {
|
|
277
|
+
throw await hook.onError(nextRequest, response, hookParams);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
259
281
|
getHookParams(_request) {
|
|
260
282
|
const hookParams = /* @__PURE__ */ new Map();
|
|
261
283
|
return hookParams;
|
|
@@ -267,47 +289,95 @@ var import_zod = require("zod");
|
|
|
267
289
|
var ResponseValidationHandler = class {
|
|
268
290
|
async handle(request) {
|
|
269
291
|
const response = await this.next.handle(request);
|
|
292
|
+
return this.decodeBody(request, response);
|
|
293
|
+
}
|
|
294
|
+
async *stream(request) {
|
|
295
|
+
const stream = this.next.stream(request);
|
|
296
|
+
for await (const response of stream) {
|
|
297
|
+
const responseChunks = this.splitByDataChunks(response);
|
|
298
|
+
for (const chunk of responseChunks) {
|
|
299
|
+
yield this.decodeBody(request, chunk);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
splitByDataChunks(response) {
|
|
304
|
+
if (!response.metadata.headers["content-type"].includes("text/event-stream")) {
|
|
305
|
+
return [response];
|
|
306
|
+
}
|
|
307
|
+
const text = new TextDecoder().decode(response.raw);
|
|
308
|
+
const encoder = new TextEncoder();
|
|
309
|
+
return text.split("\n").filter((line) => line.startsWith("data: ")).map((part) => ({
|
|
310
|
+
...response,
|
|
311
|
+
raw: encoder.encode(part)
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
decodeBody(request, response) {
|
|
270
315
|
if (!this.hasContent(request, response)) {
|
|
271
316
|
return response;
|
|
272
317
|
}
|
|
273
|
-
if (request.responseContentType === "
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
318
|
+
if (request.responseContentType === "binary" /* Binary */ || request.responseContentType === "image" /* Image */) {
|
|
319
|
+
return this.decodeFile(request, response);
|
|
320
|
+
}
|
|
321
|
+
if (request.responseContentType === "multipartFormData" /* MultipartFormData */) {
|
|
322
|
+
return this.decodeMultipartFormData(request, response);
|
|
323
|
+
}
|
|
324
|
+
if (request.responseContentType === "text" /* Text */ || request.responseContentType === "xml" /* Xml */) {
|
|
325
|
+
return this.decodeText(request, response);
|
|
326
|
+
}
|
|
327
|
+
if (request.responseContentType === "form" /* FormUrlEncoded */) {
|
|
328
|
+
return this.decodeFormUrlEncoded(request, response);
|
|
329
|
+
}
|
|
330
|
+
if (request.responseContentType === "eventStream" /* EventStream */ || response.metadata.headers["content-type"].includes("text/event-stream")) {
|
|
331
|
+
return this.decodeEventStream(request, response);
|
|
332
|
+
}
|
|
333
|
+
return this.decodeJson(request, response);
|
|
334
|
+
}
|
|
335
|
+
decodeFile(request, response) {
|
|
336
|
+
return {
|
|
337
|
+
...response,
|
|
338
|
+
data: this.validate(request, response.raw)
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
decodeMultipartFormData(request, response) {
|
|
342
|
+
const formData = this.fromFormData(response.raw);
|
|
343
|
+
return {
|
|
344
|
+
...response,
|
|
345
|
+
data: this.validate(request, formData)
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
decodeText(request, response) {
|
|
349
|
+
const decodedBody = new TextDecoder().decode(response.raw);
|
|
350
|
+
return {
|
|
351
|
+
...response,
|
|
352
|
+
data: this.validate(request, decodedBody)
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
decodeFormUrlEncoded(request, response) {
|
|
356
|
+
const decodedBody = new TextDecoder().decode(response.raw);
|
|
357
|
+
const urlEncoded = this.fromUrlEncoded(decodedBody);
|
|
358
|
+
return {
|
|
359
|
+
...response,
|
|
360
|
+
data: this.validate(request, urlEncoded)
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
decodeEventStream(request, response) {
|
|
364
|
+
let decodedBody = new TextDecoder().decode(response.raw);
|
|
365
|
+
if (decodedBody.startsWith("data: ")) {
|
|
366
|
+
decodedBody = decodedBody.substring(6);
|
|
310
367
|
}
|
|
368
|
+
const json = JSON.parse(decodedBody);
|
|
369
|
+
return {
|
|
370
|
+
...response,
|
|
371
|
+
data: this.validate(request, json)
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
decodeJson(request, response) {
|
|
375
|
+
const decodedBody = new TextDecoder().decode(response.raw);
|
|
376
|
+
const json = JSON.parse(decodedBody);
|
|
377
|
+
return {
|
|
378
|
+
...response,
|
|
379
|
+
data: this.validate(request, json)
|
|
380
|
+
};
|
|
311
381
|
}
|
|
312
382
|
validate(request, data) {
|
|
313
383
|
var _a;
|
|
@@ -351,10 +421,21 @@ var ResponseValidationHandler = class {
|
|
|
351
421
|
// src/http/handlers/request-validation-handler.ts
|
|
352
422
|
var RequestValidationHandler = class {
|
|
353
423
|
async handle(request) {
|
|
354
|
-
var _a, _b;
|
|
355
424
|
if (!this.next) {
|
|
356
425
|
throw new Error("No next handler set in ContentTypeHandler.");
|
|
357
426
|
}
|
|
427
|
+
this.validateRequest(request);
|
|
428
|
+
return this.next.handle(request);
|
|
429
|
+
}
|
|
430
|
+
async *stream(request) {
|
|
431
|
+
if (!this.next) {
|
|
432
|
+
throw new Error("No next handler set in ContentTypeHandler.");
|
|
433
|
+
}
|
|
434
|
+
this.validateRequest(request);
|
|
435
|
+
yield* this.next.stream(request);
|
|
436
|
+
}
|
|
437
|
+
validateRequest(request) {
|
|
438
|
+
var _a, _b;
|
|
358
439
|
if (request.requestContentType === "json" /* Json */) {
|
|
359
440
|
request.body = JSON.stringify((_a = request.requestSchema) == null ? void 0 : _a.parse(request.body));
|
|
360
441
|
} else if (request.requestContentType === "xml" /* Xml */ || request.requestContentType === "binary" /* Binary */ || request.requestContentType === "text" /* Text */) {
|
|
@@ -366,7 +447,6 @@ var RequestValidationHandler = class {
|
|
|
366
447
|
} else {
|
|
367
448
|
request.body = JSON.stringify((_b = request.requestSchema) == null ? void 0 : _b.parse(request.body));
|
|
368
449
|
}
|
|
369
|
-
return await this.next.handle(request);
|
|
370
450
|
}
|
|
371
451
|
toFormUrlEncoded(request) {
|
|
372
452
|
var _a;
|
|
@@ -434,6 +514,34 @@ var RequestFetchAdapter = class {
|
|
|
434
514
|
raw: await response.clone().arrayBuffer()
|
|
435
515
|
};
|
|
436
516
|
}
|
|
517
|
+
async *stream() {
|
|
518
|
+
const response = await fetch(this.request.constructFullUrl(), this.requestInit);
|
|
519
|
+
const metadata = {
|
|
520
|
+
status: response.status,
|
|
521
|
+
statusText: response.statusText || "",
|
|
522
|
+
headers: this.getHeaders(response)
|
|
523
|
+
};
|
|
524
|
+
if (response.status >= 400) {
|
|
525
|
+
throw new HttpError(metadata, await response.clone().arrayBuffer());
|
|
526
|
+
}
|
|
527
|
+
if (!response.body) {
|
|
528
|
+
return yield {
|
|
529
|
+
metadata,
|
|
530
|
+
raw: await response.clone().arrayBuffer()
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
const reader = response.body.getReader();
|
|
534
|
+
while (true) {
|
|
535
|
+
const { done, value } = await reader.read();
|
|
536
|
+
if (done) {
|
|
537
|
+
break;
|
|
538
|
+
}
|
|
539
|
+
yield {
|
|
540
|
+
metadata,
|
|
541
|
+
raw: value
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
}
|
|
437
545
|
setMethod(method) {
|
|
438
546
|
if (!method) {
|
|
439
547
|
return;
|
|
@@ -484,6 +592,9 @@ var TerminatingHandler = class {
|
|
|
484
592
|
async handle(request) {
|
|
485
593
|
return new RequestFetchAdapter(request).send();
|
|
486
594
|
}
|
|
595
|
+
async *stream(request) {
|
|
596
|
+
yield* new RequestFetchAdapter(request).stream();
|
|
597
|
+
}
|
|
487
598
|
};
|
|
488
599
|
|
|
489
600
|
// src/http/handlers/retry-handler.ts
|
|
@@ -504,6 +615,23 @@ var RetryHandler = class {
|
|
|
504
615
|
}
|
|
505
616
|
throw new Error("Error retrying request.");
|
|
506
617
|
}
|
|
618
|
+
async *stream(request) {
|
|
619
|
+
if (!this.next) {
|
|
620
|
+
throw new Error("No next handler set in retry handler.");
|
|
621
|
+
}
|
|
622
|
+
for (let attempt = 1; attempt <= request.retry.attempts; attempt++) {
|
|
623
|
+
try {
|
|
624
|
+
yield* this.next.stream(request);
|
|
625
|
+
return;
|
|
626
|
+
} catch (error) {
|
|
627
|
+
if (!this.shouldRetry(error) || attempt === request.retry.attempts) {
|
|
628
|
+
throw error;
|
|
629
|
+
}
|
|
630
|
+
await this.delay(request.retry.delayMs);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
throw new Error("Error retrying request.");
|
|
634
|
+
}
|
|
507
635
|
shouldRetry(error) {
|
|
508
636
|
return error instanceof HttpError && (error.metadata.status >= 500 || error.metadata.status === 408);
|
|
509
637
|
}
|
|
@@ -520,12 +648,22 @@ var RetryHandler = class {
|
|
|
520
648
|
// src/http/handlers/oauth-handler.ts
|
|
521
649
|
var OAuthHandler = class {
|
|
522
650
|
async handle(request) {
|
|
523
|
-
var _a;
|
|
524
651
|
if (!this.next) {
|
|
525
652
|
throw new Error(`No next handler set in OAuthHandler`);
|
|
526
653
|
}
|
|
654
|
+
await this.addToken(request);
|
|
655
|
+
return this.next.handle(request);
|
|
656
|
+
}
|
|
657
|
+
async *stream(request) {
|
|
658
|
+
if (!this.next) {
|
|
659
|
+
throw new Error(`No next handler set in OAuthHandler`);
|
|
660
|
+
}
|
|
661
|
+
await this.addToken(request);
|
|
662
|
+
yield* this.next.stream(request);
|
|
663
|
+
}
|
|
664
|
+
async addToken(request) {
|
|
527
665
|
if (!request.scopes) {
|
|
528
|
-
return
|
|
666
|
+
return;
|
|
529
667
|
}
|
|
530
668
|
const token = await request.tokenManager.getToken(request.scopes, request.config);
|
|
531
669
|
if (token.accessToken) {
|
|
@@ -539,7 +677,6 @@ var OAuthHandler = class {
|
|
|
539
677
|
isOffset: false
|
|
540
678
|
});
|
|
541
679
|
}
|
|
542
|
-
return this.next.handle(request);
|
|
543
680
|
}
|
|
544
681
|
};
|
|
545
682
|
|
|
@@ -558,6 +695,9 @@ var HttpClient = class {
|
|
|
558
695
|
call(request) {
|
|
559
696
|
return this.requestHandlerChain.callChain(request);
|
|
560
697
|
}
|
|
698
|
+
async *stream(request) {
|
|
699
|
+
yield* this.requestHandlerChain.streamChain(request);
|
|
700
|
+
}
|
|
561
701
|
async callPaginated(request) {
|
|
562
702
|
const response = await this.call(request);
|
|
563
703
|
if (!response.data) {
|
package/dist/index.mjs
CHANGED
|
@@ -16,6 +16,12 @@ var RequestHandlerChain = class {
|
|
|
16
16
|
}
|
|
17
17
|
return this.handlers[0].handle(request);
|
|
18
18
|
}
|
|
19
|
+
async *streamChain(request) {
|
|
20
|
+
if (!this.handlers.length) {
|
|
21
|
+
throw new Error("No handlers added to the chain");
|
|
22
|
+
}
|
|
23
|
+
yield* this.handlers[0].stream(request);
|
|
24
|
+
}
|
|
19
25
|
};
|
|
20
26
|
|
|
21
27
|
// src/http/error.ts
|
|
@@ -214,6 +220,22 @@ var HookHandler = class {
|
|
|
214
220
|
}
|
|
215
221
|
throw await hook.onError(nextRequest, response, hookParams);
|
|
216
222
|
}
|
|
223
|
+
async *stream(request) {
|
|
224
|
+
if (!this.next) {
|
|
225
|
+
throw new Error("No next handler set in hook handler.");
|
|
226
|
+
}
|
|
227
|
+
const hook = new TransportHookAdapter();
|
|
228
|
+
const hookParams = this.getHookParams(request);
|
|
229
|
+
const nextRequest = await hook.beforeRequest(request, hookParams);
|
|
230
|
+
const stream = this.next.stream(nextRequest);
|
|
231
|
+
for await (const response of stream) {
|
|
232
|
+
if (response.metadata.status < 400) {
|
|
233
|
+
yield await hook.afterResponse(nextRequest, response, hookParams);
|
|
234
|
+
} else {
|
|
235
|
+
throw await hook.onError(nextRequest, response, hookParams);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
217
239
|
getHookParams(_request) {
|
|
218
240
|
const hookParams = /* @__PURE__ */ new Map();
|
|
219
241
|
return hookParams;
|
|
@@ -225,47 +247,95 @@ import { ZodUndefined } from "zod";
|
|
|
225
247
|
var ResponseValidationHandler = class {
|
|
226
248
|
async handle(request) {
|
|
227
249
|
const response = await this.next.handle(request);
|
|
250
|
+
return this.decodeBody(request, response);
|
|
251
|
+
}
|
|
252
|
+
async *stream(request) {
|
|
253
|
+
const stream = this.next.stream(request);
|
|
254
|
+
for await (const response of stream) {
|
|
255
|
+
const responseChunks = this.splitByDataChunks(response);
|
|
256
|
+
for (const chunk of responseChunks) {
|
|
257
|
+
yield this.decodeBody(request, chunk);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
splitByDataChunks(response) {
|
|
262
|
+
if (!response.metadata.headers["content-type"].includes("text/event-stream")) {
|
|
263
|
+
return [response];
|
|
264
|
+
}
|
|
265
|
+
const text = new TextDecoder().decode(response.raw);
|
|
266
|
+
const encoder = new TextEncoder();
|
|
267
|
+
return text.split("\n").filter((line) => line.startsWith("data: ")).map((part) => ({
|
|
268
|
+
...response,
|
|
269
|
+
raw: encoder.encode(part)
|
|
270
|
+
}));
|
|
271
|
+
}
|
|
272
|
+
decodeBody(request, response) {
|
|
228
273
|
if (!this.hasContent(request, response)) {
|
|
229
274
|
return response;
|
|
230
275
|
}
|
|
231
|
-
if (request.responseContentType === "
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
276
|
+
if (request.responseContentType === "binary" /* Binary */ || request.responseContentType === "image" /* Image */) {
|
|
277
|
+
return this.decodeFile(request, response);
|
|
278
|
+
}
|
|
279
|
+
if (request.responseContentType === "multipartFormData" /* MultipartFormData */) {
|
|
280
|
+
return this.decodeMultipartFormData(request, response);
|
|
281
|
+
}
|
|
282
|
+
if (request.responseContentType === "text" /* Text */ || request.responseContentType === "xml" /* Xml */) {
|
|
283
|
+
return this.decodeText(request, response);
|
|
284
|
+
}
|
|
285
|
+
if (request.responseContentType === "form" /* FormUrlEncoded */) {
|
|
286
|
+
return this.decodeFormUrlEncoded(request, response);
|
|
287
|
+
}
|
|
288
|
+
if (request.responseContentType === "eventStream" /* EventStream */ || response.metadata.headers["content-type"].includes("text/event-stream")) {
|
|
289
|
+
return this.decodeEventStream(request, response);
|
|
290
|
+
}
|
|
291
|
+
return this.decodeJson(request, response);
|
|
292
|
+
}
|
|
293
|
+
decodeFile(request, response) {
|
|
294
|
+
return {
|
|
295
|
+
...response,
|
|
296
|
+
data: this.validate(request, response.raw)
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
decodeMultipartFormData(request, response) {
|
|
300
|
+
const formData = this.fromFormData(response.raw);
|
|
301
|
+
return {
|
|
302
|
+
...response,
|
|
303
|
+
data: this.validate(request, formData)
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
decodeText(request, response) {
|
|
307
|
+
const decodedBody = new TextDecoder().decode(response.raw);
|
|
308
|
+
return {
|
|
309
|
+
...response,
|
|
310
|
+
data: this.validate(request, decodedBody)
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
decodeFormUrlEncoded(request, response) {
|
|
314
|
+
const decodedBody = new TextDecoder().decode(response.raw);
|
|
315
|
+
const urlEncoded = this.fromUrlEncoded(decodedBody);
|
|
316
|
+
return {
|
|
317
|
+
...response,
|
|
318
|
+
data: this.validate(request, urlEncoded)
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
decodeEventStream(request, response) {
|
|
322
|
+
let decodedBody = new TextDecoder().decode(response.raw);
|
|
323
|
+
if (decodedBody.startsWith("data: ")) {
|
|
324
|
+
decodedBody = decodedBody.substring(6);
|
|
268
325
|
}
|
|
326
|
+
const json = JSON.parse(decodedBody);
|
|
327
|
+
return {
|
|
328
|
+
...response,
|
|
329
|
+
data: this.validate(request, json)
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
decodeJson(request, response) {
|
|
333
|
+
const decodedBody = new TextDecoder().decode(response.raw);
|
|
334
|
+
const json = JSON.parse(decodedBody);
|
|
335
|
+
return {
|
|
336
|
+
...response,
|
|
337
|
+
data: this.validate(request, json)
|
|
338
|
+
};
|
|
269
339
|
}
|
|
270
340
|
validate(request, data) {
|
|
271
341
|
var _a;
|
|
@@ -309,10 +379,21 @@ var ResponseValidationHandler = class {
|
|
|
309
379
|
// src/http/handlers/request-validation-handler.ts
|
|
310
380
|
var RequestValidationHandler = class {
|
|
311
381
|
async handle(request) {
|
|
312
|
-
var _a, _b;
|
|
313
382
|
if (!this.next) {
|
|
314
383
|
throw new Error("No next handler set in ContentTypeHandler.");
|
|
315
384
|
}
|
|
385
|
+
this.validateRequest(request);
|
|
386
|
+
return this.next.handle(request);
|
|
387
|
+
}
|
|
388
|
+
async *stream(request) {
|
|
389
|
+
if (!this.next) {
|
|
390
|
+
throw new Error("No next handler set in ContentTypeHandler.");
|
|
391
|
+
}
|
|
392
|
+
this.validateRequest(request);
|
|
393
|
+
yield* this.next.stream(request);
|
|
394
|
+
}
|
|
395
|
+
validateRequest(request) {
|
|
396
|
+
var _a, _b;
|
|
316
397
|
if (request.requestContentType === "json" /* Json */) {
|
|
317
398
|
request.body = JSON.stringify((_a = request.requestSchema) == null ? void 0 : _a.parse(request.body));
|
|
318
399
|
} else if (request.requestContentType === "xml" /* Xml */ || request.requestContentType === "binary" /* Binary */ || request.requestContentType === "text" /* Text */) {
|
|
@@ -324,7 +405,6 @@ var RequestValidationHandler = class {
|
|
|
324
405
|
} else {
|
|
325
406
|
request.body = JSON.stringify((_b = request.requestSchema) == null ? void 0 : _b.parse(request.body));
|
|
326
407
|
}
|
|
327
|
-
return await this.next.handle(request);
|
|
328
408
|
}
|
|
329
409
|
toFormUrlEncoded(request) {
|
|
330
410
|
var _a;
|
|
@@ -392,6 +472,34 @@ var RequestFetchAdapter = class {
|
|
|
392
472
|
raw: await response.clone().arrayBuffer()
|
|
393
473
|
};
|
|
394
474
|
}
|
|
475
|
+
async *stream() {
|
|
476
|
+
const response = await fetch(this.request.constructFullUrl(), this.requestInit);
|
|
477
|
+
const metadata = {
|
|
478
|
+
status: response.status,
|
|
479
|
+
statusText: response.statusText || "",
|
|
480
|
+
headers: this.getHeaders(response)
|
|
481
|
+
};
|
|
482
|
+
if (response.status >= 400) {
|
|
483
|
+
throw new HttpError(metadata, await response.clone().arrayBuffer());
|
|
484
|
+
}
|
|
485
|
+
if (!response.body) {
|
|
486
|
+
return yield {
|
|
487
|
+
metadata,
|
|
488
|
+
raw: await response.clone().arrayBuffer()
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
const reader = response.body.getReader();
|
|
492
|
+
while (true) {
|
|
493
|
+
const { done, value } = await reader.read();
|
|
494
|
+
if (done) {
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
yield {
|
|
498
|
+
metadata,
|
|
499
|
+
raw: value
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
}
|
|
395
503
|
setMethod(method) {
|
|
396
504
|
if (!method) {
|
|
397
505
|
return;
|
|
@@ -442,6 +550,9 @@ var TerminatingHandler = class {
|
|
|
442
550
|
async handle(request) {
|
|
443
551
|
return new RequestFetchAdapter(request).send();
|
|
444
552
|
}
|
|
553
|
+
async *stream(request) {
|
|
554
|
+
yield* new RequestFetchAdapter(request).stream();
|
|
555
|
+
}
|
|
445
556
|
};
|
|
446
557
|
|
|
447
558
|
// src/http/handlers/retry-handler.ts
|
|
@@ -462,6 +573,23 @@ var RetryHandler = class {
|
|
|
462
573
|
}
|
|
463
574
|
throw new Error("Error retrying request.");
|
|
464
575
|
}
|
|
576
|
+
async *stream(request) {
|
|
577
|
+
if (!this.next) {
|
|
578
|
+
throw new Error("No next handler set in retry handler.");
|
|
579
|
+
}
|
|
580
|
+
for (let attempt = 1; attempt <= request.retry.attempts; attempt++) {
|
|
581
|
+
try {
|
|
582
|
+
yield* this.next.stream(request);
|
|
583
|
+
return;
|
|
584
|
+
} catch (error) {
|
|
585
|
+
if (!this.shouldRetry(error) || attempt === request.retry.attempts) {
|
|
586
|
+
throw error;
|
|
587
|
+
}
|
|
588
|
+
await this.delay(request.retry.delayMs);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
throw new Error("Error retrying request.");
|
|
592
|
+
}
|
|
465
593
|
shouldRetry(error) {
|
|
466
594
|
return error instanceof HttpError && (error.metadata.status >= 500 || error.metadata.status === 408);
|
|
467
595
|
}
|
|
@@ -478,12 +606,22 @@ var RetryHandler = class {
|
|
|
478
606
|
// src/http/handlers/oauth-handler.ts
|
|
479
607
|
var OAuthHandler = class {
|
|
480
608
|
async handle(request) {
|
|
481
|
-
var _a;
|
|
482
609
|
if (!this.next) {
|
|
483
610
|
throw new Error(`No next handler set in OAuthHandler`);
|
|
484
611
|
}
|
|
612
|
+
await this.addToken(request);
|
|
613
|
+
return this.next.handle(request);
|
|
614
|
+
}
|
|
615
|
+
async *stream(request) {
|
|
616
|
+
if (!this.next) {
|
|
617
|
+
throw new Error(`No next handler set in OAuthHandler`);
|
|
618
|
+
}
|
|
619
|
+
await this.addToken(request);
|
|
620
|
+
yield* this.next.stream(request);
|
|
621
|
+
}
|
|
622
|
+
async addToken(request) {
|
|
485
623
|
if (!request.scopes) {
|
|
486
|
-
return
|
|
624
|
+
return;
|
|
487
625
|
}
|
|
488
626
|
const token = await request.tokenManager.getToken(request.scopes, request.config);
|
|
489
627
|
if (token.accessToken) {
|
|
@@ -497,7 +635,6 @@ var OAuthHandler = class {
|
|
|
497
635
|
isOffset: false
|
|
498
636
|
});
|
|
499
637
|
}
|
|
500
|
-
return this.next.handle(request);
|
|
501
638
|
}
|
|
502
639
|
};
|
|
503
640
|
|
|
@@ -516,6 +653,9 @@ var HttpClient = class {
|
|
|
516
653
|
call(request) {
|
|
517
654
|
return this.requestHandlerChain.callChain(request);
|
|
518
655
|
}
|
|
656
|
+
async *stream(request) {
|
|
657
|
+
yield* this.requestHandlerChain.streamChain(request);
|
|
658
|
+
}
|
|
519
659
|
async callPaginated(request) {
|
|
520
660
|
const response = await this.call(request);
|
|
521
661
|
if (!response.data) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "celitech-sdk",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.23",
|
|
4
4
|
"description": "Welcome to the CELITECH API documentation! Useful links: [Homepage](https://www.celitech.com) | [Support email](mailto:support@celitech.com) | [Blog](https://www.celitech.com/blog/) ",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./dist/index.js",
|