wrangler 0.0.0-ece06ea → 0.0.0-ed08df4

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