Skip to content

Commit

Permalink
fix(pagination): [nan-1958] per page offset pagination (#2890)
Browse files Browse the repository at this point in the history
## Describe your changes
Offset right now only adds the response data length but many APIs just
need the page to be incremented by one. As seen in
NangoHQ/integration-templates#72

## Issue ticket number and link
NAN-1958

## Checklist before requesting a review (skip if just adding/editing
APIs & templates)
- [ ] I added tests, otherwise the reason is: 
- [ ] I added observability, otherwise the reason is:
- [ ] I added analytics, otherwise the reason is:
  • Loading branch information
khaliqgant authored Oct 25, 2024
1 parent a70a276 commit 9e4fe49
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs-v2/reference/api-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ slack:
<ResponseField name="offset_name_in_request" type="string">
The name of the offset parameter in the request.
</ResponseField>
<ResponseField name="offset_calculation_method" type="string">
The method to calculate the offset in the request. Must be one of: "per-page" or "by-response-size". Optional parameter that defaults to "by-response-size".
</ResponseField>
<ResponseField name="offset_start_value" type="number">
The starting value for the offset.
</ResponseField>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface LinkPagination extends Pagination {
interface OffsetPagination extends Pagination {
offset_name_in_request: string;
offset_start_value?: number;
offset_calculation_method?: 'per-page' | 'by-response-size';
}
interface RetryHeaderConfig {
at?: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/shared/lib/models/Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ export interface LinkPagination extends Pagination {
link_path_in_response_body?: string;
}

export type OffsetCalculationMethod = 'per-page' | 'by-response-size';

export interface OffsetPagination extends Pagination {
offset_name_in_request: string;
offset_start_value?: number;
offset_calculation_method?: OffsetCalculationMethod;
}
1 change: 1 addition & 0 deletions packages/shared/lib/sdk/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ interface LinkPagination extends Pagination {
interface OffsetPagination extends Pagination {
offset_name_in_request: string;
offset_start_value?: number;
offset_calculation_method?: 'per-page' | 'by-response-size';
}

interface RetryHeaderConfig {
Expand Down
16 changes: 14 additions & 2 deletions packages/shared/lib/services/paginate.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type { AxiosResponse } from 'axios';
import parseLinksHeader from 'parse-link-header';
import get from 'lodash-es/get.js';
import type { Pagination, UserProvidedProxyConfiguration, CursorPagination, OffsetPagination, LinkPagination } from '../models/Proxy.js';
import type {
Pagination,
UserProvidedProxyConfiguration,
CursorPagination,
OffsetPagination,
LinkPagination,
OffsetCalculationMethod
} from '../models/Proxy.js';
import { PaginationType } from '../models/Proxy.js';
import { isValidHttpUrl } from '../utils/utils.js';

Expand Down Expand Up @@ -124,6 +131,7 @@ class PaginationService {
): AsyncGenerator<T[], undefined, void> {
const offsetPagination: OffsetPagination = paginationConfig;
const offsetParameterName: string = offsetPagination.offset_name_in_request;
const offsetCalculatioMethod: OffsetCalculationMethod = offsetPagination.offset_calculation_method || 'by-response-size';
let offset = offsetPagination.offset_start_value || 0;

while (true) {
Expand All @@ -149,7 +157,11 @@ class PaginationService {
return;
}

offset += responseData.length;
if (offsetCalculatioMethod === 'per-page') {
offset++;
} else {
offset += responseData.length;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/types/lib/proxy/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ export interface LinkPagination extends Pagination {
link_path_in_response_body?: string;
}

export type OffsetCalculationMethod = 'per-page' | 'by-response-size';

export interface OffsetPagination extends Pagination {
offset_name_in_request: string;
offset_start_value?: number;
offset_calculation_method?: OffsetCalculationMethod;
}
4 changes: 4 additions & 0 deletions scripts/validation/providers/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@
"offset_start_value": {
"type": "number"
},
"offset_calculation_method": {
"type": "string",
"enum": ["per-page", "by-response-size"]
},
"response_path": {
"type": "string"
},
Expand Down

0 comments on commit 9e4fe49

Please sign in to comment.