1- import fetch from ' isomorphic-fetch' ;
2- import qs from 'qs' ;
1+ import fetch from " isomorphic-fetch" ;
2+ import qs from "qs" ;
33
4- import { isObjectEmpty } from ' ./utils' ;
5- import ResponseWrapper from ' ./ResponseWrapper' ;
6- import WebflowError , { buildRequiredArgError } from ' ./WebflowError' ;
4+ import { isObjectEmpty } from " ./utils" ;
5+ import ResponseWrapper from " ./ResponseWrapper" ;
6+ import WebflowError , { buildRequiredArgError } from " ./WebflowError" ;
77
8- const DEFAULT_ENDPOINT = ' https://api.webflow.com' ;
8+ const DEFAULT_ENDPOINT = " https://api.webflow.com" ;
99
1010const buildMeta = ( res ) => {
11- if ( ! res || ! res . headers ) { return { } ; }
11+ if ( ! res || ! res . headers ) {
12+ return { } ;
13+ }
1214
1315 return {
1416 rateLimit : {
15- limit : parseInt ( res . headers . get ( ' x-ratelimit-limit' ) , 10 ) ,
16- remaining : parseInt ( res . headers . get ( ' x-ratelimit-remaining' ) , 10 ) ,
17+ limit : parseInt ( res . headers . get ( " x-ratelimit-limit" ) , 10 ) ,
18+ remaining : parseInt ( res . headers . get ( " x-ratelimit-remaining" ) , 10 ) ,
1719 } ,
1820 } ;
1921} ;
2022
21- const responseHandler = res =>
22- res . json ( )
23- . catch ( err =>
23+ const responseHandler = ( res ) =>
24+ res
25+ . json ( )
26+ . catch ( ( err ) =>
2427 // Catch unexpected server errors where json isn't sent and rewrite
2528 // with proper class (WebflowError)
26- Promise . reject ( new WebflowError ( err ) ) )
29+ Promise . reject ( new WebflowError ( err ) )
30+ )
2731 . then ( ( body ) => {
2832 if ( res . status >= 400 ) {
2933 const errOpts = {
@@ -36,7 +40,7 @@ const responseHandler = res =>
3640 errOpts . problems = body . problems ;
3741 }
3842
39- const errMsg = ( body && body . err ) ? body . err : ' Unknown error occured' ;
43+ const errMsg = body && body . err ? body . err : " Unknown error occured" ;
4044 const err = new WebflowError ( errMsg ) ;
4145
4246 return Promise . reject ( Object . assign ( err , errOpts ) ) ;
@@ -48,169 +52,160 @@ const responseHandler = res =>
4852 } ) ;
4953
5054export default class Webflow {
51- constructor ( {
52- endpoint = DEFAULT_ENDPOINT ,
53- token,
54- version = '1.0.0' ,
55- } = { } ) {
56- if ( ! token ) throw buildRequiredArgError ( 'token' ) ;
55+ constructor ( { endpoint = DEFAULT_ENDPOINT , token, version = "1.0.0" } = { } ) {
56+ if ( ! token ) throw buildRequiredArgError ( "token" ) ;
5757
5858 this . responseWrapper = new ResponseWrapper ( this ) ;
5959
6060 this . endpoint = endpoint ;
6161 this . token = token ;
6262
6363 this . headers = {
64- Accept : ' application/json' ,
64+ Accept : " application/json" ,
6565 Authorization : `Bearer ${ token } ` ,
66- ' accept-version' : version ,
67- ' Content-Type' : ' application/json' ,
66+ " accept-version" : version ,
67+ " Content-Type" : " application/json" ,
6868 } ;
6969
7070 this . authenticatedFetch = ( method , path , data , query ) => {
71- const queryString = query && ! isObjectEmpty ( query )
72- ? `?${ qs . stringify ( query ) } `
73- : '' ;
71+ const queryString = query && ! isObjectEmpty ( query ) ? `?${ qs . stringify ( query ) } ` : "" ;
7472
7573 const uri = `${ this . endpoint } ${ path } ${ queryString } ` ;
7674 const opts = {
7775 method,
7876 headers : this . headers ,
79- mode : ' cors' ,
77+ mode : " cors" ,
8078 } ;
8179
8280 if ( data ) {
8381 opts . body = JSON . stringify ( data ) ;
8482 }
8583
86- return fetch ( uri , opts )
87- . then ( responseHandler ) ;
84+ return fetch ( uri , opts ) . then ( responseHandler ) ;
8885 } ;
8986 }
9087
9188 // Generic HTTP request handlers
9289
9390 get ( path , query = { } ) {
94- return this . authenticatedFetch ( ' GET' , path , null , query ) ;
91+ return this . authenticatedFetch ( " GET" , path , null , query ) ;
9592 }
9693
9794 post ( path , data , query = { } ) {
98- return this . authenticatedFetch ( ' POST' , path , data , query ) ;
95+ return this . authenticatedFetch ( " POST" , path , data , query ) ;
9996 }
10097
10198 put ( path , data , query = { } ) {
102- return this . authenticatedFetch ( ' PUT' , path , data , query ) ;
99+ return this . authenticatedFetch ( " PUT" , path , data , query ) ;
103100 }
104101
105102 patch ( path , data , query = { } ) {
106- return this . authenticatedFetch ( ' PATCH' , path , data , query ) ;
103+ return this . authenticatedFetch ( " PATCH" , path , data , query ) ;
107104 }
108105
109106 delete ( path , query = { } ) {
110- return this . authenticatedFetch ( ' DELETE' , path , null , query ) ;
107+ return this . authenticatedFetch ( " DELETE" , path , null , query ) ;
111108 }
112109
113110 // Meta
114111
115112 info ( query = { } ) {
116- return this . get ( ' /info' , query ) ;
113+ return this . get ( " /info" , query ) ;
117114 }
118115
119116 // Sites
120117
121118 sites ( query = { } ) {
122- return this . get ( ' /sites' , query ) . then ( sites => sites . map ( site => this . responseWrapper . site ( site ) ) ) ;
119+ return this . get ( " /sites" , query ) . then ( ( sites ) => sites . map ( ( site ) => this . responseWrapper . site ( site ) ) ) ;
123120 }
124121
125122 site ( { siteId } , query = { } ) {
126- if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( ' siteId' ) ) ;
123+ if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( " siteId" ) ) ;
127124
128- return this . get ( `/sites/${ siteId } ` , query ) . then ( site => this . responseWrapper . site ( site ) ) ;
125+ return this . get ( `/sites/${ siteId } ` , query ) . then ( ( site ) => this . responseWrapper . site ( site ) ) ;
129126 }
130127
131128 publishSite ( { siteId, domains } ) {
132- if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( ' siteId' ) ) ;
133- if ( ! domains ) return Promise . reject ( buildRequiredArgError ( ' domains' ) ) ;
129+ if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( " siteId" ) ) ;
130+ if ( ! domains ) return Promise . reject ( buildRequiredArgError ( " domains" ) ) ;
134131
135132 return this . post ( `/sites/${ siteId } /publish` , { domains } ) ;
136133 }
137134
138135 // Domains
139136
140137 domains ( { siteId } ) {
141- if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( ' siteId' ) ) ;
138+ if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( " siteId" ) ) ;
142139
143- return this . get ( `/sites/${ siteId } /domains` ) . then (
144- domains => domains . map ( domain => this . responseWrapper . domain ( domain , siteId ) ) ,
140+ return this . get ( `/sites/${ siteId } /domains` ) . then ( ( domains ) =>
141+ domains . map ( ( domain ) => this . responseWrapper . domain ( domain , siteId ) )
145142 ) ;
146143 }
147144
148145 // Collections
149146
150147 collections ( { siteId } , query = { } ) {
151- if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( ' siteId' ) ) ;
148+ if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( " siteId" ) ) ;
152149
153- return this . get ( `/sites/${ siteId } /collections` , query ) . then (
154- collections => collections . map ( collection => this . responseWrapper . collection ( collection ) ) ,
150+ return this . get ( `/sites/${ siteId } /collections` , query ) . then ( ( collections ) =>
151+ collections . map ( ( collection ) => this . responseWrapper . collection ( collection ) )
155152 ) ;
156153 }
157154
158155 collection ( { collectionId } , query = { } ) {
159- if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( ' collectionId' ) ) ;
156+ if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( " collectionId" ) ) ;
160157
161- return this . get ( `/collections/${ collectionId } ` , query ) . then (
162- collection => this . responseWrapper . collection ( collection ) ,
158+ return this . get ( `/collections/${ collectionId } ` , query ) . then ( ( collection ) =>
159+ this . responseWrapper . collection ( collection )
163160 ) ;
164161 }
165162
166163 // Items
167164
168165 items ( { collectionId } , query = { } ) {
169- if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( ' collectionId' ) ) ;
166+ if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( " collectionId" ) ) ;
170167
171- return this . get ( `/collections/${ collectionId } /items` , query ) . then (
172- res => ( {
173- ...res ,
168+ return this . get ( `/collections/${ collectionId } /items` , query ) . then ( ( res ) => ( {
169+ ...res ,
174170
175- items : res . items . map ( item => this . responseWrapper . item ( item , collectionId ) ) ,
176- } ) ,
177- ) ;
171+ items : res . items . map ( ( item ) => this . responseWrapper . item ( item , collectionId ) ) ,
172+ } ) ) ;
178173 }
179174
180175 item ( { collectionId, itemId } , query = { } ) {
181- if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( ' collectionId' ) ) ;
182- if ( ! itemId ) return Promise . reject ( buildRequiredArgError ( ' itemId' ) ) ;
176+ if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( " collectionId" ) ) ;
177+ if ( ! itemId ) return Promise . reject ( buildRequiredArgError ( " itemId" ) ) ;
183178
184- return this . get ( `/collections/${ collectionId } /items/${ itemId } ` , query ) . then (
185- res => this . responseWrapper . item ( res . items [ 0 ] , collectionId ) ,
179+ return this . get ( `/collections/${ collectionId } /items/${ itemId } ` , query ) . then ( ( res ) =>
180+ this . responseWrapper . item ( res . items [ 0 ] , collectionId )
186181 ) ;
187182 }
188183
189184 createItem ( { collectionId, ...data } , query = { } ) {
190- if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( ' collectionId' ) ) ;
185+ if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( " collectionId" ) ) ;
191186
192- return this . post ( `/collections/${ collectionId } /items` , data , query ) . then (
193- item => this . responseWrapper . item ( item , collectionId ) ,
187+ return this . post ( `/collections/${ collectionId } /items` , data , query ) . then ( ( item ) =>
188+ this . responseWrapper . item ( item , collectionId )
194189 ) ;
195190 }
196191
197192 updateItem ( { collectionId, itemId, ...data } , query = { } ) {
198- if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( ' collectionId' ) ) ;
199- if ( ! itemId ) return Promise . reject ( buildRequiredArgError ( ' itemId' ) ) ;
193+ if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( " collectionId" ) ) ;
194+ if ( ! itemId ) return Promise . reject ( buildRequiredArgError ( " itemId" ) ) ;
200195
201196 return this . put ( `/collections/${ collectionId } /items/${ itemId } ` , data , query ) ;
202197 }
203198
204199 removeItem ( { collectionId, itemId } , query = { } ) {
205- if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( ' collectionId' ) ) ;
206- if ( ! itemId ) return Promise . reject ( buildRequiredArgError ( ' itemId' ) ) ;
200+ if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( " collectionId" ) ) ;
201+ if ( ! itemId ) return Promise . reject ( buildRequiredArgError ( " itemId" ) ) ;
207202
208203 return this . delete ( `/collections/${ collectionId } /items/${ itemId } ` , query ) ;
209204 }
210205
211206 patchItem ( { collectionId, itemId, ...data } , query = { } ) {
212- if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( ' collectionId' ) ) ;
213- if ( ! itemId ) return Promise . reject ( buildRequiredArgError ( ' itemId' ) ) ;
207+ if ( ! collectionId ) return Promise . reject ( buildRequiredArgError ( " collectionId" ) ) ;
208+ if ( ! itemId ) return Promise . reject ( buildRequiredArgError ( " itemId" ) ) ;
214209
215210 return this . patch ( `/collections/${ collectionId } /items/${ itemId } ` , data , query ) ;
216211 }
@@ -222,33 +217,33 @@ export default class Webflow {
222217 // Webhooks
223218
224219 webhooks ( { siteId } , query = { } ) {
225- if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( ' siteId' ) ) ;
220+ if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( " siteId" ) ) ;
226221
227- return this . get ( `/sites/${ siteId } /webhooks` , query ) . then (
228- webhooks => webhooks . map ( webhook => this . responseWrapper . webhook ( webhook , siteId ) ) ,
222+ return this . get ( `/sites/${ siteId } /webhooks` , query ) . then ( ( webhooks ) =>
223+ webhooks . map ( ( webhook ) => this . responseWrapper . webhook ( webhook , siteId ) )
229224 ) ;
230225 }
231226
232227 webhook ( { siteId, webhookId } , query = { } ) {
233- if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( ' siteId' ) ) ;
234- if ( ! webhookId ) return Promise . reject ( buildRequiredArgError ( ' webhookId' ) ) ;
228+ if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( " siteId" ) ) ;
229+ if ( ! webhookId ) return Promise . reject ( buildRequiredArgError ( " webhookId" ) ) ;
235230
236- return this . get ( `/sites/${ siteId } /webhooks/${ webhookId } ` , query ) . then (
237- webhook => this . responseWrapper . webhook ( webhook , siteId ) ,
231+ return this . get ( `/sites/${ siteId } /webhooks/${ webhookId } ` , query ) . then ( ( webhook ) =>
232+ this . responseWrapper . webhook ( webhook , siteId )
238233 ) ;
239234 }
240235
241236 createWebhook ( { siteId, ...data } , query = { } ) {
242- if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( ' siteId' ) ) ;
237+ if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( " siteId" ) ) ;
243238
244- return this . post ( `/sites/${ siteId } /webhooks` , data , query ) . then (
245- webhook => this . responseWrapper . webhook ( webhook , siteId ) ,
239+ return this . post ( `/sites/${ siteId } /webhooks` , data , query ) . then ( ( webhook ) =>
240+ this . responseWrapper . webhook ( webhook , siteId )
246241 ) ;
247242 }
248243
249244 removeWebhook ( { siteId, webhookId } , query = { } ) {
250- if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( ' siteId' ) ) ;
251- if ( ! webhookId ) return Promise . reject ( buildRequiredArgError ( ' webhookId' ) ) ;
245+ if ( ! siteId ) return Promise . reject ( buildRequiredArgError ( " siteId" ) ) ;
246+ if ( ! webhookId ) return Promise . reject ( buildRequiredArgError ( " webhookId" ) ) ;
252247
253248 return this . delete ( `/sites/${ siteId } /webhooks/${ webhookId } ` , query ) ;
254249 }
0 commit comments