Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodenext compatibility #177

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import { ContextConfigDefault, FastifyInstance, FastifyPluginCallback, FastifyReply, FastifyRequest, FastifySchema, preHandlerHookHandler } from 'fastify';
import { RouteGenericInterface } from 'fastify/types/route';

export type FastifyAuthFunction = (
this: FastifyInstance,
request: FastifyRequest,
reply: FastifyReply,
done: (error?: Error) => void
) => void;

/**
* @link [`fastify-auth` options documentation](https://github.com/fastify/fastify-auth#options)
*/
export interface FastifyAuthPluginOptions {
/**
* The default relation between the functions. It can be either `or` or `and`.
*
* - Default value: `or`
*/
defaultRelation?: 'and' | 'or',
}
import { ContextConfigDefault, RouteGenericInterface, FastifyInstance, FastifyPluginCallback, FastifyReply, FastifyRequest, FastifySchema, preHandlerHookHandler } from 'fastify';

declare module 'fastify' {
interface FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> {
auth(
functions: FastifyAuthFunction[],
functions: fastifyAuth.FastifyAuthFunction[],
options?: {
relation?: 'and' | 'or',
run?: 'all'
Expand All @@ -32,5 +12,31 @@ declare module 'fastify' {
}
}

declare const fastifyAuth: FastifyPluginCallback<FastifyAuthPluginOptions>
export default fastifyAuth;
type FastifyAuth = FastifyPluginCallback<fastifyAuth.FastifyAuthPluginOptions>

declare namespace fastifyAuth {
export type FastifyAuthFunction = (
this: FastifyInstance,
request: FastifyRequest,
reply: FastifyReply,
done: (error?: Error) => void
) => void;

/**
* @link [`fastify-auth` options documentation](https://github.com/fastify/fastify-auth#options)
*/
export interface FastifyAuthPluginOptions {
/**
* The default relation between the functions. It can be either `or` or `and`.
*
* - Default value: `or`
*/
defaultRelation?: 'and' | 'or',
}

export const fastifyAuth: FastifyAuth
export { fastifyAuth as default }
}

declare function fastifyAuth(...params: Parameters<FastifyAuth>): ReturnType<FastifyAuth>
export = fastifyAuth
6 changes: 4 additions & 2 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const reusify = require('reusify')

const DEFAULT_RELATION = 'or'

function checkAuth (fastify, opts, next) {
function fastifyAuth (fastify, opts, next) {
if (opts.defaultRelation && opts.defaultRelation !== 'or' && opts.defaultRelation !== 'and') {
return next(new Error("The value of default relation should be one of ['or', 'and']"))
} else if (!opts.defaultRelation) {
Expand Down Expand Up @@ -132,7 +132,9 @@ function auth (pluginOptions) {
}
}

module.exports = fp(checkAuth, {
module.exports = fp(fastifyAuth, {
fastify: '4.x',
name: '@fastify/auth'
})
module.exports.default = fastifyAuth
module.exports.fastifyAuth = fastifyAuth