Home Reference Source Repository

lib/entities/role.js

  1. /**
  2. * Role instances
  3. * @namespace Role
  4. */
  5. import cloneDeep from 'lodash/cloneDeep'
  6. import { freezeSys, toPlainObject } from 'contentful-sdk-core'
  7. import enhanceWithMethods from '../enhance-with-methods'
  8. import {
  9. createUpdateEntity,
  10. createDeleteEntity
  11. } from '../instance-actions'
  12.  
  13. /**
  14. * @see https://www.contentful.com/developers/docs/references/content-management-api/#/reference/roles/create-a-role
  15. * @typedef {Role} Role
  16. * @property {Object} sys - System metadata
  17. * @property {string} name
  18. * @property {Object} permissions - Permissions for application sections
  19. * @property {Object} policies
  20. * @property {function(): Prmise<Role>} update - Sends an update to the server with any changes made to the object's properties
  21. * @property {function(): Prmise} delete - Deletes this object on the server.
  22. * @property {function(): Object} toPlainObject - Returns this Role as a plain JS object
  23. */
  24.  
  25. /**
  26. * @typedef {RoleCollection} RoleCollection
  27. * @property {number} total - Total amount of records in the server
  28. * @property {number} skip - A starting point of the collection
  29. * @property {number} limit - Amount of records in collection
  30. * @property {Role[]} items - an array of roles
  31. * @property {function(): Object} toPlainObject - Returns this Role collection as a plain JS object
  32. */
  33.  
  34. function createRoleApi (http) {
  35. return {
  36.  
  37. /**
  38. * Sends an update to the server with any changes made to the object's properties
  39. * @memberof Role
  40. * @func update
  41. * @return {Promise<Role>} Object returned from the server with updated changes.
  42. * @example
  43. * role.name = 'New name'
  44. * role.update()
  45. * .then(role => console.log(role.name))
  46. */
  47. update: createUpdateEntity({
  48. http: http,
  49. entityPath: 'roles',
  50. wrapperMethod: wrapRole
  51. }),
  52.  
  53. /**
  54. * Deletes this object on the server.
  55. * @memberof Role
  56. * @func delete
  57. * @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled.
  58. * @example
  59. * role.delete()
  60. * .catch(err => console.log(err))
  61. */
  62. delete: createDeleteEntity({
  63. http: http,
  64. entityPath: 'roles'
  65. })
  66. }
  67. }
  68.  
  69. /**
  70. * @private
  71. * @param {Object} http - HTTP client instance
  72. * @param {Object} data - Raw role data
  73. * @return {Role} Wrapped role data
  74. */
  75. export function wrapRole (http, data) {
  76. const role = toPlainObject(cloneDeep(data))
  77. enhanceWithMethods(role, createRoleApi(http))
  78. return freezeSys(role)
  79. }
  80.  
  81. /**
  82. * @private
  83. * @param {Object} http - HTTP client instance
  84. * @param {Object} data - Raw role collection data
  85. * @return {RoleCollection} Wrapped role collection data
  86. */
  87. export function wrapRoleCollection (http, data) {
  88. const roles = toPlainObject(cloneDeep(data))
  89. roles.items = roles.items.map((entity) => wrapRole(http, entity))
  90. return freezeSys(roles)
  91. }