All files / lib sentry-core.module.ts

100% Statements 23/23
90% Branches 9/10
100% Functions 7/7
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 933x                       3x       3x 3x 3x       3x     2x   2x                   8x       8x     8x                             8x 1x   7x 7x                       8x 1x           7x     7x   7x 7x        
import {
    Module,
    Global,
    Provider,
    Type,
    DynamicModule,
  } from '@nestjs/common';
  import {
    SentryModuleAsyncOptions,
    SentryOptionsFactory,
    SentryModuleOptions,
  } from './interfaces/sentry-options.interface';
  import {
    SENTRY_MODULE_OPTIONS,
    SENTRY_TOKEN,
  } from './common/sentry.constants';
import { SentryService } from './services/sentry.service';
import { createSentryClient } from './common/sentry.util';
import { createSentryProviders } from './providers/sentry.providers';
 
@Global()
@Module({})
export class SentryCoreModule {
 
    public static forRoot(options: SentryModuleOptions): DynamicModule {
        const provider = createSentryProviders(options);
 
        return {
            exports: [provider, SentryService],
            module: SentryCoreModule,
            providers: [provider, SentryService],
        };
    }
 
    public static forRootAsync(
        options: SentryModuleAsyncOptions,
    ): DynamicModule {
        const provider: Provider = {
            inject: [SENTRY_MODULE_OPTIONS],
            provide: SENTRY_TOKEN,
            useFactory: (options: SentryModuleOptions) =>
                createSentryClient(options),
        };
 
        return {
            exports: [provider, SentryService],
            imports: options.imports,
            module: SentryCoreModule,
            providers: [
                ...this.createAsyncProviders(options),
                provider,
                SentryService,
            ],
        };
    }
 
    private static createAsyncProviders(
        options: SentryModuleAsyncOptions,
      ): Provider[] {
        if (options.useExisting || options.useFactory) {
          return [this.createAsyncOptionsProvider(options)];
        }
        const useClass = options.useClass as Type<SentryOptionsFactory>;
        return [
          this.createAsyncOptionsProvider(options),
          {
            provide: useClass,
            useClass,
          },
        ];
      }
    
      private static createAsyncOptionsProvider(
        options: SentryModuleAsyncOptions,
      ): Provider {
        if (options.useFactory) {
          return {
            inject: options.inject || [],
            provide: SENTRY_MODULE_OPTIONS,
            useFactory: options.useFactory,
          };
        }
        const inject = [
          (options.useClass || options.useExisting) as Type<SentryOptionsFactory>,
        ];
        return {
          provide: SENTRY_MODULE_OPTIONS,
          useFactory: async (optionsFactory: SentryOptionsFactory) =>
            await optionsFactory.createSentryModuleOptions(),
          inject,
        };
      }
}