import {z} from 'zod' export type GetApiOffersArgs = {card_network?: string | undefined, currency?: string | undefined, offer_value?: string | undefined, receivable_total_value?: string | undefined, acquirer_id?: Array | undefined, investor_id?: Array | undefined, status?: Array | undefined, created_greater_or_equal_than?: string | undefined, created_less_or_equal_than?: string | undefined, expire_date_greater_or_equal_than?: string | undefined, expire_date_less_or_equal_than?: string | undefined}; export type GetApiOffersOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const domainOfferResponse = z.object({card_network: z.string().optional(), company_id: z.number().int().optional(), created_at: z.string().optional(), currency: z.string().optional(), expire_date: z.string().optional(), id: z.number().int().optional(), minimal_value: z.number().int().optional(), offer_value: z.number().int().optional(), receivable_total_value: z.number().int().optional(), status: z.string().optional(), total_proposals: z.number().int().optional(), total_transaction_numbers: z.number().int().optional(), updated_at: z.string().optional(), user_id: z.number().int().optional()}); export const domainOfferResponseFind = z.object({limit: z.number().int().optional(), page: z.number().int().optional(), rows: z.array(domainOfferResponse).optional(), sort: z.string().optional(), total_pages: z.number().int().optional(), total_rows: 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 }; /** * Find offers by query filters. * * GET /offers */ export const getApiOffers = async (args: GetApiOffersArgs, options?: GetApiOffersOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, BASE64UsernamePassword} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.card_network != null) query.append("card_network", String(args.card_network)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.offer_value != null) query.append("offer_value", String(args.offer_value)) if (args.receivable_total_value != null) query.append("receivable_total_value", String(args.receivable_total_value)) for (const item of args.acquirer_id ?? []) query.append("acquirer_id", String(item)) for (const item of args.investor_id ?? []) query.append("investor_id", String(item)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.created_greater_or_equal_than != null) query.append("created_greater_or_equal_than", String(args.created_greater_or_equal_than)) if (args.created_less_or_equal_than != null) query.append("created_less_or_equal_than", String(args.created_less_or_equal_than)) if (args.expire_date_greater_or_equal_than != null) query.append("expire_date_greater_or_equal_than", String(args.expire_date_greater_or_equal_than)) if (args.expire_date_less_or_equal_than != null) query.append("expire_date_less_or_equal_than", String(args.expire_date_less_or_equal_than)) if (BASE64UsernamePassword !== undefined) headers.set('authorization', `Basic ${btoa(`${BASE64UsernamePassword.username}:${BASE64UsernamePassword.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/offers${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(domainOfferResponseFind, await res.json()) };