UNPKG

ngx-http-client-service

Version:

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-) <!-- ALL-CONTRIBUTORS-BADGE:END -->

333 lines (324 loc) 13.5 kB
import { __decorate } from 'tslib'; import { ɵɵdefineInjectable, Injectable, ɵɵinject } from '@angular/core'; import { HttpParams, HttpHeaders, HttpClient } from '@angular/common/http'; class NgxHttpParams { } class NgxHttpHeaders { } class HttpOptions { } class NgxHttpOptions { } /** * Injectable Http Options Service */ let HttpOptionsService = /** HttpOptionsService where all http options creations methods are defined * It can contains http params and http headers creation functionality */ class HttpOptionsService { /** * @description Creates an instance of http options service. */ constructor() { } /** * @description appendHttpParams is a common method. * @param httpParam contains an object of NgxHttpParams. * @returns returns constructed httpParameters of type HttpParam */ appendHttpParams(ngxHttpParams) { let httpParams = new HttpParams(); Object.entries(ngxHttpParams).forEach(([param, paramValue]) => { if (param && paramValue.toString()) { httpParams = httpParams.append(param, paramValue.toString()); } }); return httpParams; } /** * @description appendHttpHeaders is a common method. * @param headers contains an object of NgxHttpHeaders. * @returns returns constructed httpParameters of type HttpParam */ appendHttpHeaders(ngxHttpHeaders) { let httpHeaders = new HttpHeaders(); Object.entries(ngxHttpHeaders).forEach(([header, headerValue]) => { if (header && headerValue.toString()) { httpHeaders = httpHeaders.append(header, headerValue.toString()); } }); return httpHeaders; } /** * @description provides the http options. * @param httpOptionsParameters contains params of type NgxHttpOptions. * @returns returns appendd http options. */ appendHttpOptions(httpOptionsParameters) { if (!httpOptionsParameters) { httpOptionsParameters = new NgxHttpOptions(); httpOptionsParameters.header = {}; httpOptionsParameters.param = {}; } const httpOptions = new HttpOptions(); if (httpOptionsParameters.param) { httpOptions.params = this.appendHttpParams(httpOptionsParameters.param); } else { httpOptions.params = this.appendHttpParams({}); } if (httpOptionsParameters.header) { httpOptions.headers = this.appendHttpHeaders(httpOptionsParameters.header); } else { httpOptions.headers = this.appendHttpHeaders({}); } if (httpOptionsParameters.observe) { httpOptions.observe = httpOptionsParameters.observe; } if (httpOptionsParameters.reportProgress) { httpOptions.reportProgress = httpOptionsParameters.reportProgress; } if (httpOptionsParameters.responseType) { httpOptions.responseType = httpOptionsParameters.responseType; } if (httpOptionsParameters.withCredentials) { httpOptions.withCredentials = httpOptionsParameters.withCredentials; } return httpOptions; } }; HttpOptionsService.ngInjectableDef = ɵɵdefineInjectable({ factory: function HttpOptionsService_Factory() { return new HttpOptionsService(); }, token: HttpOptionsService, providedIn: "root" }); HttpOptionsService = __decorate([ Injectable({ providedIn: 'root' }) /** HttpOptionsService where all http options creations methods are defined * It can contains http params and http headers creation functionality */ ], HttpOptionsService); class ApiPathConstants { } ApiPathConstants.PATH_BASE = '/'; ApiPathConstants.PATH_DIVIDER = '/'; ApiPathConstants.QUERY_SPECIFIER = '?'; ApiPathConstants.QUERY_DIVIDER = '&'; ApiPathConstants.QUERY_ASSIGNER = '='; ApiPathConstants.BASIC_HEADER = {}; /** * Injectable Http Options Service */ let ApiPathService = /** api url generator Service all reusable and repeated functionalities can be managed * It can contains common methods and common methods related params. */ class ApiPathService { /** * @description creates an instance of APIPathGenerationLibrary */ constructor() { // Common Constants this.PATH_BASE = ApiPathConstants.PATH_BASE; this.PATH_DIVIDER = ApiPathConstants.PATH_DIVIDER; this.QUERY_SPECIFIER = ApiPathConstants.QUERY_SPECIFIER; this.QUERY_DIVIDER = ApiPathConstants.QUERY_DIVIDER; this.QUERY_ASSIGNER = ApiPathConstants.QUERY_ASSIGNER; } /** * @description validating QueryParam. * @param pathQuery as an argument of type PathQuery. * @return constructed PathQuery Object. */ modifyPathQuery(pathQuery) { const constructedPathQuery = {}; Object.entries(pathQuery).filter(([query, queryValue]) => { if (query) { constructedPathQuery[query] = queryValue; } }); return constructedPathQuery; } /** * @description createApiPath is a constructing method. * @param pathComponent contains an array of string. * @returns returns constructed api path url. */ createApiPath(pathComponent) { let path = this.PATH_BASE; pathComponent = pathComponent.filter((param) => { return param !== this.PATH_DIVIDER; }); path = path + pathComponent.join(this.PATH_DIVIDER); return encodeURI(path); } /** * @description Returns router parameterized path. * @param apiPathParams contains partial url. * @param pathQuery contains parameters to be passed as query parameters. * @returns returns generated routed paramerized path. */ createApiPathWithQuery(apiPathParams, pathQuery) { let path = this.createApiPath(apiPathParams); if (!pathQuery) { return path; } else { pathQuery = this.modifyPathQuery(pathQuery); if (Object.keys(pathQuery).length) { path = decodeURI(path) + this.QUERY_SPECIFIER; Object.entries(pathQuery).forEach(([query, queryValue], index) => { if (Object.keys(pathQuery).length === (index + 1)) { path = path + query + this.QUERY_ASSIGNER + queryValue; } else { path = path + query + this.QUERY_ASSIGNER + queryValue + this.QUERY_DIVIDER; } }); } return encodeURI(path); } } }; ApiPathService.ngInjectableDef = ɵɵdefineInjectable({ factory: function ApiPathService_Factory() { return new ApiPathService(); }, token: ApiPathService, providedIn: "root" }); ApiPathService = __decorate([ Injectable({ providedIn: 'root' }) /** api url generator Service all reusable and repeated functionalities can be managed * It can contains common methods and common methods related params. */ ], ApiPathService); /** * @description Injectable Http Client Service */ let NgxHttpClientService = /** NgxHttpClientService Library where all reusable and repeated functionalities can be managed * It can contains common methods and common methods related params. */ class NgxHttpClientService { /** * @description Creates an instance of http client service. * @param httpClient - an instance of HttpClient * @param httpOptionsService - an instance of HttpOptionsService */ constructor(httpClient, httpOptionsService, apiPathService) { this.httpClient = httpClient; this.httpOptionsService = httpOptionsService; this.apiPathService = apiPathService; } /** * @description will expose get method of http client. * @param pathParams contains path parameters of type string array * @param HttpOptions contains parameter of type HttpOptions. */ get(pathParams, httpOptions) { const apiUrl = this.apiPathService.createApiPath(pathParams); const options = this.httpOptionsService.appendHttpOptions(httpOptions); return this.httpClient.get(apiUrl, options); } /** * @description will expose post method of http client. * @param pathParams contains path parameters of type string array * @param HttpOptions contains parameter of type HttpOptions. * @param body contains parameter for http post body */ post(pathParams, body, httpOptions) { const apiUrl = this.apiPathService.createApiPath(pathParams); const options = this.httpOptionsService.appendHttpOptions(httpOptions); return this.httpClient.post(apiUrl, body, options); } /** * @description will expose put method of http client. * @param pathParams contains path parameters of type string array * @param HttpOptions contains parameter of type HttpOptions. * @param body contains parameter for http put body */ put(pathParams, body, httpOptions) { const apiUrl = this.apiPathService.createApiPath(pathParams); const options = this.httpOptionsService.appendHttpOptions(httpOptions); return this.httpClient.put(apiUrl, body, options); } /** * @description will expose delete method of http client. * @param pathParams contains path parameters of type string array * @param HttpOptions contains parameter of type HttpOptions. */ delete(pathParams, httpOptions) { const apiUrl = this.apiPathService.createApiPath(pathParams); const options = this.httpOptionsService.appendHttpOptions(httpOptions); return this.httpClient.delete(apiUrl, options); } /** * @description will expose request method of http client. * @param method contains the method parameter of type string. * @param pathParams contains path parameters of type string array. * @param HttpOptions contains parameter of type HttpOptions. */ request(method, pathParams, httpOptions) { const apiUrl = this.apiPathService.createApiPath(pathParams); const options = this.httpOptionsService.appendHttpOptions(httpOptions); return this.httpClient.request(method, apiUrl, options); } /** * @description will expose head method of http client. * @param pathParams contains path parameters of type string array. * @param HttpOptions contains parameter of type HttpOptions. */ head(pathParams, httpOptions) { const apiUrl = this.apiPathService.createApiPath(pathParams); const options = this.httpOptionsService.appendHttpOptions(httpOptions); return this.httpClient.head(apiUrl, options); } /** * @description will expose jsonp method of http client. * @param pathParams contains path parameters of type string array. * @param callbackFn contains parameter of type string. */ jsonp(pathParams, callbackFn) { const apiUrl = this.apiPathService.createApiPath(pathParams); return this.httpClient.jsonp(apiUrl, callbackFn); } /** * @description will expose options method of http client. * @param pathParams contains path parameters of type string array. * @param HttpOptions contains parameter of type HttpOptions. */ options(pathParams, httpOptions) { const apiUrl = this.apiPathService.createApiPath(pathParams); const options = this.httpOptionsService.appendHttpOptions(httpOptions); return this.httpClient.options(apiUrl, options); } /** * @description will expose patch method of http client. * @param pathParams contains path parameters of type string array. * @param body of type any. * @param HttpOptions contains parameter of type HttpOptions. */ patch(pathParams, body, httpOptions) { const apiUrl = this.apiPathService.createApiPath(pathParams); const options = this.httpOptionsService.appendHttpOptions(httpOptions); return this.httpClient.patch(apiUrl, body, options); } }; NgxHttpClientService.ctorParameters = () => [ { type: HttpClient }, { type: HttpOptionsService }, { type: ApiPathService } ]; NgxHttpClientService.ngInjectableDef = ɵɵdefineInjectable({ factory: function NgxHttpClientService_Factory() { return new NgxHttpClientService(ɵɵinject(HttpClient), ɵɵinject(HttpOptionsService), ɵɵinject(ApiPathService)); }, token: NgxHttpClientService, providedIn: "root" }); NgxHttpClientService = __decorate([ Injectable({ providedIn: 'root' }) /** NgxHttpClientService Library where all reusable and repeated functionalities can be managed * It can contains common methods and common methods related params. */ ], NgxHttpClientService); /* * Public API Surface of ngx-http-client-service */ /** * Generated bundle index. Do not edit. */ export { NgxHttpClientService, NgxHttpOptions, HttpOptionsService as ɵa, ApiPathService as ɵb }; //# sourceMappingURL=ngx-http-client-service.js.map