-
Notifications
You must be signed in to change notification settings - Fork 100
feat: add dedicated methods for cursor based pagination [CAPI-2357] #2824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
cd8d735
263f23a
de2e56c
84d693a
790f711
a2a540e
09e8e07
0603508
725fc0d
4810416
5fc9db5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,8 +3,10 @@ | |||||||||||||||
| import { toPlainObject } from 'contentful-sdk-core' | ||||||||||||||||
| import copy from 'fast-copy' | ||||||||||||||||
| import type { | ||||||||||||||||
| BasicCursorPaginationOptions, | ||||||||||||||||
| Collection, | ||||||||||||||||
| CollectionProp, | ||||||||||||||||
| CursorBasedParams, | ||||||||||||||||
| CursorPaginatedCollection, | ||||||||||||||||
| CursorPaginatedCollectionProp, | ||||||||||||||||
| MakeRequest, | ||||||||||||||||
|
|
@@ -14,7 +16,7 @@ | |||||||||||||||
| * @private | ||||||||||||||||
| */ | ||||||||||||||||
| export const wrapCollection = | ||||||||||||||||
| <R, T, Rest extends any[]>(fn: (makeRequest: MakeRequest, entity: T, ...rest: Rest) => R) => | ||||||||||||||||
| (makeRequest: MakeRequest, data: CollectionProp<T>, ...rest: Rest): Collection<R, T> => { | ||||||||||||||||
| const collectionData = toPlainObject(copy(data)) | ||||||||||||||||
| // @ts-expect-error | ||||||||||||||||
|
|
@@ -24,7 +26,7 @@ | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export const wrapCursorPaginatedCollection = | ||||||||||||||||
| <R, T, Rest extends any[]>(fn: (makeRequest: MakeRequest, entity: T, ...rest: Rest) => R) => | ||||||||||||||||
| ( | ||||||||||||||||
| makeRequest: MakeRequest, | ||||||||||||||||
| data: CursorPaginatedCollectionProp<T>, | ||||||||||||||||
|
|
@@ -47,3 +49,50 @@ | |||||||||||||||
| export async function waitFor(ms = 1000) { | ||||||||||||||||
| return new Promise((resolve) => setTimeout(resolve, ms)) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export function normalizeCursorPaginationParameters( | ||||||||||||||||
| query: BasicCursorPaginationOptions, | ||||||||||||||||
| ): CursorBasedParams { | ||||||||||||||||
| const { pagePrev, pageNext, ...rest } = query | ||||||||||||||||
|
|
||||||||||||||||
| return { | ||||||||||||||||
| ...rest, | ||||||||||||||||
| cursor: true, | ||||||||||||||||
| // omit pagePrev and pageNext if the value is falsy | ||||||||||||||||
| ...(pagePrev ? { pagePrev } : null), | ||||||||||||||||
| ...(pageNext ? { pageNext } : null), | ||||||||||||||||
| } as CursorBasedParams | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| function extractQueryParam(key: string, url?: string): string | undefined { | ||||||||||||||||
| if (!url) return | ||||||||||||||||
|
|
||||||||||||||||
| const queryIndex = url.indexOf('?') | ||||||||||||||||
| if (queryIndex === -1) return | ||||||||||||||||
|
|
||||||||||||||||
| const queryString = url.slice(queryIndex + 1) | ||||||||||||||||
| return new URLSearchParams(queryString).get(key) ?? undefined | ||||||||||||||||
|
Comment on lines
+70
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The url we are parsing from the response is a relative url
so we couldn't use URL here unless we mocked an appropriate base domain. Thoughts? |
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const Pages = { | ||||||||||||||||
| prev: 'pagePrev', | ||||||||||||||||
| next: 'pageNext', | ||||||||||||||||
| } as const | ||||||||||||||||
|
|
||||||||||||||||
| const PAGE_KEYS = ['prev', 'next'] as const | ||||||||||||||||
|
|
||||||||||||||||
| export function normalizeCursorPaginationResponse<T>( | ||||||||||||||||
| data: CursorPaginatedCollectionProp<T>, | ||||||||||||||||
| ): CursorPaginatedCollectionProp<T> { | ||||||||||||||||
| const pages: { prev?: string; next?: string } = {} | ||||||||||||||||
|
|
||||||||||||||||
| for (const key of PAGE_KEYS) { | ||||||||||||||||
| const token = extractQueryParam(Pages[key], data.pages?.[key]) | ||||||||||||||||
| if (token) pages[key] = token | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return { | ||||||||||||||||
| ...data, | ||||||||||||||||
| pages, | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also add the title in TOC?