wrangler 0.0.0-e0d2f35 → 0.0.0-e11e1691

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 (130) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -16
  3. package/bin/wrangler.js +122 -31
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +176 -80
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +17 -0
  11. package/templates/gitignore +167 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +136 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-d1-beta.d.ts +3 -0
  19. package/templates/middleware/middleware-ensure-req-body-drained.ts +19 -0
  20. package/templates/middleware/middleware-miniflare3-json-error.ts +33 -0
  21. package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
  22. package/templates/middleware/middleware-multiworker-dev.ts +68 -0
  23. package/templates/middleware/middleware-pretty-error.ts +40 -0
  24. package/templates/middleware/middleware-scheduled.ts +15 -0
  25. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  26. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  27. package/templates/modules-watch-stub.js +4 -0
  28. package/templates/new-worker-scheduled.js +17 -0
  29. package/templates/new-worker-scheduled.ts +32 -0
  30. package/templates/new-worker.js +15 -0
  31. package/templates/new-worker.ts +33 -0
  32. package/templates/no-op-worker.js +10 -0
  33. package/templates/pages-dev-pipeline.ts +32 -0
  34. package/templates/pages-dev-util.ts +55 -0
  35. package/templates/pages-shim.ts +9 -0
  36. package/templates/pages-template-plugin.ts +190 -0
  37. package/templates/pages-template-worker.ts +198 -0
  38. package/templates/startDevWorker/InspectorProxyWorker.ts +661 -0
  39. package/templates/startDevWorker/ProxyWorker.ts +326 -0
  40. package/templates/tsconfig-sanity.ts +11 -0
  41. package/templates/tsconfig.init.json +105 -0
  42. package/templates/tsconfig.json +8 -0
  43. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  44. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  45. package/wrangler-dist/ProxyWorker.js +237 -0
  46. package/wrangler-dist/ProxyWorker.js.map +6 -0
  47. package/wrangler-dist/cli.d.ts +23937 -0
  48. package/wrangler-dist/cli.js +164283 -116933
  49. package/wrangler-dist/wasm-sync.wasm +0 -0
  50. package/import_meta_url.js +0 -3
  51. package/miniflare-config-stubs/.env.empty +0 -0
  52. package/miniflare-config-stubs/package.empty.json +0 -1
  53. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  54. package/pages/functions/buildWorker.ts +0 -62
  55. package/pages/functions/filepath-routing.test.ts +0 -39
  56. package/pages/functions/filepath-routing.ts +0 -228
  57. package/pages/functions/identifiers.ts +0 -78
  58. package/pages/functions/routes.ts +0 -158
  59. package/pages/functions/template-worker.ts +0 -136
  60. package/src/__tests__/clipboardy-mock.js +0 -4
  61. package/src/__tests__/dev.test.tsx +0 -71
  62. package/src/__tests__/index.test.ts +0 -424
  63. package/src/__tests__/jest.setup.ts +0 -29
  64. package/src/__tests__/kv.test.ts +0 -1224
  65. package/src/__tests__/logout.test.ts +0 -64
  66. package/src/__tests__/mock-cfetch.ts +0 -173
  67. package/src/__tests__/mock-console.ts +0 -34
  68. package/src/__tests__/mock-dialogs.ts +0 -65
  69. package/src/__tests__/mock-kv.ts +0 -40
  70. package/src/__tests__/mock-user.ts +0 -27
  71. package/src/__tests__/publish.test.ts +0 -1206
  72. package/src/__tests__/run-in-tmp.ts +0 -29
  73. package/src/__tests__/run-wrangler.ts +0 -8
  74. package/src/__tests__/secret.test.ts +0 -206
  75. package/src/__tests__/whoami.test.tsx +0 -101
  76. package/src/api/form_data.ts +0 -135
  77. package/src/api/preview.ts +0 -128
  78. package/src/api/worker.ts +0 -155
  79. package/src/cfetch/index.ts +0 -111
  80. package/src/cfetch/internal.ts +0 -71
  81. package/src/cli.ts +0 -9
  82. package/src/config.ts +0 -641
  83. package/src/dev.tsx +0 -830
  84. package/src/dialogs.tsx +0 -77
  85. package/src/index.tsx +0 -2078
  86. package/src/inspect.ts +0 -595
  87. package/src/kv.tsx +0 -276
  88. package/src/module-collection.ts +0 -63
  89. package/src/pages.tsx +0 -1043
  90. package/src/proxy.ts +0 -370
  91. package/src/publish.ts +0 -410
  92. package/src/sites.tsx +0 -240
  93. package/src/tail.tsx +0 -73
  94. package/src/user.tsx +0 -1019
  95. package/src/whoami.tsx +0 -69
  96. package/static-asset-facade.js +0 -47
  97. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  98. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  99. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  100. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  101. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  102. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  103. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  104. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  105. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  106. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  107. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  108. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  109. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  110. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  111. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  112. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  113. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  114. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  115. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  116. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  117. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  118. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  119. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  120. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  121. package/vendor/wrangler-mime/LICENSE +0 -21
  122. package/vendor/wrangler-mime/Mime.js +0 -97
  123. package/vendor/wrangler-mime/README.md +0 -187
  124. package/vendor/wrangler-mime/cli.js +0 -46
  125. package/vendor/wrangler-mime/index.js +0 -4
  126. package/vendor/wrangler-mime/lite.js +0 -4
  127. package/vendor/wrangler-mime/package.json +0 -52
  128. package/vendor/wrangler-mime/types/other.js +0 -1
  129. package/vendor/wrangler-mime/types/standard.js +0 -1
  130. package/wrangler-dist/cli.js.map +0 -7
