UNPKG

@ngx-resource/handler-fetch

Version:
210 lines (198 loc) 9.17 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@ngx-resource/core')) : typeof define === 'function' && define.amd ? define('@ngx-resource/handler-fetch', ['exports', '@ngx-resource/core'], factory) : (global = global || self, factory((global['ngx-resource'] = global['ngx-resource'] || {}, global['ngx-resource']['handler-fetch'] = {}), global.core)); }(this, function (exports, core) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } function __awaiter(thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } var _a; /** @type {?} */ var REQUEST_METHOD_MAP = (_a = {}, _a[core.ResourceRequestMethod.Get] = 'GET', _a[core.ResourceRequestMethod.Post] = 'POST', _a[core.ResourceRequestMethod.Put] = 'PUT', _a[core.ResourceRequestMethod.Patch] = 'PATCH', _a[core.ResourceRequestMethod.Delete] = 'DELETE', _a[core.ResourceRequestMethod.Options] = 'OPTIONS', _a[core.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 || core.ResourceRequestMethod.Get; requestInit.method = REQUEST_METHOD_MAP[req.method]; requestInit.headers = req.headers; if (req.method !== core.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 core.ResourceResponseBodyType.ArrayBuffer: return res.arrayBuffer(); case core.ResourceResponseBodyType.Blob: return res.blob(); case core.ResourceResponseBodyType.Json: return res.json(); case core.ResourceResponseBodyType.Text: default: return res.text(); } }; return ResourceHandlerFetch; }(core.ResourceHandler)); exports.REQUEST_METHOD_MAP = REQUEST_METHOD_MAP; exports.ResourceHandlerFetch = ResourceHandlerFetch; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=ngx-resource-handler-fetch.umd.js.map