UNPKG

@koalarx/ui

Version:

Koala UI is a modern and accessible component library designed to speed up interface development in Angular projects. With simple integration and clear documentation, you can easily build robust and visually appealing applications.

109 lines (103 loc) 4.4 kB
import { provideHttpClient, HTTP_INTERCEPTORS, withInterceptorsFromDi, HttpClient } from '@angular/common/http'; import { AppConfig } from '@koalarx/ui/core/config'; import { getTranslationByLanguage } from '@koalarx/ui/core/translations'; import { provideMarkdown, MARKED_OPTIONS } from 'ngx-markdown'; import { provideEnvironmentNgxMask } from 'ngx-mask'; import * as i0 from '@angular/core'; import { inject, Injectable } from '@angular/core'; import { Authorization } from '@koalarx/ui/shared/services'; import { switchMap } from 'rxjs/internal/operators/switchMap'; import { HttpErrorFeedbackAlert } from '@koalarx/ui/shared/utils'; import { tap } from 'rxjs/internal/operators/tap'; class AuthorizationInterceptor { authorization = inject(Authorization); authConfig = inject(AppConfig).authConfig; setAuthorization(request) { if (this.authorization.hasToken()) { return request.clone({ headers: request.headers.set('Authorization', `Bearer ${request.url === this.authConfig?.refreshToken?.url ? this.authorization.refreshToken : this.authorization.accessToken}`), }); } return request.clone(); } intercept(request, next) { if (this.authorization.hasToken() && this.authorization.isExpired() && !this.authConfig?.refreshToken?.url) { return this.authorization .updateToken() .pipe(switchMap(() => next.handle(this.setAuthorization(request)))); } return next.handle(this.setAuthorization(request)); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.6", ngImport: i0, type: AuthorizationInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.0.6", ngImport: i0, type: AuthorizationInterceptor }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.6", ngImport: i0, type: AuthorizationInterceptor, decorators: [{ type: Injectable }] }); class FeedbackRequestInterceptor { httpError = inject(HttpErrorFeedbackAlert); intercept(request, next) { return next.handle(request.clone()).pipe(tap({ error: (error) => this.httpError.tapError(error), })); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.6", ngImport: i0, type: FeedbackRequestInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.0.6", ngImport: i0, type: FeedbackRequestInterceptor }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.6", ngImport: i0, type: FeedbackRequestInterceptor, decorators: [{ type: Injectable }] }); const maskOptions = { validation: false, thousandSeparator: '.', }; function provideKoala(config) { const appConfig = new AppConfig(); appConfig.language = config?.language ?? 'en'; appConfig.translation = getTranslationByLanguage(config?.language ?? 'en'); appConfig.hostApi = config?.hostApi; appConfig.httpClientErrorsMiddleware = config?.httpClientErrorsMiddleware; appConfig.authConfig = config?.authConfig; const providers = [ provideEnvironmentNgxMask(maskOptions), provideHttpClient(withInterceptorsFromDi()), provideMarkdown({ loader: HttpClient, markedOptions: { provide: MARKED_OPTIONS, useValue: { gfm: true, breaks: true, pedantic: false, }, }, }), { provide: HTTP_INTERCEPTORS, useClass: FeedbackRequestInterceptor, multi: true, }, { provide: AppConfig, useValue: appConfig, }, ]; if (config?.authConfig) { const interceptor = config.authorizationInterceptor || AuthorizationInterceptor; providers.push({ provide: HTTP_INTERCEPTORS, useClass: interceptor, multi: true, }); } return providers; } /** * Generated bundle index. Do not edit. */ export { provideKoala }; //# sourceMappingURL=koalarx-ui.mjs.map