mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
feat(core): set resources pagination to optional (#2845)
This commit is contained in:
parent
0501ccc5ca
commit
364e51d755
3 changed files with 17 additions and 4 deletions
|
@ -15,7 +15,7 @@ const { table, fields } = convertToIdentifiers(Resources);
|
|||
|
||||
export const findTotalNumberOfResources = async () => getTotalRowCount(table);
|
||||
|
||||
export const findAllResources = async (limit: number, offset: number) =>
|
||||
export const findAllResources = async (limit?: number, offset?: number) =>
|
||||
manyRows(
|
||||
envSet.pool.query<Resource>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
|
|
|
@ -54,6 +54,13 @@ describe('resource routes', () => {
|
|||
const response = await resourceRequest.get('/resources');
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual([mockResource]);
|
||||
expect(response.header).not.toHaveProperty('total-number');
|
||||
});
|
||||
|
||||
it('GET /resources?page=1', async () => {
|
||||
const response = await resourceRequest.get('/resources?page=1');
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual([mockResource]);
|
||||
expect(response.header).toHaveProperty('total-number', '10');
|
||||
});
|
||||
|
||||
|
@ -61,7 +68,6 @@ describe('resource routes', () => {
|
|||
const response = await resourceRequest.get('/resources?includeScopes=true');
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual([{ ...mockResource, scopes: [] }]);
|
||||
expect(response.header).toHaveProperty('total-number', '10');
|
||||
});
|
||||
|
||||
it('POST /resources', async () => {
|
||||
|
|
|
@ -33,18 +33,25 @@ const scopeId = resourceId;
|
|||
export default function resourceRoutes<T extends AuthedRouter>(router: T) {
|
||||
router.get(
|
||||
'/resources',
|
||||
koaPagination(),
|
||||
koaPagination({ isOptional: true }),
|
||||
koaGuard({
|
||||
query: object({
|
||||
includeScopes: string().optional(),
|
||||
}),
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
const { limit, offset } = ctx.pagination;
|
||||
const { limit, offset, disabled } = ctx.pagination;
|
||||
const {
|
||||
query: { includeScopes },
|
||||
} = ctx.guard;
|
||||
|
||||
if (disabled) {
|
||||
const resources = await findAllResources();
|
||||
ctx.body = isTrue(includeScopes) ? await attachScopesToResources(resources) : resources;
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
const [{ count }, resources] = await Promise.all([
|
||||
findTotalNumberOfResources(),
|
||||
findAllResources(limit, offset),
|
||||
|
|
Loading…
Reference in a new issue