jshttp / negotiator

An HTTP content negotiator for Node.js
MIT License
320 stars 36 forks source link

Functionnal API #53

Open nfroidure opened 7 years ago

nfroidure commented 7 years ago

I'd really like to be able to use this module by just using its pure functions.

It is feasible by requiring those functions directly:

const preferredEncodings = require('negociator/lib/encoding');

Do you think it is safe or should we add a standard way to use those functions?

dougwilson commented 6 years ago

Hi @nfroidure . Typically even though require allows you do do that, the danger of reaching into a module if it's not documented you can do so is that an internal file rearrangement can break you without warning (if you don't pin to a specific version).

The API of this module was created before I took over it and it does have a lot wanting in it. I think we could change the API if someone wanted to put together a proposal of what exactly they wanted the API to look like and the use-cases this would enable over the existing API 👍

nfroidure commented 6 years ago

Thanks for your answer. I use this module here https://github.com/nfroidure/swagger-http-router/blob/master/src/lib.js#L3-L4

To build a extractResponseSpec function https://github.com/nfroidure/swagger-http-router/blob/master/src/lib.js#L79-L97

In this router, I choose to not use the built in req/res objects unlike in every major frameworks for several reasons (snapshot testing, functional programming, function composition over middlewares...). For that matter, the requests and responses are raw JSON serializable objects so I cannot use APIs that expect a regular NodeJS response object (here is a more complete explanation of the why https://github.com/nfroidure/swagger-http-router#goal ).

I guess that the extractResponseSpec could be used with little changes to provide a functional API in this module.