UNPKG

@contentstack/management

Version:

The Content Management API is used to manage the content of your Contentstack account

142 lines (137 loc) 5.37 kB
import cloneDeep from 'lodash/cloneDeep'; import { create, update, deleteEntity, fetch, query } from '../../entity'; import { PreviewToken } from './previewToken'; /** * Delivery tokens provide read-only access to the associated environments. Read more about <a href='https://www.contentstack.com/docs/developers/create-tokens/about-delivery-tokens'>DeliveryToken</a>. * @namespace DeliveryToken */ export function DeliveryToken(http) { var _this = this; var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; this.stackHeaders = data.stackHeaders; this.urlPath = "/stacks/delivery_tokens"; if (data.token) { Object.assign(this, cloneDeep(data.token)); this.urlPath = "/stacks/delivery_tokens/".concat(this.uid); /** * @description The Update DeliveryToken call lets you update the name and description of an existing DeliveryToken. * @memberof DeliveryToken * @func update * @returns {Promise<DeliveryToken.DeliveryToken>} Promise for DeliveryToken instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).deliveryToken('delivery_token_uid').fetch() * .then((deliveryToken) => { * deliveryToken.name = 'My New Delivery Token' * deliveryToken.description = 'Delivery Token description' * return deliveryToken.update() * }) * .then((deliveryToken) => console.log(deliveryToken)) * */ this.update = update(http, 'token'); /** * @description The Delete DeliveryToken call is used to delete an existing DeliveryToken permanently from your Stack. * @memberof DeliveryToken * @func delete * @returns {Object} Response Object. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).deliveryToken('delivery_token_uid').delete() * .then((response) => console.log(response.notice)) */ this["delete"] = deleteEntity(http); /** * @description The fetch DeliveryToken call fetches DeliveryToken details. * @memberof DeliveryToken * @func fetch * @returns {Promise<DeliveryToken.DeliveryToken>} Promise for DeliveryToken instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).deliveryToken('delivery_token_uid').fetch() * .then((deliveryToken) => console.log(deliveryToken)) * */ this.fetch = fetch(http, 'token'); /** * @description The Create a PreviewToken call creates a new previewToken in a particular stack of your Contentstack account. * @memberof DeliveryToken * @func previewToken * @returns {PreviewToken} Instance of PreviewToken. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * const deliveryToken = client.stack({ api_key: 'api_key'}).deliveryToken('delivery_token_uid') * const previewToken = deliveryToken.previewToken() * console.log(previewToken) */ this.previewToken = function () { return new PreviewToken(http, { stackHeaders: _this.stackHeaders, token: { uid: _this.uid } }); }; } else { /** * @description The Create a DeliveryToken call creates a new deliveryToken in a particular stack of your Contentstack account. * @memberof DeliveryToken * @func create * @returns {Promise<DeliveryToken.DeliveryToken>} Promise for DeliveryToken instance * * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * const token = { * name: 'Test', * description: 'This is a demo token.', * scope: [{ * module: 'environment', * environments: ['development'], * acl: { * read: true * } * }] * } * * client.stack().deliveryToken().create({ token }) * .then((deliveryToken) => console.log(deliveryToken)) */ this.create = create({ http: http }); /** * @description The 'Get all deliveryToken' request returns comprehensive information about all deliveryToken created in a stack. * @memberof DeliveryToken * @func query * @returns {Object} Query builder object with find(), count(), and findOne() methods. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack().deliveryToken().query({ query: { name: 'token_name' } }).find() * .then((contentstackCollection) => console.log(contentstackCollection)) */ this.query = query({ http: http, wrapperCollection: DeliveryTokenCollection }); } } export function DeliveryTokenCollection(http, data) { var obj = cloneDeep(data.tokens) || []; var deliveryTokenCollection = obj.map(function (userdata) { return new DeliveryToken(http, { token: userdata, stackHeaders: data.stackHeaders }); }); return deliveryTokenCollection; }