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(): distinct interfaces #203

Merged
merged 1 commit into from
Jul 4, 2022
Merged
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
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