Skip to content

Commit c2e3577

Browse files
authored
Fix Link header parsing
1 parent 1659c57 commit c2e3577

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

app/core/collections/apiCollection.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
define(['backbone', 'underscore'], function(Backbone, _) {
1+
define(['backbone', 'underscore', '../helpers'], function(Backbone, _, Helpers) {
22
/**
33
* Class for collecting API data
44
* @class ApiCollection
@@ -43,14 +43,8 @@ define(['backbone', 'underscore'], function(Backbone, _) {
4343
data: this.buildQuery(),
4444
success: async (d, status, res) => {
4545
memo.push(...d.models);
46-
const link = res.xhr.getResponseHeader('Link');
47-
if(link) {
48-
const nextUrlMatch = link.match(/<[^>]*>; rel="next"/);
49-
if(nextUrlMatch) {
50-
const nextUrl = nextUrlMatch[0].match(/<(.*)>/);
51-
await _fetch(nextUrl[1], memo);
52-
}
53-
}
46+
const { next } = Helpers.parseLinksHeader(res);
47+
if(options.fetchAll !== false && next) await _fetch(next, memo);
5448
resolve(memo);
5549
},
5650
error: console.log

app/core/helpers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,19 @@ define(['handlebars', 'moment', 'core/origin'], function(Handlebars, Moment, Ori
349349
}
350350
resolve(data);
351351
});
352+
},
353+
354+
parseLinksHeader(res) {
355+
const header = res.xhr.getResponseHeader('Link');
356+
const links = {};
357+
if(header) {
358+
header.split(',').forEach(l => {
359+
const [url, name] = l.split(';');
360+
if (!url || !name) return console.trace(`Could not parse links: ${link}`);
361+
links[name.replace(/rel="(.*)"/, '$1').trim()] = url.replace(/<(.*)>/, '$1').trim();
362+
});
363+
}
364+
return links;
352365
}
353366
}
354367

0 commit comments

Comments
 (0)