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

feat: improve type declaration for errors #147

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions lib/httpError.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface HttpError extends Error {

type UnknownError = Error | string | number | { [key: string]: any };

type HttpErrorCodes = 400 | '400' // BadRequest
export type HttpErrorCodes = 400 | '400' // BadRequest
| 401 | '401' // Unauthorized
| 402 | '402' // PaymentRequired
| 403 | '403' // Forbidden
Expand Down Expand Up @@ -52,7 +52,7 @@ type HttpErrorCodes = 400 | '400' // BadRequest
| 510 | '510' // NotExtended
| 511 | '511' // NetworkAuthenticationRequire

type HttpErrorNames = 'badRequest'
export type HttpErrorNames = 'badRequest'
| 'unauthorized'
| 'paymentRequired'
| 'forbidden'
Expand Down
62 changes: 45 additions & 17 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import { FastifyPluginCallback, FastifyReply } from 'fastify'
import { HttpErrors, HttpErrorCodes, HttpErrorNames } from "../lib/httpError"
import * as Errors from '../lib/httpError'

type FastifySensible = FastifyPluginCallback<fastifySensible.SensibleOptions>

type singleValueTypes = 'must-revalidate' |
'no-cache' |
'no-store' |
'no-transform' |
'public' |
'private' |
'proxy-revalidate' |
'immutable'
type singleValueTypes =
| 'must-revalidate'
| 'no-cache'
| 'no-store'
| 'no-transform'
| 'public'
| 'private'
| 'proxy-revalidate'
| 'immutable'

type multiValueTypes = 'max-age' |
's-maxage' |
'stale-while-revalidate' |
'stale-if-error'
type multiValueTypes =
| 'max-age'
| 's-maxage'
| 'stale-while-revalidate'
| 'stale-if-error'

type staleTypes = 'while-revalidate' | 'if-error'

type HttpErrorReplys = {
getHttpError: (code: HttpErrorCodes, message?: string) => FastifyReply;
} & Record<HttpErrorNames, (msg?: string) => FastifyReply>;

declare module 'fastify' {
namespace SensibleTypes {
type ToType<T> = [Error, T];
Expand All @@ -45,7 +44,7 @@ declare module 'fastify' {
httpErrors: HttpErrors;
}

interface FastifyReply extends HttpErrorReplys {
interface FastifyReply extends fastifySensible.HttpErrorReplys {
vary: {
(field: string | string[]): void;
append: (header: string, field: string | string[]) => string;
Expand All @@ -68,8 +67,37 @@ declare module 'fastify' {

declare namespace fastifySensible {
export interface SensibleOptions {
/**
* You can use this option to register a shared JSON Schema you can use in your routes.
*
* @example
* ```js
* fastify.register(require('@fastify/sensible'), {
* sharedSchemaId: 'httpError'
* })
*
* fastify.get('/async', {
* schema: {
* response: { 404: { $ref: 'httpError' } }
* }
* handler: async (req, reply) => {
* return reply.notFound()
* }
* })
* ```
*/
sharedSchemaId?: string;
}

export type HttpErrors = Errors.HttpErrors;
export type HttpErrorCodes = Errors.HttpErrorCodes;
export type HttpErrorNames = Errors.HttpErrorNames;


export type HttpErrorReplys = {
getHttpError: (code: HttpErrorCodes, message?: string) => FastifyReply;
} & Record<HttpErrorNames, (msg?: string) => FastifyReply>

export const fastifySensible: FastifySensible
export { fastifySensible as default }
}
Expand Down
8 changes: 6 additions & 2 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { expectType, expectAssignable, expectError } from 'tsd'
import { expectType, expectAssignable, expectError, expectNotAssignable } from 'tsd'
import fastify from 'fastify'
import fastifySensible from '..'
import fastifySensible, { SensibleOptions } from '..'

const app = fastify()

app.register(fastifySensible)

expectAssignable<SensibleOptions>({});
expectAssignable<SensibleOptions>({ sharedSchemaId: 'HttpError' });
expectNotAssignable<SensibleOptions>({ notSharedSchemaId: 'HttpError' });

app.get('/', (req, reply) => {
expectAssignable<typeof reply>(reply.badRequest())
expectAssignable<typeof reply>(reply.unauthorized())
Expand Down