0
Fork 0
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:
wangsijie 2023-01-08 20:35:10 +08:00 committed by GitHub
parent 0501ccc5ca
commit 364e51d755
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View file

@ -15,7 +15,7 @@ const { table, fields } = convertToIdentifiers(Resources);
export const findTotalNumberOfResources = async () => getTotalRowCount(table); export const findTotalNumberOfResources = async () => getTotalRowCount(table);
export const findAllResources = async (limit: number, offset: number) => export const findAllResources = async (limit?: number, offset?: number) =>
manyRows( manyRows(
envSet.pool.query<Resource>(sql` envSet.pool.query<Resource>(sql`
select ${sql.join(Object.values(fields), sql`, `)} select ${sql.join(Object.values(fields), sql`, `)}

View file

@ -54,6 +54,13 @@ describe('resource routes', () => {
const response = await resourceRequest.get('/resources'); const response = await resourceRequest.get('/resources');
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.body).toEqual([mockResource]); 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'); expect(response.header).toHaveProperty('total-number', '10');
}); });
@ -61,7 +68,6 @@ describe('resource routes', () => {
const response = await resourceRequest.get('/resources?includeScopes=true'); const response = await resourceRequest.get('/resources?includeScopes=true');
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.body).toEqual([{ ...mockResource, scopes: [] }]); expect(response.body).toEqual([{ ...mockResource, scopes: [] }]);
expect(response.header).toHaveProperty('total-number', '10');
}); });
it('POST /resources', async () => { it('POST /resources', async () => {

View file

@ -33,18 +33,25 @@ const scopeId = resourceId;
export default function resourceRoutes<T extends AuthedRouter>(router: T) { export default function resourceRoutes<T extends AuthedRouter>(router: T) {
router.get( router.get(
'/resources', '/resources',
koaPagination(), koaPagination({ isOptional: true }),
koaGuard({ koaGuard({
query: object({ query: object({
includeScopes: string().optional(), includeScopes: string().optional(),
}), }),
}), }),
async (ctx, next) => { async (ctx, next) => {
const { limit, offset } = ctx.pagination; const { limit, offset, disabled } = ctx.pagination;
const { const {
query: { includeScopes }, query: { includeScopes },
} = ctx.guard; } = ctx.guard;
if (disabled) {
const resources = await findAllResources();
ctx.body = isTrue(includeScopes) ? await attachScopesToResources(resources) : resources;
return next();
}
const [{ count }, resources] = await Promise.all([ const [{ count }, resources] = await Promise.all([
findTotalNumberOfResources(), findTotalNumberOfResources(),
findAllResources(limit, offset), findAllResources(limit, offset),