Skip to content

Commit 2d0bde0

Browse files
committed
refactor: use addHook instead
1 parent 07037f0 commit 2d0bde0

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

Readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ Defaults to `rfc6750`.
8282
* `addHook`: If `false`, this plugin will not register `onRequest` hook automatically.
8383
Instead it provides two decorations: `fastify.verifyBearerAuth` and
8484
`fastify.verifyBearerAuthFactory`
85-
* `hook`: Specify the hook to register the plugin, accepts `onRequest` or `preParsing`. Defaults to `onRequest` (optional)
85+
* `addHook`: If `false`, this plugin will not register any hook automatically. Instead, it provides two decorations: `fastify.verifyBearerAuth` and
86+
`fastify.verifyBearerAuthFactory`. If `true` or nullish, it will default to `onRequest`. You can also specify `onRequest` or `preParsing` to register the respective hook
8687
* `verifyErrorLogLevel`: An optional string specifying the log level for verification errors.
8788
It must be a valid log level supported by Fastify, or an exception will be thrown when
8889
registering the plugin. By default, this option is set to `error`

index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const { FST_BEARER_AUTH_INVALID_HOOK, FST_BEARER_AUTH_INVALID_LOG_LEVEL } = requ
1111
const validHooks = new Set(['onRequest', 'preParsing'])
1212

1313
function fastifyBearerAuth (fastify, options, done) {
14-
options = { addHook: true, verifyErrorLogLevel: 'error', ...options }
14+
options = { verifyErrorLogLevel: 'error', ...options }
15+
if (options.addHook === true || options.addHook == null) {
16+
options.addHook = 'onRequest'
17+
}
1518

1619
if (
1720
Object.hasOwn(fastify.log, 'error') === false ||
@@ -32,19 +35,18 @@ function fastifyBearerAuth (fastify, options, done) {
3235
}
3336

3437
try {
35-
if (options.addHook === true) {
36-
options.hook ||= 'onRequest'
37-
if (!validHooks.has(options.hook)) {
38+
if (options.addHook) {
39+
if (!validHooks.has(options.addHook)) {
3840
done(new FST_BEARER_AUTH_INVALID_HOOK())
3941
}
4042

41-
if (options.hook === 'preParsing') {
43+
if (options.addHook === 'preParsing') {
4244
const verifyBearerAuth = verifyBearerAuthFactory(options)
4345
fastify.addHook('preParsing', (request, reply, _payload, done) => {
4446
verifyBearerAuth(request, reply, done)
4547
})
4648
} else {
47-
fastify.addHook(options.hook, verifyBearerAuthFactory(options))
49+
fastify.addHook(options.addHook, verifyBearerAuthFactory(options))
4850
}
4951
} else {
5052
fastify.decorate('verifyBearerAuthFactory', verifyBearerAuthFactory)

test/hooks.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const authorization = 'Bearer 123456'
1111
test('onrequest hook used by default', async (t) => {
1212
t.plan(9)
1313
const fastify = Fastify()
14-
fastify.register(plugin, { keys, hook: undefined }).get('/test', (_req, res) => {
14+
fastify.register(plugin, { keys, addHook: undefined }).get('/test', (_req, res) => {
1515
res.send({ hello: 'world' })
1616
})
1717

@@ -40,7 +40,7 @@ test('onrequest hook used by default', async (t) => {
4040
test('preParsing hook used when specified', async (t) => {
4141
t.plan(9)
4242
const fastify = Fastify()
43-
fastify.register(plugin, { keys, hook: 'preParsing' }).get('/test', (_req, res) => {
43+
fastify.register(plugin, { keys, addHook: 'preParsing' }).get('/test', (_req, res) => {
4444
res.send({ hello: 'world' })
4545
})
4646

@@ -69,7 +69,7 @@ test('preParsing hook used when specified', async (t) => {
6969
test('onrequest hook used when specified', async (t) => {
7070
t.plan(9)
7171
const fastify = Fastify()
72-
fastify.register(plugin, { keys, hook: 'onRequest' }).get('/test', (_req, res) => {
72+
fastify.register(plugin, { keys, addHook: 'onRequest' }).get('/test', (_req, res) => {
7373
res.send({ hello: 'world' })
7474
})
7575

@@ -99,7 +99,7 @@ test('error when invalid hook specified', async (t) => {
9999
t.plan(1)
100100
const fastify = Fastify()
101101
try {
102-
await fastify.register(plugin, { keys, hook: 'onResponse' })
102+
await fastify.register(plugin, { keys, addHook: 'onResponse' })
103103
} catch (err) {
104104
t.assert.strictEqual(err.message, 'options.hook must be either "onRequest" or "preParsing"')
105105
}

types/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ declare namespace fastifyBearerAuth {
2121
contentType?: string | undefined;
2222
bearerType?: string;
2323
specCompliance?: 'rfc6749' | 'rfc6750';
24-
addHook?: boolean | undefined;
25-
hook?: 'onRequest' | 'preParsing' | undefined;
24+
addHook?: boolean | 'onRequest' | 'preParsing' | undefined;
2625
verifyErrorLogLevel?: string;
2726
}
2827

types/index.test-d.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const pluginOptionsAuthPromise: FastifyBearerAuthOptions = {
1717
errorResponse: (err: Error) => { return { error: err.message } },
1818
contentType: '',
1919
bearerType: '',
20-
hook: 'onRequest'
20+
addHook: 'onRequest'
2121
}
2222

2323
const pluginOptionsKeyArray: FastifyBearerAuthOptions = {
@@ -34,8 +34,7 @@ const pluginOptionsUndefined: FastifyBearerAuthOptions = {
3434
errorResponse: (err: Error) => { return { error: err.message } },
3535
contentType: undefined,
3636
bearerType: undefined,
37-
addHook: undefined,
38-
hook: undefined
37+
addHook: undefined
3938
}
4039

4140
expectAssignable<{
@@ -52,7 +51,7 @@ expectAssignable<{
5251
errorResponse?: (err: Error) => { error: string };
5352
contentType?: string | undefined;
5453
bearerType?: string;
55-
addHook?: boolean | undefined;
54+
addHook?: boolean | 'onRequest' | 'preParsing' | undefined;
5655
}>(pluginOptionsKeyArray)
5756

5857
expectAssignable<{
@@ -61,8 +60,7 @@ expectAssignable<{
6160
errorResponse?: (err: Error) => { error: string };
6261
contentType?: string | undefined;
6362
bearerType?: string;
64-
addHook?: boolean | undefined;
65-
hook?: 'onRequest' | 'preParsing' | undefined;
63+
addHook?: boolean | 'onRequest' | 'preParsing' | undefined;
6664
}>(pluginOptionsUndefined)
6765

6866
expectAssignable<{

0 commit comments

Comments
 (0)