rest-api-handler
Advanced tools
Comparing version 2.20.0 to 2.21.0
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
## v2.21.0 (2021-07-18) | ||
### Changed | ||
- [dd68bb9](https://github.com/fabulator/rest-api-handler/commit/dd68bb9a250c0cc85b87f51a75f9fd6d60ff510b) Update dependencies | ||
- [71e9c33](https://github.com/fabulator/rest-api-handler/commit/71e9c33fd45779c42499e61fab7666808e6c01f5) Update dev dependency semantic-release-config-fabulator to v2 | ||
### Fixed | ||
- [4e4aa62](https://github.com/fabulator/rest-api-handler/commit/4e4aa627b682c95e779dab0e984779a9155159b1) Update lock for security updated | ||
- [84e1260](https://github.com/fabulator/rest-api-handler/commit/84e1260c9a7bf6b99933f6ef421f0614037db00d) Update lock for security updated | ||
- [b4b685c](https://github.com/fabulator/rest-api-handler/commit/b4b685c82b3c244c37b0f76db2faf91e5dc90f49) Update lock for security updated | ||
- [0ce5a2d](https://github.com/fabulator/rest-api-handler/commit/0ce5a2dcdcd5055584e3f6081f3670b17cd168c3) Update lock for security updated | ||
- [34a41cb](https://github.com/fabulator/rest-api-handler/commit/34a41cb2d956d12af1ccc72c20179051a72c5630) Update lock for security updated | ||
- [e27c682](https://github.com/fabulator/rest-api-handler/commit/e27c6822fd4900ad7791f4fb766fe409cd2c00a6) Update lock for security updated | ||
- [589ca0b](https://github.com/fabulator/rest-api-handler/commit/589ca0ba44aaaad5085ff83ae395ab453aa32e99) Update lock for security updated | ||
- [88553be](https://github.com/fabulator/rest-api-handler/commit/88553be778bcd6fba1f350168f899223bd06df16) Update lock for security updated | ||
- [c949543](https://github.com/fabulator/rest-api-handler/commit/c949543f0a1b85ba43e66908f92fdc1dbcdb2c9a) Update lock for security updated | ||
- [e929ef6](https://github.com/fabulator/rest-api-handler/commit/e929ef6492cf970070b1438eb672bce954fb9f2d) Update lock for security updated | ||
- [da5890a](https://github.com/fabulator/rest-api-handler/commit/da5890a283cd48f196d942dd76f2851cd2ef3d13) Update lock for security updated | ||
- [d22e9c0](https://github.com/fabulator/rest-api-handler/commit/d22e9c0e255867b6bdde71901ed6bebf9d82461a) Security lock update | ||
## [2.20.0] 2020-07-15 | ||
@@ -5,0 +24,0 @@ ### Changed |
module.exports = { | ||
extends: ['@socifi/commitlint-config'], | ||
extends: ['commitlint-config-fabulator'], | ||
}; |
106
dist/Api.js
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var _defineProperty = require('@babel/runtime/helpers/defineProperty'); | ||
var resolveProcessors = require('./resolveProcessors.js'); | ||
var dataFormats = require('./data-formats.js'); | ||
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty')); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
// processor can be instance of class | ||
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty); | ||
/** | ||
* Resolve given processor. | ||
* | ||
* @param response - Response to process. | ||
* @param list - Array of processors. | ||
* @param request - fetch request | ||
* @param i - Index of current processor. | ||
* @returns Processed response | ||
*/ | ||
async function resolveProcessors(response, list, request, i = 0) { | ||
const processor = list[i]; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
if (!processor) { | ||
return response; | ||
} | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty__default['default'](target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
const processedResponse = typeof processor === 'function' ? await processor(response, request) : await processor.processResponse(response, request); | ||
if (list[i + 1]) { | ||
return resolveProcessors(processedResponse, list, request, i + 1); | ||
} | ||
return processedResponse; | ||
} | ||
/** | ||
* @desc Types of formats of data you can send through body of Fetch request. | ||
*/ | ||
/** | ||
* @desc Json is object converted to string. It is default format in a library. | ||
*/ | ||
const JSON$1 = 'json'; | ||
/** | ||
* @desc Form Data can be used to send images. | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FormData | ||
*/ | ||
const FORM_DATA = 'formdata'; | ||
/** | ||
* @desc Url encoded data in body | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST | ||
*/ | ||
const URL_ENCODED = 'urlencoded'; | ||
var FORMATS = ({ | ||
__proto__: null, | ||
JSON: JSON$1, | ||
FORM_DATA: FORM_DATA, | ||
URL_ENCODED: URL_ENCODED | ||
}); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
/** | ||
* Class for handling responses and requests. | ||
@@ -99,10 +48,6 @@ */ | ||
constructor(apiUrl, processors = [], defaultHeaders = {}, defaultOptions = {}) { | ||
_defineProperty(this, "apiUrl", void 0); | ||
_defineProperty(this, "defaultHeaders", void 0); | ||
_defineProperty(this, "defaultOptions", void 0); | ||
_defineProperty(this, "processors", void 0); | ||
this.apiUrl = void 0; | ||
this.defaultHeaders = void 0; | ||
this.defaultOptions = void 0; | ||
this.processors = void 0; | ||
this.apiUrl = apiUrl; | ||
@@ -126,4 +71,4 @@ this.defaultHeaders = defaultHeaders; | ||
static convertData(data, to = JSON$1) { | ||
if (to === FORM_DATA) { | ||
static convertData(data, to = dataFormats.JSON) { | ||
if (to === dataFormats.FORM_DATA) { | ||
const formData = new FormData(); | ||
@@ -136,3 +81,3 @@ Object.entries(data).forEach(([key, value]) => { | ||
if (to === URL_ENCODED) { | ||
if (to === dataFormats.URL_ENCODED) { | ||
return Api.convertParametersToUrl(data).slice(1); | ||
@@ -162,5 +107,5 @@ } | ||
return "?" + keys.map(key => { | ||
return key + "=" + encodeURIComponent(parameters[key]); | ||
}).join('&'); | ||
return `?${keys.map(key => { | ||
return `${key}=${encodeURIComponent(parameters[key])}`; | ||
}).join('&')}`; | ||
} | ||
@@ -240,8 +185,10 @@ /** | ||
async request(namespace, method, options = {}, headers = {}) { | ||
const urlToRequest = namespace.indexOf('http') === 0 ? namespace : this.apiUrl + "/" + namespace; // eslint-disable-next-line compat/compat | ||
const urlToRequest = namespace.indexOf('http') === 0 ? namespace : `${this.apiUrl}/${namespace}`; // eslint-disable-next-line compat/compat | ||
const request = new Request(urlToRequest, _objectSpread({}, this.defaultOptions, { | ||
const request = new Request(urlToRequest, _objectSpread(_objectSpread({}, this.defaultOptions), {}, { | ||
method, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
headers: new Headers(_objectSpread({}, this.getDefaultHeaders(), {}, headers)) | ||
// eslint-disable-next-line compat/compat | ||
headers: new Headers(_objectSpread(_objectSpread({}, this.getDefaultHeaders()), headers)) | ||
}, options)); | ||
@@ -285,3 +232,3 @@ const response = await this.fetchRequest(request); | ||
get(namespace, parameters = {}, headers = {}) { | ||
return this.request("" + namespace + Api.convertParametersToUrl(parameters), 'GET', {}, headers); | ||
return this.request(`${namespace}${Api.convertParametersToUrl(parameters)}`, 'GET', {}, headers); | ||
} | ||
@@ -299,3 +246,3 @@ /** | ||
post(namespace, data = {}, format = JSON$1, headers = {}) { | ||
post(namespace, data = {}, format = dataFormats.JSON, headers = {}) { | ||
return this.requestWithBody(namespace, 'POST', data, format, headers); | ||
@@ -314,3 +261,3 @@ } | ||
put(namespace, data = {}, format = JSON$1, headers = {}) { | ||
put(namespace, data = {}, format = dataFormats.JSON, headers = {}) { | ||
return this.requestWithBody(namespace, 'PUT', data, format, headers); | ||
@@ -332,5 +279,4 @@ } | ||
} | ||
Api.FORMATS = dataFormats; | ||
_defineProperty(Api, "FORMATS", FORMATS); | ||
module.exports = Api; |
@@ -6,4 +6,4 @@ import { ApiResponseType } from './DefaultResponseProcessor'; | ||
export interface ApiExceptionInterface<ResponseType> { | ||
getRequest: () => Request; | ||
getResponse: () => ApiResponseType<ResponseType>; | ||
getRequest: () => Request; | ||
} | ||
@@ -10,0 +10,0 @@ /** |
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty')); | ||
/* eslint-disable no-proto */ | ||
@@ -23,6 +19,4 @@ | ||
constructor(response) { | ||
super("Api exception: " + JSON.stringify(response.data)); | ||
_defineProperty(this, "response", void 0); | ||
super(`Api exception: ${JSON.stringify(response.data)}`); | ||
this.response = void 0; | ||
this.response = response; | ||
@@ -29,0 +23,0 @@ } |
@@ -5,5 +5,5 @@ import { ApiExceptionConstructor } from './DefaultApiException'; | ||
data: Respond; | ||
request: Request; | ||
source: Response; | ||
status: number; | ||
source: Response; | ||
request: Request; | ||
} | ||
@@ -10,0 +10,0 @@ /** |
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty')); | ||
/** | ||
@@ -18,6 +14,4 @@ * Processor provider that process response from API and throw custom Exception. | ||
constructor(Exception, decoder) { | ||
_defineProperty(this, "Exception", void 0); | ||
_defineProperty(this, "decoder", void 0); | ||
this.Exception = void 0; | ||
this.decoder = void 0; | ||
this.Exception = Exception; | ||
@@ -24,0 +18,0 @@ this.decoder = decoder || DefaultResponseProcessor.decodeResponse; |
@@ -5,416 +5,10 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var Api = require('./Api.js'); | ||
var DefaultResponseProcessor = require('./DefaultResponseProcessor.js'); | ||
var DefaultApiException = require('./DefaultApiException.js'); | ||
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty')); | ||
// processor can be instance of class | ||
/** | ||
* Resolve given processor. | ||
* | ||
* @param response - Response to process. | ||
* @param list - Array of processors. | ||
* @param request - fetch request | ||
* @param i - Index of current processor. | ||
* @returns Processed response | ||
*/ | ||
async function resolveProcessors(response, list, request, i = 0) { | ||
const processor = list[i]; | ||
if (!processor) { | ||
return response; | ||
} | ||
const processedResponse = typeof processor === 'function' ? await processor(response, request) : await processor.processResponse(response, request); | ||
if (list[i + 1]) { | ||
return resolveProcessors(processedResponse, list, request, i + 1); | ||
} | ||
return processedResponse; | ||
} | ||
/** | ||
* @desc Types of formats of data you can send through body of Fetch request. | ||
*/ | ||
/** | ||
* @desc Json is object converted to string. It is default format in a library. | ||
*/ | ||
const JSON$1 = 'json'; | ||
/** | ||
* @desc Form Data can be used to send images. | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FormData | ||
*/ | ||
const FORM_DATA = 'formdata'; | ||
/** | ||
* @desc Url encoded data in body | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST | ||
*/ | ||
const URL_ENCODED = 'urlencoded'; | ||
var FORMATS = ({ | ||
__proto__: null, | ||
JSON: JSON$1, | ||
FORM_DATA: FORM_DATA, | ||
URL_ENCODED: URL_ENCODED | ||
}); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
/** | ||
* Class for handling responses and requests. | ||
*/ | ||
class Api { | ||
/** | ||
* Base api url | ||
*/ | ||
/** | ||
* Base http headers | ||
*/ | ||
/** | ||
* Base settings for Fetch Request | ||
*/ | ||
/** | ||
* List of processors that parse response from server. | ||
*/ | ||
/** | ||
* List of formatter you can use to process content of body request. | ||
*/ | ||
/** | ||
* Constructor. | ||
* | ||
* @param apiUrl - Base api url | ||
* @param processors - List of processors that parse response from server. | ||
* @param defaultHeaders - Base settings for Fetch Request | ||
* @param defaultOptions - List of processors that parse response from server. | ||
*/ | ||
constructor(apiUrl, processors = [], defaultHeaders = {}, defaultOptions = {}) { | ||
_defineProperty(this, "apiUrl", void 0); | ||
_defineProperty(this, "defaultHeaders", void 0); | ||
_defineProperty(this, "defaultOptions", void 0); | ||
_defineProperty(this, "processors", void 0); | ||
this.apiUrl = apiUrl; | ||
this.defaultHeaders = defaultHeaders; | ||
this.defaultOptions = defaultOptions; | ||
this.processors = processors; | ||
} | ||
/** | ||
* Convert data in object to format of Fetch body. | ||
* | ||
* @param data - Data to convert | ||
* @param to - Format to which convert the data. Default is JSON. | ||
* @returns Converted data | ||
* | ||
* @example | ||
* const body = Api.convertData({ a: 'b' }, Api.FORMATS.JSON); | ||
* // output is {"a":"b"} | ||
*/ | ||
static convertData(data, to = JSON$1) { | ||
if (to === FORM_DATA) { | ||
const formData = new FormData(); | ||
Object.entries(data).forEach(([key, value]) => { | ||
formData.append(key, value); | ||
}); | ||
return formData; | ||
} | ||
if (to === URL_ENCODED) { | ||
return Api.convertParametersToUrl(data).slice(1); | ||
} | ||
return JSON.stringify(data); | ||
} | ||
/** | ||
* Convert object to url parameters string. | ||
* | ||
* @param parameters - List of parameters | ||
* @returns Encoded string with ? prefix and variables separated by & | ||
* | ||
* @example | ||
* const parameters = Api.convertData({ a: '%b%' }); | ||
* // output is ?a=%25b%25 | ||
*/ | ||
static convertParametersToUrl(parameters) { | ||
const keys = Object.keys(parameters); | ||
if (keys.length === 0) { | ||
return ''; | ||
} | ||
return "?" + keys.map(key => { | ||
return key + "=" + encodeURIComponent(parameters[key]); | ||
}).join('&'); | ||
} | ||
/** | ||
* Set default headers. | ||
* | ||
* @param headers - HTTP headers | ||
*/ | ||
setDefaultHeaders(headers) { | ||
this.defaultHeaders = headers; | ||
} | ||
/** | ||
* Add default HTTP header. | ||
* | ||
* @param name - Name of header | ||
* @param value - Value for header | ||
* @example | ||
* api.setDefaultHeader('content-type', 'application/json'); | ||
*/ | ||
setDefaultHeader(name, value) { | ||
this.defaultHeaders[name] = value; | ||
} | ||
/** | ||
* Remove default header. | ||
* | ||
* @param name - Name of header | ||
*/ | ||
removeDefaultHeader(name) { | ||
delete this.defaultHeaders[name]; | ||
} | ||
/** | ||
* Get default headers. | ||
* | ||
* @returns Get Default headers | ||
*/ | ||
getDefaultHeaders() { | ||
return this.defaultHeaders; | ||
} | ||
/** | ||
* Fetch API url. | ||
* | ||
* @protected | ||
* @param request - Fetch request | ||
* @returns Fetch response | ||
*/ | ||
fetchRequest(request) { | ||
return fetch(request); | ||
} | ||
/** | ||
* Request given API endpoint. | ||
* | ||
* @param namespace - Api endpoint or full url | ||
* @param method - Request method eg. POST or GET | ||
* @param options - Fetch options | ||
* @param headers - Custom headers | ||
* @returns processed response | ||
* @example | ||
* const { data } = await api.request('ad', 'POST', { | ||
* body: '{"ad":1}' | ||
* }) | ||
* | ||
* const { data } = await api.request('http://i-can-request-full-url.com/?a=b', 'GET') | ||
*/ | ||
async request(namespace, method, options = {}, headers = {}) { | ||
const urlToRequest = namespace.indexOf('http') === 0 ? namespace : this.apiUrl + "/" + namespace; // eslint-disable-next-line compat/compat | ||
const request = new Request(urlToRequest, _objectSpread({}, this.defaultOptions, { | ||
method, | ||
// @ts-ignore | ||
headers: new Headers(_objectSpread({}, this.getDefaultHeaders(), {}, headers)) | ||
}, options)); | ||
const response = await this.fetchRequest(request); | ||
return resolveProcessors(response, this.processors, request); | ||
} | ||
/** | ||
* Send a request with body. | ||
* | ||
* @protected | ||
* @param namespace - api endpoint | ||
* @param method - api method | ||
* @param data - body JSON parameters | ||
* @param format - format of body request | ||
* @param headers - custom headers | ||
* @returns processed response | ||
*/ | ||
requestWithBody(namespace, method, data, format, headers = {}) { | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format) | ||
}, headers); | ||
} | ||
/** | ||
* Send a GET request. | ||
* | ||
* @param namespace - api endpoint | ||
* @param parameters - get parameters | ||
* @param headers - custom headers | ||
* @returns processed response | ||
* | ||
* @example | ||
* const { data } = await api.get('brand', { id: 5 }) | ||
* // will call YOUR_URI/brand?id=5 | ||
* console.log(data); | ||
*/ | ||
get(namespace, parameters = {}, headers = {}) { | ||
return this.request("" + namespace + Api.convertParametersToUrl(parameters), 'GET', {}, headers); | ||
} | ||
/** | ||
* Send a POST request. | ||
* | ||
* @param namespace - Api endpoint | ||
* @param data - Request object | ||
* @param format - Format of body request | ||
* @param headers - custom headers | ||
* @returns Processed response | ||
*/ | ||
post(namespace, data = {}, format = JSON$1, headers = {}) { | ||
return this.requestWithBody(namespace, 'POST', data, format, headers); | ||
} | ||
/** | ||
* Send a PUT request. | ||
* | ||
* @param namespace - Api endpoint | ||
* @param data - Request object | ||
* @param format - Format of body request | ||
* @param headers - custom headers | ||
* @returns Processed response | ||
*/ | ||
put(namespace, data = {}, format = JSON$1, headers = {}) { | ||
return this.requestWithBody(namespace, 'PUT', data, format, headers); | ||
} | ||
/** | ||
* Send a DELETE request. | ||
* | ||
* @param namespace - Api endpoint | ||
* @param headers - custom headers | ||
* @returns Processed response | ||
*/ | ||
delete(namespace, headers = {}) { | ||
return this.request(namespace, 'DELETE', {}, headers); | ||
} | ||
} | ||
_defineProperty(Api, "FORMATS", FORMATS); | ||
/** | ||
* Processor provider that process response from API and throw custom Exception. | ||
*/ | ||
class DefaultResponseProcessor { | ||
/** | ||
* Constructor. | ||
* | ||
* @param Exception - Exception class that will be throwed if request fails. | ||
* @param decoder - Define custom response body decoder. | ||
*/ | ||
constructor(Exception, decoder) { | ||
_defineProperty(this, "Exception", void 0); | ||
_defineProperty(this, "decoder", void 0); | ||
this.Exception = Exception; | ||
this.decoder = decoder || DefaultResponseProcessor.decodeResponse; | ||
} | ||
async processResponse(response, request) { | ||
const decodedResponse = await this.decoder(response); | ||
const toRespond = { | ||
data: decodedResponse, | ||
status: response.status, | ||
source: response, | ||
request | ||
}; | ||
if (!response.ok) { | ||
throw new this.Exception(toRespond, request); | ||
} | ||
return toRespond; | ||
} | ||
static decodeResponse(response) { | ||
const contentType = response.headers.get('content-type'); // on default decode response as text | ||
if (!contentType) { | ||
return response.text(); | ||
} | ||
if (contentType.includes('json')) { | ||
return response.json(); | ||
} | ||
if (contentType.includes('text') || contentType.includes('xml')) { | ||
return response.text(); | ||
} | ||
return response.blob(); | ||
} | ||
} | ||
/* eslint-disable no-proto */ | ||
/** | ||
* Default API Exception | ||
*/ | ||
class DefaultApiException extends Error { | ||
/** | ||
* Response from server that throwed an error. | ||
*/ | ||
/** | ||
* Constructor. | ||
* | ||
* @param response - Processed response from server. | ||
*/ | ||
constructor(response) { | ||
super("Api exception: " + JSON.stringify(response.data)); | ||
_defineProperty(this, "response", void 0); | ||
this.response = response; | ||
} | ||
getResponse() { | ||
return this.response; | ||
} | ||
getRequest() { | ||
return this.response.request; | ||
} | ||
} | ||
exports.Api = Api; | ||
exports.DefaultResponseProcessor = DefaultResponseProcessor; | ||
exports.DefaultApiException = DefaultApiException; | ||
exports.DefaultResponseProcessor = DefaultResponseProcessor; |
{ | ||
"name": "rest-api-handler", | ||
"version": "2.20.0", | ||
"version": "2.21.0", | ||
"description": "Handler for REST APIs", | ||
"main": "dist/index.js", | ||
"jsnext:main": "dist/index.esm.js", | ||
"module": "dist/index.esm.js", | ||
"module": "es/index.js", | ||
"jsnext:main": "es/index.js", | ||
"types": "dist/index.d.ts", | ||
@@ -25,12 +25,13 @@ "repository": { | ||
"devDependencies": { | ||
"@socifi/babel-config": "^1.0.0", | ||
"@socifi/commitlint-config": "^1.1.0", | ||
"@socifi/eslint-config": "^2.10.0", | ||
"@socifi/jest-config": "^2.4.1", | ||
"@socifi/rollup-config": "^2.2.2", | ||
"cross-fetch": "^3.0.4", | ||
"semantic-release-config-fabulator": "^1.3.2" | ||
"babel-config-fabulator": "^1.2.0", | ||
"commitlint-config-fabulator": "^2.0.1", | ||
"eslint-config-fabulator": "^1.2.2", | ||
"jest-config-fabulator": "^1.1.0", | ||
"rollup-config-fabulator": "^1.1.0", | ||
"cross-fetch": "^3.1.4", | ||
"semantic-release-config-fabulator": "^2.6.2" | ||
}, | ||
"scripts": { | ||
"lint": "eslint --ext .ts ./src ./tests", | ||
"lint:fix": "npm run lint -- --fix", | ||
"tsc": "tsc", | ||
@@ -45,4 +46,4 @@ "test": "jest", | ||
"dependencies": { | ||
"@babel/runtime": "^7.6.3" | ||
"@babel/runtime": "^7.14.6" | ||
} | ||
} |
@@ -6,3 +6,5 @@ import resolveProcessors, { ProcessorAdapter } from './resolveProcessors'; | ||
interface HeadersParameters {[key: string]: string | number} | ||
interface HeadersParameters { | ||
[key: string]: string | number; | ||
} | ||
@@ -69,3 +71,3 @@ /** | ||
*/ | ||
public static convertData(data: {[key: string]: any}, to: FORMATS.Format = FORMATS.JSON): string | FormData { | ||
public static convertData(data: { [key: string]: any }, to: FORMATS.Format = FORMATS.JSON): string | FormData { | ||
if (to === FORMATS.FORM_DATA) { | ||
@@ -96,3 +98,3 @@ const formData = new FormData(); | ||
*/ | ||
public static convertParametersToUrl(parameters: {[key: string]: any}): string { | ||
public static convertParametersToUrl(parameters: { [key: string]: any }): string { | ||
const keys = Object.keys(parameters); | ||
@@ -104,5 +106,7 @@ | ||
return `?${keys.map((key: string) => { | ||
return `${key}=${encodeURIComponent(parameters[key])}`; | ||
}).join('&')}`; | ||
return `?${keys | ||
.map((key: string) => { | ||
return `${key}=${encodeURIComponent(parameters[key])}`; | ||
}) | ||
.join('&')}`; | ||
} | ||
@@ -187,3 +191,5 @@ | ||
method, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line compat/compat | ||
headers: new Headers({ | ||
@@ -219,5 +225,10 @@ ...this.getDefaultHeaders(), | ||
): Promise<ProcessedResponse> { | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format), | ||
}, headers); | ||
return this.request( | ||
namespace, | ||
method, | ||
{ | ||
body: Api.convertData(data, format), | ||
}, | ||
headers, | ||
); | ||
} | ||
@@ -224,0 +235,0 @@ |
@@ -5,8 +5,9 @@ /* eslint-disable no-proto */ | ||
export interface ApiExceptionConstructor<ResponseType> { | ||
new (response: ApiResponseType<ResponseType>, request: Request): ApiExceptionInterface<ResponseType>, | ||
// eslint-disable-next-line no-use-before-define | ||
new (response: ApiResponseType<ResponseType>, request: Request): ApiExceptionInterface<ResponseType>; | ||
} | ||
export interface ApiExceptionInterface<ResponseType> { | ||
getResponse: () => ApiResponseType<ResponseType>, | ||
getRequest: () => Request, | ||
getRequest: () => Request; | ||
getResponse: () => ApiResponseType<ResponseType>; | ||
} | ||
@@ -13,0 +14,0 @@ |
@@ -6,6 +6,6 @@ import { ApiExceptionConstructor } from './DefaultApiException'; | ||
export interface ApiResponseType<Respond = any> { | ||
data: Respond, | ||
status: number, | ||
source: Response, | ||
request: Request, | ||
data: Respond; | ||
request: Request; | ||
source: Response; | ||
status: number; | ||
} | ||
@@ -12,0 +12,0 @@ |
// processor can be instance of class | ||
type Processor<Input = any, Output = any> = (response: Input, request: Request) => Promise<Output>; | ||
export type ProcessorAdapter<Input = any, Output = any> = { | ||
processResponse: Processor<Input, Output>, | ||
} | Processor<Input, Output>; | ||
export type ProcessorAdapter<Input = any, Output = any> = | ||
| { | ||
processResponse: Processor<Input, Output>; | ||
} | ||
| Processor<Input, Output>; | ||
@@ -29,5 +31,4 @@ /** | ||
const processedResponse = typeof processor === 'function' | ||
? await processor(response, request) | ||
: await processor.processResponse(response, request); | ||
const processedResponse = | ||
typeof processor === 'function' ? await processor(response, request) : await processor.processResponse(response, request); | ||
@@ -34,0 +35,0 @@ if (list[i + 1]) { |
2
71267
1379
Updated@babel/runtime@^7.14.6