accepts
accepts copied to clipboard
Can we make this library more abstract?
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