Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support include total count for listing roles and resources #97

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ export interface IPagination {
perPage?: number;
}

interface IBasePaginationExtended {
/**
* the page number to fetch (default: 1)
*/
page?: number;
/**
* how many items to fetch per page (default: 100)
*/
perPage?: number;
/**
* the total number of items
*/
includeTotalCount?: boolean;
}

type IPaginationForceIncludeTotal = IBasePaginationExtended & { includeTotalCount: true };
export type IPaginationExtended = IBasePaginationExtended | IPaginationForceIncludeTotal;

export type ReturnPaginationType<
T extends IPaginationExtended,
Y,
Z,
> = T extends IPaginationForceIncludeTotal ? Y : Z;

export abstract class BasePermitApi {
protected openapiClientConfig: Configuration;
private scopeApi: APIKeysApi;
Expand Down
4 changes: 2 additions & 2 deletions src/api/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ export class DeprecatedApiClient extends BasePermitApi implements IDeprecatedPer
await this.ensureContext(ApiContextLevel.ENVIRONMENT);

try {
const response = await this._roles.listRoles({
const response = (await this._roles.listRoles({
...this.config.apiContext.environmentContext,
});
})) as AxiosResponse<RoleRead[]>;

this.logger.debug(`[${response.status}] permit.api.listRoles()`);
return response.data;
Expand Down
27 changes: 17 additions & 10 deletions src/api/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ import { Logger } from 'pino';
import { IPermitConfig } from '../config';
import {
ResourcesApi as AutogenResourcesApi,
PaginatedResultResourceRead,
ResourceCreate,
ResourceRead,
ResourceReplace,
ResourceUpdate,
} from '../openapi';
import { BASE_PATH } from '../openapi/base';

import { BasePermitApi, IPagination } from './base';
import { BasePermitApi, IPaginationExtended, ReturnPaginationType } from './base';
import { ApiContextLevel, ApiKeyLevel } from './context';

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

export interface IListResourceUsers extends IPagination {
resourceKey: string;
}

export interface IResourcesApi {
/**
* Retrieves a list of resources.
*
* @param pagination The pagination options, @see {@link IPagination}
* @param pagination The pagination options, @see {@link IPaginationExtended}
* @returns A promise that resolves to an array of resources.
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
*/
list(pagination?: IPagination): Promise<ResourceRead[]>;
list(): Promise<ResourceRead[]>;
list<T extends IPaginationExtended>(
pagination?: T,
): Promise<ReturnPaginationType<T, PaginatedResultResourceRead, ResourceRead[]>>;

/**
* Retrieves a resource by its key.
Expand Down Expand Up @@ -128,13 +128,19 @@ export class ResourcesApi extends BasePermitApi implements IResourcesApi {
/**
* Retrieves a list of resources.
*
* @param pagination The pagination options, @see {@link IPagination}
* @param pagination The pagination options, @see {@link IPaginationExtended}
* @returns A promise that resolves to an array of resources.
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
*/
public async list(pagination?: IPagination): Promise<ResourceRead[]> {
const { page = 1, perPage = 100 } = pagination ?? {};
public async list(): Promise<ResourceRead[]>;
public async list<T extends IPaginationExtended>(
pagination?: T,
): Promise<ReturnPaginationType<T, PaginatedResultResourceRead, ResourceRead[]>>;
public async list(
pagination?: IPaginationExtended,
): Promise<ResourceRead[] | PaginatedResultResourceRead> {
const { page = 1, perPage = 100, includeTotalCount } = pagination ?? {};
await this.ensureAccessLevel(ApiKeyLevel.ENVIRONMENT_LEVEL_API_KEY);
await this.ensureContext(ApiContextLevel.ENVIRONMENT);
try {
Expand All @@ -143,6 +149,7 @@ export class ResourcesApi extends BasePermitApi implements IResourcesApi {
...this.config.apiContext.environmentContext,
page,
perPage,
includeTotalCount,
})
).data;
} catch (err) {
Expand Down
30 changes: 23 additions & 7 deletions src/api/roles.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Logger } from 'pino';

import { IPermitConfig } from '../config';
import { RolesApi as AutogenRolesApi, RoleCreate, RoleRead, RoleUpdate } from '../openapi';
import {
RolesApi as AutogenRolesApi,
PaginatedResultRoleRead,
RoleCreate,
RoleRead,
RoleUpdate,
} from '../openapi';
import { BASE_PATH } from '../openapi/base';

import { BasePermitApi, IPagination } from './base';
import { BasePermitApi, IPaginationExtended, ReturnPaginationType } from './base';
import { ApiContextLevel, ApiKeyLevel } from './context';

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

/**
* Retrieves a role by its key.
Expand Down Expand Up @@ -127,13 +136,19 @@ export class RolesApi extends BasePermitApi implements IRolesApi {
/**
* Retrieves a list of roles.
*
* @param pagination The pagination options, @see {@link IPagination}
* @param pagination The pagination options, @see {@link IPaginationExtended}
* @returns A promise that resolves to an array of roles.
* @throws {@link PermitApiError} If the API returns an error HTTP status code.
* @throws {@link PermitContextError} If the configured {@link ApiContext} does not match the required endpoint context.
*/
public async list(pagination?: IPagination): Promise<RoleRead[]> {
const { page = 1, perPage = 100 } = pagination ?? {};
public async list(): Promise<RoleRead[]>;
public async list<T extends IPaginationExtended>(
pagination?: T,
): Promise<ReturnPaginationType<T, PaginatedResultRoleRead, RoleRead[]>>;
public async list(
pagination?: IPaginationExtended,
): Promise<PaginatedResultRoleRead | RoleRead[]> {
const { page = 1, perPage = 100, includeTotalCount } = pagination ?? {};
await this.ensureAccessLevel(ApiKeyLevel.ENVIRONMENT_LEVEL_API_KEY);
await this.ensureContext(ApiContextLevel.ENVIRONMENT);
try {
Expand All @@ -142,6 +157,7 @@ export class RolesApi extends BasePermitApi implements IRolesApi {
...this.config.apiContext.environmentContext,
page,
perPage,
includeTotalCount,
})
).data;
} catch (err) {
Expand Down
33 changes: 29 additions & 4 deletions src/openapi/api/resources-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
// @ts-ignore
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from '../base';
// @ts-ignore
import { HTTPValidationError } from '../types';
import { HTTPValidationError, PaginatedResultResourceRead } from '../types';
// @ts-ignore
import { ResourceCreate } from '../types';
// @ts-ignore
Expand Down Expand Up @@ -219,6 +219,7 @@ export const ResourcesApiAxiosParamCreator = function (configuration?: Configura
* @param {boolean} [includeBuiltIn] Whether to include or exclude built-in resources, default is False
* @param {number} [page] Page number of the results to fetch, starting at 1.
* @param {number} [perPage] The number of results per page (max 100).
* @param {includeTotalCount} [includeTotalCount] Include the total count of resources in the response, default is False
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
Expand All @@ -228,6 +229,7 @@ export const ResourcesApiAxiosParamCreator = function (configuration?: Configura
includeBuiltIn?: boolean,
page?: number,
perPage?: number,
includeTotalCount?: boolean,
options: AxiosRequestConfig = {},
): Promise<RequestArgs> => {
// verify required parameter 'projId' is not null or undefined
Expand Down Expand Up @@ -264,6 +266,10 @@ export const ResourcesApiAxiosParamCreator = function (configuration?: Configura
localVarQueryParameter['per_page'] = perPage;
}

if (includeTotalCount !== undefined) {
localVarQueryParameter['include_total_count'] = includeTotalCount;
}

setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {
Expand Down Expand Up @@ -492,6 +498,7 @@ export const ResourcesApiFp = function (configuration?: Configuration) {
* @param {boolean} [includeBuiltIn] Whether to include or exclude built-in resources, default is False
* @param {number} [page] Page number of the results to fetch, starting at 1.
* @param {number} [perPage] The number of results per page (max 100).
* @param {includeTotalCount} [includeTotalCount] Include the total count of resources in the response, default is False
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
Expand All @@ -501,14 +508,21 @@ export const ResourcesApiFp = function (configuration?: Configuration) {
includeBuiltIn?: boolean,
page?: number,
perPage?: number,
includeTotalCount?: boolean,
options?: AxiosRequestConfig,
): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<Array<ResourceRead>>> {
): Promise<
(
axios?: AxiosInstance,
basePath?: string,
) => AxiosPromise<Array<ResourceRead> | PaginatedResultResourceRead>
> {
const localVarAxiosArgs = await localVarAxiosParamCreator.listResources(
projId,
envId,
includeBuiltIn,
page,
perPage,
includeTotalCount,
options,
);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
Expand Down Expand Up @@ -644,6 +658,7 @@ export const ResourcesApiFactory = function (
* @param {boolean} [includeBuiltIn] Whether to include or exclude built-in resources, default is False
* @param {number} [page] Page number of the results to fetch, starting at 1.
* @param {number} [perPage] The number of results per page (max 100).
* @param {includeTotalCount} [includeTotalCount] Include the total count of resources in the response, default is False
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
Expand All @@ -653,10 +668,11 @@ export const ResourcesApiFactory = function (
includeBuiltIn?: boolean,
page?: number,
perPage?: number,
includeTotalCount?: boolean,
options?: any,
): AxiosPromise<Array<ResourceRead>> {
): AxiosPromise<Array<ResourceRead> | PaginatedResultResourceRead> {
return localVarFp
.listResources(projId, envId, includeBuiltIn, page, perPage, options)
.listResources(projId, envId, includeBuiltIn, page, perPage, includeTotalCount, options)
.then((request) => request(axios, basePath));
},
/**
Expand Down Expand Up @@ -828,6 +844,14 @@ export interface ResourcesApiListResourcesRequest {
* @memberof ResourcesApiListResources
*/
readonly perPage?: number;

/**
* Include total count in response
* @type {boolean}
* @memberof RolesApiListRoles
* @default false
*/
readonly includeTotalCount?: boolean;
}

/**
Expand Down Expand Up @@ -992,6 +1016,7 @@ export class ResourcesApi extends BaseAPI {
requestParameters.includeBuiltIn,
requestParameters.page,
requestParameters.perPage,
requestParameters.includeTotalCount,
options,
)
.then((request) => request(this.axios, this.basePath));
Expand Down
Loading