Skip to content

Commit

Permalink
errorResponse type
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPlayz0 committed Dec 22, 2023
1 parent c224718 commit 0f79029
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -249,7 +249,7 @@ export class Server<Context = {}> {
}
}

type CtxMiddlewareFunction<Context> = (ctx: Context & HTTPContext) => (express.NextFunction | void) | Promise<express.NextFunction | void>;
type CtxMiddlewareFunction<Context> = (ctx: Context & HTTPContext) => (express.NextFunction | errorResponse | void) | Promise<express.NextFunction | errorResponse | void>;
type MiddlewareFunction = (req: express.Request, res: express.Response, next: express.NextFunction) => (express.NextFunction | void) | Promise<express.NextFunction | void>;
type MiddlewareWhen = 'init' | 'precors' | 'postcors' | 'predocs' | 'postdocs' | 'preroutes' | 'postroutes' | 'finish';
interface RouteMiddleware {
Expand Down
18 changes: 10 additions & 8 deletions src/types/httprouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ export interface RouteConfig {
documentation: Documentation;
}

export type errorResponse = (
status: HttpStatus,
opts?: {
message?: string;
data?: any;
code?: string;
},
) => express.Response<any, Record<string, any>>;

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<any, Record<string, any>>;
errorResponse: errorResponse;
}

// Converted to `export type RouteHandler = typeof api.routeHandler<HTTPContext>;`
Expand Down

0 comments on commit 0f79029

Please sign in to comment.