UNPKG

kentico-deliver-js

Version:

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

145 lines (110 loc) 3.51 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _combineQueryValues = function _combineQueryValues(values) { return values.join(''); }; var _id = function _id(id, operator) { if (typeof id !== 'string') { throw new Error('id must be passed as a string'); } if (id.indexOf(' ') >= 0) { throw new Error('id must not contain spaces'); } var op = _parseOperator(operator, 'id'); return '&system.id' + op + '=' + id; }; var _name = function _name(name, operator) { if (typeof name !== 'string') { throw new Error('name must be passed as a string'); } var op = _parseOperator(operator, 'name'); return '&system.name' + op + '=' + encodeURIComponent(name); }; var _codeName = function _codeName(codeName, operator) { if (typeof codeName !== 'string') { throw new Error('codeName must be passed as a string'); } if (codeName.indexOf(' ') >= 0) { throw new Error('codeName must not contain spaces'); } var op = _parseOperator(operator, 'codeName'); return '&system.codename' + op + '=' + codeName; }; var _type = function _type(type, operator) { if (typeof type !== 'string') { throw new Error('type must be passed as a string'); } var op = _parseOperator(operator, 'type'); return '&system.type' + op + '=' + encodeURIComponent(type); }; var _sitemapLocation = function _sitemapLocation(sitemapLocation, operator) { if (typeof sitemapLocation !== 'string') { throw new Error('sitemapLocation must be passed as a string'); } var op = _parseOperator(operator, 'sitemapLocation'); return '&system.sitemap_locations' + op + '=' + encodeURIComponent(sitemapLocation); }; var _lastModified = function _lastModified(lastModified, operator) { if (!lastModified instanceof Date) { throw new Error('lastModified must be passed as a Date object'); } var op = _parseOperator(operator, 'lastModified'); return '&system.last_modified' + op + '=' + lastModified; }; var _published = function _published(published, previewAPIKey) { if (published === false && previewAPIKey === undefined) { throw new Error('To fetch unpublished content, you must supply a Preview API Key'); } if (typeof published !== 'boolean') { throw new Error('published must be passed as a boolean value'); } return published; }; var _parseOperator = function _parseOperator(operator, fnCalled) { if (operator === undefined) { return ''; } switch (operator.toLowerCase()) { case 'lt': return '[lt]'; case 'lessthan': return '[lt]'; case 'lte': return '[lte]'; case 'lessthanorequal': return '[lte]'; case 'lessthanorequalto': return '[lte]'; case 'gt': return '[gt]'; case 'greaterthan': return '[gt]'; case 'gte': return '[gte]'; case 'greaterthanorequal': return '[gte]'; case 'greaterthanorequalto': return '[gte]'; case 'range': return '[range]'; case 'ranging': return '[range]'; case 'in': return '[in]'; case 'contains': return '[contains]'; default: throw new Error('Unrecognised operator in ' + fnCalled); } }; exports._combineQueryValues = _combineQueryValues; exports._id = _id; exports._name = _name; exports._codeName = _codeName; exports._type = _type; exports._sitemapLocation = _sitemapLocation; exports._lastModified = _lastModified; exports._published = _published; exports._parseOperator = _parseOperator;