@@ -10,7 +10,23 @@ if (!process.env.CLOUDFLARE_ACCOUNT_ID) {
10
10
process . exit ( 1 ) ;
11
11
}
12
12
13
- const apiFetch = async ( path , init = { method : "GET" } , queryParams = { } ) => {
13
+ type ApiErrorBody = {
14
+ errors : string [ ] ;
15
+ } ;
16
+
17
+ type ApiSuccessBody = {
18
+ result : any [ ] ;
19
+ } ;
20
+
21
+ type Project = {
22
+ name : string ;
23
+ } ;
24
+
25
+ const apiFetch = async (
26
+ path : string ,
27
+ init = { method : "GET" } ,
28
+ queryParams = { }
29
+ ) => {
14
30
const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${ process . env . CLOUDFLARE_ACCOUNT_ID } ` ;
15
31
const queryString = queryParams
16
32
? `?${ new URLSearchParams ( queryParams ) . toString ( ) } `
@@ -27,7 +43,7 @@ const apiFetch = async (path, init = { method: "GET" }, queryParams = {}) => {
27
43
if ( response . status >= 400 ) {
28
44
console . error ( `REQUEST ERROR: ${ url } ` ) ;
29
45
console . error ( `(${ response . status } ) ${ response . statusText } ` ) ;
30
- const body = await response . json ( ) ;
46
+ const body = ( await response . json ( ) ) as ApiErrorBody ;
31
47
console . error ( body . errors ) ;
32
48
33
49
// Returning null instead of throwing an error here allows the caller to decide whether
@@ -37,7 +53,7 @@ const apiFetch = async (path, init = { method: "GET" }, queryParams = {}) => {
37
53
return null ;
38
54
}
39
55
40
- const json = await response . json ( ) ;
56
+ const json = ( await response . json ( ) ) as ApiSuccessBody ;
41
57
42
58
return json . result ;
43
59
} ;
@@ -72,20 +88,25 @@ const listC3Projects = async () => {
72
88
return projects . filter ( ( p ) => p . name . startsWith ( "c3-e2e-" ) ) ;
73
89
} ;
74
90
75
- const deleteProject = async ( project ) => {
76
- console . log ( `Deleting project: ${ project . name } ` ) ;
77
- await apiFetch ( `/pages/projects/${ project . name } ` , {
91
+ export const deleteProject = async ( project : string ) => {
92
+ console . log ( `Deleting project: ${ project } ` ) ;
93
+ await apiFetch ( `/pages/projects/${ project } ` , {
78
94
method : "DELETE" ,
79
95
} ) ;
80
96
} ;
81
97
82
- const projectsToDelete = await listC3Projects ( ) ;
83
- for ( const project of projectsToDelete ) {
84
- await deleteProject ( project ) ;
85
- }
98
+ const run = async ( ) => {
99
+ const projectsToDelete = ( await listC3Projects ( ) ) as Project [ ] ;
86
100
87
- if ( projectsToDelete . length === 0 ) {
88
- console . log ( `No projects to delete.` ) ;
89
- } else {
90
- console . log ( `Successfully deleted ${ projectsToDelete . length } projects` ) ;
91
- }
101
+ for ( const project of projectsToDelete ) {
102
+ await deleteProject ( project . name ) ;
103
+ }
104
+
105
+ if ( projectsToDelete . length === 0 ) {
106
+ console . log ( `No projects to delete.` ) ;
107
+ } else {
108
+ console . log ( `Successfully deleted ${ projectsToDelete . length } projects` ) ;
109
+ }
110
+ } ;
111
+
112
+ run ( ) ;
0 commit comments