All files / lib/stack/api/asset index.js

100% Statements 27/27
100% Branches 19/19
100% Functions 11/11
100% Lines 26/26
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239      9x                       9x 6x       3x         33x 33x 33x 33x                             9x 9x     9x       24x       39x                     3x                     9x 3x   6x                                                                                                                                                                                                                                       6x 3x   3x                                               6x 3x   3x         288x 288x   33x        
import Base from '../base.js';
import { getReferences, language, environment } from '../../utils.js';
 
let connection = {};
 
/**
 * @summary Creates an instance of `Asset`.
 * @description An initializer is responsible for creating an Asset object.
 * @param {String} uid - uid of the asset
 * @example
 * let Asset = extension.stack.Asset('bltsomething123');
 * @returns {Asset}
 * @ignore
 */
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 Asset extends Base {
  constructor(uid) {
    super(uid);
    this.getReferences = getReferences;
    this.environment = environment;
    return this;
  }
 
  /**
   * @function
   * @name Stack#Asset.Query
   * @description This static method instantiates the query module for assets. To see the list of methods that can be used for querying assets, refer the {@link Query} module.
   * @example
   * let assetQuery = extension.stack.Asset.Query();
   * assetQuery.where("title": "main.js").limit(10).skip(10).find().then(...).catch(...);
   * @return {Query}
   */
 
 
  static Query() {
    const entryQuery = super.Query();
    Object.assign(entryQuery, {
      language, environment
    });
    return entryQuery;
  }
 
  static module(plural = false) {
    return plural ? 'Assets' : 'Asset';
  }
 
  static get connection() {
    return connection;
  }
 
  /**
   * @function
   * @name Stack#Asset.getRteAssets
   * @description This static method retrieves comprehensive information on all assets uploaded through the Rich Text Editor field.
   * @return {external:Promise}
   */
 
  static getRteAssets() {
    return this.connection.sendToParent('stackQuery', { action: 'getRteAssets' }).then(onData).catch(onError);
  }
 
  /**
   * @function
   * @name Stack#Asset.getAssetsOfSpecificTypes
   * @description This static method retrieves assets that are either image or video files, based on the request query.
   * @param  {String} assetType Type of asset to be received for e.g., ‘image/png’
   * @return {external:Promise}
   */
  static getAssetsOfSpecificTypes(assetType) {
    if (!assetType || typeof assetType !== 'string') {
      return Promise.reject(new Error('Kindly provide valid parameters'));
    }
    return this.connection.sendToParent('stackQuery', { action: 'getAssetsOfSpecificTypes', asset_type: assetType }).then(onData).catch(onError);
  }
 
 
  /**
   * @name Stack#Asset#only
   * @function
   * @description This method is used to show the selected fields of the assets in the result set.
   * @param {String} [key=BASE] - Single field of an asset
   * @param {Array} values - Array of fields to be shown in the result set
   * @example
   * <caption> Only with the field UID </caption>
   * extension.stack.Asset('bltsomething123').only('title').fetch();
   * @example
   * <caption> Only with the field UID </caption>
   * extension.stack.Asset('bltsomething123').only('BASE','title').fetch();
   * @example
   * <caption> Only with the field UIDs(array) </caption>
   * extension.stack.Asset('bltsomething123').only(['title','description']).fetch();
   * @returns {Stack#Asset}
   */
 
  /**
   * @name Stack#Asset#except
   * @function
   * @description This method is used to hide the selected fields of the assets in result set.
   * @param {String} [key=BASE] - Single field of an asset
   * @param {Array} values - Array of fields to be hidden in the result set
   * @example
   * <caption> .Except with the field UID </caption>
   * extension.stack.Asset('bltsomething123').except('title').fetch();
   * @example
   * <caption> .Except with the field UID </caption>
   * extension.stack.Asset('bltsomething123').except('BASE','title').fetch();
   * @example
   * <caption> .Except with the field UIDs(array) </caption>
   * extension.stack.Asset('bltsomething123').except(['title','description']).fetch();
   * @returns {Stack#Asset}
   */
 
  /**
   * @function
   * @name Stack#Asset#environment
   * @description This method is used to set the environment name of which you want to retrieve the data.
   * @param {String} environment_uid - UID/Name of environment
   * @example extension.stack.Asset('bltsomething123').environment('development').fetch()
   * @returns {Stack#Asset}
   */
 
 
  /**
   This method includes a query parameter in your query.
   @name Stack#Asset#addParam
   @function
   @example extension.stack.Asset('uid').addParam('key', 'value').fetch().then().catch();
   @param {string} key - Key of the parammeter
   @param {string} value - Value of the parammeter
   @return {Stack#Asset}
  */
 
  /**
   This method includes a query parameter in your query.
   @name Stack#Asset#addQuery
   @function
   @example extension.stack.Asset('uid').addQuery('key', 'value').fetch().then().catch();
   @param {string} key - Key of the parammeter
   @param {string} value - Value of the parammeter
   @return {Stack#Asset}
  */
 
 
  /**
   This method will fetch the details of the entries and the assets in which the specified asset is referenced.
   @see {@link
   https://www.contentstack.com/docs/apis/content-management-api/#get-all-references-of-asset|
   Asset References}
   @name Stack#Asset#getReferences
   @function
   @example extension.stack.Asset('uid').getReferences().then().catch();
   @return {external:Promise}
  */
 
  /**
   This method deletes an existing asset.
   @see {@link
   https://www.contentstack.com/docs/apis/content-management-api/#delete-an-asset|
   Delete Asset}
   @name Stack#Asset#delete
   @function
   @example extension.stack.Asset('uid').delete().then().catch();
   @return {external:Promise}
   */
 
 
  /**
   * @name Stack#Asset#publish
   * @function
   * @description This method allows you to publish the asset either immediately or schedule the publish for a later date/time.
   * @param {object} payload - Payload for the request.
   * @example extension.stack.Asset('bltsomething123')
   .publish(
     {
        "asset": {
          "locales": [
            "en-us"
          ],
          "environments": [
            "development"
          ]
        },
        "version": 1,
        "scheduled_at": "2019-02-08T18:30:00.000Z"
      });
     * @return {external:Promise}
     */
  publish(payload) {
    if (!payload || (typeof payload !== 'object') || (payload instanceof Array)) {
      return Promise.reject(new Error('Kindly provide valid parameters'));
    }
    return this.fetch('publishAsset', payload);
  }
 
  /**
   * @function
   * @name Stack#Asset#unpublish
   * @description This method will instantly unpublish the asset, and also give you the provision to automatically unpublish the asset at a later date/time.
   * @param {object} payload - Payload for the request.
   * @example extension.stack.Asset('bltsomething123')
    .unpublish({
    "asset": {
      "locales": [
        "en-us"
      ],
      "environments": [
        "development"
      ]
    },
    "version": 1,
    "scheduled_at": "2019-02-08T18:30:00.000Z"
  });
   * @return {external:Promise}
   */
  unpublish(payload) {
    if (!payload || (typeof payload !== 'object') || (payload instanceof Array)) {
      return Promise.reject(new Error('Kindly provide valid parameters'));
    }
    return this.fetch('unpublishAsset', payload);
  }
}
 
export default (uiConnection) => {
  connection = uiConnection;
  return new Proxy(Asset, {
    apply(Target, thisArg, argumentsList) {
      return new Target(...argumentsList);
    }
  });
};