Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
Merge pull request #18 from integration-os/feat/pagination-helper
Browse files Browse the repository at this point in the history
feat: add pagination helper
  • Loading branch information
paulkr authored Oct 17, 2024
2 parents c19c968 + 6fa13a9 commit 24ef22b
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 110 deletions.
1 change: 1 addition & 0 deletions generator/templates/index.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { convertFilterToQueryParams } from './utils';

export * from './types/generic';
export * from './types/models';
export * from './paginate';

import {
{{#each resources}}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@integrationos/node",
"version": "4.1.9",
"version": "4.1.10",
"description": "Node SDK for the IntegrationOS Unified API",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { convertFilterToQueryParams } from './utils';

export * from './types/generic';
export * from './types/models';
export * from './paginate';

import {
Events,
Expand Down
67 changes: 67 additions & 0 deletions src/paginate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ListFilter, PaginationHelperFetch } from "./types";

export class PaginationHelper<T> {
private currentBatch: T[] = [];
private nextBatch: T[] | null = null;
private currentCursor: string | undefined;
private nextCursor: string | undefined;
private isInitialized: boolean = false;

private defaultBatchSize: number = 25

constructor(
private fetchFunction: PaginationHelperFetch<T>,
private listParams: ListFilter = {
limit: this.defaultBatchSize
}
) { }

async initialize(): Promise<void> {
if (this.isInitialized) return;

const firstResponse = await this.fetchFunction({ ...this.listParams });
this.currentBatch = firstResponse.unified;
this.currentCursor = firstResponse.pagination.nextCursor;

if (this.currentCursor) {
const secondResponse = await this.fetchFunction({
...this.listParams,
cursor: this.currentCursor
});

this.nextBatch = secondResponse.unified;
this.nextCursor = secondResponse.pagination.nextCursor;
}

this.isInitialized = true;
}

async getNextBatch(): Promise<T[]> {
if (!this.isInitialized) {
await this.initialize();
}

const batchToReturn = this.currentBatch;
this.currentBatch = this.nextBatch || [];
this.currentCursor = this.nextCursor;

if (this.currentCursor) {
const response = await this.fetchFunction({
...this.listParams,
cursor: this.currentCursor
});

this.nextBatch = response.unified;
this.nextCursor = response.pagination.nextCursor;
} else {
this.nextBatch = null;
this.nextCursor = undefined;
}

return batchToReturn;
}

hasMoreData(): boolean {
return this.currentBatch.length > 0 || (this.nextBatch !== null && this.nextBatch.length > 0);
}
}
2 changes: 2 additions & 0 deletions src/types/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface Pagination {
previousCursor?: string;
}

export type PaginationHelperFetch<T> = (params: ListFilter) => Promise<ListResponse<T>>;

export interface DeleteOptions {
modifyToken?: string;
}
Expand Down
218 changes: 109 additions & 109 deletions src/types/models.ts

Large diffs are not rendered by default.

0 comments on commit 24ef22b

Please sign in to comment.