1
- import { WebflowClient } from "webflow-api" ;
1
+ // Webflow version support here: https://www.npmjs.com/package/webflow-api?activeTab=versions
2
+ // @note : this is pinned to Webflow 1.3.1
3
+ // because the upgrade to version 2 requires a new app
4
+ import Webflow from "[email protected] " ;
2
5
import constants from "./common/constants.mjs" ;
3
6
4
7
export default {
5
8
type : "app" ,
6
9
app : "webflow" ,
7
10
propDefinitions : {
8
11
domains : {
9
- label : "Custom Domains " ,
10
- description : "Select one or more custom domains to publish ." ,
12
+ label : "Domain " ,
13
+ description : "The list of domains." ,
11
14
type : "string[]" ,
12
15
async options ( { siteId } ) {
13
- const domains = await this . listDomains ( siteId ) ;
14
- return domains . map ( ( id , url ) => ( {
15
- label : url ,
16
- id,
17
- } ) ) ;
16
+ const domains = await this . getDomains ( siteId ) ;
17
+
18
+ return domains . map ( ( domain ) => domain . name ) ;
18
19
} ,
19
20
} ,
20
21
sites : {
21
22
label : "Site" ,
22
- description : "Select a site or provide a custom site ID. " ,
23
+ description : "The list of sites " ,
23
24
type : "string" ,
24
25
async options ( ) {
25
- const sites = await this . listSites ( ) ;
26
+ const sites = await this . getSites ( ) ;
26
27
27
28
return sites . map ( ( site ) => ( {
28
- label : site . displayName || site . shortName ,
29
- value : site . id ,
29
+ label : site . name ,
30
+ value : site . _id ,
30
31
} ) ) ;
31
32
} ,
32
33
} ,
33
34
collections : {
34
35
label : "Collection" ,
35
- description : "Select a collection or provide a custom collection ID. " ,
36
+ description : "The list of collection of a site " ,
36
37
type : "string" ,
37
38
async options ( { siteId } ) {
38
- const collections = await this . listCollections ( siteId ) ;
39
+ const collections = await this . getCollections ( siteId ) ;
39
40
40
41
return collections . map ( ( collection ) => ( {
41
- label : collection . displayName || collection . slug ,
42
- value : collection . id ,
42
+ label : collection . name ,
43
+ value : collection . _id ,
43
44
} ) ) ;
44
45
} ,
45
46
} ,
46
47
items : {
47
48
label : "Item" ,
48
- description : "Select an item or provide a custom item ID. " ,
49
+ description : "The list of items of a collection " ,
49
50
type : "string" ,
50
51
async options ( {
51
52
collectionId, page,
52
53
} ) {
53
- const items = await this . listCollectionItems ( page , collectionId ) ;
54
+ const items = await this . getItems ( page , collectionId ) ;
54
55
55
56
return items . map ( ( item ) => ( {
56
- label : item . fieldData ?. name || item . fieldData ?. slug ,
57
- value : item . id ,
57
+ label : item . name ,
58
+ value : item . _id ,
58
59
} ) ) ;
59
60
} ,
60
61
} ,
61
62
orders : {
62
63
label : "Order" ,
63
- description : "Select an order, or provide a custom order ID. " ,
64
+ description : "The list of orders of a site " ,
64
65
type : "string" ,
65
66
async options ( {
66
67
siteId, page,
67
68
} ) {
68
- const items = await this . listOrders ( {
69
+ const items = await this . getOrders ( {
69
70
page,
70
71
siteId,
71
72
} ) ;
@@ -75,96 +76,179 @@ export default {
75
76
} ,
76
77
} ,
77
78
methods : {
79
+ /**
80
+ * Get the auth access token;
81
+ *
82
+ * @returns {string } The base auth access token.
83
+ */
78
84
_authToken ( ) {
79
85
return this . $auth . oauth_access_token ;
80
86
} ,
81
- webflowClient ( ) {
82
- return new WebflowClient ( {
83
- accessToken : this . _authToken ( ) ,
87
+ /**
88
+ * Create a Webflow API client;
89
+ *
90
+ * @returns {params } The Webflow API client.
91
+ */
92
+ _createApiClient ( ) {
93
+ return new Webflow ( {
94
+ token : this . _authToken ( ) ,
84
95
} ) ;
85
96
} ,
86
- async createWebhook ( siteId , data ) {
87
- return this . webflowClient ( ) . webhooks . create ( siteId , data ) ;
88
- } ,
89
- async removeWebhook ( webhookId ) {
90
- return this . webflowClient ( ) . webhooks . delete ( webhookId ) ;
97
+ /**
98
+ * Create a Webflow webhook;
99
+ *
100
+ * @param {siteId } ID of the site to be monitored.
101
+ * @param {url } URL to webhook return.
102
+ * @param {triggerType } Type of event that will be triggered.
103
+ * @param {filter } Filters to be applied in webhook.
104
+ *
105
+ * @returns {params } The Webflow webhook.
106
+ */
107
+ async createWebhook ( siteId , url , triggerType , filter = { } ) {
108
+ const apiClient = this . _createApiClient ( ) ;
109
+
110
+ return apiClient . createWebhook ( {
111
+ siteId,
112
+ triggerType,
113
+ url,
114
+ filter,
115
+ } ) ;
91
116
} ,
92
- async getOrder ( siteId , orderId ) {
93
- return this . webflowClient ( ) . orders . get ( siteId , orderId ) ;
117
+ /**
118
+ * Remove a Webflow webhook;
119
+ *
120
+ * @param {siteId } ID of the site.
121
+ * @param {webhookId } ID of the webhook.
122
+ */
123
+ async removeWebhook ( siteId , webhookId ) {
124
+ const apiClient = this . _createApiClient ( ) ;
125
+ return apiClient . removeWebhook ( {
126
+ siteId,
127
+ webhookId,
128
+ } ) ;
94
129
} ,
95
- async listOrders ( {
96
- page : offset = 0 , siteId, status,
130
+ /**
131
+ * Get an order;
132
+ *
133
+ * @param {options } Options to filter the order.
134
+ *
135
+ * @returns {params } An order.
136
+ */
137
+ async getOrder ( {
138
+ siteId, orderId,
139
+ } ) {
140
+ const apiClient = this . _createApiClient ( ) ;
141
+
142
+ return apiClient . get ( `/sites/${ siteId } /order/${ orderId } ` ) ;
143
+ } ,
144
+ /**
145
+ * Get a list of orders;
146
+ *
147
+ * @param {options } Options to filter the orders.
148
+ *
149
+ * @returns {params } A list of orders.
150
+ */
151
+ async getOrders ( {
152
+ page, siteId, status,
97
153
} ) {
98
- const response = await this . webflowClient ( ) . orders . list ( siteId , {
99
- offset,
100
- status,
154
+ const apiClient = this . _createApiClient ( ) ;
155
+
156
+ return apiClient . get ( `/sites/${ siteId } /orders` , {
157
+ status : status ,
158
+ offset : page ?? 0 ,
159
+ limit : constants . LIMIT ,
101
160
} ) ;
102
- return response ?. orders ;
103
- } ,
104
- async listDomains ( siteId ) {
105
- const response = await this . webflowClient ( ) . sites . getCustomDomain ( siteId ) ;
106
- return response ?. customDomains ;
107
161
} ,
108
- getSite ( siteId ) {
109
- return this . webflowClient ( ) . sites . get ( siteId ) ;
162
+ /**
163
+ * Get a list of domains;
164
+ *
165
+ * @param {options } Options to filter the domains.
166
+ *
167
+ * @returns {params } A list of domains.
168
+ */
169
+ async getDomains ( siteId ) {
170
+ const webflow = this . _createApiClient ( ) ;
171
+
172
+ return await webflow . domains ( {
173
+ siteId,
174
+ } ) ;
110
175
} ,
111
- async listSites ( ) {
112
- const response = await this . webflowClient ( ) . sites . list ( ) ;
113
- return response ?. sites ;
176
+ /**
177
+ * Get a site;
178
+ *
179
+ * @param {options } Options to filter the site.
180
+ *
181
+ * @returns {params } A site.
182
+ */
183
+ async getSite ( siteId ) {
184
+ const webflow = this . _createApiClient ( ) ;
185
+
186
+ return await webflow . site ( {
187
+ siteId,
188
+ } ) ;
114
189
} ,
115
- getCollection ( collectionId ) {
116
- return this . webflowClient ( ) . collections . get ( collectionId ) ;
190
+ /**
191
+ * Get a list of sites;
192
+ *
193
+ * @param {options } Options to filter the sites.
194
+ *
195
+ * @returns {params } A list of sites.
196
+ */
197
+ async getSites ( ) {
198
+ const webflow = this . _createApiClient ( ) ;
199
+
200
+ return await webflow . sites ( ) ;
201
+ } ,
202
+ /**
203
+ * Get a collection;
204
+ *
205
+ * @param {options } Options to filter the collection.
206
+ *
207
+ * @returns {params } A collection.
208
+ */
209
+ async getCollection ( collectionId ) {
210
+ const webflow = this . _createApiClient ( ) ;
211
+
212
+ return await webflow . collection ( {
213
+ collectionId,
214
+ } ) ;
117
215
} ,
118
- async listCollections ( siteId ) {
216
+ /**
217
+ * Get a list of collections;
218
+ *
219
+ * @param {options } Options to filter the collections.
220
+ *
221
+ * @returns {params } A list of collections.
222
+ */
223
+ async getCollections ( siteId ) {
224
+ const webflow = this . _createApiClient ( ) ;
225
+
119
226
if ( ! siteId ) return [ ] ;
120
227
121
- const response = await this . webflowClient ( ) . collections . list ( siteId ) ;
122
- return response ?. collections ;
228
+ return await webflow . collections ( {
229
+ siteId : siteId ,
230
+ } ) ;
123
231
} ,
124
- async listCollectionItems ( page = 0 , collectionId ) {
232
+ /**
233
+ * Get a list of items;
234
+ *
235
+ * @param {options } Options to filter the items.
236
+ *
237
+ * @returns {params } A list of items.
238
+ */
239
+ async getItems ( page = 0 , collectionId ) {
240
+ const webflow = this . _createApiClient ( ) ;
241
+
125
242
if ( ! collectionId ) return [ ] ;
126
243
127
- const response = await this . webflowClient ( ) . collections . items . listItems ( collectionId , {
244
+ const response = await webflow . items ( {
245
+ collectionId,
246
+ } , {
128
247
limit : constants . LIMIT ,
129
248
offset : page ,
130
249
} ) ;
131
250
132
- return response ?. items ;
133
- } ,
134
- getCollectionItem ( collectionId , itemId ) {
135
- return this . webflowClient ( ) . collections . items . getItem ( collectionId , itemId ) ;
136
- } ,
137
- deleteCollectionItem ( collectionId , itemId ) {
138
- return this . webflowClient ( ) . collections . items . deleteItem ( collectionId , itemId ) ;
139
- } ,
140
- createCollectionItem ( collectionId , data ) {
141
- return this . webflowClient ( ) . collections . items . createItem ( collectionId , data ) ;
142
- } ,
143
- updateCollectionItem ( collectionId , itemId , data ) {
144
- return this . webflowClient ( ) . collections . items . updateItem ( collectionId , itemId , data ) ;
145
- } ,
146
- getCollectionItemInventory ( collectionId , itemId ) {
147
- return this . webflowClient ( ) . inventory . list ( collectionId , itemId ) ;
148
- } ,
149
- updateCollectionItemInventory ( collectionId , itemId , data ) {
150
- return this . webflowClient ( ) . inventory . update ( collectionId , itemId , data ) ;
151
- } ,
152
- publishSite ( siteId , customDomains ) {
153
- return this . webflowClient ( ) . sites . publish ( siteId , {
154
- customDomains,
155
- } ) ;
156
- } ,
157
- fulfillOrder ( siteId , orderId , data ) {
158
- return this . webflowClient ( ) . orders . updateFulfill ( siteId , orderId , data ) ;
159
- } ,
160
- unfulfillOrder ( siteId , orderId ) {
161
- return this . webflowClient ( ) . orders . updateUnfulfill ( siteId , orderId ) ;
162
- } ,
163
- refundOrder ( siteId , orderId ) {
164
- return this . webflowClient ( ) . orders . refund ( siteId , orderId ) ;
165
- } ,
166
- updateOrder ( siteId , orderId , data ) {
167
- return this . webflowClient ( ) . orders . update ( siteId , orderId , data ) ;
251
+ return response ;
168
252
} ,
169
253
} ,
170
254
} ;
0 commit comments