middie
Advanced tools
Comparing version 5.3.0 to 5.4.0
@@ -15,3 +15,3 @@ 'use strict' | ||
fastify | ||
.addHook('onRequest', runMiddie) | ||
.addHook(options.hook || 'onRequest', runMiddie) | ||
.addHook('onRegister', onRegister) | ||
@@ -18,0 +18,0 @@ |
{ | ||
"name": "middie", | ||
"version": "5.3.0", | ||
"version": "5.4.0", | ||
"description": "Middleware engine for Fastify", | ||
@@ -35,3 +35,3 @@ "main": "index.js", | ||
"@types/connect": "^3.4.33", | ||
"@types/node": "^15.0.0", | ||
"@types/node": "^17.0.0", | ||
"cors": "^2.8.5", | ||
@@ -45,4 +45,4 @@ "fastify": "^3.0.0", | ||
"tap": "^15.0.1", | ||
"tsd": "^0.17.0", | ||
"typescript": "4.3.4" | ||
"tsd": "^0.19.0", | ||
"typescript": "4.5.4" | ||
}, | ||
@@ -49,0 +49,0 @@ "dependencies": { |
@@ -26,3 +26,5 @@ # middie | ||
const fastify = Fastify() | ||
await fastify.register(require('middie')) | ||
await fastify.register(require('middie'), { | ||
hook: 'onRequest' // default | ||
}) | ||
// do you know we also have cors support? | ||
@@ -70,3 +72,3 @@ // https://github.com/fastify/fastify-cors | ||
Every registered middleware will be run during the `onRequest` hook phase, so the registration order is important. | ||
__Every registered middleware will be run during the `onRequest` hook phase__, so the registration order is important. | ||
Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Lifecycle/) documentation page to understand better how every request is executed. | ||
@@ -97,2 +99,31 @@ | ||
If you want to change the Fastify hook that the middleware will be attached to, pass a `hook` option like so: | ||
```js | ||
const fastify = require('fastify')() | ||
fastify | ||
.register(require('middie'), { hook: 'preHandler' }) | ||
.register(subsystem) | ||
async function subsystem (fastify, opts) { | ||
fastify.addHook('onRequest', async (req, reply) => { | ||
console.log('first') | ||
}) | ||
fastify.use((req, res, next) => { | ||
console.log('third') | ||
next() | ||
}) | ||
fastify.addHook('onRequest', async (req, reply) => { | ||
console.log('second') | ||
}) | ||
fastify.addHook('preHandler', async (req, reply) => { | ||
console.log('fourth') | ||
}) | ||
} | ||
``` | ||
### Restrict middleware execution to a certain path(s) | ||
@@ -99,0 +130,0 @@ |
@@ -628,1 +628,29 @@ 'use strict' | ||
}) | ||
test('register the middleware at a different hook', async t => { | ||
const fastify = Fastify() | ||
t.teardown(fastify.close) | ||
let onRequestCalled = false | ||
await fastify.register(middiePlugin, { | ||
hook: 'preHandler' | ||
}) | ||
fastify.use(function (req, res, next) { | ||
t.ok(onRequestCalled) | ||
next() | ||
}) | ||
fastify.addHook('onRequest', function (req, reply, next) { | ||
onRequestCalled = true | ||
next() | ||
}) | ||
fastify.get('/', async (req, reply) => { | ||
return { hello: 'world' } | ||
}) | ||
const res = await fastify.inject('/') | ||
t.same(res.json(), { hello: 'world' }) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
59793
1885
251