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 00ee225 commit df30313
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
35 changes: 22 additions & 13 deletions src/routes/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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 { getDefaultFailResponse, getDefaultPageResponse, getDefaultResponse } from '../utils';
import { ApiPageResponseZod, ApiResponseZod, PageInfoQueryZod } from '../types';
import { PageInfoQueryZod, PageInfoZod } from '../types';
import { TRPCError } from '@trpc/server';

export const applicationRouter = router({
/** 新增应用 */
Expand All @@ -15,8 +15,10 @@ export const applicationRouter = router({
.input(ApplicationCreateZod)
.mutation(async ({ input }) => {
const res = await addApplication(input);
if (res?.id) return getDefaultResponse();
return getDefaultFailResponse();
if (!res?.id) throw new TRPCError({
code: 'CONFLICT',
message: '错误提示',
})
}),
/** 根据id删除应用 */
deleteRecodeById: procedure
Expand All @@ -26,40 +28,47 @@ export const applicationRouter = router({
.input(ApplicationZod.pick({ id: true }))
.mutation(async ({ input }) => {
const res = await deleteApplicationById(input.id);
if (res.id === input.id) return getDefaultResponse();
return getDefaultFailResponse();
if (res.id !== input.id) throw new TRPCError({
code: 'CONFLICT',
message: '错误提示',
})
}),
/** 获取全部应用列表 */
getList: procedure
.meta({
description: "获取全部应用列表"
})
.output(ApiResponseZod(z.array(ApplicationZod)))
.output(z.array(ApplicationZod))
.query(async () => {
const list = await getApplicationList();
return getDefaultResponse(list);
return list;
}),
/** 分页获取应用列表 */
getListByPage: procedure
.meta({
description: "分页获取应用列表"
})
.input(PageInfoQueryZod)
.output(ApiPageResponseZod(ApplicationZod))
.output(PageInfoZod(ApplicationZod))
.query(async ({ input }) => {
const list = await getApplicationList();
return getDefaultPageResponse(input, list, 1000);
return {
...input,
list,
totalCount: 1000
};
}),
/** 更新应用信息 */
updateRecord: procedure
.meta({
description: '更新应用信息'
})
.input(ApplicationUpdateZod)
.output(ApiResponseZod())
.mutation(async ({ input }) => {
const res = await updateApplicationById(input);
if (res.id == input.id) return getDefaultResponse();
return getDefaultFailResponse();
if (res.id !== input.id) throw new TRPCError({
code: 'CONFLICT',
message: '错误提示',
})
}),
});
2 changes: 1 addition & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const appRouter = router({
/** 应用 */
application: applicationRouter,
/** 应用环境 */
appEnv: appEnvRouter,
// appEnv: appEnvRouter,
})

export type AppRouter = typeof appRouter;

0 comments on commit df30313

Please sign in to comment.