import {z} from 'zod' export type VehicleDebtCustomerDTO = {email?: (string | null) | undefined, phone?: (string | null) | undefined, document?: (string | null) | undefined, name?: (string | null) | undefined}; export type VehicleDebtSearchExtraDataCustomDataDTO = {dealership?: (string | null) | undefined, dealership_document?: (string | null) | undefined, customer_score?: (string | null) | undefined, customer_age?: (string | null) | undefined, seller_age?: (string | null) | undefined, active_customer?: (string | null) | undefined, defaulting_customer?: (string | null) | undefined}; export type VehicleDebtSearchExtraDataAffiliatesDataDTO = {coupon?: (string | null) | undefined, gclid?: (string | null) | undefined, utm_source?: (string | null) | undefined, utm_medium?: (string | null) | undefined, utm_keyword?: (string | null) | undefined, utm_campaign?: (string | null) | undefined}; export type VehicleDebtSearchExtraDataDTO = {custom_field1?: (string | Record | null) | undefined, custom_data?: (VehicleDebtSearchExtraDataCustomDataDTO | null) | undefined, issue_ecrlv?: boolean | undefined, affiliates_params?: (VehicleDebtSearchExtraDataAffiliatesDataDTO | null) | undefined}; export type V2PackagesEnterpriseSchemasVehicleDebtSearchSchemaVehicleDebtSearchDTO = {license_plate: string, uf?: (string | null) | undefined, renavam?: (string | null) | undefined, chassis?: (string | null) | undefined, request_id?: (string | null) | undefined, customer?: (VehicleDebtCustomerDTO | null) | undefined, extra_data?: (VehicleDebtSearchExtraDataDTO | null) | undefined, store_id?: (number | null) | undefined}; export type CreateApiVehicleDebtsArgs = {body: V2PackagesEnterpriseSchemasVehicleDebtSearchSchemaVehicleDebtSearchDTO}; export type CreateApiVehicleDebtsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const vehicleDebtSearchResponse = z.object({request_id: z.string()}); 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 }; /** * Solicitação De Consulta * * POST /vehicle/debts/ */ export const createApiVehicleDebts = async (args: CreateApiVehicleDebtsArgs, options?: CreateApiVehicleDebtsOptions) => { const {baseUrl = "https://api.b2b.usezapay.com.br/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/vehicle/debts/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(vehicleDebtSearchResponse, await res.json()) };