Skip to content

Commit a1ff1b0

Browse files
andveawesleytodd
authored andcommitted
feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729)
1 parent d2de128 commit a1ff1b0

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ unreleased
1010
* refactor: prefix built-in node module imports
1111
* Remove unused `depd` dependency
1212
* Add support for `Uint8Array` in `res.send`
13+
* Extend res.links() to allow adding multiple links with the same rel
1314
* deps: debug@^4.4.0
1415
* deps: body-parser@^2.1.0
1516
* deps: router@^2.1.0

lib/response.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ res.links = function(links){
9292
var link = this.get('Link') || '';
9393
if (link) link += ', ';
9494
return this.set('Link', link + Object.keys(links).map(function(rel){
95-
return '<' + links[rel] + '>; rel="' + rel + '"';
95+
// Allow multiple links if links[rel] is an array
96+
if (Array.isArray(links[rel])) {
97+
return links[rel].map(function(singleLink){
98+
return '<' + singleLink + '>; rel="' + rel + '"';
99+
}).join(', ');
100+
}else{
101+
return '<' + links[rel] + '>; rel="' + rel + '"';
102+
}
96103
}).join(', '));
97104
};
98105

test/res.links.js

+18
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@ describe('res', function(){
4343
.expect('Link', '<http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last", <http://api.example.com/users?page=1>; rel="prev"')
4444
.expect(200, done);
4545
})
46+
47+
it('should set multiple links for single rel', function (done) {
48+
var app = express();
49+
50+
app.use(function (req, res) {
51+
res.links({
52+
next: 'http://api.example.com/users?page=2',
53+
last: ['http://api.example.com/users?page=5', 'http://api.example.com/users?page=1']
54+
});
55+
56+
res.end();
57+
});
58+
59+
request(app)
60+
.get('/')
61+
.expect('Link', '<http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last", <http://api.example.com/users?page=1>; rel="last"')
62+
.expect(200, done);
63+
})
4664
})
4765
})

0 commit comments

Comments
 (0)