Skip to content

Creating a separate interface for IncomingMessageHeaders in node/index.d.ts - #17157

Merged
2 commits merged into
DefinitelyTyped:masterfrom
hewholived:patch-1
Jun 16, 2017
Merged

2 commits merged into
DefinitelyTyped:masterfrom
hewholived:patch-1

Conversation

@hewholived

@hewholived hewholived commented Jun 13, 2017 •

Copy link
Copy Markdown
Contributor

This allows for IncomingMessage.headers types to be extensible per project. Suppose a node service expects a fixed set of headers of certain type (say string), that service can specify custom IncomingMessageHeaders with more concrete types defined.
For example

interface IncomingMessageHeaders {
    "host": string;
    "set-cookie": string[]
}

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If changing an existing definition:

This allows for IncomingMessage.headers types to be extensible per project. Suppose a node service expects a fixed set of headers of certain type (say string), that service can specify custom IncomingMessageHeaders with more concrete types defined.
For example

```
interface IncomingMessageHeaders {
    "host": string;
    "set-cookie": string[]
}
```
@hewholived hewholived changed the title Creating a separate interface for IncomingMessageHeaders Creating a separate interface for IncomingMessageHeaders in node/index.d.ts Jun 13, 2017
@Flarna

Flarna commented Jun 14, 2017

Copy link
Copy Markdown
Contributor

I like the idea but I tried a slightly different variant to extend the interface in my application and it fails. As headers are optional I tried the following:

interface IncomingMessageHeaders {
    authorization?: string;
}

but this results in Property 'authorization' of type 'string | undefined' is not assignable to string index type 'string | string[]'.'

I use typescript 2.3 and have set strict to true in my tsconfig.json.

If I explicitly add undefined to the index signature (I know it should be implicit) it works:

export interface IncomingMessageHeaders {
    [key: string]: string | string[] | undefined;
}

@ghost

ghost commented Jun 15, 2017

Copy link
Copy Markdown

I think string | string[] | undefined is good.
Could you review #17229?

This definition adds undefined in addition to string and string[].
@hewholived

Copy link
Copy Markdown
Contributor Author

@Andy-MS Any idea why build fails? Not able to make sense out of the logs.

@ghost

ghost commented Jun 16, 2017 •

Copy link
Copy Markdown

Looks like that's broken in master. I'll fix separately. Thanks! (Ref: #17141 (comment))

@ghost
ghost merged commit edbb4de into DefinitelyTyped:master Jun 16, 2017
@ikokostya

Copy link
Copy Markdown
Contributor

Could you provide example when IncomingMessageHeaders has value with string[] type?

@ikokostya

Copy link
Copy Markdown
Contributor

Found the problem source c9e01b3#commitcomment-22503480

@Flarna

Flarna commented Jun 27, 2017

Copy link
Copy Markdown
Contributor
import * as http from "http";
import * as util from "util";

http.createServer((req: http.ServerRequest, res: http.ServerResponse) => {
	console.log(util.inspect(req.headers));

	res.end();
}).listen(8000);

const reqOptions: http.RequestOptions = {
	headers: {
		"set-cookie": [ "foo=bar", "bar=foo" ],
		"x-merged": [ "foo=bar", "bar=foo" ]
	},
	port: 8000
};

http.get(reqOptions);

prints the following:

{ 'set-cookie': [ 'foo=bar', 'bar=foo' ],
  'x-merged': 'foo=bar, bar=foo',
  host: 'localhost:8000',
  connection: 'close' }

==> At least the set-cookie header is provided as array; other headers are merged to a string.
see https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_message_headers

@ikokostya

Copy link
Copy Markdown
Contributor

Thanks. I see problem now. We can't declare IncomingMessageHeaders interface like this:

interface IncomingMessageHeaders {
    [key: string]: string;
    "set-cookie": string[];
}

because Property '"set-cookie"' of type 'string[]' is not assignable to string index type 'string'.

@ikokostya

ikokostya commented Jun 27, 2017 •

Copy link
Copy Markdown
Contributor

Now I should use type assertion in each header access

<string>req.headers['foo']

This is very annoying.

Instead we can declare interface IncomingMessageHeaders as intersection:

interface CommonHeaders {
   [key: string]: string;
}

interface SpecialHeaders {
   "set-cookie": string[];
}

type IncomingMessageHeaders = CommonHeaders & SpecialHeaders;

Then we can remove type assertions:

function foo(headers: IncomingMessageHeaders) {
    headers['a'].trim();
    headers['set-cookie'].concat();
}

@Flarna

Flarna commented Jun 27, 2017

Copy link
Copy Markdown
Contributor

I think this looks ok.
As headers are optional we should add | undefined like it is now.
Additionally we should be aware that such a change is not backward compatible as users have to extend CommonHeaders or SpecialHeaders instead IncomingMessageHeaders.

KSXGitHub pushed a commit to KSXGitHub/DefinitelyTyped that referenced this pull request May 12, 2018
…x.d.ts (DefinitelyTyped#17157)

* Creating a separate interface for IncomingMessageHeaders

This allows for IncomingMessage.headers types to be extensible per project. Suppose a node service expects a fixed set of headers of certain type (say string), that service can specify custom IncomingMessageHeaders with more concrete types defined.
For example

```
interface IncomingMessageHeaders {
    "host": string;
    "set-cookie": string[]
}
```

* Changing IncomingMessageHeaders defn

This definition adds undefined in addition to string and string[].
This pull request was closed.
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.

3 participants