Skip to content

Commit 20ca569

Browse files
committed
Issue-84: fix plugin page in docs
1 parent 4b5dcd5 commit 20ca569

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

docs/concept/plugin.md

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
---
22
title: Plugin - ElysiaJS
33
head:
4-
- - meta
5-
- property: 'og:title'
6-
content: Plugin - ElysiaJS
4+
- - meta
5+
- property: 'og:title'
6+
content: Plugin - ElysiaJS
77

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.
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.
1111

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.
1515
---
1616

1717
# Plugin
18+
1819
A plugin is a way to decouple logic into smaller parts, defining reusable components across the server.
1920

2021
Defining a plugin is as simple as defining a new Elysia instance:
22+
2123
```typescript
2224
import { Elysia } from 'elysia'
2325

@@ -36,77 +38,80 @@ Plugin can register by using `use`.
3638
Registering a plugin will combine types between plugin and current instance, and the scope of hooks, and schema get merged too.
3739

3840
## Separate file
41+
3942
Using plugin pattern, you can define decouple your logic into a separate file.
43+
4044
```ts
4145
// plugin.ts
42-
export const plugin = new Elysia()
43-
.get('/from-plugin', () => 'Hi')
46+
export const plugin = new Elysia().get('/from-plugin', () => 'Hi')
4447

4548
// main.ts
4649
import { plugin } from './plugin'
4750

48-
const app = new Elysia()
49-
.use(plugin)
50-
.listen(8080)
51+
const app = new Elysia().use(plugin).listen(8080)
5152
```
5253

5354
## Functional callback
55+
5456
You can also define a function callback to inline the plugin.
5557

5658
```ts
5759
// plugin.ts
58-
export const plugin = (app: Elysia) => app
59-
.get('/from-plugin', () => 'Hi')
60+
export const plugin = (app: Elysia) => app.get('/from-plugin', () => 'Hi')
6061

6162
// main.ts
6263
import { plugin } from './plugin'
6364

64-
const app = new Elysia()
65-
.use(plugin)
66-
.listen(8080)
65+
const app = new Elysia().use(plugin).listen(8080)
6766
```
6867

6968
Functional callback will allows use to access main instance values like routes schema, store.
7069

7170
## Config
71+
7272
You can customize plugin by creating function to return callback which accepts Elysia.
7373

7474
```typescript
7575
import { Elysia } from 'elysia'
7676

77-
const plugin = <const Prefix>({
78-
prefix = '/v1'
79-
}: { prefix: Prefix }) => new Elysia({ prefix })
80-
.get(`/${prefix}/hi`, () => 'Hi')
77+
const plugin = <const Prefix>({ prefix = '/v1' }: { prefix: Prefix }) =>
78+
new Elysia({ prefix }).get(`/hi`, () => 'Hi')
8179

8280
const app = new Elysia()
83-
.use(plugin({
84-
prefix: '/v2'
85-
}))
81+
.use(
82+
plugin({
83+
prefix: '/v2'
84+
})
85+
)
8686
.listen(8080)
8787
```
8888

8989
Config type will be inferred into `use`, generating auto completion and type strict as intend.
9090

9191
## Plugin deduplication
92+
9293
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.
9394

9495
This can be fixed by providing name and optional seeds to help Elysia identify instance duplication:
96+
9597
```ts
9698
import { Elysia } from 'elysia'
9799

98-
const plugin = (config) => new Elysia({
99-
name: 'my-plugin',
100-
seed: config
101-
})
102-
.get(`/${config.prefix}/hi`, () => 'Hi')
100+
const plugin = (config) =>
101+
new Elysia({
102+
name: 'my-plugin',
103+
seed: config
104+
}).get(`${config.prefix}/hi`, () => 'Hi')
103105

104106
const app = new Elysia()
105-
.use(plugin({
106-
prefix: '/v2'
107-
}))
107+
.use(
108+
plugin({
109+
prefix: '/v2'
110+
})
111+
)
108112
.listen(8080)
109113
```
110114

111115
## Official plugins
116+
112117
You can find pre-built plugins for Elysia at [plugins](/plugins/overview).

0 commit comments

Comments
 (0)