webflow-api 0.8.0 → 1.0.0

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/README.md CHANGED
@@ -1,39 +1,244 @@
1
1
  # Webflow CMS API Client
2
2
 
3
- ## Requirements
4
-
5
- * Node.js 4+
6
- * NPM / YARN
7
- * Webpack / Browserify (optional)
8
-
9
3
  ## Installation
10
4
 
11
- Install the package via NPM or YARN:
5
+ Using npm:
12
6
 
13
7
  ```shell
14
- $ npm install --save webflow-api
8
+ $ npm install webflow-api
9
+ ```
15
10
 
11
+ using yarn
12
+ ```
16
13
  $ yarn add webflow-api
17
14
  ```
18
15
 
19
16
  ## Usage
17
+ The constructor takes in a few optional parameters to initialize the API client
18
+
19
+ * `token` - the access token to use
20
+ * `headers` - additional headers to add to the request
21
+ * `version` - the version of the API you wish to use
22
+ * `mode` - the [sec-fetch-mode](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Mode) to use
23
+
24
+ ``` javascript
25
+ const Webflow = require("webflow-api");
26
+
27
+ // initialize the client with the access token
28
+ const webflow = new Webflow({ token: "[ACCESS TOKEN]" });
29
+
30
+ // fully loaded
31
+ const webflow = new Webflow({
32
+ token: "[ACCESS TOKEN]"
33
+ version: "1.0.0",
34
+ mode: "cors",
35
+ headers: {
36
+ "User-Agent": "My Webflow App / 1.0"
37
+ }
38
+ });
39
+ ```
40
+ ## Basic Usage
41
+ ### Chaining Calls
42
+ You can retrieve child resources by chaining calls on the parent object.
43
+
44
+ ```javascript
45
+ // get the first site
46
+ const [site] = await webflow.sites();
47
+
48
+ // get the first collection in the site
49
+ const [collection] = await site.collections();
50
+
51
+ // get the first item in the collection
52
+ const [item] = await collection.items();
53
+
54
+ // get one item from the collection
55
+ const item = await collection.items({ itemId: "[ITEM ID]" });
56
+ ```
57
+
58
+ ### Pagination
59
+ To paginate results, pass in the `limit` and `offset` options.
60
+
61
+ ```javascript
62
+ // Get the first page of results
63
+ const page1 = await collection.items({ limit: 20 });
64
+
65
+ // Get the second page of results
66
+ const page2 = await collection.items({ limit: 20, offset: 20 });
67
+ ```
68
+
69
+ ### Rate Limit
70
+ Check rate limit status on each call by checking the `_meta` property.
71
+
72
+ ```javascript
73
+ // make an api call
74
+ const site = await webflow.site({ siteId: "[SITE ID]" });
75
+
76
+ // check rate limit
77
+ const { rateLimit } = site._meta; // { limit: 60, remaining: 56 }
78
+ ```
79
+ ### Update Token
80
+ If you need to update the access token, you can set the `token` property at any time.
81
+
82
+ ```javascript
83
+ // token is unset
84
+ const webflow = new Webflow();
85
+
86
+ // set token
87
+ webflow.token = "[ACCESS TOKEN]";
88
+ ```
89
+ ### Calling APIs Directly
90
+ All Webflow API endpoints can be called directly using the `get`, `post`, `put`, and `delete` methods.
20
91
 
21
92
  ```javascript
22
- const Webflow = require('webflow-api');
93
+ // call the sites endpoint directly
94
+ const sites = await webflow.get("/sites");
95
+
96
+ // post to an endpoint directly
97
+ const result = await webflow.post("/sites/[SITE ID]/publish", {
98
+ domains: ["hello-webflow.com"]
99
+ });
100
+ ```
101
+
102
+ ## OAuth
103
+ To implement OAuth, you'll need a Webflow App registered and a webserver running, that is publicly facing.
23
104
 
