All files / lib/stack/api/content-type entry.js

100% Statements 37/37
100% Branches 27/27
100% Functions 12/12
100% Lines 37/37
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373      9x 9x         69x 66x 66x                           45x 45x     45x       78x       108x       60x                                                                                                                                                                                                                                                                                                                                                                                               3x                       6x 3x   3x 3x                                       6x 3x   3x 3x                                       6x 3x   3x 3x                                                           6x 3x   3x                                         6x 3x   3x 3x         117x 117x 117x   69x 66x     66x        
import Base from '../base.js';
import { getReferences, addQuery, language, environment, includeOwner, includeContentType, includeSchema, includeReference } from '../../utils.js';
 
let connection = {};
let contentTypeUid = '';
 
 
class Entry extends Base {
  constructor(uid) {
    super(uid);
    this._query = {};
    return this;
  }
 
  /**
   * @function
   * @name Stack#ContentType#Entry.Query
   * @description This static method instantiates the query module for entries. To see the list of methods that can be used for querying entries, refer the {@link Query} module.
   * @example
   * let entryQuery = extension.stack.ContentType('content_type_uid').Entry.Query();
   * entryQuery.where("field_uid": "10").limit(10).skip(10).find().then(...).catch(...);
   * @return {Query}
   */
 
  static Query() {
    const entryQuery = super.Query();
    Object.assign(entryQuery, {
      language, environment, includeOwner, includeContentType, includeSchema, includeReference
    });
    return entryQuery;
  }
 
  static get connection() {
    return connection;
  }
 
  static get contentTypeUid() {
    return contentTypeUid;
  }
 
  static module(plural = false) {
    return plural ? 'Entries' : 'Entry';
  }
  /**
  This method creates a new entry.
  @see {@link
  https://www.contentstack.com/docs/apis/content-management-api/#create-a-an-entry|
  Create Entry}
  @name Stack#ContentType#Entry.create
  @function
  @example extension.stack.ContentType('content_type_uid').Entry.create({
    "entry": {
      "title": "example",
      "url": "/example"
    }
  }).then(...).catch(...);
  @param {Object} payload Data to create an entry
  @return {external:Promise}
  Required data if resolved successfully
  */
 
  /**
  * @name Stack#ContentType#Entry#only
  * @function
  * @description This method is used to show the selected fields of an entry in the result set.
  * @param {String} [key=BASE] - Single field of an entry
  * @param {Array} values - Array of fields to be shown in the result set
  * @example
  * <caption> Only with field UID </caption>
  * extension.stack.ContentType('content_type_uid').Entry('bltsomething123').only('title').fetch();
  * @example
  * <caption> Only with field UID </caption>
  * extension.stack.ContentType('content_type_uid').Entry('bltsomething123').only('BASE','title').fetch();
  * @example
  * <caption> Only with field UIDs(array) </caption>
  * extension.stack.ContentType('content_type_uid').Entry('bltsomething123').only(['title','description']).fetch();
  * @returns {Stack#ContentType#Entry}
  */
 
  /**
  * @name Stack#ContentType#Entry#except
  * @function
  * @description This method is used to hide the selected fields of an entry in the result set.
  * @param {String} [key=BASE] - Single field of an entry
  * @param {Array} values - Array of fields to be hidden in the result set
  * @example
  * <caption> Except with field uid </caption>
  * extension.stack.ContentType('content_type_uid').Entry('bltsomething123').except('title').fetch();
  * @example
  * <caption> Except with field uid </caption>
  * extension.stack.ContentType('content_type_uid').Entry('bltsomething123').except('BASE','title').fetch();
  * @example
  * <caption> Except with field uids(array) </caption>
  * extension.stack.ContentType('content_type_uid').Entry('bltsomething123').except(['title','description']).fetch();
  * @returns {Stack#ContentType#Entry}
  */
 
