import {z} from 'zod' export type PageLimit = number; export type PageCursor = string; export type GetApiJournalEntriesArgs = {limit?: PageLimit | undefined, cursor?: PageCursor | undefined, subsidiary_id?: string | undefined, book_id?: string | undefined, 'updated.gt'?: string | undefined, 'date.gte'?: string | undefined, 'date.lte'?: string | undefined, account_ids?: Array | undefined, sort_by?: ('created' | 'updated') | undefined}; export type GetApiJournalEntriesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string(); export const journalEntryRelatedEntity = z.object({id: z.string(), type: z.enum(["CUSTOMER", "VENDOR"])}); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const accountCode = z.string(); export const fieldAssignments = z.array(z.object({field_id: z.string(), field_value_id: z.string()})); export const journalEntryItem = z.object({id: z.string(), description: z.string().optional(), amount: roundedMonetaryAmount, original_transaction_amount: roundedMonetaryAmount.nullable().optional(), account_id: z.string(), account_code: accountCode, side: z.enum(["DEBIT", "CREDIT"]), fields: fieldAssignments.optional()}); export const exchangeRate = z.object({base: currencyCode, target: currencyCode, rate: z.string(), date: z.string()}); export const journalEntry = z.object({id: z.string(), book_id: z.string(), subsidiary_id: z.string().optional(), name: z.string(), currency: currencyCode, related_entity: journalEntryRelatedEntity.optional(), items: z.array(journalEntryItem), date: z.string(), reversal_date: z.string().optional(), attachmentUrl: z.string().optional(), exchange_rate: exchangeRate.optional(), updated_at: z.string()}); export const pageCursor = z.string(); export const pagination = z.object({next_cursor: pageCursor.optional()}); export const getApiJournalEntriesResponse = z.object({journal_entries: z.array(journalEntry), pagination: pagination.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 }; /** * Lists all journal entries * * GET /journal-entries */ export const getApiJournalEntries = async (args: GetApiJournalEntriesArgs, options?: GetApiJournalEntriesOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = 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.cursor != null) query.append("cursor", String(args.cursor)) if (args.subsidiary_id != null) query.append("subsidiary_id", String(args.subsidiary_id)) if (args.book_id != null) query.append("book_id", String(args.book_id)) if (args['updated.gt'] != null) query.append("updated.gt", String(args['updated.gt'])) if (args['date.gte'] != null) query.append("date.gte", String(args['date.gte'])) if (args['date.lte'] != null) query.append("date.lte", String(args['date.lte'])) if (args.account_ids != null) query.append("account_ids", args.account_ids.join(',')) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/journal-entries${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(getApiJournalEntriesResponse, await res.json()) };