@@ -1,1224 +0,0 @@
1
- import { writeFileSync } from "fs";
2
- import type { KVNamespaceInfo } from "../kv";
3
- import {
4
- setMockResponse,
5
- setMockRawResponse,
6
- unsetAllMocks,
7
- } from "./mock-cfetch";
8
- import { runWrangler } from "./run-wrangler";
9
- import { runInTempDir } from "./run-in-tmp";
10
- import { mockConsoleMethods } from "./mock-console";
11
- import { mockKeyListRequest } from "./mock-kv";
12
-
13
- describe("wrangler", () => {
14
- runInTempDir();
15
- const std = mockConsoleMethods();
16
-
17
- afterEach(() => {
18
- unsetAllMocks();
19
- });
20
-
21
- describe("kv:namespace", () => {
22
- describe("create", () => {
23
- function mockCreateRequest(expectedTitle: string) {
24
- setMockResponse(
25
- "/accounts/:accountId/storage/kv/namespaces",
26
- "POST",
27
- ([_url, accountId], { body }) => {
28
- expect(accountId).toEqual("some-account-id");
29
- const title = JSON.parse(body as string).title;
30
- expect(title).toEqual(expectedTitle);
31
- return { id: "some-namespace-id" };
32
- }
33
- );
34
- }
35
-
36
- it("should error if no namespace is given", async () => {
37
- let error: Error | undefined;
38
- try {
39
- await runWrangler("kv:namespace create");
40
- } catch (e) {
41
- error = e;
42
- }
43
- expect(std.out).toMatchInlineSnapshot(`""`);
44
- expect(std.err).toMatchInlineSnapshot(`
45
- "wrangler kv:namespace create <namespace>
46
-
47
- Create a new namespace
48
-
49
- Positionals:
50
- namespace The name of the new namespace [string] [required]
51
-
52
- Flags:
53
- -c, --config Path to .toml configuration file [string]
54
- -h, --help Show help [boolean]
55
- -v, --version Show version number [boolean]
56
-
57
- Options:
58
- -l, --local Run on my machine [boolean] [default: false]
59
- --env Perform on a specific environment [string]
60
- --preview Interact with a preview namespace [boolean]
61
-
62
- Not enough non-option arguments: got 0, need at least 1"
63
- `);
64
- expect(error).toMatchInlineSnapshot(
65
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
66
- );
67
- });
68
-
69
- it("should error if the namespace to create contains spaces", async () => {
70
- let error: Error | undefined;
71
- try {
72
- await runWrangler("kv:namespace create abc def ghi");
73
- } catch (e) {
74
- error = e;
75
- }
76
- expect(std.out).toMatchInlineSnapshot(`""`);
77
- expect(std.err).toMatchInlineSnapshot(`
78
- "wrangler kv:namespace create <namespace>
79
-
80
- Create a new namespace
81
-
82
- Positionals:
83
- namespace The name of the new namespace [string] [required]
84
-
85
- Flags:
86
- -c, --config Path to .toml configuration file [string]
87
- -h, --help Show help [boolean]
88
- -v, --version Show version number [boolean]
89
-
90
- Options:
91
- -l, --local Run on my machine [boolean] [default: false]
92
- --env Perform on a specific environment [string]
93
- --preview Interact with a preview namespace [boolean]
94
-
95
- Unexpected additional positional arguments \\"def ghi\\"."
96
- `);
97
- expect(error).toMatchInlineSnapshot(
98
- `[Error: Unexpected additional positional arguments "def ghi".]`
99
- );
100
- });
101
-
102
- it("should error if the namespace to create is not valid", async () => {
103
- let error: Error | undefined;
104
- try {
105
- await runWrangler("kv:namespace create abc-def");
106
- } catch (e) {
107
- error = e;
108
- }
109
-
110
- expect(std.out).toMatchInlineSnapshot(`""`);
111
- expect(std.err).toMatchInlineSnapshot(`
112
- "wrangler kv:namespace create <namespace>
113
-
114
- Create a new namespace
115
-
116
- Positionals:
117
- namespace The name of the new namespace [string] [required]
118
-
119
- Flags:
120
- -c, --config Path to .toml configuration file [string]
121
- -h, --help Show help [boolean]
122
- -v, --version Show version number [boolean]
123
-
124
- Options:
125
- -l, --local Run on my machine [boolean] [default: false]
126
- --env Perform on a specific environment [string]
127
- --preview Interact with a preview namespace [boolean]
128
-
129
- The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."
130
- `);
131
- expect(error).toMatchInlineSnapshot(
132
- `[Error: The namespace binding name "abc-def" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.]`
133
- );
134
- });
135
-
136
- it("should create a namespace", async () => {
137
- mockCreateRequest("worker-UnitTestNamespace");
138
- await runWrangler("kv:namespace create UnitTestNamespace");
139
- expect(std.out).toMatchInlineSnapshot(`
140
- "🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
141
- ✨ Success!
142
- Add the following to your configuration file in your kv_namespaces array:
143
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
144
- `);
145
- });
146
-
147
- it("should create a preview namespace if configured to do so", async () => {
148
- mockCreateRequest("worker-UnitTestNamespace_preview");
149
- await runWrangler("kv:namespace create UnitTestNamespace --preview");
150
- expect(std.out).toMatchInlineSnapshot(`
151
- "🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
152
- ✨ Success!
153
- Add the following to your configuration file in your kv_namespaces array:
154
- { binding = \\"UnitTestNamespace\\", preview_id = \\"some-namespace-id\\" }"
155
- `);
156
- });
157
-
158
- it("should create a namespace using configured worker name", async () => {
159
- writeFileSync("./wrangler.toml", 'name = "otherWorker"', "utf-8");
160
- mockCreateRequest("otherWorker-UnitTestNamespace");
161
- await runWrangler("kv:namespace create UnitTestNamespace");
162
- expect(std.out).toMatchInlineSnapshot(`
163
- "🌀 Creating namespace with title \\"otherWorker-UnitTestNamespace\\"
164
- ✨ Success!
165
- Add the following to your configuration file in your kv_namespaces array:
166
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
167
- `);
168
- });
169
-
170
- it("should create a namespace in an environment if configured to do so", async () => {
171
- mockCreateRequest("worker-customEnv-UnitTestNamespace");
172
- await runWrangler(
173
- "kv:namespace create UnitTestNamespace --env customEnv"
174
- );
175
- expect(std.out).toMatchInlineSnapshot(`
176
- "🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
177
- ✨ Success!
178
- Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
179
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
180
- `);
181
- });
182
- });
183
-
184
- describe("list", () => {
185
- function mockListRequest(namespaces: unknown[]) {
186
- const requests = { count: 0 };
187
- setMockResponse(
188
- "/accounts/:accountId/storage/kv/namespaces",
189
- ([_url, accountId], init, query) => {
190
- requests.count++;
191
- expect(accountId).toEqual("some-account-id");
192
- expect(query.get("per_page")).toEqual("100");
193
- expect(query.get("order")).toEqual("title");
194
- expect(query.get("direction")).toEqual("asc");
195
- expect(query.get("page")).toEqual(`${requests.count}`);
196
- expect(init).toEqual({});
197
- const pageSize = Number(query.get("per_page"));
198
- const page = Number(query.get("page"));
199
- return namespaces.slice((page - 1) * pageSize, page * pageSize);
200
- }
201
- );
202
- return requests;
203
- }
204
-
205
- it("should list namespaces", async () => {
206
- const kvNamespaces: KVNamespaceInfo[] = [
207
- { title: "title-1", id: "id-1" },
208
- { title: "title-2", id: "id-2" },
209
- ];
210
- mockListRequest(kvNamespaces);
211
- await runWrangler("kv:namespace list");
212
-
213
- expect(std.err).toMatchInlineSnapshot(`""`);
214
- const namespaces = JSON.parse(std.out);
215
- expect(namespaces).toEqual(kvNamespaces);
216
- });
217
-
218
- it("should make multiple requests for paginated results", async () => {
219
- // Create a lot of mock namespaces, so that the fetch requests will be paginated
220
- const kvNamespaces: KVNamespaceInfo[] = [];
221
- for (let i = 0; i < 550; i++) {
222
- kvNamespaces.push({ title: "title-" + i, id: "id-" + i });
223
- }
224
- const requests = mockListRequest(kvNamespaces);
225
- await runWrangler("kv:namespace list");
226
- const namespaces = JSON.parse(std.out);
227
- expect(namespaces).toEqual(kvNamespaces);
228
- expect(requests.count).toEqual(6);
229
- });
230
- });
231
-
232
- describe("delete", () => {
233
- function mockDeleteRequest(expectedNamespaceId: string) {
234
- const requests = { count: 0 };
235
- setMockResponse(
236
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId",
237
- "DELETE",
238
- ([_url, accountId, namespaceId]) => {
239
- requests.count++;
240
- expect(accountId).toEqual("some-account-id");
241
- expect(namespaceId).toEqual(expectedNamespaceId);
242
- return null;
243
- }
244
- );
245
- return requests;
246
- }
247
-
248
- it("should delete a namespace specified by id", async () => {
249
- const requests = mockDeleteRequest("some-namespace-id");
250
- await runWrangler(
251
- `kv:namespace delete --namespace-id some-namespace-id`
252
- );
253
- expect(requests.count).toEqual(1);
254
- });
255
-
256
- it("should delete a namespace specified by binding name", async () => {
257
- writeWranglerConfig();
258
- const requests = mockDeleteRequest("bound-id");
259
- await runWrangler(
260
- `kv:namespace delete --binding someBinding --preview false`
261
- );
262
- expect(requests.count).toEqual(1);
263
- });
264
-
265
- it("should delete a preview namespace specified by binding name", async () => {
266
- writeWranglerConfig();
267
- const requests = mockDeleteRequest("preview-bound-id");
268
- await runWrangler(
269
- `kv:namespace delete --binding someBinding --preview`
270
- );
271
- expect(requests.count).toEqual(1);
272
- });
273
-
274
- it("should error if a given binding name is not in the configured kv namespaces", async () => {
275
- writeWranglerConfig();
276
- let error: Error | undefined;
277
- try {
278
- await runWrangler("kv:namespace delete --binding otherBinding");
279
- } catch (e) {
280
- error = e;
281
- }
282
- expect(error).toMatchInlineSnapshot(`
283
- [Error: Not able to delete namespace.
284
- A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]
285
- `);
286
- expect(std.err).toMatchInlineSnapshot(`
287
- "wrangler kv:namespace delete
288
-
289
- Deletes a given namespace.
290
-
291
- Flags:
292
- -c, --config Path to .toml configuration file [string]
293
- -h, --help Show help [boolean]
294
- -v, --version Show version number [boolean]
295
-
296
- Options:
297
- -l, --local Run on my machine [boolean] [default: false]
298
- --binding The name of the namespace to delete [string]
299
- --namespace-id The id of the namespace to delete [string]
300
- --env Perform on a specific environment [string]
301
- --preview Interact with a preview namespace [boolean]
302
-
303
- Not able to delete namespace.
304
- A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
305
- `);
306
- });
307
-
308
- it("should delete a namespace specified by binding name in a given environment", async () => {
309
- writeWranglerConfig();
310
- const requests = mockDeleteRequest("env-bound-id");
311
- await runWrangler(
312
- "kv:namespace delete --binding someBinding --env some-environment --preview false"
313
- );
314
-
315
- expect(std.out).toMatchInlineSnapshot(`""`);
316
- expect(std.err).toMatchInlineSnapshot(`""`);
317
- expect(requests.count).toEqual(1);
318
- });
319
-
320
- it("should delete a preview namespace specified by binding name in a given environment", async () => {
321
- writeWranglerConfig();
322
- const requests = mockDeleteRequest("preview-env-bound-id");
323
- await runWrangler(
324
- `kv:namespace delete --binding someBinding --env some-environment --preview`
325
- );
326
- expect(requests.count).toEqual(1);
327
- });
328
- });
329
- });
330
-
331
- describe("kv:key", () => {
332
- describe("put", () => {
333
- function mockKeyPutRequest(
334
- expectedNamespaceId: string,
335
- expectedKey: string,
336
- expectedValue: string,
337
- expiration?: number,
338
- expirationTtl?: number
339
- ) {
340
- const requests = { count: 0 };
341
- setMockResponse(
342
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
343
- "PUT",
344
- ([_url, accountId, namespaceId, key], { body }, query) => {
345
- requests.count++;
346
- expect(accountId).toEqual("some-account-id");
347
- expect(namespaceId).toEqual(expectedNamespaceId);
348
- expect(key).toEqual(expectedKey);
349
- expect(body).toEqual(expectedValue);
350
- if (expiration !== undefined) {
351
- expect(query.get("expiration")).toEqual(`${expiration}`);
352
- } else {
353
- expect(query.has("expiration")).toBe(false);
354
- }
355
- if (expirationTtl) {
356
- expect(query.get("expiration_ttl")).toEqual(`${expirationTtl}`);
357
- } else {
358
- expect(query.has("expiration_ttl")).toBe(false);
359
- }
360
- return null;
361
- }
362
- );
363
- return requests;
364
- }
365
-
366
- it("should put a key in a given namespace specified by namespace-id", async () => {
367
- const requests = mockKeyPutRequest(
368
- "some-namespace-id",
369
- "my-key",
370
- "my-value"
371
- );
372
-
373
- await runWrangler(
374
- "kv:key put my-key my-value --namespace-id some-namespace-id"
375
- );
376
-
377
- expect(requests.count).toEqual(1);
378
- expect(std.out).toMatchInlineSnapshot(
379
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
380
- );
381
- expect(std.err).toMatchInlineSnapshot(`""`);
382
- });
383
-
384
- it("should put a key in a given namespace specified by binding", async () => {
385
- writeWranglerConfig();
386
- const requests = mockKeyPutRequest("bound-id", "my-key", "my-value");
387
- await runWrangler(
388
- "kv:key put my-key my-value --binding someBinding --preview false"
389
- );
390
-
391
- expect(std.out).toMatchInlineSnapshot(
392
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace bound-id"`
393
- );
394
- expect(std.err).toMatchInlineSnapshot(`""`);
395
- expect(requests.count).toEqual(1);
396
- });
397
-
398
- it("should put a key in a given preview namespace specified by binding", async () => {
399
- writeWranglerConfig();
400
- const requests = mockKeyPutRequest(
401
- "preview-bound-id",
402
- "my-key",
403
- "my-value"
404
- );
405
-
406
- await runWrangler(
407
- "kv:key put my-key my-value --binding someBinding --preview"
408
- );
409
-
410
- expect(std.out).toMatchInlineSnapshot(
411
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace preview-bound-id"`
412
- );
413
- expect(std.err).toMatchInlineSnapshot(`""`);
414
- expect(requests.count).toEqual(1);
415
- });
416
-
417
- it("should add expiration and ttl properties when putting a key", async () => {
418
- const requests = mockKeyPutRequest(
419
- "some-namespace-id",
420
- "my-key",
421
- "my-value",
422
- 10,
423
- 20
424
- );
425
- let error: Error | undefined;
426
- try {
427
- await runWrangler(
428
- "kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
429
- );
430
- } catch (e) {
431
- error = e;
432
- }
433
-
434
- expect(requests.count).toEqual(1);
435
- expect(std.out).toMatchInlineSnapshot(
436
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
437
- );
438
- expect(std.err).toMatchInlineSnapshot(`""`);
439
- expect(error).toMatchInlineSnapshot(`undefined`);
440
- });
441
-
442
- it("should put a key to the specified environment in a given namespace", async () => {
443
- writeWranglerConfig();
444
- const requests = mockKeyPutRequest(
445
- "env-bound-id",
446
- "my-key",
447
- "my-value"
448
- );
449
- await runWrangler(
450
- "kv:key put my-key my-value --binding someBinding --env some-environment --preview false"
451
- );
452
- expect(std.out).toMatchInlineSnapshot(
453
- `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace env-bound-id"`
454
- );
455
- expect(std.err).toMatchInlineSnapshot(`""`);
456
- expect(requests.count).toEqual(1);
457
- });
458
-
459
- it("should put a key with a value loaded from a given path", async () => {
460
- writeFileSync("foo.txt", "file-contents", "utf-8");
461
- const requests = mockKeyPutRequest(
462
- "some-namespace-id",
463
- "my-key",
464
- "file-contents"
465
- );
466
- await runWrangler(
467
- "kv:key put my-key --namespace-id some-namespace-id --path foo.txt"
468
- );
469
- expect(std.out).toMatchInlineSnapshot(
470
- `"writing the contents of foo.txt to the key \\"my-key\\" on namespace some-namespace-id"`
471
- );
472
- expect(std.err).toMatchInlineSnapshot(`""`);
473
- expect(requests.count).toEqual(1);
474
- });
475
-
476
- it("should error if no key is provided", async () => {
477
- let error: Error | undefined;
478
- try {
479
- await runWrangler("kv:key put");
480
- } catch (e) {
481
- error = e;
482
- }
483
-
484
- expect(std.out).toMatchInlineSnapshot(`""`);
485
- expect(std.err).toMatchInlineSnapshot(`
486
- "wrangler kv:key put <key> [value]
487
-
488
- Writes a single key/value pair to the given namespace.
489
-
490
- Positionals:
491
- key The key to write to. [string] [required]
492
- value The value to write. [string]
493
-
494
- Flags:
495
- -c, --config Path to .toml configuration file [string]
496
- -h, --help Show help [boolean]
497
- -v, --version Show version number [boolean]
498
-
499
- Options:
500
- -l, --local Run on my machine [boolean] [default: false]
501
- --binding The binding of the namespace to write to. [string]
502
- --namespace-id The id of the namespace to write to. [string]
503
- --env Perform on a specific environment [string]
504
- --preview Interact with a preview namespace [boolean]
505
- --ttl Time for which the entries should be visible. [number]
506
- --expiration Time since the UNIX epoch after which the entry expires [number]
507
- --path Read value from the file at a given path. [string]
508
-
509
- Not enough non-option arguments: got 0, need at least 1"
510
- `);
511
- expect(error).toMatchInlineSnapshot(
512
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
513
- );
514
- });
515
-
516
- it("should error if no binding nor namespace is provided", async () => {
517
- let error: Error | undefined;
518
- try {
519
- await runWrangler("kv:key put foo bar");
520
- } catch (e) {
521
- error = e;
522
- }
523
-
524
- expect(std.out).toMatchInlineSnapshot(`""`);
525
- expect(std.err).toMatchInlineSnapshot(`
526
- "wrangler kv:key put <key> [value]
527
-
528
- Writes a single key/value pair to the given namespace.
529
-
530
- Positionals:
531
- key The key to write to. [string] [required]
532
- value The value to write. [string]
533
-
534
- Flags:
535
- -c, --config Path to .toml configuration file [string]
536
- -h, --help Show help [boolean]
537
- -v, --version Show version number [boolean]
538
-
539
- Options:
540
- -l, --local Run on my machine [boolean] [default: false]
541
- --binding The binding of the namespace to write to. [string]
542
- --namespace-id The id of the namespace to write to. [string]
543
- --env Perform on a specific environment [string]
544
- --preview Interact with a preview namespace [boolean]
545
- --ttl Time for which the entries should be visible. [number]
546
- --expiration Time since the UNIX epoch after which the entry expires [number]
547
- --path Read value from the file at a given path. [string]
548
-
549
- Exactly one of the arguments binding and namespace-id is required"
550
- `);
551
- expect(error).toMatchInlineSnapshot(
552
- `[Error: Exactly one of the arguments binding and namespace-id is required]`
553
- );
554
- });
555
-
556
- it("should error if both binding and namespace is provided", async () => {
557
- let error: Error | undefined;
558
- try {
559
- await runWrangler("kv:key put foo bar --binding x --namespace-id y");
560
- } catch (e) {
561
- error = e;
562
- }
563
-
564
- expect(std.out).toMatchInlineSnapshot(`""`);
565
- expect(std.err).toMatchInlineSnapshot(`
566
- "wrangler kv:key put <key> [value]
567
-
568
- Writes a single key/value pair to the given namespace.
569
-
570
- Positionals:
571
- key The key to write to. [string] [required]
572
- value The value to write. [string]
573
-
574
- Flags:
575
- -c, --config Path to .toml configuration file [string]
576
- -h, --help Show help [boolean]
577
- -v, --version Show version number [boolean]
578
-
579
- Options:
580
- -l, --local Run on my machine [boolean] [default: false]
581
- --binding The binding of the namespace to write to. [string]
582
- --namespace-id The id of the namespace to write to. [string]
583
- --env Perform on a specific environment [string]
584
- --preview Interact with a preview namespace [boolean]
585
- --ttl Time for which the entries should be visible. [number]
586
- --expiration Time since the UNIX epoch after which the entry expires [number]
587
- --path Read value from the file at a given path. [string]
588
-
589
- Arguments binding and namespace-id are mutually exclusive"
590
- `);
591
- expect(error).toMatchInlineSnapshot(
592
- `[Error: Arguments binding and namespace-id are mutually exclusive]`
593
- );
594
- });
595
-
596
- it("should error if no value nor path is provided", async () => {
597
- let error: Error | undefined;
598
- try {
599
- await runWrangler("kv:key put key --namespace-id 12345");
600
- } catch (e) {
601
- error = e;
602
- }
603
-
604
- expect(std.out).toMatchInlineSnapshot(`""`);
605
- expect(std.err).toMatchInlineSnapshot(`
606
- "wrangler kv:key put <key> [value]
607
-
608
- Writes a single key/value pair to the given namespace.
609
-
610
- Positionals:
611
- key The key to write to. [string] [required]
612
- value The value to write. [string]
613
-
614
- Flags:
615
- -c, --config Path to .toml configuration file [string]
616
- -h, --help Show help [boolean]
617
- -v, --version Show version number [boolean]
618
-
619
- Options:
620
- -l, --local Run on my machine [boolean] [default: false]
621
- --binding The binding of the namespace to write to. [string]
622
- --namespace-id The id of the namespace to write to. [string]
623
- --env Perform on a specific environment [string]
624
- --preview Interact with a preview namespace [boolean]
625
- --ttl Time for which the entries should be visible. [number]
626
- --expiration Time since the UNIX epoch after which the entry expires [number]
627
- --path Read value from the file at a given path. [string]
628
-
629
- Exactly one of the arguments value and path is required"
630
- `);
631
- expect(error).toMatchInlineSnapshot(
632
- `[Error: Exactly one of the arguments value and path is required]`
633
- );
634
- });
635
-
636
- it("should error if both value and path is provided", async () => {
637
- let error: Error | undefined;
638
- try {
639
- await runWrangler(
640
- "kv:key put key value --path xyz --namespace-id 12345"
641
- );
642
- } catch (e) {
643
- error = e;
644
- }
645
-
646
- expect(std.out).toMatchInlineSnapshot(`""`);
647
- expect(std.err).toMatchInlineSnapshot(`
648
- "wrangler kv:key put <key> [value]
649
-
650
- Writes a single key/value pair to the given namespace.
651
-
652
- Positionals:
653
- key The key to write to. [string] [required]
654
- value The value to write. [string]
655
-
656
- Flags:
657
- -c, --config Path to .toml configuration file [string]
658
- -h, --help Show help [boolean]
659
- -v, --version Show version number [boolean]
660
-
661
- Options:
662
- -l, --local Run on my machine [boolean] [default: false]
663
- --binding The binding of the namespace to write to. [string]
664
- --namespace-id The id of the namespace to write to. [string]
665
- --env Perform on a specific environment [string]
666
- --preview Interact with a preview namespace [boolean]
667
- --ttl Time for which the entries should be visible. [number]
668
- --expiration Time since the UNIX epoch after which the entry expires [number]
669
- --path Read value from the file at a given path. [string]
670
-
671
- Arguments value and path are mutually exclusive"
672
- `);
673
- expect(error).toMatchInlineSnapshot(
674
- `[Error: Arguments value and path are mutually exclusive]`
675
- );
676
- });
677
-
678
- it("should error if a given binding name is not in the configured kv namespaces", async () => {
679
- writeWranglerConfig();
680
- let error: Error | undefined;
681
- try {
682
- await runWrangler("kv:key put key value --binding otherBinding");
683
- } catch (e) {
684
- error = e;
685
- }
686
-
687
- expect(std.out).toMatchInlineSnapshot(`""`);
688
- expect(std.err).toMatchInlineSnapshot(`
689
- "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
690
-
691
- %s
692
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
693
- `);
694
- expect(error).toMatchInlineSnapshot(
695
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
696
- );
697
- });
698
-
699
- it("should error if a given binding has both preview and non-preview and --preview is not specified", async () => {
700
- writeWranglerConfig();
701
- const requests = mockKeyPutRequest(
702
- "preview-bound-id",
703
- "my-key",
704
- "my-value"
705
- );
706
- let error: Error | undefined;
707
- try {
708
- await runWrangler("kv:key put my-key my-value --binding someBinding");
709
- } catch (e) {
710
- error = e;
711
- }
712
- expect(std.out).toMatchInlineSnapshot(`""`);
713
- expect(std.err).toMatchInlineSnapshot(`
714
- "someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.
715
-
716
- %s
717
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
718
- `);
719
- expect(error).toMatchInlineSnapshot(
720
- `[Error: someBinding has both a namespace ID and a preview ID. Specify "--preview" or "--preview false" to avoid writing data to the wrong namespace.]`
721
- );
722
- expect(requests.count).toEqual(0);
723
- });
724
- });
725
-
726
- describe("list", () => {
727
- it("should list the keys of a namespace specified by namespace-id", async () => {
728
- const keys = ["key-1", "key-2", "key-3"];
729
- mockKeyListRequest("some-namespace-id", keys);
730
- await runWrangler("kv:key list --namespace-id some-namespace-id");
731
- expect(std.err).toMatchInlineSnapshot(`""`);
732
- expect(std.out).toMatchInlineSnapshot(`
733
- "[
734
- {
735
- \\"name\\": \\"key-1\\",
736
- \\"expiration\\": 123456789,
737
- \\"metadata\\": {}
738
- },
739
- {
740
- \\"name\\": \\"key-2\\",
741
- \\"expiration\\": 123456789,
742
- \\"metadata\\": {}
743
- },
744
- {
745
- \\"name\\": \\"key-3\\",
746
- \\"expiration\\": 123456789,
747
- \\"metadata\\": {}
748
- }
749
- ]"
750
- `);
751
- });
752
-
753
- it("should list the keys of a namespace specified by binding", async () => {
754
- writeWranglerConfig();
755
- const keys = ["key-1", "key-2", "key-3"];
756
- mockKeyListRequest("bound-id", keys);
757
-
758
- await runWrangler("kv:key list --binding someBinding");
759
- expect(std.err).toMatchInlineSnapshot(`""`);
760
- expect(std.out).toMatchInlineSnapshot(`
761
- "[
762
- {
763
- \\"name\\": \\"key-1\\",
764
- \\"expiration\\": 123456789,
765
- \\"metadata\\": {}
766
- },
767
- {
768
- \\"name\\": \\"key-2\\",
769
- \\"expiration\\": 123456789,
770
- \\"metadata\\": {}
771
- },
772
- {
773
- \\"name\\": \\"key-3\\",
774
- \\"expiration\\": 123456789,
775
- \\"metadata\\": {}
776
- }
777
- ]"
778
- `);
779
- });
780
-
781
- it("should list the keys of a preview namespace specified by binding", async () => {
782
- writeWranglerConfig();
783
- const keys = ["key-1", "key-2", "key-3"];
784
- mockKeyListRequest("preview-bound-id", keys);
785
- await runWrangler("kv:key list --binding someBinding --preview");
786
- expect(std.err).toMatchInlineSnapshot(`""`);
787
- expect(std.out).toMatchInlineSnapshot(`
788
- "[
789
- {
790
- \\"name\\": \\"key-1\\",
791
- \\"expiration\\": 123456789,
792
- \\"metadata\\": {}
793
- },
794
- {
795
- \\"name\\": \\"key-2\\",
796
- \\"expiration\\": 123456789,
797
- \\"metadata\\": {}
798
- },
799
- {
800
- \\"name\\": \\"key-3\\",
801
- \\"expiration\\": 123456789,
802
- \\"metadata\\": {}
803
- }
804
- ]"
805
- `);
806
- });
807
-
808
- it("should list the keys of a namespace specified by binding, in a given environment", async () => {
809
- writeWranglerConfig();
810
- const keys = ["key-1", "key-2", "key-3"];
811
- mockKeyListRequest("env-bound-id", keys);
812
- await runWrangler(
813
- "kv:key list --binding someBinding --env some-environment"
814
- );
815
- expect(std.err).toMatchInlineSnapshot(`""`);
816
- expect(std.out).toMatchInlineSnapshot(`
817
- "[
818
- {
819
- \\"name\\": \\"key-1\\",
820
- \\"expiration\\": 123456789,
821
- \\"metadata\\": {}
822
- },
823
- {
824
- \\"name\\": \\"key-2\\",
825
- \\"expiration\\": 123456789,
826
- \\"metadata\\": {}
827
- },
828
- {
829
- \\"name\\": \\"key-3\\",
830
- \\"expiration\\": 123456789,
831
- \\"metadata\\": {}
832
- }
833
- ]"
834
- `);
835
- });
836
-
837
- it("should list the keys of a preview namespace specified by binding, in a given environment", async () => {
838
- writeWranglerConfig();
839
- const keys = ["key-1", "key-2", "key-3"];
840
- mockKeyListRequest("preview-env-bound-id", keys);
841
- await runWrangler(
842
- "kv:key list --binding someBinding --preview --env some-environment"
843
- );
844
- expect(std.err).toMatchInlineSnapshot(`""`);
845
- expect(std.out).toMatchInlineSnapshot(`
846
- "[
847
- {
848
- \\"name\\": \\"key-1\\",
849
- \\"expiration\\": 123456789,
850
- \\"metadata\\": {}
851
- },
852
- {
853
- \\"name\\": \\"key-2\\",
854
- \\"expiration\\": 123456789,
855
- \\"metadata\\": {}
856
- },
857
- {
858
- \\"name\\": \\"key-3\\",
859
- \\"expiration\\": 123456789,
860
- \\"metadata\\": {}
861
- }
862
- ]"
863
- `);
864
- });
865
-
866
- // We'll run the next test with variations on the cursor
867
- // that's returned on cloudflare's API after all results
868
- // have been drained.
869
- for (const blankCursorValue of [undefined, null, ""] as [
870
- undefined,
871
- null,
872
- ""
873
- ]) {
874
- describe(`cursor - ${blankCursorValue}`, () => {
875
- it("should make multiple requests for paginated results", async () => {
876
- // Create a lot of mock keys, so that the fetch requests will be paginated
877
- const keys: string[] = [];
878
- for (let i = 0; i < 550; i++) {
879
- keys.push("key-" + i);
880
- }
881
- // Ask for the keys in pages of size 100.
882
- const requests = mockKeyListRequest(
883
- "some-namespace-id",
884
- keys,
885
- 100,
886
- blankCursorValue
887
- );
888
- await runWrangler(
889
- "kv:key list --namespace-id some-namespace-id --limit 100"
890
- );
891
- expect(std.err).toMatchInlineSnapshot(`""`);
892
- expect(JSON.parse(std.out).map((k) => k.name)).toEqual(keys);
893
- expect(requests.count).toEqual(6);
894
- });
895
- });
896
- }
897
-
898
- it("should error if a given binding name is not in the configured kv namespaces", async () => {
899
- writeWranglerConfig();
900
- let error: Error | undefined;
901
- try {
902
- await runWrangler("kv:key list --binding otherBinding");
903
- } catch (e) {
904
- error = e;
905
- }
906
- expect(error).toMatchInlineSnapshot(
907
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
908
- );
909
- expect(std.err).toMatchInlineSnapshot(`
910
- "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
911
-
912
- %s
913
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
914
- `);
915
- expect(std.out).toMatchInlineSnapshot(`""`);
916
- });
917
- });
918
-
919
- describe("get", () => {
920
- function mockKeyGetRequest(
921
- expectedNamespaceId: string,
922
- expectedKey: string,
923
- expectedValue: string
924
- ) {
925
- const requests = { count: 0 };
926
- setMockRawResponse(
927
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
928
- ([_url, accountId, namespaceId, key]) => {
929
- requests.count++;
930
- expect(accountId).toEqual("some-account-id");
931
- expect(namespaceId).toEqual(expectedNamespaceId);
932
- expect(key).toEqual(expectedKey);
933
- return expectedValue;
934
- }
935
- );
936
- return requests;
937
- }
938
-
939
- it("should get a key in a given namespace specified by namespace-id", async () => {
940
- const requests = mockKeyGetRequest(
941
- "some-namespace-id",
942
- "my-key",
943
- "my-value"
944
- );
945
- await runWrangler("kv:key get my-key --namespace-id some-namespace-id");
946
- expect(std.out).toMatchInlineSnapshot(`"my-value"`);
947
- expect(std.err).toMatchInlineSnapshot(`""`);
948
- expect(requests.count).toEqual(1);
949
- });
950
-
951
- it("should get a key in a given namespace specified by binding", async () => {
952
- writeWranglerConfig();
953
- const requests = mockKeyGetRequest("bound-id", "my-key", "my-value");
954
- await runWrangler(
955
- "kv:key get my-key --binding someBinding --preview false"
956
- );
957
- expect(std.out).toMatchInlineSnapshot(`"my-value"`);
958
- expect(std.err).toMatchInlineSnapshot(`""`);
959
- expect(requests.count).toEqual(1);
960
- });
961
-
962
- it("should get a key in a given preview namespace specified by binding", async () => {
963
- writeWranglerConfig();
964
- const requests = mockKeyGetRequest(
965
- "preview-bound-id",
966
- "my-key",
967
- "my-value"
968
- );
969
- await runWrangler("kv:key get my-key --binding someBinding --preview");
970
- expect(std.out).toMatchInlineSnapshot(`"my-value"`);
971
- expect(std.err).toMatchInlineSnapshot(`""`);
972
- expect(requests.count).toEqual(1);
973
- });
974
-
975
- it("should get a key for the specified environment in a given namespace", async () => {
976
- writeWranglerConfig();
977
- const requests = mockKeyGetRequest(
978
- "env-bound-id",
979
- "my-key",
980
- "my-value"
981
- );
982
- await runWrangler(
983
- "kv:key get my-key my-value --binding someBinding --env some-environment --preview false"
984
- );
985
- expect(std.out).toMatchInlineSnapshot(`"my-value"`);
986
- expect(std.err).toMatchInlineSnapshot(`""`);
987
- expect(requests.count).toEqual(1);
988
- });
989
-
990
- it("should error if no key is provided", async () => {
991
- let error: Error | undefined;
992
- try {
993
- await runWrangler("kv:key get");
994
- } catch (e) {
995
- error = e;
996
- }
997
- expect(std.out).toMatchInlineSnapshot(`""`);
998
- expect(std.err).toMatchInlineSnapshot(`
999
- "wrangler kv:key get <key>
1000
-
1001
- Reads a single value by key from the given namespace.
1002
-
1003
- Positionals:
1004
- key The key value to get. [string] [required]
1005
-
1006
- Flags:
1007
- -c, --config Path to .toml configuration file [string]
1008
- -h, --help Show help [boolean]
1009
- -v, --version Show version number [boolean]
1010
-
1011
- Options:
1012
- -l, --local Run on my machine [boolean] [default: false]
1013
- --binding The name of the namespace to get from [string]
1014
- --namespace-id The id of the namespace to get from [string]
1015
- --env Perform on a specific environment [string]
1016
- --preview Interact with a preview namespace [boolean] [default: false]
1017
-
1018
- Not enough non-option arguments: got 0, need at least 1"
1019
- `);
1020
- expect(error).toMatchInlineSnapshot(
1021
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
1022
- );
1023
- });
1024
-
1025
- it("should error if no binding nor namespace is provided", async () => {
1026
- let error: Error | undefined;
1027
- try {
1028
- await runWrangler("kv:key get foo");
1029
- } catch (e) {
1030
- error = e;
1031
- }
1032
- expect(std.out).toMatchInlineSnapshot(`""`);
1033
- expect(std.err).toMatchInlineSnapshot(`
1034
- "wrangler kv:key get <key>
1035
-
1036
- Reads a single value by key from the given namespace.
1037
-
1038
- Positionals:
1039
- key The key value to get. [string] [required]
1040
-
1041
- Flags:
1042
- -c, --config Path to .toml configuration file [string]
1043
- -h, --help Show help [boolean]
1044
- -v, --version Show version number [boolean]
1045
-
1046
- Options:
1047
- -l, --local Run on my machine [boolean] [default: false]
1048
- --binding The name of the namespace to get from [string]
1049
- --namespace-id The id of the namespace to get from [string]
1050
- --env Perform on a specific environment [string]
1051
- --preview Interact with a preview namespace [boolean] [default: false]
1052
-
1053
- Exactly one of the arguments binding and namespace-id is required"
1054
- `);
1055
- expect(error).toMatchInlineSnapshot(
1056
- `[Error: Exactly one of the arguments binding and namespace-id is required]`
1057
- );
1058
- });
1059
-
1060
- it("should error if both binding and namespace is provided", async () => {
1061
- let error: Error | undefined;
1062
- try {
1063
- await runWrangler("kv:key get foo --binding x --namespace-id y");
1064
- } catch (e) {
1065
- error = e;
1066
- }
1067
-
1068
- expect(std.out).toMatchInlineSnapshot(`""`);
1069
- expect(std.err).toMatchInlineSnapshot(`
1070
- "wrangler kv:key get <key>
1071
-
1072
- Reads a single value by key from the given namespace.
1073
-
1074
- Positionals:
1075
- key The key value to get. [string] [required]
1076
-
1077
- Flags:
1078
- -c, --config Path to .toml configuration file [string]
1079
- -h, --help Show help [boolean]
1080
- -v, --version Show version number [boolean]
1081
-
1082
- Options:
1083
- -l, --local Run on my machine [boolean] [default: false]
1084
- --binding The name of the namespace to get from [string]
1085
- --namespace-id The id of the namespace to get from [string]
1086
- --env Perform on a specific environment [string]
1087
- --preview Interact with a preview namespace [boolean] [default: false]
1088
-
1089
- Arguments binding and namespace-id are mutually exclusive"
1090
- `);
1091
- expect(error).toMatchInlineSnapshot(
1092
- `[Error: Arguments binding and namespace-id are mutually exclusive]`
1093
- );
1094
- });
1095
-
1096
- it("should error if a given binding name is not in the configured kv namespaces", async () => {
1097
- writeWranglerConfig();
1098
- let error: Error | undefined;
1099
- try {
1100
- await runWrangler("kv:key get key --binding otherBinding");
1101
- } catch (e) {
1102
- error = e;
1103
- }
1104
- expect(std.out).toMatchInlineSnapshot(`""`);
1105
- expect(std.err).toMatchInlineSnapshot(`
1106
- "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1107
-
1108
- %s
1109
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1110
- `);
1111
- expect(error).toMatchInlineSnapshot(
1112
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
1113
- );
1114
- });
1115
- });
1116
-
1117
- describe("delete", () => {
1118
- function mockDeleteRequest(
1119
- expectedNamespaceId: string,
1120
- expectedKey: string
1121
- ) {
1122
- const requests = { count: 0 };
1123
- setMockResponse(
1124
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
1125
- "DELETE",
1126
- ([_url, accountId, namespaceId, key]) => {
1127
- requests.count++;
1128
- expect(accountId).toEqual("some-account-id");
1129
- expect(namespaceId).toEqual(expectedNamespaceId);
1130
- expect(key).toEqual(expectedKey);
1131
- return null;
1132
- }
1133
- );
1134
- return requests;
1135
- }
1136
-
1137
- it("should delete a key in a namespace specified by id", async () => {
1138
- const requests = mockDeleteRequest("some-namespace-id", "someKey");
1139
- await runWrangler(
1140
- `kv:key delete --namespace-id some-namespace-id someKey`
1141
- );
1142
- expect(requests.count).toEqual(1);
1143
- });
1144
-
1145
- it("should delete a key in a namespace specified by binding name", async () => {
1146
- writeWranglerConfig();
1147
- const requests = mockDeleteRequest("bound-id", "someKey");
1148
- await runWrangler(
1149
- `kv:key delete --binding someBinding --preview false someKey`
1150
- );
1151
- expect(requests.count).toEqual(1);
1152
- });
1153
-
1154
- it("should delete a key in a preview namespace specified by binding name", async () => {
1155
- writeWranglerConfig();
1156
- const requests = mockDeleteRequest("preview-bound-id", "someKey");
1157
- await runWrangler(
1158
- `kv:key delete --binding someBinding --preview someKey`
1159
- );
1160
- expect(requests.count).toEqual(1);
1161
- });
1162
-
1163
- it("should error if a given binding name is not in the configured kv namespaces", async () => {
1164
- writeWranglerConfig();
1165
- let error: Error | undefined;
1166
- try {
1167
- await runWrangler(`kv:key delete --binding otherBinding someKey`);
1168
- } catch (e) {
1169
- error = e;
1170
- }
1171
-
1172
- expect(std.err).toMatchInlineSnapshot(`
1173
- "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1174
-
1175
- %s
1176
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1177
- `);
1178
- expect(error).toMatchInlineSnapshot(
1179
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
1180
- );
1181
- });
1182
-
1183
- it("should delete a key in a namespace specified by binding name in a given environment", async () => {
1184
- writeWranglerConfig();
1185
- const requests = mockDeleteRequest("env-bound-id", "someKey");
1186
- await runWrangler(
1187
- `kv:key delete --binding someBinding --env some-environment --preview false someKey`
1188
- );
1189
- expect(std.out).toMatchInlineSnapshot(
1190
- `"deleting the key \\"someKey\\" on namespace env-bound-id"`
1191
- );
1192
- expect(std.err).toMatchInlineSnapshot(`""`);
1193
- expect(requests.count).toEqual(1);
1194
- });
1195
-
1196
- it("should delete a key in a preview namespace specified by binding name in a given environment", async () => {
1197
- writeWranglerConfig();
1198
- const requests = mockDeleteRequest("preview-env-bound-id", "someKey");
1199
- await runWrangler(
1200
- `kv:key delete --binding someBinding --env some-environment --preview someKey`
1201
- );
1202
- expect(requests.count).toEqual(1);
1203
- });
1204
- });
1205
- });
1206
- });
1207
-
1208
- function writeWranglerConfig() {
1209
- writeFileSync(
1210
- "./wrangler.toml",
1211
- [
1212
- 'name = "otherWorker"',
1213
- "kv_namespaces = [",
1214
- ' { binding = "someBinding", id = "bound-id", preview_id = "preview-bound-id" }',
1215
- "]",
1216
- "",
1217
- "[env.some-environment]",
1218
- "kv_namespaces = [",
1219
- ' { binding = "someBinding", id = "env-bound-id", preview_id = "preview-env-bound-id" }',
1220
- "]",
1221
- ].join("\n"),
1222
- "utf-8"
1223
- );
1224
- }