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

Can we make this library more abstract? #30

Open
krutoo opened this issue Mar 20, 2024 · 0 comments
Open

Can we make this library more abstract? #30

krutoo opened this issue Mar 20, 2024 · 0 comments

Comments

@krutoo
Copy link

krutoo commented Mar 20, 2024

Hi, i want to use this library with not only IncomingMessage instances.

Node.js 16+ has Fetch API now and it has global constructors such as Request, Response, Headers and other.

Also i see that implementation of this library uses only headers field from req parameter.

Idea 1

Maybe we can make this library more abstract, for example we can make next interface changes:

// it is just a variant of interface
declare function accepts(getHeader: (headerName: string) => string | null): Accepts

With this interface we can use this library with instance of any class:

const req1 = new IncomingMessage(/* ... */);
const result1 = accepts(headerName => req1.getHeader(headerName));

const req2 = new Request(/* ... */)
const result2 = accepts(headerName => req2.headers.get(headerName))

Idea 2

Incoming message already has getHeader method, we can make interface like:

declare function accepts(req: Pick<IncomingMessage, 'getHeader'>): Accepts

And then we can use it now too for IncomingMessage:

const req = new IncomingMessage(/* ... */);

// no changes in usage
const accept = accepts(req);

And also we can use it easy with Request instance or any other request value:

const req = new Request(/* ... */);

// easy making wrapper object for use with Request
const accept = accepts({
  getHeader: headerName => req.headers.get(headerName)
});

Idea 3

Maybe we can make just TypeScript types of this library more abstract, for example:

declare function accepts(req: Pick<IncomingMessage, 'headers'>): Accepts
@krutoo krutoo changed the title Can we reduce type of req from IncomingMessage to just {headers: Headers}? Can we make this library more abstract? Mar 20, 2024
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

No branches or pull requests

1 participant