-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pmb0/feature/register-async
feat: add forRootAsync()
- Loading branch information
Showing
11 changed files
with
320 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,24 @@ | ||
import { ModuleMetadata, Type } from '@nestjs/common' | ||
|
||
export interface UnleashClientModuleOptions { | ||
baseURL: string | ||
instanceId: string | ||
appName: string | ||
timeout: number | ||
} | ||
|
||
export interface UnleashClientModuleOptionsFactory { | ||
createStrategiesOptions(): | ||
| Promise<UnleashClientModuleOptions> | ||
| UnleashClientModuleOptions | ||
} | ||
|
||
export interface UnleashClientModuleAsyncOptions | ||
extends Pick<ModuleMetadata, 'imports'> { | ||
inject?: any[] | ||
useExisting?: Type<UnleashClientModuleOptionsFactory> | ||
useClass?: Type<UnleashClientModuleOptionsFactory> | ||
useFactory?: ( | ||
...args: any[] | ||
) => Promise<UnleashClientModuleOptions> | UnleashClientModuleOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,99 @@ | ||
import { DynamicModule, Module, Type } from '@nestjs/common' | ||
import { DynamicModule, Module, Provider } from '@nestjs/common' | ||
import { | ||
UnleashStrategiesModuleAsyncOptions, | ||
UnleashStrategiesModuleOptions, | ||
UnleashStrategiesOptionsFactory, | ||
} from '.' | ||
import { | ||
ApplicationHostnameStrategy, | ||
DefaultStrategy, | ||
FlexibleRolloutStrategy, | ||
GradualRolloutSessionIdStrategy, | ||
RemoteAddressStrategy, | ||
UnleashStrategy, | ||
UserWithIdStrategy, | ||
} from './strategy' | ||
import { GradualRolloutRandomStrategy } from './strategy/gradual-rollout-random' | ||
import { GradualRolloutUserIdStrategy } from './strategy/gradual-rollout-user-id' | ||
import { CUSTOM_STRATEGIES } from './unleash-strategies.constants' | ||
import { UnleashStrategiesService } from './unleash-strategies.service' | ||
|
||
@Module({}) | ||
@Module({ | ||
providers: [ | ||
DefaultStrategy, | ||
ApplicationHostnameStrategy, | ||
FlexibleRolloutStrategy, | ||
RemoteAddressStrategy, | ||
UserWithIdStrategy, | ||
GradualRolloutRandomStrategy, | ||
GradualRolloutSessionIdStrategy, | ||
GradualRolloutUserIdStrategy, | ||
], | ||
}) | ||
export class UnleashStrategiesModule { | ||
static register(strategies: Type<UnleashStrategy>[] = []): DynamicModule { | ||
static register({ | ||
strategies = [], | ||
}: UnleashStrategiesModuleOptions): DynamicModule { | ||
return { | ||
module: UnleashStrategiesModule, | ||
providers: [ | ||
UnleashStrategiesService, | ||
DefaultStrategy, | ||
ApplicationHostnameStrategy, | ||
FlexibleRolloutStrategy, | ||
RemoteAddressStrategy, | ||
UserWithIdStrategy, | ||
GradualRolloutRandomStrategy, | ||
GradualRolloutSessionIdStrategy, | ||
GradualRolloutUserIdStrategy, | ||
...strategies, | ||
{ provide: CUSTOM_STRATEGIES, useValue: strategies }, | ||
], | ||
exports: [UnleashStrategiesService], | ||
exports: [ | ||
UnleashStrategiesService, | ||
{ provide: CUSTOM_STRATEGIES, useValue: strategies }, | ||
], | ||
} | ||
} | ||
|
||
public static registerAsync( | ||
options: UnleashStrategiesModuleAsyncOptions, | ||
): DynamicModule { | ||
const providers = this.createStrategiesProviders(options) | ||
return { | ||
module: UnleashStrategiesModule, | ||
imports: options.imports, | ||
providers: [ | ||
...(options.extraProviders ?? []), | ||
...providers, | ||
UnleashStrategiesService, | ||
], | ||
exports: [...providers, UnleashStrategiesService], | ||
} | ||
} | ||
|
||
static createStrategiesProviders( | ||
options: UnleashStrategiesModuleAsyncOptions, | ||
): Provider[] { | ||
if (options.useExisting || options.useFactory) { | ||
return [this.createStrategiesOptionsProvider(options)] | ||
} | ||
|
||
return [ | ||
this.createStrategiesOptionsProvider(options), | ||
{ | ||
provide: options.useClass!, | ||
useClass: options.useClass!, | ||
}, | ||
] | ||
} | ||
|
||
private static createStrategiesOptionsProvider( | ||
options: UnleashStrategiesModuleAsyncOptions, | ||
): Provider { | ||
if (options.useFactory) { | ||
return { | ||
provide: CUSTOM_STRATEGIES, | ||
useFactory: options.useFactory, | ||
inject: options.inject, | ||
} | ||
} | ||
|
||
return { | ||
provide: CUSTOM_STRATEGIES, | ||
useFactory: async (optionsFactory: UnleashStrategiesOptionsFactory) => | ||
await optionsFactory.createStrategiesOptions(), | ||
inject: [options.useExisting || options.useClass!], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const REFRESH_INTERVAL = 'REFRESH_INTERVAL' | ||
export const METRICS_INTERVAL = 'METRICS_INTERVAL' | ||
export const APP_NAME = 'APP_NAME' | ||
export const UNLEASH_MODULE_OPTIONS = 'UNLEASH_MODULE_OPTIONS' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.