fix: refactor search routes (#1529)

Refactor find endpoints to be consistent in terms of input and output.
This commit is contained in:
David Nguyen
2024-12-11 19:39:50 +09:00
committed by GitHub
parent 3d7b28a92b
commit 5df1a6602e
35 changed files with 171 additions and 184 deletions

View File

@@ -1,18 +0,0 @@
import { z } from 'zod';
export const ZFindResultSet = z.object({
data: z.union([z.array(z.unknown()), z.unknown()]),
count: z.number(),
currentPage: z.number(),
perPage: z.number(),
totalPages: z.number(),
});
// Can't infer generics from Zod.
export type FindResultSet<T> = {
data: T extends Array<unknown> ? T : T[];
count: number;
currentPage: number;
perPage: number;
totalPages: number;
};

View File

@@ -1,6 +1,24 @@
import { z } from 'zod';
export const ZBaseTableSearchParamsSchema = z.object({
/**
* Backend only schema is used for find search params.
*
* Does not catch, because TRPC Open API won't allow catches as a type.
*
* Keep this and `ZUrlSearchParamsSchema` in sync.
*/
export const ZFindSearchParamsSchema = z.object({
query: z.string().optional(),
page: z.coerce.number().min(1).optional(),
perPage: z.coerce.number().min(1).optional(),
});
/**
* Frontend schema used to parse search params from URL.
*
* Keep this and `ZFindSearchParamsSchema` in sync.
*/
export const ZUrlSearchParamsSchema = z.object({
query: z
.string()
.optional()
@@ -17,4 +35,19 @@ export const ZBaseTableSearchParamsSchema = z.object({
.catch(() => undefined),
});
export type TBaseTableSearchParamsSchema = z.infer<typeof ZBaseTableSearchParamsSchema>;
export const ZFindResultResponse = z.object({
data: z.union([z.array(z.unknown()), z.unknown()]),
count: z.number(),
currentPage: z.number(),
perPage: z.number(),
totalPages: z.number(),
});
// Can't infer generics from Zod.
export type FindResultResponse<T> = {
data: T extends Array<unknown> ? T : T[];
count: number;
currentPage: number;
perPage: number;
totalPages: number;
};