Skip to content

Commit d2b3698

Browse files
committed
PR comments: include link to CORS API docs on error, use parseJSON
1 parent d37461d commit d2b3698

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

packages/wrangler/src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ import {
9999
r2BucketUpdateNamespace,
100100
r2BucketUpdateStorageClassCommand,
101101
} from "./r2/bucket";
102+
import {
103+
r2BucketCORSDeleteCommand,
104+
r2BucketCORSListCommand,
105+
r2BucketCORSNamespace,
106+
r2BucketCORSSetCommand,
107+
} from "./r2/cors";
102108
import {
103109
r2BucketDomainAddCommand,
104110
r2BucketDomainListCommand,
@@ -829,6 +835,22 @@ export function createCLIParser(argv: string[]) {
829835
command: "wrangler r2 bucket lifecycle set",
830836
definition: r2BucketLifecycleSetCommand,
831837
},
838+
{
839+
command: "wrangler r2 bucket cors",
840+
definition: r2BucketCORSNamespace,
841+
},
842+
{
843+
command: "wrangler r2 bucket cors delete",
844+
definition: r2BucketCORSDeleteCommand,
845+
},
846+
{
847+
command: "wrangler r2 bucket cors list",
848+
definition: r2BucketCORSListCommand,
849+
},
850+
{
851+
command: "wrangler r2 bucket cors set",
852+
definition: r2BucketCORSSetCommand,
853+
},
832854
]);
833855
registry.registerNamespace("r2");
834856

packages/wrangler/src/r2/cors.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { defineCommand, defineNamespace } from "../core";
1+
import path from "node:path";
2+
import { createCommand, createNamespace } from "../core/create-command";
23
import { confirm } from "../dialogs";
34
import { UserError } from "../errors";
45
import { logger } from "../logger";
5-
import { readFileSync } from "../parse";
6+
import { parseJSON, readFileSync } from "../parse";
67
import { requireAuth } from "../user";
78
import formatLabelledValues from "../utils/render-labelled-values";
89
import {
@@ -13,17 +14,15 @@ import {
1314
} from "./helpers";
1415
import type { CORSRule } from "./helpers";
1516

16-
defineNamespace({
17-
command: "wrangler r2 bucket cors",
17+
export const r2BucketCORSNamespace = createNamespace({
1818
metadata: {
1919
description: "Manage CORS configuration for an R2 bucket",
2020
status: "stable",
2121
owner: "Product: R2",
2222
},
2323
});
2424

25-
defineCommand({
26-
command: "wrangler r2 bucket cors list",
25+
export const r2BucketCORSListCommand = createCommand({
2726
metadata: {
2827
description: "List the CORS rules for an R2 bucket",
2928
status: "stable",
@@ -60,8 +59,7 @@ defineCommand({
6059
},
6160
});
6261

63-
defineCommand({
64-
command: "wrangler r2 bucket cors set",
62+
export const r2BucketCORSSetCommand = createCommand({
6563
metadata: {
6664
description: "Set the CORS configuration for an R2 bucket from a JSON file",
6765
status: "stable",
@@ -96,22 +94,17 @@ defineCommand({
9694
async handler({ bucket, file, jurisdiction, force }, { config }) {
9795
const accountId = await requireAuth(config);
9896

99-
let corsConfig: { rules: CORSRule[] };
100-
try {
101-
corsConfig = JSON.parse(readFileSync(file));
102-
} catch (e) {
103-
if (e instanceof Error) {
104-
throw new UserError(
105-
`Failed to read or parse the CORS configuration file: '${e.message}'`
106-
);
107-
} else {
108-
throw e;
109-
}
110-
}
97+
const jsonFilePath = path.resolve(file);
98+
99+
const corsConfig = parseJSON<{ rules: CORSRule[] }>(
100+
readFileSync(jsonFilePath),
101+
jsonFilePath
102+
);
111103

112104
if (!corsConfig.rules || !Array.isArray(corsConfig.rules)) {
113105
throw new UserError(
114-
"The CORS configuration file must contain a 'rules' array."
106+
`The CORS configuration file must contain a 'rules' array as expected by the request body of the CORS API: ` +
107+
`https://developers.cloudflare.com/api/operations/r2-put-bucket-cors-policy`
115108
);
116109
}
117110

@@ -133,8 +126,7 @@ defineCommand({
133126
},
134127
});
135128

136-
defineCommand({
137-
command: "wrangler r2 bucket cors delete",
129+
export const r2BucketCORSDeleteCommand = createCommand({
138130
metadata: {
139131
description: "Clear the CORS configuration for an R2 bucket",
140132
status: "stable",

0 commit comments

Comments
 (0)