@cerebral/http
Advanced tools
Comparing version 4.2.0-1519242130161 to 4.2.0-1519392810913
import { Provider } from "function-tree" | ||
export enum HttpProviderErrorTypes {Http = 'http', Abort = 'abort', Timeout = 'timeout'} | ||
export class HttpProviderError extends Error { | ||
constructor (status: number, headers: any, body: any, message?: string, isAborted?: boolean) | ||
constructor (type: HttpProviderErrorTypes, status: number, headers: any, body: any, message?: string, isAborted?: boolean) | ||
type: HttpProviderErrorTypes | ||
response: { | ||
@@ -6,0 +9,0 @@ status: number, |
@@ -62,7 +62,6 @@ 'use strict'; | ||
headers: responseHeaders, | ||
result: result, | ||
isAborted: false | ||
result: result | ||
}); | ||
} else { | ||
reject(new _HttpProviderError2.default(xhr.status, responseHeaders, result, xhr.responseText)); | ||
reject(new _HttpProviderError2.default(undefined, xhr.status, responseHeaders, result, xhr.responseText)); | ||
} | ||
@@ -69,0 +68,0 @@ } |
@@ -40,8 +40,10 @@ 'use strict'; | ||
var _warnAboutDeprecatedIsAborted = false; | ||
var HttpProviderError = function (_extendableBuiltin2) { | ||
_inherits(HttpProviderError, _extendableBuiltin2); | ||
function HttpProviderError(status, headers, result) { | ||
var message = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
var isAborted = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; | ||
function HttpProviderError(type, status, headers, result) { | ||
var message = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null; | ||
var isAborted = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false; | ||
@@ -54,8 +56,18 @@ _classCallCheck(this, HttpProviderError); | ||
_this.message = message; | ||
_this.type = type || 'http'; | ||
_this.response = { | ||
status: status, | ||
headers: headers, | ||
result: result, | ||
isAborted: isAborted | ||
result: result | ||
}; | ||
Object.defineProperty(_this.response, 'isAborted', { | ||
get: function get() { | ||
if (!_warnAboutDeprecatedIsAborted) { | ||
console.warn('DEPRECATED - Please use error.type === "abort" instead'); | ||
_warnAboutDeprecatedIsAborted = true; | ||
} | ||
return isAborted; | ||
} | ||
}); | ||
return _this; | ||
@@ -68,2 +80,3 @@ } | ||
return { | ||
type: this.type, | ||
name: this.name, | ||
@@ -70,0 +83,0 @@ message: this.message, |
@@ -9,4 +9,8 @@ 'use strict'; | ||
function request(options, cb) { | ||
function request() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var cb = arguments[1]; | ||
var xhr = new XMLHttpRequest(); | ||
xhr.addEventListener('progress', cb); | ||
@@ -16,2 +20,13 @@ xhr.addEventListener('load', cb); | ||
xhr.addEventListener('abort', cb); | ||
xhr.ontimeout = function () { | ||
var ev = { | ||
type: 'timeout', | ||
currentTarget: xhr | ||
}; | ||
cb(ev); | ||
}; | ||
xhr.timeout = options.timeout || 0; | ||
xhr.open(options.method, options.baseUrl + options.url); | ||
@@ -18,0 +33,0 @@ options.onRequest(xhr, options); |
@@ -35,7 +35,10 @@ 'use strict'; | ||
case 'error': | ||
reject(new _HttpProviderError2.default(event.currentTarget.status, getAllResponseHeaders(event.currentTarget), event.currentTarget.responseText, event.currentTarget.responseText || 'request error')); | ||
reject(new _HttpProviderError2.default(undefined, event.currentTarget.status, getAllResponseHeaders(event.currentTarget), event.currentTarget.responseText, event.currentTarget.responseText || 'request error')); | ||
break; | ||
case 'abort': | ||
reject(new _HttpProviderError2.default(event.currentTarget.status, getAllResponseHeaders(event.currentTarget), null, 'request abort', true)); | ||
reject(new _HttpProviderError2.default(event.type, event.currentTarget.status, getAllResponseHeaders(event.currentTarget), null, 'request abort', true)); | ||
break; | ||
case 'timeout': | ||
reject(new _HttpProviderError2.default(event.type, event.currentTarget.status, getAllResponseHeaders(event.currentTarget), null, 'request timeout')); | ||
break; | ||
} | ||
@@ -104,6 +107,10 @@ }; | ||
if (error.isAborted && path.abort) { | ||
if (error.type === 'abort' && path.abort) { | ||
return path.abort({ error: error.toJSON() }); | ||
} | ||
if (error.type === 'timeout' && path.timeout) { | ||
return path.timeout({ error: error.toJSON() }); | ||
} | ||
if (path[error.response.status]) { | ||
@@ -110,0 +117,0 @@ return path[error.response.status]({ error: error.toJSON() }); |
{ | ||
"name": "@cerebral/http", | ||
"version": "4.2.0-1519242130161", | ||
"version": "4.2.0-1519392810913", | ||
"description": "HTTP provider for Cerebral 2", | ||
@@ -27,8 +27,8 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"cerebral": "^4.2.0-1519242130161" | ||
"cerebral": "^4.2.0-1519392810913" | ||
}, | ||
"devDependencies": { | ||
"cerebral": "^4.2.0-1519242130161", | ||
"xhr-mock": "^1.9.0" | ||
"cerebral": "^4.2.0-1519392810913", | ||
"xhr-mock": "^2.1.0" | ||
} | ||
} |
@@ -44,3 +44,9 @@ # @cerebral/http | ||
// if set to true | ||
withCredentials: false | ||
withCredentials: false, | ||
// Provide a global request timeout for all calls | ||
// which can be overwritten for request by providing | ||
// a different timeout when doing a request | ||
// in actions or operators | ||
timeout: 5000 | ||
}) | ||
@@ -66,3 +72,3 @@ | ||
## abort | ||
You can abort any running request, causing the request to resolve as status code **0** and set an **isAborted** property on the response object. | ||
You can abort any running request, causing the request to resolve as status code **0** and sets the type to **abort** on the error object. | ||
@@ -75,3 +81,3 @@ ```js | ||
.catch((error) => { | ||
if (error.isAborted) { | ||
if (error.type === 'abort') { | ||
return path.abort() | ||
@@ -114,2 +120,3 @@ } | ||
name: 'HttpProviderError', | ||
type: 'http | abort | timeout', | ||
message: 'Some error message or responseText', | ||
@@ -119,4 +126,3 @@ response: { | ||
headers: {}, | ||
status: 200, | ||
isAborted: false | ||
status: 200 | ||
}, | ||
@@ -214,2 +220,5 @@ stack: '...' | ||
], | ||
timeout: [ | ||
/* PROPS: {error: {...}} */ | ||
], | ||
@@ -273,2 +282,5 @@ // Optionally any status code, ex. 404: [] | ||
], | ||
timeout: [ | ||
/* PROPS: {error: {...}} */ | ||
], | ||
@@ -332,2 +344,5 @@ // Optionally any status code, ex. 404: [] | ||
], | ||
timeout: [ | ||
/* PROPS: {error: {...}} */ | ||
], | ||
@@ -397,2 +412,5 @@ // Optionally any status code, ex. 404: [] | ||
], | ||
timeout: [ | ||
/* PROPS: {error: {...}} */ | ||
], | ||
@@ -458,2 +476,5 @@ // Optionally any status code, ex. 404: [] | ||
], | ||
timeout: [ | ||
/* PROPS: {error: {...}} */ | ||
], | ||
@@ -476,4 +497,3 @@ // Optionally any status code, ex. 404: [] | ||
headers: {...}, | ||
status: 200, | ||
isAborted: false | ||
status: 200 | ||
} | ||
@@ -486,6 +506,6 @@ ``` | ||
name: 'HttpProviderError', | ||
type: 'http | abort | timeout', | ||
message: 'Some potential error message', | ||
result: 'Message or response body', | ||
status: 500, | ||
isAborted: false, | ||
headers: {}, | ||
@@ -492,0 +512,0 @@ stack: '...' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
76948
693
598