mavenagi 0.0.0-alpha.11 → 0.0.0-alpha.12

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.
Files changed (139) hide show
  1. package/Client.d.ts +9 -0
  2. package/README.md +41 -4
  3. package/api/resources/actions/client/Client.d.ts +9 -0
  4. package/api/resources/actions/client/Client.js +18 -15
  5. package/api/resources/commons/types/EntityType.d.ts +2 -1
  6. package/api/resources/commons/types/EntityType.js +1 -0
  7. package/api/resources/commons/types/FeedbackBase.d.ts +10 -0
  8. package/api/resources/commons/types/index.d.ts +1 -1
  9. package/api/resources/commons/types/index.js +1 -1
  10. package/api/resources/conversation/client/Client.d.ts +18 -3
  11. package/api/resources/conversation/client/Client.js +71 -58
  12. package/api/resources/conversation/types/FeedbackRequest.d.ts +8 -1
  13. package/api/resources/knowledge/client/Client.d.ts +9 -0
  14. package/api/resources/knowledge/client/Client.js +44 -37
  15. package/api/resources/triggers/client/Client.d.ts +9 -0
  16. package/api/resources/triggers/client/Client.js +18 -15
  17. package/api/resources/users/client/Client.d.ts +9 -0
  18. package/api/resources/users/client/Client.js +13 -11
  19. package/core/fetcher/Fetcher.d.ts +4 -1
  20. package/core/fetcher/Fetcher.js +18 -159
  21. package/core/fetcher/createRequestUrl.d.ts +1 -0
  22. package/core/fetcher/createRequestUrl.js +6 -0
  23. package/core/fetcher/getFetchFn.d.ts +4 -0
  24. package/core/fetcher/getFetchFn.js +32 -0
  25. package/core/fetcher/getRequestBody.d.ts +7 -0
  26. package/core/fetcher/getRequestBody.js +19 -0
  27. package/core/fetcher/getResponseBody.d.ts +1 -0
  28. package/core/fetcher/getResponseBody.js +48 -0
  29. package/core/fetcher/makeRequest.d.ts +1 -0
  30. package/core/fetcher/makeRequest.js +38 -0
  31. package/core/fetcher/requestWithRetries.d.ts +1 -0
  32. package/core/fetcher/requestWithRetries.js +28 -0
  33. package/core/fetcher/signals.d.ts +12 -0
  34. package/core/fetcher/signals.js +32 -0
  35. package/core/fetcher/stream-wrappers/Node18UniversalStreamWrapper.d.ts +30 -0
  36. package/core/fetcher/stream-wrappers/Node18UniversalStreamWrapper.js +222 -0
  37. package/core/fetcher/stream-wrappers/NodePre18StreamWrapper.d.ts +21 -0
  38. package/core/fetcher/stream-wrappers/NodePre18StreamWrapper.js +104 -0
  39. package/core/fetcher/stream-wrappers/UndiciStreamWrapper.d.ts +30 -0
  40. package/core/fetcher/stream-wrappers/UndiciStreamWrapper.js +204 -0
  41. package/core/fetcher/stream-wrappers/chooseStreamWrapper.d.ts +17 -0
  42. package/core/fetcher/stream-wrappers/chooseStreamWrapper.js +23 -0
  43. package/core/runtime/runtime.d.ts +1 -0
  44. package/core/runtime/runtime.js +1 -0
  45. package/core/schemas/Schema.d.ts +7 -4
  46. package/core/schemas/builders/lazy/lazy.d.ts +2 -2
  47. package/core/schemas/builders/lazy/lazy.js +8 -19
  48. package/core/schemas/builders/lazy/lazyObject.js +1 -10
  49. package/core/schemas/builders/list/list.js +31 -44
  50. package/core/schemas/builders/object/object.js +90 -111
  51. package/core/schemas/builders/object/types.d.ts +2 -2
  52. package/core/schemas/builders/object-like/getObjectLikeUtils.js +3 -12
  53. package/core/schemas/builders/record/record.js +49 -60
  54. package/core/schemas/builders/schema-utils/getSchemaUtils.d.ts +2 -2
  55. package/core/schemas/builders/schema-utils/getSchemaUtils.js +18 -21
  56. package/core/schemas/builders/set/set.js +6 -15
  57. package/core/schemas/builders/undiscriminated-union/undiscriminatedUnion.js +21 -32
  58. package/core/schemas/builders/union/union.js +51 -62
  59. package/core/schemas/utils/maybeSkipValidation.js +3 -12
  60. package/dist/Client.d.ts +9 -0
  61. package/dist/api/resources/actions/client/Client.d.ts +9 -0
  62. package/dist/api/resources/actions/client/Client.js +18 -15
  63. package/dist/api/resources/commons/types/EntityType.d.ts +2 -1
  64. package/dist/api/resources/commons/types/EntityType.js +1 -0
  65. package/dist/api/resources/commons/types/FeedbackBase.d.ts +10 -0
  66. package/dist/api/resources/commons/types/index.d.ts +1 -1
  67. package/dist/api/resources/commons/types/index.js +1 -1
  68. package/dist/api/resources/conversation/client/Client.d.ts +18 -3
  69. package/dist/api/resources/conversation/client/Client.js +71 -58
  70. package/dist/api/resources/conversation/types/FeedbackRequest.d.ts +8 -1
  71. package/dist/api/resources/knowledge/client/Client.d.ts +9 -0
  72. package/dist/api/resources/knowledge/client/Client.js +44 -37
  73. package/dist/api/resources/triggers/client/Client.d.ts +9 -0
  74. package/dist/api/resources/triggers/client/Client.js +18 -15
  75. package/dist/api/resources/users/client/Client.d.ts +9 -0
  76. package/dist/api/resources/users/client/Client.js +13 -11
  77. package/dist/core/fetcher/Fetcher.d.ts +4 -1
  78. package/dist/core/fetcher/Fetcher.js +18 -159
  79. package/dist/core/fetcher/createRequestUrl.d.ts +1 -0
  80. package/dist/core/fetcher/createRequestUrl.js +6 -0
  81. package/dist/core/fetcher/getFetchFn.d.ts +4 -0
  82. package/dist/core/fetcher/getFetchFn.js +32 -0
  83. package/dist/core/fetcher/getRequestBody.d.ts +7 -0
  84. package/dist/core/fetcher/getRequestBody.js +19 -0
  85. package/dist/core/fetcher/getResponseBody.d.ts +1 -0
  86. package/dist/core/fetcher/getResponseBody.js +48 -0
  87. package/dist/core/fetcher/makeRequest.d.ts +1 -0
  88. package/dist/core/fetcher/makeRequest.js +38 -0
  89. package/dist/core/fetcher/requestWithRetries.d.ts +1 -0
  90. package/dist/core/fetcher/requestWithRetries.js +28 -0
  91. package/dist/core/fetcher/signals.d.ts +12 -0
  92. package/dist/core/fetcher/signals.js +32 -0
  93. package/dist/core/fetcher/stream-wrappers/Node18UniversalStreamWrapper.d.ts +30 -0
  94. package/dist/core/fetcher/stream-wrappers/Node18UniversalStreamWrapper.js +222 -0
  95. package/dist/core/fetcher/stream-wrappers/NodePre18StreamWrapper.d.ts +21 -0
  96. package/dist/core/fetcher/stream-wrappers/NodePre18StreamWrapper.js +104 -0
  97. package/dist/core/fetcher/stream-wrappers/UndiciStreamWrapper.d.ts +30 -0
  98. package/dist/core/fetcher/stream-wrappers/UndiciStreamWrapper.js +204 -0
  99. package/dist/core/fetcher/stream-wrappers/chooseStreamWrapper.d.ts +17 -0
  100. package/dist/core/fetcher/stream-wrappers/chooseStreamWrapper.js +23 -0
  101. package/dist/core/runtime/runtime.d.ts +1 -0
  102. package/dist/core/runtime/runtime.js +1 -0
  103. package/dist/core/schemas/Schema.d.ts +7 -4
  104. package/dist/core/schemas/builders/lazy/lazy.d.ts +2 -2
  105. package/dist/core/schemas/builders/lazy/lazy.js +8 -19
  106. package/dist/core/schemas/builders/lazy/lazyObject.js +1 -10
  107. package/dist/core/schemas/builders/list/list.js +31 -44
  108. package/dist/core/schemas/builders/object/object.js +90 -111
  109. package/dist/core/schemas/builders/object/types.d.ts +2 -2
  110. package/dist/core/schemas/builders/object-like/getObjectLikeUtils.js +3 -12
  111. package/dist/core/schemas/builders/record/record.js +49 -60
  112. package/dist/core/schemas/builders/schema-utils/getSchemaUtils.d.ts +2 -2
  113. package/dist/core/schemas/builders/schema-utils/getSchemaUtils.js +18 -21
  114. package/dist/core/schemas/builders/set/set.js +6 -15
  115. package/dist/core/schemas/builders/undiscriminated-union/undiscriminatedUnion.js +21 -32
  116. package/dist/core/schemas/builders/union/union.js +51 -62
  117. package/dist/core/schemas/utils/maybeSkipValidation.js +3 -12
  118. package/dist/serialization/resources/commons/types/EntityType.d.ts +1 -1
  119. package/dist/serialization/resources/commons/types/EntityType.js +1 -0
  120. package/{serialization/resources/commons/types/Feedback.d.ts → dist/serialization/resources/commons/types/FeedbackBase.d.ts} +2 -5
  121. package/{serialization/resources/commons/types/Feedback.js → dist/serialization/resources/commons/types/FeedbackBase.js} +1 -4
  122. package/dist/serialization/resources/commons/types/index.d.ts +1 -1
  123. package/dist/serialization/resources/commons/types/index.js +1 -1
  124. package/dist/serialization/resources/conversation/types/FeedbackRequest.d.ts +7 -2
  125. package/dist/serialization/resources/conversation/types/FeedbackRequest.js +10 -2
  126. package/package.json +7 -2
  127. package/reference.md +1723 -0
  128. package/serialization/resources/commons/types/EntityType.d.ts +1 -1
  129. package/serialization/resources/commons/types/EntityType.js +1 -0
  130. package/{dist/serialization/resources/commons/types/Feedback.d.ts → serialization/resources/commons/types/FeedbackBase.d.ts} +2 -5
  131. package/{dist/serialization/resources/commons/types/Feedback.js → serialization/resources/commons/types/FeedbackBase.js} +1 -4
  132. package/serialization/resources/commons/types/index.d.ts +1 -1
  133. package/serialization/resources/commons/types/index.js +1 -1
  134. package/serialization/resources/conversation/types/FeedbackRequest.d.ts +7 -2
  135. package/serialization/resources/conversation/types/FeedbackRequest.js +10 -2
  136. package/api/resources/commons/types/Feedback.d.ts +0 -16
  137. package/dist/api/resources/commons/types/Feedback.d.ts +0 -16
  138. /package/api/resources/commons/types/{Feedback.js → FeedbackBase.js} +0 -0
  139. /package/dist/api/resources/commons/types/{Feedback.js → FeedbackBase.js} +0 -0
@@ -7,13 +7,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import qs from "qs";
11
- import { RUNTIME } from "../runtime";
12
- const INITIAL_RETRY_DELAY = 1;
13
- const MAX_RETRY_DELAY = 60;
14
- const DEFAULT_MAX_RETRIES = 2;
15
- function fetcherImpl(args) {
16
- var _a, _b;
10
+ import { createRequestUrl } from "./createRequestUrl";
11
+ import { getFetchFn } from "./getFetchFn";
12
+ import { getRequestBody } from "./getRequestBody";
13
+ import { getResponseBody } from "./getResponseBody";
14
+ import { makeRequest } from "./makeRequest";
15
+ import { requestWithRetries } from "./requestWithRetries";
16
+ export function fetcherImpl(args) {
17
17
  return __awaiter(this, void 0, void 0, function* () {
18
18
  const headers = {};
19
19
  if (args.body !== undefined && args.contentType != null) {
@@ -26,112 +26,21 @@ function fetcherImpl(args) {
26
26
  }
27
27
  }
28
28
  }
29
- const url = Object.keys((_a = args.queryParameters) !== null && _a !== void 0 ? _a : {}).length > 0
30
- ? `${args.url}?${qs.stringify(args.queryParameters, { arrayFormat: "repeat" })}`
31
- : args.url;
32
- let body = undefined;
33
- const maybeStringifyBody = (body) => {
34
- if (body instanceof Uint8Array) {
35
- return body;
36
- }
37
- else if (args.contentType === "application/x-www-form-urlencoded" && typeof args.body === "string") {
38
- return args.body;
39
- }
40
- else {
41
- return JSON.stringify(body);
42
- }
43
- };
44
- if (RUNTIME.type === "node") {
45
- if (args.body instanceof (yield import("formdata-node")).FormData) {
46
- // @ts-expect-error
47
- body = args.body;
48
- }
49
- else {
50
- body = maybeStringifyBody(args.body);
51
- }
52
- }
53
- else {
54
- if (args.body instanceof (yield import("form-data")).default) {
55
- // @ts-expect-error
56
- body = args.body;
57
- }
58
- else {
59
- body = maybeStringifyBody(args.body);
60
- }
61
- }
62
- const fetchFn = yield getFetchFn();
63
- const makeRequest = () => __awaiter(this, void 0, void 0, function* () {
64
- const signals = [];
65
- // Add timeout signal
66
- let timeoutAbortId = undefined;
67
- if (args.timeoutMs != null) {
68
- const { signal, abortId } = getTimeoutSignal(args.timeoutMs);
69
- timeoutAbortId = abortId;
70
- signals.push(signal);
71
- }
72
- // Add arbitrary signal
73
- if (args.abortSignal != null) {
74
- signals.push(args.abortSignal);
75
- }
76
- const response = yield fetchFn(url, {
77
- method: args.method,
78
- headers,
79
- body,
80
- signal: anySignal(signals),
81
- credentials: args.withCredentials ? "include" : undefined,
82
- });
83
- if (timeoutAbortId != null) {
84
- clearTimeout(timeoutAbortId);
85
- }
86
- return response;
29
+ const url = createRequestUrl(args.url, args.queryParameters);
30
+ let requestBody = yield getRequestBody({
31
+ body: args.body,
32
+ type: args.requestType === "json" ? "json" : "other",
87
33
  });
34
+ const fetchFn = yield getFetchFn();
88
35
  try {
89
- let response = yield makeRequest();
90
- for (let i = 0; i < ((_b = args.maxRetries) !== null && _b !== void 0 ? _b : DEFAULT_MAX_RETRIES); ++i) {
91
- if (response.status === 408 ||
92
- response.status === 409 ||
93
- response.status === 429 ||
94
- response.status >= 500) {
95
- const delay = Math.min(INITIAL_RETRY_DELAY * Math.pow(i, 2), MAX_RETRY_DELAY);
96
- yield new Promise((resolve) => setTimeout(resolve, delay));
97
- response = yield makeRequest();
98
- }
99
- else {
100
- break;
101
- }
102
- }
103
- let body;
104
- if (response.body != null && args.responseType === "blob") {
105
- body = yield response.blob();
106
- }
107
- else if (response.body != null && args.responseType === "streaming") {
108
- body = response.body;
109
- }
110
- else if (response.body != null && args.responseType === "text") {
111
- body = yield response.text();
112
- }
113
- else {
114
- const text = yield response.text();
115
- if (text.length > 0) {
116
- try {
117
- body = JSON.parse(text);
118
- }
119
- catch (err) {
120
- return {
121
- ok: false,
122
- error: {
123
- reason: "non-json",
124
- statusCode: response.status,
125
- rawBody: text,
126
- },
127
- };
128
- }
129
- }
130
- }
36
+ const response = yield requestWithRetries(() => __awaiter(this, void 0, void 0, function* () {
37
+ return makeRequest(fetchFn, url, args.method, headers, requestBody, args.timeoutMs, args.abortSignal, args.withCredentials, args.duplex);
38
+ }), args.maxRetries);
39
+ let responseBody = yield getResponseBody(response, args.responseType);
131
40
  if (response.status >= 200 && response.status < 400) {
132
41
  return {
133
42
  ok: true,
134
- body: body,
43
+ body: responseBody,
135
44
  headers: response.headers,
136
45
  };
137
46
  }
@@ -141,7 +50,7 @@ function fetcherImpl(args) {
141
50
  error: {
142
51
  reason: "status-code",
143
52
  statusCode: response.status,
144
- body,
53
+ body: responseBody,
145
54
  },
146
55
  };
147
56
  }
@@ -183,54 +92,4 @@ function fetcherImpl(args) {
183
92
  }
184
93
  });
185
94
  }
186
- const TIMEOUT = "timeout";
187
- function getTimeoutSignal(timeoutMs) {
188
- const controller = new AbortController();
189
- const abortId = setTimeout(() => controller.abort(TIMEOUT), timeoutMs);
190
- return { signal: controller.signal, abortId };
191
- }
192
- /**
193
- * Returns an abort signal that is getting aborted when
194
- * at least one of the specified abort signals is aborted.
195
- *
196
- * Requires at least node.js 18.
197
- */
198
- function anySignal(...args) {
199
- // Allowing signals to be passed either as array
200
- // of signals or as multiple arguments.
201
- const signals = (args.length === 1 && Array.isArray(args[0]) ? args[0] : args);
202
- const controller = new AbortController();
203
- for (const signal of signals) {
204
- if (signal.aborted) {
205
- // Exiting early if one of the signals
206
- // is already aborted.
207
- controller.abort(signal === null || signal === void 0 ? void 0 : signal.reason);
208
- break;
209
- }
210
- // Listening for signals and removing the listeners
211
- // when at least one symbol is aborted.
212
- signal.addEventListener("abort", () => controller.abort(signal === null || signal === void 0 ? void 0 : signal.reason), {
213
- signal: controller.signal,
214
- });
215
- }
216
- return controller.signal;
217
- }
218
- /**
219
- * Returns a fetch function based on the runtime
220
- */
221
- function getFetchFn() {
222
- return __awaiter(this, void 0, void 0, function* () {
223
- // In Node.js environments, the SDK always uses`node-fetch`.
224
- if (RUNTIME.type === "node") {
225
- return (yield import("node-fetch")).default;
226
- }
227
- // Otherwise the SDK uses global fetch if available,
228
- // and falls back to node-fetch.
229
- if (typeof fetch == "function") {
230
- return fetch;
231
- }
232
- // Defaults to node `node-fetch` if global fetch isn't available
233
- return (yield import("node-fetch")).default;
234
- });
235
- }
236
95
  export const fetcher = fetcherImpl;
@@ -0,0 +1 @@
1
+ export declare function createRequestUrl(baseUrl: string, queryParameters?: Record<string, string | string[] | object | object[]>): string;
@@ -0,0 +1,6 @@
1
+ import qs from "qs";
2
+ export function createRequestUrl(baseUrl, queryParameters) {
3
+ return Object.keys(queryParameters !== null && queryParameters !== void 0 ? queryParameters : {}).length > 0
4
+ ? `${baseUrl}?${qs.stringify(queryParameters, { arrayFormat: "repeat" })}`
5
+ : baseUrl;
6
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Returns a fetch function based on the runtime
3
+ */
4
+ export declare function getFetchFn(): Promise<any>;
@@ -0,0 +1,32 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { RUNTIME } from "../runtime";
11
+ /**
12
+ * Returns a fetch function based on the runtime
13
+ */
14
+ export function getFetchFn() {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ // In Node.js 18+ environments, use native fetch
17
+ if (RUNTIME.type === "node" && RUNTIME.parsedVersion != null && RUNTIME.parsedVersion >= 18) {
18
+ return fetch;
19
+ }
20
+ // In Node.js 18 or lower environments, the SDK always uses`node-fetch`.
21
+ if (RUNTIME.type === "node") {
22
+ return (yield import("node-fetch")).default;
23
+ }
24
+ // Otherwise the SDK uses global fetch if available,
25
+ // and falls back to node-fetch.
26
+ if (typeof fetch == "function") {
27
+ return fetch;
28
+ }
29
+ // Defaults to node `node-fetch` if global fetch isn't available
30
+ return (yield import("node-fetch")).default;
31
+ });
32
+ }
@@ -0,0 +1,7 @@
1
+ export declare namespace GetRequestBody {
2
+ interface Args {
3
+ body: unknown;
4
+ type: "json" | "file" | "bytes" | "other";
5
+ }
6
+ }
7
+ export declare function getRequestBody({ body, type }: GetRequestBody.Args): Promise<BodyInit | undefined>;
@@ -0,0 +1,19 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ export function getRequestBody({ body, type }) {
11
+ return __awaiter(this, void 0, void 0, function* () {
12
+ if (type.includes("json")) {
13
+ return JSON.stringify(body);
14
+ }
15
+ else {
16
+ return body;
17
+ }
18
+ });
19
+ }
@@ -0,0 +1 @@
1
+ export declare function getResponseBody(response: Response, responseType?: string): Promise<unknown>;
@@ -0,0 +1,48 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { chooseStreamWrapper } from "./stream-wrappers/chooseStreamWrapper";
11
+ export function getResponseBody(response, responseType) {
12
+ return __awaiter(this, void 0, void 0, function* () {
13
+ if (response.body != null && responseType === "blob") {
14
+ return yield response.blob();
15
+ }
16
+ else if (response.body != null && responseType === "sse") {
17
+ return response.body;
18
+ }
19
+ else if (response.body != null && responseType === "streaming") {
20
+ return chooseStreamWrapper(response.body);
21
+ }
22
+ else if (response.body != null && responseType === "text") {
23
+ return yield response.text();
24
+ }
25
+ else {
26
+ const text = yield response.text();
27
+ if (text.length > 0) {
28
+ try {
29
+ let responseBody = JSON.parse(text);
30
+ return responseBody;
31
+ }
32
+ catch (err) {
33
+ return {
34
+ ok: false,
35
+ error: {
36
+ reason: "non-json",
37
+ statusCode: response.status,
38
+ rawBody: text,
39
+ },
40
+ };
41
+ }
42
+ }
43
+ else {
44
+ return undefined;
45
+ }
46
+ }
47
+ });
48
+ }
@@ -0,0 +1 @@
1
+ export declare const makeRequest: (fetchFn: (url: string, init: RequestInit) => Promise<Response>, url: string, method: string, headers: Record<string, string>, requestBody: BodyInit | undefined, timeoutMs?: number | undefined, abortSignal?: AbortSignal | undefined, withCredentials?: boolean | undefined, duplex?: "half" | undefined) => Promise<Response>;
@@ -0,0 +1,38 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { anySignal, getTimeoutSignal } from "./signals";
11
+ export const makeRequest = (fetchFn, url, method, headers, requestBody, timeoutMs, abortSignal, withCredentials, duplex) => __awaiter(void 0, void 0, void 0, function* () {
12
+ const signals = [];
13
+ // Add timeout signal
14
+ let timeoutAbortId = undefined;
15
+ if (timeoutMs != null) {
16
+ const { signal, abortId } = getTimeoutSignal(timeoutMs);
17
+ timeoutAbortId = abortId;
18
+ signals.push(signal);
19
+ }
20
+ // Add arbitrary signal
21
+ if (abortSignal != null) {
22
+ signals.push(abortSignal);
23
+ }
24
+ let newSignals = anySignal(signals);
25
+ const response = yield fetchFn(url, {
26
+ method: method,
27
+ headers,
28
+ body: requestBody,
29
+ signal: newSignals,
30
+ credentials: withCredentials ? "include" : undefined,
31
+ // @ts-ignore
32
+ duplex,
33
+ });
34
+ if (timeoutAbortId != null) {
35
+ clearTimeout(timeoutAbortId);
36
+ }
37
+ return response;
38
+ });
@@ -0,0 +1 @@
1
+ export declare function requestWithRetries(requestFn: () => Promise<Response>, maxRetries?: number): Promise<Response>;
@@ -0,0 +1,28 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const INITIAL_RETRY_DELAY = 1;
11
+ const MAX_RETRY_DELAY = 60;
12
+ const DEFAULT_MAX_RETRIES = 2;
13
+ export function requestWithRetries(requestFn, maxRetries = DEFAULT_MAX_RETRIES) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ let response = yield requestFn();
16
+ for (let i = 0; i < maxRetries; ++i) {
17
+ if ([408, 409, 429].includes(response.status) || response.status >= 500) {
18
+ const delay = Math.min(INITIAL_RETRY_DELAY * Math.pow(2, i), MAX_RETRY_DELAY);
19
+ yield new Promise((resolve) => setTimeout(resolve, delay));
20
+ response = yield requestFn();
21
+ }
22
+ else {
23
+ break;
24
+ }
25
+ }
26
+ return response;
27
+ });
28
+ }
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ export declare function getTimeoutSignal(timeoutMs: number): {
3
+ signal: AbortSignal;
4
+ abortId: NodeJS.Timeout;
5
+ };
6
+ /**
7
+ * Returns an abort signal that is getting aborted when
8
+ * at least one of the specified abort signals is aborted.
9
+ *
10
+ * Requires at least node.js 18.
11
+ */
12
+ export declare function anySignal(...args: AbortSignal[] | [AbortSignal[]]): AbortSignal;
@@ -0,0 +1,32 @@
1
+ const TIMEOUT = "timeout";
2
+ export function getTimeoutSignal(timeoutMs) {
3
+ const controller = new AbortController();
4
+ const abortId = setTimeout(() => controller.abort(TIMEOUT), timeoutMs);
5
+ return { signal: controller.signal, abortId };
6
+ }
7
+ /**
8
+ * Returns an abort signal that is getting aborted when
9
+ * at least one of the specified abort signals is aborted.
10
+ *
11
+ * Requires at least node.js 18.
12
+ */
13
+ export function anySignal(...args) {
14
+ // Allowing signals to be passed either as array
15
+ // of signals or as multiple arguments.
16
+ const signals = (args.length === 1 && Array.isArray(args[0]) ? args[0] : args);
17
+ const controller = new AbortController();
18
+ for (const signal of signals) {
19
+ if (signal.aborted) {
20
+ // Exiting early if one of the signals
21
+ // is already aborted.
22
+ controller.abort(signal === null || signal === void 0 ? void 0 : signal.reason);
23
+ break;
24
+ }
25
+ // Listening for signals and removing the listeners
26
+ // when at least one symbol is aborted.
27
+ signal.addEventListener("abort", () => controller.abort(signal === null || signal === void 0 ? void 0 : signal.reason), {
28
+ signal: controller.signal,
29
+ });
30
+ }
31
+ return controller.signal;
32
+ }
@@ -0,0 +1,30 @@
1
+ /// <reference types="node" />
2
+ import type { Writable } from "stream";
3
+ import { EventCallback, StreamWrapper } from "./chooseStreamWrapper";
4
+ export declare class Node18UniversalStreamWrapper implements StreamWrapper<Node18UniversalStreamWrapper | Writable | WritableStream<Uint8Array>, Uint8Array> {
5
+ private readableStream;
6
+ private reader;
7
+ private events;
8
+ private paused;
9
+ private resumeCallback;
10
+ private encoding;
11
+ constructor(readableStream: ReadableStream<Uint8Array>);
12
+ on(event: string, callback: EventCallback): void;
13
+ off(event: string, callback: EventCallback): void;
14
+ pipe(dest: Node18UniversalStreamWrapper | Writable | WritableStream<Uint8Array>): Node18UniversalStreamWrapper | Writable | WritableStream<Uint8Array>;
15
+ pipeTo(dest: Node18UniversalStreamWrapper | Writable | WritableStream<Uint8Array>): Node18UniversalStreamWrapper | Writable | WritableStream<Uint8Array>;
16
+ unpipe(dest: Node18UniversalStreamWrapper | Writable | WritableStream<Uint8Array>): void;
17
+ destroy(error?: Error): void;
18
+ pause(): void;
19
+ resume(): void;
20
+ get isPaused(): boolean;
21
+ read(): Promise<Uint8Array | undefined>;
22
+ setEncoding(encoding: string): void;
23
+ text(): Promise<string>;
24
+ json<T>(): Promise<T>;
25
+ private _write;
26
+ private _end;
27
+ private _error;
28
+ private _emit;
29
+ private _startReading;
30
+ }