@nodeart/cloudflare-provisioning 1.0.12 → 1.0.14

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.
package/cloudflare.js CHANGED
@@ -210,7 +210,43 @@ class CloudFlare {
210
210
  response = await body.text()
211
211
  }
212
212
 
213
- if (statusCode !== 200) {
213
+ if (statusCode === 404) {
214
+ console.log('Firewall ruleset was not found. Initializing firewall ruleset creation...')
215
+ const createRulesetUrl = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets`
216
+ const payload = {
217
+ name: 'Custom firewall ruleset',
218
+ kind: 'zone',
219
+ phase: 'http_request_firewall_custom',
220
+ rules: []
221
+ }
222
+
223
+ const { statusCode: createStatusCode, body: createBody } = await this.requestWithDelay(createRulesetUrl, {
224
+ method: 'POST',
225
+ headers: {
226
+ ...this.authorizationHeaders,
227
+ 'Content-Type': 'application/json'
228
+ },
229
+ body: JSON.stringify(payload)
230
+ })
231
+
232
+ let createResponse
233
+ try {
234
+ createResponse = await createBody.json()
235
+ } catch (e) {
236
+ createResponse = await createBody.text()
237
+ }
238
+
239
+ if (createStatusCode !== 200) {
240
+ throw new Error(`Could not create firewall ruleset: ${createStatusCode}, error: ${JSON.stringify(createResponse)}`)
241
+ }
242
+
243
+ const { id, rules } = createResponse?.result ?? {}
244
+ if (!id) {
245
+ throw new Error(`Could not get firewall rules ruleset ID: got ${id}, received value: ${JSON.stringify(createResponse)}`)
246
+ }
247
+
248
+ return { id, rules: rules ?? [] }
249
+ } else if (statusCode !== 200) {
214
250
  throw new Error(`Could not get firewall rules: ${statusCode}, error: ${JSON.stringify(response)}`)
215
251
  }
216
252
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodeart/cloudflare-provisioning",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/template.js CHANGED
@@ -23,7 +23,7 @@ module.exports = {
23
23
  },
24
24
  workers: {
25
25
  disableApi: {
26
- pattern: '*$DOMAIN/api/*',
26
+ pattern: 'www.$DOMAIN/api/*',
27
27
  script: null
28
28
  },
29
29
  rootDomainCookies: {