import {z} from 'zod' export type GetApiApiV1StatementsPeriodsArgs = {bank_account_id: string, limit?: number | undefined, cursor?: string | undefined}; export type GetApiApiV1StatementsPeriodsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiSharedEnumffb4886f2b = z.enum(["ARS", "USD"]); export const apiSharedEnum714ff181d6 = z.enum(["exact", "derived", "unavailable"]); export const apiSharedEnumfb34785183 = z.enum(["payment", "journal_entry"]); export const apiSharedObject7687b554ab = z.object({target_type: apiSharedEnumfb34785183, target_id: z.string(), occurred_at: z.string(), amount: z.number().int(), reference: z.string().nullable(), description: z.string().nullable()}); export const getApiApiV1StatementsPeriodsResponse = z.object({request_id: z.string(), data: z.array(z.object({object: z.literal("statement_period"), bank_account_id: z.string(), statement_id: z.string().nullable(), period_start: z.string(), period_end: z.string(), currency: apiSharedEnumffb4886f2b, opening_balance: z.number().int().nullable(), closing_balance: z.number().int().nullable(), book_balance: z.number().int().nullable(), difference: z.number().int().nullable(), book_balance_quality: apiSharedEnum714ff181d6, pending_line_count: z.number().int().gte(0), line_count: z.number().int().gte(0), status: z.enum(["open", "closed", "without_statement"]), outstanding_records: z.array(apiSharedObject7687b554ab)})), object: z.literal("list"), url: z.string(), has_more: z.boolean(), next_cursor: z.string().nullable()}); 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 statement periods * * GET /api/v1/statements/periods */ export const getApiApiV1StatementsPeriods = async (args: GetApiApiV1StatementsPeriodsArgs, options?: GetApiApiV1StatementsPeriodsOptions) => { const {baseUrl = "https://api.lapyme.com.ar", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.bank_account_id != null) query.append("bank_account_id", String(args.bank_account_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/statements/periods${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(getApiApiV1StatementsPeriodsResponse, await res.json()) };