Skip to content

Commit d8fcd9b

Browse files
committed
fix(sdk): use nameOrRequestOptions in envvars.update outside a task
The 4-arg management form assigned $name from a binding that is not a parameter. TypeScript accepted it via the DOM global. Node throws ReferenceError before any request. Repro: tsx --eval update("proj_x","dev","FOO",{value:"bar"}) on 0a23814. Guard the string, assign nameOrRequestOptions, add envvars.test.ts. Tests failed with ReferenceError without the assignment, 2 passed after.
1 parent 0a23814 commit d8fcd9b

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
`envvars.update(projectRef, slug, name, params)` no longer throws `ReferenceError: name is not defined` when called from a Node script outside a task run.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
2+
import type { AddressInfo } from "node:net";
3+
import { apiClientManager, taskContext } from "@trigger.dev/core/v3";
4+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
5+
import { update } from "./envvars.js";
6+
7+
type ReceivedRequest = {
8+
method: string;
9+
url: string;
10+
authorization?: string;
11+
body: unknown;
12+
};
13+
14+
describe("envvars.update outside a task", () => {
15+
let server: Server;
16+
let baseUrl: string;
17+
let requests: ReceivedRequest[];
18+
19+
beforeEach(async () => {
20+
requests = [];
21+
apiClientManager.disable();
22+
taskContext.disable();
23+
server = createServer((request, response) => {
24+
void handleRequest(request, response, requests);
25+
});
26+
await new Promise<void>((resolve) => {
27+
server.listen(0, "127.0.0.1", () => {
28+
const address = server.address() as AddressInfo;
29+
baseUrl = `http://127.0.0.1:${address.port}`;
30+
resolve();
31+
});
32+
});
33+
});
34+
35+
afterEach(async () => {
36+
await new Promise<void>((resolve) => server.close(() => resolve()));
37+
apiClientManager.disable();
38+
taskContext.disable();
39+
});
40+
41+
it("PUTs the 4-arg form to /projects/{ref}/envvars/{slug}/{name}", async () => {
42+
const key = "tr_dev_sk_0123456789abcdefghijklmn";
43+
await apiClientManager.runWithConfig({ baseURL: baseUrl, accessToken: key }, () =>
44+
update("proj_x", "dev", "FOO", { value: "bar" })
45+
);
46+
47+
expect(requests).toEqual([
48+
{
49+
method: "PUT",
50+
url: "/api/v1/projects/proj_x/envvars/dev/FOO",
51+
authorization: `Bearer ${key}`,
52+
body: { value: "bar" },
53+
},
54+
]);
55+
});
56+
57+
it("throws name is required when the 4-arg name is missing", () => {
58+
expect(() => update("proj_x", "dev", undefined as unknown as string, { value: "bar" })).toThrow(
59+
"name is required"
60+
);
61+
expect(requests).toEqual([]);
62+
});
63+
});
64+
65+
async function handleRequest(
66+
request: IncomingMessage,
67+
response: ServerResponse,
68+
requests: ReceivedRequest[]
69+
) {
70+
const chunks: Buffer[] = [];
71+
for await (const chunk of request) {
72+
chunks.push(Buffer.from(chunk));
73+
}
74+
const rawBody = Buffer.concat(chunks).toString();
75+
requests.push({
76+
method: request.method ?? "",
77+
url: request.url ?? "",
78+
authorization: request.headers.authorization,
79+
body: rawBody ? JSON.parse(rawBody) : undefined,
80+
});
81+
82+
response.writeHead(200, { "content-type": "application/json" });
83+
response.end(JSON.stringify({ success: true }));
84+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,17 @@ export function update(
332332
throw new Error("projectRef is required");
333333
}
334334

335+
if (typeof nameOrRequestOptions !== "string") {
336+
throw new Error("name is required");
337+
}
338+
335339
if (!params) {
336340
throw new Error("params is required");
337341
}
338342

339343
$projectRef = projectRefOrName;
340344
$slug = slugOrParams;
341-
$name = name!;
345+
$name = nameOrRequestOptions;
342346
$params = params;
343347
}
344348

0 commit comments

Comments
 (0)