jshttp / accepts

Higher-level content negotiation
MIT License
252 stars 42 forks source link

change * default #20

Closed gajus closed 4 years ago

gajus commented 4 years ago

The current default is that:

Treats non-existent headers as *

, which doesn't make sense.

const contentType = accepts(incomingMessage)
  .type([
    'image/webp',
    'image/jpeg',
  ]) || 'image/jpeg';

In this case, I want to default to the safest option, which is the last entry in the array, or image/jpeg if nothing is matched.

When no header is present, however, the first option is picked, which is the least likely to be supported.

Ideally, I should be able to pick the fallback option when either header is not present or header value cannot be matched.

dougwilson commented 4 years ago

This is not a default this module made up: it is in the specification.

https://tools.ietf.org/html/rfc7231#section-5.3.2

A request without any Accept header field implies that the user agent will accept any media type in response.

If you want the behavior of choosing a "fallback" when the agent accepts anything, you should list your default as the first option, so it is the one chosen when the client doesn't have any particular preference.

gajus commented 4 years ago

Unless I am overlooking something, this doesn't make much sense.

The input of types is the types in the order of preference, e.g.

const contentType = accepts(incomingMessage)
  .type([
    'image/webp',
    'image/jpeg',
  ]) || 'image/jpeg';

Here I prefer webp, but want to fallback to jpeg if the latter is not supported.

Having jpeg first will just mean using jpeg all the time.

dougwilson commented 4 years ago

That's correct, your array says your server-side prefers, in order (1) image/webp and (2) image/jpeg. The client does not send any Accept header, in which as in the HTTP specs, it means that the user agent accepts any type as the response, so since you most prefer to send back image/webp, that is what this module sends, as the client does accept that and it's what you prefer the most to send.