import {z} from 'zod' export type GetApiCaApiGetCashFlowActivityArgs = {cadence?: ('daily' | 'monthly' | 'quarterly' | 'weekly' | 'yearly') | undefined, end_date?: string | undefined, entity?: number | undefined, entity_rollup?: boolean | undefined, start_date?: string | undefined}; export type GetApiCaApiGetCashFlowActivityOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCaApiGetCashFlowActivityResponse = z.object({transaction_activity: z.record(z.string(), z.object({total_debit: z.number().optional(), total_credit: z.number().optional()})).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 Activity * * GET /ca/api/get_cash_flow_activity */ export const getApiCaApiGetCashFlowActivity = async (args: GetApiCaApiGetCashFlowActivityArgs, options?: GetApiCaApiGetCashFlowActivityOptions) => { 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.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_activity${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(getApiCaApiGetCashFlowActivityResponse, await res.json()) };