All files / lib/stack index.js

100% Statements 53/53
100% Branches 38/38
100% Functions 14/14
100% Lines 53/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                    66x 66x                   1x                 24x 23x 23x   24x                 12x 12x 6x   12x                   6x 6x 3x 3x 3x   3x                 14x 14x                   4x 1x   3x 3x 3x                   3x 3x 3x 3x 3x                     4x 1x   3x 3x 3x                   3x 3x 3x 3x 3x                   4x 1x   3x 3x 3x                   3x 3x 3x 3x 3x         12x 6x 6x       12x      
import * as Utils from "./utils.js";
import Entry from './entry.js';
import Assets from './assets.js';
import Query from './query.js';
 
/** Class representing the current stack from contentstack ui. */
 
class Stack {
 
  constructor(data, connection) {
    this._connection = connection;
    this._data = data;
  }
 
 
  /**
   * This method returns data of the current stack.
   * @return {Object} Returns stack data.
   */
 
  getData() {
    return this._data;
  }
 
  /**
   * @description Set "ContentType" from the Stack from where you want to retrive the entries.
   * @param {String} [content_type_uid] - uid of the existing contenttype
   * @returns {Stack}
   */
  ContentType(uid) {
    if (uid && typeof uid === 'string') {
      this.content_type_uid = uid;
      this.type = "contentType";
    }
    return this;
  }
  /**
   * @description Set the Entry Uid which you want to retrive from the Contenttype specified.
   * @param {String} uid - entry_uid
   * @example ContentType('blog').Entry('blt1234567890abcef')
   * @returns {Object}
   */
  Entry(uid) {
    let entry = new Entry(this._connection);
    if (uid && typeof uid === "string") {
      entry.entry_uid = uid;
    }
    return Utils.merge(entry, this);
  }
 
  /**
   * @description Set the Asset Uid which you want to retrive the Asset.
   * @param {String} uid - asset_uid
   * @example stack.Assets('blt1234567890abcef').fetch()
   * @returns {Object}
   */
  Assets(uid) {
    this.type = 'asset';
    if (uid && typeof uid === "string") {
        let asset = new Assets();
        asset.asset_uid = uid;
        return Utils.merge(asset, this);
    }
    return this;
  }
 
  /**
   * @description Query instance to provide support for all search queries.
   * @example ContentType('blog').Query()
   * @returns {Object}
   */
  Query() {
    let query = new Query(this._connection);
    return Utils.merge(query, this);
  }
 
    /**
   * This API allows users to interact with a content type of a stack using the Content Type Content Delivery API requests. Method returns a Promise object.
   * @param {string} uid Uid of the desired content type
   * @param {Object} params optional params for the get call
   * @return {Object} A Promise object which will resolve with content type details.
   */
  getContentType(uid, params) {
    if (!uid){
      return Promise.reject('uid is required');
    }
    params = params || {};
    let options = { uid, params, action: "getContentType" };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows users to interact with the content types of a stack using the Content Type Content Delivery API requests. Method returns a Promise object.
   * @param {Object} query query for the get call
   * @param {Object} params optional params for the get call
   * @return {Object} A Promise object which will resolve with details of content type.
   */
  getContentTypes(query, params) {
    query = query || {};
    params = params || {};
    params.query = query;
    let options = { params, action: "getContentTypes" };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
 
  /**
   * This API allows users to interact with an environment of a stack using the Environment Content Delivery API requests. Method returns a Promise object.
   * @param {string} name Name of the desired environment
   * @param {Object} params optional params for the get call
   * @return {Object} A Promise object which will resolve with environment details.
   */
  getEnvironment(name, params) {
    if (!name) {
      return Promise.reject('name is required');
    }
    params = params || {};
    let options = { name, params, action: "getEnvironment" };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows users to interact with the environments of a stack using the Environment Content Delivery API requests. Method returns a Promise object.   
   * @param {Object} query query for the get call
   * @param {Object} params optional params for the get call
   * @return {Object} A Promise object which will resolve with details of environments.
   */
  getEnvironments(query, params) {
    query = query || {};
    params = params || {};
    params.query = query;
    let options = { params, action: "getEnvironments" };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows users to interact with a locale of a stack using the Languages Content Delivery API Requests. Method returns a Promise object.
   * @param {string} code Code of the desired locale
   * @param {Object} params optional params for the get call
   * @return {Object} A Promise object which will resolve with locale details.
   */
  getLocale(code, params) {
    if (!code) {
      return Promise.reject('code is required');
    }
    params = params || {};
    let options = { code, params, action: "getLocale" };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows users to interact with the locales of a stack using the Languages Content Delivery API Requests. Method returns a Promise object.
   * @param {Object} query query for the get call
   * @param {Object} params optional params for the get call
   * @return {Object} A Promise object which will resolve with details of locales.
   */
  getLocales(query, params) {
    query = query || {};
    params = params || {};
    params.query = query;
    let options = { params, action: "getLocales" };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
}
 
function onData(data) {
  if (typeof (data.data) === "string")
    return Promise.reject(data.data)
  return Promise.resolve(data.data)
}
 
function onError(error) {
  return Promise.reject(error)
}
 
export default Stack;