UNPKG

olx-api

Version:

A module to access the OLX API resources.

123 lines (107 loc) 3.83 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var axios = require("axios"); var OlxApi = function () { /** * OlxApi * Initialize the instance of the OlxApi class. * * @name OlxApi * @function * @param {String} host The OLX host (e.g. `https://www.olx.ro/api`). * @param {String} clientId Your application client id. * @param {String} clientSecret Your application client secret. * @returns {OlxApi} The OlxApi instance. */ function OlxApi(host, clientId, clientSecret) { _classCallCheck(this, OlxApi); this.host = host; this.client_id = clientId; this.client_secret = clientSecret; this.access_token = null; this.refresh_token = null; this.version = "2.0"; } /** * getTokens * Get and set the authentication tokens in the instance. * * @name getTokens * @function * @param {String} grantType One of the following: * * - `authorization_code` * - `client_credentials` * - `refresh_token` * * @returns {Object} An object containing the tokens or other authentication data. */ _createClass(OlxApi, [{ key: "getTokens", value: function getTokens(grantType) { var _this = this; return this.post("/open/oauth/token", { grant_type: grantType, client_id: this.client_id, client_secret: this.client_secret, scope: "v2 read write" }).then(function (res) { _this.access_token = res.access_token; _this.refresh_token = res.refresh_token; return res; }); } }, { key: "_makeRequest", value: function _makeRequest(path, data) { var conf = { url: "" + this.host + path, headers: { Version: this.version } }; if (this.access_token) { conf.headers.Authorization = "Bearer " + this.access_token; } if (data) { conf.data = data; conf.method = "POST"; } return axios(conf).then(function (r) { return r.data; }); } /** * post * Make a POST request. * * @name post * @function * @param {String} path The API path. * @param {Object} data The POST data. * @returns {Object} The API response. */ }, { key: "post", value: function post(path, data) { return this._makeRequest(path, data); } /** * get * Make a GET request. * * @name get * @function * @param {String} path The API path. * @returns {Object} The API response. */ }, { key: "get", value: function get(path) { return this._makeRequest(path); } }]); return OlxApi; }(); module.exports = OlxApi;