import {z} from 'zod' export type GetApiDebtLinesIdStatusArgs = {id: string}; export type GetApiDebtLinesIdStatusOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiDebtLinesIdStatusResponse = z.object({checks: z.object({kyx: z.object({status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()}).optional(), collateral: z.object({status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()}).optional(), payout: z.object({status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()}).optional(), payin: z.object({status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()}).optional(), financials: z.object({status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()}).optional(), receivables: z.object({status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()}).optional(), yc: z.object({status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()}).optional()}), steps: z.array(z.object({key: z.enum(["kyx", "yc", "payin", "financials", "receivables", "collateral", "review_terms", "payout"]), blocking: z.boolean(), status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()})), completed_count: z.number().int().gte(0).lte(2147483647), total_count: z.number().int().gte(0).lte(2147483647), can_submit: z.boolean(), can_draw: z.boolean(), blockers: z.array(z.object({key: z.enum(["kyx", "yc", "payin", "financials", "receivables", "collateral", "review_terms", "payout"]), blocking: z.boolean(), status: z.enum(["not_started", "started", "submitted", "completed"]), reason: z.string().min(1).optional(), detail: z.union([z.object({code: z.literal("kyb_not_submitted")}), z.object({code: z.literal("kyb_declined")}), z.object({code: z.literal("kyc_owners_pending")}), z.object({code: z.literal("kyb_info_requested")}), z.object({code: z.literal("kyb_session_expired")}), z.object({code: z.literal("collateral_insufficient"), settled: z.number().int(), required: z.number().int()}), z.object({code: z.literal("financials_months_missing"), months: z.number().int().gte(0).lte(2147483647), required_months: z.number().int().gte(0).lte(2147483647)}), z.object({code: z.literal("financials_short"), fields: z.array(z.object({field: z.enum(["cash_and_equivalents", "revenue", "expenses"]), issue: z.enum(["not_reported", "below_application"])}))}), z.object({code: z.literal("receivables_none_eligible")}), z.object({code: z.literal("receivables_pending_review")})]).optional(), message: z.string().min(1).optional()})), summary: z.string().min(1)}); 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 a debt line's onboarding and draw-eligibility status * * GET /debt-lines/{id}/status * * Also accepts auth: OAuth2 */ export const getApiDebtLinesIdStatus = async (args: GetApiDebtLinesIdStatusArgs, options?: GetApiDebtLinesIdStatusOptions) => { const {baseUrl = "https://core.sivo.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/debt-lines/${encodeURIComponent(String(args.id))}/status${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(getApiDebtLinesIdStatusResponse, await res.json()) };