webflow-api 1.0.2 → 1.1.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.
Files changed (74) hide show
  1. package/README.md +4 -5
  2. package/dist/api/collection.d.ts +51 -0
  3. package/dist/api/collection.js +35 -0
  4. package/dist/api/index.d.ts +7 -0
  5. package/dist/api/index.js +33 -0
  6. package/dist/api/item.d.ts +143 -0
  7. package/dist/api/item.js +131 -0
  8. package/dist/api/membership.d.ts +91 -0
  9. package/dist/api/membership.js +80 -0
  10. package/dist/api/meta.d.ts +51 -0
  11. package/dist/api/meta.js +24 -0
  12. package/dist/api/oauth.d.ts +67 -0
  13. package/dist/api/oauth.js +65 -0
  14. package/dist/api/site.d.ts +65 -0
  15. package/dist/api/site.js +59 -0
  16. package/dist/api/webhook.d.ts +80 -0
  17. package/dist/api/webhook.js +67 -0
  18. package/dist/core/client.d.ts +40 -0
  19. package/dist/core/client.js +49 -0
  20. package/dist/core/error.d.ts +19 -0
  21. package/dist/core/error.js +23 -0
  22. package/dist/core/index.d.ts +3 -0
  23. package/dist/core/index.js +19 -0
  24. package/dist/core/options.d.ts +8 -0
  25. package/dist/core/options.js +5 -0
  26. package/dist/index.d.ts +2 -0
  27. package/dist/index.js +2 -9
  28. package/dist/webflow.d.ts +366 -0
  29. package/dist/webflow.js +388 -0
  30. package/dist/wrapper/collection.d.ts +85 -0
  31. package/dist/wrapper/collection.js +94 -0
  32. package/dist/wrapper/index.d.ts +6 -0
  33. package/dist/wrapper/index.js +22 -0
  34. package/dist/wrapper/item.d.ts +140 -0
  35. package/dist/wrapper/item.js +153 -0
  36. package/dist/wrapper/membership.d.ts +91 -0
  37. package/dist/wrapper/membership.js +106 -0
  38. package/dist/wrapper/response.d.ts +16 -0
  39. package/dist/wrapper/response.js +17 -0
  40. package/dist/wrapper/site.d.ts +119 -0
  41. package/dist/wrapper/site.js +136 -0
  42. package/dist/wrapper/webhook.d.ts +78 -0
  43. package/dist/wrapper/webhook.js +82 -0
  44. package/package.json +16 -17
  45. package/src/api/collection.ts +87 -0
  46. package/src/api/index.ts +7 -0
  47. package/src/api/item.ts +231 -0
  48. package/src/api/membership.ts +125 -0
  49. package/src/api/meta.ts +61 -0
  50. package/src/api/oauth.ts +119 -0
  51. package/src/api/site.ts +86 -0
  52. package/src/api/webhook.ts +125 -0
  53. package/src/core/client.ts +76 -0
  54. package/src/core/error.ts +32 -0
  55. package/src/core/index.ts +3 -0
  56. package/src/core/options.ts +9 -0
  57. package/src/index.ts +3 -0
  58. package/src/webflow.ts +487 -0
  59. package/src/wrapper/collection.ts +115 -0
  60. package/src/wrapper/index.ts +6 -0
  61. package/src/wrapper/item.ts +218 -0
  62. package/src/wrapper/membership.ts +138 -0
  63. package/src/wrapper/response.ts +25 -0
  64. package/src/wrapper/site.ts +164 -0
  65. package/src/wrapper/webhook.ts +116 -0
  66. package/yarn.lock +388 -1473
  67. package/dist/ResponseWrapper.js +0 -135
  68. package/dist/Webflow.js +0 -344
  69. package/dist/WebflowClient.js +0 -115
  70. package/index.d.ts +0 -430
  71. package/src/ResponseWrapper.js +0 -103
  72. package/src/Webflow.js +0 -301
  73. package/src/WebflowClient.js +0 -98
  74. package/src/index.js +0 -3
