UNPKG

ngx-restful-client

Version:

The ultimate Angular library to exquisitely declare and implement the REST APIs you'll use in your application.

160 lines (159 loc) 6.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var http_headers_builder_1 = require("./http-headers-builder"); var http_params_builder_1 = require("./http-params-builder"); var http_1 = require("@angular/common/http"); var utils_1 = require("../utils"); var propertyExists = function (obj, propName) { return Object.prototype.hasOwnProperty.call(obj, propName); }; var isOptionsNotInstanceOfHttpOptions = function (obj) { return (!!obj && !propertyExists(obj, 'headers') && !propertyExists(obj, 'params') && !propertyExists(obj, 'reportProgress') && !propertyExists(obj, 'withCredentials')); }; exports.configureOptions = function (options) { var opts; if (isOptionsNotInstanceOfHttpOptions(options) && !!options) { var supplier = options; opts = HttpOptionsBuilder.params(supplier()).build(); } else { opts = options; } return opts; }; var HttpOptionsBuilder = /** @class */ (function () { function HttpOptionsBuilder() { // intentionally left blank } HttpOptionsBuilder.fresh = function () { return new HttpOptionsBuilder(); }; HttpOptionsBuilder.headers = function (headers) { return new HttpOptionsBuilder().headers(headers); }; HttpOptionsBuilder.params = function (obj) { return new HttpOptionsBuilder().params(obj); }; HttpOptionsBuilder.reportProgress = function () { return new HttpOptionsBuilder().reportProgress(); }; HttpOptionsBuilder.withCredentials = function () { return new HttpOptionsBuilder().withCredentials(); }; HttpOptionsBuilder.prototype.reportProgress = function () { this._reportProgress = true; return this; }; HttpOptionsBuilder.prototype.withCredentials = function () { this._withCredentials = true; return this; }; HttpOptionsBuilder.prototype.addOptions = function (options) { if (!!options) { if (!!options.params) { if (options.params instanceof http_1.HttpParams) { this.params(options.params); } else { this.params(http_params_builder_1.HttpParamsBuilder.fromKeyValue(options.params).build()); } } if (!!options.headers) { if (options.headers instanceof http_1.HttpHeaders) { this.headers(options.headers); } else { this.headers(http_headers_builder_1.HttpHeadersBuilder.fromHeaders(options.headers).build()); } } if (options.withCredentials) { this.withCredentials(); } if (options.reportProgress) { this.reportProgress(); } } return this; }; HttpOptionsBuilder.prototype.params = function (params) { // eslint-disable-next-line @typescript-eslint/no-unused-vars var generate = function (params) { return new http_params_builder_1.HttpParamsBuilder(); }; var append = function (params, httpParams) { return httpParams; }; if (params instanceof http_1.HttpParams) { append = function (params, httpParams) { return httpParams.appendHttpParams(params); }; generate = function (params) { return http_params_builder_1.HttpParamsBuilder.fromHttpParams(params); }; } else if (!!params) { if (utils_1.CoreUtil.hasAttributes(params)) { append = function (params, httpParams) { return httpParams.appendHttpParams(http_params_builder_1.HttpParamsBuilder.fromObject(params).params); }; generate = function (params) { return http_params_builder_1.HttpParamsBuilder.fromObject(params); }; } else { var q_1 = utils_1.CoreUtil.objectToString(params); append = function (params, httpParams) { return httpParams.appendHttpParams(http_params_builder_1.HttpParamsBuilder.fromObject({ q: q_1 }).params); }; generate = function () { return http_params_builder_1.HttpParamsBuilder.fromObject({ q: q_1 }); }; } } this._ngxParams = !!this._ngxParams ? append(params, this._ngxParams) : generate(params); return this; }; HttpOptionsBuilder.prototype.headers = function (headers) { if (headers instanceof http_1.HttpHeaders) { this._ngxHeaders = !!this._ngxHeaders ? this._ngxHeaders.addAll(headers) : http_headers_builder_1.HttpHeadersBuilder.fromHeaders(headers); } else { this._ngxHeaders = headers(!!this._ngxHeaders ? this._ngxHeaders : http_headers_builder_1.HttpHeadersBuilder.fresh()); } return this; }; HttpOptionsBuilder.prototype.checkUrlLength = function (uri) { var path = uri || ''; var fullPath = path; if (!!this._ngxParams && !!this._ngxParams.params) { fullPath = path + "?" + this._ngxParams.params.toString(); } if (2048 < fullPath.length) { console.warn("URL with params shouldn't be more than 2048 characters long. Some browsers may be incompatible."); } return this; }; HttpOptionsBuilder.prototype.build = function () { return this.options; }; Object.defineProperty(HttpOptionsBuilder.prototype, "options", { get: function () { var headers = !!this._ngxHeaders ? this._ngxHeaders.headers : undefined; var params = !!this._ngxParams ? this._ngxParams.params : undefined; var opts = {}; if (!!this._withCredentials) { opts['withCredentials'] = this._withCredentials; } if (!!this._reportProgress) { opts['reportProgress'] = this._reportProgress; } if (!!headers) { opts['headers'] = headers; } if (!!params) { opts['params'] = params; } return opts; }, enumerable: true, configurable: true }); return HttpOptionsBuilder; }()); exports.HttpOptionsBuilder = HttpOptionsBuilder;