import _ from 'lodash' import * as z from 'zod' import type { FastifyPluginCallbackZod } from 'fastify-type-provider-zod' import { PaginationMetaSchema } from '../../schemas/misc.ts' import { ErrorSchema } from '../../schemas/db.ts' import { paginate, applyWhere } from '../../lib/kysely_helpers.ts' const errorRoutes: FastifyPluginCallbackZod = (fastify, _options, done) => { const { db } = fastify // addParentSchema({ // $id: 'logged-error', // type: 'object', // properties: { // id: { type: 'integer' }, // statusCode: { type: 'integer' }, // type: { type: 'string' }, // message: { type: 'string' }, // details: { type: ['object', 'null'] }, // stack: { type: 'string' }, // method: { type: 'string' }, // path: { type: 'string' }, // headers: { type: ['object', 'null'], additionalProperties: { type: 'string' } }, // ip: { type: 'string' }, // reqId: { type: 'string' }, // createdAt: { type: 'string' }, // }, // }) // fastify.addHook('onRequest', fastify.auth) fastify.route({ method: 'GET', url: '/', schema: { querystring: z.object({ statusCode: z.coerce.number(), sort: ErrorSchema.keyof(), limit: z.coerce.number().default(40), offset: z.coerce.number().default(0), }), response: { 200: z.object({ meta: PaginationMetaSchema, data: z.array(ErrorSchema), }), }, }, // @ts-ignore handler(request) { const { limit, offset, sort, ...where } = request.query const baseQuery = db.selectFrom('error').selectAll() return paginate(baseQuery, baseQuery, { where, limit, offset, sort }) }, }) fastify.route({ method: 'DELETE', url: '/', schema: { querystring: z.object({ statusCode: z.coerce.number().optional(), id: z.union([z.coerce.number(), z.array(z.coerce.number()).optional()]), }), }, handler(request) { return applyWhere(db.deleteFrom('error'), request.query) }, }) fastify.route({ method: 'GET', url: '/:id', schema: { params: z.object({ id: z.coerce.number(), }), }, handler(request) { return db.selectFrom('error').selectAll().where('id', '=', request.params.id).executeTakeFirst() }, }) fastify.route({ method: 'DELETE', url: '/:id', schema: { params: z.object({ id: z.coerce.number(), }), response: { 204: {} }, }, handler(request) { return db.deleteFrom('error').where('id', '=', request.params.id).execute() }, }) done() } export default errorRoutes