From 1cd854a63c7c0ae44c07c9fc5ac347baaaf4677f Mon Sep 17 00:00:00 2001 From: Charles Zhao Date: Wed, 17 May 2023 11:50:53 +0800 Subject: [PATCH] chore(core): add response guard for user roles APIs (#3773) refactor(core): add response and status guards to admin user role APIs --- packages/core/src/routes/admin-user-role.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/core/src/routes/admin-user-role.ts b/packages/core/src/routes/admin-user-role.ts index efcd6a4dc..06d4ca7b4 100644 --- a/packages/core/src/routes/admin-user-role.ts +++ b/packages/core/src/routes/admin-user-role.ts @@ -1,6 +1,7 @@ +import { Roles } from '@logto/schemas'; import { generateStandardId } from '@logto/shared'; import { tryThat } from '@silverhand/essentials'; -import { object, string } from 'zod'; +import { array, object, string } from 'zod'; import RequestError from '#src/errors/RequestError/index.js'; import koaGuard from '#src/middleware/koa-guard.js'; @@ -27,19 +28,19 @@ export default function adminUserRoleRoutes( koaPagination(), koaGuard({ params: object({ userId: string() }), + response: array(Roles.guard), + status: [200, 400, 404], }), async (ctx, next) => { - const { - params: { userId }, - } = ctx.guard; + const { userId } = ctx.guard.params; const { limit, offset } = ctx.pagination; const { searchParams } = ctx.request.URL; + await findUserById(userId); return tryThat( async () => { const search = parseSearchParamsForSearch(searchParams); - const usersRoles = await findUsersRolesByUserId(userId); const roleIds = usersRoles.map(({ roleId }) => roleId); const [{ count }, roles] = await Promise.all([ @@ -71,6 +72,7 @@ export default function adminUserRoleRoutes( koaGuard({ params: object({ userId: string() }), body: object({ roleIds: string().min(1).array() }), + status: [201, 404, 422], }), async (ctx, next) => { const { @@ -107,6 +109,7 @@ export default function adminUserRoleRoutes( koaGuard({ params: object({ userId: string() }), body: object({ roleIds: string().min(1).array() }), + status: [200, 404], }), async (ctx, next) => { const { @@ -144,6 +147,7 @@ export default function adminUserRoleRoutes( '/users/:userId/roles/:roleId', koaGuard({ params: object({ userId: string(), roleId: string() }), + status: [204, 404], }), async (ctx, next) => { const {