Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable typescript strict mode for null-checking #288

Open
wants to merge 1 commit into
base: main
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
11 changes: 9 additions & 2 deletions lib/indicator/redis-health.indicator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Injectable, Inject } from '@nestjs/common';
import { HealthIndicator, HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';
import {
HealthIndicator,
HealthIndicatorResult,
HealthCheckError,
} from '@nestjs/terminus';
import Redis from 'ioredis';

import { REDIS_HEALTH_INDICATOR } from '../redis.constants';
Expand All @@ -15,7 +19,10 @@ export class RedisHealthIndicator extends HealthIndicator {
await this.redis.ping();
return this.getStatus(key, true);
} catch (error) {
throw new HealthCheckError('Redis check failed', this.getStatus(key, false, { message: error.message }));
throw new HealthCheckError(
'Redis check failed',
this.getStatus(key, false, { message: error?.['message'] }),
);
}
}
}
92 changes: 58 additions & 34 deletions lib/redis.core-module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { DynamicModule, Module, Global, Provider } from '@nestjs/common';
import { RedisModuleAsyncOptions, RedisModuleOptions, RedisModuleOptionsFactory } from './redis.interfaces';
import { createRedisConnection, getRedisOptionsToken, getRedisConnectionToken } from './redis.utils'
import {
RedisModuleAsyncOptions,
RedisModuleOptions,
RedisModuleOptionsFactory,
} from './redis.interfaces';
import {
createRedisConnection,
getRedisOptionsToken,
getRedisConnectionToken,
} from './redis.utils';

@Global()
@Module({})
export class RedisCoreModule {

/* forRoot */
static forRoot(options: RedisModuleOptions, connection?: string): DynamicModule {

static forRoot(
options: RedisModuleOptions,
connection?: string,
): DynamicModule {
const redisOptionsProvider: Provider = {
provide: getRedisOptionsToken(connection),
useValue: options,
Expand All @@ -21,76 +30,91 @@ export class RedisCoreModule {

return {
module: RedisCoreModule,
providers: [
redisOptionsProvider,
redisConnectionProvider,
],
exports: [
redisOptionsProvider,
redisConnectionProvider,
],
providers: [redisOptionsProvider, redisConnectionProvider],
exports: [redisOptionsProvider, redisConnectionProvider],
};
}

/* forRootAsync */
public static forRootAsync(options: RedisModuleAsyncOptions, connection: string): DynamicModule {

public static forRootAsync(
options: RedisModuleAsyncOptions,
connection?: string,
): DynamicModule {
const redisConnectionProvider: Provider = {
provide: getRedisConnectionToken(connection),
useFactory(options: RedisModuleOptions) {
return createRedisConnection(options)
return createRedisConnection(options);
},
inject: [getRedisOptionsToken(connection)],
};

return {
module: RedisCoreModule,
imports: options.imports,
providers: [...this.createAsyncProviders(options, connection), redisConnectionProvider],
providers: [
...this.createAsyncProviders(options, connection),
redisConnectionProvider,
],
exports: [redisConnectionProvider],
};
}

/* createAsyncProviders */
public static createAsyncProviders(options: RedisModuleAsyncOptions, connection?: string): Provider[] {

if(!(options.useExisting || options.useFactory || options.useClass)) {
throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
public static createAsyncProviders(
options: RedisModuleAsyncOptions,
connection?: string,
): Provider[] {
if (
options.useExisting == null &&
options.useExisting == null &&
options.useExisting == null
) {
throw new Error(
'Invalid configuration. Must provide useFactory, useClass or useExisting',
);
}

if (options.useExisting || options.useFactory) {
return [
this.createAsyncOptionsProvider(options, connection)
];
if (options.useExisting == null) {
return [this.createAsyncOptionsProvider(options, connection)];
}

return [
this.createAsyncOptionsProvider(options, connection),
{ provide: options.useClass, useClass: options.useClass },
{ provide: options.useExisting, useClass: options.useExisting },
];
}

/* createAsyncOptionsProvider */
public static createAsyncOptionsProvider(options: RedisModuleAsyncOptions, connection?: string): Provider {

if(!(options.useExisting || options.useFactory || options.useClass)) {
throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
public static createAsyncOptionsProvider(
options: RedisModuleAsyncOptions,
connection?: string,
): Provider {
if (
options.useExisting == null &&
options.useExisting == null &&
options.useExisting == null
) {
throw new Error(
'Invalid configuration. Must provide useFactory, useClass or useExisting',
);
}

if (options.useFactory) {
if (options.useFactory != null) {
return {
provide: getRedisOptionsToken(connection),
useFactory: options.useFactory,
inject: options.inject || [],
inject: options.inject ?? [],
};
}

return {
provide: getRedisOptionsToken(connection),
async useFactory(optionsFactory: RedisModuleOptionsFactory): Promise<RedisModuleOptions> {
async useFactory(
optionsFactory: RedisModuleOptionsFactory,
): Promise<RedisModuleOptions> {
return await optionsFactory.createRedisModuleOptions();
},
inject: [options.useClass || options.useExisting],
inject: [options.useClass ?? options.useExisting],
};
}
}
16 changes: 4 additions & 12 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@
"sourceMap": false,
"outDir": "./dist",
"rootDir": "./lib",
"lib": ["es7"]
"lib": ["es7"],
"strict": true
},
"include": [
"lib/**/*"
],
"exclude": [
"node_modules"
]
"include": ["lib/**/*"],
"exclude": ["node_modules"]
}