Skip to content

Commit 3e19960

Browse files
committed
feat: adding dedicated cursor based methods for getPublished for entry and asset entities [CAPI-2387]
Adding dedicated getPublishedWithCursor methods for cbp fetching of published entry and asset entities. Adding relevant integration tests as well
1 parent 6d0d2a2 commit 3e19960

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

lib/common-types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ type MRInternal<UA extends boolean> = {
490490
(opts: MROpts<'Asset', 'getMany', UA>): MRReturn<'Asset', 'getMany'>
491491
(opts: MROpts<'Asset', 'getManyWithCursor', UA>): MRReturn<'Asset', 'getManyWithCursor'>
492492
(opts: MROpts<'Asset', 'getPublished', UA>): MRReturn<'Asset', 'getPublished'>
493+
(opts: MROpts<'Asset', 'getPublishedWithCursor', UA>): MRReturn<'Asset', 'getPublishedWithCursor'>
493494
(opts: MROpts<'Asset', 'get', UA>): MRReturn<'Asset', 'get'>
494495
(opts: MROpts<'Asset', 'update', UA>): MRReturn<'Asset', 'update'>
495496
(opts: MROpts<'Asset', 'delete', UA>): MRReturn<'Asset', 'delete'>
@@ -623,6 +624,7 @@ type MRInternal<UA extends boolean> = {
623624
(opts: MROpts<'Entry', 'getMany', UA>): MRReturn<'Entry', 'getMany'>
624625
(opts: MROpts<'Entry', 'getManyWithCursor', UA>): MRReturn<'Entry', 'getManyWithCursor'>
625626
(opts: MROpts<'Entry', 'getPublished', UA>): MRReturn<'Entry', 'getPublished'>
627+
(opts: MROpts<'Entry', 'getPublishedWithCursor', UA>): MRReturn<'Entry', 'getPublishedWithCursor'>
626628
(opts: MROpts<'Entry', 'get', UA>): MRReturn<'Entry', 'get'>
627629
(opts: MROpts<'Entry', 'patch', UA>): MRReturn<'Entry', 'patch'>
628630
(opts: MROpts<'Entry', 'update', UA>): MRReturn<'Entry', 'update'>
@@ -1235,6 +1237,11 @@ export type MRActions = {
12351237
headers?: RawAxiosRequestHeaders
12361238
return: CollectionProp<AssetProps>
12371239
}
1240+
getPublishedWithCursor: {
1241+
params: GetSpaceEnvironmentParams & CursorBasedParams
1242+
headers?: RawAxiosRequestHeaders
1243+
return: CursorPaginatedCollectionProp<AssetProps>
1244+
}
12381245
getMany: {
12391246
params: GetSpaceEnvironmentParams & QueryParams & { releaseId?: string }
12401247
headers?: RawAxiosRequestHeaders
@@ -1661,6 +1668,10 @@ export type MRActions = {
16611668
params: GetSpaceEnvironmentParams & QueryParams
16621669
return: CollectionProp<EntryProps<any>>
16631670
}
1671+
getPublishedWithCursor: {
1672+
params: GetSpaceEnvironmentParams & CursorBasedParams
1673+
return: CursorPaginatedCollectionProp<EntryProps<any>>
1674+
}
16641675
getMany: {
16651676
params: GetSpaceEnvironmentParams & QueryParams & { releaseId?: string }
16661677
return: CollectionProp<EntryProps<any>>

lib/create-environment-api.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,25 @@ export default function createEnvironmentApi(makeRequest: MakeRequest) {
853853
}).then((data) => wrapEntryCollection(makeRequest, data))
854854
},
855855

856+
getPublishedEntriesWithCursor(query: BasicCursorPaginationOptions = {}) {
857+
const raw = this.toPlainObject() as EnvironmentProps
858+
const normalizedQueryParams = normalizeCursorPaginationParameters(query)
859+
return makeRequest({
860+
entityType: 'Entry',
861+
action: 'getPublished',
862+
params: {
863+
spaceId: raw.sys.space.sys.id,
864+
environmentId: raw.sys.id,
865+
query: createRequestConfig({ query: normalizedQueryParams }).params,
866+
},
867+
}).then((data) =>
868+
wrapEntryTypeCursorPaginatedCollection(
869+
makeRequest,
870+
normalizeCursorPaginationResponse(data),
871+
),
872+
)
873+
},
874+
856875
/**
857876
* Creates a Entry
858877
* @param contentTypeId - The Content Type ID of the newly created Entry
@@ -1108,6 +1127,25 @@ export default function createEnvironmentApi(makeRequest: MakeRequest) {
11081127
}).then((data) => wrapAssetCollection(makeRequest, data))
11091128
},
11101129

1130+
getPublishedAssetsWithCursor(query: BasicCursorPaginationOptions = {}) {
1131+
const raw = this.toPlainObject() as EnvironmentProps
1132+
const normalizedQueryParams = normalizeCursorPaginationParameters(query)
1133+
return makeRequest({
1134+
entityType: 'Asset',
1135+
action: 'getPublished',
1136+
params: {
1137+
spaceId: raw.sys.space.sys.id,
1138+
environmentId: raw.sys.id,
1139+
query: createRequestConfig({ query: normalizedQueryParams }).params,
1140+
},
1141+
}).then((data) =>
1142+
wrapAssetTypeCursorPaginatedCollection(
1143+
makeRequest,
1144+
normalizeCursorPaginationResponse(data),
1145+
),
1146+
)
1147+
},
1148+
11111149
/**
11121150
* Creates a Asset. After creation, call asset.processForLocale or asset.processForAllLocales to start asset processing.
11131151
* @param data - Object representation of the Asset to be created. Note that the field object should have an upload property on asset creation, which will be removed and replaced with an url property when processing is finished.

lib/plain/common-types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ export type PlainClientAPI = {
310310
rawData?: unknown,
311311
headers?: RawAxiosRequestHeaders,
312312
): Promise<CollectionProp<EntryProps<T>>>
313+
getPublishedWithCursor<T extends KeyValueMap = KeyValueMap>(
314+
params: OptionalDefaults<GetSpaceEnvironmentParams & CursorBasedParams>,
315+
rawData?: unknown,
316+
headers?: RawAxiosRequestHeaders,
317+
): Promise<CursorPaginatedCollectionProp<EntryProps<T>>>
313318
getMany<T extends KeyValueMap = KeyValueMap>(
314319
params: OptionalDefaults<GetSpaceEnvironmentParams & QueryParams & { releaseId?: string }>,
315320
rawData?: unknown,
@@ -381,6 +386,11 @@ export type PlainClientAPI = {
381386
rawData?: unknown,
382387
headers?: RawAxiosRequestHeaders,
383388
): Promise<CollectionProp<AssetProps>>
389+
getPublishedWithCursor(
390+
params: OptionalDefaults<GetSpaceEnvironmentParams & CursorBasedParams>,
391+
rawData?: unknown,
392+
headers?: RawAxiosRequestHeaders,
393+
): Promise<CursorPaginatedCollectionProp<AssetProps>>
384394
getMany(
385395
params: OptionalDefaults<GetSpaceEnvironmentParams & QueryParams & { releaseId?: string }>,
386396
rawData?: unknown,

lib/plain/plain-client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ export const createPlainClient = (
247247
},
248248
entry: {
249249
getPublished: wrap(wrapParams, 'Entry', 'getPublished'),
250+
getPublishedWithCursor: wrap(wrapParams, 'Entry', 'getPublishedWithCursor'),
250251
getMany: wrap(wrapParams, 'Entry', 'getMany'),
251252
getManyWithCursor: wrap(wrapParams, 'Entry', 'getManyWithCursor'),
252253
get: wrap(wrapParams, 'Entry', 'get'),
@@ -263,6 +264,7 @@ export const createPlainClient = (
263264
},
264265
asset: {
265266
getPublished: wrap(wrapParams, 'Asset', 'getPublished'),
267+
getPublishedWithCursor: wrap(wrapParams, 'Asset', 'getPublishedWithCursor'),
266268
getMany: wrap(wrapParams, 'Asset', 'getMany'),
267269
getManyWithCursor: wrap(wrapParams, 'Asset', 'getManyWithCursor'),
268270
get: wrap(wrapParams, 'Asset', 'get'),

test/integration/asset-integration.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,73 @@ describe('Asset API - Read', () => {
110110
const response = await environment.getPublishedAssets()
111111
expect(response.items).toBeTruthy()
112112
})
113+
114+
describe('Gets published assets with cursor pagination', () => {
115+
test('gets published assets with cursor pagination with items', async () => {
116+
const response = await environment.getPublishedAssetsWithCursor()
117+
expect(response.items).toBeTruthy()
118+
})
119+
120+
test('returns a cursor paginated published asset collection when no query is provided', async () => {
121+
const response = await environment.getPublishedAssetsWithCursor()
122+
123+
expect(response.items).not.toHaveLength(0)
124+
expect(response.pages).toBeDefined()
125+
expect((response as { total?: number }).total).toBeUndefined()
126+
127+
response.items.forEach((item) => {
128+
expect(item.sys.type).toEqual('Asset')
129+
expect(item.fields).toBeDefined()
130+
})
131+
})
132+
133+
test('returns [limit] number of items', async () => {
134+
const response = await environment.getPublishedAssetsWithCursor({ limit: 3 })
135+
136+
expect(response.items).toHaveLength(3)
137+
expect(response.pages).toBeDefined()
138+
expect((response as { total?: number }).total).toBeUndefined()
139+
140+
response.items.forEach((item) => {
141+
expect(item.sys.type).toEqual('Asset')
142+
expect(item.fields).toBeDefined()
143+
})
144+
})
145+
146+
test('supports forward pagination', async () => {
147+
const firstPage = await environment.getPublishedAssetsWithCursor({ limit: 2 })
148+
const secondPage = await environment.getPublishedAssetsWithCursor({
149+
limit: 2,
150+
pageNext: firstPage?.pages?.next,
151+
})
152+
153+
expect(secondPage.items).toHaveLength(2)
154+
expect(firstPage.items[0].sys.id).not.toEqual(secondPage.items[0].sys.id)
155+
})
156+
157+
test('should support backward pagination', async () => {
158+
const firstPage = await environment.getAssetsWithCursor({
159+
limit: 2,
160+
order: ['sys.createdAt'],
161+
})
162+
const secondPage = await environment.getAssetsWithCursor({
163+
limit: 2,
164+
pageNext: firstPage?.pages?.next,
165+
order: ['sys.createdAt'],
166+
})
167+
const result = await environment.getAssetsWithCursor({
168+
limit: 2,
169+
pagePrev: secondPage?.pages?.prev,
170+
order: ['sys.createdAt'],
171+
})
172+
173+
expect(result.items).toHaveLength(2)
174+
175+
firstPage.items.forEach((item, index) => {
176+
expect(item.sys.id).equal(result.items[index].sys.id)
177+
})
178+
})
179+
})
113180
})
114181

115182
describe('Asset API - Write', { concurrent: true }, () => {

test/integration/entry-integration.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,74 @@ describe('Entry Api', () => {
120120
expect(response.items[0].sys.publishedAt).to.not.be.undefined
121121
})
122122
})
123+
124+
describe('Gets published entries with cursor pagination', () => {
125+
test('gets published entries with cursor pagination with items', async () => {
126+
const response = await environment.getPublishedEntriesWithCursor()
127+
expect(response.items).toBeTruthy()
128+
})
129+
130+
test('returns a cursor paginated published entry collection when no query is provided', async () => {
131+
const response = await environment.getPublishedEntriesWithCursor()
132+
133+
expect(response.items).not.toHaveLength(0)
134+
expect(response.pages).toBeDefined()
135+
expect((response as { total?: number }).total).toBeUndefined()
136+
137+
response.items.forEach((item) => {
138+
expect(item.sys.type).toEqual('Entry')
139+
expect(item.fields).toBeDefined()
140+
})
141+
})
142+
143+
test('returns [limit] number of items', async () => {
144+
const response = await environment.getPublishedEntriesWithCursor({ limit: 3 })
145+
146+
expect(response.items).toHaveLength(3)
147+
expect(response.pages).toBeDefined()
148+
expect((response as { total?: number }).total).toBeUndefined()
149+
150+
response.items.forEach((item) => {
151+
expect(item.sys.type).toEqual('Entry')
152+
expect(item.fields).toBeDefined()
153+
})
154+
})
155+
156+
test('supports forward pagination', async () => {
157+
const firstPage = await environment.getPublishedEntriesWithCursor({ limit: 2 })
158+
const secondPage = await environment.getPublishedEntriesWithCursor({
159+
limit: 2,
160+
pageNext: firstPage?.pages?.next,
161+
})
162+
163+
expect(secondPage.items).toHaveLength(2)
164+
expect(firstPage.items[0].sys.id).not.toEqual(secondPage.items[0].sys.id)
165+
})
166+
167+
test('should support backward pagination', async () => {
168+
const firstPage = await environment.getPublishedEntriesWithCursor({
169+
limit: 2,
170+
order: ['sys.createdAt'],
171+
})
172+
const secondPage = await environment.getPublishedEntriesWithCursor({
173+
limit: 2,
174+
pageNext: firstPage?.pages?.next,
175+
order: ['sys.createdAt'],
176+
})
177+
const result = await environment.getPublishedEntriesWithCursor({
178+
limit: 2,
179+
pagePrev: secondPage?.pages?.prev,
180+
order: ['sys.createdAt'],
181+
})
182+
183+
expect(result.items).toHaveLength(2)
184+
185+
firstPage.items.forEach((item, index) => {
186+
expect(item.sys.id).equal(result.items[index].sys.id)
187+
})
188+
})
189+
})
190+
123191
test('Gets Entry snapshots', async () => {
124192
return environment.getEntry(TestDefaults.entry.testEntryId).then((entry) => {
125193
return entry.getSnapshots().then((response) => {

0 commit comments

Comments
 (0)