-
-
Notifications
You must be signed in to change notification settings - Fork 94
Open
Labels
enhancementNew feature or requestNew feature or request
Description
In order to get better typing on the arguments for the Handler
type, does it make sense to use generics for the request and response arguments?
For example, what if there was:
import { Request as ExpressRequest, Response as ExpressResponse } from 'express'
import { Params, ParamsDictionary } from 'express-serve-static-core'
import { Request } from 'openapi-backend'
import { Context } from 'openapi-backend/backend'
export type Handler<
TRequest extends Request = any,
TResponse = any
> = (context: Context, req: TRequest, res: TResponse, ...args: any[]) => any | Promise<any>;
export type OpenApiExpressRequest<
P extends Params = ParamsDictionary,
ResBody = any,
ReqBody = any
> = ExpressRequest<P, ResBody, ReqBody> & Request
export type ExpressHandler<
P extends Params = ParamsDictionary,
ResBody = any,
ReqBody = any
> = Handler<OpenApiExpressRequest<P, ResBody, ReqBody>, ExpressResponse<ResBody>>
This way, when writing handlers for an express-backed service, they could be typed as:
import { ExpressHandler } from 'openapi-backend/backend'
export const getById: ExpressHandler = (c, req, res) => {
res.status(200).json({ message: `getById: ${req.path}` })
}
Thoughts?
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request