UNPKG

oidc-client-rx

Version:

ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications

49 lines (48 loc) 1.93 kB
import { HttpHeaders } from "@ngify/http"; import { Injectable, inject } from "injection-js"; import { HttpParams } from "../http/index.js"; import { HttpBaseService } from "./http-base.service.js"; function _ts_decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc); else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } const NGSW_CUSTOM_PARAM = "ngsw-bypass"; class DataService { get(url, config, token) { const headers = this.prepareHeaders(token); const params = this.prepareParams(config); return this.httpClient.get(url, { headers, params }); } post(url, body, config, headersParams) { const headers = headersParams || this.prepareHeaders(); const params = this.prepareParams(config); return this.httpClient.post(url ?? "", body, { headers, params }); } prepareHeaders(token) { let headers = new HttpHeaders(); headers = headers.set("Accept", "application/json"); if (token) headers = headers.set("Authorization", `Bearer ${decodeURIComponent(token)}`); return headers; } prepareParams(config) { let params = new HttpParams(); const { ngswBypass } = config; if (ngswBypass) params = params.set(NGSW_CUSTOM_PARAM, ""); return params; } constructor(){ this.httpClient = inject(HttpBaseService); } } DataService = _ts_decorate([ Injectable() ], DataService); export { DataService };