1- import { defineCommand , defineNamespace } from "../core" ;
1+ import path from "node:path" ;
2+ import { createCommand , createNamespace } from "../core/create-command" ;
23import { confirm } from "../dialogs" ;
34import { UserError } from "../errors" ;
45import { logger } from "../logger" ;
5- import { readFileSync } from "../parse" ;
6+ import { parseJSON , readFileSync } from "../parse" ;
67import { requireAuth } from "../user" ;
78import formatLabelledValues from "../utils/render-labelled-values" ;
89import {
@@ -13,17 +14,15 @@ import {
1314} from "./helpers" ;
1415import 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