Skip to content

Commit

Permalink
feat(projects): includeMembers option on projects.list() (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored Aug 1, 2023
1 parent dfc1031 commit 5f14eaf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/projects/ProjectsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ export class ObservableProjectsClient {
}

/**
* Fetch a list of projects the authenticated user has access to
* Fetch a list of projects the authenticated user has access to.
*
* @param options - Options for the list request
* @param options.includeMembers - Whether to include members in the response (default: true)
*/
list(): Observable<SanityProject[]> {
return _request<SanityProject[]>(this.#client, this.#httpRequest, {uri: '/projects'})
list(options?: {includeMembers?: true}): Observable<SanityProject[]>
list(options?: {includeMembers?: false}): Observable<Omit<SanityProject, 'members'>[]>
list(options?: {
includeMembers?: boolean
}): Observable<SanityProject[] | Omit<SanityProject, 'members'>[]> {
const uri = options?.includeMembers === false ? '/projects?includeMembers=false' : '/projects'
return _request<SanityProject[]>(this.#client, this.#httpRequest, {uri})
}

/**
Expand All @@ -40,12 +48,16 @@ export class ProjectsClient {
}

/**
* Fetch a list of projects the authenticated user has access to
* Fetch a list of projects the authenticated user has access to.
*
* @param options - Options for the list request
* @param options.includeMembers - Whether to include members in the response (default: true)
*/
list(): Promise<SanityProject[]> {
return lastValueFrom(
_request<SanityProject[]>(this.#client, this.#httpRequest, {uri: '/projects'}),
)
list(options?: {includeMembers?: true}): Promise<SanityProject[]>
list(options?: {includeMembers?: false}): Promise<Omit<SanityProject, 'members'>[]>
list(options?: {includeMembers?: boolean}): Promise<SanityProject[]> {
const uri = options?.includeMembers === false ? '/projects?includeMembers=false' : '/projects'
return lastValueFrom(_request<SanityProject[]>(this.#client, this.#httpRequest, {uri}))
}

/**
Expand Down
45 changes: 45 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,51 @@ describe('client', async () => {
expect(projects[0].id, 'should have project id').toBe('foo')
})

test('can request list of projects with members', async () => {
nock(`https://${apiHost}`)
.get('/v1/projects')
.times(2)
.reply(200, [{id: 'foo'}, {id: 'bar'}])

const client = createClient({useProjectHostname: false, apiHost: `https://${apiHost}`})
let projects = await client.projects.list({includeMembers: true})
expect(projects.length, 'should have two projects').toBe(2)
expect(projects[0].id, 'should have project id').toBe('foo')

projects = await client.projects.list({includeMembers: undefined})
expect(projects.length, 'should have two projects').toBe(2)
expect(projects[0].id, 'should have project id').toBe('foo')
})

test('can request list of projects without members', async () => {
nock(`https://${apiHost}`)
.get('/v1/projects?includeMembers=false')
.reply(200, [{id: 'foo'}, {id: 'bar'}])

const client = createClient({useProjectHostname: false, apiHost: `https://${apiHost}`})
const projects = await client.projects.list({includeMembers: false})
expect(projects.length, 'should have two projects').toBe(2)
expect(projects[0].id, 'should have project id').toBe('foo')
expect(projects[0]).not.toHaveProperty('members')

// @ts-expect-error - `members` should not be part of type when using `includeMembers: false`
expect(projects[0].members, 'should not have "members" prop').toBeUndefined()
})

test('can request list of projects, ignoring non-false `includeMembers` option', async () => {
nock(`https://${apiHost}`)
.get('/v1/projects')
.reply(200, [{id: 'foo'}, {id: 'bar'}])

const client = createClient({useProjectHostname: false, apiHost: `https://${apiHost}`})

// @ts-expect-error - `includeMembers` should be a boolean if specified
const projects = await client.projects.list({includeMembers: 'nope'})

expect(projects.length, 'should have two projects').toBe(2)
expect(projects[0].id, 'should have project id').toBe('foo')
})

test('can request list of projects (custom api version)', async () => {
nock(`https://${apiHost}`)
.get('/v2019-01-29/projects')
Expand Down

0 comments on commit 5f14eaf

Please sign in to comment.