mirror of https://github.com/buster-so/buster.git
Update scheams for get list
This commit is contained in:
parent
ccdd271e07
commit
a4f960ff13
|
@ -1,5 +1,9 @@
|
|||
import type { User } from '@buster/database';
|
||||
import type { GetReportsListRequest, GetReportsListResponse } from '@buster/server-shared/reports';
|
||||
import type {
|
||||
GetReportsListRequest,
|
||||
GetReportsListResponse,
|
||||
ReportResponse,
|
||||
} from '@buster/server-shared/reports';
|
||||
import { GetReportsListRequestSchema } from '@buster/server-shared/reports';
|
||||
import { zValidator } from '@hono/zod-validator';
|
||||
import { Hono } from 'hono';
|
||||
|
@ -8,7 +12,7 @@ async function getReportsListHandler(
|
|||
request: GetReportsListRequest,
|
||||
user: User
|
||||
): Promise<GetReportsListResponse> {
|
||||
const stubbedReports = [
|
||||
const stubbedReports: ReportResponse[] = [
|
||||
{
|
||||
id: 'report-1',
|
||||
name: 'Sales Analysis Q4',
|
||||
|
@ -77,7 +81,7 @@ async function getReportsListHandler(
|
|||
const endIndex = startIndex + page_size;
|
||||
const paginatedReports = stubbedReports.slice(startIndex, endIndex);
|
||||
|
||||
return {
|
||||
const result: GetReportsListResponse = {
|
||||
data: paginatedReports,
|
||||
pagination: {
|
||||
page,
|
||||
|
@ -86,6 +90,8 @@ async function getReportsListHandler(
|
|||
total_pages: Math.ceil(stubbedReports.length / page_size),
|
||||
},
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const app = new Hono().get('/', zValidator('query', GetReportsListRequestSchema), async (c) => {
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
import type { User } from '@buster/database';
|
||||
import type { UpdateReportRequest, UpdateReportResponse } from '@buster/server-shared/reports';
|
||||
import type {
|
||||
ReportResponse,
|
||||
UpdateReportRequest,
|
||||
UpdateReportResponse,
|
||||
} from '@buster/server-shared/reports';
|
||||
import { UpdateReportRequestSchema } from '@buster/server-shared/reports';
|
||||
import { zValidator } from '@hono/zod-validator';
|
||||
import { Hono } from 'hono';
|
||||
|
|
|
@ -2,3 +2,4 @@ export * from './report-elements';
|
|||
export * from './reports.types';
|
||||
export * from './requests';
|
||||
export * from './responses';
|
||||
export * from './reports.types';
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
import { z } from 'zod';
|
||||
import type { ReportElement } from './report-elements';
|
||||
import type { ReportElement, ReportElements } from './report-elements';
|
||||
import { ReportElementSchema } from './report-elements';
|
||||
|
||||
// Define the type explicitly
|
||||
export type ReportResponse = {
|
||||
const ReportResponseSchema: z.ZodType<{
|
||||
id: string;
|
||||
name: string;
|
||||
file_name: string;
|
||||
|
@ -14,10 +13,7 @@ export type ReportResponse = {
|
|||
deleted_at: string | null;
|
||||
publicly_accessible: boolean;
|
||||
content: ReportElement[];
|
||||
};
|
||||
|
||||
// Create schema with explicit type annotation
|
||||
export const ReportResponseSchema = z.object({
|
||||
}> = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
file_name: z.string(),
|
||||
|
@ -27,5 +23,10 @@ export const ReportResponseSchema = z.object({
|
|||
updated_at: z.string(),
|
||||
deleted_at: z.string().nullable(),
|
||||
publicly_accessible: z.boolean(),
|
||||
content: z.lazy(() => z.array(ReportElementSchema)), // Now using the actual schema
|
||||
}) as z.ZodType<ReportResponse>;
|
||||
content: z.array(ReportElementSchema) as z.ZodType<ReportElements>,
|
||||
});
|
||||
|
||||
// Export base schema for operations like .pick()
|
||||
export { ReportResponseSchema };
|
||||
|
||||
export type ReportResponse = z.infer<typeof ReportResponseSchema>;
|
||||
|
|
|
@ -1,17 +1,22 @@
|
|||
import type { z } from 'zod';
|
||||
import { z } from 'zod';
|
||||
import { PaginatedRequestSchema } from '../type-utilities/pagination';
|
||||
import type { ReportElements } from './report-elements';
|
||||
import type { ReportElement, ReportElements } from './report-elements';
|
||||
import { ReportElementSchema } from './report-elements';
|
||||
import { ReportResponseSchema } from './reports.types';
|
||||
|
||||
export const GetReportsListRequestSchema = PaginatedRequestSchema;
|
||||
|
||||
// UpdateReportRequestSchema uses zod's .pick to select updatable fields from ReportResponseSchema
|
||||
export const UpdateReportRequestSchema = ReportResponseSchema.pick({
|
||||
name: true,
|
||||
description: true,
|
||||
publicly_accessible: true,
|
||||
content: true,
|
||||
}).partial();
|
||||
// Define UpdateReportRequestSchema with explicit type annotation
|
||||
export const UpdateReportRequestSchema = z
|
||||
.object({
|
||||
name: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
publicly_accessible: z.boolean().optional(),
|
||||
content: z.lazy(() => z.array(ReportElementSchema)).optional() as z.ZodOptional<
|
||||
z.ZodType<ReportElements>
|
||||
>,
|
||||
})
|
||||
.partial();
|
||||
|
||||
export type GetReportsListRequest = z.infer<typeof GetReportsListRequestSchema>;
|
||||
export type UpdateReportRequest = z.infer<typeof UpdateReportRequestSchema>;
|
||||
export type GetReportsListRequest = z.infer<typeof GetReportsListRequestSchema>;
|
||||
|
|
|
@ -18,6 +18,6 @@ export const PaginatedResponseSchema = <T>(schema: z.ZodType<T>) =>
|
|||
export type PaginatedResponse<T> = z.infer<ReturnType<typeof PaginatedResponseSchema<T>>>;
|
||||
|
||||
export const PaginatedRequestSchema = z.object({
|
||||
page: z.number(),
|
||||
page_size: z.number(),
|
||||
page: z.coerce.number().min(1).default(1),
|
||||
page_size: z.coerce.number().min(1).max(5000).default(250),
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue