import {z} from 'zod' export type AccountCodeDto = {id?: unknown | undefined, accountingSoftwareId?: unknown | undefined, name: string, value: string}; export type ItemCodeDto = {id?: unknown | undefined, accountingSoftwareId?: unknown | undefined, value: string}; export type AccountingSoftwareProductDto = {id?: unknown | undefined, taxName: string, accountCode?: AccountCodeDto | undefined, itemCode?: ItemCodeDto | undefined}; export type ProductDto = {id?: unknown | undefined, price?: number | undefined, accountingSoftwareProduct?: AccountingSoftwareProductDto | undefined}; export type PlanDto = {id?: unknown | undefined, type?: number | undefined, product?: ProductDto | undefined, chapter: string, unitsMin?: (number | null) | undefined, unitsMax?: (number | null) | undefined}; export type UpdatePlansRequest = {memberships: Array}; export type UpdateApiApiPlansArgs = {body: UpdatePlansRequest}; export type UpdateApiApiPlansOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const accountCodeDto = z.object({id: z.string().optional(), accountingSoftwareId: z.string().optional(), name: z.string().min(1), value: z.string().min(1)}); export const itemCodeDto = z.object({id: z.string().optional(), accountingSoftwareId: z.string().optional(), value: z.string().min(1)}); export const accountingSoftwareProductDto = z.object({id: z.string().optional(), taxName: z.string().min(1), accountCode: accountCodeDto.optional(), itemCode: itemCodeDto.optional()}); export const productDto = z.object({id: z.string().optional(), price: z.number().optional(), accountingSoftwareProduct: accountingSoftwareProductDto.optional()}); export const planDto = z.object({id: z.string().optional(), type: z.number().int().optional(), product: productDto.optional(), chapter: z.string().min(1), unitsMin: z.number().int().nullable().optional(), unitsMax: z.number().int().nullable().optional()}); export const updateApiApiPlansResponse = z.array(planDto); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * PUT /api/plans */ export const updateApiApiPlans = async (args: UpdateApiApiPlansArgs, options?: UpdateApiApiPlansOptions) => { const {baseUrl = "https://api.rgcq.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/plans${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiApiPlansResponse, await res.json()) };