Skip to content

Commit 99f23e6

Browse files
committed
Use the new clientOrThrow() method everywhere
1 parent 5832de8 commit 99f23e6

File tree

6 files changed

+34
-141
lines changed

6 files changed

+34
-141
lines changed

packages/core/src/v3/apiClientManager/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { ApiClientConfiguration } from "./types.js";
55

66
const API_NAME = "api-client";
77

8-
export class ApiClientMissingError extends Error {}
8+
export class ApiClientMissingError extends Error {
9+
constructor(message: string) {
10+
super(message);
11+
this.name = "ApiClientMissingError";
12+
}
13+
}
914

1015
export class APIClientManagerAPI {
1116
private static _instance?: APIClientManagerAPI;
@@ -62,11 +67,11 @@ export class APIClientManagerAPI {
6267
const hasBaseUrl = !!this.baseURL;
6368
const hasAccessToken = !!this.accessToken;
6469
if (!hasBaseUrl && !hasAccessToken) {
65-
return `You need to set the TRIGGER_API_URL and TRIGGER_SECRET_KEY environment variables.`;
70+
return `You need to set the TRIGGER_API_URL and TRIGGER_SECRET_KEY environment variables. See https://trigger.dev/docs/management/overview#authentication`;
6671
} else if (!hasBaseUrl) {
67-
return `You need to set the TRIGGER_API_URL environment variable.`;
72+
return `You need to set the TRIGGER_API_URL environment variable. See https://trigger.dev/docs/management/overview#authentication`;
6873
} else if (!hasAccessToken) {
69-
return `You need to set the TRIGGER_SECRET_KEY environment variable.`;
74+
return `You need to set the TRIGGER_SECRET_KEY environment variable. See https://trigger.dev/docs/management/overview#authentication`;
7075
}
7176

7277
return `Unknown error`;

packages/trigger-sdk/src/v3/envvars.ts

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
mergeRequestOptions,
1515
taskContext,
1616
} from "@trigger.dev/core/v3";
17-
import { apiClientMissingError } from "./shared.js";
1817
import { tracer } from "./tracer.js";
1918

2019
export type { CreateEnvironmentVariableParams, ImportEnvironmentVariablesParams };
@@ -76,11 +75,7 @@ export function upload(
7675
$params = params;
7776
}
7877

79-
const apiClient = apiClientManager.client;
80-
81-
if (!apiClient) {
82-
throw apiClientMissingError();
83-
}
78+
const apiClient = apiClientManager.clientOrThrow();
8479

8580
return apiClient.importEnvVars($projectRef, $slug, $params, $requestOptions);
8681
}
@@ -121,11 +116,7 @@ export function list(
121116
$requestOptions
122117
);
123118

124-
const apiClient = apiClientManager.client;
125-
126-
if (!apiClient) {
127-
throw apiClientMissingError();
128-
}
119+
const apiClient = apiClientManager.clientOrThrow();
129120

130121
return apiClient.listEnvVars($projectRef, $slug, $requestOptions);
131122
}
@@ -187,11 +178,7 @@ export function create(
187178
$params = params;
188179
}
189180

190-
const apiClient = apiClientManager.client;
191-
192-
if (!apiClient) {
193-
throw apiClientMissingError();
194-
}
181+
const apiClient = apiClientManager.clientOrThrow();
195182

196183
return apiClient.createEnvVar($projectRef, $slug, $params, $requestOptions);
197184
}
@@ -238,11 +225,7 @@ export function retrieve(
238225
throw new Error("slug is required");
239226
}
240227

241-
const apiClient = apiClientManager.client;
242-
243-
if (!apiClient) {
244-
throw apiClientMissingError();
245-
}
228+
const apiClient = apiClientManager.clientOrThrow();
246229

247230
return apiClient.retrieveEnvVar($projectRef, $slug, $name, $requestOptions);
248231
}
@@ -289,11 +272,7 @@ export function del(
289272
throw new Error("slug is required");
290273
}
291274

