wrangler 0.0.0-e61907d → 0.0.0-e95bea2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/README.md +51 -0
  2. package/bin/wrangler.js +36 -2
  3. package/import_meta_url.js +3 -0
  4. package/package.json +79 -21
  5. package/pages/functions/buildWorker.ts +62 -0
  6. package/pages/functions/filepath-routing.test.ts +123 -0
  7. package/pages/functions/filepath-routing.ts +178 -0
  8. package/pages/functions/identifiers.ts +78 -0
  9. package/pages/functions/routes.ts +151 -0
  10. package/pages/functions/template-worker.ts +130 -0
  11. package/src/__tests__/configuration.test.ts +2217 -0
  12. package/src/__tests__/dev.test.tsx +405 -0
  13. package/src/__tests__/dev2.test.tsx +152 -0
  14. package/src/__tests__/guess-worker-format.test.ts +58 -0
  15. package/src/__tests__/helpers/clipboardy-mock.js +4 -0
  16. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  17. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  18. package/src/__tests__/helpers/mock-account-id.ts +44 -0
  19. package/src/__tests__/helpers/mock-bin.ts +36 -0
  20. package/src/__tests__/helpers/mock-cfetch.ts +207 -0
  21. package/src/__tests__/helpers/mock-console.ts +70 -0
  22. package/src/__tests__/helpers/mock-dialogs.ts +65 -0
  23. package/src/__tests__/helpers/mock-kv.ts +40 -0
  24. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  25. package/src/__tests__/helpers/mock-user.ts +27 -0
  26. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  27. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  28. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  29. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  30. package/src/__tests__/https-options.test.ts +127 -0
  31. package/src/__tests__/index.test.ts +991 -0
  32. package/src/__tests__/jest.setup.ts +69 -0
  33. package/src/__tests__/kv.test.ts +1408 -0
  34. package/src/__tests__/logout.test.ts +50 -0
  35. package/src/__tests__/package-manager.test.ts +206 -0
  36. package/src/__tests__/pages.test.ts +44 -0
  37. package/src/__tests__/parse.test.ts +322 -0
  38. package/src/__tests__/publish.test.ts +2453 -0
  39. package/src/__tests__/r2.test.ts +198 -0
  40. package/src/__tests__/route.test.ts +45 -0
  41. package/src/__tests__/secret.test.ts +425 -0
  42. package/src/__tests__/sentry.test.ts +146 -0
  43. package/src/__tests__/tail.test.ts +564 -0
  44. package/src/__tests__/whoami.test.tsx +86 -0
  45. package/src/bundle.ts +142 -0
  46. package/src/cfetch/index.ts +104 -0
  47. package/src/cfetch/internal.ts +135 -0
  48. package/src/cli.ts +19 -0
  49. package/src/config/README.md +107 -0
  50. package/src/config/config.ts +279 -0
  51. package/src/config/diagnostics.ts +80 -0
  52. package/src/config/environment.ts +305 -0
  53. package/src/config/index.ts +52 -0
  54. package/src/config/validation-helpers.ts +388 -0
  55. package/src/config/validation.ts +1027 -0
  56. package/src/create-worker-preview.ts +166 -0
  57. package/src/create-worker-upload-form.ts +217 -0
  58. package/src/dev/dev.tsx +357 -0
  59. package/src/dev/local.tsx +241 -0
  60. package/src/dev/remote.tsx +201 -0
  61. package/src/dev/use-esbuild.ts +100 -0
  62. package/src/dialogs.tsx +77 -0
  63. package/src/entry.ts +159 -0
  64. package/src/environment-variables.ts +33 -0
  65. package/src/https-options.ts +122 -0
  66. package/src/index.tsx +2329 -0
  67. package/src/inspect.ts +617 -0
  68. package/src/kv/index.tsx +339 -0
  69. package/src/module-collection.ts +253 -0
  70. package/src/open-in-browser.ts +25 -0
  71. package/src/package-manager.ts +126 -0
  72. package/src/pages.tsx +1045 -0
  73. package/src/parse.ts +183 -0
  74. package/src/paths.ts +26 -0
  75. package/src/proxy.ts +438 -0
  76. package/src/publish.ts +354 -0
  77. package/src/r2.ts +50 -0
  78. package/src/reporting.ts +131 -0
  79. package/src/selfsigned.d.ts +29 -0
  80. package/src/sites.tsx +238 -0
  81. package/src/tail/filters.ts +279 -0
  82. package/src/tail/index.ts +299 -0
  83. package/src/tail/printing.ts +62 -0
  84. package/src/user.tsx +1065 -0
  85. package/src/whoami.tsx +69 -0
  86. package/src/worker.ts +163 -0
  87. package/templates/new-worker.js +15 -0
  88. package/templates/new-worker.ts +15 -0
  89. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  90. package/templates/tsconfig.json +105 -0
  91. package/wrangler-dist/{index_node.js → cli.js} +105532 -67432
  92. package/wrangler-dist/cli.js.map +7 -0
  93. package/wrangler-dist/index_node.js.map +0 -7
