Skip to content

Commit

Permalink
refactor: use addHook instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Feb 4, 2025
1 parent 07037f0 commit 2d0bde0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ Defaults to `rfc6750`.
* `addHook`: If `false`, this plugin will not register `onRequest` hook automatically.
Instead it provides two decorations: `fastify.verifyBearerAuth` and
`fastify.verifyBearerAuthFactory`
* `hook`: Specify the hook to register the plugin, accepts `onRequest` or `preParsing`. Defaults to `onRequest` (optional)
* `addHook`: If `false`, this plugin will not register any hook automatically. Instead, it provides two decorations: `fastify.verifyBearerAuth` and
`fastify.verifyBearerAuthFactory`. If `true` or nullish, it will default to `onRequest`. You can also specify `onRequest` or `preParsing` to register the respective hook
* `verifyErrorLogLevel`: An optional string specifying the log level for verification errors.
It must be a valid log level supported by Fastify, or an exception will be thrown when
registering the plugin. By default, this option is set to `error`
Expand Down
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const { FST_BEARER_AUTH_INVALID_HOOK, FST_BEARER_AUTH_INVALID_LOG_LEVEL } = requ
const validHooks = new Set(['onRequest', 'preParsing'])

function fastifyBearerAuth (fastify, options, done) {
options = { addHook: true, verifyErrorLogLevel: 'error', ...options }
options = { verifyErrorLogLevel: 'error', ...options }
if (options.addHook === true || options.addHook == null) {
options.addHook = 'onRequest'
}

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

try {
if (options.addHook === true) {
options.hook ||= 'onRequest'
if (!validHooks.has(options.hook)) {
if (options.addHook) {
if (!validHooks.has(options.addHook)) {
done(new FST_BEARER_AUTH_INVALID_HOOK())
}

if (options.hook === 'preParsing') {
if (options.addHook === 'preParsing') {
const verifyBearerAuth = verifyBearerAuthFactory(options)
fastify.addHook('preParsing', (request, reply, _payload, done) => {
verifyBearerAuth(request, reply, done)
})
} else {
fastify.addHook(options.hook, verifyBearerAuthFactory(options))
fastify.addHook(options.addHook, verifyBearerAuthFactory(options))
}
} else {
fastify.decorate('verifyBearerAuthFactory', verifyBearerAuthFactory)
Expand Down
8 changes: 4 additions & 4 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const authorization = 'Bearer 123456'
test('onrequest hook used by default', async (t) => {
t.plan(9)
const fastify = Fastify()
fastify.register(plugin, { keys, hook: undefined }).get('/test', (_req, res) => {
fastify.register(plugin, { keys, addHook: undefined }).get('/test', (_req, res) => {
res.send({ hello: 'world' })
})

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

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

Expand Down Expand Up @@ -99,7 +99,7 @@ test('error when invalid hook specified', async (t) => {
t.plan(1)
const fastify = Fastify()
try {
await fastify.register(plugin, { keys, hook: 'onResponse' })
await fastify.register(plugin, { keys, addHook: 'onResponse' })
} catch (err) {
t.assert.strictEqual(err.message, 'options.hook must be either "onRequest" or "preParsing"')
}
Expand Down
3 changes: 1 addition & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ declare namespace fastifyBearerAuth {
contentType?: string | undefined;
bearerType?: string;
specCompliance?: 'rfc6749' | 'rfc6750';
addHook?: boolean | undefined;
hook?: 'onRequest' | 'preParsing' | undefined;
addHook?: boolean | 'onRequest' | 'preParsing' | undefined;
verifyErrorLogLevel?: string;
}

Expand Down
10 changes: 4 additions & 6 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const pluginOptionsAuthPromise: FastifyBearerAuthOptions = {
errorResponse: (err: Error) => { return { error: err.message } },
contentType: '',
bearerType: '',
hook: 'onRequest'
addHook: 'onRequest'
}

const pluginOptionsKeyArray: FastifyBearerAuthOptions = {
Expand All @@ -34,8 +34,7 @@ const pluginOptionsUndefined: FastifyBearerAuthOptions = {
errorResponse: (err: Error) => { return { error: err.message } },
contentType: undefined,
bearerType: undefined,
addHook: undefined,
hook: undefined
addHook: undefined
}

expectAssignable<{
Expand All @@ -52,7 +51,7 @@ expectAssignable<{
errorResponse?: (err: Error) => { error: string };
contentType?: string | undefined;
bearerType?: string;
addHook?: boolean | undefined;
addHook?: boolean | 'onRequest' | 'preParsing' | undefined;
}>(pluginOptionsKeyArray)

expectAssignable<{
Expand All @@ -61,8 +60,7 @@ expectAssignable<{
errorResponse?: (err: Error) => { error: string };
contentType?: string | undefined;
bearerType?: string;
addHook?: boolean | undefined;
hook?: 'onRequest' | 'preParsing' | undefined;
addHook?: boolean | 'onRequest' | 'preParsing' | undefined;
}>(pluginOptionsUndefined)

expectAssignable<{
Expand Down

0 comments on commit 2d0bde0

Please sign in to comment.