import {z} from 'zod' export type RetentionLine = {id?: number | undefined, name: string, description?: (string | null) | undefined, amount: number, quantity: number, exempt?: boolean | undefined, tax?: (number | null) | undefined, tax_percent?: (number | null) | undefined, total?: number | undefined, discount?: (number | null) | undefined, discount_scheme?: ('%' | '$' | null) | undefined}; export type Retention = {id?: number | undefined, supplier: string, document: number, number: number, date: string, amount?: (number | null) | undefined, exempt?: (number | null) | undefined, tax?: (number | null) | undefined, tax_percent?: (number | null) | undefined, subtotal?: (number | null) | undefined, total?: (number | null) | undefined, discount?: (number | null) | undefined, discount_scheme?: ('%' | '$' | null) | undefined, status?: ('draft' | 'finalized' | 'cancelled') | undefined, biller_status?: string | undefined, tax_agency?: Record | undefined, local_file?: (string | null) | undefined, imported?: boolean | undefined, test_mode?: boolean | undefined, lines: Array, created?: string | undefined, updated?: string | undefined}; export type UpdateApiRetentionsIdArgs = {body: Retention}; export type UpdateApiRetentionsIdOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const retentionLine = z.object({id: z.number().int().optional(), name: z.string().max(80), description: z.string().max(200).nullable().optional(), amount: z.number(), quantity: z.number(), exempt: z.boolean().optional(), tax: z.number().nullable().optional(), tax_percent: z.number().nullable().optional(), total: z.number().optional(), discount: z.number().nullable().optional(), discount_scheme: z.enum(["%", "$"]).nullable().optional()}); export const retention = z.object({id: z.number().int().optional(), supplier: z.string(), document: z.number().int(), number: z.number().int(), date: z.string(), amount: z.number().nullable().optional(), exempt: z.number().nullable().optional(), tax: z.number().nullable().optional(), tax_percent: z.number().nullable().optional(), subtotal: z.number().nullable().optional(), total: z.number().nullable().optional(), discount: z.number().nullable().optional(), discount_scheme: z.enum(["%", "$"]).nullable().optional(), status: z.enum(["draft", "finalized", "cancelled"]).optional(), biller_status: z.string().optional(), tax_agency: z.object({}).optional(), local_file: z.string().nullable().optional(), imported: z.boolean().optional(), test_mode: z.boolean().optional(), lines: z.array(retentionLine), created: z.string().optional(), updated: z.string().optional()}); 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 }; /** * Replace retention (draft only) * * PUT /retentions/{id}/ */ export const updateApiRetentionsId = async (args: UpdateApiRetentionsIdArgs, options?: UpdateApiRetentionsIdOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/retentions/${encodeURIComponent(String(args.id))}/${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(retention, await res.json()) };