Skip to content

Commit 3fea3cf

Browse files
committed
Media Type: Expose more-complete, unstringified parse results
1 parent bd3e873 commit 3fea3cf

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Negotiator.prototype.mediaType = function mediaType(available) {
4747
return set && set[0];
4848
};
4949

50-
Negotiator.prototype.mediaTypes = function mediaTypes(available) {
51-
return preferredMediaTypes(this.request.headers.accept, available);
50+
Negotiator.prototype.mediaTypes = function mediaTypes(available, options) {
51+
return preferredMediaTypes(this.request.headers.accept, available, options);
5252
};
5353

5454
// Backwards compatibility

lib/mediaType.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,39 @@ function specify(type, spec, index) {
120120

121121
}
122122

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

127139
if (!provided) {
128140
// sorted list of all types
129-
return accepts.filter(isQuality).sort(compareSpecs).map(function getType(spec) {
130-
return spec.full;
131-
});
141+
var sorted = accepts.filter(isQuality).sort(compareSpecs);
142+
143+
if(!(options && options.detailed)) {
144+
return sorted.map(function(spec) {
145+
return spec.full;
146+
});
147+
} else {
148+
return sorted.map(function(spec) {
149+
return {
150+
type: "" + spec.type + '/' + spec.subtype,
151+
parameters: spec.params,
152+
q: spec.q
153+
};
154+
});
155+
}
132156
}
133157

134158
var priorities = provided.map(function getPriority(type, index) {

test/mediaType.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ describe('negotiator.mediaTypes()', function () {
194194
})
195195
})
196196

197+
describe('negotiator.mediaTypes(undefined, true)', function() {
198+
whenAccept('text/html;LEVEL=1, application/json;q=0.5', function () {
199+
it('should return more-detailed spec objects', function () {
200+
assert.deepEqual(
201+
this.negotiator.mediaTypes(undefined, {detailed: true}),
202+
[
203+
{"type": "text/html", "parameters": {"level": "1"}, "q": 1},
204+
{"type": "application/json", "parameters": {}, "q": 0.5},
205+
]
206+
);
207+
})
208+
});
209+
})
210+
197211
describe('negotiator.mediaTypes(array)', function () {
198212
whenAccept(undefined, function () {
199213
it('should return return original list', mediaTypesNegotiated(

0 commit comments

Comments
 (0)