Skip to content

Commit bd9ef01

Browse files
committed
finish usage section
1 parent bce3dc4 commit bd9ef01

File tree

1 file changed

+129
-8
lines changed

1 file changed

+129
-8
lines changed

README.md

Lines changed: 129 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# fastify-override
22

3-
![ci](https://github.com/matthyk/fastify-override/actions/workflows/ci.yml/badge.svg)
3+
![CI](https://github.com/matthyk/fastify-override/actions/workflows/ci.yml/badge.svg)
44
![CodeQL](https://github.com/matthyk/fastify-override/actions/workflows/codeql.yml/badge.svg)
55

6-
7-
Override any plugins, decorators and hooks in your Fastify application. This is useful for mocking specific
6+
Override any plugin, decorator or hook in your Fastify application. This is useful for mocking specific
87
functionalities when testing your application. For the motivation of this plugin see [here](#why-is-this-plugin-useful).
98

10-
> You should use this plugin only for testing purposes.
9+
> Note: You should use this plugin only for testing purposes.
1110
1211
## Install
1312

@@ -19,11 +18,133 @@ npm install fastify-override --save-dev
1918

2019
| Plugin Version | Fastify Version | Node Versions |
2120
|----------------|:---------------:|---------------|
22-
| 1.x | 4.x | 16, 18 |
21+
| 1.x | 4.x | 18, 20 |
2322

2423
## Usage
2524

26-
TODO
25+
This plugin allows you to override specific plugins, decorators and hooks within the plugin hierarchy. This can happen
26+
from any level within the hierarchy, and you can also register this plugin multiple times. But as soon as the plugin is
27+
to be used, the register call must be awaited so that all subsequently added plugins, decorators and hooks are considered.
28+
29+
```js
30+
31+
app.decorate('foo', 'bar')
32+
33+
await app.register(fastifyOverride, {
34+
override: {
35+
decorators: {
36+
decorate: {
37+
foo: 'overridden',
38+
num: 42
39+
}
40+
}
41+
}
42+
})
43+
44+
app.decorate('num', 1)
45+
46+
app.foo // === 'bar'
47+
app.num // === 42
48+
```
49+
50+
As you can see above, only the decorators (and of course plugins and hooks) that were added after the plugin was
51+
registered are actually taken into account. There are some differences in the way the plugins, decorators and hooks are
52+
overridden. We will therefore briefly look at these 3 separately.
53+
54+
### Override Plugins
55+
56+
Only plugins that have been assigned a name with `fastify-plugin` can be overridden. This name can then be used to
57+
specify which plugins should be used to override it.
58+
59+
```
60+
const plg = fp(async app => {}, {
61+
name: 'myPlugin' // <-- Therefore, this must match with...
62+
})
63+
64+
await app.register(fastifyOverride, {
65+
override: {
66+
plugins: {
67+
myPlugin: fp(async () => {}) // ...this name
68+
}
69+
}
70+
})
71+
```
72+
73+
Keep in mind that the encapsulation behavior of the plugin can also be changed by using or not using `fastify-plugin`.
74+
75+
### Override Decorators
76+
77+
Decorators are identified by their name and type. Therefore, the plugin with the following options
78+
79+
```js
80+
{
81+
override: {
82+
decorators: {
83+
decorateReply: {
84+
num: 1
85+
}
86+
}
87+
}
88+
}
89+
```
90+
would override `app.decorateReply('sendHtml', 2)` but not `app.decorateRequest('sendHtml', 3)`.
91+
92+
### Override Hooks
93+
94+
For each [hook type](https://fastify.dev/docs/latest/Reference/Hooks/), you can provide one function. This function then
95+
overrides all hooks of this type. This can of course lead to different hook functions being overridden with the same function.
96+
Please note that only hooks that have been added via the `addHook` API are being checked.
97+
98+
## Options
99+
100+
The following options are available.
101+
102+
```js
103+
import Fastify from 'fastify'
104+
105+
const app = Fastify()
106+
107+
await app.register(import('fastify-override'), {
108+
override: {
109+
plugins: {},
110+
decorators: {
111+
decorate: {},
112+
decorateRequest: {},
113+
decorateReply: {}
114+
},
115+
hooks: {}
116+
}
117+
})
118+
```
119+
120+
### override
121+
122+
Use the `override` object to specify which decorators, which plugins and which hooks are to be overwritten.
123+
124+
```js
125+
import Fastify from 'fastify'
126+
127+
const app = Fastify()
128+
129+
await app.register(import('fastify-override'), {
130+
override: {
131+
plugins: {
132+
'@fastifyPostgres': async app => {}
133+
},
134+
decorators: {
135+
decorate: {
136+
db: {
137+
query: async () => {}
138+
}
139+
}
140+
},
141+
hooks: {
142+
onRequest: async (req, reply) => {}
143+
}
144+
}
145+
})
146+
```
147+
27148

28149
## Why is this plugin useful?
29150

@@ -124,7 +245,7 @@ t.test('test users routes', async t => {
124245
},
125246
decorators: {
126247
decorate: {
127-
// ...or just selected decorators
248+
// ...or just specific decorators
128249
pg: {
129250
query: () => 'mocked value'
130251
}
@@ -141,5 +262,5 @@ t.test('test users routes', async t => {
141262
})
142263
```
143264
144-
As we can see, this plugin allows us to overwrite entire plugins or just selected decorators. Thereby it does not matter
265+
As we can see, this plugin allows us to overwrite entire plugins or just specific decorators. Thereby it does not matter
145266
how deep in the plugin hierarchy the plugin and/or the decorators are registered.

0 commit comments

Comments
 (0)