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

fix(types): reply.getHeader can return numbers and arrays #4154

Merged
merged 1 commit into from
Jul 26, 2022

Conversation

seanparmelee
Copy link
Contributor

Currently, the type definition for FastifyReply.getHeader has a return type of string | undefined. For a header that's allowed to appear multiple times (such as Server-Timing), one might do something like:

reply.header('server-timing', [
  'miss, db;dur=53, app;dur=47.2',
  'customView, dc;desc=atl',
  'cache;desc="Cache Read";dur=23.2',
]);

for which a call to reply.getHeader('server-timing') would return:

['miss, db;dur=53, app;dur=47.2', 'customView, dc;desc=atl', 'cache;desc="Cache Read";dur=23.2']

Given this, and the type definition for Node's getHeader, I think the the type definition for FastifyReply.getHeader should be updated to be: number | string | string[] | undefined.

Checklist

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

@mcollina mcollina merged commit 18651e6 into fastify:main Jul 26, 2022
@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 26, 2022

I am sorry, that I make this remark now:

I can understand if getHeader can return an array of strings. But numbers would mean, that we actually convert strings to numbers when we parse the headers. And I kind of doubt, that we actually do this in the header parser.

@@ -38,7 +38,7 @@ export interface FastifyReply<
send(payload?: ReplyType): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
header(key: string, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
headers(values: {[key: string]: any}): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
getHeader(key: string): string | undefined;
Copy link
Contributor

@xtx1130 xtx1130 Jul 26, 2022

Choose a reason for hiding this comment

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

image

Why not use any here? In nodejs doc, it returns is any type

@seanparmelee
Copy link
Contributor Author

seanparmelee commented Jul 26, 2022

Hi @Uzlopak,

I can understand if getHeader can return an array of strings. But numbers would mean, that we actually convert strings to numbers when we parse the headers. And I kind of doubt, that we actually do this in the header parser.

Correct, this is more for the case where somewhere in the lifecycle a header is set via .header with a value that's a number. I added a unit test in this PR to demonstrate this current functionality.

I was originally going to only add string[], but saw that getHeaders was also allowing for number as is the Node type definition for getHeader (v14, v16, latest) and figured they should be consistent.

Hi @xtx1130 hopefully this also answers your query regarding using any.

@seanparmelee seanparmelee deleted the reply-getheader-type branch July 26, 2022 13:50
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants