@trigger.dev/sdk 3.3.17 → 4.0.0-v4-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/v3/ai.d.ts +23 -0
- package/dist/commonjs/v3/ai.js +50 -0
- package/dist/commonjs/v3/ai.js.map +1 -0
- package/dist/commonjs/v3/auth.d.ts +4 -3
- package/dist/commonjs/v3/auth.js.map +1 -1
- package/dist/commonjs/v3/hooks.d.ts +22 -0
- package/dist/commonjs/v3/hooks.js +65 -0
- package/dist/commonjs/v3/hooks.js.map +1 -0
- package/dist/commonjs/v3/index.d.ts +3 -1
- package/dist/commonjs/v3/index.js +4 -1
- package/dist/commonjs/v3/index.js.map +1 -1
- package/dist/commonjs/v3/locals.d.ts +3 -0
- package/dist/commonjs/v3/locals.js +6 -0
- package/dist/commonjs/v3/locals.js.map +1 -0
- package/dist/commonjs/v3/queues.d.ts +61 -0
- package/dist/commonjs/v3/queues.js +137 -0
- package/dist/commonjs/v3/queues.js.map +1 -0
- package/dist/commonjs/v3/retry.js +10 -7
- package/dist/commonjs/v3/retry.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +40 -40
- package/dist/commonjs/v3/schedules/index.js +1 -1
- package/dist/commonjs/v3/schedules/index.js.map +1 -1
- package/dist/commonjs/v3/shared.d.ts +7 -6
- package/dist/commonjs/v3/shared.js +188 -176
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/v3/tasks.d.ts +12 -0
- package/dist/commonjs/v3/tasks.js +11 -0
- package/dist/commonjs/v3/tasks.js.map +1 -1
- package/dist/commonjs/v3/wait.d.ts +208 -3
- package/dist/commonjs/v3/wait.js +407 -7
- package/dist/commonjs/v3/wait.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +23 -0
- package/dist/esm/v3/ai.js +47 -0
- package/dist/esm/v3/ai.js.map +1 -0
- package/dist/esm/v3/auth.d.ts +4 -3
- package/dist/esm/v3/auth.js.map +1 -1
- package/dist/esm/v3/hooks.d.ts +22 -0
- package/dist/esm/v3/hooks.js +54 -0
- package/dist/esm/v3/hooks.js.map +1 -0
- package/dist/esm/v3/index.d.ts +3 -1
- package/dist/esm/v3/index.js +3 -1
- package/dist/esm/v3/index.js.map +1 -1
- package/dist/esm/v3/locals.d.ts +3 -0
- package/dist/esm/v3/locals.js +3 -0
- package/dist/esm/v3/locals.js.map +1 -0
- package/dist/esm/v3/queues.d.ts +61 -0
- package/dist/esm/v3/queues.js +131 -0
- package/dist/esm/v3/queues.js.map +1 -0
- package/dist/esm/v3/retry.js +11 -8
- package/dist/esm/v3/retry.js.map +1 -1
- package/dist/esm/v3/runs.d.ts +39 -39
- package/dist/esm/v3/schedules/index.js +2 -2
- package/dist/esm/v3/schedules/index.js.map +1 -1
- package/dist/esm/v3/shared.d.ts +7 -6
- package/dist/esm/v3/shared.js +189 -177
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/v3/tasks.d.ts +12 -0
- package/dist/esm/v3/tasks.js +11 -0
- package/dist/esm/v3/tasks.js.map +1 -1
- package/dist/esm/v3/wait.d.ts +208 -3
- package/dist/esm/v3/wait.js +406 -7
- package/dist/esm/v3/wait.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +36 -17
- package/dist/commonjs/apiClient.d.ts +0 -727
- package/dist/commonjs/apiClient.js +0 -692
- package/dist/commonjs/apiClient.js.map +0 -1
- package/dist/commonjs/concurrencyLimit.d.ts +0 -10
- package/dist/commonjs/concurrencyLimit.js +0 -17
- package/dist/commonjs/concurrencyLimit.js.map +0 -1
- package/dist/commonjs/errors.d.ts +0 -66
- package/dist/commonjs/errors.js +0 -109
- package/dist/commonjs/errors.js.map +0 -1
- package/dist/commonjs/httpEndpoint.d.ts +0 -87
- package/dist/commonjs/httpEndpoint.js +0 -130
- package/dist/commonjs/httpEndpoint.js.map +0 -1
- package/dist/commonjs/index.d.ts +0 -23
- package/dist/commonjs/index.js +0 -55
- package/dist/commonjs/index.js.map +0 -1
- package/dist/commonjs/integrations.d.ts +0 -11
- package/dist/commonjs/integrations.js +0 -3
- package/dist/commonjs/integrations.js.map +0 -1
- package/dist/commonjs/io.d.ts +0 -441
- package/dist/commonjs/io.js +0 -1165
- package/dist/commonjs/io.js.map +0 -1
- package/dist/commonjs/ioWithIntegrations.d.ts +0 -4
- package/dist/commonjs/ioWithIntegrations.js +0 -32
- package/dist/commonjs/ioWithIntegrations.js.map +0 -1
- package/dist/commonjs/job.d.ts +0 -96
- package/dist/commonjs/job.js +0 -210
- package/dist/commonjs/job.js.map +0 -1
- package/dist/commonjs/retry.d.ts +0 -20
- package/dist/commonjs/retry.js +0 -22
- package/dist/commonjs/retry.js.map +0 -1
- package/dist/commonjs/runLocalStorage.d.ts +0 -8
- package/dist/commonjs/runLocalStorage.js +0 -6
- package/dist/commonjs/runLocalStorage.js.map +0 -1
- package/dist/commonjs/security.d.ts +0 -18
- package/dist/commonjs/security.js +0 -41
- package/dist/commonjs/security.js.map +0 -1
- package/dist/commonjs/status.d.ts +0 -19
- package/dist/commonjs/status.js +0 -38
- package/dist/commonjs/status.js.map +0 -1
- package/dist/commonjs/store/keyValueStore.d.ts +0 -17
- package/dist/commonjs/store/keyValueStore.js +0 -138
- package/dist/commonjs/store/keyValueStore.js.map +0 -1
- package/dist/commonjs/store/keyValueStoreClient.d.ts +0 -19
- package/dist/commonjs/store/keyValueStoreClient.js +0 -65
- package/dist/commonjs/store/keyValueStoreClient.js.map +0 -1
- package/dist/commonjs/triggerClient.d.ts +0 -290
- package/dist/commonjs/triggerClient.js +0 -1366
- package/dist/commonjs/triggerClient.js.map +0 -1
- package/dist/commonjs/triggers/dynamic.d.ts +0 -56
- package/dist/commonjs/triggers/dynamic.js +0 -97
- package/dist/commonjs/triggers/dynamic.js.map +0 -1
- package/dist/commonjs/triggers/eventTrigger.d.ts +0 -58
- package/dist/commonjs/triggers/eventTrigger.js +0 -69
- package/dist/commonjs/triggers/eventTrigger.js.map +0 -1
- package/dist/commonjs/triggers/externalSource.d.ts +0 -148
- package/dist/commonjs/triggers/externalSource.js +0 -106
- package/dist/commonjs/triggers/externalSource.js.map +0 -1
- package/dist/commonjs/triggers/invokeTrigger.d.ts +0 -39
- package/dist/commonjs/triggers/invokeTrigger.js +0 -58
- package/dist/commonjs/triggers/invokeTrigger.js.map +0 -1
- package/dist/commonjs/triggers/notifications.d.ts +0 -111
- package/dist/commonjs/triggers/notifications.js +0 -101
- package/dist/commonjs/triggers/notifications.js.map +0 -1
- package/dist/commonjs/triggers/scheduled.d.ts +0 -145
- package/dist/commonjs/triggers/scheduled.js +0 -208
- package/dist/commonjs/triggers/scheduled.js.map +0 -1
- package/dist/commonjs/triggers/webhook.d.ts +0 -143
- package/dist/commonjs/triggers/webhook.js +0 -133
- package/dist/commonjs/triggers/webhook.js.map +0 -1
- package/dist/commonjs/typed-emitter.d.ts +0 -37
- package/dist/commonjs/typed-emitter.js +0 -3
- package/dist/commonjs/typed-emitter.js.map +0 -1
- package/dist/commonjs/types.d.ts +0 -204
- package/dist/commonjs/types.js +0 -23
- package/dist/commonjs/types.js.map +0 -1
- package/dist/commonjs/utils/formatSchemaErrors.d.ts +0 -3
- package/dist/commonjs/utils/formatSchemaErrors.js +0 -10
- package/dist/commonjs/utils/formatSchemaErrors.js.map +0 -1
- package/dist/commonjs/utils/typedAsyncLocalStorage.d.ts +0 -6
- package/dist/commonjs/utils/typedAsyncLocalStorage.js +0 -18
- package/dist/commonjs/utils/typedAsyncLocalStorage.js.map +0 -1
- package/dist/commonjs/utils.d.ts +0 -1
- package/dist/commonjs/utils.js +0 -11
- package/dist/commonjs/utils.js.map +0 -1
- package/dist/esm/apiClient.d.ts +0 -727
- package/dist/esm/apiClient.js +0 -687
- package/dist/esm/apiClient.js.map +0 -1
- package/dist/esm/concurrencyLimit.d.ts +0 -10
- package/dist/esm/concurrencyLimit.js +0 -13
- package/dist/esm/concurrencyLimit.js.map +0 -1
- package/dist/esm/errors.d.ts +0 -66
- package/dist/esm/errors.js +0 -95
- package/dist/esm/errors.js.map +0 -1
- package/dist/esm/httpEndpoint.d.ts +0 -87
- package/dist/esm/httpEndpoint.js +0 -125
- package/dist/esm/httpEndpoint.js.map +0 -1
- package/dist/esm/index.d.ts +0 -23
- package/dist/esm/index.js +0 -35
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/integrations.d.ts +0 -11
- package/dist/esm/integrations.js +0 -2
- package/dist/esm/integrations.js.map +0 -1
- package/dist/esm/io.d.ts +0 -441
- package/dist/esm/io.js +0 -1159
- package/dist/esm/io.js.map +0 -1
- package/dist/esm/ioWithIntegrations.d.ts +0 -4
- package/dist/esm/ioWithIntegrations.js +0 -29
- package/dist/esm/ioWithIntegrations.js.map +0 -1
- package/dist/esm/job.d.ts +0 -96
- package/dist/esm/job.js +0 -206
- package/dist/esm/job.js.map +0 -1
- package/dist/esm/retry.d.ts +0 -20
- package/dist/esm/retry.js +0 -19
- package/dist/esm/retry.js.map +0 -1
- package/dist/esm/runLocalStorage.d.ts +0 -8
- package/dist/esm/runLocalStorage.js +0 -3
- package/dist/esm/runLocalStorage.js.map +0 -1
- package/dist/esm/security.d.ts +0 -18
- package/dist/esm/security.js +0 -34
- package/dist/esm/security.js.map +0 -1
- package/dist/esm/status.d.ts +0 -19
- package/dist/esm/status.js +0 -34
- package/dist/esm/status.js.map +0 -1
- package/dist/esm/store/keyValueStore.d.ts +0 -17
- package/dist/esm/store/keyValueStore.js +0 -134
- package/dist/esm/store/keyValueStore.js.map +0 -1
- package/dist/esm/store/keyValueStoreClient.d.ts +0 -19
- package/dist/esm/store/keyValueStoreClient.js +0 -61
- package/dist/esm/store/keyValueStoreClient.js.map +0 -1
- package/dist/esm/triggerClient.d.ts +0 -288
- package/dist/esm/triggerClient.js +0 -1359
- package/dist/esm/triggerClient.js.map +0 -1
- package/dist/esm/triggers/dynamic.d.ts +0 -56
- package/dist/esm/triggers/dynamic.js +0 -93
- package/dist/esm/triggers/dynamic.js.map +0 -1
- package/dist/esm/triggers/eventTrigger.d.ts +0 -58
- package/dist/esm/triggers/eventTrigger.js +0 -64
- package/dist/esm/triggers/eventTrigger.js.map +0 -1
- package/dist/esm/triggers/externalSource.d.ts +0 -148
- package/dist/esm/triggers/externalSource.js +0 -100
- package/dist/esm/triggers/externalSource.js.map +0 -1
- package/dist/esm/triggers/invokeTrigger.d.ts +0 -39
- package/dist/esm/triggers/invokeTrigger.js +0 -53
- package/dist/esm/triggers/invokeTrigger.js.map +0 -1
- package/dist/esm/triggers/notifications.d.ts +0 -111
- package/dist/esm/triggers/notifications.js +0 -94
- package/dist/esm/triggers/notifications.js.map +0 -1
- package/dist/esm/triggers/scheduled.d.ts +0 -145
- package/dist/esm/triggers/scheduled.js +0 -197
- package/dist/esm/triggers/scheduled.js.map +0 -1
- package/dist/esm/triggers/webhook.d.ts +0 -143
- package/dist/esm/triggers/webhook.js +0 -128
- package/dist/esm/triggers/webhook.js.map +0 -1
- package/dist/esm/typed-emitter.d.ts +0 -37
- package/dist/esm/typed-emitter.js +0 -2
- package/dist/esm/typed-emitter.js.map +0 -1
- package/dist/esm/types.d.ts +0 -204
- package/dist/esm/types.js +0 -19
- package/dist/esm/types.js.map +0 -1
- package/dist/esm/utils/formatSchemaErrors.d.ts +0 -3
- package/dist/esm/utils/formatSchemaErrors.js +0 -7
- package/dist/esm/utils/formatSchemaErrors.js.map +0 -1
- package/dist/esm/utils/typedAsyncLocalStorage.d.ts +0 -6
- package/dist/esm/utils/typedAsyncLocalStorage.js +0 -14
- package/dist/esm/utils/typedAsyncLocalStorage.js.map +0 -1
- package/dist/esm/utils.d.ts +0 -1
- package/dist/esm/utils.js +0 -8
- package/dist/esm/utils.js.map +0 -1
package/dist/esm/apiClient.js
DELETED
|
@@ -1,687 +0,0 @@
|
|
|
1
|
-
import { API_VERSIONS, ApiEventLogSchema, CancelRunsForEventSchema, CancelRunsForJobSchema, ConnectionAuthSchema, EphemeralEventDispatcherResponseBodySchema, GetEventSchema, GetRunSchema, GetRunStatusesSchema, GetRunsSchema, InvokeJobResponseSchema, JobRunStatusRecordSchema, KeyValueStoreResponseBodySchema, RegisterScheduleResponseBodySchema, RegisterSourceEventSchemaV2, RunTaskResponseWithCachedTasksBodySchema, ServerTaskSchema, TriggerSourceSchema, assertExhaustive, urlWithSearchParams, } from "@trigger.dev/core";
|
|
2
|
-
import { Logger } from "@trigger.dev/core/logger";
|
|
3
|
-
import { env } from "node:process";
|
|
4
|
-
import { z } from "zod";
|
|
5
|
-
import { KeyValueStoreClient } from "./store/keyValueStoreClient.js";
|
|
6
|
-
import { AutoYieldRateLimitError } from "./errors.js";
|
|
7
|
-
export class UnknownVersionError extends Error {
|
|
8
|
-
constructor(version) {
|
|
9
|
-
super(`Unknown version ${version}`);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
const MAX_RETRIES = 8;
|
|
13
|
-
const EXPONENT_FACTOR = 2;
|
|
14
|
-
const MIN_DELAY_IN_MS = 80;
|
|
15
|
-
const MAX_DELAY_IN_MS = 2000;
|
|
16
|
-
const JITTER_IN_MS = 50;
|
|
17
|
-
export class ApiClient {
|
|
18
|
-
#apiUrl;
|
|
19
|
-
#options;
|
|
20
|
-
#logger;
|
|
21
|
-
#storeClient;
|
|
22
|
-
constructor(options) {
|
|
23
|
-
this.#options = options;
|
|
24
|
-
this.#apiUrl = this.#options.apiUrl ?? env.TRIGGER_API_URL ?? "https://api.trigger.dev";
|
|
25
|
-
this.#logger = new Logger("trigger.dev", this.#options.logLevel);
|
|
26
|
-
this.#storeClient = new KeyValueStoreClient(this.#queryKeyValueStore.bind(this));
|
|
27
|
-
}
|
|
28
|
-
async registerEndpoint(options) {
|
|
29
|
-
const apiKey = await this.#apiKey();
|
|
30
|
-
this.#logger.debug("Registering endpoint", {
|
|
31
|
-
url: options.url,
|
|
32
|
-
name: options.name,
|
|
33
|
-
});
|
|
34
|
-
const response = await fetch(`${this.#apiUrl}/api/v1/endpoints`, {
|
|
35
|
-
method: "POST",
|
|
36
|
-
headers: {
|
|
37
|
-
"Content-Type": "application/json",
|
|
38
|
-
Authorization: `Bearer ${apiKey}`,
|
|
39
|
-
},
|
|
40
|
-
body: JSON.stringify({
|
|
41
|
-
url: options.url,
|
|
42
|
-
name: options.name,
|
|
43
|
-
}),
|
|
44
|
-
});
|
|
45
|
-
if (response.status >= 400 && response.status < 500) {
|
|
46
|
-
const body = await response.json();
|
|
47
|
-
throw new Error(body.error);
|
|
48
|
-
}
|
|
49
|
-
if (response.status !== 200) {
|
|
50
|
-
throw new Error(`Failed to register entry point, got status code ${response.status}`);
|
|
51
|
-
}
|
|
52
|
-
return await response.json();
|
|
53
|
-
}
|
|
54
|
-
async runTask(runId, task, options = {}) {
|
|
55
|
-
const apiKey = await this.#apiKey();
|
|
56
|
-
this.#logger.debug(`[ApiClient] runTask ${task.displayKey}`);
|
|
57
|
-
return await zodfetchWithVersions(this.#logger, {
|
|
58
|
-
[API_VERSIONS.LAZY_LOADED_CACHED_TASKS]: RunTaskResponseWithCachedTasksBodySchema,
|
|
59
|
-
}, ServerTaskSchema, `${this.#apiUrl}/api/v1/runs/${runId}/tasks`, {
|
|
60
|
-
method: "POST",
|
|
61
|
-
headers: {
|
|
62
|
-
"Content-Type": "application/json",
|
|
63
|
-
Authorization: `Bearer ${apiKey}`,
|
|
64
|
-
"Idempotency-Key": task.idempotencyKey,
|
|
65
|
-
"X-Cached-Tasks-Cursor": options.cachedTasksCursor ?? "",
|
|
66
|
-
"Trigger-Version": API_VERSIONS.LAZY_LOADED_CACHED_TASKS,
|
|
67
|
-
},
|
|
68
|
-
body: JSON.stringify(task),
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
async completeTask(runId, id, task) {
|
|
72
|
-
const apiKey = await this.#apiKey();
|
|
73
|
-
this.#logger.debug("Complete Task", {
|
|
74
|
-
task,
|
|
75
|
-
});
|
|
76
|
-
return await zodfetch(ServerTaskSchema, `${this.#apiUrl}/api/v1/runs/${runId}/tasks/${id}/complete`, {
|
|
77
|
-
method: "POST",
|
|
78
|
-
headers: {
|
|
79
|
-
"Content-Type": "application/json",
|
|
80
|
-
Authorization: `Bearer ${apiKey}`,
|
|
81
|
-
"Trigger-Version": API_VERSIONS.SERIALIZED_TASK_OUTPUT,
|
|
82
|
-
},
|
|
83
|
-
body: JSON.stringify(task),
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
async failTask(runId, id, body) {
|
|
87
|
-
const apiKey = await this.#apiKey();
|
|
88
|
-
this.#logger.debug("Fail Task", {
|
|
89
|
-
id,
|
|
90
|
-
runId,
|
|
91
|
-
body,
|
|
92
|
-
});
|
|
93
|
-
return await zodfetch(ServerTaskSchema, `${this.#apiUrl}/api/v1/runs/${runId}/tasks/${id}/fail`, {
|
|
94
|
-
method: "POST",
|
|
95
|
-
headers: {
|
|
96
|
-
"Content-Type": "application/json",
|
|
97
|
-
Authorization: `Bearer ${apiKey}`,
|
|
98
|
-
},
|
|
99
|
-
body: JSON.stringify(body),
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
async sendEvent(event, options = {}) {
|
|
103
|
-
const apiKey = await this.#apiKey();
|
|
104
|
-
this.#logger.debug("Sending event", {
|
|
105
|
-
event,
|
|
106
|
-
});
|
|
107
|
-
return await zodfetch(ApiEventLogSchema, `${this.#apiUrl}/api/v1/events`, {
|
|
108
|
-
method: "POST",
|
|
109
|
-
headers: {
|
|
110
|
-
"Content-Type": "application/json",
|
|
111
|
-
Authorization: `Bearer ${apiKey}`,
|
|
112
|
-
},
|
|
113
|
-
body: JSON.stringify({ event, options }),
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
async sendEvents(events, options = {}) {
|
|
117
|
-
const apiKey = await this.#apiKey();
|
|
118
|
-
this.#logger.debug("Sending multiple events", {
|
|
119
|
-
events,
|
|
120
|
-
});
|
|
121
|
-
return await zodfetch(ApiEventLogSchema.array(), `${this.#apiUrl}/api/v1/events/bulk`, {
|
|
122
|
-
method: "POST",
|
|
123
|
-
headers: {
|
|
124
|
-
"Content-Type": "application/json",
|
|
125
|
-
Authorization: `Bearer ${apiKey}`,
|
|
126
|
-
},
|
|
127
|
-
body: JSON.stringify({ events, options }),
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
async cancelEvent(eventId) {
|
|
131
|
-
const apiKey = await this.#apiKey();
|
|
132
|
-
this.#logger.debug("Cancelling event", {
|
|
133
|
-
eventId,
|
|
134
|
-
});
|
|
135
|
-
return await zodfetch(ApiEventLogSchema, `${this.#apiUrl}/api/v1/events/${eventId}/cancel`, {
|
|
136
|
-
method: "POST",
|
|
137
|
-
headers: {
|
|
138
|
-
"Content-Type": "application/json",
|
|
139
|
-
Authorization: `Bearer ${apiKey}`,
|
|
140
|
-
},
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
async cancelRunsForEvent(eventId) {
|
|
144
|
-
const apiKey = await this.#apiKey();
|
|
145
|
-
this.#logger.debug("Cancelling runs for event", {
|
|
146
|
-
eventId,
|
|
147
|
-
});
|
|
148
|
-
return await zodfetch(CancelRunsForEventSchema, `${this.#apiUrl}/api/v1/events/${eventId}/cancel-runs`, {
|
|
149
|
-
method: "POST",
|
|
150
|
-
headers: {
|
|
151
|
-
"Content-Type": "application/json",
|
|
152
|
-
Authorization: `Bearer ${apiKey}`,
|
|
153
|
-
},
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
async updateStatus(runId, id, status) {
|
|
157
|
-
const apiKey = await this.#apiKey();
|
|
158
|
-
this.#logger.debug("Update status", {
|
|
159
|
-
id,
|
|
160
|
-
status,
|
|
161
|
-
});
|
|
162
|
-
return await zodfetch(JobRunStatusRecordSchema, `${this.#apiUrl}/api/v1/runs/${runId}/statuses/${id}`, {
|
|
163
|
-
method: "PUT",
|
|
164
|
-
headers: {
|
|
165
|
-
"Content-Type": "application/json",
|
|
166
|
-
Authorization: `Bearer ${apiKey}`,
|
|
167
|
-
},
|
|
168
|
-
body: JSON.stringify(status),
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
async updateSource(client, key, source) {
|
|
172
|
-
const apiKey = await this.#apiKey();
|
|
173
|
-
this.#logger.debug("activating http source", {
|
|
174
|
-
source,
|
|
175
|
-
});
|
|
176
|
-
const response = await zodfetch(TriggerSourceSchema, `${this.#apiUrl}/api/v2/${client}/sources/${key}`, {
|
|
177
|
-
method: "PUT",
|
|
178
|
-
headers: {
|
|
179
|
-
"Content-Type": "application/json",
|
|
180
|
-
Authorization: `Bearer ${apiKey}`,
|
|
181
|
-
},
|
|
182
|
-
body: JSON.stringify(source),
|
|
183
|
-
});
|
|
184
|
-
return response;
|
|
185
|
-
}
|
|
186
|
-
async updateWebhook(key, webhookData) {
|
|
187
|
-
const apiKey = await this.#apiKey();
|
|
188
|
-
this.#logger.debug("activating webhook", {
|
|
189
|
-
webhookData,
|
|
190
|
-
});
|
|
191
|
-
const response = await zodfetch(TriggerSourceSchema, `${this.#apiUrl}/api/v1/webhooks/${key}`, {
|
|
192
|
-
method: "PUT",
|
|
193
|
-
headers: {
|
|
194
|
-
"Content-Type": "application/json",
|
|
195
|
-
Authorization: `Bearer ${apiKey}`,
|
|
196
|
-
},
|
|
197
|
-
body: JSON.stringify(webhookData),
|
|
198
|
-
});
|
|
199
|
-
return response;
|
|
200
|
-
}
|
|
201
|
-
async registerTrigger(client, id, key, payload, idempotencyKey) {
|
|
202
|
-
const apiKey = await this.#apiKey();
|
|
203
|
-
this.#logger.debug("registering trigger", {
|
|
204
|
-
id,
|
|
205
|
-
payload,
|
|
206
|
-
});
|
|
207
|
-
const headers = {
|
|
208
|
-
"Content-Type": "application/json",
|
|
209
|
-
Authorization: `Bearer ${apiKey}`,
|
|
210
|
-
};
|
|
211
|
-
if (idempotencyKey) {
|
|
212
|
-
headers["Idempotency-Key"] = idempotencyKey;
|
|
213
|
-
}
|
|
214
|
-
const response = await zodfetch(RegisterSourceEventSchemaV2, `${this.#apiUrl}/api/v2/${client}/triggers/${id}/registrations/${key}`, {
|
|
215
|
-
method: "PUT",
|
|
216
|
-
headers: headers,
|
|
217
|
-
body: JSON.stringify(payload),
|
|
218
|
-
});
|
|
219
|
-
return response;
|
|
220
|
-
}
|
|
221
|
-
async registerSchedule(client, id, key, payload) {
|
|
222
|
-
const apiKey = await this.#apiKey();
|
|
223
|
-
this.#logger.debug("registering schedule", {
|
|
224
|
-
id,
|
|
225
|
-
payload,
|
|
226
|
-
});
|
|
227
|
-
const response = await zodfetch(RegisterScheduleResponseBodySchema, `${this.#apiUrl}/api/v1/${client}/schedules/${id}/registrations`, {
|
|
228
|
-
method: "POST",
|
|
229
|
-
headers: {
|
|
230
|
-
"Content-Type": "application/json",
|
|
231
|
-
Authorization: `Bearer ${apiKey}`,
|
|
232
|
-
},
|
|
233
|
-
body: JSON.stringify({ id: key, ...payload }),
|
|
234
|
-
});
|
|
235
|
-
return response;
|
|
236
|
-
}
|
|
237
|
-
async unregisterSchedule(client, id, key) {
|
|
238
|
-
const apiKey = await this.#apiKey();
|
|
239
|
-
this.#logger.debug("unregistering schedule", {
|
|
240
|
-
id,
|
|
241
|
-
});
|
|
242
|
-
const response = await zodfetch(z.object({ ok: z.boolean() }), `${this.#apiUrl}/api/v1/${client}/schedules/${id}/registrations/${encodeURIComponent(key)}`, {
|
|
243
|
-
method: "DELETE",
|
|
244
|
-
headers: {
|
|
245
|
-
"Content-Type": "application/json",
|
|
246
|
-
Authorization: `Bearer ${apiKey}`,
|
|
247
|
-
},
|
|
248
|
-
});
|
|
249
|
-
return response;
|
|
250
|
-
}
|
|
251
|
-
async getAuth(client, id) {
|
|
252
|
-
const apiKey = await this.#apiKey();
|
|
253
|
-
this.#logger.debug("getting auth", {
|
|
254
|
-
id,
|
|
255
|
-
});
|
|
256
|
-
const response = await zodfetch(ConnectionAuthSchema, `${this.#apiUrl}/api/v1/${client}/auth/${id}`, {
|
|
257
|
-
method: "GET",
|
|
258
|
-
headers: {
|
|
259
|
-
Accept: "application/json",
|
|
260
|
-
Authorization: `Bearer ${apiKey}`,
|
|
261
|
-
},
|
|
262
|
-
}, {
|
|
263
|
-
optional: true,
|
|
264
|
-
});
|
|
265
|
-
return response;
|
|
266
|
-
}
|
|
267
|
-
async getEvent(eventId) {
|
|
268
|
-
const apiKey = await this.#apiKey();
|
|
269
|
-
this.#logger.debug("Getting Event", {
|
|
270
|
-
eventId,
|
|
271
|
-
});
|
|
272
|
-
return await zodfetch(GetEventSchema, `${this.#apiUrl}/api/v2/events/${eventId}`, {
|
|
273
|
-
method: "GET",
|
|
274
|
-
headers: {
|
|
275
|
-
Authorization: `Bearer ${apiKey}`,
|
|
276
|
-
},
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
async getRun(runId, options) {
|
|
280
|
-
const apiKey = await this.#apiKey();
|
|
281
|
-
this.#logger.debug("Getting Run", {
|
|
282
|
-
runId,
|
|
283
|
-
});
|
|
284
|
-
return await zodfetch(GetRunSchema, urlWithSearchParams(`${this.#apiUrl}/api/v2/runs/${runId}`, options), {
|
|
285
|
-
method: "GET",
|
|
286
|
-
headers: {
|
|
287
|
-
Authorization: `Bearer ${apiKey}`,
|
|
288
|
-
},
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
async cancelRun(runId) {
|
|
292
|
-
const apiKey = await this.#apiKey();
|
|
293
|
-
this.#logger.debug("Cancelling Run", {
|
|
294
|
-
runId,
|
|
295
|
-
});
|
|
296
|
-
return await zodfetch(GetRunSchema, `${this.#apiUrl}/api/v1/runs/${runId}/cancel`, {
|
|
297
|
-
method: "POST",
|
|
298
|
-
headers: {
|
|
299
|
-
"Content-Type": "application/json",
|
|
300
|
-
Authorization: `Bearer ${apiKey}`,
|
|
301
|
-
},
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
async getRunStatuses(runId) {
|
|
305
|
-
const apiKey = await this.#apiKey();
|
|
306
|
-
this.#logger.debug("Getting Run statuses", {
|
|
307
|
-
runId,
|
|
308
|
-
});
|
|
309
|
-
return await zodfetch(GetRunStatusesSchema, `${this.#apiUrl}/api/v2/runs/${runId}/statuses`, {
|
|
310
|
-
method: "GET",
|
|
311
|
-
headers: {
|
|
312
|
-
Authorization: `Bearer ${apiKey}`,
|
|
313
|
-
},
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
async getRuns(jobSlug, options) {
|
|
317
|
-
const apiKey = await this.#apiKey();
|
|
318
|
-
this.#logger.debug("Getting Runs", {
|
|
319
|
-
jobSlug,
|
|
320
|
-
});
|
|
321
|
-
return await zodfetch(GetRunsSchema, urlWithSearchParams(`${this.#apiUrl}/api/v1/jobs/${jobSlug}/runs`, options), {
|
|
322
|
-
method: "GET",
|
|
323
|
-
headers: {
|
|
324
|
-
Authorization: `Bearer ${apiKey}`,
|
|
325
|
-
},
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
async invokeJob(jobId, payload, options = {}) {
|
|
329
|
-
const apiKey = await this.#apiKey();
|
|
330
|
-
this.#logger.debug("Invoking Job", {
|
|
331
|
-
jobId,
|
|
332
|
-
});
|
|
333
|
-
const body = {
|
|
334
|
-
payload,
|
|
335
|
-
context: options.context ?? {},
|
|
336
|
-
options: {
|
|
337
|
-
accountId: options.accountId,
|
|
338
|
-
callbackUrl: options.callbackUrl,
|
|
339
|
-
},
|
|
340
|
-
};
|
|
341
|
-
return await zodfetch(InvokeJobResponseSchema, `${this.#apiUrl}/api/v1/jobs/${jobId}/invoke`, {
|
|
342
|
-
method: "POST",
|
|
343
|
-
headers: {
|
|
344
|
-
"Content-Type": "application/json",
|
|
345
|
-
Authorization: `Bearer ${apiKey}`,
|
|
346
|
-
...(options.idempotencyKey ? { "Idempotency-Key": options.idempotencyKey } : {}),
|
|
347
|
-
},
|
|
348
|
-
body: JSON.stringify(body),
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
async cancelRunsForJob(jobId) {
|
|
352
|
-
const apiKey = await this.#apiKey();
|
|
353
|
-
this.#logger.debug("Cancelling Runs for Job", {
|
|
354
|
-
jobId,
|
|
355
|
-
});
|
|
356
|
-
return await zodfetch(CancelRunsForJobSchema, `${this.#apiUrl}/api/v1/jobs/${jobId}/cancel-runs`, {
|
|
357
|
-
method: "POST",
|
|
358
|
-
headers: {
|
|
359
|
-
"Content-Type": "application/json",
|
|
360
|
-
Authorization: `Bearer ${apiKey}`,
|
|
361
|
-
},
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
async createEphemeralEventDispatcher(payload) {
|
|
365
|
-
const apiKey = await this.#apiKey();
|
|
366
|
-
this.#logger.debug("Creating ephemeral event dispatcher", {
|
|
367
|
-
payload,
|
|
368
|
-
});
|
|
369
|
-
const response = await zodfetch(EphemeralEventDispatcherResponseBodySchema, `${this.#apiUrl}/api/v1/event-dispatchers/ephemeral`, {
|
|
370
|
-
method: "POST",
|
|
371
|
-
headers: {
|
|
372
|
-
"Content-Type": "application/json",
|
|
373
|
-
Authorization: `Bearer ${apiKey}`,
|
|
374
|
-
},
|
|
375
|
-
body: JSON.stringify(payload),
|
|
376
|
-
});
|
|
377
|
-
return response;
|
|
378
|
-
}
|
|
379
|
-
get store() {
|
|
380
|
-
return this.#storeClient;
|
|
381
|
-
}
|
|
382
|
-
async #queryKeyValueStore(action, data) {
|
|
383
|
-
const apiKey = await this.#apiKey();
|
|
384
|
-
this.#logger.debug("accessing key-value store", {
|
|
385
|
-
action,
|
|
386
|
-
data,
|
|
387
|
-
});
|
|
388
|
-
const encodedKey = encodeURIComponent(data.key);
|
|
389
|
-
const STORE_URL = `${this.#apiUrl}/api/v1/store/${encodedKey}`;
|
|
390
|
-
const authHeader = {
|
|
391
|
-
Authorization: `Bearer ${apiKey}`,
|
|
392
|
-
};
|
|
393
|
-
let requestInit;
|
|
394
|
-
switch (action) {
|
|
395
|
-
case "DELETE": {
|
|
396
|
-
requestInit = {
|
|
397
|
-
method: "DELETE",
|
|
398
|
-
headers: authHeader,
|
|
399
|
-
};
|
|
400
|
-
break;
|
|
401
|
-
}
|
|
402
|
-
case "GET": {
|
|
403
|
-
requestInit = {
|
|
404
|
-
method: "GET",
|
|
405
|
-
headers: authHeader,
|
|
406
|
-
};
|
|
407
|
-
break;
|
|
408
|
-
}
|
|
409
|
-
case "HAS": {
|
|
410
|
-
const headResponse = await fetchHead(STORE_URL, {
|
|
411
|
-
headers: authHeader,
|
|
412
|
-
});
|
|
413
|
-
return {
|
|
414
|
-
action: "HAS",
|
|
415
|
-
key: encodedKey,
|
|
416
|
-
has: !!headResponse.ok,
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
case "SET": {
|
|
420
|
-
const MAX_BODY_BYTE_LENGTH = 256 * 1024;
|
|
421
|
-
if ((data.value?.length ?? 0) > MAX_BODY_BYTE_LENGTH) {
|
|
422
|
-
throw new Error(`Max request body size exceeded: ${MAX_BODY_BYTE_LENGTH} bytes`);
|
|
423
|
-
}
|
|
424
|
-
requestInit = {
|
|
425
|
-
method: "PUT",
|
|
426
|
-
headers: {
|
|
427
|
-
...authHeader,
|
|
428
|
-
"Content-Type": "text/plain",
|
|
429
|
-
},
|
|
430
|
-
body: data.value,
|
|
431
|
-
};
|
|
432
|
-
break;
|
|
433
|
-
}
|
|
434
|
-
default: {
|
|
435
|
-
assertExhaustive(action);
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
const response = await zodfetch(KeyValueStoreResponseBodySchema, STORE_URL, requestInit);
|
|
439
|
-
return response;
|
|
440
|
-
}
|
|
441
|
-
async #apiKey() {
|
|
442
|
-
const apiKey = getApiKey(this.#options.apiKey);
|
|
443
|
-
if (apiKey.status === "invalid") {
|
|
444
|
-
throw new Error("Invalid API key");
|
|
445
|
-
// const chalk = (await import("chalk")).default;
|
|
446
|
-
// const terminalLink = (await import("terminal-link")).default;
|
|
447
|
-
// throw new Error(
|
|
448
|
-
// `${chalk.red("Trigger.dev error")}: Invalid API key ("${chalk.italic(
|
|
449
|
-
// apiKey.apiKey
|
|
450
|
-
// )}"), please set the TRIGGER_API_KEY environment variable or pass the apiKey option to a valid value. ${terminalLink(
|
|
451
|
-
// "Get your API key here",
|
|
452
|
-
// "https://app.trigger.dev",
|
|
453
|
-
// {
|
|
454
|
-
// fallback(text, url) {
|
|
455
|
-
// return `${text} 👉 ${url}`;
|
|
456
|
-
// },
|
|
457
|
-
// }
|
|
458
|
-
// )}`
|
|
459
|
-
// );
|
|
460
|
-
}
|
|
461
|
-
else if (apiKey.status === "missing") {
|
|
462
|
-
throw new Error("Missing API key");
|
|
463
|
-
// const chalk = (await import("chalk")).default;
|
|
464
|
-
// const terminalLink = (await import("terminal-link")).default;
|
|
465
|
-
// throw new Error(
|
|
466
|
-
// `${chalk.red(
|
|
467
|
-
// "Trigger.dev error"
|
|
468
|
-
// )}: Missing an API key, please set the TRIGGER_API_KEY environment variable or pass the apiKey option to the Trigger constructor. ${terminalLink(
|
|
469
|
-
// "Get your API key here",
|
|
470
|
-
// "https://app.trigger.dev",
|
|
471
|
-
// {
|
|
472
|
-
// fallback(text, url) {
|
|
473
|
-
// return `${text} 👉 ${url}`;
|
|
474
|
-
// },
|
|
475
|
-
// }
|
|
476
|
-
// )}`
|
|
477
|
-
// );
|
|
478
|
-
}
|
|
479
|
-
return apiKey.apiKey;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
function getApiKey(key) {
|
|
483
|
-
const apiKey = key ?? env.TRIGGER_API_KEY;
|
|
484
|
-
if (!apiKey) {
|
|
485
|
-
return { status: "missing" };
|
|
486
|
-
}
|
|
487
|
-
// Validate the api_key format (should be tr_{env}_XXXXX)
|
|
488
|
-
const isValid = apiKey.match(/^tr_[a-z]+_[a-zA-Z0-9]+$/);
|
|
489
|
-
if (!isValid) {
|
|
490
|
-
return { status: "invalid", apiKey };
|
|
491
|
-
}
|
|
492
|
-
return { status: "valid", apiKey };
|
|
493
|
-
}
|
|
494
|
-
async function zodfetchWithVersions(logger, versionedSchemaMap, unversionedSchema, url, requestInit, options, retryCount = 0) {
|
|
495
|
-
try {
|
|
496
|
-
const fullRequestInit = requestInitWithCache(requestInit);
|
|
497
|
-
const response = await fetch(url, fullRequestInit);
|
|
498
|
-
logger.debug(`[ApiClient] zodfetchWithVersions ${url} (attempt ${retryCount + 1})`, {
|
|
499
|
-
url,
|
|
500
|
-
retryCount,
|
|
501
|
-
requestHeaders: fullRequestInit?.headers,
|
|
502
|
-
responseHeaders: Object.fromEntries(response.headers.entries()),
|
|
503
|
-
});
|
|
504
|
-
if ((!requestInit || requestInit.method === "GET") &&
|
|
505
|
-
response.status === 404 &&
|
|
506
|
-
options?.optional) {
|
|
507
|
-
// @ts-ignore
|
|
508
|
-
return;
|
|
509
|
-
}
|
|
510
|
-
//rate limit, so we want to reschedule
|
|
511
|
-
if (response.status === 429) {
|
|
512
|
-
//unix timestamp in milliseconds
|
|
513
|
-
const retryAfter = response.headers.get("x-ratelimit-reset");
|
|
514
|
-
if (retryAfter) {
|
|
515
|
-
throw new AutoYieldRateLimitError(parseInt(retryAfter));
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
if (response.status >= 400 && response.status < 500) {
|
|
519
|
-
const rawBody = await safeResponseText(response);
|
|
520
|
-
const body = safeJsonParse(rawBody);
|
|
521
|
-
logger.error(`[ApiClient] zodfetchWithVersions failed with ${response.status}`, {
|
|
522
|
-
url,
|
|
523
|
-
retryCount,
|
|
524
|
-
requestHeaders: fullRequestInit?.headers,
|
|
525
|
-
responseHeaders: Object.fromEntries(response.headers.entries()),
|
|
526
|
-
status: response.status,
|
|
527
|
-
rawBody,
|
|
528
|
-
});
|
|
529
|
-
if (body && body.error) {
|
|
530
|
-
throw new Error(body.error);
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
throw new Error(rawBody);
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
if (response.status >= 500 && retryCount < MAX_RETRIES) {
|
|
537
|
-
// retry with exponential backoff and jitter
|
|
538
|
-
const delay = exponentialBackoff(retryCount + 1);
|
|
539
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
540
|
-
return zodfetchWithVersions(logger, versionedSchemaMap, unversionedSchema, url, requestInit, options, retryCount + 1);
|
|
541
|
-
}
|
|
542
|
-
if (response.status !== 200) {
|
|
543
|
-
const rawBody = await safeResponseText(response);
|
|
544
|
-
logger.error(`[ApiClient] zodfetchWithVersions failed with ${response.status}`, {
|
|
545
|
-
url,
|
|
546
|
-
retryCount,
|
|
547
|
-
requestHeaders: fullRequestInit?.headers,
|
|
548
|
-
responseHeaders: Object.fromEntries(response.headers.entries()),
|
|
549
|
-
status: response.status,
|
|
550
|
-
rawBody,
|
|
551
|
-
});
|
|
552
|
-
throw new Error(options?.errorMessage ?? `Failed to fetch ${url}, got status code ${response.status}`);
|
|
553
|
-
}
|
|
554
|
-
const jsonBody = await response.json();
|
|
555
|
-
const version = response.headers.get("trigger-version");
|
|
556
|
-
if (!version) {
|
|
557
|
-
return {
|
|
558
|
-
version: "unversioned",
|
|
559
|
-
body: unversionedSchema.parse(jsonBody),
|
|
560
|
-
};
|
|
561
|
-
}
|
|
562
|
-
const versionedSchema = versionedSchemaMap[version];
|
|
563
|
-
if (!versionedSchema) {
|
|
564
|
-
throw new UnknownVersionError(version);
|
|
565
|
-
}
|
|
566
|
-
return {
|
|
567
|
-
version,
|
|
568
|
-
body: versionedSchema.parse(jsonBody),
|
|
569
|
-
};
|
|
570
|
-
}
|
|
571
|
-
catch (error) {
|
|
572
|
-
if (error instanceof UnknownVersionError || error instanceof AutoYieldRateLimitError) {
|
|
573
|
-
throw error;
|
|
574
|
-
}
|
|
575
|
-
logger.error(`[ApiClient] zodfetchWithVersions failed with a connection error`, {
|
|
576
|
-
url,
|
|
577
|
-
retryCount,
|
|
578
|
-
error,
|
|
579
|
-
});
|
|
580
|
-
if (retryCount < MAX_RETRIES) {
|
|
581
|
-
// retry with exponential backoff and jitter
|
|
582
|
-
const delay = exponentialBackoff(retryCount + 1);
|
|
583
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
584
|
-
return zodfetchWithVersions(logger, versionedSchemaMap, unversionedSchema, url, requestInit, options, retryCount + 1);
|
|
585
|
-
}
|
|
586
|
-
throw error;
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
function requestInitWithCache(requestInit) {
|
|
590
|
-
try {
|
|
591
|
-
const withCache = {
|
|
592
|
-
...requestInit,
|
|
593
|
-
cache: "no-cache",
|
|
594
|
-
};
|
|
595
|
-
const _ = new Request("http://localhost", withCache);
|
|
596
|
-
return withCache;
|
|
597
|
-
}
|
|
598
|
-
catch (error) {
|
|
599
|
-
return requestInit ?? {};
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
async function fetchHead(url, requestInitWithoutMethod, retryCount = 0) {
|
|
603
|
-
const requestInit = {
|
|
604
|
-
...requestInitWithoutMethod,
|
|
605
|
-
method: "HEAD",
|
|
606
|
-
};
|
|
607
|
-
const response = await fetch(url, requestInitWithCache(requestInit));
|
|
608
|
-
if (response.status >= 500 && retryCount < MAX_RETRIES) {
|
|
609
|
-
// retry with exponential backoff and jitter
|
|
610
|
-
const delay = exponentialBackoff(retryCount + 1);
|
|
611
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
612
|
-
return fetchHead(url, requestInitWithoutMethod, retryCount + 1);
|
|
613
|
-
}
|
|
614
|
-
return response;
|
|
615
|
-
}
|
|
616
|
-
async function zodfetch(schema, url, requestInit, options, retryCount = 0) {
|
|
617
|
-
try {
|
|
618
|
-
const response = await fetch(url, requestInitWithCache(requestInit));
|
|
619
|
-
if ((!requestInit || requestInit.method === "GET") &&
|
|
620
|
-
response.status === 404 &&
|
|
621
|
-
options?.optional) {
|
|
622
|
-
// @ts-ignore
|
|
623
|
-
return;
|
|
624
|
-
}
|
|
625
|
-
//rate limit, so we want to reschedule
|
|
626
|
-
if (response.status === 429) {
|
|
627
|
-
//unix timestamp in milliseconds
|
|
628
|
-
const retryAfter = response.headers.get("x-ratelimit-reset");
|
|
629
|
-
if (retryAfter) {
|
|
630
|
-
throw new AutoYieldRateLimitError(parseInt(retryAfter));
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
if (response.status >= 400 && response.status < 500) {
|
|
634
|
-
const body = await response.json();
|
|
635
|
-
throw new Error(body.error);
|
|
636
|
-
}
|
|
637
|
-
if (response.status >= 500 && retryCount < MAX_RETRIES) {
|
|
638
|
-
// retry with exponential backoff and jitter
|
|
639
|
-
const delay = exponentialBackoff(retryCount + 1);
|
|
640
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
641
|
-
return zodfetch(schema, url, requestInit, options, retryCount + 1);
|
|
642
|
-
}
|
|
643
|
-
if (response.status !== 200) {
|
|
644
|
-
throw new Error(options?.errorMessage ?? `Failed to fetch ${url}, got status code ${response.status}`);
|
|
645
|
-
}
|
|
646
|
-
const jsonBody = await response.json();
|
|
647
|
-
return schema.parse(jsonBody);
|
|
648
|
-
}
|
|
649
|
-
catch (error) {
|
|
650
|
-
if (error instanceof AutoYieldRateLimitError) {
|
|
651
|
-
throw error;
|
|
652
|
-
}
|
|
653
|
-
if (retryCount < MAX_RETRIES) {
|
|
654
|
-
// retry with exponential backoff and jitter
|
|
655
|
-
const delay = exponentialBackoff(retryCount + 1);
|
|
656
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
657
|
-
return zodfetch(schema, url, requestInit, options, retryCount + 1);
|
|
658
|
-
}
|
|
659
|
-
throw error;
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
// First retry will have a delay of 80ms, second 160ms, third 320ms, etc.
|
|
663
|
-
function exponentialBackoff(retryCount) {
|
|
664
|
-
// Calculate the delay using the exponential backoff formula
|
|
665
|
-
const delay = Math.min(Math.pow(EXPONENT_FACTOR, retryCount) * MIN_DELAY_IN_MS, MAX_DELAY_IN_MS);
|
|
666
|
-
// Calculate the jitter
|
|
667
|
-
const jitterValue = Math.random() * JITTER_IN_MS;
|
|
668
|
-
// Return the calculated delay with jitter
|
|
669
|
-
return delay + jitterValue;
|
|
670
|
-
}
|
|
671
|
-
function safeJsonParse(rawBody) {
|
|
672
|
-
try {
|
|
673
|
-
return JSON.parse(rawBody);
|
|
674
|
-
}
|
|
675
|
-
catch (error) {
|
|
676
|
-
return;
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
async function safeResponseText(response) {
|
|
680
|
-
try {
|
|
681
|
-
return await response.text();
|
|
682
|
-
}
|
|
683
|
-
catch (error) {
|
|
684
|
-
return "";
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
//# sourceMappingURL=apiClient.js.map
|