Skip to content

Improve header handling #1339

@monolithed

Description

@monolithed

Hi,

Some time ago, I developed a micro package called http-directives. It helps reuse common HTTP constants and reduces the chance of typos in the code. A simple thing, but it helped me uncover two issues:

import { Headers } from 'http-directives';

class ServiceHeaders extends Headers {
    static readonly X_CAPTCHA_TOKEN = 'X-Captcha-Token';
}
  1. I passed this header, ServiceHeaders.X_CAPTCHA_TOKEN, into CORS and middleware for captcha handling.
    It turned out that request.headers doesn’t have a get method to retrieve a header value by name:
const token = request.headers[ServiceHeaders.X_CAPTCHA_TOKEN];

This doesn’t work because X-Captcha-Token is written in uppercase, while the server returns headers in lowercase.

  1. There can be multiple headers with the same name.

  2. The current interface is not compatible with the Headers types:

Image

It would be great to have a unified interface for working with headers and cookies, like in Next.js and similar frameworks. That would reduce boilerplate and minimize the risk of subtle bugs.

To handle header properties, there is a popular package headers-polyfill.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions