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
content: A plugin is a way to decouple logic into smaller parts, defining reusable components across the server. Plugin can register by using `use`, registering a plugin will combine types between plugin and current instance, and the scope of hooks, and schema get merged too.
8
+
- - meta
9
+
- name: 'description'
10
+
content: A plugin is a way to decouple logic into smaller parts, defining reusable components across the server. Plugin can register by using `use`, registering a plugin will combine types between plugin and current instance, and the scope of hooks, and schema get merged too.
11
11
12
-
- - meta
13
-
- property: 'og:description'
14
-
content: A plugin is a way to decouple logic into smaller parts, defining reusable components across the server. Plugin can register by using `use`, registering a plugin will combine types between plugin and current instance, and the scope of hooks, and schema get merged too.
12
+
- - meta
13
+
- property: 'og:description'
14
+
content: A plugin is a way to decouple logic into smaller parts, defining reusable components across the server. Plugin can register by using `use`, registering a plugin will combine types between plugin and current instance, and the scope of hooks, and schema get merged too.
15
15
---
16
16
17
17
# Plugin
18
+
18
19
A plugin is a way to decouple logic into smaller parts, defining reusable components across the server.
19
20
20
21
Defining a plugin is as simple as defining a new Elysia instance:
22
+
21
23
```typescript
22
24
import { Elysia } from'elysia'
23
25
@@ -36,77 +38,80 @@ Plugin can register by using `use`.
36
38
Registering a plugin will combine types between plugin and current instance, and the scope of hooks, and schema get merged too.
37
39
38
40
## Separate file
41
+
39
42
Using plugin pattern, you can define decouple your logic into a separate file.
Config type will be inferred into `use`, generating auto completion and type strict as intend.
90
90
91
91
## Plugin deduplication
92
+
92
93
By default, Elysia will register any plugin and handle type definitions which when using multiple times will results in a multiple duplication of setting value or routes.
93
94
94
95
This can be fixed by providing name and optional seeds to help Elysia identify instance duplication:
96
+
95
97
```ts
96
98
import { Elysia } from'elysia'
97
99
98
-
const plugin = (config) =>newElysia({
99
-
name: 'my-plugin',
100
-
seed: config
101
-
})
102
-
.get(`/${config.prefix}/hi`, () =>'Hi')
100
+
const plugin = (config) =>
101
+
newElysia({
102
+
name: 'my-plugin',
103
+
seed: config
104
+
}).get(`${config.prefix}/hi`, () =>'Hi')
103
105
104
106
const app =newElysia()
105
-
.use(plugin({
106
-
prefix: '/v2'
107
-
}))
107
+
.use(
108
+
plugin({
109
+
prefix: '/v2'
110
+
})
111
+
)
108
112
.listen(8080)
109
113
```
110
114
111
115
## Official plugins
116
+
112
117
You can find pre-built plugins for Elysia at [plugins](/plugins/overview).
0 commit comments