Skip to content

Commit 19d1f59

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

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-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, detailed) {
51+
return preferredMediaTypes(this.request.headers.accept, available, detailed);
5252
};
5353

5454
// Backwards compatibility

lib/mediaType.js

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

121121
}
122122

123-
function preferredMediaTypes(accept, provided) {
123+
function preferredMediaTypes(accept, provided, detailed) {
124124
// RFC 2616 sec 14.2: no header = */*
125125
var accepts = parseAccept(accept === undefined ? '*/*' : accept || '');
126126

127127
if (!provided) {
128128
// sorted list of all types
129-
return accepts.filter(isQuality).sort(compareSpecs).map(function getType(spec) {
130-
return spec.full;
131-
});
129+
var sorted = accepts.filter(isQuality).sort(compareSpecs);
130+
131+
if(!detailed) {
132+
return sorted.map(function(spec) {
133+
return spec.full;
134+
});
135+
} else {
136+
return sorted.map(function(spec) {
137+
return {
138+
type: spec.type,
139+
subtype: spec.subtype,
140+
params: spec.params,
141+
q: spec.q
142+
};
143+
});
144+
}
132145
}
133146

134147
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, true),
202+
[
203+
{"type": "text", "subtype": "html", "params": {"level": "1"}, "q": 1},
204+
{"type": "application", "subtype": "json", "params": {}, "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)