@ngx-resource/handler-fetch
Version:
Resource handler for fetch
136 lines (133 loc) • 4.18 kB
JavaScript
import { __extends, __awaiter, __generator } from 'tslib';
import { ResourceRequestMethod, ResourceResponseBodyType, ResourceHandler } from '@ngx-resource/core';
var _a;
/** @type {?} */
var REQUEST_METHOD_MAP = (_a = {},
_a[ResourceRequestMethod.Get] = 'GET',
_a[ResourceRequestMethod.Post] = 'POST',
_a[ResourceRequestMethod.Put] = 'PUT',
_a[ResourceRequestMethod.Patch] = 'PATCH',
_a[ResourceRequestMethod.Delete] = 'DELETE',
_a[ResourceRequestMethod.Options] = 'OPTIONS',
_a[ResourceRequestMethod.Head] = 'HEAD',
_a);
var ResourceHandlerFetch = /** @class */ (function (_super) {
__extends(ResourceHandlerFetch, _super);
function ResourceHandlerFetch() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} req
* @return {?}
*/
ResourceHandlerFetch.prototype.handle = /**
* @param {?} req
* @return {?}
*/
function (req) {
var _this = this;
/** @type {?} */
var url = this.createUrl(req);
/** @type {?} */
var requestInit = this.createRequestInit(req);
return {
promise: fetch(url, requestInit).then((/**
* @param {?} res
* @return {?}
*/
function (res) { return __awaiter(_this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = {
headers: res.headers,
status: res.status
};
return [4 /*yield*/, this.getBody(req, res)];
case 1: return [2 /*return*/, (_a.body = _b.sent(),
_a)];
}
});
}); })),
};
};
/**
* @private
* @param {?} req
* @return {?}
*/
ResourceHandlerFetch.prototype.createUrl = /**
* @private
* @param {?} req
* @return {?}
*/
function (req) {
var url = req.url;
if (typeof url !== 'string') {
throw new Error('Url us missing');
}
if (req.query) {
/** @type {?} */
var queryGroups = [];
for (var key in req.query) {
if (req.query.hasOwnProperty(key)) {
queryGroups.push('key=' + encodeURIComponent(req.query[key]));
}
}
if (queryGroups.length) {
url += (url.indexOf('?') > -1 ? '&' : '?') + queryGroups.join('&');
}
}
return url;
};
/**
* @private
* @param {?} req
* @return {?}
*/
ResourceHandlerFetch.prototype.createRequestInit = /**
* @private
* @param {?} req
* @return {?}
*/
function (req) {
/** @type {?} */
var requestInit = {};
req.method = req.method || ResourceRequestMethod.Get;
requestInit.method = REQUEST_METHOD_MAP[req.method];
requestInit.headers = req.headers;
if (req.method !== ResourceRequestMethod.Get && req.body) {
requestInit.body = req.body;
}
return requestInit;
};
/**
* @private
* @param {?} req
* @param {?} res
* @return {?}
*/
ResourceHandlerFetch.prototype.getBody = /**
* @private
* @param {?} req
* @param {?} res
* @return {?}
*/
function (req, res) {
switch (req.responseBodyType) {
case ResourceResponseBodyType.ArrayBuffer:
return res.arrayBuffer();
case ResourceResponseBodyType.Blob:
return res.blob();
case ResourceResponseBodyType.Json:
return res.json();
case ResourceResponseBodyType.Text:
default:
return res.text();
}
};
return ResourceHandlerFetch;
}(ResourceHandler));
export { REQUEST_METHOD_MAP, ResourceHandlerFetch };
//# sourceMappingURL=ngx-resource-handler-fetch.js.map