  /**
  * This method includes a query parameter in your query.
  * @name Stack#ContentType#Entry#addParam
  * @function
  * @example extension.stack.ContentType('content_type_uid').Entry('uid').addParam('include_count', 'true').fetch().then().catch();
  * @param {string} key - Key of the parameter
  * @param {string} value - Value of the parameter
  * @return {Stack#ContentType#Entry} Returns
  */
 
 
  /**
  This method will fetch all the entries in which the current entry is referenced.
  @see {@link
  https://www.contentstack.com/docs/apis/content-management-api/#get-all-references-of-an-entry|
  Entry References}
  @name Stack#ContentType#Entry#getReferences
  @function
  @example extension.stack.ContentType('content_type_uid').Entry('uid').getReferences().then().catch();
  @return {external:Promise}
  Required data if resolved successfully
  */
 
 
  /**
  @example extension.stack.ContentType('content_type_uid').Entry('uid').update({"entry" : {...}}).then().catch();
  @param {Object} payload Data to be update exsisting entry
  @return {external:Promise}
  Required data if resolved successfully
  */
 
 
  /**
  This method deletes an existing entry.
  @see {@link
  https://www.contentstack.com/docs/apis/content-management-api/#delete-an-entry|
  Delete Entry}
  @name Stack#ContentType#Entry#delete
  @function
  @example extension.stack.ContentType('content_type_uid').Entry('uid').delete().then().catch();
  @return {external:Promise}
  Required data if resolved successfully
  */
 
 
  /**
  This method fetches information of a specific entry.
  @see {@link
  https://www.contentstack.com/docs/apis/content-management-api/#get-a-single-an-entry|
  Get A Single Entry}
  @name Stack#ContentType#Entry#fetch
  @function
  @example extension.stack.ContentType('content_type_uid').Entry('uid').fetch().then().catch();
  @return {external:Promise}
  Required data if resolved successfully
  */
 
  /**
   * @function
   * @name Stack#ContentType#Entry#includeReference
   * @description This method is used to include referenced entries from other content types.
   * @example
   * <caption> .includeReference with reference_field_uids as array </caption>
   * stack.ContentType('contenttype_uid').Entry('bltsomething123').includeReference(['category', 'author']).fetch()
   * @example
   * <caption> .includeReference with reference_field_uids </caption>
   * stack.ContentType('contenttype_uid').Entry('bltsomething123').includeReference('category', 'author').fetch()
   * @returns {Stack#ContentType#Entry}
   */
 
 
  /**
   * @function
   * @name Stack#ContentType#Entry#language
   * @description This method is used to set the language code of which you want to retrieve the data.
   * @param {String} languageCode - Language code, for e.g. 'en-us', 'ja-jp', and so on
   * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').language('en-us').fetch()
   * @returns {Stack#ContentType#Entry}
   */
 
  /**
   * @function
   * @name Stack#ContentType#Entry#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.ContentType('contenttype_uid').Entry('bltsomething123').environment('development').fetch()
   * @returns {Stack#ContentType#Entry}
   */
 
 
  /**
   * @function
   * @name Stack#ContentType#Entry#addQuery
   * @description This method is used to add a query to an entry object.
   * @param {String} key - Key of the query
   * @param {String} value - Value of the query
   * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').addQuery('include_schema',true).fetch()
   * @returns {Stack#ContentType#Entry}
   */
 
 
  /**
   * @function
   * @name Stack#ContentType#Entry#includeSchema
   * @description This method is used to include the schema of the current contenttype in result set along with the entry/entries.
   * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').includeSchema().fetch()
   * @returns {Stack#ContentType#Entry}
   */
 
 
  /**
   * @function
   * @name Stack#ContentType#Entry#includeContentType
   * @description This method is used to include the current content type in the result set along with the entry(ies).
   * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').includeContentType().fetch()
   * @returns {Stack#ContentType#Entry}
   */
 
 
  /**
   * @function
   * @name Stack#ContentType#Entry#includeOwner
   * @description This method is used to include the owner of the entry(ies) in the result set.
   * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').includeOwner().fetch()
   * @returns {Stack#ContentType#Entry}
   */
 
 
  /**
  * @function
  * @name Stack#ContentType#Entry#getLanguages
  * @description This method returns the details of all the languages that an entry is localized in.
  * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').getLanguages()
  * @return {external:Promise}
  */
  getLanguages() {
    return this.fetch('getEntryLanguages');
  }
 
  /**
  * @function
  * @name Stack#ContentType#Entry#unlocalize
  * @description This method is used to unlocalize an entry
  * @param  {string} locale Locale in which the entry has to be unlocalized
  * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').unlocalize('fr-fr').then(...).catch(...);
  * @return {external:Promise}
  */
  unlocalize(locale) {
    if (!locale || typeof locale !== 'string') {
      return Promise.reject(new Error('Kindly provide valid parameters'));
    }
    this._query.locale = locale;
    return this.fetch('unlocalizeEntry');
  }
 
