UNPKG

@contentstack/management

Version:

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

270 lines (263 loc) 10 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import cloneDeep from 'lodash/cloneDeep'; import { create, fetch, update, query, deleteEntity, move, parseData } from '../../../entity'; export function Terms(http, data) { var _this = this; this.stackHeaders = data.stackHeaders; this.taxonomy_uid = data.taxonomy_uid; this.urlPath = "/taxonomies/".concat(this.taxonomy_uid, "/terms"); if (data && data.term) { Object.assign(this, cloneDeep(data.term)); this.urlPath = "/taxonomies/".concat(this.taxonomy_uid, "/terms/").concat(this.uid); /** * @description The Update terms call is used to update an existing term. * @memberof Terms * @func update * @returns {Promise<Terms.Terms>} Promise for Terms instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch() * .then((terms) => { * terms.name = 'terms name' * return terms.update() * }) * .then((terms) => console.log(terms)) * */ this.update = update(http, 'term'); /** * @description The Delete terms call is used to delete an existing term. * @memberof Terms * @func delete * @returns {Promise<Terms.Terms>} Response Object. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).terms('terms_uid').delete() * .then((response) => console.log(response.notice)) * */ this["delete"] = deleteEntity(http); /** * @description The Fetch terms call is used to fetch an existing term. * @memberof Terms * @func fetch * @returns {Promise<Terms.Terms>} Promise for Terms instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).terms('terms_uid').fetch() * .then((terms) => console.log(terms)) * */ this.fetch = fetch(http, 'term'); /** * @description The ancestors call is used to get all the ancestor terms of an existing term. * @memberof Terms * @func ancestors * @returns {Promise<Terms.Terms>} Promise for Terms instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).terms('terms_uid').ancestors() * .then((terms) => console.log(terms)) * */ this.ancestors = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var params, headers, response, _args = arguments, _t; return _regeneratorRuntime.wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: params = _args.length > 0 && _args[0] !== undefined ? _args[0] : {}; _context.prev = 1; headers = { headers: _objectSpread(_objectSpread({}, cloneDeep(_this.stackHeaders)), cloneDeep(params)) }; _context.next = 2; return http.get("".concat(_this.urlPath, "/ancestors"), headers); case 2: response = _context.sent; return _context.abrupt("return", parseData(response, _this.stackHeaders)); case 3: _context.prev = 3; _t = _context["catch"](1); console.error(_t); throw _t; case 4: case "end": return _context.stop(); } }, _callee, null, [[1, 3]]); })); /** * @description The move call is used to existing term. * @memberof Terms * @func descendants * @returns {Promise<Terms.Terms>} Promise for Terms instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).terms('terms_uid').descendants() * .then((terms) => console.log(terms)) * */ this.descendants = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() { var params, headers, response, _args2 = arguments, _t2; return _regeneratorRuntime.wrap(function (_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: params = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : {}; _context2.prev = 1; headers = { headers: _objectSpread(_objectSpread({}, cloneDeep(_this.stackHeaders)), cloneDeep(params)) }; _context2.next = 2; return http.get("".concat(_this.urlPath, "/descendants"), headers); case 2: response = _context2.sent; return _context2.abrupt("return", parseData(response, _this.stackHeaders)); case 3: _context2.prev = 3; _t2 = _context2["catch"](1); console.error(_t2); throw _t2; case 4: case "end": return _context2.stop(); } }, _callee2, null, [[1, 3]]); })); /** * @description The move call is used to update the parent uid. * @memberof Terms * @func anscestors * @returns {Promise<Terms.Terms>} Promise for Terms instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * const term = { * parent_uid: 'parent_uid', * order: 2 * } * client.stack({ api_key: 'api_key'}).terms('terms_uid').move(term) * .then((terms) => console.log(terms)) * */ this.move = move(http, 'term'); } else { /** * @description The Create terms call is used to create a terms. * @memberof Terms * @func create * @returns {Promise<Terms.Terms>} Promise for Terms instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * const terms = { * uid: 'terms_testing1', * name: 'terms testing', * description: 'Description for terms testing' * } * client.stack({ api_key: 'api_key'}).terms().create({terms}) * .then(terms) => console.log(terms) * */ this.create = create({ http: http }); /** * @description The Query on Terms will allow to fetch details of all Terms. * @memberof Terms * @param {Object} params - URI parameters * @prop {Object} params.query - Queries that you can use to fetch filtered results. * @func query * @returns {Array<Terms>} Array of Terms. * * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack().terms().query().find() * .then((terms) => console.log(terms) */ this.query = query({ http: http, wrapperCollection: TermsCollection }); } /** * @description The Search terms call is used to search a term. * @memberof Terms * @func search * @returns {Promise<Terms.Terms>} Promise for Terms instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * const term_string = '' * client.stack({ api_key: 'api_key'}).terms().search(term_string) * .then(terms) => console.log(terms) * */ this.search = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() { var term, params, headers, response, _args3 = arguments, _t3; return _regeneratorRuntime.wrap(function (_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: term = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : ''; params = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {}; _context3.prev = 1; headers = { headers: _objectSpread(_objectSpread({}, cloneDeep(_this.stackHeaders)), cloneDeep(params)) }; _context3.next = 2; return http.get("taxonomies/$all/terms?typeahead=".concat(term), headers); case 2: response = _context3.sent; return _context3.abrupt("return", parseData(response, _this.stackHeaders)); case 3: _context3.prev = 3; _t3 = _context3["catch"](1); console.error(_t3); throw _t3; case 4: case "end": return _context3.stop(); } }, _callee3, null, [[1, 3]]); })); } export function TermsCollection(http, data) { var obj = cloneDeep(data.terms) || []; var termsCollection = obj.map(function (term) { return new Terms(http, { term: term, taxonomy_uid: data.taxonomy_uid, stackHeaders: data.stackHeaders }); }); return termsCollection; }