fix: refactor search routes (#1529)
Refactor find endpoints to be consistent in terms of input and output.
This commit is contained in:
@@ -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;
|
||||
};
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user