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 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`, `)}
|
||||||
|
|
|
@ -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 () => {
|
||||||
|
|
|
@ -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),
|
||||||
|
|
Loading…
Reference in a new issue