@dc0de/http-client
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -10,3 +10,11 @@ "use strict"; | ||
var pathToRegexp = require("path-to-regexp"); | ||
var tslib_1 = require("tslib"); | ||
var _objectSpread = _interopDefault( | ||
require("@babel/runtime/helpers/objectSpread") | ||
); | ||
var _classCallCheck = _interopDefault( | ||
require("@babel/runtime/helpers/classCallCheck") | ||
); | ||
var _createClass = _interopDefault( | ||
require("@babel/runtime/helpers/createClass") | ||
); | ||
var axios = _interopDefault(require("axios")); | ||
@@ -16,25 +24,2 @@ var rxjs = require("rxjs"); | ||
var cacheCount = 0; | ||
var cacheLimit = 10000; | ||
var patternCache = {}; | ||
function compileGenerator(urlPattern) { | ||
if (patternCache[urlPattern]) { | ||
return patternCache[urlPattern]; | ||
} | ||
var compiledGenerator = pathToRegexp.compile(urlPattern); | ||
/* istanbul ignore else */ | ||
if (cacheCount < cacheLimit) { | ||
cacheCount += 1; | ||
patternCache[urlPattern] = compiledGenerator; | ||
} | ||
return compiledGenerator; | ||
} | ||
function generatePath(urlPattern, urlParams) { | ||
if (!urlPattern || !urlParams) { | ||
return urlPattern; | ||
} | ||
var generator = compileGenerator(urlPattern); | ||
return generator(urlParams); | ||
} | ||
(function(HttpStatus) { | ||
@@ -92,176 +77,260 @@ HttpStatus[(HttpStatus["BadRequest"] = 400)] = "BadRequest"; | ||
var cacheCount = 0; | ||
var cacheLimit = 10000; | ||
var patternCache = {}; | ||
function compileGenerator(urlPattern) { | ||
if (patternCache[urlPattern]) { | ||
return patternCache[urlPattern]; | ||
} | ||
var compiledGenerator = pathToRegexp.compile(urlPattern); | ||
/* istanbul ignore else */ | ||
if (cacheCount < cacheLimit) { | ||
cacheCount += 1; | ||
patternCache[urlPattern] = compiledGenerator; | ||
} | ||
return compiledGenerator; | ||
} | ||
function generatePath(urlPattern, urlParams) { | ||
if (!urlPattern || !urlParams) { | ||
return urlPattern; | ||
} | ||
var generator = compileGenerator(urlPattern); | ||
return generator(urlParams); | ||
} | ||
function isAxiosError(error) { | ||
return "config" in error; | ||
} | ||
/** | ||
##### Initialization | ||
```javascript | ||
export function createHttpClient(logger) { | ||
return new HttpClient({ | ||
requestInterceptor(config) { | ||
logger.logRequest(config); | ||
}, | ||
* ##### Initialization | ||
* | ||
responseInterceptor(config, response) { | ||
logger.logResponse(config, response); | ||
}, | ||
* ```javascript | ||
* export function createHttpClient(logger) { | ||
* return new HttpClient({ | ||
* requestInterceptor(config) { | ||
* logger.logRequest(config); | ||
* }, | ||
* | ||
errorInterceptor(error) { | ||
logger.logError(error); | ||
}, | ||
}); | ||
} | ||
``` | ||
* responseInterceptor(config, response) { | ||
* logger.logResponse(config, response); | ||
* }, | ||
* | ||
* errorInterceptor(error) { | ||
* logger.logError(error); | ||
* }, | ||
* }); | ||
* } | ||
* ``` | ||
* | ||
* ##### Request | ||
* | ||
* ```javascript | ||
* const USER_URL = "/user/:userId"; | ||
* const USER_COMMENTS_URL = "/user/:userId/comments"; | ||
* | ||
* export function fetchUser(httpClient, userId) { | ||
* return httpClient.get(USER_URL, { | ||
* pathParams: { userId } | ||
* }); | ||
* } | ||
* | ||
* export function fetchUserPosts(httpClient, userId, page, limit) { | ||
* return httpClient.get(USER_COMMENTS_URL, { | ||
* url: USER_URL, | ||
* pathParams: { userId }, | ||
* queryParams: { page, limit } | ||
* }); | ||
* } | ||
* ``` | ||
*/ | ||
var HttpClient = | ||
/*#__PURE__*/ | ||
(function() { | ||
function HttpClient() { | ||
var options = | ||
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
##### Request | ||
```javascript | ||
const USER_URL = "/user/:userId"; | ||
const USER_COMMENTS_URL = "/user/:userId/comments"; | ||
_classCallCheck(this, HttpClient); | ||
export function fetchUser(httpClient, userId) { | ||
return httpClient.get(USER_URL, { | ||
pathParams: { userId } | ||
}); | ||
} | ||
this.options = options; | ||
this.client = axios.create(); | ||
} | ||
export function fetchUserPosts(httpClient, userId, page, limit) { | ||
return httpClient.get(USER_COMMENTS_URL, { | ||
url: USER_URL, | ||
pathParams: { userId }, | ||
queryParams: { page, limit } | ||
}); | ||
} | ||
``` | ||
*/ | ||
var HttpClient = /** @class */ (function() { | ||
function HttpClient(options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
this.options = options; | ||
this.client = axios.create(); | ||
} | ||
HttpClient.prototype.get = function(url, options) { | ||
return this.request( | ||
tslib_1.__assign({}, options, { url: url, method: "GET" }) | ||
); | ||
}; | ||
HttpClient.prototype.post = function(url, options) { | ||
return this.request( | ||
tslib_1.__assign({}, options, { url: url, method: "POST" }) | ||
); | ||
}; | ||
HttpClient.prototype.put = function(url, options) { | ||
return this.request( | ||
tslib_1.__assign({}, options, { url: url, method: "PUT" }) | ||
); | ||
}; | ||
HttpClient.prototype.patch = function(url, options) { | ||
return this.request( | ||
tslib_1.__assign({}, options, { url: url, method: "PATCH" }) | ||
); | ||
}; | ||
HttpClient.prototype.delete = function(url, options) { | ||
return this.request( | ||
tslib_1.__assign({}, options, { url: url, method: "DELETE" }) | ||
); | ||
}; | ||
HttpClient.prototype.request = function(options) { | ||
var _this = this; | ||
var _a = this.options, | ||
shouldRetry = _a.shouldRetry, | ||
errorInterceptor = _a.errorInterceptor, | ||
requestInterceptor = _a.requestInterceptor, | ||
responseInterceptor = _a.responseInterceptor; | ||
var stream = new rxjs.Observable(function(subscriber) { | ||
var cancelTokenSource = axios.CancelToken.source(); | ||
if (requestInterceptor) { | ||
requestInterceptor(options); | ||
} | ||
var config = { | ||
method: options.method, | ||
url: generatePath(options.url, options.pathParams), | ||
params: tslib_1.__assign({}, options.queryParams), | ||
data: options.data, | ||
headers: tslib_1.__assign({}, options.headers), | ||
timeout: options.timeout, | ||
cancelToken: cancelTokenSource.token | ||
}; | ||
// Fix for react-native in Android devices. | ||
if (config.timeout == null || !isFinite(config.timeout)) { | ||
delete config.timeout; | ||
} | ||
var complete = false; | ||
_this.client | ||
.request(config) | ||
.then(function(x) { | ||
var response = { | ||
data: x.data, | ||
status: x.status, | ||
headers: x.headers | ||
}; | ||
if (responseInterceptor) { | ||
responseInterceptor(options, response); | ||
} | ||
subscriber.next(response); | ||
}) | ||
.catch(function(x) { | ||
if (axios.isCancel(x)) { | ||
return; | ||
} | ||
var error = !isAxiosError(x) | ||
? x | ||
: createHttpClientError({ | ||
code: x.code, | ||
config: options, | ||
message: x.message, | ||
response: x.response | ||
}); | ||
if (errorInterceptor) { | ||
errorInterceptor(error); | ||
} | ||
subscriber.error(error); | ||
}) | ||
.then(function() { | ||
complete = true; | ||
subscriber.complete(); | ||
}); | ||
return function() { | ||
if (!complete) { | ||
cancelTokenSource.cancel(); | ||
_createClass(HttpClient, [ | ||
{ | ||
key: "get", | ||
value: function get(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "GET" | ||
}) | ||
); | ||
} | ||
}; | ||
}); | ||
return !shouldRetry | ||
? stream | ||
: stream.pipe( | ||
operators.shareReplay(), | ||
operators.retryWhen(function(errors) { | ||
return errors.pipe( | ||
operators.scan(function(attempt, error) { | ||
if ( | ||
shouldRetry({ | ||
error: error, | ||
config: options, | ||
attempt: attempt | ||
}) | ||
) { | ||
return attempt + 1; | ||
}, | ||
{ | ||
key: "post", | ||
value: function post(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "POST" | ||
}) | ||
); | ||
} | ||
}, | ||
{ | ||
key: "put", | ||
value: function put(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "PUT" | ||
}) | ||
); | ||
} | ||
}, | ||
{ | ||
key: "patch", | ||
value: function patch(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "PATCH" | ||
}) | ||
); | ||
} | ||
}, | ||
{ | ||
key: "delete", | ||
value: function _delete(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "DELETE" | ||
}) | ||
); | ||
} | ||
}, | ||
{ | ||
key: "request", | ||
value: function request(options) { | ||
var _this = this; | ||
var _this$options = this.options, | ||
shouldRetry = _this$options.shouldRetry, | ||
errorInterceptor = _this$options.errorInterceptor, | ||
requestInterceptor = _this$options.requestInterceptor, | ||
responseInterceptor = _this$options.responseInterceptor; | ||
var stream = new rxjs.Observable(function(subscriber) { | ||
var cancelTokenSource = axios.CancelToken.source(); | ||
if (requestInterceptor) { | ||
requestInterceptor(options); | ||
} | ||
var config = { | ||
method: options.method, | ||
url: generatePath(options.url, options.pathParams), | ||
params: _objectSpread({}, options.queryParams), | ||
data: options.data, | ||
headers: _objectSpread({}, options.headers), | ||
timeout: options.timeout, | ||
cancelToken: cancelTokenSource.token | ||
}; // Fix for react-native in Android devices. | ||
if (config.timeout == null || !isFinite(config.timeout)) { | ||
delete config.timeout; | ||
} | ||
var complete = false; | ||
_this.client | ||
.request(config) | ||
.then(function(x) { | ||
var response = { | ||
data: x.data, | ||
status: x.status, | ||
headers: x.headers | ||
}; | ||
if (responseInterceptor) { | ||
responseInterceptor(options, response); | ||
} | ||
throw error; | ||
}, 1) | ||
); | ||
}) | ||
); | ||
}; | ||
return HttpClient; | ||
})(); | ||
exports.HttpClient = HttpClient; | ||
subscriber.next(response); | ||
}) | ||
.catch(function(x) { | ||
if (axios.isCancel(x)) { | ||
return; | ||
} | ||
var error = !isAxiosError(x) | ||
? x | ||
: createHttpClientError({ | ||
code: x.code, | ||
config: options, | ||
message: x.message, | ||
response: x.response | ||
}); | ||
if (errorInterceptor) { | ||
errorInterceptor(error); | ||
} | ||
subscriber.error(error); | ||
}) | ||
.then(function() { | ||
complete = true; | ||
subscriber.complete(); | ||
}); | ||
return function() { | ||
if (!complete) { | ||
cancelTokenSource.cancel(); | ||
} | ||
}; | ||
}); | ||
return !shouldRetry | ||
? stream | ||
: stream.pipe( | ||
operators.shareReplay(), | ||
operators.retryWhen(function(errors) { | ||
return errors.pipe( | ||
operators.scan(function(attempt, error) { | ||
if ( | ||
shouldRetry({ | ||
error: error, | ||
config: options, | ||
attempt: attempt | ||
}) | ||
) { | ||
return attempt + 1; | ||
} | ||
throw error; | ||
}, 1) | ||
); | ||
}) | ||
); | ||
} | ||
} | ||
]); | ||
return HttpClient; | ||
})(); | ||
exports.HTTP_ERROR_TIMEOUT_CODE = HTTP_ERROR_TIMEOUT_CODE; | ||
exports.createHttpClientError = createHttpClientError; | ||
exports.isHttpClientError = isHttpClientError; | ||
exports.getHttpClientErrorStatus = getHttpClientErrorStatus; | ||
exports.isHttpClientResponseError = isHttpClientResponseError; | ||
exports.isHttpClientTimeoutError = isHttpClientTimeoutError; | ||
exports.isHttpClientResponseError = isHttpClientResponseError; | ||
exports.getHttpClientErrorStatus = getHttpClientErrorStatus; | ||
exports.HTTP_ERROR_TIMEOUT_CODE = HTTP_ERROR_TIMEOUT_CODE; | ||
exports.HttpClient = HttpClient; |
import { compile } from "path-to-regexp"; | ||
import { __assign } from "tslib"; | ||
import _objectSpread from "@babel/runtime/helpers/objectSpread"; | ||
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; | ||
import _createClass from "@babel/runtime/helpers/createClass"; | ||
import axios from "axios"; | ||
@@ -7,26 +9,4 @@ import { Observable } from "rxjs/_esm5"; | ||
var cacheCount = 0; | ||
var cacheLimit = 10000; | ||
var patternCache = {}; | ||
function compileGenerator(urlPattern) { | ||
if (patternCache[urlPattern]) { | ||
return patternCache[urlPattern]; | ||
} | ||
var compiledGenerator = compile(urlPattern); | ||
/* istanbul ignore else */ | ||
if (cacheCount < cacheLimit) { | ||
cacheCount += 1; | ||
patternCache[urlPattern] = compiledGenerator; | ||
} | ||
return compiledGenerator; | ||
} | ||
function generatePath(urlPattern, urlParams) { | ||
if (!urlPattern || !urlParams) { | ||
return urlPattern; | ||
} | ||
var generator = compileGenerator(urlPattern); | ||
return generator(urlParams); | ||
} | ||
var HttpStatus; | ||
var HttpStatus; | ||
(function(HttpStatus) { | ||
@@ -84,169 +64,263 @@ HttpStatus[(HttpStatus["BadRequest"] = 400)] = "BadRequest"; | ||
var cacheCount = 0; | ||
var cacheLimit = 10000; | ||
var patternCache = {}; | ||
function compileGenerator(urlPattern) { | ||
if (patternCache[urlPattern]) { | ||
return patternCache[urlPattern]; | ||
} | ||
var compiledGenerator = compile(urlPattern); | ||
/* istanbul ignore else */ | ||
if (cacheCount < cacheLimit) { | ||
cacheCount += 1; | ||
patternCache[urlPattern] = compiledGenerator; | ||
} | ||
return compiledGenerator; | ||
} | ||
function generatePath(urlPattern, urlParams) { | ||
if (!urlPattern || !urlParams) { | ||
return urlPattern; | ||
} | ||
var generator = compileGenerator(urlPattern); | ||
return generator(urlParams); | ||
} | ||
function isAxiosError(error) { | ||
return "config" in error; | ||
} | ||
/** | ||
##### Initialization | ||
```javascript | ||
export function createHttpClient(logger) { | ||
return new HttpClient({ | ||
requestInterceptor(config) { | ||
logger.logRequest(config); | ||
}, | ||
* ##### Initialization | ||
* | ||
responseInterceptor(config, response) { | ||
logger.logResponse(config, response); | ||
}, | ||
* ```javascript | ||
* export function createHttpClient(logger) { | ||
* return new HttpClient({ | ||
* requestInterceptor(config) { | ||
* logger.logRequest(config); | ||
* }, | ||
* | ||
errorInterceptor(error) { | ||
logger.logError(error); | ||
}, | ||
}); | ||
} | ||
``` | ||
* responseInterceptor(config, response) { | ||
* logger.logResponse(config, response); | ||
* }, | ||
* | ||
* errorInterceptor(error) { | ||
* logger.logError(error); | ||
* }, | ||
* }); | ||
* } | ||
* ``` | ||
* | ||
* ##### Request | ||
* | ||
* ```javascript | ||
* const USER_URL = "/user/:userId"; | ||
* const USER_COMMENTS_URL = "/user/:userId/comments"; | ||
* | ||
* export function fetchUser(httpClient, userId) { | ||
* return httpClient.get(USER_URL, { | ||
* pathParams: { userId } | ||
* }); | ||
* } | ||
* | ||
* export function fetchUserPosts(httpClient, userId, page, limit) { | ||
* return httpClient.get(USER_COMMENTS_URL, { | ||
* url: USER_URL, | ||
* pathParams: { userId }, | ||
* queryParams: { page, limit } | ||
* }); | ||
* } | ||
* ``` | ||
*/ | ||
var HttpClient = | ||
/*#__PURE__*/ | ||
(function() { | ||
function HttpClient() { | ||
var options = | ||
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
##### Request | ||
```javascript | ||
const USER_URL = "/user/:userId"; | ||
const USER_COMMENTS_URL = "/user/:userId/comments"; | ||
_classCallCheck(this, HttpClient); | ||
export function fetchUser(httpClient, userId) { | ||
return httpClient.get(USER_URL, { | ||
pathParams: { userId } | ||
}); | ||
} | ||
this.options = options; | ||
this.client = axios.create(); | ||
} | ||
export function fetchUserPosts(httpClient, userId, page, limit) { | ||
return httpClient.get(USER_COMMENTS_URL, { | ||
url: USER_URL, | ||
pathParams: { userId }, | ||
queryParams: { page, limit } | ||
}); | ||
} | ||
``` | ||
*/ | ||
var HttpClient = /** @class */ (function() { | ||
function HttpClient(options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
this.options = options; | ||
this.client = axios.create(); | ||
} | ||
HttpClient.prototype.get = function(url, options) { | ||
return this.request(__assign({}, options, { url: url, method: "GET" })); | ||
}; | ||
HttpClient.prototype.post = function(url, options) { | ||
return this.request(__assign({}, options, { url: url, method: "POST" })); | ||
}; | ||
HttpClient.prototype.put = function(url, options) { | ||
return this.request(__assign({}, options, { url: url, method: "PUT" })); | ||
}; | ||
HttpClient.prototype.patch = function(url, options) { | ||
return this.request(__assign({}, options, { url: url, method: "PATCH" })); | ||
}; | ||
HttpClient.prototype.delete = function(url, options) { | ||
return this.request(__assign({}, options, { url: url, method: "DELETE" })); | ||
}; | ||
HttpClient.prototype.request = function(options) { | ||
var _this = this; | ||
var _a = this.options, | ||
shouldRetry = _a.shouldRetry, | ||
errorInterceptor = _a.errorInterceptor, | ||
requestInterceptor = _a.requestInterceptor, | ||
responseInterceptor = _a.responseInterceptor; | ||
var stream = new Observable(function(subscriber) { | ||
var cancelTokenSource = axios.CancelToken.source(); | ||
if (requestInterceptor) { | ||
requestInterceptor(options); | ||
} | ||
var config = { | ||
method: options.method, | ||
url: generatePath(options.url, options.pathParams), | ||
params: __assign({}, options.queryParams), | ||
data: options.data, | ||
headers: __assign({}, options.headers), | ||
timeout: options.timeout, | ||
cancelToken: cancelTokenSource.token | ||
}; | ||
// Fix for react-native in Android devices. | ||
if (config.timeout == null || !isFinite(config.timeout)) { | ||
delete config.timeout; | ||
} | ||
var complete = false; | ||
_this.client | ||
.request(config) | ||
.then(function(x) { | ||
var response = { | ||
data: x.data, | ||
status: x.status, | ||
headers: x.headers | ||
}; | ||
if (responseInterceptor) { | ||
responseInterceptor(options, response); | ||
} | ||
subscriber.next(response); | ||
}) | ||
.catch(function(x) { | ||
if (axios.isCancel(x)) { | ||
return; | ||
} | ||
var error = !isAxiosError(x) | ||
? x | ||
: createHttpClientError({ | ||
code: x.code, | ||
config: options, | ||
message: x.message, | ||
response: x.response | ||
}); | ||
if (errorInterceptor) { | ||
errorInterceptor(error); | ||
} | ||
subscriber.error(error); | ||
}) | ||
.then(function() { | ||
complete = true; | ||
subscriber.complete(); | ||
}); | ||
return function() { | ||
if (!complete) { | ||
cancelTokenSource.cancel(); | ||
_createClass(HttpClient, [ | ||
{ | ||
key: "get", | ||
value: function get(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "GET" | ||
}) | ||
); | ||
} | ||
}; | ||
}); | ||
return !shouldRetry | ||
? stream | ||
: stream.pipe( | ||
shareReplay(), | ||
retryWhen(function(errors) { | ||
return errors.pipe( | ||
scan(function(attempt, error) { | ||
if ( | ||
shouldRetry({ | ||
error: error, | ||
config: options, | ||
attempt: attempt | ||
}) | ||
) { | ||
return attempt + 1; | ||
}, | ||
{ | ||
key: "post", | ||
value: function post(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "POST" | ||
}) | ||
); | ||
} | ||
}, | ||
{ | ||
key: "put", | ||
value: function put(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "PUT" | ||
}) | ||
); | ||
} | ||
}, | ||
{ | ||
key: "patch", | ||
value: function patch(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "PATCH" | ||
}) | ||
); | ||
} | ||
}, | ||
{ | ||
key: "delete", | ||
value: function _delete(url, options) { | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url: url, | ||
method: "DELETE" | ||
}) | ||
); | ||
} | ||
}, | ||
{ | ||
key: "request", | ||
value: function request(options) { | ||
var _this = this; | ||
var _this$options = this.options, | ||
shouldRetry = _this$options.shouldRetry, | ||
errorInterceptor = _this$options.errorInterceptor, | ||
requestInterceptor = _this$options.requestInterceptor, | ||
responseInterceptor = _this$options.responseInterceptor; | ||
var stream = new Observable(function(subscriber) { | ||
var cancelTokenSource = axios.CancelToken.source(); | ||
if (requestInterceptor) { | ||
requestInterceptor(options); | ||
} | ||
var config = { | ||
method: options.method, | ||
url: generatePath(options.url, options.pathParams), | ||
params: _objectSpread({}, options.queryParams), | ||
data: options.data, | ||
headers: _objectSpread({}, options.headers), | ||
timeout: options.timeout, | ||
cancelToken: cancelTokenSource.token | ||
}; // Fix for react-native in Android devices. | ||
if (config.timeout == null || !isFinite(config.timeout)) { | ||
delete config.timeout; | ||
} | ||
var complete = false; | ||
_this.client | ||
.request(config) | ||
.then(function(x) { | ||
var response = { | ||
data: x.data, | ||
status: x.status, | ||
headers: x.headers | ||
}; | ||
if (responseInterceptor) { | ||
responseInterceptor(options, response); | ||
} | ||
throw error; | ||
}, 1) | ||
); | ||
}) | ||
); | ||
}; | ||
return HttpClient; | ||
})(); | ||
subscriber.next(response); | ||
}) | ||
.catch(function(x) { | ||
if (axios.isCancel(x)) { | ||
return; | ||
} | ||
var error = !isAxiosError(x) | ||
? x | ||
: createHttpClientError({ | ||
code: x.code, | ||
config: options, | ||
message: x.message, | ||
response: x.response | ||
}); | ||
if (errorInterceptor) { | ||
errorInterceptor(error); | ||
} | ||
subscriber.error(error); | ||
}) | ||
.then(function() { | ||
complete = true; | ||
subscriber.complete(); | ||
}); | ||
return function() { | ||
if (!complete) { | ||
cancelTokenSource.cancel(); | ||
} | ||
}; | ||
}); | ||
return !shouldRetry | ||
? stream | ||
: stream.pipe( | ||
shareReplay(), | ||
retryWhen(function(errors) { | ||
return errors.pipe( | ||
scan(function(attempt, error) { | ||
if ( | ||
shouldRetry({ | ||
error: error, | ||
config: options, | ||
attempt: attempt | ||
}) | ||
) { | ||
return attempt + 1; | ||
} | ||
throw error; | ||
}, 1) | ||
); | ||
}) | ||
); | ||
} | ||
} | ||
]); | ||
return HttpClient; | ||
})(); | ||
export { | ||
HttpClient, | ||
HttpStatus, | ||
HTTP_ERROR_TIMEOUT_CODE, | ||
createHttpClientError, | ||
isHttpClientError, | ||
getHttpClientErrorStatus, | ||
isHttpClientResponseError, | ||
isHttpClientTimeoutError, | ||
isHttpClientResponseError, | ||
getHttpClientErrorStatus, | ||
HTTP_ERROR_TIMEOUT_CODE | ||
HttpClient, | ||
HttpStatus | ||
}; |
import { compile } from "path-to-regexp"; | ||
import _objectSpread from "@babel/runtime/helpers/objectSpread"; | ||
import axios from "axios"; | ||
@@ -6,26 +7,4 @@ import { Observable } from "rxjs/_esm2015"; | ||
let cacheCount = 0; | ||
const cacheLimit = 10000; | ||
const patternCache = {}; | ||
function compileGenerator(urlPattern) { | ||
if (patternCache[urlPattern]) { | ||
return patternCache[urlPattern]; | ||
} | ||
const compiledGenerator = compile(urlPattern); | ||
/* istanbul ignore else */ | ||
if (cacheCount < cacheLimit) { | ||
cacheCount += 1; | ||
patternCache[urlPattern] = compiledGenerator; | ||
} | ||
return compiledGenerator; | ||
} | ||
function generatePath(urlPattern, urlParams) { | ||
if (!urlPattern || !urlParams) { | ||
return urlPattern; | ||
} | ||
const generator = compileGenerator(urlPattern); | ||
return generator(urlParams); | ||
} | ||
let HttpStatus; | ||
var HttpStatus; | ||
(function(HttpStatus) { | ||
@@ -83,45 +62,76 @@ HttpStatus[(HttpStatus["BadRequest"] = 400)] = "BadRequest"; | ||
function isAxiosError(error) { | ||
return "config" in error; | ||
} | ||
/** | ||
##### Initialization | ||
let cacheCount = 0; | ||
const cacheLimit = 10000; | ||
const patternCache = {}; | ||
```javascript | ||
export function createHttpClient(logger) { | ||
return new HttpClient({ | ||
requestInterceptor(config) { | ||
logger.logRequest(config); | ||
}, | ||
* | ||
responseInterceptor(config, response) { | ||
logger.logResponse(config, response); | ||
}, | ||
* | ||
errorInterceptor(error) { | ||
logger.logError(error); | ||
}, | ||
}); | ||
function compileGenerator(urlPattern) { | ||
if (patternCache[urlPattern]) { | ||
return patternCache[urlPattern]; | ||
} | ||
const compiledGenerator = compile(urlPattern); | ||
/* istanbul ignore else */ | ||
if (cacheCount < cacheLimit) { | ||
cacheCount += 1; | ||
patternCache[urlPattern] = compiledGenerator; | ||
} | ||
return compiledGenerator; | ||
} | ||
``` | ||
##### Request | ||
```javascript | ||
const USER_URL = "/user/:userId"; | ||
const USER_COMMENTS_URL = "/user/:userId/comments"; | ||
function generatePath(urlPattern, urlParams) { | ||
if (!urlPattern || !urlParams) { | ||
return urlPattern; | ||
} | ||
export function fetchUser(httpClient, userId) { | ||
return httpClient.get(USER_URL, { | ||
pathParams: { userId } | ||
}); | ||
const generator = compileGenerator(urlPattern); | ||
return generator(urlParams); | ||
} | ||
export function fetchUserPosts(httpClient, userId, page, limit) { | ||
return httpClient.get(USER_COMMENTS_URL, { | ||
url: USER_URL, | ||
pathParams: { userId }, | ||
queryParams: { page, limit } | ||
}); | ||
function isAxiosError(error) { | ||
return "config" in error; | ||
} | ||
``` | ||
/** | ||
* ##### Initialization | ||
* | ||
* ```javascript | ||
* export function createHttpClient(logger) { | ||
* return new HttpClient({ | ||
* requestInterceptor(config) { | ||
* logger.logRequest(config); | ||
* }, | ||
* | ||
* responseInterceptor(config, response) { | ||
* logger.logResponse(config, response); | ||
* }, | ||
* | ||
* errorInterceptor(error) { | ||
* logger.logError(error); | ||
* }, | ||
* }); | ||
* } | ||
* ``` | ||
* | ||
* ##### Request | ||
* | ||
* ```javascript | ||
* const USER_URL = "/user/:userId"; | ||
* const USER_COMMENTS_URL = "/user/:userId/comments"; | ||
* | ||
* export function fetchUser(httpClient, userId) { | ||
* return httpClient.get(USER_URL, { | ||
* pathParams: { userId } | ||
* }); | ||
* } | ||
* | ||
* export function fetchUserPosts(httpClient, userId, page, limit) { | ||
* return httpClient.get(USER_COMMENTS_URL, { | ||
* url: USER_URL, | ||
* pathParams: { userId }, | ||
* queryParams: { page, limit } | ||
* }); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -133,42 +143,75 @@ class HttpClient { | ||
} | ||
get(url, options) { | ||
return this.request(Object.assign({}, options, { url, method: "GET" })); | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url, | ||
method: "GET" | ||
}) | ||
); | ||
} | ||
post(url, options) { | ||
return this.request(Object.assign({}, options, { url, method: "POST" })); | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url, | ||
method: "POST" | ||
}) | ||
); | ||
} | ||
put(url, options) { | ||
return this.request(Object.assign({}, options, { url, method: "PUT" })); | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url, | ||
method: "PUT" | ||
}) | ||
); | ||
} | ||
patch(url, options) { | ||
return this.request(Object.assign({}, options, { url, method: "PATCH" })); | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url, | ||
method: "PATCH" | ||
}) | ||
); | ||
} | ||
delete(url, options) { | ||
return this.request(Object.assign({}, options, { url, method: "DELETE" })); | ||
return this.request( | ||
_objectSpread({}, options, { | ||
url, | ||
method: "DELETE" | ||
}) | ||
); | ||
} | ||
request(options) { | ||
const { | ||
shouldRetry, | ||
errorInterceptor, | ||
requestInterceptor, | ||
responseInterceptor | ||
} = this.options; | ||
const _this$options = this.options, | ||
shouldRetry = _this$options.shouldRetry, | ||
errorInterceptor = _this$options.errorInterceptor, | ||
requestInterceptor = _this$options.requestInterceptor, | ||
responseInterceptor = _this$options.responseInterceptor; | ||
const stream = new Observable(subscriber => { | ||
const cancelTokenSource = axios.CancelToken.source(); | ||
if (requestInterceptor) { | ||
requestInterceptor(options); | ||
} | ||
const config = { | ||
method: options.method, | ||
url: generatePath(options.url, options.pathParams), | ||
params: Object.assign({}, options.queryParams), | ||
params: _objectSpread({}, options.queryParams), | ||
data: options.data, | ||
headers: Object.assign({}, options.headers), | ||
headers: _objectSpread({}, options.headers), | ||
timeout: options.timeout, | ||
cancelToken: cancelTokenSource.token | ||
}; | ||
// Fix for react-native in Android devices. | ||
}; // Fix for react-native in Android devices. | ||
if (config.timeout == null || !isFinite(config.timeout)) { | ||
delete config.timeout; | ||
} | ||
let complete = false; | ||
@@ -183,5 +226,7 @@ this.client | ||
}; | ||
if (responseInterceptor) { | ||
responseInterceptor(options, response); | ||
} | ||
subscriber.next(response); | ||
@@ -193,2 +238,3 @@ }) | ||
} | ||
const error = !isAxiosError(x) | ||
@@ -202,5 +248,7 @@ ? x | ||
}); | ||
if (errorInterceptor) { | ||
errorInterceptor(error); | ||
} | ||
subscriber.error(error); | ||
@@ -225,5 +273,12 @@ }) | ||
scan((attempt, error) => { | ||
if (shouldRetry({ error, config: options, attempt })) { | ||
if ( | ||
shouldRetry({ | ||
error, | ||
config: options, | ||
attempt | ||
}) | ||
) { | ||
return attempt + 1; | ||
} | ||
throw error; | ||
@@ -238,10 +293,10 @@ }, 1) | ||
export { | ||
HttpClient, | ||
HttpStatus, | ||
HTTP_ERROR_TIMEOUT_CODE, | ||
createHttpClientError, | ||
isHttpClientError, | ||
getHttpClientErrorStatus, | ||
isHttpClientResponseError, | ||
isHttpClientTimeoutError, | ||
isHttpClientResponseError, | ||
getHttpClientErrorStatus, | ||
HTTP_ERROR_TIMEOUT_CODE | ||
HttpClient, | ||
HttpStatus | ||
}; |
{ | ||
"private": false, | ||
"license": "MIT", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"name": "@dc0de/http-client", | ||
@@ -24,3 +24,3 @@ "description": "Simple HttpClient built on top of Axios and RxJS", | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=8.3.0" | ||
}, | ||
@@ -34,6 +34,4 @@ "scripts": { | ||
"cover": "jest --coverage", | ||
"build": "yarn build:es && yarn build:cjs && yarn build:es2015 && yarn build:typings", | ||
"build:es": "rollup -c ./config/rollup.es.js", | ||
"build:cjs": "rollup -c ./config/rollup.cjs.js", | ||
"build:es2015": "rollup -c ./config/rollup.es2015.js", | ||
"build": "yarn build:bundle && yarn build:typings", | ||
"build:bundle": "rollup -c ./config/rollup.config.js", | ||
"build:typings": "rimraf typings && tsc --project tsconfig.typings.json", | ||
@@ -43,5 +41,5 @@ "codecov": "codecov -f ./coverage/lcov.info" | ||
"dependencies": { | ||
"@babel/runtime": "^7.0.0", | ||
"axios": "^0.18.0", | ||
"path-to-regexp": "^2.4.0", | ||
"tslib": "^1.9.0" | ||
"path-to-regexp": "^2.4.0" | ||
}, | ||
@@ -53,3 +51,5 @@ "peerDependencies": { | ||
"@babel/core": "^7.1.2", | ||
"@babel/plugin-transform-runtime": "^7.1.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"@babel/preset-typescript": "^7.1.0", | ||
"@dc0de/eslint-config": "^0.4.4", | ||
@@ -62,2 +62,3 @@ "@dc0de/eslint-config-base": "^0.4.1", | ||
"babel-jest": "^23.6.0", | ||
"babel-plugin-module-resolver": "^3.1.1", | ||
"codecov": "^3.1.0", | ||
@@ -74,6 +75,5 @@ "eslint": "^5.6.1", | ||
"rollup": "^0.66.6", | ||
"rollup-plugin-cleaner": "^0.2.0", | ||
"rollup-plugin-babel": "^4.0.3", | ||
"rollup-plugin-node-resolve": "^3.4.0", | ||
"rollup-plugin-prettier": "^0.4.0", | ||
"rollup-plugin-replace": "^2.1.0", | ||
"rollup-plugin-typescript2": "^0.17.1", | ||
"rxjs": "^6.3.3", | ||
@@ -80,0 +80,0 @@ "source-map-support": "^0.5.9", |
@@ -16,41 +16,42 @@ import { Observable } from "rxjs"; | ||
/** | ||
##### Initialization | ||
```javascript | ||
export function createHttpClient(logger) { | ||
return new HttpClient({ | ||
requestInterceptor(config) { | ||
logger.logRequest(config); | ||
}, | ||
* ##### Initialization | ||
* | ||
responseInterceptor(config, response) { | ||
logger.logResponse(config, response); | ||
}, | ||
* ```javascript | ||
* export function createHttpClient(logger) { | ||
* return new HttpClient({ | ||
* requestInterceptor(config) { | ||
* logger.logRequest(config); | ||
* }, | ||
* | ||
errorInterceptor(error) { | ||
logger.logError(error); | ||
}, | ||
}); | ||
} | ||
``` | ||
##### Request | ||
```javascript | ||
const USER_URL = "/user/:userId"; | ||
const USER_COMMENTS_URL = "/user/:userId/comments"; | ||
export function fetchUser(httpClient, userId) { | ||
return httpClient.get(USER_URL, { | ||
pathParams: { userId } | ||
}); | ||
} | ||
export function fetchUserPosts(httpClient, userId, page, limit) { | ||
return httpClient.get(USER_COMMENTS_URL, { | ||
url: USER_URL, | ||
pathParams: { userId }, | ||
queryParams: { page, limit } | ||
}); | ||
} | ||
``` | ||
* responseInterceptor(config, response) { | ||
* logger.logResponse(config, response); | ||
* }, | ||
* | ||
* errorInterceptor(error) { | ||
* logger.logError(error); | ||
* }, | ||
* }); | ||
* } | ||
* ``` | ||
* | ||
* ##### Request | ||
* | ||
* ```javascript | ||
* const USER_URL = "/user/:userId"; | ||
* const USER_COMMENTS_URL = "/user/:userId/comments"; | ||
* | ||
* export function fetchUser(httpClient, userId) { | ||
* return httpClient.get(USER_URL, { | ||
* pathParams: { userId } | ||
* }); | ||
* } | ||
* | ||
* export function fetchUserPosts(httpClient, userId, page, limit) { | ||
* return httpClient.get(USER_COMMENTS_URL, { | ||
* url: USER_URL, | ||
* pathParams: { userId }, | ||
* queryParams: { page, limit } | ||
* }); | ||
* } | ||
* ``` | ||
*/ | ||
@@ -57,0 +58,0 @@ export declare class HttpClient { |
@@ -1,7 +0,7 @@ | ||
export { HttpClient, HttpClientOptions } from "./http-client/HttpClient"; | ||
export { HttpStatus } from "./http-status/HttpStatus"; | ||
export { HttpClientError } from "./interfaces/HttpClientError"; | ||
export { HttpClientResponse } from "./interfaces/HttpClientResponse"; | ||
export { HttpClientRequestConfig, HttpClientFetchRequestConfig, HttpClientUpdateRequestConfig } from "./interfaces/HttpClientRequestConfig"; | ||
export { HttpClientRequestMethod } from "./interfaces/HttpClientRequestMethod"; | ||
export { createHttpClientError, isHttpClientError, isHttpClientTimeoutError, isHttpClientResponseError, getHttpClientErrorStatus, HTTP_ERROR_TIMEOUT_CODE } from "./utils/HttpErrorUtils"; | ||
export * from "./utils/HttpErrorUtils"; | ||
export * from "./http-client/HttpClient"; | ||
export * from "./http-status/HttpStatus"; | ||
export * from "./interfaces/HttpClientError"; | ||
export * from "./interfaces/HttpClientResponse"; | ||
export * from "./interfaces/HttpClientRequestConfig"; | ||
export * from "./interfaces/HttpClientRequestMethod"; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
38622
999
0
31
+ Added@babel/runtime@^7.0.0
+ Added@babel/runtime@7.25.6(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removedtslib@^1.9.0