Skip to content

Commit

Permalink
Match API
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanresnick committed Jun 1, 2015
1 parent 19d1f59 commit f7d4b83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Negotiator.prototype.mediaType = function mediaType(available) {
return set && set[0];
};

Negotiator.prototype.mediaTypes = function mediaTypes(available, detailed) {
return preferredMediaTypes(this.request.headers.accept, available, detailed);
Negotiator.prototype.mediaTypes = function mediaTypes(available, options) {
return preferredMediaTypes(this.request.headers.accept, available, options);
};

// Backwards compatibility
Expand Down
21 changes: 16 additions & 5 deletions lib/mediaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,35 @@ function specify(type, spec, index) {

}

function preferredMediaTypes(accept, provided, detailed) {
/**
* Returns a list of the acceptable media ranges in preferred order.
*
* @param {string} accept The raw contents of the `Accept` header
* @param {string[]} provided The media ranges the server can produce
* for the resource in question.
* @param {Object} options Configuration options
* @param {boolean} options.detailed When `provided` is not defined, such
* that the caller is asking for a list of _all_ the parsed media types,
* setting this option to true returns an {type, parameters, q} object
* for each type, rather just than a string.
*/
function preferredMediaTypes(accept, provided, options) {
// RFC 2616 sec 14.2: no header = */*
var accepts = parseAccept(accept === undefined ? '*/*' : accept || '');

if (!provided) {
// sorted list of all types
var sorted = accepts.filter(isQuality).sort(compareSpecs);

if(!detailed) {
if(!options.detailed) {
return sorted.map(function(spec) {
return spec.full;
});
} else {
return sorted.map(function(spec) {
return {
type: spec.type,
subtype: spec.subtype,
params: spec.params,
type: "" + spec.type + '/' + spec.subtype,
parameters: spec.params,
q: spec.q
};
});
Expand Down

0 comments on commit f7d4b83

Please sign in to comment.