Skip to content

Commit

Permalink
fix: export HttpError as class instead of interface (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrishi authored Dec 14, 2024
1 parent c32d674 commit 72c6863
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions lib/httpError.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface HttpError extends Error {
status: number;
statusCode: number;
export declare class HttpError<N extends number = number> extends Error {
status: N;
statusCode: N;
expose: boolean;
headers?: {
[key: string]: string;
Expand Down Expand Up @@ -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<HttpErrorNames, (msg?: string) => HttpError>;
Expand Down
5 changes: 3 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<fastifySensible.FastifySensibleOptions>
Expand Down Expand Up @@ -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;
Expand Down
80 changes: 40 additions & 40 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ app.get('/', (req, reply) => {
})

app.get('/', (req, reply) => {
expectAssignable<Error>(app.httpErrors.createError(405, 'Method Not Allowed'))
expectAssignable<HttpError>(app.httpErrors.createError(405, 'Method Not Allowed'))
})

app.get("/", (req, reply) => {
Expand All @@ -69,45 +69,45 @@ app.get("/", (req, reply) => {


app.get('/', async (req, reply) => {
expectAssignable<Error>(app.httpErrors.badRequest())
expectAssignable<Error>(app.httpErrors.unauthorized())
expectAssignable<Error>(app.httpErrors.paymentRequired())
expectAssignable<Error>(app.httpErrors.forbidden())
expectAssignable<Error>(app.httpErrors.notFound())
expectAssignable<Error>(app.httpErrors.methodNotAllowed())
expectAssignable<Error>(app.httpErrors.notAcceptable())
expectAssignable<Error>(app.httpErrors.proxyAuthenticationRequired())
expectAssignable<Error>(app.httpErrors.requestTimeout())
expectAssignable<Error>(app.httpErrors.gone())
expectAssignable<Error>(app.httpErrors.lengthRequired())
expectAssignable<Error>(app.httpErrors.preconditionFailed())
expectAssignable<Error>(app.httpErrors.payloadTooLarge())
expectAssignable<Error>(app.httpErrors.uriTooLong())
expectAssignable<Error>(app.httpErrors.unsupportedMediaType())
expectAssignable<Error>(app.httpErrors.rangeNotSatisfiable())
expectAssignable<Error>(app.httpErrors.expectationFailed())
expectAssignable<Error>(app.httpErrors.imateapot())
expectAssignable<Error>(app.httpErrors.unprocessableEntity())
expectAssignable<Error>(app.httpErrors.locked())
expectAssignable<Error>(app.httpErrors.failedDependency())
expectAssignable<Error>(app.httpErrors.tooEarly())
expectAssignable<Error>(app.httpErrors.upgradeRequired())
expectAssignable<Error>(app.httpErrors.preconditionFailed())
expectAssignable<Error>(app.httpErrors.tooManyRequests())
expectAssignable<Error>(app.httpErrors.requestHeaderFieldsTooLarge())
expectAssignable<Error>(app.httpErrors.unavailableForLegalReasons())
expectAssignable<Error>(app.httpErrors.internalServerError())
expectAssignable<Error>(app.httpErrors.notImplemented())
expectAssignable<Error>(app.httpErrors.badGateway())
expectAssignable<Error>(app.httpErrors.serviceUnavailable())
expectAssignable<Error>(app.httpErrors.gatewayTimeout())
expectAssignable<Error>(app.httpErrors.httpVersionNotSupported())
expectAssignable<Error>(app.httpErrors.variantAlsoNegotiates())
expectAssignable<Error>(app.httpErrors.insufficientStorage())
expectAssignable<Error>(app.httpErrors.loopDetected())
expectAssignable<Error>(app.httpErrors.bandwidthLimitExceeded())
expectAssignable<Error>(app.httpErrors.notExtended())
expectAssignable<Error>(app.httpErrors.networkAuthenticationRequired())
expectAssignable<HttpError>(app.httpErrors.badRequest())
expectAssignable<HttpError>(app.httpErrors.unauthorized())
expectAssignable<HttpError>(app.httpErrors.paymentRequired())
expectAssignable<HttpError>(app.httpErrors.forbidden())
expectAssignable<HttpError>(app.httpErrors.notFound())
expectAssignable<HttpError>(app.httpErrors.methodNotAllowed())
expectAssignable<HttpError>(app.httpErrors.notAcceptable())
expectAssignable<HttpError>(app.httpErrors.proxyAuthenticationRequired())
expectAssignable<HttpError>(app.httpErrors.requestTimeout())
expectAssignable<HttpError>(app.httpErrors.gone())
expectAssignable<HttpError>(app.httpErrors.lengthRequired())
expectAssignable<HttpError>(app.httpErrors.preconditionFailed())
expectAssignable<HttpError>(app.httpErrors.payloadTooLarge())
expectAssignable<HttpError>(app.httpErrors.uriTooLong())
expectAssignable<HttpError>(app.httpErrors.unsupportedMediaType())
expectAssignable<HttpError>(app.httpErrors.rangeNotSatisfiable())
expectAssignable<HttpError>(app.httpErrors.expectationFailed())
expectAssignable<HttpError>(app.httpErrors.imateapot())
expectAssignable<HttpError>(app.httpErrors.unprocessableEntity())
expectAssignable<HttpError>(app.httpErrors.locked())
expectAssignable<HttpError>(app.httpErrors.failedDependency())
expectAssignable<HttpError>(app.httpErrors.tooEarly())
expectAssignable<HttpError>(app.httpErrors.upgradeRequired())
expectAssignable<HttpError>(app.httpErrors.preconditionFailed())
expectAssignable<HttpError>(app.httpErrors.tooManyRequests())
expectAssignable<HttpError>(app.httpErrors.requestHeaderFieldsTooLarge())
expectAssignable<HttpError>(app.httpErrors.unavailableForLegalReasons())
expectAssignable<HttpError>(app.httpErrors.internalServerError())
expectAssignable<HttpError>(app.httpErrors.notImplemented())
expectAssignable<HttpError>(app.httpErrors.badGateway())
expectAssignable<HttpError>(app.httpErrors.serviceUnavailable())
expectAssignable<HttpError>(app.httpErrors.gatewayTimeout())
expectAssignable<HttpError>(app.httpErrors.httpVersionNotSupported())
expectAssignable<HttpError>(app.httpErrors.variantAlsoNegotiates())
expectAssignable<HttpError>(app.httpErrors.insufficientStorage())
expectAssignable<HttpError>(app.httpErrors.loopDetected())
expectAssignable<HttpError>(app.httpErrors.bandwidthLimitExceeded())
expectAssignable<HttpError>(app.httpErrors.notExtended())
expectAssignable<HttpError>(app.httpErrors.networkAuthenticationRequired())
})

app.get('/', async (req, reply) => {
Expand Down

0 comments on commit 72c6863

Please sign in to comment.