@@ -62,36 +62,53 @@ Sendwithus.prototype._buildUrl = function (resource, identifier, action) {
6262} ;
6363
6464Sendwithus . prototype . _fetchBoilerplate = async function ( url , args ) {
65+
6566 const response = await fetch ( url , args ) ;
6667
6768 try {
6869 const result = await response . json ( ) ;
6970 return { response, result } ;
70- } catch ( error ) {
71+ }
72+ catch ( error ) {
7173 return { response, result : error } ;
7274 }
75+
7376} ;
7477
75- Sendwithus . prototype . _fetch = function ( ) {
78+ Sendwithus . prototype . _fetch = function ( callback ) {
79+ var that = this ;
80+
7681 return {
7782 postJson : ( url , data , options ) => {
7883 return this . _fetchBoilerplate ( url , {
7984 method : "post" ,
8085 body : JSON . stringify ( data ) ,
8186 ...options ,
82- } ) ;
87+ } )
88+ . then ( ( { response, result } ) => {
89+ that . _handleResponse . call ( that , result , response , callback ) ;
90+ } )
91+ . catch ( ( { response, result } ) => { that . _handleResponse . call ( that , result , response , callback ) ; } ) ;
8392 } ,
8493 get : ( url , options ) => {
8594 return this . _fetchBoilerplate ( url , {
8695 method : "get" ,
8796 ...options ,
88- } ) ;
97+ } )
98+ . then ( ( { response, result } ) => {
99+ that . _handleResponse . call ( that , result , response , callback ) ;
100+ } )
101+ . catch ( ( { response, result } ) => { that . _handleResponse . call ( that , result , response , callback ) ; } ) ;
89102 } ,
90103 del : ( url , options ) => {
91104 return this . _fetchBoilerplate ( url , {
92105 method : "delete" ,
93106 ...options ,
94- } ) ;
107+ } )
108+ . then ( ( { response, result } ) => {
109+ that . _handleResponse . call ( that , result , response , callback ) ;
110+ } )
111+ . catch ( ( { response, result } ) => { that . _handleResponse . call ( that , result , response , callback ) ; } ) ;
95112 } ,
96113 } ;
97114} ;
@@ -102,86 +119,61 @@ Sendwithus.prototype._handleResponse = function (result, response, callback) {
102119 if ( typeof callback == "function" ) {
103120 callback ( result , response ) ;
104121 }
105- } else if ( response . status === 200 ) {
122+ } else if ( response . ok ) { // the range 200-299 means the response was successful and should not return an error. https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
106123 this . emit ( "response" , response . status , result , response ) ;
107- this . _debug ( " Response 200: " + JSON . stringify ( result ) ) ;
124+ this . _debug ( ` Response ${ response . status } : ` + JSON . stringify ( result ) ) ;
108125 if ( typeof callback == "function" ) {
109126 callback ( null , result ) ;
110127 }
111- } else {
112- this . emit ( "response" , response . status , result , response ) ;
128+ }
129+ // handle anything which is an error outside of the instaceof Error type
130+ else if ( result && result . stack && result . message ) {
113131 this . _debug ( "Response " + response . status + ": " + JSON . stringify ( result ) ) ;
114-
115132 var err = new Error ( "Request failed with " + response . status ) ;
116133 err . statusCode = response . status ;
117-
118134 if ( typeof callback == "function" ) {
119135 callback ( err , result ) ;
120136 }
121137 }
138+ else {
139+ // handle anything else outside errors level responses which are not errors (100, and 300 response group).
140+ this . emit ( "response" , response . status , result , response ) ;
141+ this . _debug ( `Response ${ response . status } : ` + JSON . stringify ( result ) ) ;
142+ if ( typeof callback == "function" ) {
143+ callback ( null , result ) ;
144+ }
145+ }
122146} ;
123147
124148////////////////////////////
125149// PUBLIC METHODS
126150
127151Sendwithus . prototype . batch = function ( data , callback ) {
128152 var url = this . _buildUrl ( "batch" ) ;
129-
130153 var options = this . _getOptions ( ) ;
131-
132- var that = this ;
133154 this . emit ( "request" , "POST" , url , options . headers , data ) ;
134-
135- this . _fetch ( )
136- . postJson ( url , data , options )
137- . then ( ( { response, result } ) => {
138- that . _handleResponse . call ( that , result , response , callback ) ;
139- } ) ;
155+ this . _fetch ( callback ) . postJson ( url , data , options )
140156} ;
141157
142158Sendwithus . prototype . send = function ( data , callback ) {
143159 var url = this . _buildUrl ( "send" ) ;
144-
145160 var options = this . _getOptions ( ) ;
146-
147- var that = this ;
148161 this . emit ( "request" , "POST" , url , options . headers , data ) ;
149-
150- this . _fetch ( )
151- . postJson ( url , data , options )
152- . then ( ( { response, result } ) => {
153- that . _handleResponse . call ( that , result , response , callback ) ;
154- } ) ;
162+ this . _fetch ( callback ) . postJson ( url , data , options )
155163} ;
156164
157165Sendwithus . prototype . render = function ( data , callback ) {
158166 var url = this . _buildUrl ( "render" ) ;
159-
160167 var options = this . _getOptions ( ) ;
161-
162- var that = this ;
163168 this . emit ( "request" , "POST" , url , options . headers , data ) ;
164-
165- this . _fetch ( )
166- . postJson ( url , data , options )
167- . then ( ( { response, result } ) => {
168- that . _handleResponse . call ( that , result , response , callback ) ;
169- } ) ;
169+ this . _fetch ( callback ) . postJson ( url , data , options )
170170} ;
171171
172172Sendwithus . prototype . templates = function ( callback ) {
173173 var url = this . _buildUrl ( "templates" ) ;
174-
175174 var options = this . _getOptions ( ) ;
176-
177- var that = this ;
178175 this . emit ( "request" , "GET" , url , options . headers , { } ) ;
179-
180- this . _fetch ( )
181- . get ( url , options )
182- . then ( ( { response, result } ) => {
183- that . _handleResponse . call ( that , result , response , callback ) ;
184- } ) ;
176+ this . _fetch ( callback ) . get ( url , options )
185177} ;
186178
187179/* 'emails' is a deprecated method since Sendwithus has changed their language
@@ -191,65 +183,33 @@ Sendwithus.prototype.emails = Sendwithus.prototype.templates;
191183
192184Sendwithus . prototype . customersUpdateOrCreate = function ( data , callback ) {
193185 var url = this . _buildUrl ( "customers" ) ;
194-
195186 var options = this . _getOptions ( ) ;
196-
197- var that = this ;
198187 this . emit ( "request" , "POST" , url , options . headers , data ) ;
199-
200- this . _fetch ( )
201- . postJson ( url , data , options )
202- . then ( ( { response, result } ) => {
203- that . _handleResponse . call ( that , result , response , callback ) ;
204- } ) ;
188+ this . _fetch ( callback ) . postJson ( url , data , options )
205189} ;
206190
207191Sendwithus . prototype . customersDelete = function ( email , callback ) {
208192 var url = this . _buildUrl ( "customers" , email ) ;
209-
210193 var options = this . _getOptions ( ) ;
211-
212- var that = this ;
213194 this . emit ( "request" , "DELETE" , url , options . headers , { } ) ;
214-
215- this . _fetch ( )
216- . del ( url , options )
217- . then ( ( { response, result } ) => {
218- that . _handleResponse . call ( that , result , response , callback ) ;
219- } ) ;
195+ this . _fetch ( callback ) . del ( url , options )
220196} ;
221197
222198Sendwithus . prototype . dripCampaignList = function ( callback ) {
223199 var url = this . _buildUrl ( "drip_campaigns" ) ;
224-
225200 var options = this . _getOptions ( ) ;
226-
227- var that = this ;
228201 this . emit ( "request" , "GET" , url , options . headers , { } ) ;
229-
230- this . _fetch ( )
231- . get ( url , options )
232- . then ( ( { response, result } ) => {
233- that . _handleResponse . call ( that , result , response , callback ) ;
234- } ) ;
202+ this . _fetch ( callback ) . get ( url , options )
235203} ;
236204
237205Sendwithus . prototype . dripCampaignDetails = function (
238206 drip_campaign_id ,
239207 callback
240208) {
241209 var url = this . _buildUrl ( "drip_campaigns" , drip_campaign_id ) ;
242-
243210 var options = this . _getOptions ( ) ;
244-
245- var that = this ;
246211 this . emit ( "request" , "GET" , url , options . headers , { } ) ;
247-
248- this . _fetch ( )
249- . get ( url , options )
250- . then ( ( { response, result } ) => {
251- that . _handleResponse . call ( that , result , response , callback ) ;
252- } ) ;
212+ this . _fetch ( callback ) . get ( url , options )
253213} ;
254214
255215Sendwithus . prototype . dripCampaignActivate = function (
@@ -258,17 +218,9 @@ Sendwithus.prototype.dripCampaignActivate = function (
258218 callback
259219) {
260220 var url = this . _buildUrl ( "drip_campaigns" , drip_campaign_id , "activate" ) ;
261-
262221 var options = this . _getOptions ( ) ;
263-
264- var that = this ;
265222 this . emit ( "request" , "POST" , url , options . headers , data ) ;
266-
267- this . _fetch ( )
268- . postJson ( url , data , options )
269- . then ( ( { response, result } ) => {
270- that . _handleResponse . call ( that , result , response , callback ) ;
271- } ) ;
223+ this . _fetch ( callback ) . postJson ( url , data , options )
272224} ;
273225
274226Sendwithus . prototype . dripCampaignDeactivate = function (
@@ -277,47 +229,23 @@ Sendwithus.prototype.dripCampaignDeactivate = function (
277229 callback
278230) {
279231 var url = this . _buildUrl ( "drip_campaigns" , drip_campaign_id , "deactivate" ) ;
280-
281232 var options = this . _getOptions ( ) ;
282-
283- var that = this ;
284233 this . emit ( "request" , "POST" , url , options . headers , data ) ;
285-
286- this . _fetch ( )
287- . del ( url , options )
288- . then ( ( { response, result } ) => {
289- that . _handleResponse . call ( that , result , response , callback ) ;
290- } ) ;
234+ this . _fetch ( callback ) . del ( url , options ) ;
291235} ;
292236
293237Sendwithus . prototype . dripCampaignDeactivateAll = function ( data , callback ) {
294238 var url = this . _buildUrl ( "drip_campaigns" , "deactivate" ) ;
295-
296239 var options = this . _getOptions ( ) ;
297-
298- var that = this ;
299240 this . emit ( "request" , "POST" , url , options . headers , data ) ;
300-
301- this . _fetch ( )
302- . postJson ( url , data , options )
303- . then ( ( { response, result } ) => {
304- that . _handleResponse . call ( that , result , response , callback ) ;
305- } ) ;
241+ this . _fetch ( callback ) . postJson ( url , data , options )
306242} ;
307243
308244Sendwithus . prototype . createTemplate = function ( data , callback ) {
309245 var url = this . _buildUrl ( "templates" ) ;
310-
311246 var options = this . _getOptions ( ) ;
312-
313- var that = this ;
314247 this . emit ( "request" , "POST" , url , options . headers , data ) ;
315-
316- this . _fetch ( )
317- . postJson ( url , data , options )
318- . then ( ( { response, result } ) => {
319- that . _handleResponse . call ( that , result , response , callback ) ;
320- } ) ;
248+ this . _fetch ( callback ) . postJson ( url , data , options )
321249} ;
322250
323251Sendwithus . prototype . createTemplateVersion = function (
@@ -326,31 +254,16 @@ Sendwithus.prototype.createTemplateVersion = function (
326254 callback
327255) {
328256 var url = this . _buildUrl ( "templates" , templateId , "versions" ) ;
329-
330257 var options = this . _getOptions ( ) ;
331-
332- var that = this ;
333258 this . emit ( "request" , "POST" , url , options . headers , data ) ;
334-
335- this . _fetch ( )
336- . postJson ( url , data , options )
337- . then ( ( { response, result } ) => {
338- that . _handleResponse . call ( that , result , response , callback ) ;
339- } ) ;
259+ this . _fetch ( callback ) . postJson ( url , data , options )
340260} ;
341261
342262Sendwithus . prototype . resend = function ( data , callback ) {
343263 var url = this . _buildUrl ( "resend" ) ;
344-
345264 var options = this . _getOptions ( ) ;
346-
347- var that = this ;
348265 this . emit ( "request" , "POST" , url , options . headers , data ) ;
349- this . _fetch ( )
350- . postJson ( url , data , options )
351- . then ( ( { response, result } ) => {
352- that . _handleResponse . call ( that , result , response , callback ) ;
353- } ) ;
266+ this . _fetch ( callback ) . postJson ( url , data , options )
354267} ;
355268
356269module . exports = function ( apiKey , debug ) {
0 commit comments