import {z} from 'zod' export type GetApiApiV1FinancialReportsGeneralledgerArgs = {fromDate: string, toDate: string, accountCode: string, isShowSubAccount?: string | undefined, 'Time-Stamp': string, 'Time-Signature': string, 'User-Token': string, 'Client-Token': string}; export type GetApiApiV1FinancialReportsGeneralledgerOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerAccountModel = z.object({accountCode: z.string().optional(), accountName: z.string().optional(), subAccountCode: z.string().optional(), subAccountName: z.string().optional(), isSubAccount: z.boolean().optional()}); export const peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPITrialBalanceValueModel = z.object({debit: z.number().optional(), credit: z.number().optional()}); export const peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerSummaryModel = z.object({beginningBalance: peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPITrialBalanceValueModel.optional(), change: peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPITrialBalanceValueModel.optional(), endingBalance: peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPITrialBalanceValueModel.optional()}); export const peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerTransactionModel = z.object({date: z.string().optional(), journalNumber: z.string().optional(), description: z.string().optional(), debit: z.number().optional(), credit: z.number().optional()}); export const peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerItemModel = z.object({account: peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerAccountModel.optional(), summary: peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerSummaryModel.optional(), transactions: z.array(peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerTransactionModel).optional(), totalTransactions: z.number().int().optional()}); export const peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerModel = z.object({resCode: z.string().optional(), resDesc: z.string().optional(), fromDate: z.string().optional(), toDate: z.string().optional(), generalLedgers: z.array(peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerItemModel).optional()}); export const peakAPIsLibraryModelResponseFinancialReportsResponseGeneralLedgerModel = z.object({peakGeneralLedger: peakAPIsLibraryModelEcoSystemResponseFinancialReportsAPIGeneralLedgerModel.optional(), eventType: z.string().optional(), apiType: z.string().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 }; /** * Get General Ledger * * GET /api/v1/FinancialReports/generalledger */ export const getApiApiV1FinancialReportsGeneralledger = async (args: GetApiApiV1FinancialReportsGeneralledgerArgs, options?: GetApiApiV1FinancialReportsGeneralledgerOptions) => { const {baseUrl = "https://api.peakaccount.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.accountCode != null) query.append("accountCode", String(args.accountCode)) if (args.isShowSubAccount != null) query.append("isShowSubAccount", String(args.isShowSubAccount)) if (args['Time-Stamp'] != null) headers.set("Time-Stamp", String(args['Time-Stamp'])) if (args['Time-Signature'] != null) headers.set("Time-Signature", String(args['Time-Signature'])) if (args['User-Token'] != null) headers.set("User-Token", String(args['User-Token'])) if (args['Client-Token'] != null) headers.set("Client-Token", String(args['Client-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/FinancialReports/generalledger${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(peakAPIsLibraryModelResponseFinancialReportsResponseGeneralLedgerModel, await res.json()) };