Skip to content

Commit

Permalink
feat: add decorator for asynchronous injection and optimize previous …
Browse files Browse the repository at this point in the history
…module registry
  • Loading branch information
ChoGathK committed Aug 12, 2022
1 parent 9a401ea commit abea2c6
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 208 deletions.
20 changes: 9 additions & 11 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Vodyani core

👩🏻‍🚀 "core" is the top-level design package for vodyani scaffolding, providing a single responsibility module registrar for the different tiers.
👩🏻‍🚀 Vodyani corevodyani 脚手架的顶层设计,包含:按层级划分的单一职责模块注册装饰器、异步提供者注入装饰器。

[![Npm](https://img.shields.io/npm/v/@vodyani/core/latest.svg)](https://www.npmjs.com/package/@vodyani/core)
[![Npm](https://img.shields.io/npm/v/@vodyani/core/beta.svg)](https://www.npmjs.com/package/@vodyani/core)
Expand All @@ -13,20 +13,18 @@

## Installation

```bash
```sh
npm install @vodyani/core
```

## Usage
## Getting started

|Features|Type|Description|
|:-:|:-:|:-:|
|InfrastructureRegister|class decorator|Used to replace the **@Module** from **Nest.js** and provide a single responsible `infrastructure module` registrar.|
|DomainRegister|class decorator|Used to replace the **@Module** from **Nest.js** and provide a single responsible `domain module` registrar.|
|ApiRegister|class decorator|Used to replace the **@Module** from **Nest.js** and provide a single responsible `application interface module` registrar.|
|ContainerRegister|class decorator|Used to replace the **@Module** from **Nest.js** and provide a single responsible `application container module` registrar.|
|InstanceContainer|class|The Structure for storing multiple instances.|
|AsyncProviderFactory|interface|The factory class for implementing to create a `Nest.js` factory providers.|
- [使用文档 📚](https://vodyani.vercel.app/docs/other/core)
- [Documentation 📚](https://vodyani.vercel.app/en/docs/other/core)

## Questions

- [Discussions 🧐](https://github.com/vodyani/core/discussions)

## Team

Expand Down
201 changes: 108 additions & 93 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 0 additions & 30 deletions src/base.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './interface';
36 changes: 15 additions & 21 deletions src/common.ts → src/common/interface.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { FactoryProvider, InjectionToken, ModuleMetadata } from '@nestjs/common';
import { FactoryProvider, ModuleMetadata } from '@nestjs/common';

export interface Class<T = any> extends Function {
new (...args: any[]): T;
}
/**
* Infrastructure Module Registration Options.
*/
export interface InfrastructureRegisterOptions {
/**
* Other modules that need to be imported.
*/
imports?: ModuleMetadata['imports'];
import?: ModuleMetadata['imports'];
/**
* Providers to be exported in the current module.
*/
exports?: ModuleMetadata['exports'];
export?: ModuleMetadata['exports'];
/**
* Providers in the current module.
*
Expand All @@ -25,7 +29,7 @@ export interface DomainRegisterOptions {
/**
* Other modules that need to be imported.
*/
imports?: ModuleMetadata['imports'];
import?: ModuleMetadata['imports'];
/**
* Service entry for the domain module.
*
Expand All @@ -44,10 +48,6 @@ export interface DomainRegisterOptions {
* Infrastructure module call and wrapper provider for domain modules.
*/
provider?: ModuleMetadata['providers'];
/**
* Entity for domain modules.
*/
entity?: ModuleMetadata['providers'];
}
/**
* Api Module Registration Options.
Expand All @@ -56,7 +56,7 @@ export interface ApiRegisterOptions {
/**
* Other modules that need to be imported.
*/
imports: ModuleMetadata['imports'];
import?: ModuleMetadata['imports'];
/**
* Controller for api modules.
*/
Expand All @@ -77,7 +77,7 @@ export interface ContainerRegisterOptions {
/**
* Api modules that need to be imported.
*/
api: ModuleMetadata['imports'];
api?: ModuleMetadata['imports'];
/**
* Infrastructure modules that need to be imported.
*/
Expand All @@ -88,24 +88,18 @@ export interface ContainerRegisterOptions {
aop?: ModuleMetadata['providers'];
}
/**
* Asynchronous provider factory for creating [factory provider objects](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory).
* Asynchronous provider factory for creating
*
* @see: [factory provider objects](https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory)
*/
export interface AsyncProviderFactory {
/**
* Get dependency injection `token` for factory providers.
*
* @returns InjectionToken
*
* @publicApi
*/
getToken: () => InjectionToken;
/**
* Create a factory provider by specifying the creation parameters externally.
*
* @param ...args Arbitrary length, arbitrary type of parameters
* @returns FactoryProvider
*
* @publicApi
*/
createProvider: (...args: any[]) => FactoryProvider;
create: (...args: any[]) => FactoryProvider;
}

27 changes: 27 additions & 0 deletions src/decorator/async-inject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Inject } from '@nestjs/common';

import { StaticStore } from '../structs';
import { getToken } from '../method';
import { AsyncProviderFactory, Class } from '../common';

/**
* Use this decorator to handle dependency management for asynchronous provider factory classes.
*
* @param target The asynchronous provider factory class.
*
* @publicApi
*/
export function AsyncInjectable(target: Class<AsyncProviderFactory>) {
StaticStore.set(target.name);
}
/**
* Use this decorator to inject the asynchronous provider into the class instantiation process.
*
* @param target The asynchronous provider factory class.
*
* @publicApi
*/
export function AsyncInject(target: Class<AsyncProviderFactory>) {
const token = getToken(target);
return Inject(token);
}
2 changes: 2 additions & 0 deletions src/decorator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './async-inject';
export * from './module';
45 changes: 19 additions & 26 deletions src/decorator.ts → src/decorator/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ContainerRegisterOptions,
DomainRegisterOptions,
InfrastructureRegisterOptions,
} from './common';
} from '../common';

/**
* Decorator of Infrastructure Module Register.
Expand All @@ -17,13 +17,11 @@ import {
*
* @publicApi
*/
export function InfrastructureRegister(options: InfrastructureRegisterOptions) {
const { imports, exports, provider } = options;

export function Infrastructure(options: InfrastructureRegisterOptions) {
return Module({
imports,
exports: exports || [],
providers: provider || [],
imports: options.import || [],
exports: options.export || [],
providers: options.provider,
});
}
/**
Expand All @@ -36,18 +34,15 @@ export function InfrastructureRegister(options: InfrastructureRegisterOptions) {
*
* @publicApi
*/
export function DomainRegister(options: DomainRegisterOptions) {
const { imports, service, manager, repository, provider, entity } = options;

export function Domain(options: DomainRegisterOptions) {
return Module({
imports,
exports: service,
imports: options.import || [],
exports: options.service,
providers: [
...service,
...(manager || []),
...(repository || []),
...(provider || []),
...(entity || []),
...options.service,
...(options.manager || []),
...(options.repository || []),
...(options.provider || []),
],
});
}
Expand All @@ -61,15 +56,13 @@ export function DomainRegister(options: DomainRegisterOptions) {
*
* @publicApi
*/
export function ApiRegister(options: ApiRegisterOptions) {
const { imports, aop, consumer, controller } = options;

export function Api(options: ApiRegisterOptions) {
return Module({
imports,
controllers: controller,
imports: options.import || [],
controllers: options.controller,
providers: [
...(aop || []),
...(consumer || []),
...(options.aop || []),
...(options.consumer || []),
],
});
}
Expand All @@ -83,12 +76,12 @@ export function ApiRegister(options: ApiRegisterOptions) {
*
* @publicApi
*/
export function ContainerRegister(options: ContainerRegisterOptions) {
export function Container(options: ContainerRegisterOptions) {
const { aop, api, infrastructure } = options;

return Module({
imports: [
...api,
...(api || []),
...(infrastructure || []),
],
providers: aop || [],
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './base';
export * from './common';
export * from './decorator';
export * from './method';
15 changes: 15 additions & 0 deletions src/method/async-inject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AsyncProviderFactory, Class } from '../common';
import { StaticStore } from '../structs';

/**
* The corresponding injected token value is obtained by passing in the asynchronous provider factory class.
*
* @param target The asynchronous provider factory class.
* @returns token (symbol)
*
* @publicApi
*/
export function getToken(target: Class<AsyncProviderFactory>) {
const token = StaticStore.get(target.name);
return token;
}
1 change: 1 addition & 0 deletions src/method/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './async-inject';
1 change: 1 addition & 0 deletions src/structs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './store';
11 changes: 11 additions & 0 deletions src/structs/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class StaticStore {
private static readonly map = new Map<string, symbol>();

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

public static set(key: string) {
StaticStore.map.set(key, Symbol(key));
}
}
13 changes: 0 additions & 13 deletions test/base.spec.ts

This file was deleted.

Loading

0 comments on commit abea2c6

Please sign in to comment.