cursor-init/src/modules/permission/permission.response.ts

198 lines
6.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file 权限模块响应格式定义
* @author AI Assistant
* @date 2024-12-19
* @lastEditor AI Assistant
* @lastEditTime 2025-01-07
* @description 定义权限模块的响应格式,包括新增权限等
*/
import { t, type Static } from 'elysia';
import { responseWrapperSchema } from '@/utils/responseFormate';
/**
* 新增权限成功响应数据结构
*/
export const CreatePermissionSuccessSchema = t.Object({
id: t.String({
description: '权限IDbigint类型以字符串返回防止精度丢失',
examples: ['1', '2', '100'],
}),
permissionKey: t.String({
description: '权限标识',
examples: ['user:create', 'user:read', 'user:update'],
}),
name: t.String({
description: '权限名称',
examples: ['创建用户', '查看用户', '修改用户'],
}),
description: t.Optional(
t.String({
description: '权限描述',
examples: ['创建新用户的权限', '查看用户信息的权限'],
}),
),
type: t.Number({
description: '权限类型1=菜单2=按钮3=接口4=数据',
examples: [1, 2, 3, 4],
}),
apiPathKey: t.Optional(
t.String({
description: '接口路径标识',
examples: ['/api/user', '/api/user/create', 'POST:/api/user'],
}),
),
pagePathKey: t.Optional(
t.String({
description: '前端路由标识',
examples: ['/user', '/user/list', '/user/detail'],
}),
),
module: t.String({
description: '所属模块',
examples: ['user', 'role', 'permission', 'system'],
}),
pid: t.String({
description: '父权限IDbigint类型以字符串返回',
examples: ['0', '1', '2'],
}),
level: t.Number({
description: '权限层级',
examples: [1, 2, 3],
}),
sort: t.String({
description: '排序值,同级内排序使用',
examples: ['0','1', '10', '100'],
}),
icon: t.Optional(
t.String({
description: '图标标识',
examples: ['icon-user', 'icon-add', '/icons/user.png'],
}),
),
status: t.Number({
description: '权限状态1=启用0=禁用',
examples: [1, 0],
}),
createdAt: t.String({
description: '创建时间',
examples: ['2024-12-19T10:30:00Z'],
}),
updatedAt: t.String({
description: '更新时间',
examples: ['2024-12-19T10:30:00Z'],
}),
});
/**
* 新增权限接口响应组合
* @description 用于Controller中定义所有可能的响应格式
*/
export const CreatePermissionResponsesSchema = {
200: responseWrapperSchema(CreatePermissionSuccessSchema),
409: responseWrapperSchema(
t.Object({
error: t.String({
description: '唯一性冲突',
examples: ['权限标识已存在', '权限名称在同级下已存在'],
}),
}),
),
400: responseWrapperSchema(
t.Object({
error: t.String({
description: '参数错误',
examples: ['参数校验失败', '父级权限状态非启用', '权限层级超过限制', '权限类型无效', '模块格式错误', '父权限模块不一致'],
}),
}),
),
404: responseWrapperSchema(
t.Object({
error: t.String({
description: '资源不存在',
examples: ['父级权限不存在'],
}),
}),
),
403: responseWrapperSchema(
t.Object({
error: t.String({
description: '权限不足',
examples: ['权限不足', '需要管理员权限'],
}),
}),
),
500: responseWrapperSchema(
t.Object({
error: t.String({
description: '服务器错误',
examples: ['内部服务器错误'],
}),
}),
),
};
/** 新增权限成功响应数据类型 */
export type CreatePermissionSuccessType = Static<(typeof CreatePermissionResponsesSchema)[200]>;
/**
* 删除权限接口响应组合
* @description 用于Controller中定义所有可能的响应格式
*/
export const DeletePermissionResponsesSchema = {
200: responseWrapperSchema(t.Object({})),
400: responseWrapperSchema(
t.Object({
error: t.String({
description: '请求错误',
examples: ['存在子权限,无法删除', '权限状态非启用', '被引用不可删除'],
}),
}),
),
404: responseWrapperSchema(
t.Object({
error: t.String({
description: '资源不存在',
examples: ['权限不存在'],
}),
}),
),
};
/** 删除权限成功响应类型 */
export type DeletePermissionSuccessType = Static<(typeof DeletePermissionResponsesSchema)[200]>;
/**
* 修改权限接口响应组合
* @description 用于Controller中定义所有可能的响应格式
*/
export const UpdatePermissionResponsesSchema = {
200: responseWrapperSchema(CreatePermissionSuccessSchema),
400: responseWrapperSchema(
t.Object({
error: t.String({
description: '请求错误',
examples: ['权限状态非启用', '父权限module不一致', '非法移动'],
}),
}),
),
404: responseWrapperSchema(
t.Object({
error: t.String({
description: '资源不存在',
examples: ['权限不存在'],
}),
}),
),
409: responseWrapperSchema(
t.Object({
error: t.String({
description: '唯一性冲突',
examples: ['同级下已存在该名称'],
}),
}),
),
};
/** 修改权限成功响应类型 */
export type UpdatePermissionSuccessType = Static<(typeof UpdatePermissionResponsesSchema)[200]>;