Skip to content

Commit 5f14eaf

Browse files
authored
feat(projects): includeMembers option on projects.list() (#273)
1 parent dfc1031 commit 5f14eaf

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

src/projects/ProjectsClient.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ export class ObservableProjectsClient {
1414
}
1515

1616
/**
17-
* Fetch a list of projects the authenticated user has access to
17+
* Fetch a list of projects the authenticated user has access to.
18+
*
19+
* @param options - Options for the list request
20+
* @param options.includeMembers - Whether to include members in the response (default: true)
1821
*/
19-
list(): Observable<SanityProject[]> {
20-
return _request<SanityProject[]>(this.#client, this.#httpRequest, {uri: '/projects'})
22+
list(options?: {includeMembers?: true}): Observable<SanityProject[]>
23+
list(options?: {includeMembers?: false}): Observable<Omit<SanityProject, 'members'>[]>
24+
list(options?: {
25+
includeMembers?: boolean
26+
}): Observable<SanityProject[] | Omit<SanityProject, 'members'>[]> {
27+
const uri = options?.includeMembers === false ? '/projects?includeMembers=false' : '/projects'
28+
return _request<SanityProject[]>(this.#client, this.#httpRequest, {uri})
2129
}
2230

2331
/**
@@ -40,12 +48,16 @@ export class ProjectsClient {
4048
}
4149

4250
/**
43-
* Fetch a list of projects the authenticated user has access to
51+
* Fetch a list of projects the authenticated user has access to.
52+
*
53+
* @param options - Options for the list request
54+
* @param options.includeMembers - Whether to include members in the response (default: true)
4455
*/
45-
list(): Promise<SanityProject[]> {
46-
return lastValueFrom(
47-
_request<SanityProject[]>(this.#client, this.#httpRequest, {uri: '/projects'}),
48-
)
56+
list(options?: {includeMembers?: true}): Promise<SanityProject[]>
57+
list(options?: {includeMembers?: false}): Promise<Omit<SanityProject, 'members'>[]>
58+
list(options?: {includeMembers?: boolean}): Promise<SanityProject[]> {
59+
const uri = options?.includeMembers === false ? '/projects?includeMembers=false' : '/projects'
60+
return lastValueFrom(_request<SanityProject[]>(this.#client, this.#httpRequest, {uri}))
4961
}
5062

5163
/**

test/client.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,51 @@ describe('client', async () => {
287287
expect(projects[0].id, 'should have project id').toBe('foo')
288288
})
289289

290+
test('can request list of projects with members', async () => {
291+
nock(`https://${apiHost}`)
292+
.get('/v1/projects')
293+
.times(2)
294+
.reply(200, [{id: 'foo'}, {id: 'bar'}])
295+
296+
const client = createClient({useProjectHostname: false, apiHost: `https://${apiHost}`})
297+
let projects = await client.projects.list({includeMembers: true})
298+
expect(projects.length, 'should have two projects').toBe(2)
299+
expect(projects[0].id, 'should have project id').toBe('foo')
300+
301+
projects = await client.projects.list({includeMembers: undefined})
302+
expect(projects.length, 'should have two projects').toBe(2)
303+
expect(projects[0].id, 'should have project id').toBe('foo')
304+
})
305+
306+
test('can request list of projects without members', async () => {
307+
nock(`https://${apiHost}`)
308+
.get('/v1/projects?includeMembers=false')
309+
.reply(200, [{id: 'foo'}, {id: 'bar'}])
310+
311+
const client = createClient({useProjectHostname: false, apiHost: `https://${apiHost}`})
312+
const projects = await client.projects.list({includeMembers: false})
313+
expect(projects.length, 'should have two projects').toBe(2)
314+
expect(projects[0].id, 'should have project id').toBe('foo')
315+
expect(projects[0]).not.toHaveProperty('members')
316+
317+
// @ts-expect-error - `members` should not be part of type when using `includeMembers: false`
318+
expect(projects[0].members, 'should not have "members" prop').toBeUndefined()
319+
})
320+
321+
test('can request list of projects, ignoring non-false `includeMembers` option', async () => {
322+
nock(`https://${apiHost}`)
323+
.get('/v1/projects')
324+
.reply(200, [{id: 'foo'}, {id: 'bar'}])
325+
326+
const client = createClient({useProjectHostname: false, apiHost: `https://${apiHost}`})
327+
328+
// @ts-expect-error - `includeMembers` should be a boolean if specified
329+
const projects = await client.projects.list({includeMembers: 'nope'})
330+
331+
expect(projects.length, 'should have two projects').toBe(2)
332+
expect(projects[0].id, 'should have project id').toBe('foo')
333+
})
334+
290335
test('can request list of projects (custom api version)', async () => {
291336
nock(`https://${apiHost}`)
292337
.get('/v2019-01-29/projects')

0 commit comments

Comments
 (0)