import axios, { AxiosInstance, AxiosError } from 'axios' import * as AxiosRetry from 'axios-retry' import apiOrderToAppOrder from '../libs/apiOrderToAppOrder' import appBatchToApiBatch from '../libs/appBatchToApiBatch' const axiosRetry = AxiosRetry.default const retryConfig: AxiosRetry.IAxiosRetryConfig = { retries: 5, retryDelay: axiosRetry.exponentialDelay, retryCondition: (error) => error.response?.status === 404 && error.response?.data?.error === 'order not exist', } export default class Api { baseUrlGo: string baseUrlNode: string apiVersion: string token?: string refresh?: string locale?: string axiosGo: AxiosInstance axiosNode: AxiosInstance updateUserCallback?: (auth: IRegisterResponse) => void resetUserCallback?: (errorMessage: string) => void refreshCustomerApiCallback?: () => void constructor (baseUrlGo: string, baseUrlNode: string, locale = 'zh') { this.locale = locale this.baseUrlGo = baseUrlGo this.baseUrlNode = baseUrlNode this.axiosGo = axios.create({ baseURL: baseUrlGo, }) this.axiosNode = axios.create({ baseURL: baseUrlNode, }) axiosRetry(this.axiosGo, retryConfig) axiosRetry(this.axiosNode, retryConfig) this.apiVersion = '2.0.0' this.axiosGo.defaults.headers.common['X-Api-Version'] = this.apiVersion this.axiosNode.defaults.headers.common['X-Api-Version'] = this.apiVersion } setUpdateUserCallback (callback: (auth: IRegisterResponse) => void): void { this.updateUserCallback = callback } setResetUserCallback (callback: (errorMessage: string) => void): void { this.resetUserCallback = callback } setRefreshCustomerApiCallback (callback: () => void): void { this.refreshCustomerApiCallback = callback } setToken (token: string, refresh: string): void { this.token = token this.refresh = refresh this.axiosGo.defaults.headers.common.Authorization = `Bearer ${token}` this.axiosNode.defaults.headers.common.Authorization = `Bearer ${token}` } setLanguage (locale: string): void { this.locale = locale this.axiosGo.defaults.headers.common['Accept-Language'] = locale this.axiosNode.defaults.headers.common['Accept-Language'] = locale } logError (error: AxiosError): void { if (error.response?.data != null && error.response?.status != null) { console.error({ type: 'ajax error', request: { method: error.config.method, url: error.config.url, data: error.config.data, params: error.config.params, }, response: { data: error.response.data, status: error.response.status, }, }) } else { console.error(error) } } async shortUrl (longUrl: string): Promise { const response = await axios.post('https://s.dimorder.com/api/v1/shorten', { long_url: longUrl }) return response.data.short_url } async calculateOrder (params: ICalculateOrderParams): Promise { const endpoint = '/g/order' const { batches, deliveryType, shipping, rounding, surcharge, modifiers, promoCodeId, usedRiceCoin, } = params const response = await this.axiosGo.post(endpoint, { batches: batches.map((batch) => { const apiBatch = appBatchToApiBatch({ orderId: '', orderBatch: batch, deliveryType: deliveryType, shipping: shipping, ignoreUnavailable: false, }) apiBatch.status = 'submitted' return apiBatch }), deliveryType, shipping: deliveryType === 'storeDelivery' ? { ...shipping, customerAmount: shipping.totalFee, } : null, rounding, surcharge: deliveryType === 'table' ? surcharge : null, modifiers, promocode: promoCodeId, riceCoin: usedRiceCoin, }) return apiOrderToAppOrder(response.data) } }