rest-api-handler
Advanced tools
Comparing version 1.7.0 to 1.8.0
@@ -5,2 +5,8 @@ # Change Log | ||
## [1.8.0] 2018-05-31 | ||
### Added | ||
- Add option to define custom body decoder for DefaultResponseProcessor | ||
- Add option to get, post, put and delete request to define custom headers | ||
## [1.7.0] 2018-05-31 | ||
@@ -7,0 +13,0 @@ ### Added |
@@ -268,2 +268,3 @@ 'use strict'; | ||
* @param {Format} format - format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
@@ -275,5 +276,7 @@ */ | ||
value: function requestWithBody(namespace, method, data, format) { | ||
var headers = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format) | ||
}); | ||
}, headers); | ||
} | ||
@@ -286,2 +289,3 @@ | ||
* @param {Object} parameters - get parameters | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
@@ -299,4 +303,5 @@ * | ||
var parameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.request('' + namespace + Api.convertParametersToUrl(parameters), 'GET'); | ||
return this.request('' + namespace + Api.convertParametersToUrl(parameters), 'GET', {}, headers); | ||
} | ||
@@ -310,2 +315,3 @@ | ||
* @param {?Format} format - Format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -319,4 +325,5 @@ */ | ||
var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : JSON_FORMAT; | ||
var headers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
return this.requestWithBody(namespace, 'POST', data, format); | ||
return this.requestWithBody(namespace, 'POST', data, format, headers); | ||
} | ||
@@ -330,2 +337,3 @@ | ||
* @param {?Format} format - Format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -339,4 +347,5 @@ */ | ||
var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : JSON_FORMAT; | ||
var headers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
return this.requestWithBody(namespace, 'PUT', data, format); | ||
return this.requestWithBody(namespace, 'PUT', data, format, headers); | ||
} | ||
@@ -348,2 +357,3 @@ | ||
* @param {string} namespace - Api endpoint | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -355,3 +365,5 @@ */ | ||
value: function _delete(namespace) { | ||
return this.request(namespace, 'DELETE'); | ||
var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return this.request(namespace, 'DELETE', {}, headers); | ||
} | ||
@@ -358,0 +370,0 @@ }], [{ |
@@ -32,7 +32,9 @@ 'use strict'; | ||
* @param {Response} response - Native response. | ||
* @param {BodyDecoder} decoder - Custom body decoder. | ||
* @returns {Promise<ProcessedResponse>} Processed response from API. | ||
*/ | ||
function responseProcessor(response) { | ||
var decoder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : decodeResponse; | ||
function responseProcessor(response) { | ||
return decodeResponse(response).then(function (decodedResponse) { | ||
return decoder(response).then(function (decodedResponse) { | ||
// create custom response format | ||
@@ -88,7 +90,11 @@ var toRespond = { | ||
* @param {Class<ApiExceptionInterface>} Exception - Exception class that will be throwed if request fails. | ||
* @param {Function} decoder - Define custom response body decoder. | ||
*/ | ||
function DefaultResponseProcessor(Exception) { | ||
var decoder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : decodeResponse; | ||
classCallCheck(this, DefaultResponseProcessor); | ||
this.decoder = decodeResponse; | ||
this.Exception = Exception; | ||
this.decoder = decoder; | ||
this.processResponse = this.processResponse.bind(this); | ||
@@ -111,3 +117,3 @@ } | ||
return responseProcessor(response).catch(function (exception) { | ||
return responseProcessor(response, this.decoder).catch(function (exception) { | ||
if (exception.data && exception.status && exception.source) { | ||
@@ -114,0 +120,0 @@ throw new _this.Exception(exception, request); |
@@ -290,2 +290,3 @@ /** | ||
* @param {Format} format - format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
@@ -297,5 +298,7 @@ */ | ||
value: function requestWithBody(namespace, method, data, format) { | ||
var headers = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format) | ||
}); | ||
}, headers); | ||
} | ||
@@ -308,2 +311,3 @@ | ||
* @param {Object} parameters - get parameters | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
@@ -321,4 +325,5 @@ * | ||
var parameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.request('' + namespace + Api.convertParametersToUrl(parameters), 'GET'); | ||
return this.request('' + namespace + Api.convertParametersToUrl(parameters), 'GET', {}, headers); | ||
} | ||
@@ -332,2 +337,3 @@ | ||
* @param {?Format} format - Format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -341,4 +347,5 @@ */ | ||
var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : JSON_FORMAT; | ||
var headers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
return this.requestWithBody(namespace, 'POST', data, format); | ||
return this.requestWithBody(namespace, 'POST', data, format, headers); | ||
} | ||
@@ -352,2 +359,3 @@ | ||
* @param {?Format} format - Format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -361,4 +369,5 @@ */ | ||
var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : JSON_FORMAT; | ||
var headers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
return this.requestWithBody(namespace, 'PUT', data, format); | ||
return this.requestWithBody(namespace, 'PUT', data, format, headers); | ||
} | ||
@@ -370,2 +379,3 @@ | ||
* @param {string} namespace - Api endpoint | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -377,3 +387,5 @@ */ | ||
value: function _delete(namespace) { | ||
return this.request(namespace, 'DELETE'); | ||
var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return this.request(namespace, 'DELETE', {}, headers); | ||
} | ||
@@ -465,7 +477,9 @@ }], [{ | ||
* @param {Response} response - Native response. | ||
* @param {BodyDecoder} decoder - Custom body decoder. | ||
* @returns {Promise<ProcessedResponse>} Processed response from API. | ||
*/ | ||
function responseProcessor(response) { | ||
var decoder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : decodeResponse; | ||
function responseProcessor(response) { | ||
return decodeResponse(response).then(function (decodedResponse) { | ||
return decoder(response).then(function (decodedResponse) { | ||
// create custom response format | ||
@@ -497,7 +511,11 @@ var toRespond = { | ||
* @param {Class<ApiExceptionInterface>} Exception - Exception class that will be throwed if request fails. | ||
* @param {Function} decoder - Define custom response body decoder. | ||
*/ | ||
function DefaultResponseProcessor(Exception) { | ||
var decoder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : decodeResponse; | ||
classCallCheck(this, DefaultResponseProcessor); | ||
this.decoder = decodeResponse; | ||
this.Exception = Exception; | ||
this.decoder = decoder; | ||
this.processResponse = this.processResponse.bind(this); | ||
@@ -520,3 +538,3 @@ } | ||
return responseProcessor(response).catch(function (exception) { | ||
return responseProcessor(response, this.decoder).catch(function (exception) { | ||
if (exception.data && exception.status && exception.source) { | ||
@@ -523,0 +541,0 @@ throw new _this.Exception(exception, request); |
@@ -294,2 +294,3 @@ 'use strict'; | ||
* @param {Format} format - format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
@@ -301,5 +302,7 @@ */ | ||
value: function requestWithBody(namespace, method, data, format) { | ||
var headers = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format) | ||
}); | ||
}, headers); | ||
} | ||
@@ -312,2 +315,3 @@ | ||
* @param {Object} parameters - get parameters | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
@@ -325,4 +329,5 @@ * | ||
var parameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.request('' + namespace + Api.convertParametersToUrl(parameters), 'GET'); | ||
return this.request('' + namespace + Api.convertParametersToUrl(parameters), 'GET', {}, headers); | ||
} | ||
@@ -336,2 +341,3 @@ | ||
* @param {?Format} format - Format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -345,4 +351,5 @@ */ | ||
var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : JSON_FORMAT; | ||
var headers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
return this.requestWithBody(namespace, 'POST', data, format); | ||
return this.requestWithBody(namespace, 'POST', data, format, headers); | ||
} | ||
@@ -356,2 +363,3 @@ | ||
* @param {?Format} format - Format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -365,4 +373,5 @@ */ | ||
var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : JSON_FORMAT; | ||
var headers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
return this.requestWithBody(namespace, 'PUT', data, format); | ||
return this.requestWithBody(namespace, 'PUT', data, format, headers); | ||
} | ||
@@ -374,2 +383,3 @@ | ||
* @param {string} namespace - Api endpoint | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
@@ -381,3 +391,5 @@ */ | ||
value: function _delete(namespace) { | ||
return this.request(namespace, 'DELETE'); | ||
var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return this.request(namespace, 'DELETE', {}, headers); | ||
} | ||
@@ -469,7 +481,9 @@ }], [{ | ||
* @param {Response} response - Native response. | ||
* @param {BodyDecoder} decoder - Custom body decoder. | ||
* @returns {Promise<ProcessedResponse>} Processed response from API. | ||
*/ | ||
function responseProcessor(response) { | ||
var decoder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : decodeResponse; | ||
function responseProcessor(response) { | ||
return decodeResponse(response).then(function (decodedResponse) { | ||
return decoder(response).then(function (decodedResponse) { | ||
// create custom response format | ||
@@ -501,7 +515,11 @@ var toRespond = { | ||
* @param {Class<ApiExceptionInterface>} Exception - Exception class that will be throwed if request fails. | ||
* @param {Function} decoder - Define custom response body decoder. | ||
*/ | ||
function DefaultResponseProcessor(Exception) { | ||
var decoder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : decodeResponse; | ||
classCallCheck(this, DefaultResponseProcessor); | ||
this.decoder = decodeResponse; | ||
this.Exception = Exception; | ||
this.decoder = decoder; | ||
this.processResponse = this.processResponse.bind(this); | ||
@@ -524,3 +542,3 @@ } | ||
return responseProcessor(response).catch(function (exception) { | ||
return responseProcessor(response, this.decoder).catch(function (exception) { | ||
if (exception.data && exception.status && exception.source) { | ||
@@ -527,0 +545,0 @@ throw new _this.Exception(exception, request); |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/** | ||
@@ -32,7 +34,9 @@ * Decode API body response. | ||
* @param {Response} response - Native response. | ||
* @param {BodyDecoder} decoder - Custom body decoder. | ||
* @returns {Promise<ProcessedResponse>} Processed response from API. | ||
*/ | ||
function responseProcessor(response) { | ||
var decoder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : decodeResponse; | ||
function responseProcessor(response) { | ||
return decodeResponse(response).then(function (decodedResponse) { | ||
return decoder(response).then(function (decodedResponse) { | ||
// create custom response format | ||
@@ -55,2 +59,3 @@ var toRespond = { | ||
module.exports = responseProcessor; | ||
exports.decodeResponse = decodeResponse; | ||
exports.default = responseProcessor; |
{ | ||
"name": "rest-api-handler", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "Handler for REST APIs", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -12,3 +12,3 @@ // @flow | ||
*/ | ||
class Api<ProcessedResponse> { | ||
export default class Api<ProcessedResponse> { | ||
/** | ||
@@ -219,8 +219,9 @@ * Base api url | ||
* @param {Format} format - format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
*/ | ||
requestWithBody(namespace: string, method: MethodType, data: Object, format: Format): Promise<ProcessedResponse> { | ||
requestWithBody(namespace: string, method: MethodType, data: Object, format: Format, headers: Object = {}): Promise<ProcessedResponse> { | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format), | ||
}); | ||
}, headers); | ||
} | ||
@@ -233,2 +234,3 @@ | ||
* @param {Object} parameters - get parameters | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
@@ -241,4 +243,4 @@ * | ||
*/ | ||
get(namespace: string, parameters: Object = {}): Promise<ProcessedResponse> { | ||
return this.request(`${namespace}${Api.convertParametersToUrl(parameters)}`, 'GET'); | ||
get(namespace: string, parameters: Object = {}, headers: Object = {}): Promise<ProcessedResponse> { | ||
return this.request(`${namespace}${Api.convertParametersToUrl(parameters)}`, 'GET', {}, headers); | ||
} | ||
@@ -252,6 +254,7 @@ | ||
* @param {?Format} format - Format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
*/ | ||
post(namespace: string, data: Object = {}, format: Format = JSON_FORMAT): Promise<ProcessedResponse> { | ||
return this.requestWithBody(namespace, 'POST', data, format); | ||
post(namespace: string, data: Object = {}, format: Format = JSON_FORMAT, headers: Object = {}): Promise<ProcessedResponse> { | ||
return this.requestWithBody(namespace, 'POST', data, format, headers); | ||
} | ||
@@ -265,6 +268,7 @@ | ||
* @param {?Format} format - Format of body request | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
*/ | ||
put(namespace: string, data: Object = {}, format: Format = JSON_FORMAT): Promise<ProcessedResponse> { | ||
return this.requestWithBody(namespace, 'PUT', data, format); | ||
put(namespace: string, data: Object = {}, format: Format = JSON_FORMAT, headers: Object = {}): Promise<ProcessedResponse> { | ||
return this.requestWithBody(namespace, 'PUT', data, format, headers); | ||
} | ||
@@ -276,9 +280,8 @@ | ||
* @param {string} namespace - Api endpoint | ||
* @param {Object} headers - custom headers | ||
* @returns {Promise<ProcessedResponse>} Processed response | ||
*/ | ||
delete(namespace: string): Promise<ProcessedResponse> { | ||
return this.request(namespace, 'DELETE'); | ||
delete(namespace: string, headers: Object = {}): Promise<ProcessedResponse> { | ||
return this.request(namespace, 'DELETE', {}, headers); | ||
} | ||
} | ||
export default Api; |
@@ -9,3 +9,3 @@ // @flow | ||
*/ | ||
class DefaultApiException extends Error implements ApiExceptionInterface { | ||
export default class DefaultApiException extends Error implements ApiExceptionInterface { | ||
/** | ||
@@ -37,3 +37,1 @@ * Response from server that throwed an error. | ||
} | ||
export default DefaultApiException; |
// @flow | ||
import responseProcessor from './responseProcessor'; | ||
import type { ProcessedResponse } from './responseProcessor'; | ||
import responseProcessor, { decodeResponse } from './responseProcessor'; | ||
import type { ProcessedResponse, BodyDecoder } from './responseProcessor'; | ||
import type { ApiExceptionInterface } from './ApiExceptionInterface'; | ||
@@ -9,5 +9,6 @@ | ||
*/ | ||
class DefaultResponseProcessor { | ||
export default class DefaultResponseProcessor { | ||
Exception: Class<ApiExceptionInterface>; | ||
processResponse: (response: Response, request: Request) => Promise<ProcessedResponse>; | ||
decoder: BodyDecoder = decodeResponse; | ||
@@ -18,5 +19,7 @@ /** | ||
* @param {Class<ApiExceptionInterface>} Exception - Exception class that will be throwed if request fails. | ||
* @param {Function} decoder - Define custom response body decoder. | ||
*/ | ||
constructor(Exception: Class<ApiExceptionInterface>) { | ||
constructor(Exception: Class<ApiExceptionInterface>, decoder: BodyDecoder = decodeResponse) { | ||
this.Exception = Exception; | ||
this.decoder = decoder; | ||
this.processResponse = this.processResponse.bind(this); | ||
@@ -33,3 +36,3 @@ } | ||
processResponse(response: Response, request: Request): Promise<ProcessedResponse> { | ||
return responseProcessor(response) | ||
return responseProcessor(response, this.decoder) | ||
.catch((exception: *) => { | ||
@@ -44,3 +47,1 @@ if (exception.data && exception.status && exception.source) { | ||
} | ||
export default DefaultResponseProcessor; |
@@ -7,6 +7,7 @@ // @flow | ||
} | ||
export type BodyDecoder = (response: Response) => *; | ||
type DecodedStream = Blob | Object | string; | ||
export type ProcessedResponse = ApiResponseType<DecodedStream>; | ||
export type ProcessedResponse = ApiResponseType<*>; | ||
@@ -19,3 +20,3 @@ /** | ||
*/ | ||
function decodeResponse(response: Response): Promise<DecodedStream> { | ||
export function decodeResponse(response: Response): Promise<DecodedStream> { | ||
const contentType: ?string = response.headers.get('content-type'); | ||
@@ -43,7 +44,8 @@ | ||
* @param {Response} response - Native response. | ||
* @param {BodyDecoder} decoder - Custom body decoder. | ||
* @returns {Promise<ProcessedResponse>} Processed response from API. | ||
*/ | ||
export default function responseProcessor(response: Response): Promise<ProcessedResponse> { | ||
return decodeResponse(response) | ||
.then((decodedResponse: DecodedStream) => { | ||
export default function responseProcessor(response: Response, decoder: BodyDecoder = decodeResponse): Promise<ProcessedResponse> { | ||
return decoder(response) | ||
.then((decodedResponse: *) => { | ||
// create custom response format | ||
@@ -50,0 +52,0 @@ const toRespond: ProcessedResponse = { |
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
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
100262
2015