UNPKG

ngx-http-configure

Version:

Angular (6+) helpers for configure HTTP interceptors and services.

1 lines 4.71 kB
{"version":3,"file":"ngx-http-configure.js","sources":["ng://ngx-http-configure/constants.ts","ng://ngx-http-configure/filter-options.ts","ng://ngx-http-configure/configure.ts","ng://ngx-http-configure/selector-predicate.ts","ng://ngx-http-configure/reconfigure.ts"],"sourcesContent":["export const HTTP_OPTIONS_KEYS = [\n 'headers',\n 'observe',\n 'params',\n 'reportProgress',\n 'responseType',\n 'withCredentials',\n];\n\nexport const HTTP_OPTION_PREFIX = '~';\n","import { HttpParams } from '@angular/common/http';\nimport { HttpConfigureOptions } from './types';\n\nexport function filterOptions<T>(options: HttpConfigureOptions, predicate: (key: string) => boolean, keyPrefix: string = ''): T {\n return Object.keys(options).reduce((acc, key) => {\n if (predicate(key)) {\n const prefixedKey = `${keyPrefix}${key}`;\n let value = options[key];\n if (value instanceof HttpParams) {\n value = value.keys().reduce((p, k) => ({ ...p, [k]: value.get(k) }), {});\n }\n acc[prefixedKey] = value;\n }\n return acc;\n }, {} as T);\n}\n","import { HttpConfigureOptions, HttpOptions } from './types';\nimport { HTTP_OPTION_PREFIX, HTTP_OPTIONS_KEYS } from './constants';\nimport { filterOptions } from './filter-options';\n\n/**\n * Configure the http client options tu extend functionality and use in interceptors.\n * @param options Set of extended http client options\n */\nexport function configure(options: HttpConfigureOptions): HttpOptions {\n const safeOptions = filterOptions<HttpOptions>(options, (key) => HTTP_OPTIONS_KEYS.includes(key));\n const extraParams = filterOptions(options, (key) => !HTTP_OPTIONS_KEYS.includes(key), HTTP_OPTION_PREFIX);\n safeOptions.params = {...safeOptions.params, ...extraParams};\n return safeOptions;\n}\n","import { HTTP_OPTION_PREFIX } from './constants';\n\nexport function selectorPredicate(selector: string[]): (key: string) => boolean {\n return key => Array.isArray(selector)\n ? selector.map(sel => `${HTTP_OPTION_PREFIX}${sel}`).includes(key)\n : true;\n}\n","import { HttpParams, HttpRequest } from '@angular/common/http';\nimport { HttpReconfiguredOptions } from './types';\nimport { HTTP_OPTION_PREFIX } from './constants';\nimport { selectorPredicate } from './selector-predicate';\n\n/**\n * Re-configure a request previously configured with this library.\n * @param request The current http request\n * @param selector Apply params to array else ignore this filters\n */\nexport function reconfigure(request: HttpRequest<any>, selector?: string[]): HttpReconfiguredOptions {\n const predicate = selectorPredicate(selector);\n const { config, params } = request.params.keys().reduce((acc, key) => {\n const value = request.params.get(key);\n if (key.startsWith(HTTP_OPTION_PREFIX) && predicate(key)) {\n const unprefixedKey = key.replace(HTTP_OPTION_PREFIX, '');\n acc.config[unprefixedKey] = value;\n } else {\n acc.params = acc.params.set(key, value);\n }\n return acc;\n }, { config: {}, params: new HttpParams() });\n return { config, request: request.clone({ params }) };\n}\n"],"names":[],"mappings":";;;;;;;;IAAa,iBAAiB,GAAG;IAC/B,SAAS;IACT,SAAS;IACT,QAAQ;IACR,gBAAgB;IAChB,cAAc;IACd,iBAAiB;CAClB;;IAEY,kBAAkB,GAAG,GAAG;;;;;;;;;;;;;ACNrC,SAAgB,aAAa,CAAI,OAA6B,EAAE,SAAmC,EAAE,SAAsB;IAAtB,0BAAA,EAAA,cAAsB;IACzH,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM;;;;;IAAC,UAAC,GAAG,EAAE,GAAG;QAC1C,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE;;gBACZ,WAAW,GAAG,KAAG,SAAS,GAAG,GAAK;;gBACpC,OAAK,GAAG,OAAO,CAAC,GAAG,CAAC;YACxB,IAAI,OAAK,YAAY,UAAU,EAAE;gBAC/B,OAAK,GAAG,OAAK,CAAC,IAAI,EAAE,CAAC,MAAM;;;;;gBAAC,UAAC,CAAC,EAAE,CAAC;;oBAAK,qBAAM,CAAC,eAAG,CAAC,IAAG,OAAK,CAAC,GAAG,CAAC,CAAC,CAAC;iBAAG,GAAE,EAAE,CAAC,CAAC;aAC1E;YACD,GAAG,CAAC,WAAW,CAAC,GAAG,OAAK,CAAC;SAC1B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