40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { logResource } from '@/service';
|
|
import axios from 'axios';
|
|
import { MarketType } from "../type";
|
|
import { PriceGetter } from './MarketTypePrice';
|
|
|
|
export const fuzzworkAxiosInstance = axios.create({
|
|
baseURL: import.meta.env.VITE_FUZZWORK_URL,
|
|
headers: {
|
|
'accept': 'application/json',
|
|
"Content-Type": "application/json"
|
|
},
|
|
})
|
|
logResource(fuzzworkAxiosInstance)
|
|
|
|
const batchSize = 100;
|
|
|
|
export const getfuzzworkPrices: PriceGetter = async types => {
|
|
const batches = [];
|
|
|
|
for (let i = 0; i < types.length; i += batchSize) {
|
|
batches.push(fuzzworkAxiosInstance.post(`/aggregates/?station=60003760&types=${types.slice(i, i + batchSize).map(t => t.id).join(",")}`));
|
|
}
|
|
return (await Promise.all(batches))
|
|
.flatMap(b => Object.entries(b.data))
|
|
.map(entry => {
|
|
const id = doParseInt(entry[0]);
|
|
const prices = entry[1] as any;
|
|
|
|
return {
|
|
type: types.find(t => t.id === id) as MarketType,
|
|
buy: doParseFloat(prices?.buy?.max),
|
|
sell: doParseFloat(prices?.sell?.min),
|
|
orderCount: doParseInt(prices?.buy?.order_count) + doParseInt(prices?.sell?.order_count)
|
|
}
|
|
});
|
|
};
|
|
|
|
const doParseInt = (s?: string) => s ? parseInt(s) : 0;
|
|
const doParseFloat = (s?: string) => s ? parseFloat(s) : 0;
|