Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(pagination): [nan-1958] per page offset pagination #2890

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading