@trigger.dev/core 0.0.0-prerelease-20240603154820 → 0.0.0-prerelease-20240723165848

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 (187) hide show
  1. package/dist/api-Pl9fxB8v.d.mts +13879 -0
  2. package/dist/api-sNoooMbT.d.ts +13879 -0
  3. package/dist/{catalog-dRKTgwQ7.d.ts → catalog-H7yFiZ60.d.mts} +52 -52
  4. package/dist/{catalog-XTlJQaMn.d.mts → catalog-h79CG5Wy.d.ts} +52 -52
  5. package/dist/{manager-JkbddlcO.d.mts → common-CLW82lkt.d.mts} +280 -16
  6. package/dist/{manager-JkbddlcO.d.ts → common-CLW82lkt.d.ts} +280 -16
  7. package/dist/{eventFilterMatches-2kHImluE.d.mts → eventFilter-66NaBAMi.d.mts} +1 -3
  8. package/dist/{eventFilterMatches-2kHImluE.d.ts → eventFilter-66NaBAMi.d.ts} +1 -3
  9. package/dist/eventFilterMatches.d.mts +6 -0
  10. package/dist/eventFilterMatches.d.ts +6 -0
  11. package/dist/eventFilterMatches.js +165 -0
  12. package/dist/eventFilterMatches.js.map +1 -0
  13. package/dist/eventFilterMatches.mjs +163 -0
  14. package/dist/eventFilterMatches.mjs.map +1 -0
  15. package/dist/index.d.mts +14 -16716
  16. package/dist/index.d.ts +14 -16716
  17. package/dist/index.js +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs +1 -1
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/json-DBPEJQRe.d.mts +16 -0
  22. package/dist/json-DBPEJQRe.d.ts +16 -0
  23. package/dist/manager-KvCabW1a.d.mts +18 -0
  24. package/dist/manager-OBA35PzR.d.ts +18 -0
  25. package/dist/{messages-6_-q72KG.d.mts → messages-JvHJ83wv.d.mts} +2978 -291
  26. package/dist/{messages-6_-q72KG.d.ts → messages-JvHJ83wv.d.ts} +2978 -291
  27. package/dist/replacements.d.mts +22 -0
  28. package/dist/replacements.d.ts +22 -0
  29. package/dist/replacements.js +33 -0
  30. package/dist/replacements.js.map +1 -0
  31. package/dist/replacements.mjs +28 -0
  32. package/dist/replacements.mjs.map +1 -0
  33. package/dist/requestFilter-HNZQEpQb.d.ts +180 -0
  34. package/dist/requestFilter-b5W2_1oP.d.mts +180 -0
  35. package/dist/requestFilterMatches.d.mts +13 -0
  36. package/dist/requestFilterMatches.d.ts +13 -0
  37. package/dist/requestFilterMatches.js +238 -0
  38. package/dist/requestFilterMatches.js.map +1 -0
  39. package/dist/requestFilterMatches.mjs +235 -0
  40. package/dist/requestFilterMatches.mjs.map +1 -0
  41. package/dist/retry.d.mts +10 -0
  42. package/dist/retry.d.ts +10 -0
  43. package/dist/retry.js +100 -0
  44. package/dist/retry.js.map +1 -0
  45. package/dist/retry.mjs +97 -0
  46. package/dist/retry.mjs.map +1 -0
  47. package/dist/schemas/index.d.mts +2611 -0
  48. package/dist/schemas/index.d.ts +2611 -0
  49. package/dist/schemas/index.js +1630 -0
  50. package/dist/schemas/index.js.map +1 -0
  51. package/dist/schemas/index.mjs +1463 -0
  52. package/dist/schemas/index.mjs.map +1 -0
  53. package/dist/{schemas-r4ZP9S-F.d.mts → schemas-C9ssfehv.d.mts} +270 -33
  54. package/dist/{schemas-r4ZP9S-F.d.ts → schemas-C9ssfehv.d.ts} +270 -33
  55. package/dist/taskLogger-CzADe4je.d.ts +33 -0
  56. package/dist/taskLogger-r_AFTX45.d.mts +33 -0
  57. package/dist/tracer-N0p2Fuuv.d.mts +23 -0
  58. package/dist/tracer-N0p2Fuuv.d.ts +23 -0
  59. package/dist/types.d.mts +11 -0
  60. package/dist/types.d.ts +11 -0
  61. package/dist/types.js +4 -0
  62. package/dist/types.js.map +1 -0
  63. package/dist/types.mjs +3 -0
  64. package/dist/types.mjs.map +1 -0
  65. package/dist/utils.d.mts +7 -0
  66. package/dist/utils.d.ts +7 -0
  67. package/dist/utils.js +33 -0
  68. package/dist/utils.js.map +1 -0
  69. package/dist/utils.mjs +30 -0
  70. package/dist/utils.mjs.map +1 -0
  71. package/dist/v3/dev/index.d.mts +2 -1
  72. package/dist/v3/dev/index.d.ts +2 -1
  73. package/dist/v3/dev/index.js.map +1 -1
  74. package/dist/v3/dev/index.mjs.map +1 -1
  75. package/dist/v3/errors.d.mts +39 -0
  76. package/dist/v3/errors.d.ts +39 -0
  77. package/dist/v3/errors.js +204 -0
  78. package/dist/v3/errors.js.map +1 -0
  79. package/dist/v3/errors.mjs +195 -0
  80. package/dist/v3/errors.mjs.map +1 -0
  81. package/dist/v3/index.d.mts +172 -4737
  82. package/dist/v3/index.d.ts +172 -4737
  83. package/dist/v3/index.js +871 -620
  84. package/dist/v3/index.js.map +1 -1
  85. package/dist/v3/index.mjs +856 -620
  86. package/dist/v3/index.mjs.map +1 -1
  87. package/dist/v3/logger-api.d.mts +24 -0
  88. package/dist/v3/logger-api.d.ts +24 -0
  89. package/dist/v3/logger-api.js +118 -0
  90. package/dist/v3/logger-api.js.map +1 -0
  91. package/dist/v3/logger-api.mjs +116 -0
  92. package/dist/v3/logger-api.mjs.map +1 -0
  93. package/dist/v3/otel/index.js +36 -12
  94. package/dist/v3/otel/index.js.map +1 -1
  95. package/dist/v3/otel/index.mjs +36 -12
  96. package/dist/v3/otel/index.mjs.map +1 -1
  97. package/dist/v3/prod/index.d.mts +4 -3
  98. package/dist/v3/prod/index.d.ts +4 -3
  99. package/dist/v3/prod/index.js +7 -131
  100. package/dist/v3/prod/index.js.map +1 -1
  101. package/dist/v3/prod/index.mjs +7 -131
  102. package/dist/v3/prod/index.mjs.map +1 -1
  103. package/dist/v3/schemas/index.d.mts +4807 -0
  104. package/dist/v3/schemas/index.d.ts +4807 -0
  105. package/dist/v3/schemas/index.js +2065 -0
  106. package/dist/v3/schemas/index.js.map +1 -0
  107. package/dist/v3/schemas/index.mjs +1924 -0
  108. package/dist/v3/schemas/index.mjs.map +1 -0
  109. package/dist/v3/semanticInternalAttributes.d.mts +56 -0
  110. package/dist/v3/semanticInternalAttributes.d.ts +56 -0
  111. package/dist/v3/semanticInternalAttributes.js +61 -0
  112. package/dist/v3/semanticInternalAttributes.js.map +1 -0
  113. package/dist/v3/semanticInternalAttributes.mjs +59 -0
  114. package/dist/v3/semanticInternalAttributes.mjs.map +1 -0
  115. package/dist/v3/utils/durations.d.mts +15 -0
  116. package/dist/v3/utils/durations.d.ts +15 -0
  117. package/dist/v3/utils/durations.js +93 -0
  118. package/dist/v3/utils/durations.js.map +1 -0
  119. package/dist/v3/utils/durations.mjs +82 -0
  120. package/dist/v3/utils/durations.mjs.map +1 -0
  121. package/dist/v3/utils/flattenAttributes.d.mts +8 -0
  122. package/dist/v3/utils/flattenAttributes.d.ts +8 -0
  123. package/dist/v3/utils/flattenAttributes.js +131 -0
  124. package/dist/v3/utils/flattenAttributes.js.map +1 -0
  125. package/dist/v3/utils/flattenAttributes.mjs +126 -0
  126. package/dist/v3/utils/flattenAttributes.mjs.map +1 -0
  127. package/dist/v3/utils/ioSerialization.d.mts +21 -0
  128. package/dist/v3/utils/ioSerialization.d.ts +21 -0
  129. package/dist/v3/utils/ioSerialization.js +2403 -0
  130. package/dist/v3/utils/ioSerialization.js.map +1 -0
  131. package/dist/v3/utils/ioSerialization.mjs +2394 -0
  132. package/dist/v3/utils/ioSerialization.mjs.map +1 -0
  133. package/dist/v3/utils/omit.d.mts +3 -0
  134. package/dist/v3/utils/omit.d.ts +3 -0
  135. package/dist/v3/utils/omit.js +20 -0
  136. package/dist/v3/utils/omit.js.map +1 -0
  137. package/dist/v3/utils/omit.mjs +18 -0
  138. package/dist/v3/utils/omit.mjs.map +1 -0
  139. package/dist/v3/utils/retries.d.mts +46 -0
  140. package/dist/v3/utils/retries.d.ts +46 -0
  141. package/dist/v3/utils/retries.js +118 -0
  142. package/dist/v3/utils/retries.js.map +1 -0
  143. package/dist/v3/utils/retries.mjs +113 -0
  144. package/dist/v3/utils/retries.mjs.map +1 -0
  145. package/dist/v3/utils/timers.d.mts +6 -0
  146. package/dist/v3/utils/timers.d.ts +6 -0
  147. package/dist/v3/utils/timers.js +31 -0
  148. package/dist/v3/utils/timers.js.map +1 -0
  149. package/dist/v3/utils/timers.mjs +28 -0
  150. package/dist/v3/utils/timers.mjs.map +1 -0
  151. package/dist/v3/workers/index.d.mts +56 -8
  152. package/dist/v3/workers/index.d.ts +56 -8
  153. package/dist/v3/workers/index.js +755 -279
  154. package/dist/v3/workers/index.js.map +1 -1
  155. package/dist/v3/workers/index.mjs +753 -280
  156. package/dist/v3/workers/index.mjs.map +1 -1
  157. package/dist/v3/zodIpc.js.map +1 -1
  158. package/dist/v3/zodIpc.mjs.map +1 -1
  159. package/dist/v3/zodMessageHandler.d.mts +16 -3
  160. package/dist/v3/zodMessageHandler.d.ts +16 -3
  161. package/dist/v3/zodMessageHandler.js +77 -23
  162. package/dist/v3/zodMessageHandler.js.map +1 -1
  163. package/dist/v3/zodMessageHandler.mjs +77 -23
  164. package/dist/v3/zodMessageHandler.mjs.map +1 -1
  165. package/dist/v3/zodNamespace.js +59 -28
  166. package/dist/v3/zodNamespace.js.map +1 -1
  167. package/dist/v3/zodNamespace.mjs +60 -29
  168. package/dist/v3/zodNamespace.mjs.map +1 -1
  169. package/dist/v3/zodSocket.d.mts +8 -3
  170. package/dist/v3/zodSocket.d.ts +8 -3
  171. package/dist/v3/zodSocket.js +56 -25
  172. package/dist/v3/zodSocket.js.map +1 -1
  173. package/dist/v3/zodSocket.mjs +57 -26
  174. package/dist/v3/zodSocket.mjs.map +1 -1
  175. package/dist/v3/zodfetch.d.mts +15 -58
  176. package/dist/v3/zodfetch.d.ts +15 -58
  177. package/dist/v3/zodfetch.js +246 -156
  178. package/dist/v3/zodfetch.js.map +1 -1
  179. package/dist/v3/zodfetch.mjs +244 -149
  180. package/dist/v3/zodfetch.mjs.map +1 -1
  181. package/dist/versions.d.mts +11 -0
  182. package/dist/versions.d.ts +11 -0
  183. package/dist/versions.js +31 -0
  184. package/dist/versions.js.map +1 -0
  185. package/dist/versions.mjs +27 -0
  186. package/dist/versions.mjs.map +1 -0
  187. package/package.json +154 -10
@@ -2,8 +2,6 @@
2
2
 
3
3
  var zod = require('zod');
4
4
  var zodValidationError = require('zod-validation-error');
5
- var formDataEncoder = require('form-data-encoder');
6
- var stream = require('stream');
7
5
 
8
6
  var __defProp = Object.defineProperty;
9
7
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -26,10 +24,34 @@ var __privateMethod = (obj, member, method) => {
26
24
  return method;
27
25
  };
28
26
 
27
+ // src/v3/utils/retries.ts
28
+ var defaultRetryOptions = {
29
+ maxAttempts: 3,
30
+ factor: 2,
31
+ minTimeoutInMs: 1e3,
32
+ maxTimeoutInMs: 6e4,
33
+ randomize: true
34
+ };
35
+ function calculateNextRetryDelay(options, attempt) {
36
+ const opts = {
37
+ ...defaultRetryOptions,
38
+ ...options
39
+ };
40
+ if (attempt >= opts.maxAttempts) {
41
+ return;
42
+ }
43
+ const { factor, minTimeoutInMs, maxTimeoutInMs, randomize } = opts;
44
+ const random = randomize ? Math.random() + 1 : 1;
45
+ const timeout = Math.min(maxTimeoutInMs, random * minTimeoutInMs * Math.pow(factor, attempt - 1));
46
+ return Math.round(timeout);
47
+ }
48
+ __name(calculateNextRetryDelay, "calculateNextRetryDelay");
49
+
29
50
  // src/v3/apiClient/errors.ts
