import { stringify } from "querystring"; import { get } from "request"; const URI = "https://api.cardmarket.com/ws/v2.0/output.json"; export default class CardMarketApi { private AppToken: string; private AppSecret: string; private AccessToken: string; private AccessSecret: string; constructor() { this.AppToken = process.env.APP_TOKEN; this.AppSecret = process.env.APP_SECRET; this.AccessToken = process.env.ACCESS_TOKEN; this.AccessSecret = process.env.ACCESS_SECRET; } get(path: string, params?: Record): Promise { const oauthParameters = { realm: `${URI}${path}`, consumer_key: this.AppToken, consumer_secret: this.AppSecret, token: this.AccessToken, token_secret: this.AccessSecret }; return new Promise((resolve, reject) => { get( oauthParameters.realm + (params ? `?${stringify(params)}` : ""), { oauth: oauthParameters }, (error, response) => { if (error) { throw error; } else if (response.statusCode > 299) { throw JSON.stringify({ statusCode: response.statusCode, statusMessage: response.statusMessage }); } try { resolve(JSON.parse(response.body)); } catch (error) { reject(error); } } ); }); } }