@@ -1,135 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- class ResponseWrapper {
8
- constructor(api) {
9
- this.api = api;
10
- }
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;
package/dist/Webflow.js DELETED
@@ -1,344 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
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");
15
- var _ResponseWrapper = _interopRequireDefault(require("./ResponseWrapper"));
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
- set token(value) {
29
- this.client.token = value;
30
- }
31
- get token() {
32
- return this.client.token;
33
- }
34
- get(path, query = {}) {
35
- return this.client.get(path, query);
36
- }
37
- post(path, data, query = {}) {
38
- return this.client.post(path, data, query);
39
- }
40
- put(path, data, query = {}) {
41
- return this.client.put(path, data, query);
42
- }
43
- patch(path, data, query = {}) {
44
- return this.client.patch(path, data, query);
45
- }
46
- delete(path, data, query = {}) {
47
- return this.client.delete(path, data, query);
48
- }
49
-
50
- // Meta
51
- info() {
52
- return this.get("/info");
53
- }
54
- authenticatedUser() {
55
- return this.get("/user");
56
- }
57
-
58
- // Sites
59
- async sites(query = {}) {
60
- const sites = await this.get("/sites", query);
61
- return sites.map(site => this.responseWrapper.site(site));
62
- }
63
- async site({
64
- siteId
65
- }, query = {}) {
66
- if (!siteId) throw new WebflowArgumentError("siteId");
67
- const site = await this.get(`/sites/${siteId}`, query);
68
- return this.responseWrapper.site(site);
69
- }
70
- publishSite({
71
- siteId,
72
- domains
73
- }) {
74
- if (!siteId) throw new WebflowArgumentError("siteId");
75
- if (!domains) throw new WebflowArgumentError("domains");
76
- return this.post(`/sites/${siteId}/publish`, {
77
- domains
78
- });
79
- }
80
- async domains({
81
- siteId
82
- }) {
83
- if (!siteId) throw new WebflowArgumentError("siteId");
84
- const domains = await this.client.get(`/sites/${siteId}/domains`);
85
- return domains.map(domain => this.responseWrapper.domain(domain, siteId));
86
- }
87
-
88
- // Collections
89
- async collections({
90
- siteId
91
- }, query = {}) {
92
- if (!siteId) throw new WebflowArgumentError("siteId");
93
- const collections = await this.get(`/sites/${siteId}/collections`, query);
94
- return collections.map(collection => this.responseWrapper.collection(collection));
95
- }
96
- async collection({
97
- collectionId
98
- }, query = {}) {
99
- if (!collectionId) throw new WebflowArgumentError("collectionId");
100
- const uri = `/collections/${collectionId}`;
101
- const collection = await this.client.get(uri, query);
102
- return this.responseWrapper.collection(collection);
103
- }
104
-
105
- // Items
106
- async items({
107
- collectionId
108
- }, query = {}) {
109
- if (!collectionId) throw new WebflowArgumentError("collectionId");
110
- const uri = `/collections/${collectionId}/items`;
111
- const res = await this.client.get(uri, query);
112
- return {
113
- ...res,
114
- items: res.items.map(item => this.responseWrapper.item(item, collectionId))
115
- };
116
- }
117
- async item({
118
- collectionId,
119
- itemId
120
- }, query = {}) {
121
- if (!collectionId) throw new WebflowArgumentError("collectionId");
122
- if (!itemId) throw new WebflowArgumentError("itemId");
123
- const uri = `/collections/${collectionId}/items/${itemId}`;
124
- const {
125
- items
126
- } = await this.client.get(uri, query);
127
- return this.responseWrapper.item(items[0], collectionId);
128
- }
129
- async createItem({
130
- collectionId,
131
- ...data
132
- }, query = {}) {
133
- if (!collectionId) throw new WebflowArgumentError("collectionId");
134
- const uri = `/collections/${collectionId}/items`;
135
- const item = await this.post(uri, data, query);
136
- return this.responseWrapper.item(item, collectionId);
137
- }
138
- updateItem({
139
- collectionId,
140
- itemId,
141
- ...data
142
- }, query = {}) {
143
- if (!collectionId) throw new WebflowArgumentError("collectionId");
144
- if (!itemId) throw new WebflowArgumentError("itemId");
145
- const uri = `/collections/${collectionId}/items/${itemId}`;
146
- return this.put(uri, data, query);
147
- }
148
- removeItem({
149
- collectionId,
150
- itemId
151
- }, query = {}) {
152
- if (!collectionId) throw new WebflowArgumentError("collectionId");
153
- if (!itemId) throw new WebflowArgumentError("itemId");
154
- const uri = `/collections/${collectionId}/items/${itemId}`;
155
- return this.delete(uri, null, query);
156
- }
157
- patchItem({
158
- collectionId,
159
- itemId,
160
- ...data
161
- }, query = {}) {
162
- if (!collectionId) throw new WebflowArgumentError("collectionId");
163
- if (!itemId) throw new WebflowArgumentError("itemId");
164
- const uri = `/collections/${collectionId}/items/${itemId}`;
165
- return this.patch(uri, data, query);
166
- }
167
- deleteItems({
168
- collectionId,
169
- itemIds,
170
- ...data
171
- }, query = {}) {
172
- if (!collectionId) throw new WebflowArgumentError("collectionId");
173
- if (!itemIds) throw new WebflowArgumentError("itemIds");
174
- const uri = `/collections/${collectionId}/items`;
175
- return this.delete(uri, {
176
- ...data,
177
- itemIds
178
- }, query);
179
- }
180
- publishItems({
181
- collectionId,
182
- itemIds,
183
- ...data
184
- }, query = {}) {
185
- if (!collectionId) throw new WebflowArgumentError("collectionId");
186
- if (!itemIds) throw new WebflowArgumentError("itemIds");
187
- const uri = `/collections/${collectionId}/items/publish`;
188
- return this.put(uri, {
189
- ...data,
190
- itemIds
191
- }, query);
192
- }
193
-
194
- // Users
195
- async users({
196
- siteId
197
- }, query = {}) {
198
- if (!siteId) throw new WebflowArgumentError("siteId");
199
- const res = await this.get(`/sites/${siteId}/users`, query);
200
- return {
201
- ...res,
202
- users: res.users.map(user => this.responseWrapper.user(user, siteId))
203
- };
204
- }
205
- async user({
206
- siteId,
207
- userId
208
- }, query = {}) {
209
- if (!siteId) throw new WebflowArgumentError("siteId");
210
- if (!userId) throw new WebflowArgumentError("userId");
211
- const uri = `/sites/${siteId}/users/${userId}`;
212
- const user = await this.get(uri, query);
213
- return this.responseWrapper.user(user, siteId);
214
- }
215
- async updateUser({
216
- siteId,
217
- userId,
218
- ...data
219
- }, query = {}) {
220
- if (!siteId) throw new WebflowArgumentError("siteId");
221
- if (!userId) throw new WebflowArgumentError("userId");
222
- const uri = `/sites/${siteId}/users/${userId}`;
223
- const user = await this.patch(uri, data, query);
224
- return this.responseWrapper.user(user, siteId);
225
- }
226
- async inviteUser({
227
- siteId,
228
- email
229
- }, query = {}) {
230
- if (!siteId) throw new WebflowArgumentError("siteId");
231
- if (!email) throw new WebflowArgumentError("email");
232
- const uri = `/sites/${siteId}/users/invite`;
233
- const user = await this.post(uri, {
234
- email
235
- }, query);
236
- return this.responseWrapper.user(user, siteId);
237
- }
238
- removeUser({
239
- siteId,
240
- userId
241
- }, query = {}) {
242
- if (!siteId) throw new WebflowArgumentError("siteId");
243
- if (!userId) throw new WebflowArgumentError("userId");
244
- return this.delete(`/sites/${siteId}/users/${userId}`, null, query);
245
- }
246
-
247
- // Webhooks
248
- async webhooks({
249
- siteId
250
- }, query = {}) {
251
- if (!siteId) throw new WebflowArgumentError("siteId");
252
- const uri = `/sites/${siteId}/webhooks`;
253
- const webhooks = await this.client.get(uri, query);
254
- return webhooks.map(webhook => this.responseWrapper.webhook(webhook, siteId));
255
- }
256
- async webhook({
257
- siteId,
258
- webhookId
259
- }, query = {}) {
260
- if (!siteId) throw new WebflowArgumentError("siteId");
261
- if (!webhookId) throw new WebflowArgumentError("webhookId");
262
- const uri = `/sites/${siteId}/webhooks/${webhookId}`;
263
- const webhook = await this.client.get(uri, query);
264
- return this.responseWrapper.webhook(webhook, siteId);
265
- }
266
- async createWebhook({
267
- siteId,
268
- triggerType,
269
- ...data
270
- }, query = {}) {
271
- if (!siteId) throw new WebflowArgumentError("siteId");
272
- if (!triggerType) throw new WebflowArgumentError("triggerType");
273
- const uri = `/sites/${siteId}/webhooks`;
274
- const webhook = {
275
- ...data,
276
- triggerType
277
- };
278
- const createdWebhook = await this.client.post(uri, webhook, query);
279
- return this.responseWrapper.webhook(createdWebhook, siteId);
280
- }
281
- removeWebhook({
282
- siteId,
283
- webhookId
284
- }, query = {}) {
285
- if (!siteId) throw new WebflowArgumentError("siteId");
286
- if (!webhookId) throw new WebflowArgumentError("webhookId");
287
- return this.delete(`/sites/${siteId}/webhooks/${webhookId}`, null, query);
288
- }
289
-
290
- // OAuth
291
- authorizeUrl({
292
- client_id,
293
- redirect_uri,
294
- state,
295
- scope,
296
- response_type = "code"
297
- }) {
298
- if (!client_id) throw new WebflowArgumentError("clientId");
299
- const query = new URLSearchParams({
300
- response_type,
301
- client_id
302
- });
303
- if (redirect_uri) query.set("redirect_uri", redirect_uri);
304
- if (state) query.set("state", state);
305
- if (scope) query.set("scope", scope);
306
- return `https://${this.host}/oauth/authorize?${query}`;
307
- }
308
- accessToken({
309
- client_id,
310
- client_secret,
311
- code,
312
- redirect_uri,
313
- grant_type = "authorization_code"
314
- }) {
315
- if (!client_id) throw new WebflowArgumentError("client_id");
316
- if (!client_secret) throw new WebflowArgumentError("client_secret");
317
- if (!code) throw new WebflowArgumentError("code");
318
- return this.post("/oauth/access_token", {
319
- client_id,
320
- client_secret,
321
- code,
322
- redirect_uri,
323
- grant_type
324
- });
325
- }
326
- revokeToken({
327
- client_id,
328
- client_secret,
329
- access_token
330
- }) {
331
- if (!client_id) throw new WebflowArgumentError("client_id");
332
- if (!client_secret) throw new WebflowArgumentError("client_secret");
333
- if (!access_token) throw new WebflowArgumentError("access_token");
334
- const uri = "/oauth/revoke_authorization";
335
- return this.post(uri, {
336
- client_id,
337
- client_secret,
338
- access_token
339
- });
340
- }
341
- }
342
- exports.Webflow = Webflow;
343
- var _default = Webflow;
344
- exports.default = _default;
@@ -1,115 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.WebflowRequestError = exports.WebflowClient = void 0;
7
- var _isomorphicFetch = _interopRequireDefault(require("isomorphic-fetch"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- const DEFAULT_HOST = "webflow.com";
10
- const USER_AGENT = "Webflow Javascript SDK / 1.0";
11
- class WebflowRequestError extends Error {
12
- constructor(error) {
13
- super(error.err ? error.err : "Unknown error occured");
14
- Object.assign(this, error);
15
- }
16
- }
17
- exports.WebflowRequestError = WebflowRequestError;
18
- class WebflowClient {
19
- constructor({
20
- host,
21
- token,
22
- version,
23
- headers,
24
- mode
25
- } = {}) {
26
- this.host = host || DEFAULT_HOST;
27
- this.headers = headers || {};
28
- this.version = version;
29
- this.token = token;
30
- this.mode = mode;
31
- }
32
- getUri(path, query = {}) {
33
- const hasQuery = Object.keys(query).length > 0;
34
- const queryString = hasQuery ? `?${new URLSearchParams(query)}` : "";
35
- return `https://api.${this.host}${path}${queryString}`;
36
- }
37
- getHeaders() {
38
- const {
39
- version,
40
- token
41
- } = this;
42
- const headers = {
43
- "Content-Type": "application/json",
44
- Accept: "application/json",
45
- "User-Agent": USER_AGENT
46
- };
47
-
48
- // set authorization header if token is set
49
- if (token) headers.Authorization = `Bearer ${token}`;
50
-
51
- // set the API version
52
- if (version) headers["accept-version"] = version;
53
-
54
- // merge headers with user headers;
55
- return {
56
- ...headers,
57
- ...this.headers
58
- };
59
- }
60
- async parseBody(res) {
61
- const body = await res.json();
62
-
63
- // append ratelimit meta data to response
64
- if (res.headers) {
65
- const limit = parseInt(res.headers.get("x-ratelimit-limit"), 10);
66
- const remaining = parseInt(res.headers.get("x-ratelimit-remaining"), 10);
67
- body._meta = {
68
- rateLimit: {
69
- limit,
70
- remaining
71
- }
72
- };
73
- }
74
-
75
- // webflow error
76
- if (body.err) throw new WebflowRequestError(body);
77
- return body;
78
- }
79
- fetch(method, path, data, query) {
80
- // build uri
81
- const uri = this.getUri(path, query);
82
-
83
- // build request options
84
- const headers = this.getHeaders();
85
- const opts = {
86
- method,
87
- headers,
88
- mode: this.mode
89
- };
90
- if (data) opts.body = JSON.stringify(data);
91
-
92
- // call fetch and wrap response
93
- return (0, _isomorphicFetch.default)(uri, opts).then(this.parseBody.bind(this));
94
- }
95
-
96
- // Generic HTTP request handlers
97
- get(path, query = {}) {
98
- return this.fetch("GET", path, null, query);
99
- }
100
- post(path, data, query = {}) {
101
- return this.fetch("POST", path, data, query);
102
- }
103
- put(path, data, query = {}) {
104
- return this.fetch("PUT", path, data, query);
105
- }
106
- patch(path, data, query = {}) {
107
- return this.fetch("PATCH", path, data, query);
108
- }
109
- delete(path, data, query = {}) {
110
- return this.fetch("DELETE", path, data, query);
111
- }
112
- }
113
- exports.WebflowClient = WebflowClient;
114
- var _default = WebflowClient;
115
- exports.default = _default;