diff --git a/src/helper/client.ts b/src/helper/client.ts index fb2b3dd..32f518f 100644 --- a/src/helper/client.ts +++ b/src/helper/client.ts @@ -22,34 +22,38 @@ export class UpClient { }); } + processLink(link: string): null | (() => Promise) { + if (link) { + const linkFunc: () => Promise = async () => { + const parsedLink = link.slice(BASE_URL.length); + return await this.get(parsedLink); + }; + linkFunc.bind(this); + return linkFunc; + } + return null; + } + public async get(url: string): Promise { const res = await this.getApi().get(url); - const linksProcessor = (res.data as unknown) as { - links: { - next: null | (() => Promise); - prev: null | (() => Promise); + const linksProcessObj = (res.data as unknown) as { + links?: { + next: null | string | (() => Promise); + prev: null | string | (() => Promise); }; }; - if (linksProcessor.links) { - linksProcessor.links.next = linksProcessor.links.next - ? async () => { - return ( - await this.getApi().get( - (linksProcessor.links.next as unknown) as string - ) - ).data; - } - : null; - linksProcessor.links.prev = linksProcessor.links.prev - ? async () => { - return ( - await this.getApi().get( - (linksProcessor.links.prev as unknown) as string - ) - ).data; - } - : null; + /* + * If links exist, process the strings into functions that + * re-execute 'this.get()' with the new url + */ + if (linksProcessObj.links) { + linksProcessObj.links.next = this.processLink( + linksProcessObj.links.next as string + ); + linksProcessObj.links.prev = this.processLink( + linksProcessObj.links.prev as string + ); } return res.data; diff --git a/src/interfaces.ts b/src/interfaces.ts index b72a130..55c6543 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -31,9 +31,9 @@ export interface Relationship { export interface PaginationLinks { /** The link to the previous page in the results. If this value is null there is no previous page. */ - prev: () => Promise | null; + prev: null | (() => Promise); /** The link to the next page in the results. If this value is null there is no next page. */ - next: () => Promise | null; + next: null | (() => Promise); } export interface UpApiError {