All files / lib sendgridclient-core.module.ts

100% Statements 23/23
90% Branches 9/10
100% Functions 8/8
100% Lines 20/20

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 702x   2x 2x       2x   3x   3x               2x     2x     2x                     2x 1x   1x 1x                       2x 1x           1x     1x   1x 1x       2x
import { Global, Module, DynamicModule, Provider, Type } from '@nestjs/common';
import { SendGridClientModuleOptions, SendGridClientAsyncOptions, SendGridClientOptionsFactory } from './interfaces';
import { createSendGridClientProviders } from './providers';
import { SENDGRIDCLIENT_MODULE_OPTIONS, SENDGRIDCLIENT_TOKEN, createSendGridClient } from './common';
 
@Global()
@Module({})
export class SendGridClientCoreModule {
    public static forRoot(options: SendGridClientModuleOptions): DynamicModule {
        const provider = createSendGridClientProviders(options);
 
        return {
            exports: [provider,],
            module: SendGridClientCoreModule,
            providers: [provider]
        };
    }
 
    public static forRootAsync(options:SendGridClientAsyncOptions): DynamicModule {
        const provider: Provider = {
            inject: [SENDGRIDCLIENT_MODULE_OPTIONS],
            provide: SENDGRIDCLIENT_TOKEN,
            useFactory: (options: SendGridClientModuleOptions) => createSendGridClient(options),
        };
 
        return {
            exports: [provider],
            imports: options.imports,
            module: SendGridClientCoreModule,
            providers: [...this.createAsyncProviders(options), provider]
        };
    }
 
    private static createAsyncProviders(
        options: SendGridClientAsyncOptions,
      ): Provider[] {
        if (options.useExisting || options.useFactory) {
          return [this.createAsyncOptionsProvider(options)];
        }
        const useClass = options.useClass as Type<SendGridClientOptionsFactory>;
        return [
          this.createAsyncOptionsProvider(options),
          {
            provide: useClass,
            useClass,
          },
        ];
      }
    
      private static createAsyncOptionsProvider(
        options: SendGridClientAsyncOptions,
      ): Provider {
        if (options.useFactory) {
          return {
            inject: options.inject || [],
            provide: SENDGRIDCLIENT_MODULE_OPTIONS,
            useFactory: options.useFactory,
          };
        }
        const inject = [
          (options.useClass || options.useExisting) as Type<SendGridClientOptionsFactory>,
        ];
        return {
          provide: SENDGRIDCLIENT_MODULE_OPTIONS,
          useFactory: async (optionsFactory: SendGridClientOptionsFactory) =>
            await optionsFactory.createSendGridClientOptions(),
          inject,
        };
      }
}