Skip to content

Commit 86adbb4

Browse files
committed
WIP envvar SDK
1 parent b0d12cc commit 86adbb4

File tree

7 files changed

+392
-13
lines changed

7 files changed

+392
-13
lines changed

packages/core/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,14 @@
134134
"@opentelemetry/sdk-trace-base": "^1.22.0",
135135
"@opentelemetry/sdk-trace-node": "^1.22.0",
136136
"@opentelemetry/semantic-conventions": "^1.22.0",
137+
"form-data-encoder": "^4.0.2",
137138
"humanize-duration": "^3.27.3",
139+
"socket.io-client": "4.7.4",
138140
"superjson": "^2.2.1",
139141
"ulidx": "^2.2.1",
140142
"zod": "3.22.3",
141143
"zod-error": "1.5.0",
142-
"zod-validation-error": "^1.5.0",
143-
"socket.io-client": "4.7.4"
144+
"zod-validation-error": "^1.5.0"
144145
},
145146
"devDependencies": {
146147
"@trigger.dev/tsconfig": "workspace:*",
@@ -150,10 +151,10 @@
150151
"@types/node": "20.12.7",
151152
"jest": "^29.6.2",
152153
"rimraf": "^3.0.2",
154+
"socket.io": "4.7.4",
153155
"ts-jest": "^29.1.1",
154156
"tsup": "^8.0.1",
155-
"typescript": "^5.3.0",
156-
"socket.io": "4.7.4"
157+
"typescript": "^5.3.0"
157158
},
158159
"engines": {
159160
"node": ">=18.0.0"

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import {
66
BatchTriggerTaskRequestBody,
77
BatchTriggerTaskResponse,
88
CanceledRunResponse,
9+
CreateEnvironmentVariableRequestBody,
910
CreateScheduleOptions,
1011
CreateUploadPayloadUrlResponseBody,
1112
DeletedScheduleObject,
13+
EnvironmentVariableResponseBody,
14+
EnvironmentVariableValue,
15+
EnvironmentVariables,
1216
ListScheduleOptions,
1317
ListSchedulesResult,
1418
ReplayRunResponse,
@@ -20,7 +24,8 @@ import {
2024
UpdateScheduleOptions,
2125
} from "../schemas";
2226
import { taskContext } from "../task-context-api";
23-
import { ZodFetchOptions, zodfetch } from "../zodfetch";
27+
import { ZodFetchOptions, isRecordLike, zodfetch, zodupload } from "../zodfetch";
28+
import { ImportEnvironmentVariablesParams } from "./types";
2429

2530
export type TriggerOptions = {
2631
spanParentAsLink?: boolean;
@@ -234,6 +239,92 @@ export class ApiClient {
234239
});
235240
}
236241

242+
listEnvVars(projectRef: string, slug: string) {
243+
return zodfetch(
244+
EnvironmentVariables,
245+
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}`,
246+
{
247+
method: "GET",
248+
headers: this.#getHeaders(false),
249+
}
250+
);
251+
}
252+
253+
importEnvVars(projectRef: string, slug: string, body: ImportEnvironmentVariablesParams) {
254+
if (isRecordLike(body.variables)) {
255+
return zodfetch(
256+
EnvironmentVariableResponseBody,
257+
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}/import`,
258+
{
259+
method: "POST",
260+
headers: this.#getHeaders(false),
261+
body: JSON.stringify(body),
262+
}
263+
);
264+
} else {
265+
return zodupload(
266+
EnvironmentVariableResponseBody,
267+
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}/import`,
268+
body,
269+
{
270+
method: "POST",
271+
headers: this.#getHeaders(false),
272+
}
273+
);
274+
}
275+
}
276+
277+
retrieveEnvVar(projectRef: string, slug: string, key: string) {
278+
return zodfetch(
279+
EnvironmentVariableValue,
280+
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}/${key}`,
281+
{
282+
method: "GET",
283+
headers: this.#getHeaders(false),
284+
}
285+
);
286+
}
287+
288+
createEnvVar(projectRef: string, slug: string, body: CreateEnvironmentVariableRequestBody) {
289+
return zodfetch(
290+
EnvironmentVariableResponseBody,
291+
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}`,
292+
{
293+
method: "POST",
294+
headers: this.#getHeaders(false),
295+
body: JSON.stringify(body),
296+
}
297+
);
298+
}
299+
300+
updateEnvVar(
301+
projectRef: string,
302+
slug: string,
303+
key: string,
304+
body: CreateEnvironmentVariableRequestBody
305+
) {
306+
return zodfetch(
307+
EnvironmentVariableResponseBody,
308+
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}/${key}`,
309+
{
310+
method: "PUT",
311+
headers: this.#getHeaders(false),
312+
body: JSON.stringify(body),
313+
}
314+
);
315+
}
316+
317+
deleteEnvVar(projectRef: string, slug: string, key: string) {
318+
return zodfetch(
319+
EnvironmentVariableResponseBody,
320+
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}/${key}`,
321+
{
322+
method: "DELETE",
323+
headers: this.#getHeaders(false),
324+
}
325+
);
326+
}
327+
237328
#getHeaders(spanParentAsLink: boolean) {
238329
const headers: Record<string, string> = {
239330
"Content-Type": "application/json",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { BlobLikePart, Uploadable } from "../zodfetch";
2+
3+
export interface ImportEnvironmentVariablesParams {
4+
/**
5+
* The variables to be imported. If a variable with the same key already exists, it will be overwritten when `overwrite` is `true`.
6+
*
7+
* There are two ways to specify the variables:
8+
*
9+
* 1. As a record of key-value pairs. e.g. `{ "key1": "value1", "key2": "value2" }`
10+
* 2. As an "uploadable" object in dotenv format. An uploadable can be a Node readable stream, a string, or a Buffer. You can also pass the return value of a `fetch` call.
11+
*/
12+
variables: Uploadable | BlobLikePart | Record<string, string>;
13+
14+
overwrite?: boolean;
15+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class APIClientManagerAPI {
3333

3434
get accessToken(): string | undefined {
3535
const store = this.#getConfig();
36-
return store?.secretKey ?? getEnvVar("TRIGGER_SECRET_KEY");
36+
return store?.secretKey ?? getEnvVar("TRIGGER_SECRET_KEY") ?? getEnvVar("TRIGGER_ACCESS_TOKEN");
3737
}
3838

3939
get client(): ApiClient | undefined {

packages/core/src/v3/schemas/api.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,18 @@ export const ImportEnvironmentVariablesRequestBody = z.object({
389389
export type ImportEnvironmentVariablesRequestBody = z.infer<
390390
typeof ImportEnvironmentVariablesRequestBody
391391
>;
392+
393+
export const EnvironmentVariableResponseBody = z.object({
394+
success: z.boolean(),
395+
});
396+
397+
export const EnvironmentVariableValue = z.object({
398+
value: z.string(),
399+
});
400+
401+
export const EnvironmentVariable = z.object({
402+
key: z.string(),
403+
value: z.string(),
404+
});
405+
406+
export const EnvironmentVariables = z.array(EnvironmentVariable);

0 commit comments

Comments
 (0)