Source: entities/api-key.js

'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.wrapApiKey = wrapApiKey;
exports.wrapApiKeyCollection = wrapApiKeyCollection;

var _cloneDeep = require('lodash/cloneDeep');

var _cloneDeep2 = _interopRequireDefault(_cloneDeep);

var _freezeSys = require('contentful-sdk-core/freeze-sys');

var _freezeSys2 = _interopRequireDefault(_freezeSys);

var _enhanceWithMethods = require('../enhance-with-methods');

var _enhanceWithMethods2 = _interopRequireDefault(_enhanceWithMethods);

var _toPlainObject = require('contentful-sdk-core/mixins/to-plain-object');

var _toPlainObject2 = _interopRequireDefault(_toPlainObject);

var _instanceActions = require('../instance-actions');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * @memberof ApiKey
 * @typedef ApiKey
 * @prop {Meta.Sys} sys - System metadata
 * @prop {string} name
 * @prop {string} description
 * @prop {function(): Object} toPlainObject() - Returns this Api Key as a plain JS object
 */

function createApiKeyApi(http) {
  return {

    /**
     * Sends an update to the server with any changes made to the object's properties
     * @memberof ApiKey
     * @func update
     * @return {Promise<ApiKey>} Object returned from the server with updated changes.
     * @example
     * apiKey.name = 'New name'
     * apiKey.update()
     * .then(apiKey => console.log(apiKey.name))
     */
    update: (0, _instanceActions.createUpdateEntity)({
      http: http,
      entityPath: 'api_keys',
      wrapperMethod: wrapApiKey
    }),

    /**
     * Deletes this object on the server.
     * @memberof ApiKey
     * @func delete
     * @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled.
     * @example
     * apiKey.delete()
     * .catch(err => console.log(err))
     */
    delete: (0, _instanceActions.createDeleteEntity)({
      http: http,
      entityPath: 'api_keys'
    })
  };
}

/**
 * @private
 * @param {Object} http - HTTP client instance
 * @param {Object} data - Raw api key data
 * @return {ApiKey} Wrapped api key data
 */
/**
 * Api Key instances
 * @namespace ApiKey
 */
function wrapApiKey(http, data) {
  var apiKey = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
  (0, _enhanceWithMethods2.default)(apiKey, createApiKeyApi(http));
  return (0, _freezeSys2.default)(apiKey);
}

/**
 * @memberof ApiKey
 * @typedef ApiKeyCollection
 * @prop {number} total
 * @prop {number} skip
 * @prop {number} limit
 * @prop {Array<ApiKey.ApiKey>} items
 * @prop {function(): Object} toPlainObject() - Returns this Api Key collection as a plain JS object
 */

/**
 * @private
 * @param {Object} http - HTTP client instance
 * @param {Object} data - Raw api key collection data
 * @return {ApiKeyCollection} Wrapped api key collection data
 */
function wrapApiKeyCollection(http, data) {
  var apiKeys = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
  apiKeys.items = apiKeys.items.map(function (entity) {
    return wrapApiKey(http, entity);
  });
  return (0, _freezeSys2.default)(apiKeys);
}