Skip to content

Commit

Permalink
删除多余代码
Browse files Browse the repository at this point in the history
  • Loading branch information
LAMMUpro committed Jan 14, 2024
1 parent 6de4b16 commit aad4c62
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Trpc/src/routes/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { procedure, router } from '../trpc';
import { ApplicationZod } from '../types/zod';
import { addApplication, deleteApplicationById, getApplicationList, updateApplicationById } from '../service/application';
import { ApplicationCreateZod, ApplicationUpdateZod } from '../types/zodExt/Application';
import { PageInfoQueryZod, PageInfoZod } from '../types';
import { PageInfoQueryZod, PageInfoResultZod } from '../types';
import { TRPCError } from '@trpc/server';

export const applicationRouter = router({
Expand Down Expand Up @@ -49,7 +49,7 @@ export const applicationRouter = router({
description: "分页获取应用列表"
})
.input(PageInfoQueryZod)
.output(PageInfoZod(ApplicationZod))
.output(PageInfoResultZod(ApplicationZod))
.query(async ({ input }) => {
const list = await getApplicationList();
return {
Expand Down
30 changes: 1 addition & 29 deletions Trpc/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import { z } from "zod";

/**
* 获取响应Zod对象
*/
export function ApiResponseZod<T extends z.ZodArray<z.AnyZodObject> | z.AnyZodObject>(data?: T) {
return z.object({
/** 请求是否成功 */
success: z.literal(false),
/** 状态码, 不同状态码代表不同含义 */
code: z.string(),
/** 错误信息 */
msg: z.string(),
/** 弹窗时间 */
msgTimeout: z.number().optional(),
}).or(z.object({
/** 请求是否成功 */
success: z.literal(true),
/** 数据 */
data: data ? data : z.literal(void 0),
}))
}

/**
* 获取分页查询Zod对象
*/
Expand All @@ -32,18 +11,11 @@ export const PageInfoQueryZod = z.object({
/**
* 获取分页响应Zod对象
*/
export function PageInfoZod<T extends z.AnyZodObject>(item: T) {
export function PageInfoResultZod<T extends z.AnyZodObject>(item: T) {
return z.object({
currentPage: z.number(),
pageSize: z.number(),
totalCount: z.number(),
list: z.array(item),
})
}

/**
* 获取分页响应Zod对象
*/
export function ApiPageResponseZod<T extends z.AnyZodObject>(listItem: T) {
return ApiResponseZod(PageInfoZod(listItem));
}
39 changes: 0 additions & 39 deletions Trpc/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +0,0 @@
import { PageInfoQueryZod } from "src/types"
import { z } from "zod"

/**
* 获取默认响应数据
*/
export function getDefaultResponse<T extends Array<BaseObj<any>> | BaseObj<any>>(data?: T) {
return {
success: true as const,
data,
}
}

/**
* 获取默认失败响应数据
*/
export function getDefaultFailResponse(code: string = 'ERROR', msg: string = '', msgTimeout = 2000) {
return {
success: false as const,
code,
msg,
msgTimeout,
}
}

/**
* 获取默认分页响应数据
*/
export function getDefaultPageResponse<T extends Array<BaseObj<any>>>(pageInfo: z.infer<typeof PageInfoQueryZod>, list: T, totalCount: number): ApiPageResponse<T[number]> {
return {
success: true as const,
data: {
currentPage: pageInfo.currentPage,
pageSize: pageInfo.pageSize,
totalCount,
list,
}
}
}

0 comments on commit aad4c62

Please sign in to comment.