import {z} from 'zod' export type DomainCreateSimulationDataInput = {card_network: string, expire_date: string, investors: Array, minimal_value: number, offer_value: number, receivable_id: Array}; export type CreateApiOffersSimulationArgs = {body: DomainCreateSimulationDataInput}; export type CreateApiOffersSimulationOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const domainReceivable = z.object({card_network: z.string().optional(), cnab_id: z.number().int().optional(), company_id: z.number().int().optional(), compound_key: z.string().optional(), created_at: z.string().optional(), due_date: z.string().optional(), id: z.number().int().optional(), issuer: z.string().optional(), previous_id: z.number().int().optional(), spreadsheet_id: z.number().int().optional(), status: z.string().optional(), transactions_number: z.number().int().optional(), transactions_number_available: z.number().int().optional(), updated_at: z.string().optional(), value: z.number().optional(), value_available: z.number().optional()}); export const domainResponseSimulationCreate = z.object({card_network: z.string().optional(), company_id: z.number().int().optional(), expire_date: z.string().optional(), higher_date_offer_create_limit: z.string().optional(), id: z.number().int().optional(), issuers: z.number().int().optional(), lower_date_offer_create_limit: z.string().optional(), max_receivable_due_date: z.string().optional(), min_receivable_due_date: z.string().optional(), receivable_numbers: z.number().int().optional(), receivable_total_value: z.number().optional(), receivables: z.array(domainReceivable).optional(), receivables_id: z.array(z.number().int()).optional(), total_transaction_numbers: z.number().int().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 }; /** * Offer Creation Simulation * * POST /offers/simulation */ export const createApiOffersSimulation = async (args: CreateApiOffersSimulationArgs, options?: CreateApiOffersSimulationOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, BASE64UsernamePassword} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BASE64UsernamePassword !== undefined) headers.set('authorization', `Basic ${btoa(`${BASE64UsernamePassword.username}:${BASE64UsernamePassword.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/offers/simulation${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(domainResponseSimulationCreate, await res.json()) };