24
- // Initialize the API
25
- const api = new Webflow({ token: 'api-token' });
105
+ ### Authorize
106
+ The first step in OAuth is to generate an authorization url to redirect the user to.
26
107
 
27
- // Fetch a site
28
- api.site({ siteId: '580e63e98c9a982ac9b8b741' }).then(site => console.log(site));
108
+ ```javascript
109
+ // Get the authorization url to redirect users to
110
+ const url = webflow.authorizeUrl({
111
+ client_id: "[CLIENT ID]",
112
+ state: "1234567890", // optional
113
+ redirect_uri: "https://my.server.com/oauth/callback" // optional
114
+ });
115
+
116
+ // redirect user from your server route
117
+ res.redirect(url);
29
118
  ```
30
119
 
31
- The `Webflow` constructor takes several options to initialize the API client:
120
+ ### Access Token
121
+ Once a user has authorized their Webflow resource(s), Webflow will redirect back to your server with a `code`. Use this to get an access token.
122
+
123
+ ```javascript
124
+ const auth = await webflow.accessToken({
125
+ client_id,
126
+ client_secret,
127
+ code,
128
+ redirect_uri // optional - required if used in the authorize step
129
+ });
130
+
131
+ // you now have the user's access token to make API requests with
132
+ const userWF = new Webflow({ token: auth.access_token });
133
+
134
+ // pull information for the installer
135
+ const installer = await userWF.installer();
136
+ ```
32
137
 
33
- * `token` - the API token **(required)**
34
- * `version` - the version of the API you wish to use (optional)
138
+ ### Revoke Token
139
+ If the user decides to disconnect from your server, you should call revoke token to remove the authorization.
140
+
141
+ ```javascript
142
+ const result = await webflow.revokeToken({
143
+ client_id,
144
+ client_secret,
145
+ access_token
146
+ });
147
+
148
+ // ensure it went through
149
+ if (result.didRevoke) {
150
+ // should equal true
151
+ }
152
+ ```
153
+
154
+ ## Examples
155
+ ### Sites
156
+ Get all sites available or lookup by site id.
157
+
158
+ ```javascript
159
+ // List all sites
160
+ const sites = await webflow.sites();
161
+
162
+ // Get a single site
163
+ const site = await webflow.sites({ siteId: "[SITE ID]" });
164
+ ```
165
+
166
+ ### Collections
167
+ Get all collections available for a site or lookup by collection id.
168
+ ```javascript
169
+ // Get a site's collection from the site
170
+ const collections = await site.collections();
171
+
172
+ // Get a site's collection by passing in a site id
173
+ const collections = await webflow.collections({ siteId: "[SITE ID]" });
174
+
175
+ // Get a single collection
176
+ const collection = await webflow.collection({ collectionId: "[COLLECTION ID]" });
177
+ ```
178
+
179
+ ### Collection Items
180
+ Get all collection items available for a collection or lookup by item id.
181
+ ```javascript
182
+ // Get the items from a collection
183
+ const items = await collection.items();
184
+
185
+ // Get a subset of items
186
+ const items = await collection.items({ limit: 10, offset: 2 });
187
+
188
+ // Get a single item
189
+ const item = await webflow.item({ collectionId: "[COLLECTION ID]", itemId: "[ITEM ID]" });
190
+ ```
191
+ ### Update an Item
192
+ ```javascript
193
+ // Set the fields to update
194
+ const fields = {
195
+ name: "New Name",
196
+ _archived: false,
197
+ _draft: false,
198
+ slug: "new-name",
199
+ };
200
+
201
+ // call update
202
+ const updatedItem = await webflow.updateItem({
203
+ collectionId: "[COLLECTION ID]",
204
+ itemId: "[ITEM ID]",
205
+ fields,
206
+ });
207
+ ```
208
+
209
+ ### Memberships
210
+ ```javascript
211
+ // Get the all users for a site
212
+ const users = await webflow.users({
213
+ siteId: "[SITE ID]"
214
+ });
215
+
216
+ // Get a single user
217
+ const user = await site.user({
218
+ siteId: "[SITE ID]",
219
+ userId: "[USER ID]"
220
+ });
221
+ ```
222
+
223
+ ### Webhooks
224
+ ```javascript
225
+ // get webhooks for a site
226
+ const webhooks = await site.webhooks();
227
+
228
+ // create a webhook
229
+ const webhook = await site.createWebhook({
230
+ triggerType: "form_submission",
231
+ url: "https://webhook.site"
232
+ });
233
+
234
+ ```
235
+
236
+ ### Installer
237
+ ```javascript
238
+ // pull information for the installer
239
+ const installer = await webflow.installer();
240
+ ```
35
241
 
