UNPKG

tesco

Version:

A complimentary Node wrapper for the public Tesco Supermarket API.

95 lines (70 loc) 3.33 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _requestPromise = require('request-promise'); var _requestPromise2 = _interopRequireDefault(_requestPromise); var _ProductSearchFormat = require('./Helpers/ProductSearchFormat'); var _ProductSearchFormat2 = _interopRequireDefault(_ProductSearchFormat); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var TescoAPI = function () { function TescoAPI(key) { _classCallCheck(this, TescoAPI); if (!key) throw new Error('You need to provide an API key. Go to https://devportal.tescolabs.com/ and grab one.'); this.baseURL = 'https://dev.tescolabs.com'; this.request = { uri: this.baseURL, json: true, resolveWithFullResponse: true, headers: { 'Ocp-Apim-Subscription-Key': key }, qs: { query: String, offset: Number, limit: Number } }; } _createClass(TescoAPI, [{ key: 'search', value: function search(query, props, cb) { if (!query) throw new Error('You must pass a query string parameter.'); // At this moment in time, the API only accepts a subscription // to the Grocery Product Search API. // This will need case switching/split in to modules to in // order to define what the URL will be. this.request.uri = this.baseURL + '/grocery/products/'; this.request.qs.query = query; // If nothing is passed to the props object, we still need to // set them in order to return data. if (!props.limit) props.limit = 10; if (!props.offset) props.offset = 0; // Merge the request query string with the props. Object.assign(this.request.qs, props); var res = void 0, err = void 0; // Send request. (0, _requestPromise2.default)(this.request).then(function (response) { var Products = response.body.uk.ghs.products; // Build pagination URL's. var Pagination = {}; var URI = response.request.uri; Pagination.next = URI.protocol + '//' + URI.host + '/?query=' + query + '&offset=' + (props.offset + 1) + '&limit=' + props.limit; Pagination.previous = URI.protocol + '//' + URI.host + '/?query=' + query + '&offset=' + (props.offset > 0 ? props.offset - 1 : props.offset) + '&limit=' + props.limit; // Set Pagination inside response. Products.pagination = Pagination; // Format that lovely data. res = new _ProductSearchFormat2.default(Products); }).catch(function (error) { return err = error; }).then(function () { return cb(err, res); }); } }]); return TescoAPI; }(); exports.default = TescoAPI;