Skip to content
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

Merged
merged 1 commit into from
Sep 4, 2023

Conversation

erunion
Copy link
Contributor

@erunion erunion commented Aug 30, 2023

Fixes #294

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.

Here it is in action:

Screen Shot 2023-08-30 at 3 50 39 PM

And before without this generic, TS doesn't recognize our project property being on our Express Request object:

Screen Shot 2023-08-30 at 4 06 32 PM

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
Copy link
Contributor Author

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>;
Copy link
Contributor Author

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.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IncomingMessage and ServerMessage typings are too specific
3 participants