All files sentry-core.module.ts

82.6% Statements 38/46
65.11% Branches 28/43
93.33% Functions 14/15
94.28% Lines 33/35

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  3x 3x 3x   3x   3x 3x 3x 3x   6x 3x       3x 3x 3x 3x 3x 3x 3x   15x 15x             4x     4x   4x                       4x 2x   2x 2x                 4x 1x           3x     3x   3x         3x       3x            
import {
  Module,
  Global,
  Provider,
  Type,E
  DynamicModule,
} from '@nestjs/common';
import {
  SentryModuleAsyncOptions,
  SentryOptionsFactory,
  SentryModuleOptions,
} from './sentry.interfaces';
import {
  SENTRY_MODULE_OPTIONS,
  SENTRY_TOKEN,
} from './sentry.constants';
import { SentryService } from './sentry.service';
import { createSentryProviders } from './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) => new SentryService(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,
        };
      }
}