@@ -17,9 +17,7 @@ import {
17
17
ImportEnvironmentVariablesRequestBody ,
18
18
EnvironmentVariableResponseBody ,
19
19
TaskRunExecution ,
20
- APIError ,
21
20
} from "@trigger.dev/core/v3" ;
22
- import { zodfetch } from "@trigger.dev/core/v3/zodfetch" ;
23
21
24
22
export class CliApiClient {
25
23
private readonly apiURL : string ;
@@ -32,7 +30,7 @@ export class CliApiClient {
32
30
}
33
31
34
32
async createAuthorizationCode ( ) {
35
- return wrapZodFetch (
33
+ return zodfetch (
36
34
CreateAuthorizationCodeResponseSchema ,
37
35
`${ this . apiURL } /api/v1/authorization-code` ,
38
36
{
@@ -42,7 +40,7 @@ export class CliApiClient {
42
40
}
43
41
44
42
async getPersonalAccessToken ( authorizationCode : string ) {
45
- return wrapZodFetch ( GetPersonalAccessTokenResponseSchema , `${ this . apiURL } /api/v1/token` , {
43
+ return zodfetch ( GetPersonalAccessTokenResponseSchema , `${ this . apiURL } /api/v1/token` , {
46
44
method : "POST" ,
47
45
body : JSON . stringify ( {
48
46
authorizationCode,
@@ -55,7 +53,7 @@ export class CliApiClient {
55
53
throw new Error ( "whoAmI: No access token" ) ;
56
54
}
57
55
58
- return wrapZodFetch ( WhoAmIResponseSchema , `${ this . apiURL } /api/v2/whoami` , {
56
+ return zodfetch ( WhoAmIResponseSchema , `${ this . apiURL } /api/v2/whoami` , {
59
57
headers : {
60
58
Authorization : `Bearer ${ this . accessToken } ` ,
61
59
"Content-Type" : "application/json" ,
@@ -68,7 +66,7 @@ export class CliApiClient {
68
66
throw new Error ( "getProject: No access token" ) ;
69
67
}
70
68
71
- return wrapZodFetch ( GetProjectResponseBody , `${ this . apiURL } /api/v1/projects/${ projectRef } ` , {
69
+ return zodfetch ( GetProjectResponseBody , `${ this . apiURL } /api/v1/projects/${ projectRef } ` , {
72
70
headers : {
73
71
Authorization : `Bearer ${ this . accessToken } ` ,
74
72
"Content-Type" : "application/json" ,
@@ -81,7 +79,7 @@ export class CliApiClient {
81
79
throw new Error ( "getProjects: No access token" ) ;
82
80
}
83
81
84
- return wrapZodFetch ( GetProjectsResponseBody , `${ this . apiURL } /api/v1/projects` , {
82
+ return zodfetch ( GetProjectsResponseBody , `${ this . apiURL } /api/v1/projects` , {
85
83
headers : {
86
84
Authorization : `Bearer ${ this . accessToken } ` ,
87
85
"Content-Type" : "application/json" ,
@@ -94,7 +92,7 @@ export class CliApiClient {
94
92
throw new Error ( "createBackgroundWorker: No access token" ) ;
95
93
}
96
94
97
- return wrapZodFetch (
95
+ return zodfetch (
98
96
CreateBackgroundWorkerResponse ,
99
97
`${ this . apiURL } /api/v1/projects/${ projectRef } /background-workers` ,
100
98
{
@@ -113,7 +111,7 @@ export class CliApiClient {
113
111
throw new Error ( "creatTaskRunAttempt: No access token" ) ;
114
112
}
115
113
116
- return wrapZodFetch ( TaskRunExecution , `${ this . apiURL } /api/v1/runs/${ runFriendlyId } /attempts` , {
114
+ return zodfetch ( TaskRunExecution , `${ this . apiURL } /api/v1/runs/${ runFriendlyId } /attempts` , {
117
115
method : "POST" ,
118
116
headers : {
119
117
Authorization : `Bearer ${ this . accessToken } ` ,
@@ -133,24 +131,20 @@ export class CliApiClient {
133
131
throw new Error ( "getProjectDevEnv: No access token" ) ;
134
132
}
135
133
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
+ } ) ;
146
140
}
147
141
148
142
async getEnvironmentVariables ( projectRef : string ) {
149
143
if ( ! this . accessToken ) {
150
144
throw new Error ( "getEnvironmentVariables: No access token" ) ;
151
145
}
152
146
153
- return wrapZodFetch (
147
+ return zodfetch (
154
148
GetEnvironmentVariablesResponseBody ,
155
149
`${ this . apiURL } /api/v1/projects/${ projectRef } /envvars` ,
156
150
{
@@ -190,7 +184,7 @@ export class CliApiClient {
190
184
throw new Error ( "initializeDeployment: No access token" ) ;
191
185
}
192
186
193
- return wrapZodFetch ( InitializeDeploymentResponseBody , `${ this . apiURL } /api/v1/deployments` , {
187
+ return zodfetch ( InitializeDeploymentResponseBody , `${ this . apiURL } /api/v1/deployments` , {
194
188
method : "POST" ,
195
189
headers : {
196
190
Authorization : `Bearer ${ this . accessToken } ` ,
@@ -205,7 +199,7 @@ export class CliApiClient {
205
199
throw new Error ( "startDeploymentIndexing: No access token" ) ;
206
200
}
207
201
208
- return wrapZodFetch (
202
+ return zodfetch (
209
203
StartDeploymentIndexingResponseBody ,
210
204
`${ this . apiURL } /api/v1/deployments/${ deploymentId } /start-indexing` ,
211
205
{
@@ -224,7 +218,7 @@ export class CliApiClient {
224
218
throw new Error ( "getDeployment: No access token" ) ;
225
219
}
226
220
227
- return wrapZodFetch (
221
+ return zodfetch (
228
222
GetDeploymentResponseBody ,
229
223
`${ this . apiURL } /api/v1/deployments/${ deploymentId } ` ,
230
224
{
@@ -244,42 +238,56 @@ type ApiResult<TSuccessResult> =
244
238
error : string ;
245
239
} ;
246
240
247
- async function wrapZodFetch < T extends z . ZodTypeAny > (
248
- schema : T ,
241
+ async function zodfetch < TResponseBody extends any > (
242
+ schema : z . Schema < TResponseBody > ,
249
243
url : string ,
250
244
requestInit ?: RequestInit
251
- ) : Promise < ApiResult < z . infer < T > > > {
245
+ ) : Promise < ApiResult < TResponseBody > > {
252
246
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 ) ;
262
248
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 ) {
269
250
return {
270
251
success : false ,
271
- error : error . message ,
252
+ error : `404: ${ response . statusText } ` ,
272
253
} ;
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 ) {
274
266
return {
275
267
success : false ,
276
- error : error . message ,
268
+ error : `Failed to fetch ${ url } , got status code ${ response . status } ` ,
277
269
} ;
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 ) {
279
280
return {
280
281
success : false ,
281
- error : String ( error ) ,
282
+ error : typeof jsonBody . error === "string" ? jsonBody . error : JSON . stringify ( jsonBody . error ) ,
282
283
} ;
283
284
}
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
+ } ;
284
292
}
285
293
}
0 commit comments