-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ts): loosening up the IncomingMessage and ServerResponse typings #295
Conversation
The typings on the various instances of `IncomingMessage` and `ServerResponse` are too strict when coupled with Express and Fastify `Request` instances that have been extended for custom properties. This loosens them up by allowing the main `pino` instance to be supplied a generic of the Request and Response interfaces that you use in your application. Fixes pinojs#294
const logger = pino(); | ||
|
||
pinoHttp(); | ||
pinoHttp({ logger }); | ||
pinoHttp({ logger }).logger = logger; | ||
pinoHttp<CustomRequest, CustomResponse>({ logger }); | ||
|
||
// #genReqId |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generic tests I added added a lot of clutter to this file so I've broken these option tests visually up a bit so it's easier to grok.
@@ -12,41 +12,41 @@ import { IncomingMessage, ServerResponse } from 'http'; | |||
import pino from 'pino'; | |||
import { err, req, res, SerializedError, SerializedRequest, SerializedResponse } from 'pino-std-serializers'; | |||
|
|||
declare function PinoHttp(opts?: Options, stream?: pino.DestinationStream): HttpLogger; | |||
declare function PinoHttp<IM = IncomingMessage, SR = ServerResponse>(opts?: Options<IM, SR>, stream?: pino.DestinationStream): HttpLogger<IM, SR>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally had all of these as <Request = IncomingMessage, Response = ServerResponse>
but that added a ton of visual clutter to this file. I'm happy to revise these though if you'd like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, good work!
Fixes #294
The typings on the various instances of
IncomingMessage
andServerResponse
are too strict when coupled with Express and FastifyRequest
instances that have been extended for custom properties. This loosens them up by allowing the mainpino
instance to be supplied a generic of the Request and Response interfaces that you use in your application.Here it is in action:
And before without this generic, TS doesn't recognize our
project
property being on our ExpressRequest
object: