Skip to content

Commit 9acfd2d

Browse files
txchenmcollina
authored andcommitted
replace beforeHandler with preHandler (#36)
* replace beforeHandler with preHandler Fastify core is complaining Warning: The route option `beforeHandler` has been deprecated, use `preHandler` instead. This commit change the `beforeHandler` to `preHandler` * support beforeHandler for backward compatibility
1 parent 72797e5 commit 9acfd2d

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ request.
9090

9191
Rewrite the prefix to the specified string. Default: `''`.
9292

93-
### beforeHandler
93+
### preHandler
9494

95-
A `beforeHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
95+
A `preHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
9696

9797
### replyOptions
9898

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = async function (fastify, opts) {
77
throw new Error('upstream must be specified')
88
}
99

10-
const beforeHandler = opts.beforeHandler
10+
const preHandler = opts.preHandler || opts.beforeHandler
1111
const rewritePrefix = opts.rewritePrefix || ''
1212

1313
const fromOpts = Object.assign({}, opts)
@@ -40,8 +40,8 @@ module.exports = async function (fastify, opts) {
4040
done(null, req)
4141
}
4242

43-
fastify.all('/', { beforeHandler }, reply)
44-
fastify.all('/*', { beforeHandler }, reply)
43+
fastify.all('/', { preHandler }, reply)
44+
fastify.all('/*', { preHandler }, reply)
4545

4646
function reply (request, reply) {
4747
var dest = request.req.url

test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,38 @@ async function run () {
9696
t.deepEqual(resultRoot.body, { something: 'posted' })
9797
})
9898

99-
test('beforeHandler', async (t) => {
99+
test('preHandler', async (t) => {
100+
const server = Fastify()
101+
server.register(proxy, {
102+
upstream: `http://localhost:${origin.server.address().port}`,
103+
async preHandler (request, reply) {
104+
throw new Unauthorized()
105+
}
106+
})
107+
108+
await server.listen(0)
109+
t.tearDown(server.close.bind(server))
110+
111+
var errored = false
112+
try {
113+
await got(`http://localhost:${server.server.address().port}`)
114+
} catch (err) {
115+
t.equal(err.statusCode, 401)
116+
errored = true
117+
}
118+
t.ok(errored)
119+
120+
errored = false
121+
try {
122+
await got(`http://localhost:${server.server.address().port}/a`)
123+
} catch (err) {
124+
t.equal(err.statusCode, 401)
125+
errored = true
126+
}
127+
t.ok(errored)
128+
})
129+
130+
test('beforeHandler(deprecated)', async (t) => {
100131
const server = Fastify()
101132
server.register(proxy, {
102133
upstream: `http://localhost:${origin.server.address().port}`,

0 commit comments

Comments
 (0)