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 be registered by using `use` method, registering a plugin will combine types between plugin and current instance, and the scope of hooks and schema get merged as well.
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 be registered by using `use` method, registering a plugin will combine types between plugin and current instance, and the scope of hooks and schema get merged as well.
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,79 @@ Plugin can be registered by using `use` method.
36
38
Registering a plugin will combine types between plugin and current instance, and the scope of hooks and schema get merged as well.
37
39
38
40
## Separate file
39
-
Using plugin pattern, you can separate your logic into a separate file.
41
+
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
89
91
90
## Plugin deduplication
91
+
92
92
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
93
94
94
This can be fixed by providing name and optional seeds to help Elysia identify instance duplication:
95
+
95
96
```ts
96
97
import { Elysia } from'elysia'
97
98
98
-
const plugin = (config) =>newElysia({
99
-
name: 'my-plugin',
100
-
seed: config
101
-
})
102
-
.get(`/${config.prefix}/hi`, () =>'Hi')
99
+
const plugin = (config) =>
100
+
newElysia({
101
+
name: 'my-plugin',
102
+
seed: config
103
+
}).get(`${config.prefix}/hi`, () =>'Hi')
103
104
104
105
const app =newElysia()
105
-
.use(plugin({
106
-
prefix: '/v2'
107
-
}))
106
+
.use(
107
+
plugin({
108
+
prefix: '/v2'
109
+
})
110
+
)
108
111
.listen(8080)
109
112
```
110
113
111
114
## Official plugins
115
+
112
116
You can find pre-built plugins for Elysia at [plugins](/plugins/overview).
0 commit comments