Skip to content

Commit 409fd9e

Browse files
authored
Merge pull request #97 from Sk0voroda/issue-84
Issue-84: fix plugin page in docs
2 parents 375be64 + 83d5091 commit 409fd9e

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

docs/concept/plugin.md

Lines changed: 39 additions & 35 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 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.
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 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.
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,79 @@ Plugin can be registered by using `use` method.
3638
Registering a plugin will combine types between plugin and current instance, and the scope of hooks and schema get merged as well.
3739

3840
## 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.
42+
4043
```ts
4144
// plugin.ts
42-
export const plugin = new Elysia()
43-
.get('/from-plugin', () => 'Hi')
45+
export const plugin = new Elysia().get('/from-plugin', () => 'Hi')
4446

4547
// main.ts
4648
import { plugin } from './plugin'
4749

48-
const app = new Elysia()
49-
.use(plugin)
50-
.listen(8080)
50+
const app = new Elysia().use(plugin).listen(8080)
5151
```
5252

5353
## Functional callback
54+
5455
You can also define a function callback to inline the plugin.
5556

5657
```ts
5758
// plugin.ts
58-
export const plugin = (app: Elysia) => app
59-
.get('/from-plugin', () => 'Hi')
59+
export const plugin = (app: Elysia) => app.get('/from-plugin', () => 'Hi')
6060

6161
// main.ts
6262
import { plugin } from './plugin'
6363

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

6967
Functional callback will allow user to access main instance values like routes schema, store.
7068

7169
## Config
70+
7271
You can customize plugin by creating function to return callback which accepts Elysia.
7372

7473
```typescript
7574
import { Elysia } from 'elysia'
7675

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

8279
const app = new Elysia()
83-
.use(plugin({
84-
prefix: '/v2'
85-
}))
80+
.use(
81+
plugin({
82+
prefix: '/v2'
83+
})
84+
)
8685
.listen(8080)
8786
```
8887

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

9190
## Plugin deduplication
91+
9292
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.
9393

9494
This can be fixed by providing name and optional seeds to help Elysia identify instance duplication:
95+
9596
```ts
9697
import { Elysia } from 'elysia'
9798

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

104105
const app = new Elysia()
105-
.use(plugin({
106-
prefix: '/v2'
107-
}))
106+
.use(
107+
plugin({
108+
prefix: '/v2'
109+
})
110+
)
108111
.listen(8080)
109112
```
110113

111114
## Official plugins
115+
112116
You can find pre-built plugins for Elysia at [plugins](/plugins/overview).

0 commit comments

Comments
 (0)