diff --git a/packages/core/src/queries/resource.ts b/packages/core/src/queries/resource.ts index effd96cb6..71b36e3fa 100644 --- a/packages/core/src/queries/resource.ts +++ b/packages/core/src/queries/resource.ts @@ -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(sql` select ${sql.join(Object.values(fields), sql`, `)} diff --git a/packages/core/src/routes/resource.test.ts b/packages/core/src/routes/resource.test.ts index be0c06dd0..f215de665 100644 --- a/packages/core/src/routes/resource.test.ts +++ b/packages/core/src/routes/resource.test.ts @@ -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 () => { diff --git a/packages/core/src/routes/resource.ts b/packages/core/src/routes/resource.ts index 523c3de94..b74a3c796 100644 --- a/packages/core/src/routes/resource.ts +++ b/packages/core/src/routes/resource.ts @@ -33,18 +33,25 @@ const scopeId = resourceId; export default function resourceRoutes(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),