Skip to content

Commit

Permalink
types: improve typings (#202)
Browse files Browse the repository at this point in the history
* types: improve typings

* Apply suggestions from code review
  • Loading branch information
Uzlopak authored Aug 11, 2023
1 parent 5d9ef5c commit 813c2d8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "git+https://github.com/fastify/fastify-auth.git"
},
"main": "auth.js",
"types": "auth.d.ts",
"types": "types/index.d.ts",
"scripts": {
"clean": "rimraf authdb",
"lint": "standard",
Expand Down Expand Up @@ -46,9 +46,6 @@
"fastify-plugin": "^4.0.0",
"reusify": "^1.0.4"
},
"tsd": {
"directory": "test"
},
"publishConfig": {
"access": "public"
}
Expand Down
27 changes: 19 additions & 8 deletions auth.d.ts → types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
import { ContextConfigDefault, RouteGenericInterface, FastifyInstance, FastifyPluginCallback, FastifyReply, FastifyRequest, FastifySchema, preHandlerHookHandler } from 'fastify';
import {
ContextConfigDefault,
RouteGenericInterface,
FastifyInstance,
FastifyPluginCallback,
FastifyReply,
FastifyRequest,
FastifySchema,
preHandlerHookHandler
} from 'fastify';

declare module 'fastify' {
interface FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> {
auth(
functions: fastifyAuth.FastifyAuthFunction[] | (fastifyAuth.FastifyAuthFunction | fastifyAuth.FastifyAuthFunction[])[],
options?: {
relation?: 'and' | 'or',
run?: 'all'
relation?: fastifyAuth.FastifyAuthRelation;
run?: 'all';
}
): preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGenericInterface, ContextConfigDefault, FastifySchema, TypeProvider>;
): preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGenericInterface, ContextConfigDefault, FastifySchema, TypeProvider, Logger>;
}
}

type FastifyAuth = FastifyPluginCallback<fastifyAuth.FastifyAuthPluginOptions>

declare namespace fastifyAuth {
export type FastifyAuthRelation = 'and' | 'or'

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`
* @default 'or'
*/
defaultRelation?: 'and' | 'or',
defaultRelation?: FastifyAuthRelation;
}

export const fastifyAuth: FastifyAuth
export { fastifyAuth as default }
}
Expand Down
2 changes: 1 addition & 1 deletion test/auth.test-d.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fastify, { FastifyRequest, FastifyReply, preHandlerHookHandler, FastifyInstance } from 'fastify';
import fastifyAuth from '../auth'
import fastifyAuth from '..'
import { expectType } from 'tsd';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
import { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts'
Expand Down

0 comments on commit 813c2d8

Please sign in to comment.