292-
const apiClient = apiClientManager.client;
293-
294-
if (!apiClient) {
295-
throw apiClientMissingError();
296-
}
275+
const apiClient = apiClientManager.clientOrThrow();
297276

298277
return apiClient.deleteEnvVar($projectRef, $slug, $name, $requestOptions);
299278
}
@@ -362,11 +341,7 @@ export function update(
362341
$params = params;
363342
}
364343

365-
const apiClient = apiClientManager.client;
366-
367-
if (!apiClient) {
368-
throw apiClientMissingError();
369-
}
344+
const apiClient = apiClientManager.clientOrThrow();
370345

371346
return apiClient.updateEnvVar($projectRef, $slug, $name, $params, $requestOptions);
372347
}

packages/trigger-sdk/src/v3/runs.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
isRequestOptions,
1919
mergeRequestOptions,
2020
} from "@trigger.dev/core/v3";
21-
import { AnyTask, Prettify, RunHandle, Task, apiClientMissingError } from "./shared.js";
21+
import { AnyTask, Prettify, RunHandle, Task } from "./shared.js";
2222
import { tracer } from "./tracer.js";
2323
import { resolvePresignedPacketUrl } from "@trigger.dev/core/v3/utils/ioSerialization";
2424

@@ -57,11 +57,7 @@ function listRuns(
5757
paramsOrOptions?: ListRunsQueryParams | ListProjectRunsQueryParams | ApiRequestOptions,
5858
requestOptions?: ApiRequestOptions
5959
): CursorPagePromise<typeof ListRunResponseItem> {
60-
const apiClient = apiClientManager.client;
61-
62-
if (!apiClient) {
63-
throw apiClientMissingError();
64-
}
60+
const apiClient = apiClientManager.clientOrThrow();
6561

6662
const $requestOptions = listRunsRequestOptions(
6763
paramsOrProjectRef,
@@ -158,11 +154,7 @@ function retrieveRun<TRunId extends RunHandle<any> | AnyTask | string>(
158154
runId: RunId<TRunId>,
159155
requestOptions?: ApiRequestOptions
160156
): ApiPromise<RetrieveRunResult<TRunId>> {
161-
const apiClient = apiClientManager.client;
162-
163-
if (!apiClient) {
164-
throw apiClientMissingError();
165-
}
157+
const apiClient = apiClientManager.clientOrThrow();
166158

167159
const $requestOptions = mergeRequestOptions(
168160
{
@@ -216,11 +208,7 @@ function replayRun(
216208
runId: string,
217209
requestOptions?: ApiRequestOptions
218210
): ApiPromise<ReplayRunResponse> {
219-
const apiClient = apiClientManager.client;
220-
221-
if (!apiClient) {
222-
throw apiClientMissingError();
223-
}
211+
const apiClient = apiClientManager.clientOrThrow();
224212

225213
const $requestOptions = mergeRequestOptions(
226214
{
@@ -250,11 +238,7 @@ function cancelRun(
250238
runId: string,
251239
requestOptions?: ApiRequestOptions
252240
): ApiPromise<CanceledRunResponse> {
253-
const apiClient = apiClientManager.client;
254-
255-
if (!apiClient) {
256-
throw apiClientMissingError();
257-
}
241+
const apiClient = apiClientManager.clientOrThrow();
258242

259243
const $requestOptions = mergeRequestOptions(
260244
{
@@ -285,11 +269,7 @@ function rescheduleRun(
285269
body: RescheduleRunRequestBody,
286270
requestOptions?: ApiRequestOptions
287271
): ApiPromise<RetrieveRunResponse> {
288-
const apiClient = apiClientManager.client;
289-
290-
if (!apiClient) {
291-
throw apiClientMissingError();
292-
}
272+
const apiClient = apiClientManager.clientOrThrow();
293273

294274
const $requestOptions = mergeRequestOptions(
295275
{

packages/trigger-sdk/src/v3/schedules/index.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
taskCatalog,
1313
} from "@trigger.dev/core/v3";
1414
import { zodfetch } from "@trigger.dev/core/v3/zodfetch";
15-
import { Task, TaskOptions, apiClientMissingError, createTask } from "../shared.js";
15+
import { Task, TaskOptions, createTask } from "../shared.js";
1616
import * as SchedulesAPI from "./api.js";
1717
import { tracer } from "../tracer.js";
1818

@@ -86,11 +86,7 @@ export function create(
8686
options: SchedulesAPI.CreateScheduleOptions,
8787
requestOptions?: ApiRequestOptions
8888
): ApiPromise<ScheduleObject> {
89-
const apiClient = apiClientManager.client;
90-
91-
if (!apiClient) {
92-
throw apiClientMissingError();
93-
}
89+
const apiClient = apiClientManager.clientOrThrow();
9490

9591
const $requestOptions = mergeRequestOptions(
9692
{
@@ -124,11 +120,7 @@ export function retrieve(
124120
scheduleId: string,
125121
requestOptions?: ApiRequestOptions
126122
): ApiPromise<ScheduleObject> {
127-
const apiClient = apiClientManager.client;
128-
129-
if (!apiClient) {
130-
throw apiClientMissingError();
131-
}
123+
const apiClient = apiClientManager.clientOrThrow();
132124

133125
const $requestOptions = mergeRequestOptions(
134126
{
@@ -169,11 +161,7 @@ export function update(
169161
options: SchedulesAPI.UpdateScheduleOptions,
170162
requestOptions?: ApiRequestOptions
171163
): ApiPromise<ScheduleObject> {
172-
const apiClient = apiClientManager.client;
173-
174-
if (!apiClient) {
175-
throw apiClientMissingError();
176-
}
164+
const apiClient = apiClientManager.clientOrThrow();
177165

178166
const $requestOptions = mergeRequestOptions(
179167
{
@@ -207,11 +195,7 @@ export function del(
207195
scheduleId: string,
208196
requestOptions?: ApiRequestOptions
209197
): ApiPromise<DeletedScheduleObject> {
210-
const apiClient = apiClientManager.client;
211-
212-
if (!apiClient) {
213-
throw apiClientMissingError();
214-
}
198+
const apiClient = apiClientManager.clientOrThrow();
215199

216200
const $requestOptions = mergeRequestOptions(
217201
{
@@ -245,11 +229,7 @@ export function deactivate(
245229
scheduleId: string,
246230
requestOptions?: ApiRequestOptions
247231
): ApiPromise<ScheduleObject> {
248-
const apiClient = apiClientManager.client;
249-
250-
if (!apiClient) {
251-
throw apiClientMissingError();
252-
}
232+
const apiClient = apiClientManager.clientOrThrow();
253233

254234
const $requestOptions = mergeRequestOptions(
255235
{
@@ -283,11 +263,7 @@ export function activate(
283263
scheduleId: string,
284264
requestOptions?: ApiRequestOptions
285265
): ApiPromise<ScheduleObject> {
286-
const apiClient = apiClientManager.client;
287-
288-
if (!apiClient) {
289-
throw apiClientMissingError();
290-
}
266+
const apiClient = apiClientManager.clientOrThrow();
291267

292268
const $requestOptions = mergeRequestOptions(
293269
{
@@ -324,11 +300,7 @@ export function list(
324300
options?: SchedulesAPI.ListScheduleOptions,
325301
requestOptions?: ApiRequestOptions
326302
): OffsetLimitPagePromise<typeof ScheduleObject> {
327-
const apiClient = apiClientManager.client;
328-
329-
if (!apiClient) {
330-
throw apiClientMissingError();
331-
}
303+
const apiClient = apiClientManager.clientOrThrow();
332304

333305
const $requestOptions = mergeRequestOptions(
334306
{
@@ -349,10 +321,6 @@ export function list(
349321
export function timezones(options?: { excludeUtc?: boolean }) {
350322
const baseUrl = apiClientManager.baseURL;
351323

352-
if (!baseUrl) {
353-
throw apiClientMissingError();
354-
}
355-
356324
return zodfetch(
357325
TimezonesResult,
358326
`${baseUrl}/api/v1/timezones${options?.excludeUtc === true ? "?excludeUtc=true" : ""}`,

packages/trigger-sdk/src/v3/shared.ts

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,7 @@ async function trigger_internal<TPayload, TOutput>(
775775
options?: TaskRunOptions,
776776
requestOptions?: ApiRequestOptions
777777
): Promise<RunHandle<TOutput>> {
778-
const apiClient = apiClientManager.client;
779-
780-
if (!apiClient) {
781-
throw apiClientMissingError();
782-
}
778+
const apiClient = apiClientManager.clientOrThrow();
783779

784780
const payloadPacket = await stringifyIO(payload);
785781

@@ -844,11 +840,7 @@ async function batchTrigger_internal<TPayload, TOutput>(
844840
requestOptions?: ApiRequestOptions,
845841
queue?: QueueOptions
846842
): Promise<BatchRunHandle<TOutput>> {
847-
const apiClient = apiClientManager.client;
848-
849-
if (!apiClient) {
850-
throw apiClientMissingError();
851-
}
843+
const apiClient = apiClientManager.clientOrThrow();
852844

853845
const response = await apiClient.batchTriggerTask(
854846
id,
@@ -920,11 +912,7 @@ async function triggerAndWait_internal<TPayload, TOutput>(
920912
throw new Error("triggerAndWait can only be used from inside a task.run()");
921913
}
922914

923-
const apiClient = apiClientManager.client;
924-
925-
if (!apiClient) {
926-
throw apiClientMissingError();
927-
}
915+
const apiClient = apiClientManager.clientOrThrow();
928916

929917
const payloadPacket = await stringifyIO(payload);
930918

@@ -1015,11 +1003,7 @@ async function batchTriggerAndWait_internal<TPayload, TOutput>(
10151003
throw new Error("batchTriggerAndWait can only be used from inside a task.run()");
10161004
}
10171005

1018-
const apiClient = apiClientManager.client;
1019-
1020-
if (!apiClient) {
1021-
throw apiClientMissingError();
1022-
}
1006+
const apiClient = apiClientManager.clientOrThrow();
10231007

10241008
return await tracer.startActiveSpan(
10251009
name,
@@ -1204,20 +1188,6 @@ async function handleTaskRunExecutionResult<TOutput>(
12041188
}
12051189
}
12061190

1207-
export function apiClientMissingError() {
1208-
const hasBaseUrl = !!apiClientManager.baseURL;
1209-
const hasAccessToken = !!apiClientManager.accessToken;
1210-
if (!hasBaseUrl && !hasAccessToken) {
1211-
return `You need to set the TRIGGER_API_URL and TRIGGER_SECRET_KEY environment variables.`;
1212-
} else if (!hasBaseUrl) {
1213-
return `You need to set the TRIGGER_API_URL environment variable.`;
1214-
} else if (!hasAccessToken) {
1215-
return `You need to set the TRIGGER_SECRET_KEY environment variable.`;
1216-
}
1217-
1218-
return `Unknown error`;
1219-
}
1220-
12211191
async function makeKey(
12221192
idempotencyKey?: IdempotencyKey | string | string[]
12231193
): Promise<IdempotencyKey | undefined> {

packages/trigger-sdk/src/v3/tags.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@ import {
77
mergeRequestOptions,
88
taskContext,
99
} from "@trigger.dev/core/v3";
10-
import { apiClientMissingError } from "./shared.js";
1110
import { tracer } from "./tracer.js";
1211

1312
export const tags = {
1413
add: addTags,
1514
};
1615

1716
async function addTags(tags: RunTags, requestOptions?: ApiRequestOptions) {
18-
const apiClient = apiClientManager.client;
19-
20-
if (!apiClient) {
21-
throw apiClientMissingError();
22-
}
17+
const apiClient = apiClientManager.clientOrThrow();
2318

2419
const run = taskContext.ctx?.run;
2520
if (!run) {

0 commit comments

Comments
 (0)