UNPKG

imng-application-insights-ngrx

Version:

Small library to facilitate the integration of Application Insights with an NGRX metaReducer.

148 lines (142 loc) 7.4 kB
import * as i0 from '@angular/core'; import { InjectionToken, inject, Injectable, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { ApplicationInsights } from '@microsoft/applicationinsights-web'; import { tap, filter } from 'rxjs'; import { Actions, createEffect } from '@ngrx/effects'; import { filter as filter$1, tap as tap$1 } from 'rxjs/operators'; const APP_INSIGHTS_CONFIG = new InjectionToken('app-insights-config'); class AppInsightsMonitoringService { constructor() { this.appInsightsConfig = inject(APP_INSIGHTS_CONFIG); const appInsightsConfig = this.appInsightsConfig; this.appInsights = new ApplicationInsights({ config: appInsightsConfig }); this.appInsights.loadAppInsights(); } setAuthenticatedUserContext(userId) { this.appInsights.setAuthenticatedUserContext(userId, undefined, true); } clearAuthenticatedUserContext() { this.appInsights.clearAuthenticatedUserContext(); } logException(exception, properties, measurements) { this.appInsights.trackException({ exception, properties, measurements }); } logEvent(name, properties, measurements) { this.appInsights.trackEvent({ name, properties, measurements }); } trackEventsEffect(actions$, logPayload) { return actions$.pipe(tap((x) => { this.logEvent(x.type, logPayload ? x.payload : undefined); })); } trackErrorsEffect(actions$) { return actions$.pipe(filter((x) => { const type = x.type.toUpperCase(); return type.endsWith('ERROR') || type.endsWith('FAILURE'); }), tap((x) => { this.logException(x); })); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsMonitoringService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsMonitoringService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsMonitoringService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [] }); class ImngAppInsightsNgrxModule { static forRoot(appInsightsConfiguration) { return { ngModule: ImngAppInsightsNgrxModule, providers: [{ provide: APP_INSIGHTS_CONFIG, multi: false, useValue: appInsightsConfiguration }], }; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: ImngAppInsightsNgrxModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.0.8", ngImport: i0, type: ImngAppInsightsNgrxModule, imports: [CommonModule] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: ImngAppInsightsNgrxModule, providers: [AppInsightsMonitoringService], imports: [CommonModule] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: ImngAppInsightsNgrxModule, decorators: [{ type: NgModule, args: [{ imports: [CommonModule], providers: [AppInsightsMonitoringService], }] }] }); class AppInsightsBaseffects { constructor(actions$, monitor) { this.actions$ = actions$; this.monitor = monitor; this.noDispatch = { dispatch: false }; } getTrackLoginPipe() { return this.actions$.pipe(filter$1((x) => x.type.toUpperCase().endsWith('USER FOUND') && !!x.payload), tap$1((x) => { this.monitor.setAuthenticatedUserContext(x.payload?.profile?.email ?? 'unknown'); })); } getTrackLogoutPipe() { return this.actions$.pipe(filter$1((x) => 0 < x.type.toUpperCase().indexOf('SIGN OUT')), tap$1(() => { this.monitor.clearAuthenticatedUserContext(); })); } getTrackExceptionsPipe() { return this.actions$.pipe(filter$1((x) => x.type.toUpperCase().endsWith('ERROR')), tap$1((x) => { this.monitor.logException(x.payload || new Error('Unknown Error')); })); } } class AppInsightsVerboseRootEffects extends AppInsightsBaseffects { constructor() { const actions$ = inject(Actions); const monitor = inject(AppInsightsMonitoringService); super(actions$, monitor); this.trackEvents = createEffect(() => { return this.actions$.pipe(tap$1((x) => this.monitor.logEvent(x.type, x.payload))); }, this.noDispatch); this.trackLogin = createEffect(() => { return this.getTrackLoginPipe(); }, this.noDispatch); this.trackLogout = createEffect(() => { return this.getTrackLogoutPipe(); }, this.noDispatch); this.trackExceptions = createEffect(() => { return this.getTrackExceptionsPipe(); }, this.noDispatch); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsVerboseRootEffects, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsVerboseRootEffects }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsVerboseRootEffects, decorators: [{ type: Injectable }], ctorParameters: () => [] }); class AppInsightsInfoRootEffects extends AppInsightsBaseffects { constructor() { const actions$ = inject(Actions); const monitor = inject(AppInsightsMonitoringService); super(actions$, monitor); this.trackEvents = createEffect(() => { return this.actions$.pipe(tap$1((x) => this.monitor.logEvent(x.type))); }, this.noDispatch); this.trackLogin = createEffect(() => { return this.getTrackLoginPipe(); }, this.noDispatch); this.trackLogout = createEffect(() => { return this.getTrackLogoutPipe(); }, this.noDispatch); this.trackExceptions = createEffect(() => { return this.getTrackExceptionsPipe(); }, this.noDispatch); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsInfoRootEffects, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsInfoRootEffects }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.8", ngImport: i0, type: AppInsightsInfoRootEffects, decorators: [{ type: Injectable }], ctorParameters: () => [] }); /** * Generated bundle index. Do not edit. */ export { APP_INSIGHTS_CONFIG, AppInsightsInfoRootEffects, AppInsightsMonitoringService, AppInsightsVerboseRootEffects, ImngAppInsightsNgrxModule }; //# sourceMappingURL=imng-application-insights-ngrx.mjs.map