import {z} from 'zod' export type GetApiCaApiGetCashFlowArgs = {cadence?: ('daily' | 'monthly' | 'quarterly' | 'weekly' | 'yearly') | undefined, end_date?: string | undefined, entity?: number | undefined, entity_rollup?: boolean | undefined, group_by?: string | undefined, start_date?: string | undefined}; export type GetApiCaApiGetCashFlowOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCaApiGetCashFlowResponse = z.object({cash_flow: z.array(z.object({account_id: z.number().int().optional(), account_name: z.string().optional(), account_number: z.string().optional(), hierarchy: z.array(z.string()).optional(), transaction_activity: z.object({"YYYY-MM-DD": z.number().optional()}).optional()})).optional(), start_date: z.string().optional(), end_date: z.string().optional(), cadence: 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 Cash Flow * * GET /ca/api/get_cash_flow */ export const getApiCaApiGetCashFlow = async (args: GetApiCaApiGetCashFlowArgs, options?: GetApiCaApiGetCashFlowOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cadence != null) query.append("cadence", String(args.cadence)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.entity != null) query.append("entity", String(args.entity)) if (args.entity_rollup != null) query.append("entity_rollup", String(args.entity_rollup)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/ca/api/get_cash_flow${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(getApiCaApiGetCashFlowResponse, await res.json()) };