UNPKG

kentico-deliver-js

Version:

A light wrapper around the Kentico Deliver/Cloud API for parameterised queries

274 lines (227 loc) 8.72 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 _endpoints = require('./config/endpoints'); var _processors = require('./processors'); require('isomorphic-fetch'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * @module KenticoDeliverAPI * @example * * // Get all items of content type `drinks` * import KenticoCloudAPI from 'kentico-deliver-js'; * * const api = new KenticoCloudAPI('Project-ID'); * * let items; * * api * .type('drinks') * .run() * .then(result => console.info(result, 👌)) * .catch(error => console.warn(error)) * */ var KenticoDeliverAPI = function () { /** * Instantiates the KenticoDeliverAPI class * @constructor * @param {string} Kentico Your Project ID. */ function KenticoDeliverAPI(projectId, previewAPIKey) { _classCallCheck(this, KenticoDeliverAPI); if (typeof projectId !== 'string' || projectId === undefined) { throw new Error('KenticoDeliverAPI must be instantiated with a Project ID'); } if (previewAPIKey && typeof previewAPIKey !== 'string') { throw new Error('KenticoDeliverAPI Preview API Key must be a string'); } this.uriEndpoint = 'items'; this.projectId = projectId; this.previewAPIKey = previewAPIKey; this.defaults = { values: [], published: true, queryText: '' }; this._initAndResetQuery(); return this; } /** * Returns one item of the given system id. * @param {string} id - The id of the resource * @param {string} operator - The operator to filter the resource by (lt, lte, gt, gte, in, contains, range) */ _createClass(KenticoDeliverAPI, [{ key: 'id', value: function id(_id2, operator) { return this._handler(_id2, operator, _processors._id); } /** * Returns one item of the given system name. * @param {string} name - The name of the resource * @param {string} operator - The operator to filter the resource by (lt, lte, gt, gte, in, contains, range) */ }, { key: 'name', value: function name(_name2, operator) { return this._handler(_name2, operator, _processors._name); } /** * Returns one item of the given system codename. * @param {string} codeName - The codeName of the resource * @param {string} operator - The operator to filter the resource by (lt, lte, gt, gte, in, contains, range) */ }, { key: 'codeName', value: function codeName(_codeName2, operator) { return this._handler(_codeName2, operator, _processors._codeName); } /** * Returns items of the given system content type. * @param {string} type - The content type of the resource * @param {string} operator - The operator to filter the resource by (lt, lte, gt, gte, in, contains, range) */ }, { key: 'type', value: function type(_type2, operator) { return this._handler(_type2, operator, _processors._type); } /** * Returns items in the given sitemap location. * @param {string} sitemapLocation - The sitemap location of the resource * @param {string} operator - The operator to filter the resource by (lt, lte, gt, gte, in, contains, range) */ }, { key: 'sitemapLocation', value: function sitemapLocation(_sitemapLocation2, operator) { return this._handler(_sitemapLocation2, operator, _processors._sitemapLocation); } /** * Returns items matching the given last modified date * @param {Date} lastModified - The last modified date of the resource * @param {string} operator - The operator to filter the resource by (lt, lte, gt, gte, in, contains, range) */ }, { key: 'lastModified', value: function lastModified(_lastModified2, operator) { return this._handler(_lastModified2, operator, _processors._lastModified); } /** * Filters the dataset to return either published or unpublished content * @param {Boolean} published - Whether the content is published or not * @param {string} operator - The operator to filter the resource by (lt, lte, gt, gte, in, contains, range) */ }, { key: 'published', value: function published(_published2) { var previewAPIKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; this.query = Object.assign(this.query, this.query.published = (0, _processors._published)(_published2, this.previewAPIKey)); return this; } /** * Runs the provided query. Must be supplied at the end of the chain * @return {Promise} The dataset. */ }, { key: 'run', value: function run() { var _this = this; return new Promise(function (resolve, reject) { _this.query.queryText = (0, _processors._combineQueryValues)(_this.query.values); _this._fetchData(_this.query, resolve, reject).then(function (result) { _this._initAndResetQuery(); resolve(result); }).catch(function (error) { _this._initAndResetQuery(); reject(error); }); }); } /** * A debug method for seeing what your query object looks like * @return {Promise} The query object as the result of the promise. */ }, { key: 'queryDebug', value: function queryDebug() { var _this2 = this; return new Promise(function (resolve, reject) { _this2.query.queryText = (0, _processors._combineQueryValues)(_this2.query.values); resolve(_this2.query); _this2._initAndResetQuery(); }); } /** * Pass a raw query (not recommended). Example: 'system.id=123' * @param {string} query - The query content */ }, { key: 'dangerousQuery', value: function dangerousQuery(query) { if (typeof query !== 'string') { throw new Error('dangerousQuery must be passed a string'); } return this._handler(query, '', function () { return query; }); } /** * Fetches the data from Kentico Deliver/Cloud using the `fetch` package * @private * @param {object} query - The query object * @param {function} resolve - The function to call once data has been received * @param {function} reject - The function to call if an error occurs */ }, { key: '_fetchData', value: function _fetchData(query, resolve, reject) { var _query = this.query, queryText = _query.queryText, published = _query.published; var fullQuery = this.projectId + '/' + this.uriEndpoint + '?' + queryText; if (!query.published) { return fetch(_endpoints.unpublishedContent + '/' + fullQuery, { headers: { Authorization: 'Bearer ' + this.previewAPIKey } }).then(function (result) { return resolve(result.json()); }).catch(function (error) { return reject(error); }); } else { return fetch(_endpoints.publishedContent + '/' + fullQuery).then(function (result) { return resolve(result.json()); }).catch(function (error) { return reject(error); }); } } /** * Processes the request for the resource and adds it our the array of queries, as a string * @private * @param {string} val - The value wanted for the query * @param {string} operator - The operator to filter the query with * @param {function} fn - The function to be called to manipulate the query string content */ }, { key: '_handler', value: function _handler(val, operator, fn) { this.query = Object.assign(this.query, this.query.values.push(fn(val, operator))); return this; } /** * Resets the query object to the default properties * @private */ }, { key: '_initAndResetQuery', value: function _initAndResetQuery() { return this.query = Object.assign({}, this.defaults); } }]); return KenticoDeliverAPI; }(); exports.default = KenticoDeliverAPI;