Skip to content

Commit 6317248

Browse files
authored
Merge pull request #97 from teofilomonteiro/support-include-total-count
Support include total count for listing roles and resources
2 parents 75174e2 + 2846aa8 commit 6317248

7 files changed

Lines changed: 146 additions & 30 deletions

File tree

src/api/base.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ export interface IPagination {
4747
perPage?: number;
4848
}
4949

50+
interface IBasePaginationExtended {
51+
/**
52+
* the page number to fetch (default: 1)
53+
*/
54+
page?: number;
55+
/**
56+
* how many items to fetch per page (default: 100)
57+
*/
58+
perPage?: number;
59+
/**
60+
* the total number of items
61+
*/
62+
includeTotalCount?: boolean;
63+
}
64+
65+
type IPaginationForceIncludeTotal = IBasePaginationExtended & { includeTotalCount: true };
66+
export type IPaginationExtended = IBasePaginationExtended | IPaginationForceIncludeTotal;
67+
68+
export type ReturnPaginationType<
69+
T extends IPaginationExtended,
70+
Y,
71+
Z,
72+
> = T extends IPaginationForceIncludeTotal ? Y : Z;
73+
5074
export abstract class BasePermitApi {
5175
protected openapiClientConfig: Configuration;
5276
private scopeApi: APIKeysApi;

src/api/deprecated.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ export class DeprecatedApiClient extends BasePermitApi implements IDeprecatedPer
179179
await this.ensureContext(ApiContextLevel.ENVIRONMENT);
180180

181181
try {
182-
const response = await this._roles.listRoles({
182+
const response = (await this._roles.listRoles({
183183
...this.config.apiContext.environmentContext,
184-
});
184+
})) as AxiosResponse<RoleRead[]>;
185185

186186
this.logger.debug(`[${response.status}] permit.api.listRoles()`);
187187
return response.data;

src/api/resources.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ import { Logger } from 'pino';
33
import { IPermitConfig } from '../config';
44
import {
55
ResourcesApi as AutogenResourcesApi,
6+
PaginatedResultResourceRead,
67
ResourceCreate,
78
ResourceRead,
89
ResourceReplace,
910
ResourceUpdate,
1011
} from '../openapi';
1112
import { BASE_PATH } from '../openapi/base';
1213

13-
import { BasePermitApi, IPagination } from './base';
14+
import { BasePermitApi, IPaginationExtended, ReturnPaginationType } from './base';
1415
import { ApiContextLevel, ApiKeyLevel } from './context';
1516

1617
export { ResourceCreate, ResourceRead, ResourceReplace, ResourceUpdate } from '../openapi';
1718

18-
export interface IListResourceUsers extends IPagination {
19-
resourceKey: string;
20-
}
21-
2219
export interface IResourcesApi {
2320
/**
2421
* Retrieves a list of resources.
2522
*
26-
* @param pagination The pagination options, @see {@link IPagination}
23+
* @param pagination The pagination options, @see {@link IPaginationExtended}
2724
* @returns A promise that resolves to an array of resources.
2825
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
2926
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
3027
*/
31-
list(pagination?: IPagination): Promise<ResourceRead[]>;
28+
list(): Promise<ResourceRead[]>;
29+
list<T extends IPaginationExtended>(
30+
pagination?: T,
31+
): Promise<ReturnPaginationType<T, PaginatedResultResourceRead, ResourceRead[]>>;
3232

3333
/**
3434
* Retrieves a resource by its key.
@@ -128,13 +128,19 @@ export class ResourcesApi extends BasePermitApi implements IResourcesApi {
128128
/**
129129
* Retrieves a list of resources.
130130
*
131-
* @param pagination The pagination options, @see {@link IPagination}
131+
* @param pagination The pagination options, @see {@link IPaginationExtended}
132132
* @returns A promise that resolves to an array of resources.
133133
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
134134
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
135135
*/
136-
public async list(pagination?: IPagination): Promise<ResourceRead[]> {
137-
const { page = 1, perPage = 100 } = pagination ?? {};
136+
public async list(): Promise<ResourceRead[]>;
137+
public async list<T extends IPaginationExtended>(
138+
pagination?: T,
139+
): Promise<ReturnPaginationType<T, PaginatedResultResourceRead, ResourceRead[]>>;
140+
public async list(
141+
pagination?: IPaginationExtended,
142+
): Promise<ResourceRead[] | PaginatedResultResourceRead> {
143+
const { page = 1, perPage = 100, includeTotalCount } = pagination ?? {};
138144
await this.ensureAccessLevel(ApiKeyLevel.ENVIRONMENT_LEVEL_API_KEY);
139145
await this.ensureContext(ApiContextLevel.ENVIRONMENT);
140146
try {
@@ -143,6 +149,7 @@ export class ResourcesApi extends BasePermitApi implements IResourcesApi {
143149
...this.config.apiContext.environmentContext,
144150
page,
145151
perPage,
152+
includeTotalCount,
146153
})
147154
).data;
148155
} catch (err) {

src/api/roles.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { Logger } from 'pino';
22

33
import { IPermitConfig } from '../config';
4-
import { RolesApi as AutogenRolesApi, RoleCreate, RoleRead, RoleUpdate } from '../openapi';
4+
import {
5+
RolesApi as AutogenRolesApi,
6+
PaginatedResultRoleRead,
7+
RoleCreate,
8+
RoleRead,
9+
RoleUpdate,
10+
} from '../openapi';
511
import { BASE_PATH } from '../openapi/base';
612

7-
import { BasePermitApi, IPagination } from './base';
13+
import { BasePermitApi, IPaginationExtended, ReturnPaginationType } from './base';
814
import { ApiContextLevel, ApiKeyLevel } from './context';
915

1016
export { RoleCreate, RoleRead, RoleUpdate } from '../openapi';
@@ -13,12 +19,15 @@ export interface IRolesApi {
1319
/**
1420
* Retrieves a list of roles.
1521
*
16-
* @param pagination The pagination options, @see {@link IPagination}
22+
* @param pagination The pagination options, @see {@link IPaginationExtended}
1723
* @returns A promise that resolves to an array of roles.
1824
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
1925
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
2026
*/
21-
list(pagination?: IPagination): Promise<RoleRead[]>;
27+
list(): Promise<RoleRead[]>;
28+
list<T extends IPaginationExtended>(
29+
pagination?: T,
30+
): Promise<ReturnPaginationType<T, PaginatedResultRoleRead, RoleRead[]>>;
2231

2332
/**
2433
* Retrieves a role by its key.
@@ -127,13 +136,19 @@ export class RolesApi extends BasePermitApi implements IRolesApi {
127136
/**
128137
* Retrieves a list of roles.
129138
*
130-
* @param pagination The pagination options, @see {@link IPagination}
139+
* @param pagination The pagination options, @see {@link IPaginationExtended}
131140
* @returns A promise that resolves to an array of roles.
132141
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
133142
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
134143
*/
135-
public async list(pagination?: IPagination): Promise<RoleRead[]> {
136-
const { page = 1, perPage = 100 } = pagination ?? {};
144+
public async list(): Promise<RoleRead[]>;
145+
public async list<T extends IPaginationExtended>(
146+
pagination?: T,
147+
): Promise<ReturnPaginationType<T, PaginatedResultRoleRead, RoleRead[]>>;
148+
public async list(
149+
pagination?: IPaginationExtended,
150+
): Promise<PaginatedResultRoleRead | RoleRead[]> {
151+
const { page = 1, perPage = 100, includeTotalCount } = pagination ?? {};
137152
await this.ensureAccessLevel(ApiKeyLevel.ENVIRONMENT_LEVEL_API_KEY);
138153
await this.ensureContext(ApiContextLevel.ENVIRONMENT);
139154
try {
@@ -142,6 +157,7 @@ export class RolesApi extends BasePermitApi implements IRolesApi {
142157
...this.config.apiContext.environmentContext,
143158
page,
144159
perPage,
160+
includeTotalCount,
145161
})
146162
).data;
147163
} catch (err) {

src/openapi/api/resources-api.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
// @ts-ignore
3232
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from '../base';
3333
// @ts-ignore
34-
import { HTTPValidationError } from '../types';
34+
import { HTTPValidationError, PaginatedResultResourceRead } from '../types';
3535
// @ts-ignore
3636
import { ResourceCreate } from '../types';
3737
// @ts-ignore
@@ -219,6 +219,7 @@ export const ResourcesApiAxiosParamCreator = function (configuration?: Configura
219219
* @param {boolean} [includeBuiltIn] Whether to include or exclude built-in resources, default is False
220220
* @param {number} [page] Page number of the results to fetch, starting at 1.
221221
* @param {number} [perPage] The number of results per page (max 100).
222+
* @param {includeTotalCount} [includeTotalCount] Include the total count of resources in the response, default is False
222223
* @param {*} [options] Override http request option.
223224
* @throws {RequiredError}
224225
*/
@@ -228,6 +229,7 @@ export const ResourcesApiAxiosParamCreator = function (configuration?: Configura
228229
includeBuiltIn?: boolean,
229230
page?: number,
230231
perPage?: number,
232+
includeTotalCount?: boolean,
231233
options: AxiosRequestConfig = {},
232234
): Promise<RequestArgs> => {
233235
// verify required parameter 'projId' is not null or undefined
@@ -264,6 +266,10 @@ export const ResourcesApiAxiosParamCreator = function (configuration?: Configura
264266
localVarQueryParameter['per_page'] = perPage;
265267
}
266268

269+
if (includeTotalCount !== undefined) {
270+
localVarQueryParameter['include_total_count'] = includeTotalCount;
271+
}
272+
267273
setSearchParams(localVarUrlObj, localVarQueryParameter);
268274
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
269275
localVarRequestOptions.headers = {
@@ -492,6 +498,7 @@ export const ResourcesApiFp = function (configuration?: Configuration) {
492498
* @param {boolean} [includeBuiltIn] Whether to include or exclude built-in resources, default is False
493499
* @param {number} [page] Page number of the results to fetch, starting at 1.
494500
* @param {number} [perPage] The number of results per page (max 100).
501+
* @param {includeTotalCount} [includeTotalCount] Include the total count of resources in the response, default is False
495502
* @param {*} [options] Override http request option.
496503
* @throws {RequiredError}
497504
*/
@@ -501,14 +508,21 @@ export const ResourcesApiFp = function (configuration?: Configuration) {
501508
includeBuiltIn?: boolean,
502509
page?: number,
503510
perPage?: number,
511+
includeTotalCount?: boolean,
504512
options?: AxiosRequestConfig,
505-
): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<Array<ResourceRead>>> {
513+
): Promise<
514+
(
515+
axios?: AxiosInstance,
516+
basePath?: string,
517+
) => AxiosPromise<Array<ResourceRead> | PaginatedResultResourceRead>
518+
> {
506519
const localVarAxiosArgs = await localVarAxiosParamCreator.listResources(
507520
projId,
508521
envId,
509522
includeBuiltIn,
510523
page,
511524
perPage,
525+
includeTotalCount,
512526
options,
513527
);
514528
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
@@ -644,6 +658,7 @@ export const ResourcesApiFactory = function (
644658
* @param {boolean} [includeBuiltIn] Whether to include or exclude built-in resources, default is False
645659
* @param {number} [page] Page number of the results to fetch, starting at 1.
646660
* @param {number} [perPage] The number of results per page (max 100).
661+
* @param {includeTotalCount} [includeTotalCount] Include the total count of resources in the response, default is False
647662
* @param {*} [options] Override http request option.
648663
* @throws {RequiredError}
649664
*/
@@ -653,10 +668,11 @@ export const ResourcesApiFactory = function (
653668
includeBuiltIn?: boolean,
654669
page?: number,
655670
perPage?: number,
671+
includeTotalCount?: boolean,
656672
options?: any,
657-
): AxiosPromise<Array<ResourceRead>> {
673+
): AxiosPromise<Array<ResourceRead> | PaginatedResultResourceRead> {
658674
return localVarFp
659-
.listResources(projId, envId, includeBuiltIn, page, perPage, options)
675+
.listResources(projId, envId, includeBuiltIn, page, perPage, includeTotalCount, options)
660676
.then((request) => request(axios, basePath));
661677
},
662678
/**
@@ -828,6 +844,14 @@ export interface ResourcesApiListResourcesRequest {
828844
* @memberof ResourcesApiListResources
829845
*/
830846
readonly perPage?: number;
847+
848+
/**
849+
* Include total count in response
850+
* @type {boolean}
851+
* @memberof RolesApiListRoles
852+
* @default false
853+
*/
854+
readonly includeTotalCount?: boolean;
831855
}
832856

833857
/**
@@ -992,6 +1016,7 @@ export class ResourcesApi extends BaseAPI {
9921016
requestParameters.includeBuiltIn,
9931017
requestParameters.page,
9941018
requestParameters.perPage,
1019+
requestParameters.includeTotalCount,
9951020
options,
9961021
)
9971022
.then((request) => request(this.axios, this.basePath));

0 commit comments

Comments
 (0)