import {z} from 'zod' export type AttachmentBase64 = {files?: (Array<{fileName?: string | undefined, base64Data?: string | undefined}>) | undefined}; export type CreateApiTaxInvoicesIdAttachmentBase64Args = {Authorization: string, id: string, body: AttachmentBase64}; export type CreateApiTaxInvoicesIdAttachmentBase64Options = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const attachmentBase64Response = z.object({status: z.boolean().optional(), message: z.string().optional(), code: z.number().int().optional(), data: z.array(z.object({attachmentId: z.number().int().optional(), attachmentName: z.string().optional(), createdOn: z.string().optional(), orderNumber: z.number().int().optional(), isDisplay: z.boolean().optional(), attachmentPath: z.string().optional()})).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 }; /** * Base64 attachment tax Invoices document. * * POST /tax-invoices/{id}/attachment/base64 */ export const createApiTaxInvoicesIdAttachmentBase64 = async (args: CreateApiTaxInvoicesIdAttachmentBase64Args, options?: CreateApiTaxInvoicesIdAttachmentBase64Options) => { const {baseUrl = "https://openapi.flowaccount.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/tax-invoices/${encodeURIComponent(String(args.id))}/attachment/base64${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(attachmentBase64Response, await res.json()) };