up-bank-api icon indicating copy to clipboard operation
up-bank-api copied to clipboard

feat: pagination support

Open kvithana opened this issue 3 years ago • 7 comments

Should we try to support pagination for list endpoints? I'm not sure what the best practice would be for support this.

What do you think of something like this which wraps around the response objects which allow pagination, which replaces the links with the api client method to get more data (keeping the interface type).

interface PaginationMethods {
  links: {
    next: () => Promise<unknown> | null;
    prev: () => Promise<unknown> | null;
  };
}

export function withPagination<T>(
  client: UpClient,
  data: T
): T & PaginationMethods {
  const links: PaginationLinks = data.links;
  return {
    ...data,
    links: {
      next: links.next
        ? () =>
            client
              .get<T>(links.next.replace(BASE_URL, ''))
              .then((data) => withPagination(client, data))
        : null,
      prev: links.prev
        ? () =>
            client
              .get<T>(links.prev.replace(BASE_URL, ''))
              .then((data) => withPagination(client, data))
        : null,
    },
  };
}

Then for methods that support pagination we could use this builder like so:

  return this.api
      .get<ListAccountResponse>(`${ENDPOINT}?${urlParams.join('&')}`)
      .then((data) => withPagination(this.api, data));

kvithana avatar Oct 06 '20 08:10 kvithana