diff --git a/README.md b/README.md index f8f1db6..9097cf1 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,13 @@ If you set the `sharedSchemaId` option, a shared JSON Schema is added and can be ```js const fastify = require('fastify')() fastify.register(require('@fastify/sensible'), { - sharedSchemaId: 'httpError' + sharedSchemaId: 'HttpError' }) fastify.get('/async', { schema: { response: { - 404: { $ref: 'httpError' } + 404: { $ref: 'HttpError' } } } handler: async (req, reply) => { diff --git a/lib/httpError.d.ts b/lib/httpError.d.ts index 7325acb..2e7d4d3 100644 --- a/lib/httpError.d.ts +++ b/lib/httpError.d.ts @@ -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 @@ -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' diff --git a/types/index.d.ts b/types/index.d.ts index 2799cf7..b9fb13e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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 -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 FastifyReply>; - declare module 'fastify' { namespace SensibleTypes { type ToType = [Error, T]; @@ -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; @@ -68,11 +67,39 @@ declare module 'fastify' { declare namespace fastifySensible { export interface SensibleOptions { + /** + * This option registers a shared JSON Schema to be used by all response schemas. + * + * @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 FastifyReply> + export const fastifySensible: FastifySensible export { fastifySensible as default } } declare function fastifySensible(...params: Parameters): ReturnType -export = fastifySensible \ No newline at end of file +export = fastifySensible diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 1a93705..0911b76 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -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({}); +expectAssignable({ sharedSchemaId: 'HttpError' }); +expectNotAssignable({ notSharedSchemaId: 'HttpError' }); + app.get('/', (req, reply) => { expectAssignable(reply.badRequest()) expectAssignable(reply.unauthorized())