webflow-api 0.8.1 → 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/dist/Webflow.js CHANGED
@@ -1,482 +1,338 @@
1
1
  "use strict";
2
2
 
3
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
- exports["default"] = void 0;
9
-
10
- var _isomorphicFetch = _interopRequireDefault(require("isomorphic-fetch"));
11
-
12
- var _qs = _interopRequireDefault(require("qs"));
13
-
14
- var _utils = require("./utils");
15
-
6
+ exports.WebflowArgumentError = exports.Webflow = void 0;
7
+ Object.defineProperty(exports, "WebflowRequestError", {
8
+ enumerable: true,
9
+ get: function () {
10
+ return _WebflowClient.WebflowRequestError;
11
+ }
12
+ });
13
+ exports.default = void 0;
14
+ var _WebflowClient = require("./WebflowClient");
16
15
  var _ResponseWrapper = _interopRequireDefault(require("./ResponseWrapper"));
17
-
18
- var _WebflowError = _interopRequireWildcard(require("./WebflowError"));
19
-
20
- var _excluded = ["collectionId"],
21
- _excluded2 = ["collectionId", "itemId"],
22
- _excluded3 = ["collectionId", "itemId"],
23
- _excluded4 = ["collectionId", "itemIds"],
24
- _excluded5 = ["collectionId", "itemIds"],
25
- _excluded6 = ["siteId", "userId"],
26
- _excluded7 = ["siteId"];
27
-
28
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
29
-
30
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
31
-
32
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
33
-
34
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
35
-
36
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
37
-
38
- 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; }
39
-
40
- 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; }
41
-
42
- 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; }
43
-
44
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
45
-
46
- 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); } }
47
-
48
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
49
-
50
- var DEFAULT_ENDPOINT = "https://api.webflow.com";
51
-
52
- var buildMeta = function buildMeta(res) {
53
- if (!res || !res.headers) {
54
- return {};
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+ class WebflowArgumentError extends Error {
18
+ constructor(name) {
19
+ super(`Argument '${name}' is required but was not present`);
20
+ }
21
+ }
22
+ exports.WebflowArgumentError = WebflowArgumentError;
23
+ class Webflow {
24
+ constructor(options = {}) {
25
+ this.client = new _WebflowClient.WebflowClient(options);
26
+ this.responseWrapper = new _ResponseWrapper.default(this);
27
+ }
28
+ get(path, query = {}) {
29
+ return this.client.get(path, query);
30
+ }
31
+ post(path, data, query = {}) {
32
+ return this.client.post(path, data, query);
33
+ }
34
+ put(path, data, query = {}) {
35
+ return this.client.put(path, data, query);
36
+ }
37
+ patch(path, data, query = {}) {
38
+ return this.client.patch(path, data, query);
39
+ }
40
+ delete(path, data, query = {}) {
41
+ return this.client.delete(path, data, query);
55
42
  }
56
43
 
57
- return {
58
- rateLimit: {
59
- limit: parseInt(res.headers.get("x-ratelimit-limit"), 10),
60
- remaining: parseInt(res.headers.get("x-ratelimit-remaining"), 10)
61
- }
62
- };
63
- };
64
-
65
- var responseHandler = function responseHandler(res) {
66
- return res.json()["catch"](function (err) {
67
- return (// Catch unexpected server errors where json isn't sent and rewrite
68
- // with proper class (WebflowError)
69
- Promise.reject(new _WebflowError["default"](err))
70
- );
71
- }).then(function (body) {
72
- if (res.status >= 400) {
73
- var errOpts = {
74
- code: body.code,
75
- msg: body.msg,
76
- _meta: buildMeta(res)
77
- };
78
-
79
- if (body.problems && body.problems.length > 0) {
80
- errOpts.problems = body.problems;
81
- }
82
-
83
- var errMsg = body && body.err ? body.err : "Unknown error occured";
84
- var err = new _WebflowError["default"](errMsg);
85
- return Promise.reject(Object.assign(err, errOpts));
86
- }
87
-
88
- body._meta = buildMeta(res); // eslint-disable-line no-param-reassign
89
-
90
- return body;
91
- });
92
- };
93
-
94
- var Webflow = /*#__PURE__*/function () {
95
- function Webflow() {
96
- var _this = this;
44
+ // Meta
45
+ info() {
46
+ return this.get("/info");
47
+ }
48
+ installer() {
49
+ return this.get("/user");
50
+ }
97
51
 
98
- var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
99
- _ref$endpoint = _ref.endpoint,
100
- endpoint = _ref$endpoint === void 0 ? DEFAULT_ENDPOINT : _ref$endpoint,
101
- token = _ref.token,
102
- _ref$version = _ref.version,
103
- version = _ref$version === void 0 ? "1.0.0" : _ref$version;
52
+ // Sites
53
+ async sites(query = {}) {
54
+ const sites = await this.get("/sites", query);
55
+ return sites.map(site => this.responseWrapper.site(site));
56
+ }
57
+ async site({
58
+ siteId
59
+ }, query = {}) {
60
+ if (!siteId) throw new WebflowArgumentError("siteId");
61
+ const site = await this.get(`/sites/${siteId}`, query);
62
+ return this.responseWrapper.site(site);
63
+ }
64
+ publishSite({
65
+ siteId,
66
+ domains
67
+ }) {
68
+ if (!siteId) throw new WebflowArgumentError("siteId");
69
+ if (!domains) throw new WebflowArgumentError("domains");
70
+ return this.post(`/sites/${siteId}/publish`, {
71
+ domains
72
+ });
73
+ }
74
+ async domains({
75
+ siteId
76
+ }) {
77
+ if (!siteId) throw new WebflowArgumentError("siteId");
78
+ const domains = await this.client.get(`/sites/${siteId}/domains`);
79
+ return domains.map(domain => this.responseWrapper.domain(domain, siteId));
80
+ }
104
81
 
105
- _classCallCheck(this, Webflow);
82
+ // Collections
83
+ async collections({
84
+ siteId
85
+ }, query = {}) {
86
+ if (!siteId) throw new WebflowArgumentError("siteId");
87
+ const collections = await this.get(`/sites/${siteId}/collections`, query);
88
+ return collections.map(collection => this.responseWrapper.collection(collection));
89
+ }
90
+ async collection({
91
+ collectionId
92
+ }, query = {}) {
93
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
94
+ const uri = `/collections/${collectionId}`;
95
+ const collection = await this.client.get(uri, query);
96
+ return this.responseWrapper.collection(collection);
97
+ }
106
98
 
107
- if (!token) throw (0, _WebflowError.buildRequiredArgError)("token");
108
- this.responseWrapper = new _ResponseWrapper["default"](this);
109
- this.endpoint = endpoint;
110
- this.token = token;
111
- this.headers = {
112
- Accept: "application/json",
113
- Authorization: "Bearer ".concat(token),
114
- "accept-version": version,
115
- "Content-Type": "application/json"
99
+ // Items
100
+ async items({
101
+ collectionId
102
+ }, query = {}) {
103
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
104
+ const uri = `/collections/${collectionId}/items`;
105
+ const res = await this.client.get(uri, query);
106
+ return {
107
+ ...res,
108
+ items: res.items.map(item => this.responseWrapper.item(item, collectionId))
116
109
  };
110
+ }
111
+ async item({
112
+ collectionId,
113
+ itemId
114
+ }, query = {}) {
115
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
116
+ if (!itemId) throw new WebflowArgumentError("itemId");
117
+ const uri = `/collections/${collectionId}/items/${itemId}`;
118
+ const {
119
+ items
120
+ } = await this.client.get(uri, query);
121
+ return this.responseWrapper.item(items[0], collectionId);
122
+ }
123
+ async createItem({
124
+ collectionId,
125
+ ...data
126
+ }, query = {}) {
127
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
128
+ const uri = `/collections/${collectionId}/items`;
129
+ const item = await this.post(uri, data, query);
130
+ return this.responseWrapper.item(item, collectionId);
131
+ }
132
+ updateItem({
133
+ collectionId,
134
+ itemId,
135
+ ...data
136
+ }, query = {}) {
137
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
138
+ if (!itemId) throw new WebflowArgumentError("itemId");
139
+ const uri = `/collections/${collectionId}/items/${itemId}`;
140
+ return this.put(uri, data, query);
141
+ }
142
+ removeItem({
143
+ collectionId,
144
+ itemId
145
+ }, query = {}) {
146
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
147
+ if (!itemId) throw new WebflowArgumentError("itemId");
148
+ const uri = `/collections/${collectionId}/items/${itemId}`;
149
+ return this.delete(uri, null, query);
150
+ }
151
+ patchItem({
152
+ collectionId,
153
+ itemId,
154
+ ...data
155
+ }, query = {}) {
156
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
157
+ if (!itemId) throw new WebflowArgumentError("itemId");
158
+ const uri = `/collections/${collectionId}/items/${itemId}`;
159
+ return this.patch(uri, data, query);
160
+ }
161
+ deleteItems({
162
+ collectionId,
163
+ itemIds,
164
+ ...data
165
+ }, query = {}) {
166
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
167
+ if (!itemIds) throw new WebflowArgumentError("itemIds");
168
+ const uri = `/collections/${collectionId}/items`;
169
+ return this.delete(uri, {
170
+ ...data,
171
+ itemIds
172
+ }, query);
173
+ }
174
+ publishItems({
175
+ collectionId,
176
+ itemIds,
177
+ ...data
178
+ }, query = {}) {
179
+ if (!collectionId) throw new WebflowArgumentError("collectionId");
180
+ if (!itemIds) throw new WebflowArgumentError("itemIds");
181
+ const uri = `/collections/${collectionId}/items/publish`;
182
+ return this.put(uri, {
183
+ ...data,
184
+ itemIds
185
+ }, query);
186
+ }
117
187
 
118
- this.authenticatedFetch = function (method, path, data, query) {
119
- var queryString = query && !(0, _utils.isObjectEmpty)(query) ? "?".concat(_qs["default"].stringify(query)) : "";
120
- var uri = "".concat(_this.endpoint).concat(path).concat(queryString);
121
- var opts = {
122
- method: method,
123
- headers: _this.headers,
124
- mode: "cors"
125
- };
126
-
127
- if (data) {
128
- opts.body = JSON.stringify(data);
129
- }
130
-
131
- return (0, _isomorphicFetch["default"])(uri, opts).then(responseHandler);
188
+ // Users
189
+ async users({
190
+ siteId
191
+ }, query = {}) {
192
+ if (!siteId) throw new WebflowArgumentError("siteId");
193
+ const res = await this.get(`/sites/${siteId}/users`, query);
194
+ return {
195
+ ...res,
196
+ users: res.users.map(user => this.responseWrapper.user(user, siteId))
132
197
  };
133
- } // Generic HTTP request handlers
134
-
135
-
136
- _createClass(Webflow, [{
137
- key: "get",
138
- value: function get(path) {
139
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
140
- return this.authenticatedFetch("GET", path, null, query);
141
- }
142
- }, {
143
- key: "post",
144
- value: function post(path, data) {
145
- var query = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
146
- return this.authenticatedFetch("POST", path, data, query);
147
- }
148
- }, {
149
- key: "put",
150
- value: function put(path, data) {
151
- var query = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
152
- return this.authenticatedFetch("PUT", path, data, query);
153
- }
154
- }, {
155
- key: "patch",
156
- value: function patch(path, data) {
157
- var query = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
158
- return this.authenticatedFetch("PATCH", path, data, query);
159
- }
160
- }, {
161
- key: "delete",
162
- value: function _delete(path, data) {
163
- var query = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
164
- return this.authenticatedFetch("DELETE", path, data, query);
165
- } // Meta
166
-
167
- }, {
168
- key: "info",
169
- value: function info() {
170
- var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
171
- return this.get("/info", query);
172
- } // Sites
173
-
174
- }, {
175
- key: "sites",
176
- value: function sites() {
177
- var _this2 = this;
178
-
179
- var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
180
- return this.get("/sites", query).then(function (sites) {
181
- return sites.map(function (site) {
182
- return _this2.responseWrapper.site(site);
183
- });
184
- });
185
- }
186
- }, {
187
- key: "site",
188
- value: function site(_ref2) {
189
- var _this3 = this;
190
-
191
- var siteId = _ref2.siteId;
192
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
193
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
194
- return this.get("/sites/".concat(siteId), query).then(function (site) {
195
- return _this3.responseWrapper.site(site);
196
- });
197
- }
198
- }, {
199
- key: "publishSite",
200
- value: function publishSite(_ref3) {
201
- var siteId = _ref3.siteId,
202
- domains = _ref3.domains;
203
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
204
- if (!domains) return Promise.reject((0, _WebflowError.buildRequiredArgError)("domains"));
205
- return this.post("/sites/".concat(siteId, "/publish"), {
206
- domains: domains
207
- });
208
- } // Domains
209
-
210
- }, {
211
- key: "domains",
212
- value: function domains(_ref4) {
213
- var _this4 = this;
214
-
215
- var siteId = _ref4.siteId;
216
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
217
- return this.get("/sites/".concat(siteId, "/domains")).then(function (domains) {
218
- return domains.map(function (domain) {
219
- return _this4.responseWrapper.domain(domain, siteId);
220
- });
221
- });
222
- } // Collections
223
-
224
- }, {
225
- key: "collections",
226
- value: function collections(_ref5) {
227
- var _this5 = this;
228
-
229
- var siteId = _ref5.siteId;
230
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
231
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
232
- return this.get("/sites/".concat(siteId, "/collections"), query).then(function (collections) {
233
- return collections.map(function (collection) {
234
- return _this5.responseWrapper.collection(collection);
235
- });
236
- });
237
- }
238
- }, {
239
- key: "collection",
240
- value: function collection(_ref6) {
241
- var _this6 = this;
242
-
243
- var collectionId = _ref6.collectionId;
244
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
245
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
246
- return this.get("/collections/".concat(collectionId), query).then(function (collection) {
247
- return _this6.responseWrapper.collection(collection);
248
- });
249
- } // Items
250
-
251
- }, {
252
- key: "items",
253
- value: function items(_ref7) {
254
- var _this7 = this;
255
-
256
- var collectionId = _ref7.collectionId;
257
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
258
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
259
- return this.get("/collections/".concat(collectionId, "/items"), query).then(function (res) {
260
- return _objectSpread(_objectSpread({}, res), {}, {
261
- items: res.items.map(function (item) {
262
- return _this7.responseWrapper.item(item, collectionId);
263
- })
264
- });
265
- });
266
- }
267
- }, {
268
- key: "item",
269
- value: function item(_ref8) {
270
- var _this8 = this;
271
-
272
- var collectionId = _ref8.collectionId,
273
- itemId = _ref8.itemId;
274
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
275
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
276
- if (!itemId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("itemId"));
277
- return this.get("/collections/".concat(collectionId, "/items/").concat(itemId), query).then(function (res) {
278
- return _this8.responseWrapper.item(res.items[0], collectionId);
279
- });
280
- }
281
- }, {
282
- key: "createItem",
283
- value: function createItem(_ref9) {
284
- var _this9 = this;
285
-
286
- var collectionId = _ref9.collectionId,
287
- data = _objectWithoutProperties(_ref9, _excluded);
288
-
289
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
290
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
291
- return this.post("/collections/".concat(collectionId, "/items"), data, query).then(function (item) {
292
- return _this9.responseWrapper.item(item, collectionId);
293
- });
294
- }
295
- }, {
296
- key: "updateItem",
297
- value: function updateItem(_ref10) {
298
- var collectionId = _ref10.collectionId,
299
- itemId = _ref10.itemId,
300
- data = _objectWithoutProperties(_ref10, _excluded2);
301
-
302
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
303
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
304
- if (!itemId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("itemId"));
305
- return this.put("/collections/".concat(collectionId, "/items/").concat(itemId), data, query);
306
- }
307
- }, {
308
- key: "removeItem",
309
- value: function removeItem(_ref11) {
310
- var collectionId = _ref11.collectionId,
311
- itemId = _ref11.itemId;
312
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
313
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
314
- if (!itemId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("itemId"));
315
- return this["delete"]("/collections/".concat(collectionId, "/items/").concat(itemId), null, query);
316
- }
317
- }, {
318
- key: "patchItem",
319
- value: function patchItem(_ref12) {
320
- var collectionId = _ref12.collectionId,
321
- itemId = _ref12.itemId,
322
- data = _objectWithoutProperties(_ref12, _excluded3);
323
-
324
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
325
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
326
- if (!itemId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("itemId"));
327
- return this.patch("/collections/".concat(collectionId, "/items/").concat(itemId), data, query);
328
- }
329
- }, {
330
- key: "deleteItems",
331
- value: function deleteItems(_ref13) {
332
- var collectionId = _ref13.collectionId,
333
- itemIds = _ref13.itemIds,
334
- data = _objectWithoutProperties(_ref13, _excluded4);
335
-
336
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
337
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
338
- if (!itemIds) return Promise.reject((0, _WebflowError.buildRequiredArgError)("itemIds"));
339
- return this["delete"]("/collections/".concat(collectionId, "/items"), _objectSpread(_objectSpread({}, data), {}, {
340
- itemIds: itemIds
341
- }), query);
342
- }
343
- }, {
344
- key: "publishItems",
345
- value: function publishItems(_ref14) {
346
- var collectionId = _ref14.collectionId,
347
- itemIds = _ref14.itemIds,
348
- data = _objectWithoutProperties(_ref14, _excluded5);
349
-
350
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
351
- if (!collectionId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("collectionId"));
352
- if (!itemIds) return Promise.reject((0, _WebflowError.buildRequiredArgError)("itemIds"));
353
- return this.put("/collections/".concat(collectionId, "/items/publish"), _objectSpread(_objectSpread({}, data), {}, {
354
- itemIds: itemIds
355
- }), query);
356
- } // Users
357
-
358
- }, {
359
- key: "users",
360
- value: function users(_ref15) {
361
- var _this10 = this;
362
-
363
- var siteId = _ref15.siteId;
364
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
365
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
366
- return this.get("/sites/".concat(siteId, "/users"), query).then(function (res) {
367
- return res.users.map(function (user) {
368
- return _this10.responseWrapper.user(user);
369
- });
370
- });
371
- }
372
- }, {
373
- key: "user",
374
- value: function user(_ref16) {
375
- var _this11 = this;
376
-
377
- var siteId = _ref16.siteId,
378
- userId = _ref16.userId;
379
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
380
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
381
- if (!userId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("userId"));
382
- return this.get("/sites/".concat(siteId, "/users/").concat(userId), query).then(function (user) {
383
- return _this11.responseWrapper.user(user, siteId);
384
- });
385
- }
386
- }, {
387
- key: "updateUser",
388
- value: function updateUser(_ref17) {
389
- var siteId = _ref17.siteId,
390
- userId = _ref17.userId,
391
- data = _objectWithoutProperties(_ref17, _excluded6);
392
-
393
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
394
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
395
- if (!userId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("userId"));
396
- return this.patch("/sites/".concat(siteId, "/users/").concat(userId), data, query);
397
- }
398
- }, {
399
- key: "inviteUser",
400
- value: function inviteUser(_ref18) {
401
- var _this12 = this;
402
-
403
- var siteId = _ref18.siteId,
404
- email = _ref18.email;
405
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
406
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
407
- if (!email) return Promise.reject((0, _WebflowError.buildRequiredArgError)("email"));
408
- return this.post("/sites/".concat(siteId, "/users/invite"), {
409
- email: email
410
- }, query).then(function (user) {
411
- return _this12.responseWrapper.user(user, siteId);
412
- });
413
- }
414
- }, {
415
- key: "removeUser",
416
- value: function removeUser(_ref19) {
417
- var siteId = _ref19.siteId,
418
- userId = _ref19.userId;
419
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
420
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
421
- if (!userId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("userId"));
422
- return this["delete"]("/sites/".concat(siteId, "/users/").concat(userId), null, query);
423
- } // Webhooks
424
-
425
- }, {
426
- key: "webhooks",
427
- value: function webhooks(_ref20) {
428
- var _this13 = this;
429
-
430
- var siteId = _ref20.siteId;
431
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
432
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
433
- return this.get("/sites/".concat(siteId, "/webhooks"), query).then(function (webhooks) {
434
- return webhooks.map(function (webhook) {
435
- return _this13.responseWrapper.webhook(webhook, siteId);
436
- });
437
- });
438
- }
439
- }, {
440
- key: "webhook",
441
- value: function webhook(_ref21) {
442
- var _this14 = this;
443
-
444
- var siteId = _ref21.siteId,
445
- webhookId = _ref21.webhookId;
446
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
447
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
448
- if (!webhookId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("webhookId"));
449
- return this.get("/sites/".concat(siteId, "/webhooks/").concat(webhookId), query).then(function (webhook) {
450
- return _this14.responseWrapper.webhook(webhook, siteId);
451
- });
452
- }
453
- }, {
454
- key: "createWebhook",
455
- value: function createWebhook(_ref22) {
456
- var _this15 = this;
457
-
458
- var siteId = _ref22.siteId,
459
- data = _objectWithoutProperties(_ref22, _excluded7);
460
-
461
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
462
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
463
- return this.post("/sites/".concat(siteId, "/webhooks"), data, query).then(function (webhook) {
464
- return _this15.responseWrapper.webhook(webhook, siteId);
465
- });
466
- }
467
- }, {
468
- key: "removeWebhook",
469
- value: function removeWebhook(_ref23) {
470
- var siteId = _ref23.siteId,
471
- webhookId = _ref23.webhookId;
472
- var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
473
- if (!siteId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("siteId"));
474
- if (!webhookId) return Promise.reject((0, _WebflowError.buildRequiredArgError)("webhookId"));
475
- return this["delete"]("/sites/".concat(siteId, "/webhooks/").concat(webhookId), null, query);
476
- }
477
- }]);
198
+ }
199
+ async user({
200
+ siteId,
201
+ userId
202
+ }, query = {}) {
203
+ if (!siteId) throw new WebflowArgumentError("siteId");
204
+ if (!userId) throw new WebflowArgumentError("userId");
205
+ const uri = `/sites/${siteId}/users/${userId}`;
206
+ const user = await this.get(uri, query);
207
+ return this.responseWrapper.user(user, siteId);
208
+ }
209
+ async updateUser({
210
+ siteId,
211
+ userId,
212
+ ...data
213
+ }, query = {}) {
214
+ if (!siteId) throw new WebflowArgumentError("siteId");
215
+ if (!userId) throw new WebflowArgumentError("userId");
216
+ const uri = `/sites/${siteId}/users/${userId}`;
217
+ const user = await this.patch(uri, data, query);
218
+ return this.responseWrapper.user(user, siteId);
219
+ }
220
+ async inviteUser({
221
+ siteId,
222
+ email
223
+ }, query = {}) {
224
+ if (!siteId) throw new WebflowArgumentError("siteId");
225
+ if (!email) throw new WebflowArgumentError("email");
226
+ const uri = `/sites/${siteId}/users/invite`;
227
+ const user = await this.post(uri, {
228
+ email
229
+ }, query);
230
+ return this.responseWrapper.user(user, siteId);
231
+ }
232
+ removeUser({
233
+ siteId,
234
+ userId
235
+ }, query = {}) {
236
+ if (!siteId) throw new WebflowArgumentError("siteId");
237
+ if (!userId) throw new WebflowArgumentError("userId");
238
+ return this.delete(`/sites/${siteId}/users/${userId}`, null, query);
239
+ }
478
240
 
479
- return Webflow;
480
- }();
241
+ // Webhooks
242
+ async webhooks({
243
+ siteId
244
+ }, query = {}) {
245
+ if (!siteId) throw new WebflowArgumentError("siteId");
246
+ const uri = `/sites/${siteId}/webhooks`;
247
+ const webhooks = await this.client.get(uri, query);
248
+ return webhooks.map(webhook => this.responseWrapper.webhook(webhook, siteId));
249
+ }
250
+ async webhook({
251
+ siteId,
252
+ webhookId
253
+ }, query = {}) {
254
+ if (!siteId) throw new WebflowArgumentError("siteId");
255
+ if (!webhookId) throw new WebflowArgumentError("webhookId");
256
+ const uri = `/sites/${siteId}/webhooks/${webhookId}`;
257
+ const webhook = await this.client.get(uri, query);
258
+ return this.responseWrapper.webhook(webhook, siteId);
259
+ }
260
+ async createWebhook({
261
+ siteId,
262
+ triggerType,
263
+ ...data
264
+ }, query = {}) {
265
+ if (!siteId) throw new WebflowArgumentError("siteId");
266
+ if (!triggerType) throw new WebflowArgumentError("triggerType");
267
+ const uri = `/sites/${siteId}/webhooks`;
268
+ const webhook = {
269
+ ...data,
270
+ triggerType
271
+ };
272
+ const createdWebhook = await this.client.post(uri, webhook, query);
273
+ return this.responseWrapper.webhook(createdWebhook, siteId);
274
+ }
275
+ removeWebhook({
276
+ siteId,
277
+ webhookId
278
+ }, query = {}) {
279
+ if (!siteId) throw new WebflowArgumentError("siteId");
280
+ if (!webhookId) throw new WebflowArgumentError("webhookId");
281
+ return this.delete(`/sites/${siteId}/webhooks/${webhookId}`, null, query);
282
+ }
481
283
 
482
- exports["default"] = Webflow;
284
+ // OAuth
285
+ authorizeUrl({
286
+ client_id,
287
+ redirect_uri,
288
+ state,
289
+ scope,
290
+ response_type = "code"
291
+ }) {
292
+ if (!client_id) throw new WebflowArgumentError("clientId");
293
+ const query = new URLSearchParams({
294
+ response_type,
295
+ client_id
296
+ });
297
+ if (redirect_uri) query.set("redirect_uri", redirect_uri);
298
+ if (state) query.set("state", state);
299
+ if (scope) query.set("scope", scope);
300
+ return `https://${this.host}/oauth/authorize?${query}`;
301
+ }
302
+ accessToken({
303
+ client_id,
304
+ client_secret,
305
+ code,
306
+ redirect_uri,
307
+ grant_type = "authorization_code"
308
+ }) {
309
+ if (!client_id) throw new WebflowArgumentError("client_id");
310
+ if (!client_secret) throw new WebflowArgumentError("client_secret");
311
+ if (!code) throw new WebflowArgumentError("code");
312
+ return this.post("/oauth/access_token", {
313
+ client_id,
314
+ client_secret,
315
+ code,
316
+ redirect_uri,
317
+ grant_type
318
+ });
319
+ }
320
+ revokeToken({
321
+ client_id,
322
+ client_secret,
323
+ access_token
324
+ }) {
325
+ if (!client_id) throw new WebflowArgumentError("client_id");
326
+ if (!client_secret) throw new WebflowArgumentError("client_secret");
327
+ if (!access_token) throw new WebflowArgumentError("access_token");
328
+ const uri = "/oauth/revoke_authorization";
329
+ return this.post(uri, {
330
+ client_id,
331
+ client_secret,
332
+ access_token
333
+ });
334
+ }
335
+ }
336
+ exports.Webflow = Webflow;
337
+ var _default = Webflow;
338
+ exports.default = _default;