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