From 0ca1e9e7d4fc6b2e15d6bece472e60ffd341e5ec Mon Sep 17 00:00:00 2001 From: Nick Schooneman Date: Thu, 4 Jul 2019 19:35:32 +0200 Subject: [PATCH] Add support for using a factory to supply the dsn --- README.md | 38 ++++++++++++++++++++++++++++++++++++- src/angular/app.module.d.ts | 7 +++++-- src/angular/app.module.ts | 11 +++++++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1432d5b..5640299 100644 --- a/README.md +++ b/README.md @@ -44,17 +44,53 @@ Sentry.init(dsn); ### With Angular +Add the dsn directly: ```typescript import { SentryModule } from 'nativescript-sentry/angular'; NgModule({ ... imports: [ - SentryModule.forRoot({dsn: 'https://:@host/'}) + SentryModule.forRoot({ + config: { + dsn: 'https://:@host/' + } + }) ], ``` +Or use a provider: +```typescript +import { SentryModule, SentryService } from 'nativescript-sentry/angular'; +import { EnvironmentService } from './app/environment.service'; + +... + +export function sentryServiceOptionsFactory(environmentService) { + return { + dsn: environmentService.getSentryDsn() + }; +} + +... + +NgModule({ + ... + imports: [ + SentryModule.forRoot({ + sentryServiceProvider: { + provide: SentryService, + useFactory: sentryServiceOptionsFactory, + deps: [EnvironmentService] + } + }) + ], + providers: [EnvironmentService] + +``` +**Note**: If a `sentryServiceProvider` is defined the `config` property will be ignored. + **Note:** this plugin adds a custom ErrorHandler to your angular app # API diff --git a/src/angular/app.module.d.ts b/src/angular/app.module.d.ts index 76ff1dd..51be085 100644 --- a/src/angular/app.module.d.ts +++ b/src/angular/app.module.d.ts @@ -1,7 +1,10 @@ -import { InjectionToken, ModuleWithProviders } from '@angular/core'; +import { InjectionToken, ModuleWithProviders, Provider } from '@angular/core'; import { SentryErrorHandler } from './error.handler'; export interface SentryConfig { - dsn: string; + sentryServiceProvider?: Provider; + config?: { + dsn: string; + }; } export declare const SentryService: InjectionToken; export declare class SentryModule { diff --git a/src/angular/app.module.ts b/src/angular/app.module.ts index 828d179..b664e38 100644 --- a/src/angular/app.module.ts +++ b/src/angular/app.module.ts @@ -1,19 +1,22 @@ -import { ErrorHandler, InjectionToken, ModuleWithProviders, NgModule } from '@angular/core'; +import { ErrorHandler, InjectionToken, ModuleWithProviders, NgModule, Provider } from '@angular/core'; import { SentryErrorHandler } from './error.handler'; export interface SentryConfig { - dsn: string; + sentryServiceProvider?: Provider; + config?: { + dsn: string; + }; } export const SentryService = new InjectionToken('SentryConfig'); @NgModule() export class SentryModule { - static forRoot(config: SentryConfig): ModuleWithProviders { + static forRoot(options: SentryConfig): ModuleWithProviders { return { ngModule: SentryModule, providers: [ - { provide: SentryService, useValue: config }, + options.sentryServiceProvider || { provide: SentryService, useValue: options.config }, { provide: ErrorHandler, useFactory: provideSentryServiceOptions,