Skip to content

Commit

Permalink
feat(): distinct interfaces (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
houssenedao authored Jul 4, 2022
1 parent 5eeb7a4 commit 6a0f998
Show file tree
Hide file tree
Showing 4 changed files with 14,022 additions and 52 deletions.
27 changes: 21 additions & 6 deletions lib/interfaces/hasher-module.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export type HasherProviderType = 'bcrypt' | 'argon';
* @property createHasherOptions()
*/
export interface HasherModuleOptionsFactory {
createHasherOptions(): Promise<HasherModuleOptions> | HasherModuleOptions;
createHasherOptions():
| Promise<HasherBcryptModuleOptions | HasherArgonModuleOptions>
| HasherBcryptModuleOptions
| HasherArgonModuleOptions;
}

/**
Expand All @@ -26,17 +29,29 @@ export interface HasherModuleAsyncOptions
useClass?: Type<HasherModuleOptionsFactory>;
useFactory?: (
...args: any[]
) => Promise<HasherModuleOptions> | HasherModuleOptions;
) =>
| Promise<HasherBcryptModuleOptions | HasherArgonModuleOptions>
| HasherBcryptModuleOptions
| HasherArgonModuleOptions;
inject?: any[];
extraProviders?: Provider[];
}

/**
* @interface HasherModuleOptions
* @interface HasherBcryptModuleOptions
* @property {HasherProviderType} provider
* @property {number} round
*/
export interface HasherModuleOptions {
provider: HasherProviderType;
round?: number;
export interface HasherBcryptModuleOptions {
provider: 'bcrypt';
round: number;
}

/**
* @interface HasherArgonModuleOptions
* @property {HasherProviderType} provider
* @property {number} round
*/
export interface HasherArgonModuleOptions {
provider: 'argon';
}
11 changes: 7 additions & 4 deletions lib/nestjs-hasher.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { DynamicModule, Module, Provider, ValueProvider } from '@nestjs/common';
import { NestjsHasherService } from './nestjs-hasher.service';
import { HASHER_CONFIG } from './constants';
import {
HasherArgonModuleOptions,
HasherBcryptModuleOptions,
HasherModuleAsyncOptions,
HasherModuleOptions,
HasherModuleOptionsFactory,
} from './interfaces';

Expand All @@ -12,10 +13,12 @@ export class NestjsHasherModule {
/**
* Register module
* @static
* @param {HasherModuleOptions} options
* @param {HasherBcryptModuleOptions | HasherArgonModuleOptions} options
* @return DynamicModule
*/
static register(options: HasherModuleOptions): DynamicModule {
static register(
options: HasherBcryptModuleOptions | HasherArgonModuleOptions,
): DynamicModule {
const hasherProvider: ValueProvider = {
provide: HASHER_CONFIG,
useValue: options,
Expand Down Expand Up @@ -107,7 +110,7 @@ export class NestjsHasherModule {
provide: HASHER_CONFIG,
async useFactory(
optionsFactory: HasherModuleOptionsFactory,
): Promise<HasherModuleOptions> {
): Promise<HasherBcryptModuleOptions | HasherArgonModuleOptions> {
return optionsFactory.createHasherOptions();
},
inject: [inject],
Expand Down
35 changes: 26 additions & 9 deletions lib/nestjs-hasher.service.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import { Inject, Injectable } from '@nestjs/common';
import { HasherManagerService } from './managers';
import { ArgonService, BcryptService } from './providers';
import { HasherModuleOptions } from './interfaces';
import {
HasherArgonModuleOptions,
HasherBcryptModuleOptions,
HasherProviderType,
} from './interfaces';
import { HASHER_CONFIG } from './constants';

@Injectable()
export class NestjsHasherService extends HasherManagerService {
/**
* Nestjs hasher service constructor.
* @constructor
* @param {HasherModuleOptions} hasherOption
* @param {HasherBcryptModuleOptions} options
*/
constructor(
@Inject(HASHER_CONFIG) private hasherOption: HasherModuleOptions,
@Inject(HASHER_CONFIG)
private options: HasherBcryptModuleOptions | HasherArgonModuleOptions,
) {
super();

if (
this.options.provider !== 'argon' &&
this.options.provider !== 'bcrypt'
) {
throw new Error(`Config error your provider not exist`);
}
}

/**
* Get default provider
* @method
* @protected
*/
protected getDefaultProvider(): string {
return this.hasherOption.provider;
protected getDefaultProvider(): HasherProviderType {
return this.options.provider;
}

/**
Expand All @@ -41,11 +53,16 @@ export class NestjsHasherService extends HasherManagerService {
* @protected
*/
protected createBcryptProvider() {
if (!this.hasherOption.round) {
throw new Error(`To use bcrypt salt round is required`);
}
if (this.options.provider === 'bcrypt') {
// defined option type
this.options = this.options as HasherBcryptModuleOptions;

return new BcryptService(Number(this.hasherOption.round));
if (!this.options.round) {
throw new Error(`To use bcrypt salt round is required`);
}

return new BcryptService(Number(this.options?.round));
}
}

/**
Expand Down
Loading

0 comments on commit 6a0f998

Please sign in to comment.