@nestjs/config#ConfigType TypeScript Examples

The following examples show how to use @nestjs/config#ConfigType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: auth.module.ts    From nestjs-rest-sample with GNU General Public License v3.0 7 votes vote down vote up
@Module({
  imports: [
    ConfigModule.forFeature(jwtConfig),
    UserModule,
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.registerAsync({
      imports: [ConfigModule.forFeature(jwtConfig)],
      useFactory: (config: ConfigType<typeof jwtConfig>) => {
        return {
          secret: config.secretKey,
          signOptions: { expiresIn: config.expiresIn },
        } as JwtModuleOptions;
      },
      inject: [jwtConfig.KEY],
    }),
  ],
  providers: [AuthService, LocalStrategy, JwtStrategy],
  exports: [AuthService],
  controllers: [AuthController],
})
export class AuthModule {}
Example #2
Source File: jwt.strategy.spec.ts    From nestjs-rest-sample with GNU General Public License v3.0 6 votes vote down vote up
describe('JwtStrategy', () => {
  let strategy: JwtStrategy;
  let config: ConfigType<typeof jwtConfig>;
  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      providers: [
        JwtStrategy,
        {
          provide: jwtConfig.KEY,
          useValue: {
            secretKey: "test",
            expiresIn:'100s'
          },
        },
      ],
    })
    .compile();

    strategy = app.get<JwtStrategy>(JwtStrategy);
    config = app.get<ConfigType<typeof jwtConfig>>(jwtConfig.KEY);
  });

  describe('validate', () => {
    it('should return user principal if user and password is provided ', async () => {
      expect(config.secretKey).toBe('test')
      expect(config.expiresIn).toBe('100s')
      const user = await strategy.validate({
        upn: "test",
        sub: 'testid',
        email: "[email protected]",
        roles: [RoleType.USER]
      });
      expect(user.username).toEqual('test');
      expect(user.id).toEqual('testid');
    });
  });
});
Example #3
Source File: jwt.strategy.spec.ts    From nestjs-rest-sample with GNU General Public License v3.0 6 votes vote down vote up
describe('JwtStrategy(call supper)', () => {
  let local;
  let parentMock;

  beforeEach(() => {
    local = Object.getPrototypeOf(JwtStrategy);
    parentMock = jest.fn();
    Object.setPrototypeOf(JwtStrategy, parentMock);
  });

  afterEach(() => {
    Object.setPrototypeOf(JwtStrategy, local);
  });

  it('call super', () => {
    const config = mock<ConfigType<typeof jwtConfig>>();
    config.secretKey="test";
    new JwtStrategy(config);
    expect(parentMock.mock.calls.length).toBe(1);

    expect(parentMock.mock.calls[0][0].jwtFromRequest).toBeDefined();
    expect(parentMock.mock.calls[0][0].ignoreExpiration).toBeFalsy();
    expect(parentMock.mock.calls[0][0].secretOrKey).toEqual("test");

  })
});
Example #4
Source File: jwt.config.spec.ts    From nestjs-rest-sample with GNU General Public License v3.0 6 votes vote down vote up
describe('jwtConfig', () => {
  let config: ConfigType<typeof jwtConfig>;
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [ConfigModule.forFeature(jwtConfig)],
    }).compile();

    config = module.get<ConfigType<typeof jwtConfig>>(jwtConfig.KEY);
  });

  it('should be defined', () => {
    expect(jwtConfig).toBeDefined();
  });

  it('should contains expiresIn and secret key', async () => {
    expect(config.expiresIn).toBe('3600s');
    expect(config.secretKey).toBe('rzxlszyykpbgqcflzxsqcysyhljt');
  });
});
Example #5
Source File: mongodb.config.spec.ts    From nestjs-rest-sample with GNU General Public License v3.0 6 votes vote down vote up
describe('mongodbConfig', () => {
  let config: ConfigType<typeof mongodbConfig>;
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [ConfigModule.forFeature(mongodbConfig)],
    }).compile();

    config = module.get<ConfigType<typeof mongodbConfig>>(mongodbConfig.KEY);
  });

  it('should be defined', () => {
    expect(mongodbConfig).toBeDefined();
  });

  it('should contains uri key', async () => {
    expect(config.uri).toBe('mongodb://localhost/blog');
  });
});
Example #6
Source File: sendgrid.config.spec.ts    From nestjs-rest-sample with GNU General Public License v3.0 6 votes vote down vote up
describe('sendgridConfig', () => {
  let config: ConfigType<typeof sendgridConfig>;
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [ConfigModule.forFeature(sendgridConfig)],
    }).compile();

    config = module.get<ConfigType<typeof sendgridConfig>>(sendgridConfig.KEY);
  });

  it('should be defined', () => {
    expect(sendgridConfig).toBeDefined();
  });

  it('should contains expiresIn and secret key', async () => {
    expect(config.apiKey).toBeTruthy();
  });
});
Example #7
Source File: database-connection.providers.ts    From nestjs-rest-sample with GNU General Public License v3.0 6 votes vote down vote up
databaseConnectionProviders = [
  {
    provide: DATABASE_CONNECTION,
    useFactory: (dbConfig: ConfigType<typeof mongodbConfig>): Connection => {
      const conn = createConnection(dbConfig.uri, {
        //useNewUrlParser: true,
        //useUnifiedTopology: true,
        //see: https://mongoosejs.com/docs/deprecations.html#findandmodify
        //useFindAndModify: false,
      });

      // conn.on('disconnect', () => {
      //   console.log('Disconnecting to MongoDB');
      // });

      return conn;
    },
    inject: [mongodbConfig.KEY],
  },
]
Example #8
Source File: sendgrid.providers.ts    From nestjs-rest-sample with GNU General Public License v3.0 6 votes vote down vote up
sendgridProviders = [
    {
      provide: SENDGRID_MAIL,
      useFactory: (config: ConfigType<typeof sendgridConfig>): MailService =>
        {
            const mail = new MailService();
            mail.setApiKey(config.apiKey);
            mail.setTimeout(5000);
            //mail.setTwilioEmailAuth(username, password)
            return mail;
        },
      inject: [sendgridConfig.KEY],
    }
  ]
Example #9
Source File: jwt.strategy.ts    From nestjs-rest-sample with GNU General Public License v3.0 5 votes vote down vote up
constructor(@Inject(jwtConfig.KEY) config: ConfigType<typeof jwtConfig>) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: config.secretKey,
    });
  }