import {z} from 'zod' export type DocumentType = 'account_statement' | 'cash.statement' | 'contractual' | 'general' | 'income_distribution.receipt' | 'order.cost' | 'report.ex_post' | 'tax_information' | 'tax.receipt' | 'tax.statement' | 'trade.receipt'; export type GetApiAccountsAccountIdDocumentsArgs = {account_id: string, cursor?: string | undefined, limit?: number | undefined, type?: DocumentType | undefined, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string}; export type GetApiAccountsAccountIdDocumentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const documentType = z.enum(["account_statement", "cash.statement", "contractual", "general", "income_distribution.receipt", "order.cost", "report.ex_post", "tax_information", "tax.receipt", "tax.statement", "trade.receipt"]); export const documentContentType = z.literal("application/pdf"); export const documentResponse = z.object({id: z.string(), created_at: z.string(), name: z.string(), url: z.string().min(1).max(2083), language: z.string(), type: documentType, content_type: documentContentType}); export const pagination = z.object({next_cursor: z.string().optional()}); export const pageDocumentResponse = z.object({data: z.array(documentResponse), pagination: pagination}); 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 Documents * * GET /accounts/{account_id}/documents */ export const getApiAccountsAccountIdDocuments = async (args: GetApiAccountsAccountIdDocumentsArgs, options?: GetApiAccountsAccountIdDocumentsOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.type != null) query.append("type", String(args.type)) if (args['LMG-Data-Privacy-Access-Principal'] != null) headers.set("LMG-Data-Privacy-Access-Principal", String(args['LMG-Data-Privacy-Access-Principal'])) if (args['LMG-Data-Privacy-Access-Justification'] != null) headers.set("LMG-Data-Privacy-Access-Justification", String(args['LMG-Data-Privacy-Access-Justification'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_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(pageDocumentResponse, await res.json()) };