Source: entities/space.js

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.wrapSpace = wrapSpace;
  6. exports.wrapSpaceCollection = wrapSpaceCollection;
  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 _toPlainObject = require('contentful-sdk-core/mixins/to-plain-object');
  12. var _toPlainObject2 = _interopRequireDefault(_toPlainObject);
  13. var _wrapHttpClient = require('contentful-sdk-core/wrap-http-client');
  14. var _wrapHttpClient2 = _interopRequireDefault(_wrapHttpClient);
  15. var _enhanceWithMethods = require('../enhance-with-methods');
  16. var _enhanceWithMethods2 = _interopRequireDefault(_enhanceWithMethods);
  17. var _createSpaceApi = require('../create-space-api');
  18. var _createSpaceApi2 = _interopRequireDefault(_createSpaceApi);
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. /**
  21. * @memberof Space
  22. * @typedef Space
  23. * @prop {Object} sys - System metadata
  24. * @prop {string} sys.id - Space id
  25. * @prop {string} sys.type - Entity type
  26. * @prop {string} name - Space name
  27. * @prop {Array<string>} locales - Array with locale codes
  28. * @prop {function(): Object} toPlainObject() - Returns this Space as a plain JS object
  29. */
  30. /**
  31. * @memberof Space
  32. * @typedef SpaceCollection
  33. * @prop {number} total
  34. * @prop {number} skip
  35. * @prop {number} limit
  36. * @prop {Array<Space.Space>} items
  37. * @prop {function(): Object} toPlainObject() - Returns this Space collection as a plain JS object
  38. */
  39. /**
  40. * This method creates the API for the given space with all the methods for
  41. * reading and creating other entities. It also passes down a clone of the
  42. * http client with a space id, so the base path for requests now has the
  43. * space id already set.
  44. * @private
  45. * @param {Object} http - HTTP client instance
  46. * @param {Object} data - API response for a Space
  47. * @return {Space}
  48. */
  49. function wrapSpace(http, data) {
  50. var space = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
  51. var _http$httpClientParam = http.httpClientParams;
  52. var rateLimit = _http$httpClientParam.rateLimit;
  53. var rateLimitPeriod = _http$httpClientParam.rateLimitPeriod;
  54. var maxRetries = _http$httpClientParam.maxRetries;
  55. var retryOnTooManyRequests = _http$httpClientParam.retryOnTooManyRequests;
  56. var httpClientParams = {
  57. concurrency: rateLimit,
  58. delay: rateLimitPeriod,
  59. maxRetries: maxRetries,
  60. retryOnTooManyRequests: retryOnTooManyRequests
  61. };
  62. var spaceScopedHttpClient = (0, _wrapHttpClient2.default)(http.cloneWithNewParams({
  63. space: space.sys.id
  64. }), httpClientParams);
  65. var spaceApi = (0, _createSpaceApi2.default)({
  66. http: spaceScopedHttpClient
  67. });
  68. var enhancedSpace = (0, _enhanceWithMethods2.default)(space, spaceApi);
  69. return (0, _freezeSys2.default)(enhancedSpace);
  70. }
  71. /**
  72. * This method wraps each space in a collection with the space API. See wrapSpace
  73. * above for more details.
  74. * @private
  75. * @param {Object} http - HTTP client instance
  76. * @param {Object} data - API response for a Space collection
  77. * @return {SpaceCollection}
  78. */
  79. function wrapSpaceCollection(http, data) {
  80. var spaces = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
  81. spaces.items = spaces.items.map(function (entity) {
  82. return wrapSpace(http, entity);
  83. });
  84. return (0, _freezeSys2.default)(spaces);
  85. }