All files / lib/stack index.js

100% Statements 33/33
100% Branches 17/17
100% Functions 10/10
100% Lines 32/32
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        36x 18x       36x                         288x 288x                   288x                   288x                   3x                     12x 3x   9x 9x                   9x 9x 9x 9x                   12x 3x   9x 9x                   9x 9x 9x 9x                   12x 3x   9x 9x                   9x 9x 9x 9x          
import Asset from './api/asset/index.js';
import ContentType from './api/content-type/index.js';
 
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);
}
 
 
/**
 * Class representing the current stack in Contentstack UI.
 */
 
class Stack {
  /**
   * @hideconstructor
   */
  constructor(data, connection) {
    this._connection = connection;
    this._data = data;
    /**
     * @constructor
     * @hideconstructor
     * @version 2.0.0
     * @desc Content type defines the structure or schema of a page or a section of your web or mobile property
     * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#content-types| ContentType}
     * @param {string} uid - Uid of contenttype.
     * @example extension.stack.ContentType('content_type_uid')
     * */
    this.ContentType = ContentType(connection);
    /**
     * @constructor
     * @version 2.0.0
     * @hideconstructor
     * @desc An initializer is responsible for creating an Asset object.
     * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#assets| Asset}
     * @param {string} uid - UID of the asset.
     * @example extension.stack.Asset('asset_uid')
     * */
    this.Asset = Asset(connection);
  }
 
 
  /**
   * This method returns the data of the current stack.
   * @return {Object} Returns stack data.
   */
 
  getData() {
    return this._data;
  }
 
 
  /**
   * This API allows you to retrieve data of a content type of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-content-type| Content Type API} requests. This method returns a Promise object.
   * @param {string} uid Uid of the desired content type
   * @param {Object} params Optional parameters for the GET call
   * @return {Object} A promise object which will be resolved with content type details.
   */
  getContentType(uid, params = {}) {
    if (!uid) {
      return Promise.reject(new Error('uid is required'));
    }
    const options = { uid, params, action: 'getContentType' };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows you to retrieve data of a content types of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Content Types API} requests. This method returns a Promise object.
   * @param {Object} query Query for the GET call
   * @param {Object} params Optional parameters for the GET call
   * @return {Object} A promise object which will be resolved with details of the content type.
   */
  getContentTypes(query = {}, params = {}) {
    const optionParams = params;
    optionParams.query = query;
    const options = { params: optionParams, action: 'getContentTypes' };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows you to retrieve environment details of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-environment| Environment API} requests. This method returns a Promise object.
   * @param {string} name Name of the desired environment
   * @param {Object} params Optional parameters for the GET call
   * @return {Object} A promise object which will be resolved with environment details.
   */
  getEnvironment(name, params = {}) {
    if (!name) {
      return Promise.reject(new Error('name is required'));
    }
    const options = { name, params, action: 'getEnvironment' };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows you to retrieve details of environments of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-environments| Environments API} requests. This method returns a Promise object.
   * @param {Object} query Query for the GET call
   * @param {Object} params Optional parameters for the GET call
   * @return {Object} A Promise object which will be resolved with details of the environments.
   */
  getEnvironments(query = {}, params = {}) {
    const optionParams = params;
    optionParams.query = query;
    const options = { params: optionParams, action: 'getEnvironments' };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows you to retrive a locale of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-a-language| Language API} requests. Method returns a Promise object.
   * @param {string} code Code of the desired locale
   * @param {Object} params Optional parameters for the GET call
   * @return {Object} A promise object which will be resolved with locale details.
   */
  getLocale(code, params = {}) {
    if (!code) {
      return Promise.reject(new Error('code is required'));
    }
    const options = { code, params, action: 'getLocale' };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
 
  /**
   * This API allows you to retrive the locales of a stack using the {@link https://www.contentstack.com/docs/apis/content-management-api/#get-all-content-types| Languages API} requests. Method returns a Promise object.
   * @param {Object} query Query for the GET call
   * @param {Object} params Optional parameters for the GET call
   * @return {Object} A Promise object which will be resolved with details of the locales.
   */
  getLocales(query = {}, params = {}) {
    const optionParams = params;
    optionParams.query = query;
    const options = { params: optionParams, action: 'getLocales' };
    return this._connection.sendToParent('stackQuery', options).then(onData).catch(onError);
  }
}
 
export default Stack;