import {z} from 'zod' export type ExternalId = string; export type GetApiReceivablesIdDocumentsArgs = {id: ExternalId, limit?: number | undefined, page?: string | undefined, expand?: Array<'total_count'> | undefined, sort?: 'created_at_desc' | undefined, type?: ('invoice' | 'contract' | 'proof_of_delivery' | 'other') | undefined, status?: ('pending_upload' | 'uploaded' | 'quarantined') | undefined}; export type GetApiReceivablesIdDocumentsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiReceivablesIdDocumentsResponse = z.object({data: z.array(z.object({file_name: z.string().min(1).max(255), content_type: z.enum(["application/pdf", "image/png", "image/jpeg", "image/webp", "application/xml", "text/xml"]), size_bytes: z.number().int().gt(0).lte(26214400), type: z.enum(["invoice", "contract", "proof_of_delivery", "other"]).optional(), metadata: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]).nullable()).optional(), id: z.string().min(1).max(255), created_at: z.string(), created_by: z.string(), updated_at: z.string(), updated_by: z.string(), status: z.enum(["pending_upload", "uploaded", "quarantined"]), etag: z.string().min(1).optional()})), has_more: z.boolean(), next_page: z.string().min(1).max(1024).nullable(), previous_page: z.string().min(1).max(1024).nullable(), total_count: z.number().int().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 }; /** * List all documents * * GET /receivables/{id}/documents * * Also accepts auth: OAuth2 */ export const getApiReceivablesIdDocuments = async (args: GetApiReceivablesIdDocumentsArgs, options?: GetApiReceivablesIdDocumentsOptions) => { const {baseUrl = "https://core.sivo.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) for (const item of args.expand ?? []) query.append("expand", String(item)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/receivables/${encodeURIComponent(String(args.id))}/documents${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiReceivablesIdDocumentsResponse, await res.json()) };