Skip to content

Commit a855106

Browse files
committed
feat: fix types and wrapper
1 parent ec8bdcc commit a855106

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

lib/common-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,8 +2356,8 @@ export type MRActions = {
23562356
Space: {
23572357
get: { params: GetSpaceParams; return: SpaceProps }
23582358
getMany: {
2359-
params: QueryParams & { organizationId?: string }
2360-
return: CollectionProp<SpaceProps>
2359+
params: (QueryParams | BasicCursorPaginationOptions) & { organizationId?: string }
2360+
return: CollectionProp<SpaceProps> | CursorPaginatedCollectionProp<SpaceProps>
23612361
}
23622362
getManyForOrganization: {
23632363
params: GetOrganizationParams & QueryParams

lib/create-contentful-api.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { createRequestConfig } from 'contentful-sdk-core'
22
import type {
33
Collection,
4+
CollectionProp,
5+
CursorPaginatedCollectionProp,
46
MakeRequest,
57
PaginationQueryParams,
68
QueryOptions,
@@ -12,7 +14,12 @@ import type {
1214
GetOAuthApplicationParams,
1315
GetUserParams,
1416
} from './common-types'
15-
import { wrapSpace, wrapSpaceCollection } from './entities/space'
17+
import { normalizeCursorPaginationResponse } from './common-utils'
18+
import {
19+
wrapSpace,
20+
wrapSpaceCollection,
21+
wrapSpaceCursorPaginatedCollection,
22+
} from './entities/space'
1623
import { wrapUser } from './entities/user'
1724
import {
1825
wrapPersonalAccessToken,
@@ -172,11 +179,19 @@ export default function createClientApi(makeRequest: MakeRequest) {
172179
query: (QueryOptions | BasicCursorPaginationOptions) & { cursor?: boolean } = {},
173180
organizationId?: string,
174181
): Promise<Collection<Space, SpaceProps> | CursorPaginatedCollection<Space, SpaceProps>> {
182+
const { cursor } = query
175183
return makeRequest({
176184
entityType: 'Space',
177185
action: 'getMany',
178186
params: { query: createRequestConfig({ query }).params, organizationId },
179-
}).then((data) => wrapSpaceCollection(makeRequest, data))
187+
}).then((data) =>
188+
cursor
189+
? wrapSpaceCursorPaginatedCollection(
190+
makeRequest,
191+
normalizeCursorPaginationResponse(data as CursorPaginatedCollectionProp<SpaceProps>),
192+
)
193+
: wrapSpaceCollection(makeRequest, data as CollectionProp<SpaceProps>),
194+
)
180195
},
181196

182197
/**

lib/entities/space.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
22
import copy from 'fast-copy'
33
import type { BasicMetaSysProps, DefaultElements, MakeRequest } from '../common-types'
4-
import { wrapCollection } from '../common-utils'
4+
import { wrapCollection, wrapCursorPaginatedCollection } from '../common-utils'
55
import type { ContentfulSpaceAPI } from '../create-space-api'
66
import createSpaceApi from '../create-space-api'
77
import enhanceWithMethods from '../enhance-with-methods'
@@ -40,3 +40,4 @@ export function wrapSpace(makeRequest: MakeRequest, data: SpaceProps): Space {
4040
* @internal
4141
*/
4242
export const wrapSpaceCollection = wrapCollection(wrapSpace)
43+
export const wrapSpaceCursorPaginatedCollection = wrapCursorPaginatedCollection(wrapSpace)

lib/plain/entities/space.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type {
33
GetSpaceParams,
44
QueryParams,
55
CollectionProp,
6+
CursorPaginatedCollectionProp,
7+
BasicCursorPaginationOptions,
68
GetOrganizationParams,
79
} from '../../common-types'
810
import type { OptionalDefaults } from '../wrappers/wrap'
@@ -34,7 +36,11 @@ export type SpacePlainClientAPI = {
3436
* });
3537
* ```
3638
*/
37-
getMany(params: OptionalDefaults<QueryParams>): Promise<CollectionProp<SpaceProps>>
39+
getMany(
40+
params: OptionalDefaults<
41+
(QueryParams | BasicCursorPaginationOptions) & { organizationId?: string }
42+
>,
43+
): Promise<CollectionProp<SpaceProps> | CursorPaginatedCollectionProp<SpaceProps>>
3844
/**
3945
* Fetches all the spaces in the given organization
4046
* @param params the organization ID and query parameters

test/unit/create-contentful-api.test.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,48 +65,57 @@ describe('createClientApi', () => {
6565
).rejects.toEqual(error)
6666
})
6767

68-
test('getSpaces calls getMany with query', async () => {
69-
const { api, makeRequest } = setup(
70-
Promise.resolve({ sys: { type: 'Array' }, total: 0, skip: 0, limit: 100, items: [] }),
71-
)
68+
const collectionResponse = { sys: { type: 'Array' }, total: 0, skip: 0, limit: 100, items: [] }
69+
const cursorCollectionResponse = { sys: { type: 'Array' }, limit: 100, items: [], pages: {} }
70+
71+
test('getSpaces calls getMany', async () => {
72+
const { api, makeRequest } = setup(Promise.resolve(collectionResponse))
7273
await api.getSpaces({ limit: 10 })
7374
expect(makeRequest).toHaveBeenCalledWith(
7475
expect.objectContaining({ entityType: 'Space', action: 'getMany' }),
7576
)
7677
})
7778

7879
test('getSpaces with cursor:true passes cursor in query', async () => {
79-
const { api, makeRequest } = setup(
80-
Promise.resolve({ sys: { type: 'Array' }, total: 0, skip: 0, limit: 100, items: [] }),
81-
)
80+
const { api, makeRequest } = setup(Promise.resolve(cursorCollectionResponse))
8281
await api.getSpaces({ cursor: true, limit: 10 })
8382
const [call] = makeRequest.mock.calls
8483
expect(call[0].action).toBe('getMany')
8584
expect(call[0].params.query).toMatchObject({ cursor: true, limit: 10 })
8685
})
8786

8887
test('getSpaces with cursor:true and pageNext passes pageNext in query', async () => {
89-
const { api, makeRequest } = setup(
90-
Promise.resolve({ sys: { type: 'Array' }, total: 0, skip: 0, limit: 100, items: [] }),
91-
)
88+
const { api, makeRequest } = setup(Promise.resolve(cursorCollectionResponse))
9289
await api.getSpaces({ cursor: true, pageNext: 'next-token' })
9390
const [call] = makeRequest.mock.calls
9491
expect(call[0].params.query).toMatchObject({ cursor: true, pageNext: 'next-token' })
9592
})
9693

97-
test('getSpaces with organizationId passes it in params', async () => {
98-
const { api, makeRequest } = setup(
99-
Promise.resolve({ sys: { type: 'Array' }, total: 0, skip: 0, limit: 100, items: [] }),
94+
test('getSpaces with cursor:true returns cursor paginated collection', async () => {
95+
const { api } = setup(
96+
Promise.resolve({ ...cursorCollectionResponse, pages: { next: 'pageNext=abc' } }),
10097
)
98+
const result = await api.getSpaces({ cursor: true })
99+
expect(result).toHaveProperty('pages')
100+
expect(result).not.toHaveProperty('total')
101+
})
102+
103+
test('getSpaces without cursor returns regular collection', async () => {
104+
const { api } = setup(Promise.resolve(collectionResponse))
105+
const result = await api.getSpaces()
106+
expect(result).toHaveProperty('total')
107+
expect(result).not.toHaveProperty('pages')
108+
})
109+
110+
test('getSpaces with organizationId passes it in params', async () => {
111+
const { api, makeRequest } = setup(Promise.resolve(collectionResponse))
101112
await api.getSpaces({}, 'test-org')
102113
const [call] = makeRequest.mock.calls
103114
expect(call[0].params.organizationId).toBe('test-org')
104115
})
105116

106117
test('getSpaces without organizationId omits it from params', async () => {
107-
const { api, makeRequest } = setup(
108-
Promise.resolve({ sys: { type: 'Array' }, total: 0, skip: 0, limit: 100, items: [] }),
109-
)
118+
const { api, makeRequest } = setup(Promise.resolve(collectionResponse))
110119
await api.getSpaces()
111120
const [call] = makeRequest.mock.calls
112121
expect(call[0].params.organizationId).toBeUndefined()

0 commit comments

Comments
 (0)