Skip to content

Commit d137e4e

Browse files
committed
Fixed merge issue: use zodFetch, not wrapZodFetch
1 parent 16a365f commit d137e4e

File tree

1 file changed

+54
-46
lines changed

1 file changed

+54
-46
lines changed

packages/cli-v3/src/apiClient.ts

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import {
1717
ImportEnvironmentVariablesRequestBody,
1818
EnvironmentVariableResponseBody,
1919
TaskRunExecution,
20-
APIError,
2120
} from "@trigger.dev/core/v3";
22-
import { zodfetch } from "@trigger.dev/core/v3/zodfetch";
2321

2422
export class CliApiClient {
2523
private readonly apiURL: string;
@@ -32,7 +30,7 @@ export class CliApiClient {
3230
}
3331

3432
async createAuthorizationCode() {
35-
return wrapZodFetch(
33+
return zodfetch(
3634
CreateAuthorizationCodeResponseSchema,
3735
`${this.apiURL}/api/v1/authorization-code`,
3836
{
@@ -42,7 +40,7 @@ export class CliApiClient {
4240
}
4341

4442
async getPersonalAccessToken(authorizationCode: string) {
45-
return wrapZodFetch(GetPersonalAccessTokenResponseSchema, `${this.apiURL}/api/v1/token`, {
43+
return zodfetch(GetPersonalAccessTokenResponseSchema, `${this.apiURL}/api/v1/token`, {
4644
method: "POST",
4745
body: JSON.stringify({
4846
authorizationCode,
@@ -55,7 +53,7 @@ export class CliApiClient {
5553
throw new Error("whoAmI: No access token");
5654
}
5755

58-
return wrapZodFetch(WhoAmIResponseSchema, `${this.apiURL}/api/v2/whoami`, {
56+
return zodfetch(WhoAmIResponseSchema, `${this.apiURL}/api/v2/whoami`, {
5957
headers: {
6058
Authorization: `Bearer ${this.accessToken}`,
6159
"Content-Type": "application/json",
@@ -68,7 +66,7 @@ export class CliApiClient {
6866
throw new Error("getProject: No access token");
6967
}
7068

71-
return wrapZodFetch(GetProjectResponseBody, `${this.apiURL}/api/v1/projects/${projectRef}`, {
69+
return zodfetch(GetProjectResponseBody, `${this.apiURL}/api/v1/projects/${projectRef}`, {
7270
headers: {
7371
Authorization: `Bearer ${this.accessToken}`,
7472
"Content-Type": "application/json",
@@ -81,7 +79,7 @@ export class CliApiClient {
8179
throw new Error("getProjects: No access token");
8280
}
8381

84-
return wrapZodFetch(GetProjectsResponseBody, `${this.apiURL}/api/v1/projects`, {
82+
return zodfetch(GetProjectsResponseBody, `${this.apiURL}/api/v1/projects`, {
8583
headers: {
8684
Authorization: `Bearer ${this.accessToken}`,
8785
"Content-Type": "application/json",
@@ -94,7 +92,7 @@ export class CliApiClient {
9492
throw new Error("createBackgroundWorker: No access token");
9593
}
9694

97-
return wrapZodFetch(
95+
return zodfetch(
9896
CreateBackgroundWorkerResponse,
9997
`${this.apiURL}/api/v1/projects/${projectRef}/background-workers`,
10098
{
@@ -113,7 +111,7 @@ export class CliApiClient {
113111
throw new Error("creatTaskRunAttempt: No access token");
114112
}
115113

116-
return wrapZodFetch(TaskRunExecution, `${this.apiURL}/api/v1/runs/${runFriendlyId}/attempts`, {
114+
return zodfetch(TaskRunExecution, `${this.apiURL}/api/v1/runs/${runFriendlyId}/attempts`, {
117115
method: "POST",
118116
headers: {
119117
Authorization: `Bearer ${this.accessToken}`,
@@ -133,24 +131,20 @@ export class CliApiClient {
133131
throw new Error("getProjectDevEnv: No access token");
134132
}
135133

136-
return wrapZodFetch(
137-
GetProjectEnvResponse,
138-
`${this.apiURL}/api/v1/projects/${projectRef}/${env}`,
139-
{
140-
headers: {
141-
Authorization: `Bearer ${this.accessToken}`,
142-
"Content-Type": "application/json",
143-
},
144-
}
145-
);
134+
return zodfetch(GetProjectEnvResponse, `${this.apiURL}/api/v1/projects/${projectRef}/${env}`, {
135+
headers: {
136+
Authorization: `Bearer ${this.accessToken}`,
137+
"Content-Type": "application/json",
138+
},
139+
});
146140
}
147141

148142
async getEnvironmentVariables(projectRef: string) {
149143
if (!this.accessToken) {
150144
throw new Error("getEnvironmentVariables: No access token");
151145
}
152146

153-
return wrapZodFetch(
147+
return zodfetch(
154148
GetEnvironmentVariablesResponseBody,
155149
`${this.apiURL}/api/v1/projects/${projectRef}/envvars`,
156150
{
@@ -190,7 +184,7 @@ export class CliApiClient {
190184
throw new Error("initializeDeployment: No access token");
191185
}
192186

193-
return wrapZodFetch(InitializeDeploymentResponseBody, `${this.apiURL}/api/v1/deployments`, {
187+
return zodfetch(InitializeDeploymentResponseBody, `${this.apiURL}/api/v1/deployments`, {
194188
method: "POST",
195189
headers: {
196190
Authorization: `Bearer ${this.accessToken}`,
@@ -205,7 +199,7 @@ export class CliApiClient {
205199
throw new Error("startDeploymentIndexing: No access token");
206200
}
207201

208-
return wrapZodFetch(
202+
return zodfetch(
209203
StartDeploymentIndexingResponseBody,
210204
`${this.apiURL}/api/v1/deployments/${deploymentId}/start-indexing`,
211205
{
@@ -224,7 +218,7 @@ export class CliApiClient {
224218
throw new Error("getDeployment: No access token");
225219
}
226220

227-
return wrapZodFetch(
221+
return zodfetch(
228222
GetDeploymentResponseBody,
229223
`${this.apiURL}/api/v1/deployments/${deploymentId}`,
230224
{
@@ -244,42 +238,56 @@ type ApiResult<TSuccessResult> =
244238
error: string;
245239
};
246240

247-
async function wrapZodFetch<T extends z.ZodTypeAny>(
248-
schema: T,
241+
async function zodfetch<TResponseBody extends any>(
242+
schema: z.Schema<TResponseBody>,
249243
url: string,
250244
requestInit?: RequestInit
251-
): Promise<ApiResult<z.infer<T>>> {
245+
): Promise<ApiResult<TResponseBody>> {
252246
try {
253-
const response = await zodfetch(schema, url, requestInit, {
254-
retry: {
255-
minTimeoutInMs: 500,
256-
maxTimeoutInMs: 5000,
257-
maxAttempts: 3,
258-
factor: 2,
259-
randomize: false,
260-
},
261-
});
247+
const response = await fetch(url, requestInit);
262248

263-
return {
264-
success: true,
265-
data: response,
266-
};
267-
} catch (error) {
268-
if (error instanceof APIError) {
249+
if ((!requestInit || requestInit.method === "GET") && response.status === 404) {
269250
return {
270251
success: false,
271-
error: error.message,
252+
error: `404: ${response.statusText}`,
272253
};
273-
} else if (error instanceof Error) {
254+
}
255+
256+
if (response.status >= 400 && response.status < 500) {
257+
const body = await response.json();
258+
if (!body.error) {
259+
return { success: false, error: "Something went wrong" };
260+
}
261+
262+
return { success: false, error: body.error };
263+
}
264+
265+
if (response.status !== 200) {
274266
return {
275267
success: false,
276-
error: error.message,
268+
error: `Failed to fetch ${url}, got status code ${response.status}`,
277269
};
278-
} else {
270+
}
271+
272+
const jsonBody = await response.json();
273+
const parsedResult = schema.safeParse(jsonBody);
274+
275+
if (parsedResult.success) {
276+
return { success: true, data: parsedResult.data };
277+
}
278+
279+
if ("error" in jsonBody) {
279280
return {
280281
success: false,
281-
error: String(error),
282+
error: typeof jsonBody.error === "string" ? jsonBody.error : JSON.stringify(jsonBody.error),
282283
};
283284
}
285+
286+
return { success: false, error: parsedResult.error.message };
287+
} catch (error) {
288+
return {
289+
success: false,
290+
error: error instanceof Error ? error.message : JSON.stringify(error),
291+
};
284292
}
285293
}

0 commit comments

Comments
 (0)