theme.keycloak.v2.account.resources.account-service.account.service.js.map Maven / Gradle / Ivy
{"version":3,"file":"account.service.js","names":["ContentAlert","AccountServiceError","Error","constructor","response","statusText","AccountServiceClient","keycloakService","_defineProperty","kcSvc","accountUrl","authServerUrl","realm","doGet","endpoint","config","doRequest","method","doDelete","doPost","body","JSON","stringify","doPut","fetch","makeUrl","toString","makeConfig","data","json","e","ok","handleError","status","authenticated","audiencePresent","window","location","href","baseUrl","login","err","danger","startsWith","URL","url","hasOwnProperty","params","Object","keys","forEach","key","searchParams","append","Promise","resolve","getToken","then","token","headers","Authorization","catch","addEventListener","event","promise","error","preventDefault"],"sources":["../../src/app/account-service/account.service.ts"],"sourcesContent":["/*\n * Copyright 2018 Red Hat Inc. and/or its affiliates and other contributors\n * as indicated by the @author tags. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\nimport {KeycloakService} from '../keycloak-service/keycloak.service';\nimport {ContentAlert} from '../content/ContentAlert';\n\ndeclare const baseUrl: string;\n\ntype ConfigResolve = (config: RequestInit) => void;\n\nexport interface HttpResponse extends Response {\n data?: T;\n}\n\nexport interface RequestInitWithParams extends RequestInit {\n params?: {[name: string]: string | number};\n}\n\nexport class AccountServiceError extends Error {\n constructor(public response: HttpResponse) {\n super(response.statusText);\n }\n}\n\n/**\n *\n * @author Stan Silvert [email protected] (C) 2018 Red Hat Inc.\n */\nexport class AccountServiceClient {\n private kcSvc: KeycloakService;\n private accountUrl: string;\n\n public constructor(keycloakService: KeycloakService) {\n this.kcSvc = keycloakService;\n this.accountUrl = this.kcSvc.authServerUrl() + 'realms/' + this.kcSvc.realm() + '/account';\n }\n\n public async doGet(endpoint: string,\n config?: RequestInitWithParams): Promise> {\n return this.doRequest(endpoint, {...config, method: 'get'});\n }\n\n public async doDelete(endpoint: string,\n config?: RequestInitWithParams): Promise> {\n return this.doRequest(endpoint, {...config, method: 'delete'});\n }\n\n public async doPost(endpoint: string,\n body: string | {},\n config?: RequestInitWithParams): Promise> {\n return this.doRequest(endpoint, {...config, body: JSON.stringify(body), method: 'post'});\n }\n\n public async doPut(endpoint: string,\n body: string | {},\n config?: RequestInitWithParams): Promise> {\n return this.doRequest(endpoint, {...config, body: JSON.stringify(body), method: 'put'});\n }\n\n public async doRequest(endpoint: string,\n config?: RequestInitWithParams): Promise> {\n\n const response: HttpResponse = await fetch(this.makeUrl(endpoint, config).toString(),\n await this.makeConfig(config));\n\n try {\n response.data = await response.json();\n } catch (e) {} // ignore. Might be empty\n\n if (!response.ok) {\n this.handleError(response);\n throw new AccountServiceError(response);\n }\n\n return response;\n }\n\n private handleError(response: HttpResponse): void {\n if (response !== null && response.status === 401) {\n if (this.kcSvc.authenticated() && !this.kcSvc.audiencePresent()) {\n // authenticated and the audience is not present => not allowed\n window.location.href = baseUrl + '#/forbidden';\n } else {\n // session timed out?\n this.kcSvc.login();\n }\n }\n\n if (response !== null && response.status === 403) {\n window.location.href = baseUrl + '#/forbidden';\n }\n\n if (response !== null && response.data != null) {\n if (response.data['errors'] != null) {\n for(let err of response.data['errors'])\n ContentAlert.danger(err['errorMessage'], err['params']);\n } else {\n ContentAlert.danger(\n `${response.statusText}: ${response.data['errorMessage'] ? response.data['errorMessage'] : ''} ${response.data['error'] ? response.data['error'] : ''}`);\n };\n } else {\n ContentAlert.danger(response.statusText);\n }\n }\n\n private makeUrl(endpoint: string, config?: RequestInitWithParams): URL {\n if (endpoint.startsWith('http')) return new URL(endpoint);\n const url = new URL(this.accountUrl + endpoint);\n\n // add request params\n if (config && config.hasOwnProperty('params')) {\n const params: {[name: string]: string} = config.params as {} || {};\n Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))\n }\n\n return url;\n }\n\n private makeConfig(config: RequestInit = {}): Promise {\n return new Promise( (resolve: ConfigResolve) => {\n this.kcSvc.getToken()\n .then( (token: string) => {\n resolve( {\n ...config,\n headers: {'Content-Type': 'application/json',\n ...config.headers,\n Authorization: 'Bearer ' + token}\n });\n }).catch(() => {\n this.kcSvc.login();\n });\n });\n }\n\n}\n\nwindow.addEventListener(\"unhandledrejection\", (event: PromiseRejectionEvent) => {\n event.promise.catch(error => {\n if (error instanceof AccountServiceError) {\n // We already handled the error. Ignore unhandled rejection.\n event.preventDefault();\n }\n });\n});\n"],"mappings":";;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA,SAAQA,YAAY;AAcpB,OAAO,MAAMC,mBAAmB,SAASC,KAAK,CAAC;EAC3CC,WAAWA,CAAQC,QAAsB,EAAE;IACvC,KAAK,CAACA,QAAQ,CAACC,UAAU,CAAC;IAAC,KADZD,QAAsB,GAAtBA,QAAsB;EAEzC;AACJ;;AAEA;AACA;AACA;AACA;AACA,OAAO,MAAME,oBAAoB,CAAC;EAIvBH,WAAWA,CAACI,eAAgC,EAAE;IAAAC,eAAA;IAAAA,eAAA;IACjD,IAAI,CAACC,KAAK,GAAGF,eAAe;IAC5B,IAAI,CAACG,UAAU,GAAG,IAAI,CAACD,KAAK,CAACE,aAAa,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,CAACF,KAAK,CAACG,KAAK,CAAC,CAAC,GAAG,UAAU;EAC9F;EAEA,MAAaC,KAAKA,CAAIC,QAAgB,EAChBC,MAA8B,EAA4B;IAC5E,OAAO,IAAI,CAACC,SAAS,CAACF,QAAQ,EAAE;MAAC,GAAGC,MAAM;MAAEE,MAAM,EAAE;IAAK,CAAC,CAAC;EAC/D;EAEA,MAAaC,QAAQA,CAAIJ,QAAgB,EACjBC,MAA8B,EAA4B;IAC9E,OAAO,IAAI,CAACC,SAAS,CAACF,Q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