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