  /**
  * @function
  * @name Stack#ContentType#Entry#publish
  * @description This method lets you publish an entry either immediately or schedule it to be published automatically at a later date/time.
  * @param {object} payload - Payload for the request
  * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').publish({
      "entry": {
          "environments": ["development"],
          "locales": ["en-us"]
      },
      "locale": "en-us",
      "version": 1,
      "scheduled_at": "2019-02-14T18:30:00.000Z"
  }).then(...).catch(...);
  * @return {external:Promise}
  */
  publish(payload) {
    if (!payload || (typeof payload !== 'object') || (payload instanceof Array)) {
      return Promise.reject(new Error('Kindly provide valid parameters'));
    }
    this._query = {};
    return this.fetch('publishEntry', payload);
  }
 
  /**
  * @function
  * @name Stack#ContentType#Entry#unpublish
  * @description This method lets you publish an entry either immediately or schedule it to be published automatically at a later date/time.
  * @param {object} payload - Payload for the request
  * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').unpublish({
      "entry": {
          "environments": ["development"],
          "locales": ["en-us"]
      },
      "locale": "en-us",
      "version": 1,
      "scheduled_at": "2019-02-14T18:30:00.000Z"
  }).then(...).catch(...);
  * @return {external:Promise}
  */
  unpublish(payload) {
    if (!payload || (typeof payload !== 'object') || (payload instanceof Array)) {
      return Promise.reject(new Error('Kindly provide valid parameters'));
    }
    this._query = {};
    return this.fetch('unpublishEntry', payload);
  }
 
  /**
  * @function
  * @name Stack#ContentType#Entry#setWorkflowStage
  * @description This method allows you to either set a particular workflow stage or update the workflow stage details of an entry.
  * @param {object} payload - Payload for the request
  * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').setWorkflowStage({
     "workflow": {
         "workflow_stage": {
             "comment": "Test Comment",
             "due_date": "Thu Dec 01 2018",
             "notify": false,
             "uid": "blt9f52a2cd5e0014fb",
             "assigned_to": [{
                 "uid": "blt296a22e28cc0c63c",
                 "name": "John Doe",
                 "email": "john.doe@contentstack.com"
             }],
             "assigned_by_roles": [{
                 "uid": "blt5b74c24c7ae25d94",
                 "name": "Content Manager"
             }]
         }
     }
  }).then(...).catch(...);
  * @return {external:Promise}
  */
  setWorkflowStage(payload) {
    if (!payload || (typeof payload !== 'object') || (payload instanceof Array)) {
      return Promise.reject(new Error('Kindly provide valid parameters'));
    }
    return this.fetch('setWorkflowStageEntry', payload);
  }
 
  /**
  * @see {@link https://www.contentstack.com/docs/apis/content-management-api/#update-an-entry| Update Entry}
  * @name Stack#ContentType#Entry#update
  * @function
  * @description This call allows you to update entry content.
  * @param {object} payload - Payload for the request
  * @param {string} [locale] - Passing the ‘locale’ parameter will localize the entry in the specified locale
  *  to be localized in the specified locale.
  * @example extension.stack.ContentType('contenttype_uid').Entry('bltsomething123').update(
      {
      "entry": {
          "title": "example",
          "url": "/example"
      }
  }).then(...).catch(...);
  * @return {external:Promise}
  */
  update(payload, locale) {
    if (!payload || (typeof payload !== 'object') || (payload instanceof Array)) {
      return Promise.reject(new Error('Kindly provide valid parameters'));
    }
    this._query.locale = locale;
    return this.fetch('updateEntry', payload);
  }
}
 
export default (uiConnection, contentType) => {
  connection = uiConnection;
  contentTypeUid = contentType;
  return new Proxy(Entry, {
    apply(Target, thisArg, argumentsList) {
      const entryTarget = new Target(...argumentsList);
      Object.assign(entryTarget, {
        getReferences, addQuery, language, environment, includeOwner, includeContentType, includeSchema, includeReference
      });
      return entryTarget;
    }
  });
};