wrangler 0.0.0-e178d6fb → 0.0.0-e1c2f5b9

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 (239) hide show
  1. package/README.md +16 -2
  2. package/bin/wrangler.js +31 -11
  3. package/miniflare-dist/index.mjs +1340 -330
  4. package/package.json +40 -13
  5. package/src/__tests__/access.test.ts +25 -0
  6. package/src/__tests__/api-dev.test.ts +238 -0
  7. package/src/__tests__/api-devregistry.test.js +64 -0
  8. package/src/__tests__/api.test.ts +80 -0
  9. package/src/__tests__/configuration.test.ts +683 -470
  10. package/src/__tests__/d1.test.ts +123 -0
  11. package/src/__tests__/delete.test.ts +263 -0
  12. package/src/__tests__/deployments.test.ts +105 -0
  13. package/src/__tests__/dev.test.tsx +453 -70
  14. package/src/__tests__/dialogs.test.tsx +40 -0
  15. package/src/__tests__/generate.test.ts +235 -0
  16. package/src/__tests__/get-host-from-url.test.ts +16 -0
  17. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  18. package/src/__tests__/helpers/mock-cfetch.ts +92 -26
  19. package/src/__tests__/helpers/mock-console.ts +11 -2
  20. package/src/__tests__/helpers/mock-dialogs.ts +1 -1
  21. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  22. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  23. package/src/__tests__/helpers/mock-kv.ts +1 -1
  24. package/src/__tests__/helpers/mock-oauth-flow.ts +74 -53
  25. package/src/__tests__/helpers/mock-process.ts +8 -13
  26. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  27. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  28. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  29. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  30. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  31. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  32. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  33. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  34. package/src/__tests__/helpers/msw/index.ts +21 -0
  35. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  36. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  37. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  38. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  39. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  40. package/src/__tests__/index.test.ts +103 -97
  41. package/src/__tests__/init.test.ts +1484 -382
  42. package/src/__tests__/jest.setup.ts +88 -5
  43. package/src/__tests__/kv.test.ts +655 -540
  44. package/src/__tests__/logout.test.ts +47 -0
  45. package/src/__tests__/metrics.test.ts +95 -45
  46. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  47. package/src/__tests__/middleware.test.ts +750 -0
  48. package/src/__tests__/pages-deployment-tail.test.ts +884 -0
  49. package/src/__tests__/pages.test.ts +1301 -119
  50. package/src/__tests__/paths.test.ts +39 -0
  51. package/src/__tests__/publish.test.ts +2266 -1180
  52. package/src/__tests__/pubsub.test.ts +211 -88
  53. package/src/__tests__/queues.test.ts +522 -0
  54. package/src/__tests__/r2.test.ts +363 -203
  55. package/src/__tests__/secret.test.ts +261 -59
  56. package/src/__tests__/tail.test.ts +352 -151
  57. package/src/__tests__/test-old-node-version.js +3 -3
  58. package/src/__tests__/tsconfig-sanity.ts +12 -0
  59. package/src/__tests__/tsconfig.json +8 -0
  60. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  61. package/src/__tests__/type-generation.test.ts +233 -0
  62. package/src/__tests__/user.test.ts +59 -73
  63. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  64. package/src/__tests__/validate-dev-props.test.ts +56 -0
  65. package/src/__tests__/whoami.test.tsx +114 -66
  66. package/src/__tests__/worker-namespace.test.ts +154 -155
  67. package/src/api/dev.ts +170 -38
  68. package/src/api/index.ts +1 -0
  69. package/src/bundle-reporter.tsx +41 -2
  70. package/src/bundle.ts +668 -81
  71. package/src/cfetch/index.ts +40 -3
  72. package/src/cfetch/internal.ts +144 -32
  73. package/src/cli.ts +4 -3
  74. package/src/config/config.ts +27 -3
  75. package/src/config/environment.ts +131 -7
  76. package/src/config/index.ts +128 -8
  77. package/src/config/validation-helpers.ts +19 -6
  78. package/src/config/validation.ts +411 -14
  79. package/src/config-cache.ts +3 -2
  80. package/src/create-worker-preview.ts +31 -6
  81. package/src/create-worker-upload-form.ts +84 -22
  82. package/src/d1/backups.tsx +223 -0
  83. package/src/d1/constants.ts +2 -0
  84. package/src/d1/create.tsx +63 -0
  85. package/src/d1/delete.tsx +56 -0
  86. package/src/d1/execute.tsx +352 -0
  87. package/src/d1/formatTimeAgo.ts +14 -0
  88. package/src/d1/index.ts +101 -0
  89. package/src/d1/list.tsx +48 -0
  90. package/src/d1/migrations/apply.tsx +196 -0
  91. package/src/d1/migrations/create.tsx +77 -0
  92. package/src/d1/migrations/helpers.ts +147 -0
  93. package/src/d1/migrations/index.tsx +3 -0
  94. package/src/d1/migrations/list.tsx +79 -0
  95. package/src/d1/migrations/options.ts +18 -0
  96. package/src/d1/options.ts +22 -0
  97. package/src/d1/types.tsx +24 -0
  98. package/src/d1/utils.ts +48 -0
  99. package/src/delete.ts +108 -0
  100. package/src/deployments.ts +83 -0
  101. package/src/deprecated/index.ts +145 -0
  102. package/src/dev/dev-vars.ts +11 -8
  103. package/src/dev/dev.tsx +220 -58
  104. package/src/dev/get-local-persistence-path.tsx +31 -0
  105. package/src/dev/local.tsx +740 -181
  106. package/src/dev/remote.tsx +340 -123
  107. package/src/dev/start-server.ts +543 -0
  108. package/src/dev/use-esbuild.ts +61 -7
  109. package/src/dev/validate-dev-props.ts +31 -0
  110. package/src/dev-registry.tsx +201 -0
  111. package/src/dev.tsx +614 -258
  112. package/src/dialogs.tsx +41 -8
  113. package/src/{worker-namespace.ts → dispatch-namespace.ts} +23 -22
  114. package/src/docs/index.ts +97 -0
  115. package/src/entry.ts +3 -2
  116. package/src/environment-variables/factory.ts +88 -0
  117. package/src/environment-variables/misc-variables.ts +30 -0
  118. package/src/generate/index.ts +300 -0
  119. package/src/git-client.ts +100 -7
  120. package/src/index.tsx +249 -1560
  121. package/src/init.ts +626 -150
  122. package/src/inspect.ts +137 -32
  123. package/src/{metrics/is-ci.ts → is-ci.ts} +0 -0
  124. package/src/is-interactive.ts +4 -0
  125. package/src/jest.d.ts +4 -0
  126. package/src/{kv.ts → kv/helpers.ts} +3 -3
  127. package/src/kv/index.ts +596 -0
  128. package/src/logger.ts +27 -8
  129. package/src/metrics/index.ts +1 -0
  130. package/src/metrics/metrics-config.ts +4 -4
  131. package/src/metrics/metrics-dispatcher.ts +1 -0
  132. package/src/metrics/metrics-usage-headers.ts +24 -0
  133. package/src/metrics/send-event.ts +13 -8
  134. package/src/miniflare-cli/assets.ts +172 -465
  135. package/src/miniflare-cli/index.ts +149 -24
  136. package/src/miniflare-cli/tsconfig.json +9 -0
  137. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  138. package/src/miniflare-cli/types.ts +11 -0
  139. package/src/module-collection.ts +12 -3
  140. package/src/pages/build.tsx +108 -42
  141. package/src/pages/constants.ts +11 -0
  142. package/src/pages/deployment-tails.tsx +284 -0
  143. package/src/pages/deployments.tsx +9 -31
  144. package/src/pages/dev.tsx +417 -108
  145. package/src/pages/errors.ts +66 -0
  146. package/src/pages/functions/buildPlugin.ts +86 -84
  147. package/src/pages/functions/buildWorker.ts +121 -106
  148. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  149. package/src/pages/functions/routes-consolidation.ts +73 -0
  150. package/src/pages/functions/routes-transformation.test.ts +282 -0
  151. package/src/pages/functions/routes-transformation.ts +115 -0
  152. package/src/pages/functions/routes-validation.test.ts +403 -0
  153. package/src/pages/functions/routes-validation.ts +202 -0
  154. package/src/pages/functions/tsconfig.json +8 -0
  155. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  156. package/src/pages/functions.tsx +82 -0
  157. package/src/pages/hash.tsx +13 -0
  158. package/src/pages/index.tsx +73 -55
  159. package/src/pages/projects.tsx +9 -3
  160. package/src/pages/prompt-select-project.tsx +31 -0
  161. package/src/pages/publish.tsx +183 -60
  162. package/src/pages/types.ts +1 -0
  163. package/src/pages/upload.tsx +107 -56
  164. package/src/pages/utils.ts +13 -2
  165. package/src/paths.ts +49 -4
  166. package/src/proxy.ts +375 -157
  167. package/src/publish/index.ts +270 -0
  168. package/src/{publish.ts → publish/publish.ts} +431 -188
  169. package/src/pubsub/pubsub-commands.tsx +4 -3
  170. package/src/queues/cli/commands/consumer/add.ts +71 -0
  171. package/src/queues/cli/commands/consumer/index.ts +22 -0
  172. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  173. package/src/queues/cli/commands/create.ts +25 -0
  174. package/src/queues/cli/commands/delete.ts +26 -0
  175. package/src/queues/cli/commands/index.ts +36 -0
  176. package/src/queues/cli/commands/list.ts +25 -0
  177. package/src/queues/client.ts +135 -0
  178. package/src/queues/utils.ts +18 -0
  179. package/src/r2/constants.ts +4 -0
  180. package/src/r2/helpers.ts +131 -0
  181. package/src/r2/index.ts +289 -0
  182. package/src/routes.ts +140 -0
  183. package/src/secret/index.ts +379 -0
  184. package/src/sites.tsx +3 -3
  185. package/src/tail/createTail.ts +379 -0
  186. package/src/tail/filters.ts +4 -6
  187. package/src/tail/index.ts +199 -284
  188. package/src/tail/printing.ts +43 -3
  189. package/src/tsconfig-sanity.ts +16 -0
  190. package/src/type-generation.ts +181 -0
  191. package/src/user/access.ts +68 -0
  192. package/src/user/auth-variables.ts +113 -0
  193. package/src/user/choose-account.tsx +1 -1
  194. package/src/user/index.ts +0 -1
  195. package/src/user/user.tsx +116 -49
  196. package/src/utils/collectKeyValues.ts +14 -0
  197. package/src/whoami.tsx +80 -2
  198. package/src/worker.ts +69 -9
  199. package/src/yargs-types.ts +24 -0
  200. package/src/zones.ts +124 -9
  201. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  202. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  203. package/templates/__tests__/tsconfig.json +8 -0
  204. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  205. package/templates/d1-beta-facade.js +205 -0
  206. package/templates/facade.d.ts +14 -0
  207. package/templates/first-party-worker-module-facade.ts +19 -0
  208. package/templates/format-dev-errors.ts +33 -0
  209. package/templates/init-tests/test-jest-new-worker.js +23 -0
  210. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  211. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  212. package/templates/middleware/common.ts +62 -0
  213. package/templates/middleware/loader-modules.ts +82 -0
  214. package/templates/middleware/loader-sw.ts +233 -0
  215. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  216. package/templates/middleware/middleware-pretty-error.ts +40 -0
  217. package/templates/middleware/middleware-scheduled.ts +15 -0
  218. package/templates/new-worker.js +1 -1
  219. package/templates/pages-dev-pipeline.ts +31 -0
  220. package/templates/pages-dev-util.ts +52 -0
  221. package/templates/pages-shim.ts +4 -1
  222. package/templates/pages-template-plugin.ts +21 -5
  223. package/templates/pages-template-worker.ts +37 -9
  224. package/templates/{static-asset-facade.js → serve-static-assets.ts} +23 -7
  225. package/templates/service-bindings-module-facade.js +54 -0
  226. package/templates/service-bindings-sw-facade.js +42 -0
  227. package/templates/tsconfig-sanity.ts +11 -0
  228. package/templates/tsconfig.init.json +106 -0
  229. package/templates/tsconfig.json +5 -102
  230. package/templates/tsconfig.tsbuildinfo +1 -0
  231. package/wrangler-dist/cli.d.ts +621 -42
  232. package/wrangler-dist/cli.js +144441 -102606
  233. package/src/environment-variables.ts +0 -35
  234. package/src/faye-websocket.d.ts +0 -6
  235. package/src/generate.ts +0 -33
  236. package/src/miniflare-cli/enum-keys.ts +0 -17
  237. package/src/preview.ts +0 -31
  238. package/src/r2.ts +0 -50
  239. package/src/user/env-vars.ts +0 -46
