Source: entities/entry.js

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.wrapEntry = wrapEntry;
  6. exports.wrapEntryCollection = wrapEntryCollection;
  7. var _cloneDeep = require('lodash/cloneDeep');
  8. var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
  9. var _freezeSys = require('contentful-sdk-core/freeze-sys');
  10. var _freezeSys2 = _interopRequireDefault(_freezeSys);
  11. var _enhanceWithMethods = require('../enhance-with-methods');
  12. var _enhanceWithMethods2 = _interopRequireDefault(_enhanceWithMethods);
  13. var _toPlainObject = require('contentful-sdk-core/mixins/to-plain-object');
  14. var _toPlainObject2 = _interopRequireDefault(_toPlainObject);
  15. var _instanceActions = require('../instance-actions');
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. /**
  18. * Types of fields found in an Entry
  19. * @namespace EntryFields
  20. */
  21. /**
  22. * @memberof EntryFields
  23. * @typedef Symbol
  24. * @type string
  25. */
  26. /**
  27. * @memberof EntryFields
  28. * @typedef Text
  29. * @type string
  30. */
  31. /**
  32. * @memberof EntryFields
  33. * @typedef Integer
  34. * @type number
  35. */
  36. /**
  37. * @memberof EntryFields
  38. * @typedef Number
  39. * @type number
  40. */
  41. /**
  42. * @memberof EntryFields
  43. * @typedef Date
  44. * @type string
  45. */
  46. /**
  47. * @memberof EntryFields
  48. * @typedef Boolean
  49. * @type boolean
  50. */
  51. /**
  52. * @memberof EntryFields
  53. * @typedef Location
  54. * @prop {string} lat - latitude
  55. * @prop {string} lon - longitude
  56. */
  57. /**
  58. * A Field in an Entry can have one of the following types that can be defined in Contentful. See <a href="https://www.contentful.com/developers/docs/references/field-type/">Field Types</a> for more details.
  59. * @memberof EntryFields
  60. * @typedef Field
  61. * @type EntryFields.Symbol | EntryFields.Text | EntryFields.Integer | EntryFields.Number | EntryFields.Date | EntryFields.Boolean | EntryFields.Location | Meta.Link | Array<EntryFields.Symbol|Meta.Link> | Object
  62. */
  63. /**
  64. * @memberof Entry
  65. * @typedef Entry
  66. * @prop {Meta.Sys} sys - Standard system metadata with additional entry specific properties
  67. * @prop {Meta.Link} sys.contentType - Content Type used by this Entry
  68. * @prop {string=} sys.locale - If present, indicates the locale which this entry uses
  69. * @prop {Object<EntryFields.Field>} fields - Object with content for each field
  70. * @prop {function(): Object} toPlainObject() - Returns this Entry as a plain JS object
  71. */
  72. function createEntryApi(http) {
  73. return {
  74. /**
  75. * Sends an update to the server with any changes made to the object's properties
  76. * @memberof Entry
  77. * @func update
  78. * @return {Promise<Entry>} Object returned from the server with updated changes.
  79. * @example
  80. * entry.fields.name['en-US'] = 'Blog Post'
  81. * entry.update()
  82. * .then(entry => console.log(entry.fields.name['en-US']))
  83. */
  84. update: (0, _instanceActions.createUpdateEntity)({
  85. http: http,
  86. entityPath: 'entries',
  87. wrapperMethod: wrapEntry
  88. }),
  89. /**
  90. * Deletes this object on the server.
  91. * @memberof Entry
  92. * @func delete
  93. * @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled.
  94. * @example
  95. * entry.delete()
  96. * .catch(err => console.log(err))
  97. */
  98. delete: (0, _instanceActions.createDeleteEntity)({
  99. http: http,
  100. entityPath: 'entries'
  101. }),
  102. /**
  103. * Publishes the object
  104. * @memberof Entry
  105. * @func publish
  106. * @return {Promise<Entry>} Object returned from the server with updated metadata.
  107. * @example
  108. * entry.publish()
  109. * .then(entry => console.log(entry.sys.publishedVersion))
  110. */
  111. publish: (0, _instanceActions.createPublishEntity)({
  112. http: http,
  113. entityPath: 'entries',
  114. wrapperMethod: wrapEntry
  115. }),
  116. /**
  117. * Unpublishes the object
  118. * @memberof Entry
  119. * @func unpublish
  120. * @return {Promise<Entry>} Object returned from the server with updated metadata.
  121. * @example
  122. * entry.unpublish()
  123. * .then(entry => console.log(entry.sys))
  124. */
  125. unpublish: (0, _instanceActions.createUnpublishEntity)({
  126. http: http,
  127. entityPath: 'entries',
  128. wrapperMethod: wrapEntry
  129. }),
  130. /**
  131. * Archives the object
  132. * @memberof Entry
  133. * @func archive
  134. * @return {Promise<Entry>} Object returned from the server with updated metadata.
  135. * @example
  136. * entry.archive()
  137. * .then(entry => console.log(entry.sys.archivedVersion))
  138. */
  139. archive: (0, _instanceActions.createArchiveEntity)({
  140. http: http,
  141. entityPath: 'entries',
  142. wrapperMethod: wrapEntry
  143. }),
  144. /**
  145. * Unarchives the object
  146. * @memberof Entry
  147. * @func unarchive
  148. * @return {Promise<Entry>} Object returned from the server with updated metadata.
  149. * @example
  150. * entry.unarchive()
  151. * .then(entry => console.log(entry.sys))
  152. */
  153. unarchive: (0, _instanceActions.createUnarchiveEntity)({
  154. http: http,
  155. entityPath: 'entries',
  156. wrapperMethod: wrapEntry
  157. }),
  158. /**
  159. * Checks if the entry is published. A published entry might have unpublished changes (@see {Entry.isUpdated})
  160. * @memberof Entry
  161. * @func isPublished
  162. * @return {boolean}
  163. */
  164. isPublished: (0, _instanceActions.createPublishedChecker)(),
  165. /**
  166. * Checks if the entry is updated. This means the entry was previously published but has unpublished changes.
  167. * @memberof Entry
  168. * @func isUpdated
  169. * @return {boolean}
  170. */
  171. isUpdated: (0, _instanceActions.createUpdatedChecker)(),
  172. /**
  173. * Checks if the entry is in draft mode. This means it is not published.
  174. * @memberof Entry
  175. * @func isDraft
  176. * @return {boolean}
  177. */
  178. isDraft: (0, _instanceActions.createDraftChecker)(),
  179. /**
  180. * Checks if entry is archived. This means it's not exposed to the Delivery/Preview APIs.
  181. * @memberof Entry
  182. * @func isArchived
  183. * @return {boolean}
  184. */
  185. isArchived: (0, _instanceActions.createArchivedChecker)()
  186. };
  187. }
  188. /**
  189. * @private
  190. * @param {Object} http - HTTP client instance
  191. * @param {Object} data - Raw entry data
  192. * @return {Entry} Wrapped entry data
  193. */
  194. /**
  195. * Entry instances
  196. * @namespace Entry
  197. */
  198. function wrapEntry(http, data) {
  199. var entry = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
  200. (0, _enhanceWithMethods2.default)(entry, createEntryApi(http));
  201. return (0, _freezeSys2.default)(entry);
  202. }
  203. /**
  204. * @memberof Entry
  205. * @typedef EntryCollection
  206. * @prop {number} total
  207. * @prop {number} skip
  208. * @prop {number} limit
  209. * @prop {Array<Entry.Entry>} items
  210. * @prop {Array<Object>=} errors - Array of errors that might occur when retrieving entries.
  211. * @prop {function(): Object} toPlainObject() - Returns this Entry collection as a plain JS object
  212. */
  213. /**
  214. * Data is also mixed in with link getters if links exist and includes were requested
  215. * @private
  216. * @param {Object} http - HTTP client instance
  217. * @param {Object} data - Raw entry collection data
  218. * @return {EntryCollection} Wrapped entry collection data
  219. */
  220. function wrapEntryCollection(http, data, resolveLinks) {
  221. var entries = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
  222. entries.items = entries.items.map(function (entity) {
  223. return wrapEntry(http, entity);
  224. });
  225. return (0, _freezeSys2.default)(entries);
  226. }