Source: create-contentful-api.js

'use strict';

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

var _createRequestConfig = require('contentful-sdk-core/create-request-config');

var _createRequestConfig2 = _interopRequireDefault(_createRequestConfig);

var _entities = require('./entities');

var _entities2 = _interopRequireDefault(_entities);

var _pagedSync = require('./paged-sync');

var _pagedSync2 = _interopRequireDefault(_pagedSync);

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

/**
 * Creates API object with methods to access functionality from Contentful's
 * Delivery API
 * @private
 * @param {Object} params - API initialization params
 * @prop {Object} http - HTTP client instance
 * @prop {Object} entities - Object with wrapper methods for each kind of entity
 * @prop {Function} shouldLinksResolve - Link resolver preconfigured with global setting
 * @return {ClientAPI}
 */
function createContentfulApi(_ref) {
  var http = _ref.http;
  var shouldLinksResolve = _ref.shouldLinksResolve;
  var wrapSpace = _entities2.default.space.wrapSpace;
  var _entities$contentType = _entities2.default.contentType;
  var wrapContentType = _entities$contentType.wrapContentType;
  var wrapContentTypeCollection = _entities$contentType.wrapContentTypeCollection;
  var _entities$entry = _entities2.default.entry;
  var wrapEntry = _entities$entry.wrapEntry;
  var wrapEntryCollection = _entities$entry.wrapEntryCollection;
  var _entities$asset = _entities2.default.asset;
  var wrapAsset = _entities$asset.wrapAsset;
  var wrapAssetCollection = _entities$asset.wrapAssetCollection;


  function errorHandler(error) {
    if (error.data) {
      throw error.data;
    }
    throw error;
  }

  /**
   * Gets the Space which the client is currently configured to use
   * @memberof ContentfulClientAPI
   * @return {Promise<Entities.Space>} Promise for a Space
   * @example
   * client.getSpace()
   * .then(space => console.log(space))
   */
  function getSpace() {
    return http.get('').then(function (response) {
      return wrapSpace(response.data);
    }, errorHandler);
  }

  /**
   * Gets a Content Type
   * @memberof ContentfulClientAPI
   * @param  {string} id
   * @return {Promise<Entities.ContentType>} Promise for a Content Type
   * @example
   * client.getContentType('contentTypeId')
   * .then(contentType => console.log(contentType))
   */
  function getContentType(id) {
    return http.get('content_types/' + id).then(function (response) {
      return wrapContentType(response.data);
    }, errorHandler);
  }

  /**
   * Gets a collection of Content Types
   * @memberof ContentfulClientAPI
   * @param  {Object=} query - Object with search parameters. Check the <a href="https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters">JS SDK tutorial</a> and the <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters">REST API reference</a> for more details.
   * @return {Promise<Entities.ContentTypeCollection>} Promise for a collection of Content Types
   * @example
   * client.getContentTypes()
   * .then(contentTypes => console.log(contentTypes.items))
   */
  function getContentTypes() {
    var query = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

    return http.get('content_types', (0, _createRequestConfig2.default)({ query: query })).then(function (response) {
      return wrapContentTypeCollection(response.data);
    }, errorHandler);
  }

  /**
   * Gets an Entry
   * @memberof ContentfulClientAPI
   * @param  {string} id
   * @param  {Object=} query - Object with search parameters. In this method it's only useful for `locale`.
   * @return {Promise<Entities.Entry>} Promise for an Entry
   * @example
   * client.getEntry('entryId')
   * .then(entry => console.log(entry))
   */
  function getEntry(id) {
    var query = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

    return http.get('entries/' + id, (0, _createRequestConfig2.default)({ query: query })).then(function (response) {
      return wrapEntry(response.data);
    }, errorHandler);
  }

  /**
   * Gets a collection of Entries
   * @memberof ContentfulClientAPI
   * @param  {Object=} query - Object with search parameters. Check the <a href="https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters">JS SDK tutorial</a> and the <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters">REST API reference</a> for more details.
   * @param  {boolean=} query.resolveLinks - When true, links to other Entries or Assets are resolved. Default: true.
   * @return {Promise<Entities.EntryCollection>} Promise for a collection of Entries
   * @example
   * client.getEntries({content_type: 'contentTypeId'})
   * .then(entries => console.log(entries.items))
   */
  function getEntries() {
    var query = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

    var resolveLinks = shouldLinksResolve(query);
    var resolveForAllLocales = query.locale && query.locale === '*';
    return http.get('entries', (0, _createRequestConfig2.default)({ query: query })).then(function (response) {
      return wrapEntryCollection(response.data, resolveLinks, resolveForAllLocales);
    }, errorHandler);
  }

  /**
   * Gets an Asset
   * @memberof ContentfulClientAPI
   * @param  {string} id
   * @param  {Object=} query - Object with search parameters. In this method it's only useful for `locale`.
   * @return {Promise<Entities.Asset>} Promise for an Asset
   * @example
   * client.getAsset('assetId')
   * .then(asset => console.log(asset))
   */
  function getAsset(id) {
    var query = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

    return http.get('assets/' + id, (0, _createRequestConfig2.default)({ query: query })).then(function (response) {
      return wrapAsset(response.data);
    }, errorHandler);
  }

  /**
   * Gets a collection of Assets
   * @memberof ContentfulClientAPI
   * @param  {Object=} query - Object with search parameters. Check the <a href="https://www.contentful.com/developers/docs/javascript/tutorials/using-js-cda-sdk/#retrieving-entries-with-search-parameters">JS SDK tutorial</a> and the <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters">REST API reference</a> for more details.
   * @return {Promise<Entities.AssetCollection>} Promise for a collection of Assets
   * @example
   * client.getAssets()
   * .then(assets => console.log(assets.items))
   */
  function getAssets() {
    var query = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

    return http.get('assets', (0, _createRequestConfig2.default)({ query: query })).then(function (response) {
      return wrapAssetCollection(response.data);
    }, errorHandler);
  }

  /**
   * Synchronizes either all the content or only new content since last sync
   * See <a href="https://www.contentful.com/developers/docs/concepts/sync/">Synchronization</a> for more information.
   * <strong> Important note: </strong> The the sync api endpoint does not support include or link resolution.
   * However contentful.js is doing link resolution client side if you only make an initial sync.
   * For the delta sync (using nextSyncToken) it is not possible since the sdk wont have access to all the data to make such an operation.
   * @memberof ContentfulClientAPI
   * @param  {Object} query - Query object for the sync call. One of initial or nextSyncToken always needs to be specified, but not both.
   * @param  {boolean?} query.initial - Indicates if this is the first sync. Use it if you don't have a sync token.
   * @param  {string?} query.nextSyncToken - The token you got the last time you used this method. Ensures you only get changed content.
   * @param  {string=} query.type - Filter by this type (Entry or Asset)
   * @param  {string=} query.content_type - Filter by this content type id
   * @param  {boolean=} query.resolveLinks - When true, links to other Entries or Assets are resolved. Default: true.
   * @return {Promise<Sync.SyncCollection>} Promise for the collection resulting of a sync operation
   * @example
   * client.sync()
   * .then((response) => console.log(response.entries, response.assets, response.nextSyncToken))
   */
  function sync() {
    var query = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

    var resolveLinks = shouldLinksResolve(query);
    return (0, _pagedSync2.default)(http, query, resolveLinks);
  }

  return {
    getSpace: getSpace,
    getContentType: getContentType,
    getContentTypes: getContentTypes,
    getEntry: getEntry,
    getEntries: getEntries,
    getAsset: getAsset,
    getAssets: getAssets,
    sync: sync
  };
} /**
   * Contentful Delivery API Client. Contains methods which allow access to the
   * different kinds of entities present in Contentful (Entries, Assets, etc).
   * @namespace ContentfulClientAPI
   * @see Entities
   */

/**
 * The different kinds of top level entities you can find in Contentful
 * @namespace Entities
 */

/**
 * System metadata. See <a href="https://www.contentful.com/developers/docs/references/content-delivery-api/#/introduction/common-resource-attributes">Common Resource Attributes</a> for more details.
 * @memberof Entities
 * @typedef Sys
 * @prop {string} type
 * @prop {string} id
 * @prop {Entities.Link} space
 * @prop {string} createdAt
 * @prop {string} updatedAt
 * @prop {number} revision
 */

/**
 * Link to another entity. See <a href="https://www.contentful.com/developers/docs/concepts/links/">Links</a> for more details.
 * @memberof Entities
 * @typedef Link
 * @prop {string} type - type of this entity. Always link.
 * @prop {string} id
 * @prop {string} linkType - type of this link. If defined, either Entry or Asset
 */

/**
 * @memberof ContentfulClientAPI
 * @typedef {Object} ClientAPI
 * @prop {function} getSpace
 * @prop {function} getContentType
 * @prop {function} getContentTypes
 * @prop {function} getEntry
 * @prop {function} getEntries
 * @prop {function} getAsset
 * @prop {function} getAssets
 * @prop {function} sync
 */