@selkirk-systems/fetch 1.0.2 → 1.0.10

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.
@@ -33,7 +33,17 @@ export function applyMiddleware(middleware = []) {
33
33
  _middlewares = middleware;
34
34
  _middlewares.push(FetchErrorHandler);
35
35
  }
36
+ export function OnOKResponse(fn) {
37
+ return ([network, isAbort]) => {
38
+ //Is any status is outside 200 - 299 it is not ok
39
+ if (network.status.code < 200 || network.status.code >= 300 || isAbort) {
40
+ return [network, isAbort];
41
+ }
36
42
 
43
+ //Run the function since its all ok
44
+ return fn([network, isAbort]);
45
+ };
46
+ }
37
47
  /**
38
48
  * Make the fetch request with the given configuration options.
39
49
  *
@@ -131,27 +141,29 @@ export default function Fetch(url, options = {}) {
131
141
  return [{
132
142
  request: request,
133
143
  response: nextResp || response,
134
- data: data
144
+ data: data,
145
+ status: {
146
+ code: response.status,
147
+ text: response.statusText,
148
+ isAbort: false
149
+ }
135
150
  }, false];
136
151
  }
137
152
  return handleError(normalizeError(data, request, response, config), config);
138
153
  }).catch(err => {
139
154
  deleteCachedRequestSignal(finalUrl);
140
- const error = isNormalizedError(err) ? err : normalizeTransportError(err);
155
+ const error = isNormalizedError(err) ? err : normalizeTransportError(err, request, config);
141
156
  return handleError(error, config);
142
157
  });
143
158
  } catch (err) {
144
159
  deleteCachedRequestSignal(finalUrl);
145
- return handleError(normalizeTransportError(err), config);
146
- }
147
- if (config._promiseChain) {
148
- //If catch is added outside, then assume they want to handle errors and
149
- //not have them swallowed
150
-
151
- config._promiseChain.catch = function (...args) {
152
- config._hasCatch = true;
153
- return ORIGINAL_CATCH_FN.apply(this, args);
154
- };
160
+ return handleError(normalizeTransportError(err, {
161
+ url: finalUrl,
162
+ headers: finalHeaders,
163
+ method: finalMethod,
164
+ body: finalBody,
165
+ signal: finalSignal.signal
166
+ }, config), config);
155
167
  }
156
168
  return config._promiseChain;
157
169
  }
@@ -207,9 +219,10 @@ function handleError(normalizedError, config) {
207
219
  //Added a catch outside
208
220
  if (!hasCatch && !nextErr) {
209
221
  if (promiseChain && promiseChain.cancel) promiseChain.cancel();
222
+
210
223
  //In the case of aborted requests etc, we treat them as success and let the initial fetch chain
211
224
  //handle them.
212
- return Promise.resolve([normalizedError, true]);
225
+ return Promise.resolve([normalizedError, normalizedError.status.isAbort]);
213
226
  }
214
227
 
215
228
  // The request failed in a critical way; the content of this error will be
@@ -366,15 +379,19 @@ function normalizeError(data, request, response, config) {
366
379
  * application errors returned by the server. At a minimum, we want every error to
367
380
  * have the following properties:
368
381
  *
382
+ * - request
369
383
  * - data.type
370
384
  * - data.message
371
- * - status.code
372
- * - status.text
373
- * - status.isAbort
385
+ * - response.status.code
386
+ * - response.status.text
387
+ * - response.status.isAbort
374
388
  */
