|
@@ -0,0 +1,338 @@
|
|
|
|
|
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
|
|
|
|
|
+import { z } from '@hono/zod-openapi';
|
|
|
|
|
+import { GenericCrudService, CrudOptions } from './generic-crud.service';
|
|
|
|
|
+import { ErrorSchema } from './errorHandler';
|
|
|
|
|
+import { AuthContext } from '../types/context';
|
|
|
|
|
+import { ObjectLiteral } from 'typeorm';
|
|
|
|
|
+import { AppDataSource } from '../data-source';
|
|
|
|
|
+
|
|
|
|
|
+export function createCrudRoutes<
|
|
|
|
|
+ T extends ObjectLiteral,
|
|
|
|
|
+ CreateSchema extends z.ZodSchema = z.ZodSchema,
|
|
|
|
|
+ UpdateSchema extends z.ZodSchema = z.ZodSchema,
|
|
|
|
|
+ GetSchema extends z.ZodSchema = z.ZodSchema,
|
|
|
|
|
+ ListSchema extends z.ZodSchema = z.ZodSchema
|
|
|
|
|
+>(options: CrudOptions<T, CreateSchema, UpdateSchema, GetSchema, ListSchema>) {
|
|
|
|
|
+ const { entity, createSchema, updateSchema, getSchema, listSchema, searchFields, middleware = [] } = options;
|
|
|
|
|
+
|
|
|
|
|
+ // 创建CRUD服务实例
|
|
|
|
|
+ // 抽象类不能直接实例化,需要创建具体实现类
|
|
|
|
|
+ class ConcreteCrudService extends GenericCrudService<T> {
|
|
|
|
|
+ constructor() {
|
|
|
|
|
+ super(AppDataSource, entity);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ const crudService = new ConcreteCrudService();
|
|
|
|
|
+
|
|
|
|
|
+ // 创建路由实例
|
|
|
|
|
+ const app = new OpenAPIHono<AuthContext>();
|
|
|
|
|
+
|
|
|
|
|
+ // 分页查询路由
|
|
|
|
|
+ const listRoute = createRoute({
|
|
|
|
|
+ method: 'get',
|
|
|
|
|
+ path: '/',
|
|
|
|
|
+ middleware,
|
|
|
|
|
+ request: {
|
|
|
|
|
+ query: z.object({
|
|
|
|
|
+ page: z.coerce.number().int().positive().default(1).openapi({
|
|
|
|
|
+ example: 1,
|
|
|
|
|
+ description: '页码,从1开始'
|
|
|
|
|
+ }),
|
|
|
|
|
+ pageSize: z.coerce.number().int().positive().default(10).openapi({
|
|
|
|
|
+ example: 10,
|
|
|
|
|
+ description: '每页数量'
|
|
|
|
|
+ }),
|
|
|
|
|
+ keyword: z.string().optional().openapi({
|
|
|
|
|
+ example: '搜索关键词',
|
|
|
|
|
+ description: '搜索关键词'
|
|
|
|
|
+ }),
|
|
|
|
|
+ sortBy: z.string().optional().openapi({
|
|
|
|
|
+ example: 'createdAt',
|
|
|
|
|
+ description: '排序字段'
|
|
|
|
|
+ }),
|
|
|
|
|
+ sortOrder: z.enum(['ASC', 'DESC']).optional().default('DESC').openapi({
|
|
|
|
|
+ example: 'DESC',
|
|
|
|
|
+ description: '排序方向'
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ responses: {
|
|
|
|
|
+ 200: {
|
|
|
|
|
+ description: '成功获取列表',
|
|
|
|
|
+ content: {
|
|
|
|
|
+ 'application/json': {
|
|
|
|
|
+ schema: z.object({
|
|
|
|
|
+ data: z.array(listSchema),
|
|
|
|
|
+ pagination: z.object({
|
|
|
|
|
+ total: z.number().openapi({ example: 100, description: '总记录数' }),
|
|
|
|
|
+ current: z.number().openapi({ example: 1, description: '当前页码' }),
|
|
|
|
|
+ pageSize: z.number().openapi({ example: 10, description: '每页数量' })
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ 400: {
|
|
|
|
|
+ description: '参数错误',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 500: {
|
|
|
|
|
+ description: '服务器错误',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 创建资源路由
|
|
|
|
|
+ const createRouteDef = createRoute({
|
|
|
|
|
+ method: 'post',
|
|
|
|
|
+ path: '/',
|
|
|
|
|
+ middleware,
|
|
|
|
|
+ request: {
|
|
|
|
|
+ body: {
|
|
|
|
|
+ content: {
|
|
|
|
|
+ 'application/json': { schema: createSchema }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ responses: {
|
|
|
|
|
+ 201: {
|
|
|
|
|
+ description: '创建成功',
|
|
|
|
|
+ content: { 'application/json': { schema: getSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 400: {
|
|
|
|
|
+ description: '输入数据无效',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 500: {
|
|
|
|
|
+ description: '服务器错误',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 获取单个资源路由
|
|
|
|
|
+ const getRouteDef = createRoute({
|
|
|
|
|
+ method: 'get',
|
|
|
|
|
+ path: '/{id}',
|
|
|
|
|
+ middleware,
|
|
|
|
|
+ request: {
|
|
|
|
|
+ params: z.object({
|
|
|
|
|
+ id: z.coerce.number().openapi({
|
|
|
|
|
+ param: { name: 'id', in: 'path' },
|
|
|
|
|
+ example: 1,
|
|
|
|
|
+ description: '资源ID'
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ responses: {
|
|
|
|
|
+ 200: {
|
|
|
|
|
+ description: '成功获取详情',
|
|
|
|
|
+ content: { 'application/json': { schema: getSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 400: {
|
|
|
|
|
+ description: '资源不存在',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 404: {
|
|
|
|
|
+ description: '参数验证失败',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 500: {
|
|
|
|
|
+ description: '服务器错误',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 更新资源路由
|
|
|
|
|
+ const updateRouteDef = createRoute({
|
|
|
|
|
+ method: 'put',
|
|
|
|
|
+ path: '/{id}',
|
|
|
|
|
+ middleware,
|
|
|
|
|
+ request: {
|
|
|
|
|
+ params: z.object({
|
|
|
|
|
+ id: z.coerce.number().openapi({
|
|
|
|
|
+ param: { name: 'id', in: 'path' },
|
|
|
|
|
+ example: 1,
|
|
|
|
|
+ description: '资源ID'
|
|
|
|
|
+ })
|
|
|
|
|
+ }),
|
|
|
|
|
+ body: {
|
|
|
|
|
+ content: {
|
|
|
|
|
+ 'application/json': { schema: updateSchema }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ responses: {
|
|
|
|
|
+ 200: {
|
|
|
|
|
+ description: '更新成功',
|
|
|
|
|
+ content: { 'application/json': { schema: getSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 400: {
|
|
|
|
|
+ description: '无效输入',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 404: {
|
|
|
|
|
+ description: '资源不存在',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 500: {
|
|
|
|
|
+ description: '服务器错误',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 删除资源路由
|
|
|
|
|
+ const deleteRouteDef = createRoute({
|
|
|
|
|
+ method: 'delete',
|
|
|
|
|
+ path: '/{id}',
|
|
|
|
|
+ middleware,
|
|
|
|
|
+ request: {
|
|
|
|
|
+ params: z.object({
|
|
|
|
|
+ id: z.coerce.number().openapi({
|
|
|
|
|
+ param: { name: 'id', in: 'path' },
|
|
|
|
|
+ example: 1,
|
|
|
|
|
+ description: '资源ID'
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ responses: {
|
|
|
|
|
+ 204: { description: '删除成功' },
|
|
|
|
|
+ 404: {
|
|
|
|
|
+ description: '资源不存在',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 500: {
|
|
|
|
|
+ description: '服务器错误',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 注册路由处理函数
|
|
|
|
|
+ const routes = app
|
|
|
|
|
+ .openapi(listRoute, async (c) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const { page, pageSize, keyword, sortBy, sortOrder } = c.req.valid('query');
|
|
|
|
|
+
|
|
|
|
|
+ // 构建排序对象
|
|
|
|
|
+ // 使用Record和类型断言解决泛型索引写入问题
|
|
|
|
|
+ const order: Partial<Record<keyof T, 'ASC' | 'DESC'>> = {};
|
|
|
|
|
+ if (sortBy) {
|
|
|
|
|
+ (order as Record<string, 'ASC' | 'DESC'>)[sortBy] = sortOrder || 'DESC';
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 默认按id降序排序
|
|
|
|
|
+ (order as Record<string, 'ASC' | 'DESC'>)['id'] = 'DESC';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const [data, total] = await crudService.getList(
|
|
|
|
|
+ page,
|
|
|
|
|
+ pageSize,
|
|
|
|
|
+ keyword,
|
|
|
|
|
+ searchFields,
|
|
|
|
|
+ undefined, // where条件
|
|
|
|
|
+ [], // relations
|
|
|
|
|
+ order
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ data: data as any[],
|
|
|
|
|
+ pagination: { total, current: page, pageSize }
|
|
|
|
|
+ }, 200);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ if (error instanceof z.ZodError) {
|
|
|
|
|
+ return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 500,
|
|
|
|
|
+ message: error instanceof Error ? error.message : '获取列表失败'
|
|
|
|
|
+ }, 500);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .openapi(createRouteDef, async (c) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const data = c.req.valid('json');
|
|
|
|
|
+ const result = await crudService.create(data);
|
|
|
|
|
+ return c.json(result, 201);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ if (error instanceof z.ZodError) {
|
|
|
|
|
+ return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 500,
|
|
|
|
|
+ message: error instanceof Error ? error.message : '创建资源失败'
|
|
|
|
|
+ }, 500);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .openapi(getRouteDef, async (c) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const { id } = c.req.valid('param');
|
|
|
|
|
+ const result = await crudService.getById(id);
|
|
|
|
|
+
|
|
|
|
|
+ if (!result) {
|
|
|
|
|
+ return c.json({ code: 404, message: '资源不存在' }, 404);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return c.json(result, 200);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ if (error instanceof z.ZodError) {
|
|
|
|
|
+ return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 500,
|
|
|
|
|
+ message: error instanceof Error ? error.message : '获取资源失败'
|
|
|
|
|
+ }, 500);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .openapi(updateRouteDef, async (c) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const { id } = c.req.valid('param');
|
|
|
|
|
+ const data = c.req.valid('json');
|
|
|
|
|
+ const result = await crudService.update(id, data);
|
|
|
|
|
+
|
|
|
|
|
+ if (!result) {
|
|
|
|
|
+ return c.json({ code: 404, message: '资源不存在' }, 404);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return c.json(result, 200);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ if (error instanceof z.ZodError) {
|
|
|
|
|
+ return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (error instanceof z.ZodError) {
|
|
|
|
|
+ return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 500,
|
|
|
|
|
+ message: error instanceof Error ? error.message : '更新资源失败'
|
|
|
|
|
+ }, 500);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .openapi(deleteRouteDef, async (c) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const { id } = c.req.valid('param');
|
|
|
|
|
+ const success = await crudService.delete(id);
|
|
|
|
|
+
|
|
|
|
|
+ if (!success) {
|
|
|
|
|
+ return c.json({ code: 404, message: '资源不存在' }, 404);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return c.body(null, 204) as unknown as Response;
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ if (error instanceof z.ZodError) {
|
|
|
|
|
+ return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (error instanceof z.ZodError) {
|
|
|
|
|
+ return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 500,
|
|
|
|
|
+ message: error instanceof Error ? error.message : '删除资源失败'
|
|
|
|
|
+ }, 500);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return routes;
|
|
|
|
|
+}
|