From a09f3c52990c6309775117fcbe19069633418b02 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 28 Nov 2022 13:51:25 +0100 Subject: [PATCH] nodenext compatiblity --- index.js | 6 ++++-- types/index.d.ts | 37 ++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 84fdc4d..7b3c5bc 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const MissingOrBadAuthorizationHeader = createError( 401 ) -async function basicPlugin (fastify, opts) { +async function fastifyBasicAuth (fastify, opts) { if (typeof opts.validate !== 'function') { throw new Error('Basic Auth: Missing validate function') } @@ -80,7 +80,9 @@ function getAuthenticateHeader (authenticate) { throw new Error('Basic Auth: Invalid authenticate option') } -module.exports = fp(basicPlugin, { +module.exports = fp(fastifyBasicAuth, { fastify: '4.x', name: '@fastify/basic-auth' }) +module.exports.default = fastifyBasicAuth +module.exports.fastifyBasicAuth = fastifyBasicAuth diff --git a/types/index.d.ts b/types/index.d.ts index acc6211..4724b24 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -11,23 +11,30 @@ import { declare module 'fastify' { interface FastifyInstance { basicAuth: onRequestHookHandler | - preValidationHookHandler | - preHandlerHookHandler + preValidationHookHandler | + preHandlerHookHandler } } -export interface FastifyBasicAuthOptions { - validate( - this: FastifyInstance, - username: string, - password: string, - req: FastifyRequest, - reply: FastifyReply, - done: (err?: Error) => void - ): void | Promise; - authenticate?: boolean | { realm: string | ((req: FastifyRequest) => string) }; - header?: string; +type FastifyBasicAuth = FastifyPluginAsync + +declare namespace fastifyBasicAuth { + export interface FastifyBasicAuthOptions { + validate( + this: FastifyInstance, + username: string, + password: string, + req: FastifyRequest, + reply: FastifyReply, + done: (err?: Error) => void + ): void | Promise; + authenticate?: boolean | { realm: string | ((req: FastifyRequest) => string) }; + header?: string; + } + + export const fastifyBasicAuth: FastifyBasicAuth + export { fastifyBasicAuth as default } } -declare const fastifyBasicAuth: FastifyPluginAsync -export default fastifyBasicAuth; +declare function fastifyBasicAuth(...params: Parameters): ReturnType +export = fastifyBasicAuth