Skip to content

Commit

Permalink
fix: composition methods and declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoGathK committed Aug 31, 2022
1 parent aa966ff commit 7ab1b95
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@
"@nestjs/core": "^8.4.7",
"@nestjs/platform-express": "^8.4.7"
},
"peerDependencies": {
"@types/multer": "1.4.7"
},
"devDependencies": {
"@commitlint/cli": "16.3.0",
"@commitlint/config-conventional": "16.2.4",
"@nestjs/testing": "8.4.7",
"@types/jest": "27.5.2",
"@types/multer": "1.4.7",
"@types/node": "16.11.56",
"@types/supertest": "2.0.12",
"@vodyani/eslint-config": "^1.1.0",
Expand Down
17 changes: 16 additions & 1 deletion src/common/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModuleMetadata } from './declare';
import { FactoryProvider, ModuleMetadata } from './declare';

/**
* Infrastructure module registration options.
Expand Down Expand Up @@ -88,3 +88,18 @@ export interface ContainerRegisterOptions {
*/
aop?: ModuleMetadata['providers'];
}
/**
* Asynchronous provider factory for creating
*
* @see: [factory provider objects](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory)
*/
export interface AsyncProviderFactory {
/**
* Create a factory provider by specifying the creation parameters externally.
*
* @returns FactoryProvider
*
* @publicApi
*/
create: (...args: any[]) => FactoryProvider;
}
6 changes: 3 additions & 3 deletions src/decorator/async-inject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Type, Inject } from '../common';
import { AsyncProviderFactory, StaticStore } from '../struct';
import { AsyncProvider, StaticStore } from '../struct';

/**
* Use this decorator to handle dependency management for asynchronous provider factory classes.
Expand All @@ -8,7 +8,7 @@ import { AsyncProviderFactory, StaticStore } from '../struct';
*
* @publicApi
*/
export function AsyncInjectable(target: Type<AsyncProviderFactory>) {
export function AsyncInjectable(target: Type<AsyncProvider>) {
StaticStore.set(target.name, Symbol(target.name));
}
/**
Expand All @@ -18,6 +18,6 @@ export function AsyncInjectable(target: Type<AsyncProviderFactory>) {
*
* @publicApi
*/
export function AsyncInject(target: Type<AsyncProviderFactory>) {
export function AsyncInject(target: Type<AsyncProvider>) {
return Inject((target as any).getToken());
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export {
PostFormData,
};

export { AsyncProviderFactory } from './struct';
export { AsyncProvider } from './struct';
export * from './common';
17 changes: 1 addition & 16 deletions src/struct/async-provider-factory.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { FactoryProvider } from '../common';

import { StaticStore } from './store';

/**
* Asynchronous provider factory for creating
*
* @see: [factory provider objects](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory)
*/
export class AsyncProviderFactory {
/**
* Create a factory provider by specifying the creation parameters externally.
*
* @returns FactoryProvider
*
* @publicApi
*/
public create: (...args: any[]) => FactoryProvider;
export abstract class AsyncProvider {
/**
* Gets the static token for the async provider factory class.
*
Expand Down
2 changes: 1 addition & 1 deletion src/struct/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class StaticStore {
private static readonly map = new Map<string, symbol>();

public static get(key: string) {
public static get(key: string): symbol {
return StaticStore.map.get(key);
}

Expand Down
5 changes: 3 additions & 2 deletions test/decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import {
Infrastructure,
AsyncInject,
AsyncInjectable,
AsyncProvider,
AsyncProviderFactory,
} from '../src';

@AsyncInjectable
// @ts-ignore
class AsyncNameProvider extends AsyncProviderFactory {
class AsyncNameProvider extends AsyncProvider implements AsyncProviderFactory {
public create = () => ({
inject: [NameInfrastructureProvider],
provide: AsyncNameProvider.getToken(),
useFactory: (provider: NameInfrastructureProvider) => {
return provider;
},
}) as FactoryProvider<any>;
});
}

@Injectable()
Expand Down

0 comments on commit 7ab1b95

Please sign in to comment.