375
- function normalizeTransportError(transportError) {
389
+ function normalizeTransportError(transportError, request, config) {
376
390
  const isAbort = transportError.name === "AbortError";
377
391
  return {
392
+ request: request,
393
+ response: null,
394
+ config: config,
378
395
  data: {
379
396
  type: "TransportError",
380
397
  message: isAbort ? "Network Request Aborted" : UNEXPECTED_ERROR_MESSAGE,
@@ -382,7 +399,7 @@ function normalizeTransportError(transportError) {
382
399
  },
383
400
  status: {
384
401
  code: 0,
385
- text: "Unknown",
402
+ text: transportError.message || "Unknown",
386
403
  isAbort: isAbort
387
404
  }
388
405
  };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { default as Download } from './Download';
2
- export { default as fetch, applyMiddleware } from './FetchWrapper';
2
+ export { default as fetch, applyMiddleware, OnOKResponse } from './Fetch';
3
3
  export { ADD_ERROR } from "./constants/ErrorConstants";
@@ -38,6 +38,19 @@ export function applyMiddleware( middleware = [] ) {
38
38
  _middlewares.push( FetchErrorHandler );
39
39
  }
40
40
 
41
+
42
+ export function OnOKResponse( fn ) {
43
+ return ( [network, isAbort] ) => {
44
+
45
+ //Is any status is outside 200 - 299 it is not ok
46
+ if ( network.status.code < 200 || network.status.code >= 300 || isAbort ) {
47
+ return [network, isAbort];
48
+ }
49
+
50
+ //Run the function since its all ok
51
+ return fn( [network, isAbort] );
52
+ }
53
+ }
41
54
  /**
42
55
  * Make the fetch request with the given configuration options.
43
56
  *
@@ -166,7 +179,12 @@ export default function Fetch( url, options = {} ) {
166
179
  {
167
180
  request: request,
168
181
  response: nextResp || response,
169
- data: data
182
+ data: data,
183
+ status: {
184
+ code: response.status,
185
+ text: response.statusText,
186
+ isAbort: false
187
+ },
170
188
  },
171
189
  false
172
190
  ]
@@ -184,7 +202,7 @@ export default function Fetch( url, options = {} ) {
184
202
 
185
203
  const error = isNormalizedError( err ) ?
186
204
  err :
187
- normalizeTransportError( err );
205
+ normalizeTransportError( err, request, config );
188
206
 
189
207
  return handleError(
190
208
  error,
@@ -198,23 +216,17 @@ export default function Fetch( url, options = {} ) {
198
216
  deleteCachedRequestSignal( finalUrl );
199
217
 
200
218
  return handleError(
201
- normalizeTransportError( err ),
219
+ normalizeTransportError( err, {
220
+ url: finalUrl,
221
+ headers: finalHeaders,
222
+ method: finalMethod,
223
+ body: finalBody,
224
+ signal: finalSignal.signal
225
+ }, config ),
202
226
  config
203
227
  );
204
228
  }
205
229
 
206
-
207
- if ( config._promiseChain ) {
208
-
209
- //If catch is added outside, then assume they want to handle errors and
210
- //not have them swallowed
211
-
212
- config._promiseChain.catch = function ( ...args ) {
213
- config._hasCatch = true;
214
- return ORIGINAL_CATCH_FN.apply( this, args );
215
- };
216
- }
217
-
218
230
  return config._promiseChain;
219
231
  }
220
232
 
@@ -273,9 +285,10 @@ function handleError( normalizedError, config ) {
273
285
  //Added a catch outside
274
286
  if ( !hasCatch && !nextErr ) {
275
287
  if ( promiseChain && promiseChain.cancel ) promiseChain.cancel();
288
+
276
289
  //In the case of aborted requests etc, we treat them as success and let the initial fetch chain
277
290
  //handle them.
278
- return Promise.resolve( [normalizedError, true] );
291
+ return Promise.resolve( [normalizedError, normalizedError.status.isAbort] );
279
292
  }
280
293
 
281
294
  // The request failed in a critical way; the content of this error will be
@@ -490,16 +503,20 @@ function normalizeError( data, request, response, config ) {
490
503
  * application errors returned by the server. At a minimum, we want every error to
491
504
  * have the following properties:
492
505
  *
506
+ * - request
493
507
  * - data.type
494
508
  * - data.message
495
- * - status.code
496
- * - status.text
497
- * - status.isAbort
509
+ * - response.status.code
510
+ * - response.status.text
511
+ * - response.status.isAbort
498
512
  */
499
- function normalizeTransportError( transportError ) {
513
+ function normalizeTransportError( transportError, request, config ) {
500
514
 
501
515
  const isAbort = ( transportError.name === "AbortError" )
502
516
  return ( {
517
+ request: request,
518
+ response: null,
519
+ config: config,
503
520
  data: {
504
521
  type: "TransportError",
505
522
  message: isAbort ? "Network Request Aborted" : UNEXPECTED_ERROR_MESSAGE,
@@ -507,7 +524,7 @@ function normalizeTransportError( transportError ) {
507
524
  },
508
525
  status: {
509
526
  code: 0,
510
- text: "Unknown",
527
+ text: transportError.message || "Unknown",
511
528
  isAbort: isAbort
512
529
  }
513
530
  } );
package/lib/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { default as Download } from './Download';
2
- export { default as fetch, applyMiddleware } from './FetchWrapper';
2
+ export { default as fetch, applyMiddleware, OnOKResponse } from './Fetch';
3
3
  export { ADD_ERROR } from "./constants/ErrorConstants";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@selkirk-systems/fetch",
3
- "version": "1.0.2",
3
+ "version": "1.0.10",
4
4
  "description": "Abortable fetch library",
5
5
  "keywords": [],
6
6
  "author": "Marcos Bernal <mbernal@selkirksystems.com>",
@@ -26,7 +26,8 @@
26
26
  "scripts": {
27
27
  "test": "node ./__tests__/**",
28
28
  "docs": "jsdoc --configure ../../jsdoc.json -r ./lib -d docs",
29
- "build": "del dist && cross-env CI=false NODE_ENV=production babel lib --out-dir dist --copy-files --ignore __tests__,spec.js,test.js,__snapshots__"
29
+ "build": "del dist /Q && cross-env CI=false NODE_ENV=production babel lib --out-dir dist --copy-files --ignore __tests__,spec.js,test.js,__snapshots__",
30
+ "prepublishOnly": "npm run build"
30
31
  },
31
32
  "bugs": {
32
33
  "url": "https://bitbucket.org/selkirk/web-component-library/issues"
@@ -35,5 +36,5 @@
35
36
  "peerDependencies": {
36
37
  "@selkirk-systems/state-management": "^1.0.0"
37
38
  },
38
- "gitHead": "5c983164ac8e9ba82f7029463483fe5635987802"
39
+ "gitHead": "4b7ac96fea827d9ac5d1faa40917fa3a39599329"
39
40
  }
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- declare module '@selkirk-systems/fetch';