Node.js - Multiple requests to Kentico Cloud Delivery API at once

node v6.17.1
version: 1.0.0
endpointsharetweet
const request = require('request'), requestPromise = require("request-promise"), Promise = require('bluebird'); 'use strict'; /** * Initilizes class with Project ID and Preview API Key that represent a Kentico Cloud project. * @constructor Delivery * @param {string} projectID Project ID, see details in the Kentico Cloud Developers Hub: https://developer.kenticocloud.com/docs/using-delivery-api#section-getting-project-id. * @param {string} previewKey Preview API Key, see details in the Kentico Cloud Developers Hub: https://developer.kenticocloud.com/docs/preview-content-via-api. * @example * var project = new Delivery('82594550-e25c-8219-aee9-677f600bad53', 'ew0KICAiYWxnIjo...QvV8puicXQ'); */ class Delivery { constructor(projectID, previewKey) { this.projectID = projectID; this.previewKey = typeof previewKey === 'undefined' ? null : previewKey; } /** * Returns promise with data from the Kentico Cloud storage specified by the passed query string. * @method getContent * @param {object} params Object that contains filtering url parameters that are used for requesting Kentico Cloud storage. See details about filtering url parameters: https://developer.kenticocloud.com/v1/reference#delivery-api * @param {boolean} isPreview Flag that controls whether only published or all items should be requested. * @return {promise} with object of responses for each passed parameter from the Kentico Cloud storage. * @example * // returns * // { * // master: {items: [...]}, * // page: {items: [...]} * // } * project.getContent({ * master: '?system.type=master', * page: '?system.type=home' * }, true) */ getContent(params, isPreview) { // Reject operation if params parameter in not provided if (typeof params === 'undefined') { Promise.reject('Please, specify the params parameter in the getContent method.'); } // Set default state if maethod parameter are not provided if (typeof isPreview === 'undefined') { isPreview = false; } // Split categories and filtering urls in separate arrays var categories = [], values = []; Object.keys(params).forEach((key, index) => { categories.push(key); values.push(params[key]); }); params = values.slice(); // Create options that will represent your request to the Kentico Cloud storage let options = Delivery.getOptions(params, this.projectID, this.previewKey, isPreview); // Request the Kentico Cloud storage and get content from it return Delivery.getRawData(options) .then(function(data) { // And assemble an object structure of the original object return Delivery.categorizeContent(data, categories); }) .catch(console.error); } /* * Helper methods section */ // Retrun array of promises with objects obtained from the Kentico Cloud storage static getRawData(options) { return Promise.map(options, (item) => { return requestPromise(item).catch(console.error); }); } // Return url that should be requested static getDeliveryUrl (projectID, isPreview) { let url = ''; // If we want to preview our content we need request a slightly different url if (isPreview) { url = 'https://preview-deliver.kenticocloud.com/' + projectID + '/items'; } else { url ='https://deliver.kenticocloud.com/' + projectID + '/items'; } return url; } //Returns options for the request // Structure of the options object is defined by the request-promise package (https://www.npmjs.com/package/request-promise) static getOptions (params, projectID, previewKey, isPreview) { var options = []; // Create request options of each param if (isPreview && previewKey !== null) { params.forEach((item) => { options.push({ uri: Delivery.getDeliveryUrl(projectID, isPreview) + item, json: true, simple: false, headers: { Authorization: 'Bearer ' + previewKey } }); }); } else { params.forEach((item) => { options.push({ uri: Delivery.getDeliveryUrl(projectID, isPreview) + item, json: true, simple: false }); }); } return options; } // Join array of category names and array of responses to fit the orogonal object static categorizeContent(content, categories) { let categorizedContent = {}; content.forEach((item, index) => { categorizedContent[categories[index]] = item; }); return categorizedContent; } }; /* * Usage */ // Initialize Delivery with Project ID and Preview API Key let kenticoCloudProject = new Delivery('2548121d-cad8-4458-a910-5e4b54cb0956', 'ew0KICAiYWxnIjogIkhTMjU2IiwNCiAgInR5cCI6ICJKV1QiDQp9.ew0KICAidWlkIjogInVzcl8wdlVJVzkwTnRQSVNxNm1GSDN2ZFhiIiwNCiAgImVtYWlsIjogImhlbGxvQG1pbGFubHVuZC5jb20iLA0KICAicHJvamVjdF9pZCI6ICIyNTQ4MTIxZC1jYWQ4LTQ0NTgtYTkxMC01ZTRiNTRjYjA5NTYiLA0KICAianRpIjogInhrU1BLUjlzbzgxSV9rel8iLA0KICAidmVyIjogIjEuMC4wIiwNCiAgImdpdmVuX25hbWUiOiAiTWlsYW4iLA0KICAiZmFtaWx5X25hbWUiOiAiTHVuZCIsDQogICJhdWQiOiAicHJldmlldy5kZWxpdmVyLmtlbnRpY29jbG91ZC5jb20iDQp9.PpBh6wTk57e1_tPHzROiqWPTpr3IjrEoGN8J4rtfPIg'); // Request the Kentico Cloud storage with filtering url parameters and a boolean flag defining whether you want preview content or not kenticoCloudProject.getContent({ master: '?system.type=master', page: '?system.type=home' }, true) .then(console.log) // Show results .catch(console.error); // Or error
Loading…

no comments

    sign in to comment