import {z} from 'zod' export type GetApiJournalEntriesJournalEntryIdArgs = {journal_entry_id: string}; export type GetApiJournalEntriesJournalEntryIdOptions = {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 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 }; /** * Retrieves a journal entry * * GET /journal-entries/{journal_entry_id} */ export const getApiJournalEntriesJournalEntryId = async (args: GetApiJournalEntriesJournalEntryIdArgs, options?: GetApiJournalEntriesJournalEntryIdOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/journal-entries/${encodeURIComponent(String(args.journal_entry_id))}${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(journalEntry, await res.json()) };