Skip to content
Myr

feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andrea Polverino · committed by GitHub · parent 6ed3439 · Browse files · 3 files+34 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+34 added−4 removed

  • tests:+18 added−0 removed
  • other:+16 added−4 removed

3 files+34 added−4 removed

modified:lib/response.js+15 added-4 removed
78 unmodified lines
79 * Examples:80 *81 * res.links({82 * next: 'http://api.example.com/users?page=2',83 * last: 'http://api.example.com/users?page=5'83 * last: 'http://api.example.com/users?page=5',84 * pages: [85 * 'http://api.example.com/users?page=1',86 * 'http://api.example.com/users?page=2'87 * ]88 * });89 *90 * @param {Object} links91 * @return {ServerResponse}92 * @public93 */9491res.links = function(links){95res.links = function(links) {96 var link = this.get('Link') || '';97 if (link) link += ', ';94 return this.set('Link', link + Object.keys(links).map(function(rel){95 return '<' + links[rel] + '>; rel="' + rel + '"';98 return this.set('Link', link + Object.keys(links).map(function(rel) {99 // Allow multiple links if links[rel] is an array100 if (Array.isArray(links[rel])) {101 return links[rel].map(function (singleLink) {102 return `<${singleLink}>; rel="${rel}"`;103 }).join(', ');104 } else {105 return `<${links[rel]}>; rel="${rel}"`;106 }107 }).join(', '));108};109110/**
929 unmodified lines
modified:test/res.links.js+18 added
modified:History.md+1 added
9 unmodified lines
10* refactor: prefix built-in node module imports11* Remove unused `depd` dependency12* Add support for `Uint8Array` in `res.send`13* Add support for ETag option in res.sendFile14* Extend res.links() to allow adding multiple links with the same rel15* deps: debug@^4.4.016* deps: body-parser@^2.1.017* deps: router@^2.1.018* deps: nyc@^17.1.0
3843 unmodified lines