68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
import * as _ from 'lodash';
|
|
import { POE_HOME, POE_LEAGUE_LIST_URL, POE_STASH_ITEMS_URL } from '../constants';
|
|
import { http } from '../http';
|
|
import { IItem } from '../model/item';
|
|
import { PromiseQueue } from '../util';
|
|
import { ItemTextService } from './item-text.service';
|
|
import { StorageService } from './storage.service';
|
|
|
|
const queue = new PromiseQueue();
|
|
|
|
const getStash = async (username: string, league: string, tabIndex: number | string): Promise<any> => {
|
|
const res = await http.get(
|
|
`${POE_STASH_ITEMS_URL}?league=${league}&tabs=1&tabIndex=${tabIndex}&accountName=${username}`,
|
|
);
|
|
return res.data;
|
|
};
|
|
|
|
/**
|
|
* Clears the request queue. Used when switching leagues/stashes
|
|
*/
|
|
const clearRequestQueue = () => {
|
|
queue.clearQueue();
|
|
};
|
|
|
|
const priceCheck = async (item: IItem, league: string): Promise<any> => {
|
|
const itemCache = StorageService.getItemCache(item.id);
|
|
// return cached item if it exists - otherwise fetch from Poe Prices
|
|
if (itemCache) {
|
|
return Promise.resolve(itemCache);
|
|
}
|
|
|
|
return new Promise(resolve => {
|
|
queue.add(() => {
|
|
return http
|
|
.get(`https://poeprices.info/api?l=${league}&i=${encodeURI(btoa(ItemTextService.parseItem(item)))}`, {
|
|
headers: {
|
|
'Cache-Control': 'max-age=600',
|
|
},
|
|
})
|
|
.then(data => {
|
|
if (_.get(data, 'data.error') === 0) {
|
|
StorageService.storeItemCache(item.id, data.data);
|
|
}
|
|
return resolve(data.data);
|
|
});
|
|
}, 1000);
|
|
});
|
|
};
|
|
|
|
const getUsername = async (headers: any): Promise<any> => {
|
|
const res = await http.get(POE_HOME, { headers });
|
|
const username = res.data.match(/\/account\/view-profile\/(.*?)\"/);
|
|
return username[1];
|
|
};
|
|
|
|
const getLeagues = async (headers: any): Promise<any> => {
|
|
const res = await http.get(POE_LEAGUE_LIST_URL, { headers });
|
|
return res.data;
|
|
};
|
|
|
|
export const PoeService = {
|
|
clearRequestQueue,
|
|
getLeagues,
|
|
getStash,
|
|
getUsername,
|
|
priceCheck,
|
|
};
|