import {z} from 'zod' export type GetApiEstimatesIdentifierArgs = {identifier: string, window_months?: (3 | 6 | 9 | 12 | 24) | undefined, sources?: ('all' | 'invoices' | 'employments') | undefined, as_of?: string | undefined, history?: boolean | undefined}; export type GetApiEstimatesIdentifierOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const estimatesHistoryItem = z.object({month: z.string().optional(), monthly_income: z.string().nullable().optional(), income_source: z.enum(["invoices", "employment", "blended", "none"]).optional(), employment_status: z.enum(["employed", "self_employed", "unemployed", "retired", "unknown"]).optional(), tenure: z.enum(["less_than_3_months", "3_months_plus", "6_months_plus", "9_months_plus", "12_months_plus"]).nullable().optional()}); export const estimatesResponse = z.object({identifier: z.string().optional(), computed_at: z.string().optional(), parameters: z.object({window_months: z.union([z.literal(3), z.literal(6), z.literal(9), z.literal(12), z.literal(24)]).optional(), sources: z.enum(["all", "invoices", "employments"]).optional(), as_of: z.string().optional(), history: z.boolean().optional()}).optional(), monthly_income: z.string().nullable().optional(), income_source: z.enum(["invoices", "employment", "blended", "none"]).optional(), employment_status: z.enum(["employed", "self_employed", "unemployed", "retired", "unknown"]).optional(), tenure: z.enum(["less_than_3_months", "3_months_plus", "6_months_plus", "9_months_plus", "12_months_plus"]).nullable().optional(), history: z.array(estimatesHistoryItem).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 Estimates * * GET /estimates/{identifier} */ export const getApiEstimatesIdentifier = async (args: GetApiEstimatesIdentifierArgs, options?: GetApiEstimatesIdentifierOptions) => { const {baseUrl = "https://api.burodeingresos.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.window_months != null) query.append("window_months", String(args.window_months)) if (args.sources != null) query.append("sources", String(args.sources)) if (args.as_of != null) query.append("as_of", String(args.as_of)) if (args.history != null) query.append("history", String(args.history)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/estimates/${encodeURIComponent(String(args.identifier))}${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(estimatesResponse, await res.json()) };