diff --git a/index.js b/index.js index 5446ccd..0e7a317 100644 --- a/index.js +++ b/index.js @@ -82,3 +82,4 @@ module.exports = fp(fastifySensible, { module.exports.default = fastifySensible module.exports.fastifySensible = fastifySensible module.exports.httpErrors = httpErrors +module.exports.HttpError = httpErrors.HttpError diff --git a/lib/httpError.d.ts b/lib/httpError.d.ts index f23fc57..08649e4 100644 --- a/lib/httpError.d.ts +++ b/lib/httpError.d.ts @@ -1,6 +1,6 @@ -export interface HttpError extends Error { - status: number; - statusCode: number; +export declare class HttpError extends Error { + status: N; + statusCode: N; expose: boolean; headers?: { [key: string]: string; @@ -95,7 +95,7 @@ export type HttpErrorNames = 'badRequest' | 'networkAuthenticationRequired'; export type HttpErrors = { - HttpError: HttpError; + HttpError: typeof HttpError; getHttpError: (code: HttpErrorCodes, message?: string) => HttpError; createError: (...args: UnknownError[]) => HttpError; } & Record HttpError>; diff --git a/types/index.d.ts b/types/index.d.ts index c88d56a..b4861d9 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,5 @@ import { FastifyPluginCallback, FastifyReply } from 'fastify' -import { HttpErrors } from "../lib/httpError" +import { HttpErrors, HttpError } from "../lib/httpError" import * as Errors from '../lib/httpError' type FastifySensible = FastifyPluginCallback @@ -89,7 +89,8 @@ declare namespace fastifySensible { sharedSchemaId?: string; } - export type HttpError = Errors.HttpError; + export { HttpError }; + export type HttpErrors = Errors.HttpErrors; export type HttpErrorCodes = Errors.HttpErrorCodes; export type HttpErrorNames = Errors.HttpErrorNames; diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 5cc2b39..b572d62 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -57,7 +57,7 @@ app.get('/', (req, reply) => { }) app.get('/', (req, reply) => { - expectAssignable(app.httpErrors.createError(405, 'Method Not Allowed')) + expectAssignable(app.httpErrors.createError(405, 'Method Not Allowed')) }) app.get("/", (req, reply) => { @@ -69,45 +69,45 @@ app.get("/", (req, reply) => { app.get('/', async (req, reply) => { - expectAssignable(app.httpErrors.badRequest()) - expectAssignable(app.httpErrors.unauthorized()) - expectAssignable(app.httpErrors.paymentRequired()) - expectAssignable(app.httpErrors.forbidden()) - expectAssignable(app.httpErrors.notFound()) - expectAssignable(app.httpErrors.methodNotAllowed()) - expectAssignable(app.httpErrors.notAcceptable()) - expectAssignable(app.httpErrors.proxyAuthenticationRequired()) - expectAssignable(app.httpErrors.requestTimeout()) - expectAssignable(app.httpErrors.gone()) - expectAssignable(app.httpErrors.lengthRequired()) - expectAssignable(app.httpErrors.preconditionFailed()) - expectAssignable(app.httpErrors.payloadTooLarge()) - expectAssignable(app.httpErrors.uriTooLong()) - expectAssignable(app.httpErrors.unsupportedMediaType()) - expectAssignable(app.httpErrors.rangeNotSatisfiable()) - expectAssignable(app.httpErrors.expectationFailed()) - expectAssignable(app.httpErrors.imateapot()) - expectAssignable(app.httpErrors.unprocessableEntity()) - expectAssignable(app.httpErrors.locked()) - expectAssignable(app.httpErrors.failedDependency()) - expectAssignable(app.httpErrors.tooEarly()) - expectAssignable(app.httpErrors.upgradeRequired()) - expectAssignable(app.httpErrors.preconditionFailed()) - expectAssignable(app.httpErrors.tooManyRequests()) - expectAssignable(app.httpErrors.requestHeaderFieldsTooLarge()) - expectAssignable(app.httpErrors.unavailableForLegalReasons()) - expectAssignable(app.httpErrors.internalServerError()) - expectAssignable(app.httpErrors.notImplemented()) - expectAssignable(app.httpErrors.badGateway()) - expectAssignable(app.httpErrors.serviceUnavailable()) - expectAssignable(app.httpErrors.gatewayTimeout()) - expectAssignable(app.httpErrors.httpVersionNotSupported()) - expectAssignable(app.httpErrors.variantAlsoNegotiates()) - expectAssignable(app.httpErrors.insufficientStorage()) - expectAssignable(app.httpErrors.loopDetected()) - expectAssignable(app.httpErrors.bandwidthLimitExceeded()) - expectAssignable(app.httpErrors.notExtended()) - expectAssignable(app.httpErrors.networkAuthenticationRequired()) + expectAssignable(app.httpErrors.badRequest()) + expectAssignable(app.httpErrors.unauthorized()) + expectAssignable(app.httpErrors.paymentRequired()) + expectAssignable(app.httpErrors.forbidden()) + expectAssignable(app.httpErrors.notFound()) + expectAssignable(app.httpErrors.methodNotAllowed()) + expectAssignable(app.httpErrors.notAcceptable()) + expectAssignable(app.httpErrors.proxyAuthenticationRequired()) + expectAssignable(app.httpErrors.requestTimeout()) + expectAssignable(app.httpErrors.gone()) + expectAssignable(app.httpErrors.lengthRequired()) + expectAssignable(app.httpErrors.preconditionFailed()) + expectAssignable(app.httpErrors.payloadTooLarge()) + expectAssignable(app.httpErrors.uriTooLong()) + expectAssignable(app.httpErrors.unsupportedMediaType()) + expectAssignable(app.httpErrors.rangeNotSatisfiable()) + expectAssignable(app.httpErrors.expectationFailed()) + expectAssignable(app.httpErrors.imateapot()) + expectAssignable(app.httpErrors.unprocessableEntity()) + expectAssignable(app.httpErrors.locked()) + expectAssignable(app.httpErrors.failedDependency()) + expectAssignable(app.httpErrors.tooEarly()) + expectAssignable(app.httpErrors.upgradeRequired()) + expectAssignable(app.httpErrors.preconditionFailed()) + expectAssignable(app.httpErrors.tooManyRequests()) + expectAssignable(app.httpErrors.requestHeaderFieldsTooLarge()) + expectAssignable(app.httpErrors.unavailableForLegalReasons()) + expectAssignable(app.httpErrors.internalServerError()) + expectAssignable(app.httpErrors.notImplemented()) + expectAssignable(app.httpErrors.badGateway()) + expectAssignable(app.httpErrors.serviceUnavailable()) + expectAssignable(app.httpErrors.gatewayTimeout()) + expectAssignable(app.httpErrors.httpVersionNotSupported()) + expectAssignable(app.httpErrors.variantAlsoNegotiates()) + expectAssignable(app.httpErrors.insufficientStorage()) + expectAssignable(app.httpErrors.loopDetected()) + expectAssignable(app.httpErrors.bandwidthLimitExceeded()) + expectAssignable(app.httpErrors.notExtended()) + expectAssignable(app.httpErrors.networkAuthenticationRequired()) }) app.get('/', async (req, reply) => {