import { DEFAULT_TIMEOUT } from "../constants" import { showNetworkError } from "../utils/helpers" import { GetBnplInfoRequest, GetBnplInfoResponse, GetOfferResponse, GetOffersRequest } from "../types/common" import { getMarketApiUrl } from "../config" import { camelize } from "@credify/crypto" /** Customize fetch to dynamically calling request */ export const callApi = async ({ url, method, body, headers, }: { url: string method: string body?: any headers?: any }): Promise => { const controller = new AbortController() const timeout = DEFAULT_TIMEOUT const id = setTimeout(() => { controller.abort() showNetworkError() console.log('url', url) }, timeout) try { const result = await fetch(url, { method: method, signal: controller.signal, ...(body ? { body: JSON.stringify(body), } : {}), ...(headers ? { headers: { 'Accept': 'application/json', 'Content-type': 'application/json', ...headers } } : { headers: { "Content-type": "application/json; charset=UTF-8", }, }), }) return await result.json() } catch (error) { return await null } finally { clearTimeout(id) } } /** Get offers list */ export const getOffers = async ( marketName: string, token: string, data: GetOffersRequest, ): Promise => { const url = `${getMarketApiUrl()}/${marketName}/offers` try { const result = await callApi({ url, method: "POST", body: { local_id: data.localId, phone_number: data.phoneNumber, country_code: data.countryCode, product_types: data.productTypes, credify_id: data.credifyId || undefined, }, headers: { Authorization: `Bearer ${token}`, }, }) return camelize(result) } catch (error) { console.log('error', error) } }