From f8bb553fde49c2dda868bdd9ee7c2e2d7510bd8f Mon Sep 17 00:00:00 2001 From: joshAg Date: Thu, 23 Jun 2022 19:31:53 -0700 Subject: [PATCH] extend expressJS Request Handler Parameters through express-openapi The added generic types for RequestHandler come from expressJS and allow the user to give types to various parts of the request and response object in a way that allows typescript to infer types. Currently if the user wants typescript to infer one of these parts has a certain type they have to explicitly case the object as such. This will remove the need to cast at all. This change is necessary because with the current code the user cannot explicitly type req or res because the compiler will complain about assigning a function where they have been more defined to the Operation type since Operation is missing those specific type definitions. For example ``` // normally generated from the openAPI definition interface myExampleDictionary extends core.ParamsDictionary { key: string } const GET: Operation = async (req: express.Request, res: express.Response, next: express.NextFunction) => { ``` Instead we currently have to do something like ``` // normally generated from the openAPI definition interface myExampleDictionary extends core.ParamsDictionary { key: string } const GET: Operation = async (req, res, next) => { const params = req.params as myExampleDictionary; ``` --- packages/express-openapi/index.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/express-openapi/index.ts b/packages/express-openapi/index.ts index 635a9295..2a502c83 100644 --- a/packages/express-openapi/index.ts +++ b/packages/express-openapi/index.ts @@ -1,3 +1,4 @@ +import * as core from 'express-serve-static-core'; import { ErrorObject, FormatDefinition, Format } from 'ajv'; import { Application, ErrorRequestHandler, RequestHandler } from 'express'; import OpenAPIFramework, { @@ -13,16 +14,34 @@ const CASE_SENSITIVE_PARAM_PROPERTY = 'x-express-openapi-case-sensitive'; const normalizeQueryParamsMiddleware = require('express-normalize-query-params-middleware'); const loggingPrefix = 'express-openapi'; -export interface OperationFunction extends RequestHandler { +export interface OperationFunction< + P extends Record = core.ParamsDictionary, + ResBody = any, + ReqBody = any, + ReqQuery = core.Query, + Locals extends Record = Record +> extends RequestHandler { apiDoc?: OpenAPI.Operation; } -export interface OperationHandlerArray { +export interface OperationHandlerArray< + P extends Record = core.ParamsDictionary, + ResBody = any, + ReqBody = any, + ReqQuery = core.Query, + Locals extends Record = Record +> { apiDoc?: OpenAPI.Operation; - [index: number]: RequestHandler; + [index: number]: RequestHandler; } -export type Operation = OperationFunction | OperationHandlerArray; +export type Operation< + P extends Record = core.ParamsDictionary, + ResBody = any, + ReqBody = any, + ReqQuery = core.Query, + Locals extends Record = Record +> = OperationFunction | OperationHandlerArray; export interface ExpressOpenAPIArgs extends OpenAPIFrameworkArgs { app: Application;