0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00
logto/packages/core/src/routes/log.ts
2022-10-21 14:03:35 +08:00

53 lines
1.3 KiB
TypeScript

import { Logs } from '@logto/schemas';
import { object, string } from 'zod';
import koaGuard from '@/middleware/koa-guard';
import koaPagination from '@/middleware/koa-pagination';
import { countLogs, findLogById, findLogs } from '@/queries/log';
import type { AuthedRouter } from './types';
export default function logRoutes<T extends AuthedRouter>(router: T) {
router.get(
'/logs',
koaPagination(),
koaGuard({
query: object({
userId: string().optional(),
applicationId: string().optional(),
logType: string().optional(),
}),
}),
async (ctx, next) => {
const { limit, offset } = ctx.pagination;
const {
query: { userId, applicationId, logType },
} = ctx.guard;
const [{ count }, logs] = await Promise.all([
countLogs({ logType, applicationId, userId }),
findLogs(limit, offset, { logType, userId, applicationId }),
]);
// Return totalCount to pagination middleware
ctx.pagination.totalCount = count;
ctx.body = logs;
return next();
}
);
router.get(
'/logs/:id',
koaGuard({ params: object({ id: string().min(1) }), response: Logs.guard }),
async (ctx, next) => {
const {
params: { id },
} = ctx.guard;
ctx.body = await findLogById(id);
return next();
}
);
}