import {z} from 'zod' export type GetApiCreditApplicationsApplicationIdOptions = {baseUrl?: string | undefined, identity?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const creditApplication = z.object({application_id: z.string().min(1).optional(), program_id: z.string().min(1), date_created: z.string(), date_updated: z.string(), application_status: z.enum(["created", "submitted", "resubmit_required", "approved", "denied"]), applicant: z.object({customer_id: z.string().min(1).optional(), employment_status: z.enum(["employed", "self_employed", "unemployed", "retired", "student", "other"]).min(6).optional(), address_id: z.string().min(1).optional(), total_annual_income: z.number().int().optional(), monthly_housing_payment: z.number().int().optional(), terms_accepted: z.boolean().optional(), currency: z.literal("USD"), first_name: z.string().optional(), middle_name: z.string().optional(), last_name: z.string().optional(), date_of_birth: z.string().optional(), street: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), zip_code: z.string().optional(), ssn: z.string().optional(), email: z.string().optional()}), accounts: z.object({security_deposit_account_id: z.string().nullable().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 credit application * * GET /credit/applications/{application_id} */ export const getApiCreditApplicationsApplicationId = async (options?: GetApiCreditApplicationsApplicationIdOptions) => { const {baseUrl = "https://sandbox.bond.tech/api/v0", fetch: fetchFn = fetch, identity, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/credit/applications/${encodeURIComponent(String(args.application_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(creditApplication, await res.json()) };