UNPKG

@3kles/kles-ionapi

Version:
151 lines (144 loc) 6.13 kB
import * as i0 from '@angular/core'; import { Injectable, NgModule } from '@angular/core'; import { BehaviorSubject, of, throwError } from 'rxjs'; import { tap, filter, catchError, switchMap } from 'rxjs/operators'; import * as i1 from '@angular/common/http'; import { HttpErrorResponse, HTTP_INTERCEPTORS } from '@angular/common/http'; import { CommonModule } from '@angular/common'; class KlesIonApiService { constructor(http) { this.http = http; this._token$ = new BehaviorSubject(null); } auth() { return this.refreshToken(); } refreshToken(force) { this.setToken(null); if (this.isLocalhost()) { console.warn('In localhost, you cannot retrieve a ion api token'); return of(null); } else { const url = `/grid/rest/security/sessions/oauth`; return this.http.get(url, { params: { ...(force && { forceRefresh: true }), rid: this.generateRandomString() }, responseType: 'text' }).pipe(tap((token) => this.setToken(token))); } } get token() { return this._token$.getValue(); } setDevelopmentToken(token) { if (!this.isLocalhost()) { throw new Error('Development tokens are only allowed for localhost'); } else { this.setToken(token); } } getContext() { return this._token$.asObservable().pipe(filter((token) => !!token)); } setToken(token) { this._token$.next(token); } generateRandomString(stringLength = 16) { const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; let randomstring = ''; for (let i = 0; i < stringLength; i++) { const rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum, rnum + 1); } return randomstring; } isLocalhost() { return window.location.hostname === 'localhost'; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiService, deps: [{ token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [{ type: i1.HttpClient }] }); class KlesIonApiTokenInterceptor { constructor(klesIonApiService) { this.klesIonApiService = klesIonApiService; } intercept(request, next) { if (!this.klesIonApiService.token) { return next.handle(request); } const authReq = this.addToken(request, this.klesIonApiService.token); return next.handle(authReq).pipe(catchError((err) => { if (err instanceof HttpErrorResponse && err.status === 401) { return this.handle401Error(authReq, next) || throwError(err); } else { return throwError(err); } })); } handle401Error(request, next) { if (this.klesIonApiService.token) { return this.klesIonApiService.refreshToken(true).pipe(switchMap((token) => { return next.handle(this.addToken(request, token)); }), catchError((err) => { return throwError(err); })); } } addToken(request, token) { return request.clone({ headers: request.headers.set('Authorization', `Bearer ${token}`) }); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiTokenInterceptor, deps: [{ token: KlesIonApiService }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiTokenInterceptor }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiTokenInterceptor, decorators: [{ type: Injectable }], ctorParameters: () => [{ type: KlesIonApiService }] }); class KlesIonApiModule { static forRoot(configuration = { ionApiTokenInterceptor: true }) { return { ngModule: KlesIonApiModule, providers: [ ...(configuration.ionApiTokenInterceptor ? [{ provide: HTTP_INTERCEPTORS, useClass: KlesIonApiTokenInterceptor, multi: true }] : []) ] }; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiModule, imports: [CommonModule] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiModule, providers: [ KlesIonApiService, ], imports: [CommonModule] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: KlesIonApiModule, decorators: [{ type: NgModule, args: [{ declarations: [], imports: [ CommonModule ], exports: [], providers: [ KlesIonApiService, ] }] }] }); /* * Public API Surface of kles-ionapi */ /** * Generated bundle index. Do not edit. */ export { KlesIonApiModule, KlesIonApiService, KlesIonApiTokenInterceptor }; //# sourceMappingURL=3kles-kles-ionapi.mjs.map