rest-api-handler
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -5,2 +5,7 @@ # Change Log | ||
## [1.5.0] 2018-04-09 | ||
### Added | ||
- Add new method fetchRequest that handles fetch API. It can be easily overrrided by its children. | ||
## [1.4.1] 2018-03-28 | ||
@@ -7,0 +12,0 @@ ### Changed |
158
dist/Api.js
@@ -35,119 +35,2 @@ 'use strict'; | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
} | ||
}; | ||
}(); | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -270,2 +153,15 @@ if (!(instance instanceof Constructor)) { | ||
/** | ||
* Fetch API url. | ||
* | ||
* @param {Request} request - fetch request | ||
* @returns {Promise<Response>} fetch response | ||
*/ | ||
}, { | ||
key: 'fetchRequest', | ||
value: function fetchRequest(request) { | ||
return fetch(request); | ||
} | ||
/** | ||
* Request given API endpoint. | ||
@@ -295,3 +191,3 @@ * | ||
return fetch(request).then(function (response) { | ||
return this.fetchRequest(request).then(function (response) { | ||
return resolveArray(response, _this.processors, request); | ||
@@ -318,2 +214,20 @@ }); | ||
/** | ||
* Send a request with body. | ||
* | ||
* @param {string} namespace - api endpoint | ||
* @param {MethodType} method - api method | ||
* @param {Object} data - body JSON parameters | ||
* @param {Format} format - format of body request | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
*/ | ||
}, { | ||
key: 'requestWithBody', | ||
value: function requestWithBody(namespace, method, data, format) { | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format) | ||
}); | ||
} | ||
/** | ||
* Send a POST request. | ||
@@ -333,5 +247,3 @@ * | ||
return this.request(namespace, 'POST', { | ||
body: Api.convertData(data, format) | ||
}); | ||
return this.requestWithBody(namespace, 'POST', data, format); | ||
} | ||
@@ -354,5 +266,3 @@ | ||
return this.request(namespace, 'PUT', { | ||
body: Api.convertData(data, format) | ||
}); | ||
return this.requestWithBody(namespace, 'PUT', data, format); | ||
} | ||
@@ -359,0 +269,0 @@ |
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
} | ||
}; | ||
}(); | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -180,26 +53,2 @@ if (!(instance instanceof Constructor)) { | ||
var _fixBabelExtend = function (O) { | ||
var gPO = O.getPrototypeOf || function (o) { | ||
return o.__proto__; | ||
}, | ||
sPO = O.setPrototypeOf || function (o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}, | ||
construct = (typeof Reflect === 'undefined' ? 'undefined' : _typeof(Reflect)) === 'object' ? Reflect.construct : function (Parent, args, Class) { | ||
var Constructor, | ||
a = [null]; | ||
a.push.apply(a, args); | ||
Constructor = Parent.bind.apply(Parent, a); | ||
return sPO(new Constructor(), Class.prototype); | ||
}; | ||
return function fixBabelExtend(Class) { | ||
var Parent = gPO(Class); | ||
return sPO(Class, sPO(function Super() { | ||
return construct(Parent, arguments, gPO(this).constructor); | ||
}, Parent)); | ||
}; | ||
}(Object); | ||
/** | ||
@@ -210,3 +59,3 @@ * Default API Exception | ||
/* eslint-disable no-proto */ | ||
var DefaultApiException = _fixBabelExtend(function (_Error) { | ||
var DefaultApiException = function (_Error) { | ||
inherits(DefaultApiException, _Error); | ||
@@ -235,4 +84,4 @@ | ||
return DefaultApiException; | ||
}(Error)); | ||
}(Error); | ||
module.exports = DefaultApiException; |
@@ -49,119 +49,2 @@ 'use strict'; | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
} | ||
}; | ||
}(); | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -168,0 +51,0 @@ if (!(instance instanceof Constructor)) { |
@@ -33,129 +33,2 @@ /** | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
} | ||
}; | ||
}(); | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -314,2 +187,15 @@ if (!(instance instanceof Constructor)) { | ||
/** | ||
* Fetch API url. | ||
* | ||
* @param {Request} request - fetch request | ||
* @returns {Promise<Response>} fetch response | ||
*/ | ||
}, { | ||
key: 'fetchRequest', | ||
value: function fetchRequest(request) { | ||
return fetch(request); | ||
} | ||
/** | ||
* Request given API endpoint. | ||
@@ -339,3 +225,3 @@ * | ||
return fetch(request).then(function (response) { | ||
return this.fetchRequest(request).then(function (response) { | ||
return resolveArray(response, _this.processors, request); | ||
@@ -362,2 +248,20 @@ }); | ||
/** | ||
* Send a request with body. | ||
* | ||
* @param {string} namespace - api endpoint | ||
* @param {MethodType} method - api method | ||
* @param {Object} data - body JSON parameters | ||
* @param {Format} format - format of body request | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
*/ | ||
}, { | ||
key: 'requestWithBody', | ||
value: function requestWithBody(namespace, method, data, format) { | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format) | ||
}); | ||
} | ||
/** | ||
* Send a POST request. | ||
@@ -377,5 +281,3 @@ * | ||
return this.request(namespace, 'POST', { | ||
body: Api.convertData(data, format) | ||
}); | ||
return this.requestWithBody(namespace, 'POST', data, format); | ||
} | ||
@@ -398,5 +300,3 @@ | ||
return this.request(namespace, 'PUT', { | ||
body: Api.convertData(data, format) | ||
}); | ||
return this.requestWithBody(namespace, 'PUT', data, format); | ||
} | ||
@@ -552,26 +452,2 @@ | ||
var _fixBabelExtend = function (O) { | ||
var gPO = O.getPrototypeOf || function (o) { | ||
return o.__proto__; | ||
}, | ||
sPO = O.setPrototypeOf || function (o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}, | ||
construct = (typeof Reflect === 'undefined' ? 'undefined' : _typeof(Reflect)) === 'object' ? Reflect.construct : function (Parent, args, Class) { | ||
var Constructor, | ||
a = [null]; | ||
a.push.apply(a, args); | ||
Constructor = Parent.bind.apply(Parent, a); | ||
return sPO(new Constructor(), Class.prototype); | ||
}; | ||
return function fixBabelExtend(Class) { | ||
var Parent = gPO(Class); | ||
return sPO(Class, sPO(function Super() { | ||
return construct(Parent, arguments, gPO(this).constructor); | ||
}, Parent)); | ||
}; | ||
}(Object); | ||
/** | ||
@@ -582,3 +458,3 @@ * Default API Exception | ||
/* eslint-disable no-proto */ | ||
var DefaultApiException = _fixBabelExtend(function (_Error) { | ||
var DefaultApiException = function (_Error) { | ||
inherits(DefaultApiException, _Error); | ||
@@ -607,4 +483,4 @@ | ||
return DefaultApiException; | ||
}(Error)); | ||
}(Error); | ||
export { JSON_FORMAT, FORM_DATA_FORMAT, Api, responseProcessor as defaultResponseProcessor, DefaultResponseProcessor, DefaultApiException }; |
@@ -1,7 +0,5 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.apiHandler = {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/** | ||
@@ -39,129 +37,2 @@ * Resolve given processor. | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
} | ||
}; | ||
}(); | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -320,2 +191,15 @@ if (!(instance instanceof Constructor)) { | ||
/** | ||
* Fetch API url. | ||
* | ||
* @param {Request} request - fetch request | ||
* @returns {Promise<Response>} fetch response | ||
*/ | ||
}, { | ||
key: 'fetchRequest', | ||
value: function fetchRequest(request) { | ||
return fetch(request); | ||
} | ||
/** | ||
* Request given API endpoint. | ||
@@ -345,3 +229,3 @@ * | ||
return fetch(request).then(function (response) { | ||
return this.fetchRequest(request).then(function (response) { | ||
return resolveArray(response, _this.processors, request); | ||
@@ -368,2 +252,20 @@ }); | ||
/** | ||
* Send a request with body. | ||
* | ||
* @param {string} namespace - api endpoint | ||
* @param {MethodType} method - api method | ||
* @param {Object} data - body JSON parameters | ||
* @param {Format} format - format of body request | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
*/ | ||
}, { | ||
key: 'requestWithBody', | ||
value: function requestWithBody(namespace, method, data, format) { | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format) | ||
}); | ||
} | ||
/** | ||
* Send a POST request. | ||
@@ -383,5 +285,3 @@ * | ||
return this.request(namespace, 'POST', { | ||
body: Api.convertData(data, format) | ||
}); | ||
return this.requestWithBody(namespace, 'POST', data, format); | ||
} | ||
@@ -404,5 +304,3 @@ | ||
return this.request(namespace, 'PUT', { | ||
body: Api.convertData(data, format) | ||
}); | ||
return this.requestWithBody(namespace, 'PUT', data, format); | ||
} | ||
@@ -558,26 +456,2 @@ | ||
var _fixBabelExtend = function (O) { | ||
var gPO = O.getPrototypeOf || function (o) { | ||
return o.__proto__; | ||
}, | ||
sPO = O.setPrototypeOf || function (o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}, | ||
construct = (typeof Reflect === 'undefined' ? 'undefined' : _typeof(Reflect)) === 'object' ? Reflect.construct : function (Parent, args, Class) { | ||
var Constructor, | ||
a = [null]; | ||
a.push.apply(a, args); | ||
Constructor = Parent.bind.apply(Parent, a); | ||
return sPO(new Constructor(), Class.prototype); | ||
}; | ||
return function fixBabelExtend(Class) { | ||
var Parent = gPO(Class); | ||
return sPO(Class, sPO(function Super() { | ||
return construct(Parent, arguments, gPO(this).constructor); | ||
}, Parent)); | ||
}; | ||
}(Object); | ||
/** | ||
@@ -588,3 +462,3 @@ * Default API Exception | ||
/* eslint-disable no-proto */ | ||
var DefaultApiException = _fixBabelExtend(function (_Error) { | ||
var DefaultApiException = function (_Error) { | ||
inherits(DefaultApiException, _Error); | ||
@@ -613,3 +487,3 @@ | ||
return DefaultApiException; | ||
}(Error)); | ||
}(Error); | ||
@@ -622,5 +496,1 @@ exports.JSON_FORMAT = JSON_FORMAT; | ||
exports.DefaultApiException = DefaultApiException; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
{ | ||
"name": "rest-api-handler", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "Handler for REST APIs", | ||
@@ -13,5 +13,5 @@ "main": "dist/index.js", | ||
"flow-coverage": "node node_modules/flow-coverage-report/bin/flow-coverage-report.js -f \"node node_modules/flow-bin/vendor/flow\" -i ./src/**/*.js* -t html -t text -t json -o test_results/coverage/flow", | ||
"test": "node node_modules/karma/bin/karma start karma.conf.js", | ||
"build": "node node_modules/rollup/bin/rollup --config ./rollup.config.js", | ||
"report-coverage": "codecov" | ||
"test": "node ./node_modules/jest/bin/jest.js", | ||
"build": "rm -rf ./dist && node node_modules/rollup/bin/rollup --config ./rollup.config.js", | ||
"precommit": "npm run js-lint && npm run flow && npm run test && npm run build" | ||
}, | ||
@@ -25,27 +25,8 @@ "repository": { | ||
"devDependencies": { | ||
"babel-core": "^6.26.0", | ||
"babel-eslint": "^8.0.0", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-plugin-istanbul": "^4.1.5", | ||
"babel-plugin-transform-async-to-generator": "^6.24.1", | ||
"babel-plugin-transform-builtin-classes": "^0.6.1", | ||
"babel-plugin-transform-class-properties": "^6.24.1", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-flow": "^6.23.0", | ||
"codecov": "^2.3.0", | ||
"cross-fetch": "^2.1.0", | ||
"eslint-config-socifi": "1.x", | ||
"flow-bin": "0.68.0", | ||
"flow-coverage-report": "^0.3.0", | ||
"jasmine-core": "^2.5.2", | ||
"karma": "^1.3.0", | ||
"karma-babel-preprocessor": "^7.0.0", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-commonjs": "^1.0.0", | ||
"karma-coverage": "^1.1.1", | ||
"karma-jasmine": "^1.0.2", | ||
"karma-junit-reporter": "^1.1.0", | ||
"rollup": "^0.50.0", | ||
"rollup-plugin-babel": "^3.0.2", | ||
"rollup-plugin-uglify": "^2.0.1" | ||
"socifi-jest-config": "^1.4.0", | ||
"socifi-rollup-config": "^1.6.0" | ||
}, | ||
@@ -52,0 +33,0 @@ "browserslist": [ |
@@ -99,3 +99,3 @@ # REST API Handler | ||
## Authentization | ||
## Authentication | ||
@@ -102,0 +102,0 @@ You can authorize to API by using default headers or set them after. |
@@ -7,3 +7,3 @@ // @flow | ||
type MethodType = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PUT' | 'PATCH' | 'TRACE'; | ||
export type MethodType = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PUT' | 'PATCH' | 'TRACE'; | ||
@@ -19,2 +19,3 @@ class Api<ProcessedResponse> { | ||
removeDefaultHeader: (name: string) => void; | ||
fetchRequest: (request: Request) => Promise<Response>; | ||
@@ -115,2 +116,12 @@ static FORMATS = { | ||
/** | ||
* Fetch API url. | ||
* | ||
* @param {Request} request - fetch request | ||
* @returns {Promise<Response>} fetch response | ||
*/ | ||
fetchRequest(request: Request): Promise<Response> { | ||
return fetch(request); | ||
} | ||
/** | ||
* Request given API endpoint. | ||
@@ -128,3 +139,4 @@ * | ||
options: Object = {}, | ||
headers: Object = {}): Promise<ProcessedResponse> { | ||
headers: Object = {}, | ||
): Promise<ProcessedResponse> { | ||
const urlToRequest = namespace.indexOf('http') === 0 ? namespace : `${this.apiUrl}/${namespace}`; | ||
@@ -142,3 +154,3 @@ | ||
return fetch(request) | ||
return this.fetchRequest(request) | ||
.then((response: Response) => { | ||
@@ -161,11 +173,12 @@ return resolveProcessors(response, this.processors, request); | ||
/** | ||
* Send a POST request. | ||
* Send a request with body. | ||
* | ||
* @param {string} namespace - api endpoint | ||
* @param {MethodType} method - api method | ||
* @param {Object} data - body JSON parameters | ||
* @param {?Format} format - format of body request | ||
* @param {Format} format - format of body request | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
*/ | ||
post(namespace: string, data: Object = {}, format: ?Format = Api.FORMATS.JSON_FORMAT): Promise<ProcessedResponse> { | ||
return this.request(namespace, 'POST', { | ||
requestWithBody(namespace: string, method: MethodType, data: Object, format: Format): Promise<ProcessedResponse> { | ||
return this.request(namespace, method, { | ||
body: Api.convertData(data, format), | ||
@@ -176,2 +189,14 @@ }); | ||
/** | ||
* Send a POST request. | ||
* | ||
* @param {string} namespace - api endpoint | ||
* @param {Object} data - body JSON parameters | ||
* @param {?Format} format - format of body request | ||
* @returns {Promise<ProcessedResponse>} processed response | ||
*/ | ||
post(namespace: string, data: Object = {}, format: Format = Api.FORMATS.JSON_FORMAT): Promise<ProcessedResponse> { | ||
return this.requestWithBody(namespace, 'POST', data, format); | ||
} | ||
/** | ||
* Send a PUT request. | ||
@@ -184,6 +209,4 @@ * | ||
*/ | ||
put(namespace: string, data: Object = {}, format: ?Format = Api.FORMATS.JSON_FORMAT): Promise<ProcessedResponse> { | ||
return this.request(namespace, 'PUT', { | ||
body: Api.convertData(data, format), | ||
}); | ||
put(namespace: string, data: Object = {}, format: Format = Api.FORMATS.JSON_FORMAT): Promise<ProcessedResponse> { | ||
return this.requestWithBody(namespace, 'PUT', data, format); | ||
} | ||
@@ -190,0 +213,0 @@ |
@@ -56,2 +56,2 @@ // @flow | ||
}); | ||
}; | ||
} |
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
106933
6
37
2075
4