You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
unioc internally provides some decorator factories to create decorators that are compatible with both `legacy` and `stage3` environments.
4
+
5
+
## Differences Between the Two Environments
6
+
7
+
In January 2023, TypeScript 4.9 was officially released, introducing `TC39 stage 3` decorators to TypeScript for the first time. However, most `IoC` frameworks are still at the `legacy` stage and are not yet compatible with `stage3` decorators. At this point in time, to promote the development of the `TS` community, unioc internally provides decorator factory functions for creating decorators that are compatible with both `legacy` and `stage3`, as well as a series of `metadata` scanners for setting/scanning metadata and a series of APIs compatible with `emitDecoratorMetadata` (i.e., the `reflect-metadata` package).
8
+
9
+
> [!NOTE]
10
+
> The related APIs are stored in `unioc/meta` and `unioc/decorator` and support `tree-shaking`. Even if you don't use the core container API of `unioc`, you can use these APIs to create decorators, making your decorators more compatible and allowing them to be used in both `legacy` and `stage3` environments 🎉
11
+
>
12
+
> This may be very useful for some decorator library developers, such as libraries that use decorators for data validation, etc.
> Note that for plugin development, we assume you already have sufficient intermediate to advanced knowledge of TypeScript, as this involves more advanced content related to IoC core.
-`Container`: There is an internal container that stores all registered classes;
15
+
-`Bootstrap`: An abstract class that defines the startup process of an App;
16
+
-`Plugin`: Freely modify the bootstrap and container, controlling each stage of a class's initialization, execution, destruction, etc.
17
+
18
+
Plugins have the same level of authority as the bootstrap, giving them extreme flexibility. Below, we will explain in detail how to write a unioc plugin.
19
+
20
+
unioc provides a plugin system similar to the [Rollup plugin](https://rollupjs.org/plugin-development) system. However, compared to Rollup's hooks, unioc's hooks use `simple yet powerful``single` English words corresponding to each processing stage. Since this is a runtime framework, unlike Rollup which operates at compile time, it doesn't have the complex hook execution logic that Rollup has. Below is a simple diagram describing when each plugin hook will be executed.
21
+
22
+
<divalign="center"my="10">
23
+
24
+
```mermaid
25
+
flowchart TB
26
+
install("install()") --> |"installed, when try to resolve a class"| resolve
If you currently have a `nest.js` project, you can easily migrate from `nest.js` to `unioc`.
4
+
5
+
> [!WARNING]
6
+
>
7
+
> Currently, the `nest.js` adapter for `unioc` is still in the experimental stage and only supports basic `Restful` API functionality. We will continue to improve other features in the coming time.
8
+
9
+
## Installation
10
+
11
+
```bash
12
+
pnpm i unioc
13
+
```
14
+
15
+
## Usage
16
+
17
+
Change your bootstrap code to the following:
18
+
19
+
```ts twoslash
20
+
// @noErrors
21
+
importprocessfrom'node:process'
22
+
import { NestJS } from'unioc/adapter-nestjs'
23
+
import { ExpressApp } from'unioc/web-express'
24
+
import { AppModule } from'./app.module'
25
+
26
+
asyncfunction bootstrap() {
27
+
// Create an Express application
28
+
const app =awaitnewExpressApp()
29
+
30
+
// Use NestJS adapter and import your main module
31
+
app.use(NestJS, {
32
+
imports: [AppModule],
33
+
} satisfiesNestJS.Options)
34
+
35
+
// Run the application
36
+
awaitapp.run(process.env.PORT||3616)
37
+
}
38
+
bootstrap()
39
+
```
40
+
41
+
Currently, `nest.js` has not adapted plugins like `@nestjs/swagger` that are directly adapted by modifying the `bootstrap`. A unified `swagger` plugin will be planned in the future.
0 commit comments