From 0f79029cfd76476151e314eaca1b96c7aef66b40 Mon Sep 17 00:00:00 2001 From: DanPlayz0 Date: Fri, 22 Dec 2023 02:58:47 -0800 Subject: [PATCH] errorResponse type --- src/Server.ts | 4 ++-- src/types/httprouter.ts | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Server.ts b/src/Server.ts index 0881f9f..318a83c 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -5,7 +5,7 @@ import { Server as HttpServer, IncomingMessage as HttpIncomingMessage, ServerRes import indexFolder from './utils/indexFolder'; import { OpenAPIV3_1 as OpenAPI } from 'openapi-types'; import Documentation, { APIInfoObject } from './documentation'; -import { HTTPContext, RouteFile } from './types/httprouter'; +import { HTTPContext, RouteFile, errorResponse } from './types/httprouter'; import { HttpStatus } from 'utils/httpStatus'; interface OASchemaFile { @@ -249,7 +249,7 @@ export class Server { } } -type CtxMiddlewareFunction = (ctx: Context & HTTPContext) => (express.NextFunction | void) | Promise; +type CtxMiddlewareFunction = (ctx: Context & HTTPContext) => (express.NextFunction | errorResponse | void) | Promise; type MiddlewareFunction = (req: express.Request, res: express.Response, next: express.NextFunction) => (express.NextFunction | void) | Promise; type MiddlewareWhen = 'init' | 'precors' | 'postcors' | 'predocs' | 'postdocs' | 'preroutes' | 'postroutes' | 'finish'; interface RouteMiddleware { diff --git a/src/types/httprouter.ts b/src/types/httprouter.ts index 3039e37..de1208f 100644 --- a/src/types/httprouter.ts +++ b/src/types/httprouter.ts @@ -15,19 +15,21 @@ export interface RouteConfig { documentation: Documentation; } +export type errorResponse = ( + status: HttpStatus, + opts?: { + message?: string; + data?: any; + code?: string; + }, +) => express.Response>; + export interface HTTPContext { // HTTP Context req: express.Request; res: express.Response; next: express.NextFunction; - errorResponse: ( - status: HttpStatus, - opts?: { - message?: string; - data?: any; - code?: string; - }, - ) => express.Response>; + errorResponse: errorResponse; } // Converted to `export type RouteHandler = typeof api.routeHandler;`