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

types(index): export errors #210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import {
FastifyReply,
FastifyPluginCallback
} from 'fastify'
import { FastifyError } from '@fastify/error'

declare interface FastifyBearerAuthErrors {
FST_BEARER_AUTH_INVALID_KEYS_OPTION_TYPE: FastifyError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt we have some smart generic type logic for FastifyError

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...I have no idea.

FST_BEARER_AUTH_INVALID_LOG_LEVEL: FastifyError
FST_BEARER_AUTH_KEYS_OPTION_INVALID_KEY_TYPE: FastifyError
FST_BEARER_AUTH_INVALID_SPEC: FastifyError
FST_BEARER_AUTH_MISSING_AUTHORIZATION_HEADER: FastifyError
FST_BEARER_AUTH_INVALID_AUTHORIZATION_HEADER: FastifyError
}

declare module 'fastify' {
interface FastifyInstance {
Expand All @@ -29,6 +39,7 @@ declare namespace fastifyBearerAuth {
export type verifyBearerAuthFactory = (options: fastifyBearerAuth.FastifyBearerAuthOptions) => verifyBearerAuth

export const fastifyBearerAuth: FastifyBearerAuth
export const FastifyBearerAuthErrors: FastifyBearerAuthErrors
export { fastifyBearerAuth as default }
}

Expand Down
10 changes: 9 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fastify, { FastifyRequest } from 'fastify'
import { FastifyError } from '@fastify/error'
import { expectAssignable, expectType } from 'tsd'
import bearerAuth, { FastifyBearerAuthOptions, verifyBearerAuth, verifyBearerAuthFactory } from '..'
import bearerAuth, { FastifyBearerAuthErrors, FastifyBearerAuthOptions, verifyBearerAuth, verifyBearerAuthFactory } from '..'

const pluginOptions: FastifyBearerAuthOptions = {
keys: new Set(['foo']),
Expand Down Expand Up @@ -86,3 +87,10 @@ fastify().register(bearerAuth, pluginOptionsKeyArray)

expectType<verifyBearerAuth | undefined>(fastify().verifyBearerAuth)
expectType<verifyBearerAuthFactory | undefined>(fastify().verifyBearerAuthFactory)

expectType<FastifyError>(FastifyBearerAuthErrors.FST_BEARER_AUTH_INVALID_KEYS_OPTION_TYPE)
expectType<FastifyError>(FastifyBearerAuthErrors.FST_BEARER_AUTH_INVALID_LOG_LEVEL)
expectType<FastifyError>(FastifyBearerAuthErrors.FST_BEARER_AUTH_KEYS_OPTION_INVALID_KEY_TYPE)
expectType<FastifyError>(FastifyBearerAuthErrors.FST_BEARER_AUTH_INVALID_SPEC)
expectType<FastifyError>(FastifyBearerAuthErrors.FST_BEARER_AUTH_MISSING_AUTHORIZATION_HEADER)
expectType<FastifyError>(FastifyBearerAuthErrors.FST_BEARER_AUTH_INVALID_AUTHORIZATION_HEADER)