@@ -1,48 +1,46 @@
1
1
  import { writeFileSync } from "node:fs";
2
- import { FormData, Headers } from "undici";
2
+ import { rest } from "msw";
3
3
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
4
- import {
5
- setMockResponse,
6
- unsetAllMocks,
7
- unsetMockFetchKVGetValues,
8
- setMockFetchKVGetValue,
9
- setMockRawResponse,
10
- createFetchResult,
11
- } from "./helpers/mock-cfetch";
12
4
  import { mockConsoleMethods } from "./helpers/mock-console";
13
5
  import { clearConfirmMocks, mockConfirm } from "./helpers/mock-dialogs";
14
6
  import { useMockIsTTY } from "./helpers/mock-istty";
15
- import { mockKeyListRequest } from "./helpers/mock-kv";
16
- import { mockGetMemberships } from "./helpers/mock-oauth-flow";
17
7
  import { mockProcess } from "./helpers/mock-process";
8
+ import { msw } from "./helpers/msw";
18
9
  import { runInTempDir } from "./helpers/run-in-tmp";
19
10
  import { runWrangler } from "./helpers/run-wrangler";
20
- import type { KeyValue, KVNamespaceInfo, NamespaceKeyInfo } from "../kv";
11
+ import type {
12
+ KeyValue,
13
+ KVNamespaceInfo,
14
+ NamespaceKeyInfo,
15
+ } from "../kv/helpers";
21
16
 
