Skip to content

Commit 8040298

Browse files
Add-role-assiment-details (#100)
* add base to use details information * add new role assignment details types * support the new endpoints with details * add missing type * make things build * user api implements IUsersApi
1 parent 61d4432 commit 8040298

14 files changed

Lines changed: 576 additions & 41 deletions

src/api/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface IPagination {
4747
perPage?: number;
4848
}
4949

50-
interface IBasePaginationExtended {
50+
export interface IBasePaginationExtended {
5151
/**
5252
* the page number to fetch (default: 1)
5353
*/

src/api/deprecated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export class DeprecatedApiClient extends BasePermitApi implements IDeprecatedPer
415415
this.logger.debug(
416416
`[${response.status}] permit.api.getAssignedRoles(${user}, ${tenant ?? 'all tenants'})`,
417417
);
418-
return response.data;
418+
return response.data as RoleAssignmentRead[];
419419
} catch (err) {
420420
if (axios.isAxiosError(err)) {
421421
this.logger.error(

src/api/role-assignments.ts

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,33 @@ import {
55
RoleAssignmentsApi as AutogenRoleAssignmentsApi,
66
BulkRoleAssignmentReport,
77
BulkRoleUnAssignmentReport,
8+
PaginatedResultRoleAssignmentDetailedRead,
9+
PaginatedResultRoleAssignmentRead,
810
RoleAssignmentCreate,
11+
RoleAssignmentDetailedRead,
912
RoleAssignmentRead,
1013
RoleAssignmentRemove,
1114
} from '../openapi';
1215
import { BASE_PATH } from '../openapi/base';
1316

14-
import { BaseFactsPermitAPI, IPagination, IWaitForSync } from './base';
17+
import { BaseFactsPermitAPI, IBasePaginationExtended, IWaitForSync } from './base';
1518
import { ApiContextLevel, ApiKeyLevel } from './context';
1619

1720
export {
1821
BulkRoleAssignmentReport,
1922
BulkRoleUnAssignmentReport,
23+
PaginatedResultRoleAssignmentDetailedRead,
24+
PaginatedResultRoleAssignmentRead,
2025
RoleAssignmentCreate,
2126
RoleAssignmentRead,
27+
RoleAssignmentDetailedRead,
2228
RoleAssignmentRemove,
2329
} from '../openapi';
2430

2531
/**
2632
* Represents the parameters for listing role assignments.
2733
*/
28-
export interface IListRoleAssignments extends IPagination {
34+
export interface IBaseListRoleAssignments extends IBasePaginationExtended {
2935
/**
3036
* optional user filter, will only return role assignments granted to this user.
3137
*/
@@ -45,8 +51,33 @@ export interface IListRoleAssignments extends IPagination {
4551
* optional resource instance filter, will only return (resource) role assignments granted on that resource instance.
4652
*/
4753
resourceInstance?: string;
54+
55+
/**
56+
* optional detailed flag, will return detailed role assignments.
57+
*/
58+
detailed?: boolean;
4859
}
4960

61+
type IListRoleAssignmentsIncludeTotalCount = IBaseListRoleAssignments & { includeTotalCount: true };
62+
63+
type IListRoleAssignmentsDetailed = IBaseListRoleAssignments & { detailed: true };
64+
65+
export type IListRoleAssignments =
66+
| IBaseListRoleAssignments
67+
| IListRoleAssignmentsIncludeTotalCount
68+
| IListRoleAssignmentsDetailed;
69+
70+
type ReturnListRoleAssignments<T extends IListRoleAssignments> =
71+
T extends IListRoleAssignmentsIncludeTotalCount
72+
? // with total count
73+
T extends IListRoleAssignmentsDetailed
74+
? PaginatedResultRoleAssignmentDetailedRead
75+
: PaginatedResultRoleAssignmentRead
76+
: // without total count
77+
T extends IListRoleAssignmentsDetailed
78+
? RoleAssignmentDetailedRead[]
79+
: RoleAssignmentRead[];
80+
5081
/**
5182
* API client for managing role assignments.
5283
*/
@@ -59,7 +90,7 @@ export interface IRoleAssignmentsApi extends IWaitForSync {
5990
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
6091
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
6192
*/
62-
list(params: IListRoleAssignments): Promise<RoleAssignmentRead[]>;
93+
list<T extends IListRoleAssignments>(params: T): Promise<ReturnListRoleAssignments<T>>;
6394

6495
/**
6596
* Assigns a role to a user in the scope of a given tenant.
@@ -132,10 +163,29 @@ export class RoleAssignmentsApi extends BaseFactsPermitAPI implements IRoleAssig
132163
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
133164
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
134165
*/
135-
public async list(params: IListRoleAssignments): Promise<RoleAssignmentRead[]> {
166+
public async list<T extends IListRoleAssignments>(
167+
params: T,
168+
): Promise<ReturnListRoleAssignments<T>>;
169+
public async list(
170+
params: IListRoleAssignments,
171+
): Promise<
172+
| Array<RoleAssignmentRead>
173+
| Array<RoleAssignmentDetailedRead>
174+
| PaginatedResultRoleAssignmentRead
175+
| PaginatedResultRoleAssignmentDetailedRead
176+
> {
136177
await this.ensureAccessLevel(ApiKeyLevel.ENVIRONMENT_LEVEL_API_KEY);
137178
await this.ensureContext(ApiContextLevel.ENVIRONMENT);
138-
const { user, tenant, role, resourceInstance, page = 1, perPage = 100 } = params;
179+
const {
180+
user,
181+
tenant,
182+
role,
183+
resourceInstance,
184+
page = 1,
185+
perPage = 100,
186+
detailed,
187+
includeTotalCount,
188+
} = params;
139189
try {
140190
return (
141191
await this.roleAssignments.listRoleAssignments({
@@ -144,8 +194,10 @@ export class RoleAssignmentsApi extends BaseFactsPermitAPI implements IRoleAssig
144194
tenant,
145195
role,
146196
resourceInstance,
197+
detailed,
147198
page,
148199
perPage,
200+
includeTotalCount,
149201
})
150202
).data;
151203
} catch (err) {

src/api/users.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import { IPermitConfig } from '../config';
44
import {
55
RoleAssignmentsApi as AutogenRoleAssignmentsApi,
66
UsersApi as AutogenUsersApi,
7+
PaginatedResultRoleAssignmentDetailedRead,
8+
PaginatedResultRoleAssignmentRead,
79
PaginatedResultUserRead,
810
RoleAssignmentCreate,
11+
RoleAssignmentDetailedRead,
912
RoleAssignmentRead,
1013
RoleAssignmentRemove,
1114
UserCreate,
@@ -43,7 +46,7 @@ export interface ICreateOrUpdateUserResult {
4346
created: boolean;
4447
}
4548

46-
export interface IGetUserRoles {
49+
export interface IBaseGetUserRoles {
4750
/**
4851
* id or key of the user
4952
* @type {string}
@@ -56,19 +59,50 @@ export interface IGetUserRoles {
5659
*/
5760
readonly tenant?: string;
5861

62+
/**
63+
* Whether to return full details about the user, tenant and role
64+
* @type {boolean}
65+
* @default false
66+
*/
67+
readonly detailed?: boolean;
68+
69+
/**
70+
* If true, returns the list of role assignments and the total count.
71+
* @type {boolean}
72+
* @default false
73+
*/
74+
readonly includeTotalCount?: boolean;
75+
5976
/**
6077
* Page number of the results to fetch, starting at 1.
6178
* @type {number}
79+
* @default 1
6280
*/
6381
readonly page?: number;
6482

6583
/**
6684
* The number of results per page (max 100).
6785
* @type {number}
86+
* @default 100
6887
*/
6988
readonly perPage?: number;
7089
}
7190

91+
type IGetUserRolesWithTotalCount = IBaseGetUserRoles & { includeTotalCount: true };
92+
type IGetUserRolesWithDetails = IBaseGetUserRoles & { detailed: true };
93+
94+
type IGetUserRoles = IBaseGetUserRoles | IGetUserRolesWithTotalCount | IGetUserRolesWithDetails;
95+
96+
type ReturnIGetUserRolesType<T extends IGetUserRoles> = T extends IGetUserRolesWithTotalCount
97+
? // with total count
98+
T extends IGetUserRolesWithDetails
99+
? PaginatedResultRoleAssignmentDetailedRead
100+
: PaginatedResultRoleAssignmentRead
101+
: // without total count
102+
T extends IGetUserRolesWithDetails
103+
? RoleAssignmentDetailedRead[]
104+
: RoleAssignmentRead[];
105+
72106
export interface IUsersListParams extends IPagination {
73107
search?: string;
74108
role?: string;
@@ -187,7 +221,7 @@ export interface IUsersApi extends IWaitForSync {
187221
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
188222
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
189223
*/
190-
getAssignedRoles({ user, tenant, page, perPage }: IGetUserRoles): Promise<RoleAssignmentRead[]>;
224+
getAssignedRoles<T extends IGetUserRoles>(params: T): Promise<ReturnIGetUserRolesType<T>>;
191225

192226
/**
193227
* Creates users in bulk.
@@ -225,7 +259,7 @@ export interface IUsersApi extends IWaitForSync {
225259
/**
226260
* The UsersApi class provides methods for interacting with Permit Users.
227261
*/
228-
export class UsersApi extends BaseFactsPermitAPI {
262+
export class UsersApi extends BaseFactsPermitAPI implements IUsersApi {
229263
private users: AutogenUsersApi;
230264
private roleAssignments: AutogenRoleAssignmentsApi;
231265
private bulkOperationsApi: BulkOperationsApi;
@@ -559,12 +593,22 @@ export class UsersApi extends BaseFactsPermitAPI {
559593
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
560594
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
561595
*/
596+
public async getAssignedRoles<T extends IGetUserRoles>(
597+
params: T,
598+
): Promise<ReturnIGetUserRolesType<T>>;
562599
public async getAssignedRoles({
563600
user,
564601
tenant,
565602
page = 1,
566603
perPage = 100,
567-
}: IGetUserRoles): Promise<RoleAssignmentRead[]> {
604+
detailed = false,
605+
includeTotalCount = false,
606+
}: IGetUserRoles): Promise<
607+
| RoleAssignmentRead[]
608+
| RoleAssignmentDetailedRead[]
609+
| PaginatedResultRoleAssignmentRead
610+
| PaginatedResultRoleAssignmentDetailedRead
611+
> {
568612
await this.ensureAccessLevel(ApiKeyLevel.ENVIRONMENT_LEVEL_API_KEY);
569613
await this.ensureContext(ApiContextLevel.ENVIRONMENT);
570614
try {
@@ -575,6 +619,8 @@ export class UsersApi extends BaseFactsPermitAPI {
575619
tenant,
576620
page,
577621
perPage,
622+
detailed,
623+
includeTotalCount,
578624
})
579625
).data;
580626
} catch (err) {

0 commit comments

Comments
 (0)