Files
warframe-center/gather/src/api.js
anatolykopyl ccf1aaba9b
All checks were successful
continuous-integration/drone/push Build is passing
Fix empty orders in getSortedOrders
2022-07-04 12:38:56 +03:00

45 lines
1.0 KiB
JavaScript

const axios = require('axios')
const { RateLimiter } = require('limiter')
class Api {
constructor () {
this.baseUrl = 'https://api.warframe.market/v1/'
this.delay = process.env.API_DELAY ?? 3000
this.limiter = new RateLimiter({ tokensPerInterval: 1, interval: Number(this.delay) })
}
async _get (url) {
await this.limiter.removeTokens(1)
return axios.get(url).catch((error) => {
console.error(error)
return {
data: {
payload: {
orders: []
}
}
}
})
}
async getOrders (url) {
const response = await this._get(this.baseUrl + 'items/' + url + '/orders')
return response.data.payload.orders.filter(function (order) {
return order.order_type === 'sell' && order.user.status !== 'offline'
})
}
async getSortedOrders (url) {
const orders = await this.getOrders(url)
if (orders.length === 0) {
return []
}
return orders.sort(function (a, b) {
return a.platinum - b.platinum
})
}
}
module.exports = new Api()