22
17
  describe("wrangler", () => {
23
18
  mockAccountId();
24
19
  mockApiToken();
25
20
  runInTempDir();
21
+
26
22
  const std = mockConsoleMethods();
27
23
  const proc = mockProcess();
28
24
 
29
25
  afterEach(() => {
30
- unsetAllMocks();
31
26
  clearConfirmMocks();
32
27
  });
33
-
34
28
  describe("kv:namespace", () => {
35
29
  describe("create", () => {
36
30
  function mockCreateRequest(expectedTitle: string) {
37
- setMockResponse(
38
- "/accounts/:accountId/storage/kv/namespaces",
39
- "POST",
40
- ([_url, accountId], { body }) => {
41
- expect(accountId).toEqual("some-account-id");
42
- const title = JSON.parse(body as string).title;
43
- expect(title).toEqual(expectedTitle);
44
- return { id: "some-namespace-id" };
45
- }
31
+ msw.use(
32
+ rest.post(
33
+ "*/accounts/:accountId/storage/kv/namespaces",
34
+ async (req, res, ctx) => {
35
+ expect(req.params.accountId).toEqual("some-account-id");
36
+ const title = (await req.json()).title as string;
37
+ expect(title).toEqual(expectedTitle);
38
+ return res.once(
39
+ ctx.status(200),
40
+ ctx.json(createFetchResult({ id: "some-namespace-id" }))
41
+ );
42
+ }
43
+ )
46
44
  );
47
45
  }
48
46
 
@@ -53,28 +51,28 @@ describe("wrangler", () => {
53
51
  `"Not enough non-option arguments: got 0, need at least 1"`
54
52
  );
55
53
  expect(std.out).toMatchInlineSnapshot(`
56
- "
57
- wrangler kv:namespace create <namespace>
54
+ "
55
+ wrangler kv:namespace create <namespace>
58
56
 
59
- Create a new namespace
57
+ Create a new namespace
60
58
 
61
- Positionals:
62
- namespace The name of the new namespace [string] [required]
59
+ Positionals:
60
+ namespace The name of the new namespace [string] [required]
63
61
 
64
- Flags:
65
- -c, --config Path to .toml configuration file [string]
66
- -h, --help Show help [boolean]
67
- -v, --version Show version number [boolean]
62
+ Flags:
63
+ -c, --config Path to .toml configuration file [string]
64
+ -e, --env Environment to use for operations and .env files [string]
65
+ -h, --help Show help [boolean]
66
+ -v, --version Show version number [boolean]
68
67
 
69
- Options:
70
- -e, --env Perform on a specific environment [string]
71
- --preview Interact with a preview namespace [boolean]"
72
- `);
68
+ Options:
69
+ --preview Interact with a preview namespace [boolean]"
70
+ `);
73
71
  expect(std.err).toMatchInlineSnapshot(`
74
- "X [ERROR] Not enough non-option arguments: got 0, need at least 1
72
+ "X [ERROR] Not enough non-option arguments: got 0, need at least 1
75
73
 
76
- "
77
- `);
74
+ "
75
+ `);
78
76
  });
79
77
 
80
78
  it("should error if the namespace to create contains spaces", async () => {
@@ -84,28 +82,28 @@ describe("wrangler", () => {
84
82
  `"Unknown arguments: def, ghi"`
85
83
  );
86
84
  expect(std.out).toMatchInlineSnapshot(`
87
- "
88
- wrangler kv:namespace create <namespace>
85
+ "
86
+ wrangler kv:namespace create <namespace>
89
87
 
90
- Create a new namespace
88
+ Create a new namespace
91
89
 
92
- Positionals:
93
- namespace The name of the new namespace [string] [required]
90
+ Positionals:
91
+ namespace The name of the new namespace [string] [required]
94
92
 
95
- Flags:
96
- -c, --config Path to .toml configuration file [string]
97
- -h, --help Show help [boolean]
98
- -v, --version Show version number [boolean]
93
+ Flags:
94
+ -c, --config Path to .toml configuration file [string]
95
+ -e, --env Environment to use for operations and .env files [string]
96
+ -h, --help Show help [boolean]
97
+ -v, --version Show version number [boolean]
99
98
 
100
- Options:
101
- -e, --env Perform on a specific environment [string]
102
- --preview Interact with a preview namespace [boolean]"
103
- `);
99
+ Options:
100
+ --preview Interact with a preview namespace [boolean]"
101
+ `);
104
102
  expect(std.err).toMatchInlineSnapshot(`
105
- "X [ERROR] Unknown arguments: def, ghi
103
+ "X [ERROR] Unknown arguments: def, ghi
106
104
 
107
- "
108
- `);
105
+ "
106
+ `);
109
107
  });
110
108
 
111
109
  it("should error if the namespace to create is not valid", async () => {
@@ -116,50 +114,50 @@ describe("wrangler", () => {
116
114
  );
117
115
 
118
116
  expect(std.out).toMatchInlineSnapshot(`
119
- "
120
- wrangler kv:namespace create <namespace>
117
+ "
118
+ wrangler kv:namespace create <namespace>
121
119
 
122
- Create a new namespace
120
+ Create a new namespace
123
121
 
124
- Positionals:
125
- namespace The name of the new namespace [string] [required]
122
+ Positionals:
123
+ namespace The name of the new namespace [string] [required]
126
124
 
127
- Flags:
128
- -c, --config Path to .toml configuration file [string]
129
- -h, --help Show help [boolean]
130
- -v, --version Show version number [boolean]
125
+ Flags:
126
+ -c, --config Path to .toml configuration file [string]
127
+ -e, --env Environment to use for operations and .env files [string]
128
+ -h, --help Show help [boolean]
129
+ -v, --version Show version number [boolean]
131
130
 
132
- Options:
133
- -e, --env Perform on a specific environment [string]
134
- --preview Interact with a preview namespace [boolean]"
135
- `);
131
+ Options:
132
+ --preview Interact with a preview namespace [boolean]"
133
+ `);
136
134
  expect(std.err).toMatchInlineSnapshot(`
137
- "X [ERROR] The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.
135
+ "X [ERROR] The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.
138
136
 
139
- "
140
- `);
137
+ "
138
+ `);
141
139
  });
142
140
 
143
141
  it("should create a namespace", async () => {
144
142
  mockCreateRequest("worker-UnitTestNamespace");
145
143
  await runWrangler("kv:namespace create UnitTestNamespace");
146
144
  expect(std.out).toMatchInlineSnapshot(`
147
- "🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
148
- ✨ Success!
149
- Add the following to your configuration file in your kv_namespaces array:
150
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
151
- `);
145
+ "🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
146
+ ✨ Success!
147
+ Add the following to your configuration file in your kv_namespaces array:
148
+ { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
149
+ `);
152
150
  });
153
151
 
154
152
  it("should create a preview namespace if configured to do so", async () => {
155
153
  mockCreateRequest("worker-UnitTestNamespace_preview");
156
154
  await runWrangler("kv:namespace create UnitTestNamespace --preview");
157
155
  expect(std.out).toMatchInlineSnapshot(`
158
- "🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
159
- ✨ Success!
160
- Add the following to your configuration file in your kv_namespaces array:
161
- { binding = \\"UnitTestNamespace\\", preview_id = \\"some-namespace-id\\" }"
162
- `);
156
+ "🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
157
+ ✨ Success!
158
+ Add the following to your configuration file in your kv_namespaces array:
159
+ { binding = \\"UnitTestNamespace\\", preview_id = \\"some-namespace-id\\" }"
160
+ `);
163
161
  });
164
162
 
165
163
  it("should create a namespace using configured worker name", async () => {
@@ -167,11 +165,11 @@ describe("wrangler", () => {
167
165
  mockCreateRequest("other-worker-UnitTestNamespace");
168
166
  await runWrangler("kv:namespace create UnitTestNamespace");
169
167
  expect(std.out).toMatchInlineSnapshot(`
170
- "🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
171
- ✨ Success!
172
- Add the following to your configuration file in your kv_namespaces array:
173
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
174
- `);
168
+ "🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
169
+ ✨ Success!
170
+ Add the following to your configuration file in your kv_namespaces array:
171
+ { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
172
+ `);
175
173
  });
176
174
 
177
175
  it("should create a namespace in an environment if configured to do so", async () => {
@@ -180,31 +178,41 @@ describe("wrangler", () => {
180
178
  "kv:namespace create UnitTestNamespace --env customEnv"
181
179
  );
182
180
  expect(std.out).toMatchInlineSnapshot(`
183
- "🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
184
- ✨ Success!
185
- Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
186
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
187
- `);
181
+ "🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
182
+ ✨ Success!
183
+ Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
184
+ { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
185
+ `);
188
186
  });
189
187
  });
190
188
 
191
189
  describe("list", () => {
192
- function mockListRequest(namespaces: unknown[]) {
190
+ function mockListRequest(namespaces: KVNamespaceInfo[]) {
193
191
  const requests = { count: 0 };
194
- setMockResponse(
195
- "/accounts/:accountId/storage/kv/namespaces",
196
- ([_url, accountId], init, query) => {
197
- requests.count++;
198
- expect(accountId).toEqual("some-account-id");
199
- expect(query.get("per_page")).toEqual("100");
200
- expect(query.get("order")).toEqual("title");
201
- expect(query.get("direction")).toEqual("asc");
202
- expect(query.get("page")).toEqual(`${requests.count}`);
203
- expect(init).toEqual({});
204
- const pageSize = Number(query.get("per_page"));
205
- const page = Number(query.get("page"));
206
- return namespaces.slice((page - 1) * pageSize, page * pageSize);
207
- }
192
+ msw.use(
193
+ rest.get(
194
+ "*/accounts/:accountId/storage/kv/namespaces",
195
+ async (req, res, ctx) => {
196
+ requests.count++;
197
+ expect(req.params.accountId).toEqual("some-account-id");
198
+ expect(req.url.searchParams.get("per_page")).toEqual("100");
199
+ expect(req.url.searchParams.get("order")).toEqual("title");
200
+ expect(req.url.searchParams.get("direction")).toEqual("asc");
201
+ expect(req.url.searchParams.get("page")).toEqual(
202
+ `${requests.count}`
203
+ );
204
+
205
+ const pageSize = Number(req.url.searchParams.get("per_page"));
206
+ const page = Number(req.url.searchParams.get("page"));
207
+ return res(
208
+ ctx.json(
209
+ createFetchResult(
210
+ namespaces.slice((page - 1) * pageSize, page * pageSize)
211
+ )
212
+ )
213
+ );
214
+ }
215
+ )
208
216
  );
209
217
  return requests;
210
218
  }
@@ -218,8 +226,7 @@ describe("wrangler", () => {
218
226
  await runWrangler("kv:namespace list");
219
227
 
220
228
  expect(std.err).toMatchInlineSnapshot(`""`);
221
- const namespaces = JSON.parse(std.out);
222
- expect(namespaces).toEqual(kvNamespaces);
229
+ expect(JSON.parse(std.out)).toEqual(kvNamespaces);
223
230
  });
224
231
 
225
232
  it("should make multiple requests for paginated results", async () => {
@@ -230,8 +237,8 @@ describe("wrangler", () => {
230
237
  }
231
238
  const requests = mockListRequest(kvNamespaces);
232
239
  await runWrangler("kv:namespace list");
233
- const namespaces = JSON.parse(std.out);
234
- expect(namespaces).toEqual(kvNamespaces);
240
+
241
+ expect(JSON.parse(std.out)).toEqual(kvNamespaces);
235
242
  expect(requests.count).toEqual(6);
236
243
  });
237
244
  });
@@ -239,15 +246,16 @@ describe("wrangler", () => {
239
246
  describe("delete", () => {
240
247
  function mockDeleteRequest(expectedNamespaceId: string) {
241
248
  const requests = { count: 0 };
242
- setMockResponse(
243
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId",
244
- "DELETE",
245
- ([_url, accountId, namespaceId]) => {
246
- requests.count++;
247
- expect(accountId).toEqual("some-account-id");
248
- expect(namespaceId).toEqual(expectedNamespaceId);
249
- return null;
250
- }
249
+ msw.use(
250
+ rest.delete(
251
+ "*/accounts/:accountId/storage/kv/namespaces/:namespaceId",
252
+ (req, res, ctx) => {
253
+ requests.count++;
254
+ expect(req.params.accountId).toEqual("some-account-id");
255
+ expect(req.params.namespaceId).toEqual(expectedNamespaceId);
256
+ return res(ctx.status(200), ctx.json(createFetchResult(null)));
257
+ }
258
+ )
251
259
  );
252
260
  return requests;
253
261
  }
@@ -286,12 +294,12 @@ describe("wrangler", () => {
286
294
  A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
287
295
  `);
288
296
  expect(std.err).toMatchInlineSnapshot(`
289
- "X [ERROR] Not able to delete namespace.
297
+ "X [ERROR] Not able to delete namespace.
290
298
 
291
- A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
299
+ A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
292
300
 
293
- "
294
- `);
301
+ "
302
+ `);
295
303
  });
296
304
 
297
305
  it("should delete a namespace specified by binding name in a given environment", async () => {
@@ -327,39 +335,43 @@ describe("wrangler", () => {
327
335
  expectedKV: KeyValue
328
336
  ) {
329
337
  const requests = { count: 0 };
330
- setMockResponse(
331
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
332
- "PUT",
333
- ([_url, accountId, namespaceId, key], { body }, query) => {
334
- requests.count++;
335
- expect(accountId).toEqual("some-account-id");
336
- expect(namespaceId).toEqual(expectedNamespaceId);
337
- expect(key).toEqual(expectedKV.key);
338
- if (expectedKV.metadata) {
339
- expect(body).toBeInstanceOf(FormData);
340
- expect((body as FormData).get("value")).toEqual(expectedKV.value);
341
- expect((body as FormData).get("metadata")).toEqual(
342
- JSON.stringify(expectedKV.metadata)
343
- );
344
- } else {
345
- expect(body).toEqual(expectedKV.value);
338
+ msw.use(
339
+ rest.put(
340
+ "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
341
+ (req, res, ctx) => {
342
+ requests.count++;
343
+ const { accountId, namespaceId, key } = req.params;
344
+ expect(accountId).toEqual("some-account-id");
345
+ expect(namespaceId).toEqual(expectedNamespaceId);
346
+ expect(encodeURIComponent(key as string)).toEqual(expectedKV.key);
347
+ // if (expectedKV.metadata) {
348
+ // expect(body).toBeInstanceOf(FormData);
349
+ // expect((body as FormData).get("value")).toEqual(
350
+ // expectedKV.value
351
+ // );
352
+ // expect((body as FormData).get("metadata")).toEqual(
353
+ // JSON.stringify(expectedKV.metadata)
354
+ // );
355
+ // } else {
356
+ // expect(body).toEqual(expectedKV.value);
357
+ // }
358
+ if (expectedKV.expiration !== undefined) {
359
+ expect(req.url.searchParams.get("expiration")).toEqual(
360
+ `${expectedKV.expiration}`
361
+ );
362
+ } else {
363
+ expect(req.url.searchParams.has("expiration")).toBe(false);
364
+ }
365
+ if (expectedKV.expiration_ttl) {
366
+ expect(req.url.searchParams.get("expiration_ttl")).toEqual(
367
+ `${expectedKV.expiration_ttl}`
368
+ );
369
+ } else {
370
+ expect(req.url.searchParams.has("expiration_ttl")).toBe(false);
371
+ }
372
+ return res(ctx.status(200), ctx.json(createFetchResult(null)));
346
373
  }
347
- if (expectedKV.expiration !== undefined) {
348
- expect(query.get("expiration")).toEqual(
349
- `${expectedKV.expiration}`
350
- );
351
- } else {
352
- expect(query.has("expiration")).toBe(false);
353
- }
354
- if (expectedKV.expiration_ttl) {
355
- expect(query.get("expiration_ttl")).toEqual(
356
- `${expectedKV.expiration_ttl}`
357
- );
358
- } else {
359
- expect(query.has("expiration_ttl")).toBe(false);
360
- }
361
- return null;
362
- }
374
+ )
363
375
  );
364
376
  return requests;
365
377
  }
@@ -527,35 +539,35 @@ describe("wrangler", () => {
527
539
  );
528
540
 
529
541
  expect(std.out).toMatchInlineSnapshot(`
530
- "
531
- wrangler kv:key put <key> [value]
532
-
533
- Writes a single key/value pair to the given namespace.
534
-
535
- Positionals:
536
- key The key to write to [string] [required]
537
- value The value to write [string]
538
-
539
- Flags:
540
- -c, --config Path to .toml configuration file [string]
541
- -h, --help Show help [boolean]
542
- -v, --version Show version number [boolean]
543
-
544
- Options:
545
- --binding The binding of the namespace to write to [string]
546
- --namespace-id The id of the namespace to write to [string]
547
- -e, --env Perform on a specific environment [string]
548
- --preview Interact with a preview namespace [boolean]
549
- --ttl Time for which the entries should be visible [number]
550
- --expiration Time since the UNIX epoch after which the entry expires [number]
551
- --metadata Arbitrary JSON that is associated with a key [string]
552
- --path Read value from the file at a given path [string]"
553
- `);
542
+ "
543
+ wrangler kv:key put <key> [value]
544
+
545
+ Writes a single key/value pair to the given namespace.
546
+
547
+ Positionals:
548
+ key The key to write to [string] [required]
549
+ value The value to write [string]
550
+
551
+ Flags:
552
+ -c, --config Path to .toml configuration file [string]
553
+ -e, --env Environment to use for operations and .env files [string]
554
+ -h, --help Show help [boolean]
555
+ -v, --version Show version number [boolean]
556
+
557
+ Options:
558
+ --binding The binding of the namespace to write to [string]
559
+ --namespace-id The id of the namespace to write to [string]
560
+ --preview Interact with a preview namespace [boolean]
561
+ --ttl Time for which the entries should be visible [number]
562
+ --expiration Time since the UNIX epoch after which the entry expires [number]
563
+ --metadata Arbitrary JSON that is associated with a key [string]
564
+ --path Read value from the file at a given path [string]"
565
+ `);
554
566
  expect(std.err).toMatchInlineSnapshot(`
555
- "X [ERROR] Not enough non-option arguments: got 0, need at least 1
567
+ "X [ERROR] Not enough non-option arguments: got 0, need at least 1
556
568
 
557
- "
558
- `);
569
+ "
570
+ `);
559
571
  });
560
572
 
561
573
  it("should error if no binding nor namespace is provided", async () => {
@@ -566,35 +578,35 @@ describe("wrangler", () => {
566
578
  );
567
579
 
568
580
  expect(std.out).toMatchInlineSnapshot(`
569
- "
570
- wrangler kv:key put <key> [value]
571
-
572
- Writes a single key/value pair to the given namespace.
573
-
574
- Positionals:
575
- key The key to write to [string] [required]
576
- value The value to write [string]
577
-
578
- Flags:
579
- -c, --config Path to .toml configuration file [string]
580
- -h, --help Show help [boolean]
581
- -v, --version Show version number [boolean]
582
-
583
- Options:
584
- --binding The binding of the namespace to write to [string]
585
- --namespace-id The id of the namespace to write to [string]
586
- -e, --env Perform on a specific environment [string]
587
- --preview Interact with a preview namespace [boolean]
588
- --ttl Time for which the entries should be visible [number]
589
- --expiration Time since the UNIX epoch after which the entry expires [number]
590
- --metadata Arbitrary JSON that is associated with a key [string]
591
- --path Read value from the file at a given path [string]"
592
- `);
581
+ "
582
+ wrangler kv:key put <key> [value]
583
+
584
+ Writes a single key/value pair to the given namespace.
585
+
586
+ Positionals:
587
+ key The key to write to [string] [required]
588
+ value The value to write [string]
589
+
590
+ Flags:
591
+ -c, --config Path to .toml configuration file [string]
592
+ -e, --env Environment to use for operations and .env files [string]
593
+ -h, --help Show help [boolean]
594
+ -v, --version Show version number [boolean]
595
+
596
+ Options:
597
+ --binding The binding of the namespace to write to [string]
598
+ --namespace-id The id of the namespace to write to [string]
599
+ --preview Interact with a preview namespace [boolean]
600
+ --ttl Time for which the entries should be visible [number]
601
+ --expiration Time since the UNIX epoch after which the entry expires [number]
602
+ --metadata Arbitrary JSON that is associated with a key [string]
603
+ --path Read value from the file at a given path [string]"
604
+ `);
593
605
  expect(std.err).toMatchInlineSnapshot(`
594
- "X [ERROR] Exactly one of the arguments binding and namespace-id is required
606
+ "X [ERROR] Exactly one of the arguments binding and namespace-id is required
595
607
 
596
- "
597
- `);
608
+ "
609
+ `);
598
610
  });
599
611
 
600
612
  it("should error if both binding and namespace is provided", async () => {
@@ -605,35 +617,35 @@ describe("wrangler", () => {
605
617
  );
606
618
 
607
619
  expect(std.out).toMatchInlineSnapshot(`
608
- "
609
- wrangler kv:key put <key> [value]
610
-
611
- Writes a single key/value pair to the given namespace.
612
-
613
- Positionals:
614
- key The key to write to [string] [required]
615
- value The value to write [string]
616
-
617
- Flags:
618
- -c, --config Path to .toml configuration file [string]
619
- -h, --help Show help [boolean]
620
- -v, --version Show version number [boolean]
621
-
622
- Options:
623
- --binding The binding of the namespace to write to [string]
624
- --namespace-id The id of the namespace to write to [string]
625
- -e, --env Perform on a specific environment [string]
626
- --preview Interact with a preview namespace [boolean]
627
- --ttl Time for which the entries should be visible [number]
628
- --expiration Time since the UNIX epoch after which the entry expires [number]
629
- --metadata Arbitrary JSON that is associated with a key [string]
630
- --path Read value from the file at a given path [string]"
631
- `);
620
+ "
621
+ wrangler kv:key put <key> [value]
622
+
623
+ Writes a single key/value pair to the given namespace.
624
+
625
+ Positionals:
626
+ key The key to write to [string] [required]
627
+ value The value to write [string]
628
+
629
+ Flags:
630
+ -c, --config Path to .toml configuration file [string]
631
+ -e, --env Environment to use for operations and .env files [string]
632
+ -h, --help Show help [boolean]
633
+ -v, --version Show version number [boolean]
634
+
635
+ Options:
636
+ --binding The binding of the namespace to write to [string]
637
+ --namespace-id The id of the namespace to write to [string]
638
+ --preview Interact with a preview namespace [boolean]
639
+ --ttl Time for which the entries should be visible [number]
640
+ --expiration Time since the UNIX epoch after which the entry expires [number]
641
+ --metadata Arbitrary JSON that is associated with a key [string]
642
+ --path Read value from the file at a given path [string]"
643
+ `);
632
644
  expect(std.err).toMatchInlineSnapshot(`
633
- "X [ERROR] Arguments binding and namespace-id are mutually exclusive
645
+ "X [ERROR] Arguments binding and namespace-id are mutually exclusive
634
646
 
635
- "
636
- `);
647
+ "
648
+ `);
637
649
  });
638
650
 
639
651
  it("should error if no value nor path is provided", async () => {
@@ -644,35 +656,35 @@ describe("wrangler", () => {
644
656
  );
645
657
 
646
658
  expect(std.out).toMatchInlineSnapshot(`
647
- "
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
- --binding The binding of the namespace to write to [string]
663
- --namespace-id The id of the namespace to write to [string]
664
- -e, --env Perform on a specific environment [string]
665
- --preview Interact with a preview namespace [boolean]
666
- --ttl Time for which the entries should be visible [number]
667
- --expiration Time since the UNIX epoch after which the entry expires [number]
668
- --metadata Arbitrary JSON that is associated with a key [string]
669
- --path Read value from the file at a given path [string]"
670
- `);
659
+ "
660
+ wrangler kv:key put <key> [value]
661
+
662
+ Writes a single key/value pair to the given namespace.
663
+
664
+ Positionals:
665
+ key The key to write to [string] [required]
666
+ value The value to write [string]
667
+
668
+ Flags:
669
+ -c, --config Path to .toml configuration file [string]
670
+ -e, --env Environment to use for operations and .env files [string]
671
+ -h, --help Show help [boolean]
672
+ -v, --version Show version number [boolean]
673
+
674
+ Options:
675
+ --binding The binding of the namespace to write to [string]
676
+ --namespace-id The id of the namespace to write to [string]
677
+ --preview Interact with a preview namespace [boolean]
678
+ --ttl Time for which the entries should be visible [number]
679
+ --expiration Time since the UNIX epoch after which the entry expires [number]
680
+ --metadata Arbitrary JSON that is associated with a key [string]
681
+ --path Read value from the file at a given path [string]"
682
+ `);
671
683
  expect(std.err).toMatchInlineSnapshot(`
672
- "X [ERROR] Exactly one of the arguments value and path is required
684
+ "X [ERROR] Exactly one of the arguments value and path is required
673
685
 
674
- "
675
- `);
686
+ "
687
+ `);
676
688
  });
677
689
 
678
690
  it("should error if both value and path is provided", async () => {
@@ -683,35 +695,35 @@ describe("wrangler", () => {
683
695
  );
684
696
 
685
697
  expect(std.out).toMatchInlineSnapshot(`
686
- "
687
- wrangler kv:key put <key> [value]
688
-
689
- Writes a single key/value pair to the given namespace.
690
-
691
- Positionals:
692
- key The key to write to [string] [required]
693
- value The value to write [string]
694
-
695
- Flags:
696
- -c, --config Path to .toml configuration file [string]
697
- -h, --help Show help [boolean]
698
- -v, --version Show version number [boolean]
699
-
700
- Options:
701
- --binding The binding of the namespace to write to [string]
702
- --namespace-id The id of the namespace to write to [string]
703
- -e, --env Perform on a specific environment [string]
704
- --preview Interact with a preview namespace [boolean]
705
- --ttl Time for which the entries should be visible [number]
706
- --expiration Time since the UNIX epoch after which the entry expires [number]
707
- --metadata Arbitrary JSON that is associated with a key [string]
708
- --path Read value from the file at a given path [string]"
709
- `);
698
+ "
699
+ wrangler kv:key put <key> [value]
700
+
701
+ Writes a single key/value pair to the given namespace.
702
+
703
+ Positionals:
704
+ key The key to write to [string] [required]
705
+ value The value to write [string]
706
+
707
+ Flags:
708
+ -c, --config Path to .toml configuration file [string]
709
+ -e, --env Environment to use for operations and .env files [string]
710
+ -h, --help Show help [boolean]
711
+ -v, --version Show version number [boolean]
712
+
713
+ Options:
714
+ --binding The binding of the namespace to write to [string]
715
+ --namespace-id The id of the namespace to write to [string]
716
+ --preview Interact with a preview namespace [boolean]
717
+ --ttl Time for which the entries should be visible [number]
718
+ --expiration Time since the UNIX epoch after which the entry expires [number]
719
+ --metadata Arbitrary JSON that is associated with a key [string]
720
+ --path Read value from the file at a given path [string]"
721
+ `);
710
722
  expect(std.err).toMatchInlineSnapshot(`
711
- "X [ERROR] Arguments value and path are mutually exclusive
723
+ "X [ERROR] Arguments value and path are mutually exclusive
712
724
 
713
- "
714
- `);
725
+ "
726
+ `);
715
727
  });
716
728
 
717
729
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
@@ -723,14 +735,14 @@ describe("wrangler", () => {
723
735
  );
724
736
 
725
737
  expect(std.out).toMatchInlineSnapshot(`
726
- "
727
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
728
- `);
738
+ "
739
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
740
+ `);
729
741
  expect(std.err).toMatchInlineSnapshot(`
730
- "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
742
+ "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
731
743
 
732
- "
733
- `);
744
+ "
745
+ `);
734
746
  });
735
747
 
736
748
  it("should error if a given binding has both preview and non-preview and --preview is not specified", async () => {
@@ -745,14 +757,14 @@ describe("wrangler", () => {
745
757
  `"someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace."`
746
758
  );
747
759
  expect(std.out).toMatchInlineSnapshot(`
748
- "
749
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
750
- `);
760
+ "
761
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
762
+ `);
751
763
  expect(std.err).toMatchInlineSnapshot(`
752
- "X [ERROR] someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.
764
+ "X [ERROR] someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.
753
765
 
754
- "
755
- `);
766
+ "
767
+ `);
756
768
  expect(requests.count).toEqual(0);
757
769
  });
758
770
  });
@@ -768,20 +780,20 @@ describe("wrangler", () => {
768
780
  await runWrangler("kv:key list --namespace-id some-namespace-id");
769
781
  expect(std.err).toMatchInlineSnapshot(`""`);
770
782
  expect(std.out).toMatchInlineSnapshot(`
771
- "[
772
- {
773
- \\"name\\": \\"key-1\\"
774
- },
775
- {
776
- \\"name\\": \\"key-2\\",
777
- \\"expiration\\": 123456789
778
- },
779
- {
780
- \\"name\\": \\"key-3\\",
781
- \\"expiration_ttl\\": 666
782
- }
783
- ]"
784
- `);
783
+ "[
784
+ {
785
+ \\"name\\": \\"key-1\\"
786
+ },
787
+ {
788
+ \\"name\\": \\"key-2\\",
789
+ \\"expiration\\": 123456789
790
+ },
791
+ {
792
+ \\"name\\": \\"key-3\\",
793
+ \\"expiration_ttl\\": 666
794
+ }
795
+ ]"
796
+ `);
785
797
  });
786
798
 
787
799
  it("should list the keys of a namespace specified by binding", async () => {
@@ -792,18 +804,18 @@ describe("wrangler", () => {
792
804
  await runWrangler("kv:key list --binding someBinding");
793
805
  expect(std.err).toMatchInlineSnapshot(`""`);
794
806
  expect(std.out).toMatchInlineSnapshot(`
795
- "[
796
- {
797
- \\"name\\": \\"key-1\\"
798
- },
799
- {
800
- \\"name\\": \\"key-2\\"
801
- },
802
- {
803
- \\"name\\": \\"key-3\\"
804
- }
805
- ]"
806
- `);
807
+ "[
808
+ {
809
+ \\"name\\": \\"key-1\\"
810
+ },
811
+ {
812
+ \\"name\\": \\"key-2\\"
813
+ },
814
+ {
815
+ \\"name\\": \\"key-3\\"
816
+ }
817
+ ]"
818
+ `);
807
819
  });
808
820
 
809
821
  it("should list the keys of a preview namespace specified by binding", async () => {
@@ -813,18 +825,18 @@ describe("wrangler", () => {
813
825
  await runWrangler("kv:key list --binding someBinding --preview");
814
826
  expect(std.err).toMatchInlineSnapshot(`""`);
815
827
  expect(std.out).toMatchInlineSnapshot(`
816
- "[
817
- {
818
- \\"name\\": \\"key-1\\"
819
- },
820
- {
821
- \\"name\\": \\"key-2\\"
822
- },
823
- {
824
- \\"name\\": \\"key-3\\"
825
- }
826
- ]"
827
- `);
828
+ "[
829
+ {
830
+ \\"name\\": \\"key-1\\"
831
+ },
832
+ {
833
+ \\"name\\": \\"key-2\\"
834
+ },
835
+ {
836
+ \\"name\\": \\"key-3\\"
837
+ }
838
+ ]"
839
+ `);
828
840
  });
829
841
 
830
842
  it("should list the keys of a namespace specified by binding, in a given environment", async () => {
@@ -836,18 +848,18 @@ describe("wrangler", () => {
836
848
  );
837
849
  expect(std.err).toMatchInlineSnapshot(`""`);
838
850
  expect(std.out).toMatchInlineSnapshot(`
839
- "[
840
- {
841
- \\"name\\": \\"key-1\\"
842
- },
843
- {
844
- \\"name\\": \\"key-2\\"
845
- },
846
- {
847
- \\"name\\": \\"key-3\\"
848
- }
849
- ]"
850
- `);
851
+ "[
852
+ {
853
+ \\"name\\": \\"key-1\\"
854
+ },
855
+ {
856
+ \\"name\\": \\"key-2\\"
857
+ },
858
+ {
859
+ \\"name\\": \\"key-3\\"
860
+ }
861
+ ]"
862
+ `);
851
863
  });
852
864
 
853
865
  it("should list the keys of a preview namespace specified by binding, in a given environment", async () => {
@@ -859,18 +871,18 @@ describe("wrangler", () => {
859
871
  );
860
872
  expect(std.err).toMatchInlineSnapshot(`""`);
861
873
  expect(std.out).toMatchInlineSnapshot(`
862
- "[
863
- {
864
- \\"name\\": \\"key-1\\"
865
- },
866
- {
867
- \\"name\\": \\"key-2\\"
868
- },
869
- {
870
- \\"name\\": \\"key-3\\"
871
- }
872
- ]"
873
- `);
874
+ "[
875
+ {
876
+ \\"name\\": \\"key-1\\"
877
+ },
878
+ {
879
+ \\"name\\": \\"key-2\\"
880
+ },
881
+ {
882
+ \\"name\\": \\"key-3\\"
883
+ }
884
+ ]"
885
+ `);
874
886
  });
875
887
 
876
888
  // We'll run the next test with variations on the cursor
@@ -911,22 +923,18 @@ describe("wrangler", () => {
911
923
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
912
924
  );
913
925
  expect(std.err).toMatchInlineSnapshot(`
914
- "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
926
+ "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
915
927
 
916
- "
917
- `);
928
+ "
929
+ `);
918
930
  expect(std.out).toMatchInlineSnapshot(`
919
- "
920
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
921
- `);
931
+ "
932
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
933
+ `);
922
934
  });
923
935
  });
924
936
 
925
937
  describe("get", () => {
926
- afterEach(() => {
927
- unsetMockFetchKVGetValues();
928
- });
929
-
930
938
  it("should get a key in a given namespace specified by namespace-id", async () => {
931
939
  setMockFetchKVGetValue(
932
940
  "some-account-id",
@@ -934,7 +942,9 @@ describe("wrangler", () => {
934
942
  "my-key",
935
943
  "my-value"
936
944
  );
945
+
937
946
  await runWrangler("kv:key get my-key --namespace-id some-namespace-id");
947
+
938
948
  expect(proc.write).toEqual(Buffer.from("my-value"));
939
949
  expect(std.err).toMatchInlineSnapshot(`""`);
940
950
  });
@@ -951,13 +961,13 @@ describe("wrangler", () => {
951
961
  );
952
962
  expect(proc.write).not.toEqual(Buffer.from("my-value"));
953
963
  expect(std).toMatchInlineSnapshot(`
954
- Object {
955
- "debug": "",
956
- "err": "",
957
- "out": "my-value",
958
- "warn": "",
959
- }
960
- `);
964
+ Object {
965
+ "debug": "",
966
+ "err": "",
967
+ "out": "my-value",
968
+ "warn": "",
969
+ }
970
+ `);
961
971
  });
962
972
 
963
973
  it("should get a binary and decode as utf8 text, resulting in improper decoding", async () => {
@@ -1043,9 +1053,10 @@ describe("wrangler", () => {
1043
1053
  setMockFetchKVGetValue(
1044
1054
  "some-account-id",
1045
1055
  "some-namespace-id",
1046
- "%2Fmy%2Ckey",
1056
+ "%2Fmy%2Ckey", // expect the key /my,key to be encoded
1047
1057
  "my-value"
1048
1058
  );
1059
+
1049
1060
  await runWrangler(
1050
1061
  "kv:key get /my,key --namespace-id some-namespace-id"
1051
1062
  );
@@ -1060,31 +1071,31 @@ describe("wrangler", () => {
1060
1071
  `"Not enough non-option arguments: got 0, need at least 1"`
1061
1072
  );
1062
1073
  expect(std.out).toMatchInlineSnapshot(`
1063
- "
1064
- wrangler kv:key get <key>
1065
-
1066
- Reads a single value by key from the given namespace.
1067
-
1068
- Positionals:
1069
- key The key value to get. [string] [required]
1070
-
1071
- Flags:
1072
- -c, --config Path to .toml configuration file [string]
1073
- -h, --help Show help [boolean]
1074
- -v, --version Show version number [boolean]
1075
-
1076
- Options:
1077
- --binding The name of the namespace to get from [string]
1078
- --namespace-id The id of the namespace to get from [string]
1079
- -e, --env Perform on a specific environment [string]
1080
- --preview Interact with a preview namespace [boolean] [default: false]
1081
- --text Decode the returned value as a utf8 string [boolean] [default: false]"
1082
- `);
1074
+ "
1075
+ wrangler kv:key get <key>
1076
+
1077
+ Reads a single value by key from the given namespace.
1078
+
1079
+ Positionals:
1080
+ key The key value to get. [string] [required]
1081
+
1082
+ Flags:
1083
+ -c, --config Path to .toml configuration file [string]
1084
+ -e, --env Environment to use for operations and .env files [string]
1085
+ -h, --help Show help [boolean]
1086
+ -v, --version Show version number [boolean]
1087
+
1088
+ Options:
1089
+ --binding The name of the namespace to get from [string]
1090
+ --namespace-id The id of the namespace to get from [string]
1091
+ --preview Interact with a preview namespace [boolean] [default: false]
1092
+ --text Decode the returned value as a utf8 string [boolean] [default: false]"
1093
+ `);
1083
1094
  expect(std.err).toMatchInlineSnapshot(`
1084
- "X [ERROR] Not enough non-option arguments: got 0, need at least 1
1095
+ "X [ERROR] Not enough non-option arguments: got 0, need at least 1
1085
1096
 
1086
- "
1087
- `);
1097
+ "
1098
+ `);
1088
1099
  });
1089
1100
 
1090
1101
  it("should error if no binding nor namespace is provided", async () => {
@@ -1094,31 +1105,31 @@ describe("wrangler", () => {
1094
1105
  `"Exactly one of the arguments binding and namespace-id is required"`
1095
1106
  );
1096
1107
  expect(std.out).toMatchInlineSnapshot(`
1097
- "
1098
- wrangler kv:key get <key>
1099
-
1100
- Reads a single value by key from the given namespace.
1101
-
1102
- Positionals:
1103
- key The key value to get. [string] [required]
1104
-
1105
- Flags:
1106
- -c, --config Path to .toml configuration file [string]
1107
- -h, --help Show help [boolean]
1108
- -v, --version Show version number [boolean]
1109
-
1110
- Options:
1111
- --binding The name of the namespace to get from [string]
1112
- --namespace-id The id of the namespace to get from [string]
1113
- -e, --env Perform on a specific environment [string]
1114
- --preview Interact with a preview namespace [boolean] [default: false]
1115
- --text Decode the returned value as a utf8 string [boolean] [default: false]"
1116
- `);
1108
+ "
1109
+ wrangler kv:key get <key>
1110
+
1111
+ Reads a single value by key from the given namespace.
1112
+
1113
+ Positionals:
1114
+ key The key value to get. [string] [required]
1115
+
1116
+ Flags:
1117
+ -c, --config Path to .toml configuration file [string]
1118
+ -e, --env Environment to use for operations and .env files [string]
1119
+ -h, --help Show help [boolean]
1120
+ -v, --version Show version number [boolean]
1121
+
1122
+ Options:
1123
+ --binding The name of the namespace to get from [string]
1124
+ --namespace-id The id of the namespace to get from [string]
1125
+ --preview Interact with a preview namespace [boolean] [default: false]
1126
+ --text Decode the returned value as a utf8 string [boolean] [default: false]"
1127
+ `);
1117
1128
  expect(std.err).toMatchInlineSnapshot(`
1118
- "X [ERROR] Exactly one of the arguments binding and namespace-id is required
1129
+ "X [ERROR] Exactly one of the arguments binding and namespace-id is required
1119
1130
 
1120
- "
1121
- `);
1131
+ "
1132
+ `);
1122
1133
  });
1123
1134
 
1124
1135
  it("should error if both binding and namespace is provided", async () => {
@@ -1129,31 +1140,31 @@ describe("wrangler", () => {
1129
1140
  );
1130
1141
 
1131
1142
  expect(std.out).toMatchInlineSnapshot(`
1132
- "
1133
- wrangler kv:key get <key>
1134
-
1135
- Reads a single value by key from the given namespace.
1136
-
1137
- Positionals:
1138
- key The key value to get. [string] [required]
1139
-
1140
- Flags:
1141
- -c, --config Path to .toml configuration file [string]
1142
- -h, --help Show help [boolean]
1143
- -v, --version Show version number [boolean]
1144
-
1145
- Options:
1146
- --binding The name of the namespace to get from [string]
1147
- --namespace-id The id of the namespace to get from [string]
1148
- -e, --env Perform on a specific environment [string]
1149
- --preview Interact with a preview namespace [boolean] [default: false]
1150
- --text Decode the returned value as a utf8 string [boolean] [default: false]"
1151
- `);
1143
+ "
1144
+ wrangler kv:key get <key>
1145
+
1146
+ Reads a single value by key from the given namespace.
1147
+
1148
+ Positionals:
1149
+ key The key value to get. [string] [required]
1150
+
1151
+ Flags:
1152
+ -c, --config Path to .toml configuration file [string]
1153
+ -e, --env Environment to use for operations and .env files [string]
1154
+ -h, --help Show help [boolean]
1155
+ -v, --version Show version number [boolean]
1156
+
1157
+ Options:
1158
+ --binding The name of the namespace to get from [string]
1159
+ --namespace-id The id of the namespace to get from [string]
1160
+ --preview Interact with a preview namespace [boolean] [default: false]
1161
+ --text Decode the returned value as a utf8 string [boolean] [default: false]"
1162
+ `);
1152
1163
  expect(std.err).toMatchInlineSnapshot(`
1153
- "X [ERROR] Arguments binding and namespace-id are mutually exclusive
1164
+ "X [ERROR] Arguments binding and namespace-id are mutually exclusive
1154
1165
 
1155
- "
1156
- `);
1166
+ "
1167
+ `);
1157
1168
  });
1158
1169
 
1159
1170
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
@@ -1164,14 +1175,14 @@ describe("wrangler", () => {
1164
1175
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
1165
1176
  );
1166
1177
  expect(std.out).toMatchInlineSnapshot(`
1167
- "
1168
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1169
- `);
1178
+ "
1179
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1180
+ `);
1170
1181
  expect(std.err).toMatchInlineSnapshot(`
1171
- "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1182
+ "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1172
1183
 
1173
- "
1174
- `);
1184
+ "
1185
+ `);
1175
1186
  });
1176
1187
 
1177
1188
  describe("non-interactive", () => {
@@ -1211,14 +1222,21 @@ describe("wrangler", () => {
1211
1222
  });
1212
1223
 
1213
1224
  it("should recommend using a configuration if unable to fetch memberships", async () => {
1214
- setMockRawResponse("/memberships", () => {
1215
- return createFetchResult(undefined, false, [
1216
- {
1217
- code: 9109,
1218
- message: "Uauthorized to access requested resource",
1219
- },
1220
- ]);
1221
- });
1225
+ msw.use(
1226
+ rest.get("*/memberships", (req, res, ctx) => {
1227
+ return res.once(
1228
+ ctx.status(200),
1229
+ ctx.json(
1230
+ createFetchResult(null, false, [
1231
+ {
1232
+ code: 9109,
1233
+ message: "Uauthorized to access requested resource",
1234
+ },
1235
+ ])
1236
+ )
1237
+ );
1238
+ })
1239
+ );
1222
1240
  await expect(runWrangler("kv:key get key --namespace-id=xxxx"))
1223
1241
  .rejects.toThrowErrorMatchingInlineSnapshot(`
1224
1242
  "Failed to automatically retrieve account IDs for the logged in user.
@@ -1250,16 +1268,20 @@ describe("wrangler", () => {
1250
1268
  expectedKey: string
1251
1269
  ) {
1252
1270
  const requests = { count: 0 };
1253
- setMockResponse(
1254
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
1255
- "DELETE",
1256
- ([_url, accountId, namespaceId, key]) => {
1257
- requests.count++;
1258
- expect(accountId).toEqual("some-account-id");
1259
- expect(namespaceId).toEqual(expectedNamespaceId);
1260
- expect(key).toEqual(expectedKey);
1261
- return null;
1262
- }
1271
+ msw.use(
1272
+ rest.delete(
1273
+ "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
1274
+ (req, res, ctx) => {
1275
+ requests.count++;
1276
+ expect(req.params.accountId).toEqual("some-account-id");
1277
+ expect(req.params.namespaceId).toEqual(expectedNamespaceId);
1278
+ expect(req.params.key).toEqual(expectedKey);
1279
+ return res.once(
1280
+ ctx.status(200),
1281
+ ctx.json(createFetchResult(null))
1282
+ );
1283
+ }
1284
+ )
1263
1285
  );
1264
1286
  return requests;
1265
1287
  }
@@ -1273,8 +1295,9 @@ describe("wrangler", () => {
1273
1295
  });
1274
1296
 
1275
1297
  it("should encode the key in the api request to delete a value", async () => {
1276
- const requests = mockDeleteRequest("voyager", "%2FNCC-74656");
1298
+ const requests = mockDeleteRequest("voyager", "/NCC-74656");
1277
1299
  await runWrangler(`kv:key delete --namespace-id voyager /NCC-74656`);
1300
+
1278
1301
  expect(requests.count).toEqual(1);
1279
1302
  expect(std.out).toMatchInlineSnapshot(
1280
1303
  `"Deleting the key \\"/NCC-74656\\" on namespace voyager."`
@@ -1309,10 +1332,10 @@ describe("wrangler", () => {
1309
1332
  );
1310
1333
 
1311
1334
  expect(std.err).toMatchInlineSnapshot(`
1312
- "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1335
+ "X [ERROR] A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1313
1336
 
1314
- "
1315
- `);
1337
+ "
1338
+ `);
1316
1339
  });
1317
1340
 
1318
1341
  it("should delete a key in a namespace specified by binding name in a given environment", async () => {
@@ -1346,21 +1369,22 @@ describe("wrangler", () => {
1346
1369
  expectedKeyValues: KeyValue[]
1347
1370
  ) {
1348
1371
  const requests = { count: 0 };
1349
- setMockResponse(
1350
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1351
- "PUT",
1352
- ([_url, accountId, namespaceId], { body }) => {
1353
- requests.count++;
1354
- expect(accountId).toEqual("some-account-id");
1355
- expect(namespaceId).toEqual(expectedNamespaceId);
1356
- expect(JSON.parse(body as string)).toEqual(
1357
- expectedKeyValues.slice(
1358
- (requests.count - 1) * 5000,
1359
- requests.count * 5000
1360
- )
1361
- );
1362
- return null;
1363
- }
1372
+ msw.use(
1373
+ rest.put(
1374
+ "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1375
+ async (req, res, ctx) => {
1376
+ requests.count++;
1377
+ expect(req.params.accountId).toEqual("some-account-id");
1378
+ expect(req.params.namespaceId).toEqual(expectedNamespaceId);
1379
+ expect(await req.json()).toEqual(
1380
+ expectedKeyValues.slice(
1381
+ (requests.count - 1) * 5000,
1382
+ requests.count * 5000
1383
+ )
1384
+ );
1385
+ return res(ctx.status(200), ctx.json(createFetchResult(null)));
1386
+ }
1387
+ )
1364
1388
  );
1365
1389
  return requests;
1366
1390
  }
@@ -1395,12 +1419,12 @@ describe("wrangler", () => {
1395
1419
  );
1396
1420
  expect(requests.count).toEqual(3);
1397
1421
  expect(std.out).toMatchInlineSnapshot(`
1398
- "Uploaded 0% (0 out of 12,000)
1399
- Uploaded 41% (5,000 out of 12,000)
1400
- Uploaded 83% (10,000 out of 12,000)
1401
- Uploaded 100% (12,000 out of 12,000)
1402
- Success!"
1403
- `);
1422
+ "Uploaded 0% (0 out of 12,000)
1423
+ Uploaded 41% (5,000 out of 12,000)
1424
+ Uploaded 83% (10,000 out of 12,000)
1425
+ Uploaded 100% (12,000 out of 12,000)
1426
+ Success!"
1427
+ `);
1404
1428
  expect(std.warn).toMatchInlineSnapshot(`""`);
1405
1429
  expect(std.err).toMatchInlineSnapshot(`""`);
1406
1430
  });
@@ -1415,9 +1439,9 @@ describe("wrangler", () => {
1415
1439
  Expected an array of key-value objects but got type \\"object\\"."
1416
1440
  `);
1417
1441
  expect(std.out).toMatchInlineSnapshot(`
1418
- "
1419
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1420
- `);
1442
+ "
1443
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1444
+ `);
1421
1445
  expect(std.warn).toMatchInlineSnapshot(`""`);
1422
1446
  });
1423
1447
 
@@ -1477,16 +1501,16 @@ describe("wrangler", () => {
1477
1501
  `);
1478
1502
 
1479
1503
  expect(std.out).toMatchInlineSnapshot(`
1480
- "
1481
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1482
- `);
1504
+ "
1505
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1506
+ `);
1483
1507
  expect(std.warn).toMatchInlineSnapshot(`
1484
- "▲ [WARNING] Unexpected key-value properties in \\"keys.json\\".
1508
+ "▲ [WARNING] Unexpected key-value properties in \\"keys.json\\".
1485
1509
 
1486
- The item at index 5 contains unexpected properties: [\\"invalid\\"].
1510
+ The item at index 5 contains unexpected properties: [\\"invalid\\"].
1487
1511
 
1488
- "
1489
- `);
1512
+ "
1513
+ `);
1490
1514
  });
1491
1515
  });
1492
1516
 
@@ -1496,24 +1520,25 @@ describe("wrangler", () => {
1496
1520
  expectedKeys: string[]
1497
1521
  ) {
1498
1522
  const requests = { count: 0 };
1499
- setMockResponse(
1500
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1501
- "DELETE",
1502
- ([_url, accountId, namespaceId], { headers, body }) => {
1503
- requests.count++;
1504
- expect(accountId).toEqual("some-account-id");
1505
- expect(namespaceId).toEqual(expectedNamespaceId);
1506
- expect(new Headers(headers ?? []).get("Content-Type")).toEqual(
1507
- "application/json"
1508
- );
1509
- expect(JSON.parse(body as string)).toEqual(
1510
- expectedKeys.slice(
1511
- (requests.count - 1) * 5000,
1512
- requests.count * 5000
1513
- )
1514
- );
1515
- return null;
1516
- }
1523
+ msw.use(
1524
+ rest.delete(
1525
+ "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1526
+ async (req, res, ctx) => {
1527
+ requests.count++;
1528
+ expect(req.params.accountId).toEqual("some-account-id");
1529
+ expect(req.params.namespaceId).toEqual(expectedNamespaceId);
1530
+ expect(req.headers.get("Content-Type")).toEqual(
1531
+ "application/json"
1532
+ );
1533
+ expect(await req.json()).toEqual(
1534
+ expectedKeys.slice(
1535
+ (requests.count - 1) * 5000,
1536
+ requests.count * 5000
1537
+ )
1538
+ );
1539
+ return res(ctx.status(200), ctx.json(createFetchResult(null)));
1540
+ }
1541
+ )
1517
1542
  );
1518
1543
  return requests;
1519
1544
  }
@@ -1548,12 +1573,12 @@ describe("wrangler", () => {
1548
1573
  );
1549
1574
  expect(requests.count).toEqual(3);
1550
1575
  expect(std.out).toMatchInlineSnapshot(`
1551
- "Deleted 0% (0 out of 12,000)
1552
- Deleted 41% (5,000 out of 12,000)
1553
- Deleted 83% (10,000 out of 12,000)
1554
- Deleted 100% (12,000 out of 12,000)
1555
- Success!"
1556
- `);
1576
+ "Deleted 0% (0 out of 12,000)
1577
+ Deleted 41% (5,000 out of 12,000)
1578
+ Deleted 83% (10,000 out of 12,000)
1579
+ Deleted 100% (12,000 out of 12,000)
1580
+ Success!"
1581
+ `);
1557
1582
  expect(std.warn).toMatchInlineSnapshot(`""`);
1558
1583
  expect(std.err).toMatchInlineSnapshot(`""`);
1559
1584
  });
@@ -1618,9 +1643,9 @@ describe("wrangler", () => {
1618
1643
  12354"
1619
1644
  `);
1620
1645
  expect(std.out).toMatchInlineSnapshot(`
1621
- "
1622
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1623
- `);
1646
+ "
1647
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1648
+ `);
1624
1649
  expect(std.warn).toMatchInlineSnapshot(`""`);
1625
1650
  });
1626
1651
 
@@ -1643,9 +1668,9 @@ describe("wrangler", () => {
1643
1668
  The item at index 3 is type: \\"object\\" - null"
1644
1669
  `);
1645
1670
  expect(std.out).toMatchInlineSnapshot(`
1646
- "
1647
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1648
- `);
1671
+ "
1672
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
1673
+ `);
1649
1674
  expect(std.warn).toMatchInlineSnapshot(`""`);
1650
1675
  });
1651
1676
  });
@@ -1669,3 +1694,93 @@ function writeWranglerConfig() {
1669
1694
  "utf-8"
1670
1695
  );
1671
1696
  }
1697
+
1698
+ function setMockFetchKVGetValue(
1699
+ accountId: string,
1700
+ namespaceId: string,
1701
+ key: string,
1702
+ value: string | Buffer
1703
+ ) {
1704
+ msw.use(
1705
+ rest.get(
1706
+ "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
1707
+ (req, res, ctx) => {
1708
+ console.dir(req.params);
1709
+ expect(req.params.accountId).toEqual(accountId);
1710
+ expect(req.params.namespaceId).toEqual(namespaceId);
1711
+ // Getting the key from params decodes it so we need to grab the encoded key from the URL
1712
+ expect(req.url.toString().split("/").pop()).toBe(key);
1713
+
1714
+ return res.once(ctx.status(200), ctx.body(value));
1715
+ }
1716
+ )
1717
+ );
1718
+ }
1719
+
1720
+ function createFetchResult(
1721
+ result: unknown,
1722
+ success = true,
1723
+ errors: { code: number; message: string }[] = []
1724
+ ) {
1725
+ return {
1726
+ success,
1727
+ errors,
1728
+ messages: [],
1729
+ result,
1730
+ };
1731
+ }
1732
+
1733
+ function mockGetMemberships(
1734
+ accounts: { id: string; account: { id: string; name: string } }[]
1735
+ ) {
1736
+ msw.use(
1737
+ rest.get("*/memberships", (req, res, ctx) => {
1738
+ return res.once(ctx.json(createFetchResult(accounts)));
1739
+ })
1740
+ );
1741
+ }
1742
+
1743
+ function mockKeyListRequest(
1744
+ expectedNamespaceId: string,
1745
+ expectedKeys: NamespaceKeyInfo[],
1746
+ keysPerRequest = 1000,
1747
+ blankCursorValue: "" | undefined | null = undefined
1748
+ ) {
1749
+ const requests = { count: 0 };
1750
+ // See https://api.cloudflare.com/#workers-kv-namespace-list-a-namespace-s-keys
1751
+ msw.use(
1752
+ rest.get(
1753
+ "*/accounts/:accountId/storage/kv/namespaces/:namespaceId/keys",
1754
+ (req, res, ctx) => {
1755
+ requests.count++;
1756
+ let result;
1757
+ let cursor;
1758
+
1759
+ expect(req.params.accountId).toEqual("some-account-id");
1760
+ expect(req.params.namespaceId).toEqual(expectedNamespaceId);
1761
+
1762
+ if (expectedKeys.length <= keysPerRequest) {
1763
+ result = expectedKeys;
1764
+ } else {
1765
+ const start =
1766
+ parseInt(req.url.searchParams.get("cursor") ?? "0") || 0;
1767
+ const end = start + keysPerRequest;
1768
+ cursor = end < expectedKeys.length ? end : blankCursorValue;
1769
+ result = expectedKeys.slice(start, end);
1770
+ }
1771
+ return res(
1772
+ ctx.json({
1773
+ success: true,
1774
+ errors: [],
1775
+ messages: [],
1776
+ result,
1777
+ result_info: {
1778
+ cursor,
1779
+ },
1780
+ })
1781
+ );
1782
+ }
1783
+ )
1784
+ );
1785
+ return requests;
1786
+ }