30
51
  var _ApiError = class _ApiError extends Error {
31
52
  constructor(status, error, message, headers) {
32
53
  super(`${_ApiError.makeMessage(status, error, message)}`);
54
+ this.name = "TriggerApiError";
33
55
  this.status = status;
34
56
  this.headers = headers;
35
57
  const data = error;
@@ -150,6 +172,16 @@ var _RateLimitError = class _RateLimitError extends ApiError {
150
172
  super(...arguments);
151
173
  __publicField(this, "status", 429);
152
174
  }
175
+ get millisecondsUntilReset() {
176
+ const resetAtUnixEpochMs = (this.headers ?? {})["x-ratelimit-reset"];
177
+ if (typeof resetAtUnixEpochMs === "string") {
178
+ const resetAtUnixEpoch = parseInt(resetAtUnixEpochMs, 10);
179
+ if (isNaN(resetAtUnixEpoch)) {
180
+ return;
181
+ }
182
+ return Math.max(resetAtUnixEpoch - Date.now() + Math.floor(Math.random() * 2e3), 0);
183
+ }
184
+ }
153
185
  };
154
186
  __name(_RateLimitError, "RateLimitError");
155
187
  var RateLimitError = _RateLimitError;
@@ -164,28 +196,122 @@ function castToError(err) {
164
196
  }
165
197
  __name(castToError, "castToError");
166
198
 
167
- // src/v3/utils/retries.ts
168
- var defaultRetryOptions = {
169
- maxAttempts: 3,
170
- factor: 2,
171
- minTimeoutInMs: 1e3,
172
- maxTimeoutInMs: 6e4,
173
- randomize: true
199
+ // src/v3/semanticInternalAttributes.ts
200
+ var SemanticInternalAttributes = {
201
+ ENVIRONMENT_ID: "ctx.environment.id",
202
+ ENVIRONMENT_TYPE: "ctx.environment.type",
203
+ ORGANIZATION_ID: "ctx.organization.id",
204
+ ORGANIZATION_SLUG: "ctx.organization.slug",
205
+ ORGANIZATION_NAME: "ctx.organization.name",
206
+ PROJECT_ID: "ctx.project.id",
207
+ PROJECT_REF: "ctx.project.ref",
208
+ PROJECT_NAME: "ctx.project.title",
209
+ PROJECT_DIR: "project.dir",
210
+ ATTEMPT_ID: "ctx.attempt.id",
211
+ ATTEMPT_NUMBER: "ctx.attempt.number",
212
+ RUN_ID: "ctx.run.id",
213
+ RUN_IS_TEST: "ctx.run.isTest",
214
+ BATCH_ID: "ctx.batch.id",
215
+ TASK_SLUG: "ctx.task.id",
216
+ TASK_PATH: "ctx.task.filePath",
217
+ TASK_EXPORT_NAME: "ctx.task.exportName",
218
+ QUEUE_NAME: "ctx.queue.name",
219
+ QUEUE_ID: "ctx.queue.id",
220
+ MACHINE_PRESET_NAME: "ctx.machine.name",
221
+ MACHINE_PRESET_CPU: "ctx.machine.cpu",
222
+ MACHINE_PRESET_MEMORY: "ctx.machine.memory",
223
+ MACHINE_PRESET_CENTS_PER_MS: "ctx.machine.centsPerMs",
224
+ SPAN_PARTIAL: "$span.partial",
225
+ SPAN_ID: "$span.span_id",
226
+ OUTPUT: "$output",
227
+ OUTPUT_TYPE: "$mime_type_output",
228
+ STYLE: "$style",
229
+ STYLE_ICON: "$style.icon",
230
+ STYLE_VARIANT: "$style.variant",
231
+ STYLE_ACCESSORY: "$style.accessory",
232
+ METADATA: "$metadata",
233
+ TRIGGER: "$trigger",
234
+ PAYLOAD: "$payload",
235
+ PAYLOAD_TYPE: "$mime_type_payload",
236
+ SHOW: "$show",
237
+ SHOW_ACTIONS: "$show.actions",
238
+ WORKER_ID: "worker.id",
239
+ WORKER_VERSION: "worker.version",
240
+ CLI_VERSION: "cli.version",
241
+ SDK_VERSION: "sdk.version",
242
+ SDK_LANGUAGE: "sdk.language",
243
+ RETRY_AT: "retry.at",
244
+ RETRY_DELAY: "retry.delay",
245
+ RETRY_COUNT: "retry.count",
246
+ LINK_TITLE: "$link.title",
247
+ IDEMPOTENCY_KEY: "ctx.run.idempotencyKey",
248
+ USAGE_DURATION_MS: "$usage.durationMs",
249
+ USAGE_COST_IN_CENTS: "$usage.costInCents",
250
+ RATE_LIMIT_LIMIT: "response.rateLimit.limit",
251
+ RATE_LIMIT_REMAINING: "response.rateLimit.remaining",
252
+ RATE_LIMIT_RESET: "response.rateLimit.reset"
174
253
  };
175
- function calculateNextRetryDelay(options, attempt) {
176
- const opts = {
177
- ...defaultRetryOptions,
178
- ...options
179
- };
180
- if (attempt >= opts.maxAttempts) {
181
- return;
254
+
255
+ // src/v3/utils/flattenAttributes.ts
256
+ var NULL_SENTINEL = "$@null((";
257
+ function flattenAttributes(obj, prefix) {
258
+ const result = {};
259
+ if (obj === void 0) {
260
+ return result;
261
+ }
262
+ if (obj === null) {
263
+ result[prefix || ""] = NULL_SENTINEL;
264
+ return result;
265
+ }
266
+ if (typeof obj === "string") {
267
+ result[prefix || ""] = obj;
268
+ return result;
269
+ }
270
+ if (typeof obj === "number") {
271
+ result[prefix || ""] = obj;
272
+ return result;
273
+ }
274
+ if (typeof obj === "boolean") {
275
+ result[prefix || ""] = obj;
276
+ return result;
277
+ }
278
+ for (const [key, value] of Object.entries(obj)) {
279
+ const newPrefix = `${prefix ? `${prefix}.` : ""}${Array.isArray(obj) ? `[${key}]` : key}`;
280
+ if (Array.isArray(value)) {
281
+ for (let i = 0; i < value.length; i++) {
282
+ if (typeof value[i] === "object" && value[i] !== null) {
283
+ Object.assign(result, flattenAttributes(value[i], `${newPrefix}.[${i}]`));
284
+ } else {
285
+ if (value[i] === null) {
286
+ result[`${newPrefix}.[${i}]`] = NULL_SENTINEL;
287
+ } else {
288
+ result[`${newPrefix}.[${i}]`] = value[i];
289
+ }
290
+ }
291
+ }
292
+ } else if (isRecord(value)) {
293
+ Object.assign(result, flattenAttributes(value, newPrefix));
294
+ } else {
295
+ if (typeof value === "number" || typeof value === "string" || typeof value === "boolean") {
296
+ result[newPrefix] = value;
297
+ } else if (value === null) {
298
+ result[newPrefix] = NULL_SENTINEL;
299
+ }
300
+ }
182
301
  }
183
- const { factor, minTimeoutInMs, maxTimeoutInMs, randomize } = opts;
184
- const random = randomize ? Math.random() + 1 : 1;
185
- const timeout = Math.min(maxTimeoutInMs, random * minTimeoutInMs * Math.pow(factor, attempt - 1));
186
- return Math.round(timeout);
302
+ return result;
187
303
  }
188
- __name(calculateNextRetryDelay, "calculateNextRetryDelay");
304
+ __name(flattenAttributes, "flattenAttributes");
305
+ function isRecord(value) {
306
+ return value !== null && typeof value === "object" && !Array.isArray(value);
307
+ }
308
+ __name(isRecord, "isRecord");
309
+
310
+ // src/v3/utils/styleAttributes.ts
311
+ function accessoryAttributes(accessory) {
312
+ return flattenAttributes(accessory, SemanticInternalAttributes.STYLE_ACCESSORY);
313
+ }
314
+ __name(accessoryAttributes, "accessoryAttributes");
189
315
 
190
316
  // src/v3/apiClient/pagination.ts
191
317
  var _CursorPage = class _CursorPage {
@@ -295,6 +421,12 @@ var defaultRetryOptions2 = {
295
421
  maxTimeoutInMs: 6e4,
296
422
  randomize: false
297
423
  };
424
+ var requestOptionsKeys = {
425
+ retry: true
426
+ };
427
+ var isRequestOptions = /* @__PURE__ */ __name((obj) => {
428
+ return typeof obj === "object" && obj !== null && !isEmptyObj(obj) && Object.keys(obj).every((k) => hasOwn(requestOptionsKeys, k));
429
+ }, "isRequestOptions");
298
430
  function zodfetch(schema, url, requestInit, options) {
299
431
  return new ApiPromise(_doZodFetch(schema, url, requestInit, options));
300
432
  }
@@ -345,47 +477,47 @@ function zodfetchOffsetLimitPage(schema, url, params, requestInit, options) {
345
477
  return new OffsetLimitPagePromise(fetchResult, schema, url, params, requestInit, options);
346
478
  }
347
479
  __name(zodfetchOffsetLimitPage, "zodfetchOffsetLimitPage");
348
- function zodupload(schema, url, body, requestInit, options) {
349
- const finalRequestInit = createMultipartFormRequestInit(body, requestInit);
350
- return new ApiPromise(_doZodFetch(schema, url, finalRequestInit, options));
480
+ async function traceZodFetch(params, callback) {
481
+ if (!params.options?.tracer) {
482
+ return callback();
483
+ }
484
+ const url = new URL(params.url);
485
+ const method = params.requestInit?.method ?? "GET";
486
+ const name = params.options.name ?? `${method} ${url.pathname}`;
487
+ return await params.options.tracer.startActiveSpan(name, async (span) => {
488
+ return await callback(span);
489
+ }, {
490
+ attributes: {
491
+ [SemanticInternalAttributes.STYLE_ICON]: params.options?.icon ?? "api",
492
+ ...params.options.attributes
493
+ }
494
+ });
351
495
  }
352
- __name(zodupload, "zodupload");
353
- async function createMultipartFormRequestInit(body, requestInit) {
354
- const form = await createForm(body);
355
- const encoder = new formDataEncoder.FormDataEncoder(form);
356
- const finalHeaders = {};
357
- for (const [key, value] of Object.entries(requestInit?.headers || {})) {
358
- finalHeaders[key] = value;
359
- }
360
- for (const [key, value] of Object.entries(encoder.headers)) {
361
- finalHeaders[key] = value;
362
- }
363
- finalHeaders["Content-Length"] = String(encoder.contentLength);
364
- const finalRequestInit = {
365
- ...requestInit,
366
- headers: finalHeaders,
367
- body: stream.Readable.from(encoder),
368
- // @ts-expect-error
369
- duplex: "half"
370
- };
371
- return finalRequestInit;
496
+ __name(traceZodFetch, "traceZodFetch");
497
+ async function _doZodFetch(schema, url, requestInit, options) {
498
+ const $requestInit = await requestInit;
499
+ return traceZodFetch({
500
+ url,
501
+ requestInit: $requestInit,
502
+ options
503
+ }, async (span) => {
504
+ const result = await _doZodFetchWithRetries(schema, url, $requestInit, options);
505
+ if (options?.onResponseBody && span) {
506
+ options.onResponseBody(result.data, span);
507
+ }
508
+ return result;
509
+ });
372
510
  }
373
- __name(createMultipartFormRequestInit, "createMultipartFormRequestInit");
374
- var createForm = /* @__PURE__ */ __name(async (body) => {
375
- const form = new FormData();
376
- await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
377
- return form;
378
- }, "createForm");
379
- async function _doZodFetch(schema, url, requestInit, options, attempt = 1) {
511
+ __name(_doZodFetch, "_doZodFetch");
512
+ async function _doZodFetchWithRetries(schema, url, requestInit, options, attempt = 1) {
380
513
  try {
381
- const $requestInit = await requestInit;
382
- const response = await fetch(url, requestInitWithCache($requestInit));
514
+ const response = await fetch(url, requestInitWithCache(requestInit));
383
515
  const responseHeaders = createResponseHeaders(response.headers);
384
516
  if (!response.ok) {
385
517
  const retryResult = shouldRetry(response, attempt, options?.retry);
386
518
  if (retryResult.retry) {
387
- await new Promise((resolve) => setTimeout(resolve, retryResult.delay));
388
- return await _doZodFetch(schema, url, requestInit, options, attempt + 1);
519
+ await waitForRetry(url, attempt + 1, retryResult.delay, options, requestInit, response);
520
+ return await _doZodFetchWithRetries(schema, url, requestInit, options, attempt + 1);
389
521
  } else {
390
522
  const errText = await response.text().catch((e) => castToError2(e).message);
391
523
  const errJSON = safeJsonParse(errText);
@@ -413,8 +545,8 @@ async function _doZodFetch(schema, url, requestInit, options, attempt = 1) {
413
545
  };
414
546
  const delay = calculateNextRetryDelay(retry, attempt);
415
547
  if (delay) {
416
- await new Promise((resolve) => setTimeout(resolve, delay));
417
- return await _doZodFetch(schema, url, requestInit, options, attempt + 1);
548
+ await waitForRetry(url, attempt + 1, delay, options, requestInit);
549
+ return await _doZodFetchWithRetries(schema, url, requestInit, options, attempt + 1);
418
550
  }
419
551
  }
420
552
  throw new ApiConnectionError({
@@ -422,7 +554,7 @@ async function _doZodFetch(schema, url, requestInit, options, attempt = 1) {
422
554
  });
423
555
  }
424
556
  }
425
- __name(_doZodFetch, "_doZodFetch");
557
+ __name(_doZodFetchWithRetries, "_doZodFetchWithRetries");
426
558
  function castToError2(err) {
427
559
  if (err instanceof Error)
428
560
  return err;
@@ -459,8 +591,25 @@ function shouldRetry(response, attempt, retryOptions) {
459
591
  return shouldRetryForOptions();
460
592
  if (response.status === 409)
461
593
  return shouldRetryForOptions();
462
- if (response.status === 429)
594
+ if (response.status === 429) {
595
+ if (attempt >= (typeof retryOptions?.maxAttempts === "number" ? retryOptions?.maxAttempts : 3)) {
596
+ return {
597
+ retry: false
598
+ };
599
+ }
600
+ const resetAtUnixEpochMs = response.headers.get("x-ratelimit-reset");
601
+ if (resetAtUnixEpochMs) {
602
+ const resetAtUnixEpoch = parseInt(resetAtUnixEpochMs, 10);
603
+ const delay = resetAtUnixEpoch - Date.now() + Math.floor(Math.random() * 1e3);
604
+ if (delay > 0) {
605
+ return {
606
+ retry: true,
607
+ delay
608
+ };
609
+ }
610
+ }
463
611
  return shouldRetryForOptions();
612
+ }
464
613
  if (response.status >= 500)
465
614
  return shouldRetryForOptions();
466
615
  return {
@@ -501,95 +650,6 @@ function requestInitWithCache(requestInit) {
501
650
  }
502
651
  }
503
652
  __name(requestInitWithCache, "requestInitWithCache");
504
- var addFormValue = /* @__PURE__ */ __name(async (form, key, value) => {
505
- if (value === void 0)
506
- return;
507
- if (value == null) {
508
- throw new TypeError(`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`);
509
- }
510
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
511
- form.append(key, String(value));
512
- } else if (isUploadable(value) || isBlobLike(value) || value instanceof Buffer || value instanceof ArrayBuffer) {
513
- const file = await toFile(value);
514
- form.append(key, file);
515
- } else if (Array.isArray(value)) {
516
- await Promise.all(value.map((entry) => addFormValue(form, key + "[]", entry)));
517
- } else if (typeof value === "object") {
518
- await Promise.all(Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)));
519
- } else {
520
- throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`);
521
- }
522
- }, "addFormValue");
523
- async function toFile(value, name, options) {
524
- value = await value;
525
- options ??= isFileLike(value) ? {
526
- lastModified: value.lastModified,
527
- type: value.type
528
- } : {};
529
- if (isResponseLike(value)) {
530
- const blob = await value.blob();
531
- name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? "unknown_file";
532
- return new File([
533
- blob
534
- ], name, options);
535
- }
536
- const bits = await getBytes(value);
537
- name ||= getName(value) ?? "unknown_file";
538
- if (!options.type) {
539
- const type = bits[0]?.type;
540
- if (typeof type === "string") {
541
- options = {
542
- ...options,
543
- type
544
- };
545
- }
546
- }
547
- return new File(bits, name, options);
548
- }
549
- __name(toFile, "toFile");
550
- function getName(value) {
551
- return getStringFromMaybeBuffer(value.name) || getStringFromMaybeBuffer(value.filename) || // For fs.ReadStream
552
- getStringFromMaybeBuffer(value.path)?.split(/[\\/]/).pop();
553
- }
554
- __name(getName, "getName");
555
- var getStringFromMaybeBuffer = /* @__PURE__ */ __name((x) => {
556
- if (typeof x === "string")
557
- return x;
558
- if (typeof Buffer !== "undefined" && x instanceof Buffer)
559
- return String(x);
560
- return void 0;
561
- }, "getStringFromMaybeBuffer");
562
- async function getBytes(value) {
563
- let parts = [];
564
- if (typeof value === "string" || ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
565
- value instanceof ArrayBuffer) {
566
- parts.push(value);
567
- } else if (isBlobLike(value)) {
568
- parts.push(await value.arrayBuffer());
569
- } else if (isAsyncIterableIterator(value)) {
570
- for await (const chunk of value) {
571
- parts.push(chunk);
572
- }
573
- } else {
574
- throw new Error(`Unexpected data type: ${typeof value}; constructor: ${value?.constructor?.name}; props: ${propsForError(value)}`);
575
- }
576
- return parts;
577
- }
578
- __name(getBytes, "getBytes");
579
- function propsForError(value) {
580
- const props = Object.getOwnPropertyNames(value);
581
- return `[${props.map((p) => `"${p}"`).join(", ")}]`;
582
- }
583
- __name(propsForError, "propsForError");
584
- var isAsyncIterableIterator = /* @__PURE__ */ __name((value) => value != null && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function", "isAsyncIterableIterator");
585
- var isResponseLike = /* @__PURE__ */ __name((value) => value != null && typeof value === "object" && typeof value.url === "string" && typeof value.blob === "function", "isResponseLike");
586
- var isFileLike = /* @__PURE__ */ __name((value) => value != null && typeof value === "object" && typeof value.name === "string" && typeof value.lastModified === "number" && isBlobLike(value), "isFileLike");
587
- var isBlobLike = /* @__PURE__ */ __name((value) => value != null && typeof value === "object" && typeof value.size === "number" && typeof value.type === "string" && typeof value.text === "function" && typeof value.slice === "function" && typeof value.arrayBuffer === "function", "isBlobLike");
588
- var isFsReadStream = /* @__PURE__ */ __name((value) => value instanceof stream.Readable, "isFsReadStream");
589
- var isUploadable = /* @__PURE__ */ __name((value) => {
590
- return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);
591
- }, "isUploadable");
592
- var isRecordLike = /* @__PURE__ */ __name((value) => value != null && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length > 0 && Object.keys(value).every((key) => typeof key === "string" && typeof value[key] === "string"), "isRecordLike");
593
653
  var _ApiPromise = class _ApiPromise extends Promise {
594
654
  constructor(responsePromise) {
595
655
  super((resolve) => {
@@ -712,6 +772,41 @@ fetchPage_fn2 = /* @__PURE__ */ __name(function(params1) {
712
772
  }, "#fetchPage");
713
773
  __name(_OffsetLimitPagePromise, "OffsetLimitPagePromise");
714
774
  var OffsetLimitPagePromise = _OffsetLimitPagePromise;
775
+ async function waitForRetry(url, attempt, delay, options, requestInit, response) {
776
+ if (options?.tracer) {
777
+ const method = requestInit?.method ?? "GET";
778
+ return options.tracer.startActiveSpan(response ? `wait after ${response.status}` : `wait after error`, async (span) => {
779
+ await new Promise((resolve) => setTimeout(resolve, delay));
780
+ }, {
781
+ attributes: {
782
+ [SemanticInternalAttributes.STYLE_ICON]: "wait",
783
+ ...accessoryAttributes({
784
+ items: [
785
+ {
786
+ text: `retrying ${options?.name ?? method.toUpperCase()} in ${delay}ms`,
787
+ variant: "normal"
788
+ }
789
+ ],
790
+ style: "codepath"
791
+ })
792
+ }
793
+ });
794
+ }
795
+ await new Promise((resolve) => setTimeout(resolve, delay));
796
+ }
797
+ __name(waitForRetry, "waitForRetry");
798
+ function isEmptyObj(obj) {
799
+ if (!obj)
800
+ return true;
801
+ for (const _k in obj)
802
+ return false;
803
+ return true;
804
+ }
805
+ __name(isEmptyObj, "isEmptyObj");
806
+ function hasOwn(obj, key) {
807
+ return Object.prototype.hasOwnProperty.call(obj, key);
808
+ }
809
+ __name(hasOwn, "hasOwn");
715
810
 
716
811
  exports.ApiConnectionError = ApiConnectionError;
717
812
  exports.ApiError = ApiError;
@@ -729,16 +824,11 @@ exports.PermissionDeniedError = PermissionDeniedError;
729
824
  exports.RateLimitError = RateLimitError;
730
825
  exports.UnprocessableEntityError = UnprocessableEntityError;
731
826
  exports.defaultRetryOptions = defaultRetryOptions2;
732
- exports.isBlobLike = isBlobLike;
733
- exports.isFileLike = isFileLike;
734
- exports.isFsReadStream = isFsReadStream;
735
- exports.isRecordLike = isRecordLike;
736
- exports.isResponseLike = isResponseLike;
737
- exports.isUploadable = isUploadable;
738
- exports.toFile = toFile;
827
+ exports.hasOwn = hasOwn;
828
+ exports.isEmptyObj = isEmptyObj;
829
+ exports.isRequestOptions = isRequestOptions;
739
830
  exports.zodfetch = zodfetch;
740
831
  exports.zodfetchCursorPage = zodfetchCursorPage;
741
832
  exports.zodfetchOffsetLimitPage = zodfetchOffsetLimitPage;
742
- exports.zodupload = zodupload;
743
833
  //# sourceMappingURL=out.js.map
744
834
  //# sourceMappingURL=zodfetch.js.map