Skip to content

Commit 358f259

Browse files
authored
Search roles (#343)
* search roles * readme
1 parent e391c4e commit 358f259

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

Diff for: README.md

+9
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,15 @@ const rolesRes = await descopeClient.management.role.loadAll();
932932
rolesRes.data.forEach((role) => {
933933
// do something
934934
});
935+
936+
// Search roles
937+
const rolesRes = await descopeClient.management.role.search({
938+
tenantIds: ['t1', 't2'],
939+
roleNames: ['role1'],
940+
});
941+
rolesRes.data.forEach((role) => {
942+
// do something
943+
});
935944
```
936945

937946
### Query SSO Groups

Diff for: lib/management/paths.ts

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export default {
100100
update: '/v1/mgmt/role/update',
101101
delete: '/v1/mgmt/role/delete',
102102
loadAll: '/v1/mgmt/role/all',
103+
search: '/v1/mgmt/role/search',
103104
},
104105
flow: {
105106
list: '/v1/mgmt/flow/list',

Diff for: lib/management/role.test.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SdkResponse } from '@descope/core-js-sdk';
22
import withManagement from '.';
33
import apiPaths from './paths';
44
import { mockCoreSdk, mockHttpClient } from './testutils';
5-
import { Role } from './types';
5+
import { Role, RoleSearchOptions } from './types';
66

77
const management = withManagement(mockCoreSdk, 'key');
88

@@ -157,4 +157,36 @@ describe('Management Role', () => {
157157
});
158158
});
159159
});
160+
161+
describe('search', () => {
162+
it('should send the correct request and receive correct response', async () => {
163+
const httpResponse = {
164+
ok: true,
165+
json: () => mockAllRolesResponse,
166+
clone: () => ({
167+
json: () => Promise.resolve(mockAllRolesResponse),
168+
}),
169+
status: 200,
170+
};
171+
mockHttpClient.post.mockResolvedValue(httpResponse);
172+
const req: RoleSearchOptions = {
173+
tenantIds: ['t'],
174+
roleNames: ['r'],
175+
roleNameLike: 'x',
176+
permissionNames: ['p'],
177+
};
178+
const resp: SdkResponse<Role[]> = await management.role.search(req);
179+
180+
expect(mockHttpClient.post).toHaveBeenCalledWith(apiPaths.role.search, req, {
181+
token: 'key',
182+
});
183+
184+
expect(resp).toEqual({
185+
code: 200,
186+
data: mockRoles,
187+
ok: true,
188+
response: httpResponse,
189+
});
190+
});
191+
});
160192
});

Diff for: lib/management/role.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SdkResponse, transformResponse } from '@descope/core-js-sdk';
22
import { CoreSdk } from '../types';
33
import apiPaths from './paths';
4-
import { Role } from './types';
4+
import { Role, RoleSearchOptions } from './types';
55

66
type MultipleRoleResponse = {
77
roles: Role[];
@@ -46,6 +46,13 @@ const withRole = (sdk: CoreSdk, managementKey?: string) => ({
4646
}),
4747
(data) => data.roles,
4848
),
49+
search: (options: RoleSearchOptions): Promise<SdkResponse<Role[]>> =>
50+
transformResponse<MultipleRoleResponse, Role[]>(
51+
sdk.httpClient.post(apiPaths.role.search, options, {
52+
token: managementKey,
53+
}),
54+
(data) => data.roles,
55+
),
4956
});
5057

5158
export default withRole;

Diff for: lib/management/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ export type Role = {
236236
tenantId?: string;
237237
};
238238

239+
/** Search roles based on the parameters */
240+
export type RoleSearchOptions = {
241+
tenantIds?: string[];
242+
roleNames?: string[];
243+
roleNameLike?: string; // Search roles where name contains this - case insensitive
244+
permissionNames?: string[];
245+
};
246+
239247
/** Represents a group in a project. It has an id and display name and a list of group members. */
240248
export type Group = {
241249
id: string;

0 commit comments

Comments
 (0)