Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Add support for using a factory to supply the dsn #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://<key>:<secret>@host/<project>'})
SentryModule.forRoot({
config: {
dsn: 'https://<key>:<secret>@host/<project>'
}
})
],

```

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
Expand Down
7 changes: 5 additions & 2 deletions src/angular/app.module.d.ts
Original file line number Diff line number Diff line change
@@ -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<SentryConfig>;
export declare class SentryModule {
Expand Down
11 changes: 7 additions & 4 deletions src/angular/app.module.ts
Original file line number Diff line number Diff line change
@@ -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>('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,
Expand Down