UNPKG

bptf-api

Version:

A Backpack.tf API for Node.JS

125 lines (119 loc) 5.61 kB
var request = require('request-promise'); class BPTF { constructor(key){ if (typeof key === 'string') this.key = key; else throw new Error('Key must be string.'); } GetCurrencies() { return new Promise ((resolve,reject) => { try { if (this.key){ let link = `https://backpack.tf/api/IGetCurrencies/v1?key=${this.key}` request({ method:"GET", uri:link, headers: { 'User-Agent': 'Mozilla/5.0 (WOW64; rv:16.0) Gecko/20100101 Firefox/16.0' }, json:true }).then((jsonValue)=>{ if (!jsonValue) resolve(new Error('There is no JSON Value on site')) if (jsonValue.response && jsonValue.response.success === 1) resolve(jsonValue.response.currencies) else reject(new Error('There is no response or response is not succesfull.')) }).catch((err)=>{ reject(err) }) } else { reject(new Error('There is no key value.')) } } catch (ex) { resolve(ex) } }) } GetPrices() { return new Promise ((resolve,reject) => { try { if (this.key){ let link = `https://backpack.tf/api/IGetPrices/v4?key=${this.key}` request({ method:"GET", uri:link, headers: { 'User-Agent': 'Mozilla/5.0 (WOW64; rv:16.0) Gecko/20100101 Firefox/16.0' }, json:true }).then((jsonValue)=>{ if (!jsonValue) resolve(new Error('There is no JSON Value on site')) if (jsonValue.response && jsonValue.response.success === 1) resolve(jsonValue.response.items) else if (jsonValue.response.message) reject(new Error('There is no response or response is not succesfull. Error Message is ' + jsonValue.response.message)) else reject(new Error('There is no response or response is not succesfull.')) }).catch((err)=>{ reject(err) }) } else { reject(new Error('There is no key value.')) } } catch (ex) { reject(ex) } }) } GetPriceHistory(item,quality){ return new Promise ((resolve,reject) => { try { if (this.key){ let link = `https://backpack.tf/api/IGetPriceHistory/v1?item=${item.replace(' ','+')}&quality=${quality}&key=${this.key}` request({ method:"GET", uri:link, headers: { 'User-Agent': 'Mozilla/5.0 (WOW64; rv:16.0) Gecko/20100101 Firefox/16.0' }, json:true }).then((jsonValue)=>{ if (!jsonValue) resolve(new Error('There is no JSON Value on site')) if (jsonValue.response && jsonValue.response.success === 1) resolve(jsonValue.response.history) else if (jsonValue.response.message) reject(new Error('There is no response or response is not succesfull. Error Message is ' + jsonValue.response.message)) else reject(new Error('There is no response or response is not succesfull.')) }).catch((err)=>{ reject(err) }) } else { reject(new Error('There is no key value.')) } } catch (ex) { reject(ex) } }) } GetSpecialItems() { return new Promise ((resolve,reject) => { try { if (this.key){ let link = `https://backpack.tf/api/IGetSpecialItems/v1?key=&key=${this.key}` request({ method:"GET", uri:link, headers: { 'User-Agent': 'Mozilla/5.0 (WOW64; rv:16.0) Gecko/20100101 Firefox/16.0' }, json:true }).then((jsonValue)=>{ if (!jsonValue) resolve(new Error('There is no JSON Value on site')) if (jsonValue.response && jsonValue.response.success === 1) resolve(jsonValue.response.items) else if (jsonValue.response.message) reject(new Error('There is no response or response is not succesfull. Error Message is ' + jsonValue.response.message)) else reject(new Error('There is no response or response is not succesfull.')) }).catch((err)=>{ reject(err) }) } else { reject(new Error('There is no key value.')) } } catch (ex) { reject(ex) } }) } } module.exports = BPTF