Skip to content

Commit

Permalink
fix: avoid regex, properly paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
honzikec committed Aug 29, 2023
1 parent b7437a9 commit b20b4bb
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export class RepositoryService {
url += `+sort:${params.sort}`;
}

if (params?.page) {
url += `&page=${params.page}`;
}

return this.http
.get(url, {
observe: 'response',
Expand Down Expand Up @@ -391,20 +395,22 @@ export class RepositoryService {
return 0;
}

// Find the link with rel="last"
const lastLinkPattern = /<([^>]+?)>; rel="last"/;
const lastLinkMatch = linkHeader.match(lastLinkPattern);
// Split the linkHeader by commas to separate individual links
const links = linkHeader.split(',');

// Find the last link in the header
const lastLink = links
.find((link) => link.includes('rel="last"'))
?.split(';')[0]
?.trim()
.slice(1, -1);

if (!lastLinkMatch) {
if (!lastLink) {
return 0;
}

const lastLink = lastLinkMatch[1];

// Parse as a URL
const url = new URL(lastLink);

// Extract query parameters
const queryParams = new URLSearchParams(url.search);
const page = parseInt(queryParams.get('page') || '', 10);

Expand Down

0 comments on commit b20b4bb

Please sign in to comment.