import {z} from 'zod' export type GetApiApiCheckoutV1ApplicationsArgs = {confirmed_end_date?: string | undefined, confirmed_start_date?: string | undefined, end_date?: string | undefined, external_contract_uuid?: string | undefined, limit?: number | undefined, offset?: number | undefined, start_date?: string | undefined}; export type GetApiApiCheckoutV1ApplicationsOptions = {baseUrl?: string | undefined, MerchantAPIAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const merchantApplicationDetailResponse = z.object({contract_uuid: z.string().optional(), merchant_uuid: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), phone_number: z.string().optional(), formatted_shipping_address: z.record(z.string(), z.unknown()).nullable(), order_reference: z.string().nullable(), status: z.string(), failure_url: z.string().nullable(), success_url: z.string().nullable(), number_of_installments: z.string().optional(), amount: z.number(), currency: z.string(), minor_amount: z.number().int(), credit_type: z.string().optional(), application_approved_at: z.string().nullable(), contract_confirmed_at: z.string().nullable(), secondary_contract_id: z.string().optional(), national_identification_number: z.string().optional(), external_product_ids: z.array(z.string()), created_at: z.string().optional(), updated_at: z.string().optional(), payment_plan_reference: z.string().optional(), refund_amount: z.number()}); export const paginatedMerchantApplicationDetailResponseList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(merchantApplicationDetailResponse)}); 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 }; /** * Contract List * * GET /api/checkout/v1/applications/ */ export const getApiApiCheckoutV1Applications = async (args: GetApiApiCheckoutV1ApplicationsArgs, options?: GetApiApiCheckoutV1ApplicationsOptions) => { const {baseUrl = "https://sbx-origination.heidipay.io", fetch: fetchFn = fetch, MerchantAPIAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.confirmed_end_date != null) query.append("confirmed_end_date", String(args.confirmed_end_date)) if (args.confirmed_start_date != null) query.append("confirmed_start_date", String(args.confirmed_start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.external_contract_uuid != null) query.append("external_contract_uuid", String(args.external_contract_uuid)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (MerchantAPIAuthentication !== undefined) headers.set('authorization', `Bearer ${MerchantAPIAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/checkout/v1/applications/${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(paginatedMerchantApplicationDetailResponseList, await res.json()) };