Skip to content

Commit

Permalink
Fixing pagination (#17)
Browse files Browse the repository at this point in the history
* Made tags.list() return an array of TagResource within ListTagsResponse.data

* Added preprocessing check for link strings in client

* Cleaned up code for pagination and fixed issue on calling function

* Fixed issue with type thinking that it could return null

* Fixed Typing issues
  • Loading branch information
IAmMadfly authored Oct 19, 2021
1 parent ce1a804 commit a42c00f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
50 changes: 27 additions & 23 deletions src/helper/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,38 @@ export class UpClient {
});
}

processLink<T>(link: string): null | (() => Promise<T>) {
if (link) {
const linkFunc: () => Promise<T> = async () => {
const parsedLink = link.slice(BASE_URL.length);
return await this.get<T>(parsedLink);
};
linkFunc.bind(this);
return linkFunc;
}
return null;
}

public async get<T>(url: string): Promise<T> {
const res = await this.getApi().get<T>(url);

const linksProcessor = (res.data as unknown) as {
links: {
next: null | (() => Promise<T>);
prev: null | (() => Promise<T>);
const linksProcessObj = (res.data as unknown) as {
links?: {
next: null | string | (() => Promise<T>);
prev: null | string | (() => Promise<T>);
};
};
if (linksProcessor.links) {
linksProcessor.links.next = linksProcessor.links.next
? async () => {
return (
await this.getApi().get<T>(
(linksProcessor.links.next as unknown) as string
)
).data;
}
: null;
linksProcessor.links.prev = linksProcessor.links.prev
? async () => {
return (
await this.getApi().get<T>(
(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<T>(
linksProcessObj.links.next as string
);
linksProcessObj.links.prev = this.processLink<T>(
linksProcessObj.links.prev as string
);
}

return res.data;
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export interface Relationship<TRelationshipData> {

export interface PaginationLinks<ReturnType> {
/** The link to the previous page in the results. If this value is null there is no previous page. */
prev: () => Promise<ReturnType> | null;
prev: null | (() => Promise<ReturnType>);
/** The link to the next page in the results. If this value is null there is no next page. */
next: () => Promise<ReturnType> | null;
next: null | (() => Promise<ReturnType>);
}

export interface UpApiError {
Expand Down

0 comments on commit a42c00f

Please sign in to comment.