36
- All of the API methods are documented in the [API documentation](https://developers.webflow.com).
37
242
 
38
243
  ## Contributing
39
244
 
@@ -3,202 +3,133 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
7
-
8
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
9
-
10
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
-
12
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
-
14
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
-
16
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
17
-
18
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
19
-
20
- var ResponseWrapper = /*#__PURE__*/function () {
21
- function ResponseWrapper(api) {
22
- _classCallCheck(this, ResponseWrapper);
23
-
6
+ exports.default = void 0;
7
+ class ResponseWrapper {
8
+ constructor(api) {
24
9
  this.api = api;
25
10
  }
26
-
27
- _createClass(ResponseWrapper, [{
28
- key: "site",
29
- value: function site(_site) {
30
- return _objectSpread(_objectSpread({}, _site), {}, {
31
- collections: this.api.collections.bind(this.api, {
32
- siteId: _site._id
33
- }),
34
- webhooks: this.api.webhooks.bind(this.api, {
35
- siteId: _site._id
36
- }),
37
- domains: this.api.domains.bind(this.api, {
38
- siteId: _site._id
39
- }),
40
- webhook: function webhook(first) {
41
- var _this$api;
42
-
43
- for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
44
- rest[_key - 1] = arguments[_key];
45
- }
46
-
47
- return (_this$api = this.api).webhook.apply(_this$api, [_objectSpread(_objectSpread({}, first), {}, {
48
- siteId: _site._id
49
- })].concat(rest));
50
- },
51
- createWebhook: function createWebhook(first) {
52
- var _this$api2;
53
-
54
- for (var _len2 = arguments.length, rest = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
55
- rest[_key2 - 1] = arguments[_key2];
56
- }
57
-
58
- return (_this$api2 = this.api).createWebhook.apply(_this$api2, [_objectSpread(_objectSpread({}, first), {}, {
59
- siteId: _site._id
60
- })].concat(rest));
61
- },
62
- removeWebhook: function removeWebhook(first) {
63
- var _this$api3;
64
-
65
- for (var _len3 = arguments.length, rest = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
66
- rest[_key3 - 1] = arguments[_key3];
67
- }
68
-
69
- return (_this$api3 = this.api).removeWebhook.apply(_this$api3, [_objectSpread(_objectSpread({}, first), {}, {
70
- siteId: _site._id
71
- })].concat(rest));
72
- },
73
- publishSite: function publishSite(domains) {
74
- return this.api.publishSite({
75
- siteId: _site._id,
76
- domains: domains
77
- });
78
- }
79
- });
80
- }
81
- }, {
82
- key: "domain",
83
- value: function domain(_domain) {
84
- return _objectSpread({}, _domain);
85
- }
86
- }, {
87
- key: "collection",
88
- value: function collection(_collection) {
89
- return _objectSpread(_objectSpread({}, _collection), {}, {
90
- items: this.api.items.bind(this.api, {
91
- collectionId: _collection._id
92
- }),
93
- item: function item(first) {
94
- var _this$api4;
95
-
96
- for (var _len4 = arguments.length, rest = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
97
- rest[_key4 - 1] = arguments[_key4];
98
- }
99
-
100
- return (_this$api4 = this.api).item.apply(_this$api4, [_objectSpread(_objectSpread({}, first), {}, {
101
- collectionId: _collection._id
102
- })].concat(rest));
103
- },
104
- createItem: function createItem(first) {
105
- var _this$api5;
106
-
107
- for (var _len5 = arguments.length, rest = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
108
- rest[_key5 - 1] = arguments[_key5];
109
- }
110
-
111
- return (_this$api5 = this.api).createItem.apply(_this$api5, [_objectSpread(_objectSpread({}, first), {}, {
112
- collectionId: _collection._id
113
- })].concat(rest));
114
- },
115
- updateItem: function updateItem(first) {
116
- var _this$api6;
117
-
118
- for (var _len6 = arguments.length, rest = new Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) {
119
- rest[_key6 - 1] = arguments[_key6];
120
- }
121
-
122
- return (_this$api6 = this.api).updateItem.apply(_this$api6, [_objectSpread(_objectSpread({}, first), {}, {
123
- collectionId: _collection._id
124
- })].concat(rest));
125
- },
126
- removeItem: function removeItem(first) {
127
- var _this$api7;
128
-
129
- for (var _len7 = arguments.length, rest = new Array(_len7 > 1 ? _len7 - 1 : 0), _key7 = 1; _key7 < _len7; _key7++) {
130
- rest[_key7 - 1] = arguments[_key7];
131
- }
132
-
133
- return (_this$api7 = this.api).removeItem.apply(_this$api7, [_objectSpread(_objectSpread({}, first), {}, {
134
- collectionId: _collection._id
135
- })].concat(rest));
136
- }
137
- });
138
- }
139
- }, {
140
- key: "item",
141
- value: function item(_item, collectionId) {
142
- return _objectSpread(_objectSpread({}, _item), {}, {
143
- update: function update(first) {
144
- var _this$api8;
145
-
146
- for (var _len8 = arguments.length, rest = new Array(_len8 > 1 ? _len8 - 1 : 0), _key8 = 1; _key8 < _len8; _key8++) {
147
- rest[_key8 - 1] = arguments[_key8];
148
- }
149
-
150
- return (_this$api8 = this.api).updateItem.apply(_this$api8, [_objectSpread(_objectSpread({}, first), {}, {
151
- collectionId: collectionId,
152
- itemId: _item._id
153
- })].concat(rest));
154
- },
155
- remove: this.api.updateItem.bind(this.api, {
156
- collectionId: collectionId,
157
- itemId: _item._id
158
- })
159
- });
160
- }
161
- }, {
162
- key: "user",
163
- value: function user(_user, siteId) {
164
- return _objectSpread(_objectSpread({}, _user), {}, {
165
- update: function update(first) {
166
- var _this$api9;
167
-
168
- for (var _len9 = arguments.length, rest = new Array(_len9 > 1 ? _len9 - 1 : 0), _key9 = 1; _key9 < _len9; _key9++) {
169
- rest[_key9 - 1] = arguments[_key9];
170
- }
171
-
172
- return (_this$api9 = this.api).updateUser.apply(_this$api9, [_objectSpread(_objectSpread({}, first), {}, {
173
- siteId: siteId
174
- })].concat(rest));
175
- },
176
- remove: function remove(first) {
177
- var _this$api10;
178
-
179
- for (var _len10 = arguments.length, rest = new Array(_len10 > 1 ? _len10 - 1 : 0), _key10 = 1; _key10 < _len10; _key10++) {
180
- rest[_key10 - 1] = arguments[_key10];
181
- }
182
-
183
- return (_this$api10 = this.api).removeUser.apply(_this$api10, [_objectSpread(_objectSpread({}, first), {}, {
184
- siteId: siteId
185
- })].concat(rest));
186
- }
187
- });
188
- }
189
- }, {
190
- key: "webhook",
191
- value: function webhook(_webhook, siteId) {
192
- return _objectSpread(_objectSpread({}, _webhook), {}, {
193
- remove: this.api.removeWebhook.bind(this.api, {
194
- siteId: siteId,
195
- webhookId: _webhook._id
196
- })
197
- });
198
- }
199
- }]);
200
-
201
- return ResponseWrapper;
202
- }();
203
-
204
- exports["default"] = ResponseWrapper;
11
+ site(site) {
12
+ return {
13
+ ...site,
14
+ collections: this.api.collections.bind(this.api, {
15
+ siteId: site._id
16
+ }),
17
+ webhooks: this.api.webhooks.bind(this.api, {
18
+ siteId: site._id
19
+ }),
20
+ domains: this.api.domains.bind(this.api, {
21
+ siteId: site._id
22
+ }),
23
+ webhook: (first, ...rest) => {
24
+ return this.api.webhook({
25
+ ...first,
26
+ siteId: site._id
27
+ }, ...rest);
28
+ },
29
+ createWebhook: (first, ...rest) => {
30
+ return this.api.createWebhook({
31
+ ...first,
32
+ siteId: site._id
33
+ }, ...rest);
34
+ },
35
+ removeWebhook: (first, ...rest) => {
36
+ return this.api.removeWebhook({
37
+ ...first,
38
+ siteId: site._id
39
+ }, ...rest);
40
+ },
41
+ publishSite: domains => {
42
+ return this.api.publishSite({
43
+ siteId: site._id,
44
+ domains
45
+ });
46
+ }
47
+ };
48
+ }
49
+ domain(domain) {
50
+ return {
51
+ ...domain
52
+ };
53
+ }
54
+ collection(collection) {
55
+ return {
56
+ ...collection,
57
+ items: this.api.items.bind(this.api, {
58
+ collectionId: collection._id
59
+ }),
60
+ item: (first, ...rest) => {
61
+ return this.api.item({
62
+ ...first,
63
+ collectionId: collection._id
64
+ }, ...rest);
65
+ },
66
+ createItem: (first, ...rest) => {
67
+ return this.api.createItem({
68
+ ...first,
69
+ collectionId: collection._id
70
+ }, ...rest);
71
+ },
72
+ updateItem: (first, ...rest) => {
73
+ return this.api.updateItem({
74
+ ...first,
75
+ collectionId: collection._id
76
+ }, ...rest);
77
+ },
78
+ removeItem: (first, ...rest) => {
79
+ return this.api.removeItem({
80
+ ...first,
81
+ collectionId: collection._id
82
+ }, ...rest);
83
+ }
84
+ };
85
+ }
86
+ item(item, collectionId) {
87
+ const itemId = item._id;
88
+ const ids = {
89
+ itemId,
90
+ collectionId
91
+ };
92
+ return {
93
+ ...item,
94
+ update: (itemData, query) => {
95
+ return this.api.updateItem({
96
+ ...itemData,
97
+ ...ids
98
+ }, query);
99
+ },
100
+ remove: query => {
101
+ return this.api.removeItem(ids, query);
102
+ }
103
+ };
104
+ }
105
+ user(user, siteId) {
106
+ const userId = user._id;
107
+ const ids = {
108
+ userId,
109
+ siteId
110
+ };
111
+ return {
112
+ ...user,
113
+ update: userData => {
114
+ return this.api.updateUser({
115
+ ...ids,
116
+ ...userData
117
+ });
118
+ },
119
+ remove: this.api.removeUser.bind(this.api, ids)
120
+ };
121
+ }
122
+ webhook(webhook, siteId) {
123
+ const webhookId = webhook._id;
124
+ const ids = {
125
+ webhookId,
126
+ siteId
127
+ };
128
+ return {
129
+ ...webhook,
130
+ remove: this.api.removeWebhook.bind(this.api, ids)
131
+ };
132
+ }
133
+ }
134
+ exports.default = ResponseWrapper;
135
+ module.exports = exports.default;