jshttp / negotiator

An HTTP content negotiator for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Functionnal API

nfroidure opened this issue · comments

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?

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 👍

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.