From d38dfd869df37ca6340c243be0bfdf78b7f4754c Mon Sep 17 00:00:00 2001 From: Jake Wood Date: Wed, 3 Aug 2022 10:41:05 -0400 Subject: [PATCH 1/3] feat: allow cors to be optional --- index.d.ts | 1 + src/Webflow.js | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3b2d566d..70ff670d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -178,6 +178,7 @@ declare namespace Webflow { token: string; endpoint?: string; version?: string; + useCors?: boolean; } namespace WebflowApiModel { diff --git a/src/Webflow.js b/src/Webflow.js index e5e8a49c..32c27af8 100644 --- a/src/Webflow.js +++ b/src/Webflow.js @@ -52,7 +52,7 @@ const responseHandler = (res) => }); export default class Webflow { - constructor({ endpoint = DEFAULT_ENDPOINT, token, version = "1.0.0" } = {}) { + constructor({ endpoint = DEFAULT_ENDPOINT, token, version = "1.0.0", useCors = true } = {}) { if (!token) throw buildRequiredArgError("token"); this.responseWrapper = new ResponseWrapper(this); @@ -75,9 +75,12 @@ export default class Webflow { const opts = { method, headers: this.headers, - mode: "cors", }; + if (useCors) { + opts.mode = "cors"; + } + if (data) { opts.body = JSON.stringify(data); } From bac82754abe6865947fee268a1aaec9f167f00cf Mon Sep 17 00:00:00 2001 From: Jake Wood Date: Mon, 8 Aug 2022 22:37:34 -0400 Subject: [PATCH 2/3] feat: overwrite the entirety of fetch options --- index.d.ts | 2 +- src/Webflow.js | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 70ff670d..a304fb74 100644 --- a/index.d.ts +++ b/index.d.ts @@ -178,7 +178,7 @@ declare namespace Webflow { token: string; endpoint?: string; version?: string; - useCors?: boolean; + fetchOpts?: RequestInit; } namespace WebflowApiModel { diff --git a/src/Webflow.js b/src/Webflow.js index 32c27af8..a0201bca 100644 --- a/src/Webflow.js +++ b/src/Webflow.js @@ -52,7 +52,7 @@ const responseHandler = (res) => }); export default class Webflow { - constructor({ endpoint = DEFAULT_ENDPOINT, token, version = "1.0.0", useCors = true } = {}) { + constructor({ endpoint = DEFAULT_ENDPOINT, token, version = "1.0.0", fetchOpts = {} } = {}) { if (!token) throw buildRequiredArgError("token"); this.responseWrapper = new ResponseWrapper(this); @@ -75,12 +75,10 @@ export default class Webflow { const opts = { method, headers: this.headers, + mode: "cors", + ...fetchOpts, }; - if (useCors) { - opts.mode = "cors"; - } - if (data) { opts.body = JSON.stringify(data); } From 42b64c52b107962212d3ed562c4607ce1874f61d Mon Sep 17 00:00:00 2001 From: Jake Wood Date: Mon, 8 Aug 2022 22:40:55 -0400 Subject: [PATCH 3/3] docs: add readme for fetchOpts --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 8fcd8d55..25eaeb01 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ const Webflow = require('webflow-api'); // Initialize the API const api = new Webflow({ token: 'api-token' }); +// Initialize the API without CORS mode +const apiNoCors = new Webflow({ token: 'api-token', fetchOpts: { mode: undefined }}); + // Fetch a site api.site({ siteId: '580e63e98c9a982ac9b8b741' }).then(site => console.log(site)); ``` @@ -32,6 +35,7 @@ The `Webflow` constructor takes several options to initialize the API client: * `token` - the API token **(required)** * `version` - the version of the API you wish to use (optional) +* `fetchOpts` - an option to override `fetch` options All of the API methods are documented in the [API documentation](https://developers.webflow.com).