@@ -0,0 +1,198 @@
1
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
2
+ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
3
+ import { mockConsoleMethods } from "./helpers/mock-console";
4
+ import { runInTempDir } from "./helpers/run-in-tmp";
5
+ import { runWrangler } from "./helpers/run-wrangler";
6
+ import type { R2BucketInfo } from "../r2";
7
+
8
+ describe("wrangler", () => {
9
+ mockAccountId();
10
+ mockApiToken();
11
+ runInTempDir();
12
+ const std = mockConsoleMethods();
13
+
14
+ afterEach(() => {
15
+ unsetAllMocks();
16
+ });
17
+
18
+ describe("r2", () => {
19
+ describe("bucket", () => {
20
+ describe("list", () => {
21
+ function mockListRequest(buckets: R2BucketInfo[]) {
22
+ const requests = { count: 0 };
23
+ setMockResponse(
24
+ "/accounts/:accountId/r2/buckets",
25
+ ([_url, accountId], init) => {
26
+ requests.count++;
27
+ expect(accountId).toEqual("some-account-id");
28
+ expect(init).toEqual({});
29
+ return { buckets };
30
+ }
31
+ );
32
+ return requests;
33
+ }
34
+
35
+ it("should list buckets", async () => {
36
+ const expectedBuckets: R2BucketInfo[] = [
37
+ { name: "bucket-1", creation_date: "01-01-2001" },
38
+ { name: "bucket-2", creation_date: "01-01-2001" },
39
+ ];
40
+ mockListRequest(expectedBuckets);
41
+ await runWrangler("r2 bucket list");
42
+
43
+ expect(std.err).toMatchInlineSnapshot(`""`);
44
+ const buckets = JSON.parse(std.out);
45
+ expect(buckets).toEqual(expectedBuckets);
46
+ });
47
+ });
48
+
49
+ describe("create", () => {
50
+ function mockCreateRequest(expectedBucketName: string) {
51
+ const requests = { count: 0 };
52
+ setMockResponse(
53
+ "/accounts/:accountId/r2/buckets/:bucketName",
54
+ "PUT",
55
+ ([_url, accountId, bucketName]) => {
56
+ expect(accountId).toEqual("some-account-id");
57
+ expect(bucketName).toEqual(expectedBucketName);
58
+ requests.count += 1;
59
+ }
60
+ );
61
+ return requests;
62
+ }
63
+
64
+ it("should error if no bucket name is given", async () => {
65
+ await expect(
66
+ runWrangler("r2 bucket create")
67
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
68
+ `"Not enough non-option arguments: got 0, need at least 1"`
69
+ );
70
+ expect(std.out).toMatchInlineSnapshot(`""`);
71
+ expect(std.err).toMatchInlineSnapshot(`
72
+ "wrangler r2 bucket create <name>
73
+
74
+ Create a new R2 bucket
75
+
76
+ Positionals:
77
+ name The name of the new bucket [string] [required]
78
+
79
+ Flags:
80
+ -c, --config Path to .toml configuration file [string]
81
+ -h, --help Show help [boolean]
82
+ -v, --version Show version number [boolean]
83
+ --legacy-env Use legacy environments [boolean]
84
+
85
+ Not enough non-option arguments: got 0, need at least 1"
86
+ `);
87
+ });
88
+
89
+ it("should error if the bucket to create contains spaces", async () => {
90
+ await expect(
91
+ runWrangler("r2 bucket create abc def ghi")
92
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
93
+ `"Unexpected additional positional arguments \\"def ghi\\"."`
94
+ );
95
+ expect(std.out).toMatchInlineSnapshot(`""`);
96
+ expect(std.err).toMatchInlineSnapshot(`
97
+ "wrangler r2 bucket create <name>
98
+
99
+ Create a new R2 bucket
100
+
101
+ Positionals:
102
+ name The name of the new bucket [string] [required]
103
+
104
+ Flags:
105
+ -c, --config Path to .toml configuration file [string]
106
+ -h, --help Show help [boolean]
107
+ -v, --version Show version number [boolean]
108
+ --legacy-env Use legacy environments [boolean]
109
+
110
+ Unexpected additional positional arguments \\"def ghi\\"."
111
+ `);
112
+ });
113
+
114
+ it("should create a bucket", async () => {
115
+ const requests = mockCreateRequest("testBucket");
116
+ await runWrangler("r2 bucket create testBucket");
117
+ expect(std.out).toMatchInlineSnapshot(`
118
+ "Creating bucket testBucket.
119
+ Created bucket testBucket."
120
+ `);
121
+ expect(requests.count).toEqual(1);
122
+ });
123
+ });
124
+
125
+ describe("delete", () => {
126
+ function mockDeleteRequest(expectedBucketName: string) {
127
+ const requests = { count: 0 };
128
+ setMockResponse(
129
+ "/accounts/:accountId/r2/buckets/:bucketName",
130
+ "DELETE",
131
+ ([_url, accountId, bucketName]) => {
132
+ expect(accountId).toEqual("some-account-id");
133
+ expect(bucketName).toEqual(expectedBucketName);
134
+ requests.count += 1;
135
+ }
136
+ );
137
+ return requests;
138
+ }
139
+
140
+ it("should error if no bucket name is given", async () => {
141
+ await expect(
142
+ runWrangler("r2 bucket delete")
143
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
144
+ `"Not enough non-option arguments: got 0, need at least 1"`
145
+ );
146
+ expect(std.out).toMatchInlineSnapshot(`""`);
147
+ expect(std.err).toMatchInlineSnapshot(`
148
+ "wrangler r2 bucket delete <name>
149
+
150
+ Delete an R2 bucket
151
+
152
+ Positionals:
153
+ name The name of the bucket to delete [string] [required]
154
+
155
+ Flags:
156
+ -c, --config Path to .toml configuration file [string]
157
+ -h, --help Show help [boolean]
158
+ -v, --version Show version number [boolean]
159
+ --legacy-env Use legacy environments [boolean]
160
+
161
+ Not enough non-option arguments: got 0, need at least 1"
162
+ `);
163
+ });
164
+
165
+ it("should error if the bucket name to delete contains spaces", async () => {
166
+ await expect(
167
+ runWrangler("r2 bucket delete abc def ghi")
168
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
169
+ `"Unexpected additional positional arguments \\"def ghi\\"."`
170
+ );
171
+ expect(std.out).toMatchInlineSnapshot(`""`);
172
+ expect(std.err).toMatchInlineSnapshot(`
173
+ "wrangler r2 bucket delete <name>
174
+
175
+ Delete an R2 bucket
176
+
177
+ Positionals:
178
+ name The name of the bucket to delete [string] [required]
179
+
180
+ Flags:
181
+ -c, --config Path to .toml configuration file [string]
182
+ -h, --help Show help [boolean]
183
+ -v, --version Show version number [boolean]
184
+ --legacy-env Use legacy environments [boolean]
185
+
186
+ Unexpected additional positional arguments \\"def ghi\\"."
187
+ `);
188
+ });
189
+
190
+ it("should delete a bucket specified by name", async () => {
191
+ const requests = mockDeleteRequest("some-bucket");
192
+ await runWrangler(`r2 bucket delete some-bucket`);
193
+ expect(requests.count).toEqual(1);
194
+ });
195
+ });
196
+ });
197
+ });
198
+ });
@@ -0,0 +1,45 @@
1
+ import { mockConsoleMethods } from "./helpers/mock-console";
2
+ import { runInTempDir } from "./helpers/run-in-tmp";
3
+ import { runWrangler } from "./helpers/run-wrangler";
4
+
5
+ describe("wrangler route", () => {
6
+ mockConsoleMethods();
7
+ runInTempDir();
8
+
9
+ it("shows a deprecation notice when `wrangler route` is run", async () => {
10
+ await expect(runWrangler("route")).rejects
11
+ .toThrowErrorMatchingInlineSnapshot(`
12
+ "DEPRECATION WARNING:
13
+ \`wrangler route\` has been deprecated.
14
+ Please use wrangler.toml and/or \`wrangler publish --routes\` to modify routes"
15
+ `);
16
+ });
17
+
18
+ it("shows a deprecation notice when `wrangler route delete` is run", async () => {
19
+ await expect(runWrangler("route delete")).rejects
20
+ .toThrowErrorMatchingInlineSnapshot(`
21
+ "DEPRECATION WARNING:
22
+ \`wrangler route delete\` has been deprecated.
23
+ Remove the unwanted route(s) from wrangler.toml and run \`wrangler publish\` to remove your worker from those routes."
24
+ `);
25
+ });
26
+
27
+ it("shows a deprecation notice when `wrangler route delete <id>` is run", async () => {
28
+ await expect(runWrangler("route delete some-zone-id")).rejects
29
+ .toThrowErrorMatchingInlineSnapshot(`
30
+ "DEPRECATION WARNING:
31
+ \`wrangler route delete\` has been deprecated.
32
+ Remove the unwanted route(s) from wrangler.toml and run \`wrangler publish\` to remove your worker from those routes."
33
+ `);
34
+ });
35
+
36
+ it("shows a deprecation notice when `wrangler route list` is run", async () => {
37
+ await expect(runWrangler("route list")).rejects
38
+ .toThrowErrorMatchingInlineSnapshot(`
39
+ "DEPRECATION WARNING:
40
+ \`wrangler route list\` has been deprecated.
41
+ Refer to wrangler.toml for a list of routes the worker will be deployed to upon publishing.
42
+ Refer to the Cloudflare Dashboard to see the routes this worker is currently running on."
43
+ `);
44
+ });
45
+ });
@@ -0,0 +1,425 @@
1
+ import * as fs from "node:fs";
2
+ import * as TOML from "@iarna/toml";
3
+ import fetchMock from "jest-fetch-mock";
4
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
5
+ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
6
+ import { mockConsoleMethods } from "./helpers/mock-console";
7
+ import { mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
8
+ import { useMockStdin } from "./helpers/mock-stdin";
9
+ import { runInTempDir } from "./helpers/run-in-tmp";
10
+ import { runWrangler } from "./helpers/run-wrangler";
11
+
12
+ describe("wrangler secret", () => {
13
+ const std = mockConsoleMethods();
14
+ runInTempDir();
15
+ mockAccountId();
16
+ mockApiToken();
17
+
18
+ afterEach(() => {
19
+ unsetAllMocks();
20
+ });
21
+
22
+ describe("put", () => {
23
+ function mockPutRequest(
24
+ input: { name: string; text: string },
25
+ env?: string,
26
+ legacyEnv = false
27
+ ) {
28
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
29
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
30
+ setMockResponse(
31
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets`,
32
+ "PUT",
33
+ ([_url, accountId, scriptName, envName], { body }) => {
34
+ expect(accountId).toEqual("some-account-id");
35
+ expect(scriptName).toEqual(
36
+ legacyEnv && env ? `script-name-${env}` : "script-name"
37
+ );
38
+ if (!legacyEnv) {
39
+ expect(envName).toEqual(env);
40
+ }
41
+ const { name, text, type } = JSON.parse(body as string);
42
+ expect(type).toEqual("secret_text");
43
+ expect(name).toEqual(input.name);
44
+ expect(text).toEqual(input.text);
45
+
46
+ return { name, type };
47
+ }
48
+ );
49
+ }
50
+
51
+ describe("interactive", () => {
52
+ useMockStdin({ isTTY: true });
53
+
54
+ it("should create a secret", async () => {
55
+ mockPrompt({
56
+ text: "Enter a secret value:",
57
+ type: "password",
58
+ result: "the-secret",
59
+ });
60
+
61
+ mockPutRequest({ name: "the-key", text: "the-secret" });
62
+ await runWrangler("secret put the-key --name script-name");
63
+
64
+ expect(std.out).toMatchInlineSnapshot(`
65
+ "🌀 Creating the secret for script script-name
66
+ ✨ Success! Uploaded secret the-key"
67
+ `);
68
+ expect(std.err).toMatchInlineSnapshot(`""`);
69
+ });
70
+
71
+ it("should create a secret: legacy envs", async () => {
72
+ mockPrompt({
73
+ text: "Enter a secret value:",
74
+ type: "password",
75
+ result: "the-secret",
76
+ });
77
+
78
+ mockPutRequest(
79
+ { name: "the-key", text: "the-secret" },
80
+ "some-env",
81
+ true
82
+ );
83
+ await runWrangler(
84
+ "secret put the-key --name script-name --env some-env --legacy-env"
85
+ );
86
+
87
+ expect(std.out).toMatchInlineSnapshot(`
88
+ "🌀 Creating the secret for script script-name-some-env
89
+ ✨ Success! Uploaded secret the-key"
90
+ `);
91
+ expect(std.err).toMatchInlineSnapshot(`""`);
92
+ });
93
+
94
+ it("should create a secret: service envs", async () => {
95
+ mockPrompt({
96
+ text: "Enter a secret value:",
97
+ type: "password",
98
+ result: "the-secret",
99
+ });
100
+
101
+ mockPutRequest(
102
+ { name: "the-key", text: "the-secret" },
103
+ "some-env",
104
+ false
105
+ );
106
+ await runWrangler(
107
+ "secret put the-key --name script-name --env some-env --legacy-env false"
108
+ );
109
+
110
+ expect(std.out).toMatchInlineSnapshot(`
111
+ "🌀 Creating the secret for script script-name (some-env)
112
+ ✨ Success! Uploaded secret the-key"
113
+ `);
114
+ expect(std.err).toMatchInlineSnapshot(`""`);
115
+ });
116
+
117
+ it("should error without a script name", async () => {
118
+ let error: Error | undefined;
119
+ try {
120
+ await runWrangler("secret put the-key");
121
+ } catch (e) {
122
+ error = e as Error;
123
+ }
124
+ expect(std.out).toMatchInlineSnapshot(`""`);
125
+ expect(std.err).toMatchInlineSnapshot(`
126
+ "Missing script name
127
+
128
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
129
+ `);
130
+ expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
131
+ });
132
+ });
133
+
134
+ describe("non-interactive", () => {
135
+ const mockStdIn = useMockStdin({ isTTY: false });
136
+
137
+ it("should create a secret, from piped input", async () => {
138
+ mockPutRequest({ name: "the-key", text: "the-secret" });
139
+ // Pipe the secret in as three chunks to test that we reconstitute it correctly.
140
+ mockStdIn.send("the", "-", "secret");
141
+ await runWrangler("secret put the-key --name script-name");
142
+
143
+ expect(std.out).toMatchInlineSnapshot(`
144
+ "🌀 Creating the secret for script script-name
145
+ ✨ Success! Uploaded secret the-key"
146
+ `);
147
+ expect(std.warn).toMatchInlineSnapshot(`""`);
148
+ expect(std.err).toMatchInlineSnapshot(`""`);
149
+ });
150
+
151
+ it("should error if the piped input fails", async () => {
152
+ mockPutRequest({ name: "the-key", text: "the-secret" });
153
+ mockStdIn.throwError(new Error("Error in stdin stream"));
154
+ await expect(
155
+ runWrangler("secret put the-key --name script-name")
156
+ ).rejects.toThrowErrorMatchingInlineSnapshot(`"Error in stdin stream"`);
157
+
158
+ expect(std.out).toMatchInlineSnapshot(`""`);
159
+ expect(std.warn).toMatchInlineSnapshot(`""`);
160
+ });
161
+
162
+ describe("with accountId", () => {
163
+ mockAccountId({ accountId: null });
164
+
165
+ it("should error if a user has no account", async () => {
166
+ await expect(
167
+ runWrangler("secret put the-key --name script-name")
168
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
169
+ `"No account id found, quitting..."`
170
+ );
171
+ });
172
+
173
+ it("should use the account from wrangler.toml", async () => {
174
+ fs.writeFileSync(
175
+ "wrangler.toml",
176
+ TOML.stringify({
177
+ account_id: "some-account-id",
178
+ }),
179
+ "utf-8"
180
+ );
181
+ mockStdIn.send("the-secret");
182
+ mockPutRequest({ name: "the-key", text: "the-secret" });
183
+ await runWrangler("secret put the-key --name script-name");
184
+ expect(std.out).toMatchInlineSnapshot(`
185
+ "🌀 Creating the secret for script script-name
186
+ ✨ Success! Uploaded secret the-key"
187
+ `);
188
+ expect(std.warn).toMatchInlineSnapshot(`""`);
189
+ expect(std.err).toMatchInlineSnapshot(`""`);
190
+ });
191
+
192
+ it("should error if a user has multiple accounts, and has not specified an account in wrangler.toml", async () => {
193
+ // This is a mock response for the request to the CF API memberships of the current user.
194
+ fetchMock.doMockOnce(async () => {
195
+ return {
196
+ body: JSON.stringify({
197
+ success: true,
198
+ result: [
199
+ {
200
+ id: "1",
201
+ account: { id: "account-id-1", name: "account-name-1" },
202
+ },
203
+ {
204
+ id: "2",
205
+ account: { id: "account-id-2", name: "account-name-2" },
206
+ },
207
+ {
208
+ id: "3",
209
+ account: { id: "account-id-3", name: "account-name-3" },
210
+ },
211
+ ],
212
+ }),
213
+ };
214
+ });
215
+ await expect(runWrangler("secret put the-key --name script-name"))
216
+ .rejects.toThrowErrorMatchingInlineSnapshot(`
217
+ "More than one account available but unable to select one in non-interactive mode.
218
+ Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
219
+ Available accounts are (\\"<name>\\" - \\"<id>\\"):
220
+ \\"account-name-1\\" - \\"account-id-1\\")
221
+ \\"account-name-2\\" - \\"account-id-2\\")
222
+ \\"account-name-3\\" - \\"account-id-3\\")"
223
+ `);
224
+ });
225
+ });
226
+ });
227
+ });
228
+
229
+ describe("delete", () => {
230
+ function mockDeleteRequest(
231
+ input: {
232
+ scriptName: string;
233
+ secretName: string;
234
+ },
235
+ env?: string,
236
+ legacyEnv = false
237
+ ) {
238
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
239
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
240
+ setMockResponse(
241
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets/:secretName`,
242
+ "DELETE",
243
+ ([_url, accountId, scriptName, envOrSecretName, secretName]) => {
244
+ expect(accountId).toEqual("some-account-id");
245
+ expect(scriptName).toEqual(
246
+ legacyEnv && env ? `script-name-${env}` : "script-name"
247
+ );
248
+ if (!legacyEnv) {
249
+ if (env) {
250
+ expect(envOrSecretName).toEqual(env);
251
+ expect(secretName).toEqual(input.secretName);
252
+ } else {
253
+ expect(envOrSecretName).toEqual(input.secretName);
254
+ }
255
+ } else {
256
+ expect(envOrSecretName).toEqual(input.secretName);
257
+ }
258
+ return null;
259
+ }
260
+ );
261
+ }
262
+ it("should delete a secret", async () => {
263
+ mockDeleteRequest({ scriptName: "script-name", secretName: "the-key" });
264
+ mockConfirm({
265
+ text: "Are you sure you want to permanently delete the variable the-key on the script script-name?",
266
+ result: true,
267
+ });
268
+ await runWrangler("secret delete the-key --name script-name");
269
+ expect(std.out).toMatchInlineSnapshot(`
270
+ "🌀 Deleting the secret the-key on script script-name
271
+ ✨ Success! Deleted secret the-key"
272
+ `);
273
+ expect(std.err).toMatchInlineSnapshot(`""`);
274
+ });
275
+
276
+ it("should delete a secret: legacy envs", async () => {
277
+ mockDeleteRequest(
278
+ { scriptName: "script-name", secretName: "the-key" },
279
+ "some-env",
280
+ true
281
+ );
282
+ mockConfirm({
283
+ text: "Are you sure you want to permanently delete the variable the-key on the script script-name-some-env?",
284
+ result: true,
285
+ });
286
+ await runWrangler(
287
+ "secret delete the-key --name script-name --env some-env --legacy-env"
288
+ );
289
+ expect(std.out).toMatchInlineSnapshot(`
290
+ "🌀 Deleting the secret the-key on script script-name-some-env
291
+ ✨ Success! Deleted secret the-key"
292
+ `);
293
+ expect(std.err).toMatchInlineSnapshot(`""`);
294
+ });
295
+
296
+ it("should delete a secret: service envs", async () => {
297
+ mockDeleteRequest(
298
+ { scriptName: "script-name", secretName: "the-key" },
299
+ "some-env"
300
+ );
301
+ mockConfirm({
302
+ text: "Are you sure you want to permanently delete the variable the-key on the script script-name (some-env)?",
303
+ result: true,
304
+ });
305
+ await runWrangler(
306
+ "secret delete the-key --name script-name --env some-env --legacy-env false"
307
+ );
308
+ expect(std.out).toMatchInlineSnapshot(`
309
+ "🌀 Deleting the secret the-key on script script-name (some-env)
310
+ ✨ Success! Deleted secret the-key"
311
+ `);
312
+ expect(std.err).toMatchInlineSnapshot(`""`);
313
+ });
314
+
315
+ it("should error without a script name", async () => {
316
+ let error: Error | undefined;
317
+
318
+ try {
319
+ await runWrangler("secret delete the-key");
320
+ } catch (e) {
321
+ error = e as Error;
322
+ }
323
+ expect(std.out).toMatchInlineSnapshot(`""`);
324
+ expect(std.err).toMatchInlineSnapshot(`
325
+ "Missing script name
326
+
327
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
328
+ `);
329
+ expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
330
+ });
331
+ });
332
+
333
+ describe("list", () => {
334
+ function mockListRequest(
335
+ input: { scriptName: string },
336
+ env?: string,
337
+ legacyEnv = false
338
+ ) {
339
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
340
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
341
+ setMockResponse(
342
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets`,
343
+ "GET",
344
+ ([_url, accountId, scriptName, envName]) => {
345
+ expect(accountId).toEqual("some-account-id");
346
+ expect(scriptName).toEqual(
347
+ legacyEnv && env ? `script-name-${env}` : "script-name"
348
+ );
349
+ if (!legacyEnv) {
350
+ expect(envName).toEqual(env);
351
+ }
352
+
353
+ return [
354
+ {
355
+ name: "the-secret-name",
356
+ type: "secret_text",
357
+ },
358
+ ];
359
+ }
360
+ );
361
+ }
362
+
363
+ it("should list secrets", async () => {
364
+ mockListRequest({ scriptName: "script-name" });
365
+ await runWrangler("secret list --name script-name");
366
+ expect(std.out).toMatchInlineSnapshot(`
367
+ "[
368
+ {
369
+ \\"name\\": \\"the-secret-name\\",
370
+ \\"type\\": \\"secret_text\\"
371
+ }
372
+ ]"
373
+ `);
374
+ expect(std.err).toMatchInlineSnapshot(`""`);
375
+ });
376
+
377
+ it("should list secrets: legacy envs", async () => {
378
+ mockListRequest({ scriptName: "script-name" }, "some-env", true);
379
+ await runWrangler(
380
+ "secret list --name script-name --env some-env --legacy-env"
381
+ );
382
+ expect(std.out).toMatchInlineSnapshot(`
383
+ "[
384
+ {
385
+ \\"name\\": \\"the-secret-name\\",
386
+ \\"type\\": \\"secret_text\\"
387
+ }
388
+ ]"
389
+ `);
390
+ expect(std.err).toMatchInlineSnapshot(`""`);
391
+ });
392
+
393
+ it("should list secrets: service envs", async () => {
394
+ mockListRequest({ scriptName: "script-name" }, "some-env");
395
+ await runWrangler(
396
+ "secret list --name script-name --env some-env --legacy-env false"
397
+ );
398
+ expect(std.out).toMatchInlineSnapshot(`
399
+ "[
400
+ {
401
+ \\"name\\": \\"the-secret-name\\",
402
+ \\"type\\": \\"secret_text\\"
403
+ }
404
+ ]"
405
+ `);
406
+ expect(std.err).toMatchInlineSnapshot(`""`);
407
+ });
408
+
409
+ it("should error without a script name", async () => {
410
+ let error: Error | undefined;
411
+ try {
412
+ await runWrangler("secret list");
413
+ } catch (e) {
414
+ error = e as Error;
415
+ }
416
+ expect(std.out).toMatchInlineSnapshot(`""`);
417
+ expect(std.err).toMatchInlineSnapshot(`
418
+ "Missing script name
419
+
420
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
421
+ `);
422
+ expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
423
+ });
424
+ });
425
+ });