UNPKG

@contentstack/management

Version:

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

401 lines (392 loc) 14.7 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; } /* eslint-disable camelcase */ import cloneDeep from 'lodash/cloneDeep'; import { create, fetch, query, update, deleteEntity, upload, parseData } from '../../entity'; import { Terms, TermsCollection } from './terms'; import FormData from 'form-data'; import { createReadStream } from 'fs'; import error from '../../core/contentstackError'; export function Taxonomy(http) { var _this = this; var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; this.stackHeaders = data.stackHeaders; this.urlPath = "/taxonomies"; if (data.taxonomy) { Object.assign(this, cloneDeep(data.taxonomy)); if (data.taxonomy.terms) { this.terms = new TermsCollection(http, { terms: data.taxonomy.terms, stackHeaders: data.stackHeaders }, this.uid); } this.urlPath = "/taxonomies/".concat(this.uid); /** * @description The Update taxonomy call is used to update an existing taxonomy. * @memberof Taxonomy * @func update * @returns {Promise<Taxonomy>} Promise for Taxonomy instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').fetch() * .then((taxonomy) => { * taxonomy.name = 'taxonomy name' * return taxonomy.update() * }) * .then((taxonomy) => console.log(taxonomy)) * */ this.update = update(http, 'taxonomy'); /** * @description The Delete taxonomy call is used to delete an existing taxonomy. * @memberof Taxonomy * @func delete * @returns {Promise<Object>} Response Object. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').delete() * .then((response) => console.log(response.notice)) * */ this["delete"] = deleteEntity(http); /** * @description The Fetch taxonomy call is used to fetch an existing taxonomy. * @memberof Taxonomy * @func fetch * @returns {Promise<Taxonomy>} Promise for Taxonomy instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').fetch() * .then((taxonomy) => console.log(taxonomy)) * */ this.fetch = fetch(http, 'taxonomy'); /** * @description The Export taxonomy call is used to export an existing taxonomy. * @memberof Taxonomy * @func export * @async * @param {Object=} params - Optional query parameters. * @returns {Promise<Object>} Promise for taxonomy export data * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').export() * .then((taxonomy) => console.log(taxonomy)) * */ this["export"] = /*#__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({}, cloneDeep(_this.stackHeaders)), params: params }; _context.next = 2; return http.get("".concat(_this.urlPath, "/export"), headers); case 2: response = _context.sent; if (!response.data) { _context.next = 3; break; } return _context.abrupt("return", response.data); case 3: throw error(response); case 4: _context.next = 6; break; case 5: _context.prev = 5; _t = _context["catch"](1); throw error(_t); case 6: case "end": return _context.stop(); } }, _callee, null, [[1, 5]]); })); /** * @description The Get taxonomy locales call is used to fetch a taxonomy in all locales where it's localized. * @memberof Taxonomy * @func locales * @async * @param {Object=} params - Optional query parameters. * @returns {Promise<Object>} Promise for taxonomy locales response * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).taxonomy('taxonomyUid').locales() * .then((response) => console.log(response.taxonomies)) * */ this.locales = /*#__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({}, cloneDeep(_this.stackHeaders)), params: params }; _context2.next = 2; return http.get("".concat(_this.urlPath, "/locales"), headers); case 2: response = _context2.sent; if (!response.data) { _context2.next = 3; break; } return _context2.abrupt("return", response.data); case 3: throw error(response); case 4: _context2.next = 6; break; case 5: _context2.prev = 5; _t2 = _context2["catch"](1); throw error(_t2); case 6: case "end": return _context2.stop(); } }, _callee2, null, [[1, 5]]); })); /** * @description The Localize taxonomy call is used to create a localized version of a taxonomy. * @memberof Taxonomy * @func localize * @async * @param {Object} data - The localization data containing taxonomy object * @param {Object} params - Query parameters including locale * @returns {Promise<Taxonomy>} Promise for Taxonomy instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_UID').localize({ taxonomy: data }, { locale: 'hi-in' }) * .then((taxonomy) => console.log(taxonomy)) * */ this.localize = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(data) { var params, headers, response, _args3 = arguments, _t3; return _regeneratorRuntime.wrap(function (_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: params = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {}; _context3.prev = 1; headers = { headers: _objectSpread({}, cloneDeep(_this.stackHeaders)) }; _context3.next = 2; return http.post(_this.urlPath, data, _objectSpread(_objectSpread({}, headers), {}, { params: params })); case 2: response = _context3.sent; if (!response.data) { _context3.next = 3; break; } return _context3.abrupt("return", new _this.constructor(http, parseData(response, _this.stackHeaders))); case 3: throw error(response); case 4: _context3.next = 6; break; case 5: _context3.prev = 5; _t3 = _context3["catch"](1); throw error(_t3); case 6: case "end": return _context3.stop(); } }, _callee3, null, [[1, 5]]); })); return function (_x) { return _ref3.apply(this, arguments); }; }(); /** * @description The terms call returns a Terms instance for managing terms of a taxonomy. * @memberof Taxonomy * @func terms * @param {String=} uid - Term UID. If not provided, returns Terms instance for querying all terms. * @returns {Terms} Instance of Terms. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').terms('term_uid').fetch() * .then((term) => console.log(term)) */ this.terms = function () { var uid = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var data = { stackHeaders: _this.stackHeaders }; data.taxonomy_uid = _this.uid; if (uid) { data.term = { uid: uid }; } return new Terms(http, data); }; } else { /** * @description The Create taxonomy call is used to create a taxonomy. * @memberof Taxonomy * @func create * @returns {Promise<Taxonomy>} Promise for Taxonomy instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * const taxonomy = { * uid: 'taxonomy_testing1', * name: 'taxonomy testing', * description: 'Description for Taxonomy testing' * } * client.stack({ api_key: 'api_key'}).taxonomy().create({ taxonomy }) * .then((taxonomy) => console.log(taxonomy)) * */ this.create = create({ http: http }); /** * @description The Query on Taxonomy will allow you to fetch details of all Taxonomies. * @memberof Taxonomy * @param {Object} params - URI parameters * @prop {Object} params.query - Queries that you can use to fetch filtered results. * @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().taxonomy().query().find() * .then((taxonomies) => console.log(taxonomies)) */ this.query = query({ http: http, wrapperCollection: TaxonomyCollection }); /** * @description The 'Import taxonomy' call imports a taxonomy by uploading JSON or CSV files. * @memberof Taxonomy * @func import * @async * @param {Object} data - Import data object. * @prop {string} data.taxonomy - Path to taxonomy file. * @param {Object=} params - Optional request parameters. * @returns {Promise<Taxonomy>} Promise for Taxonomy instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * const data = { * taxonomy: 'path/to/file.json', * } * // Import a Taxonomy * client.stack({ api_key: 'api_key'}).taxonomy().import(data) * .then((taxonomy) => console.log(taxonomy)) */ this["import"] = /*#__PURE__*/function () { var _ref4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4(data) { var params, response, _args4 = arguments, _t4; return _regeneratorRuntime.wrap(function (_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: params = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {}; _context4.prev = 1; _context4.next = 2; return upload({ http: http, urlPath: "".concat(this.urlPath, "/import"), stackHeaders: this.stackHeaders, formData: createFormData(data), params: params }); case 2: response = _context4.sent; if (!response.data) { _context4.next = 3; break; } return _context4.abrupt("return", new this.constructor(http, parseData(response, this.stackHeaders))); case 3: throw error(response); case 4: _context4.next = 6; break; case 5: _context4.prev = 5; _t4 = _context4["catch"](1); throw error(_t4); case 6: case "end": return _context4.stop(); } }, _callee4, this, [[1, 5]]); })); return function (_x2) { return _ref4.apply(this, arguments); }; }(); } } export function TaxonomyCollection(http, data) { var obj = cloneDeep(data.taxonomies) || []; var taxonomyCollection = obj.map(function (userdata) { return new Taxonomy(http, { taxonomy: userdata, stackHeaders: data.stackHeaders }); }); return taxonomyCollection; } export function createFormData(data) { return function () { var formData = new FormData(); var uploadStream = createReadStream(data.taxonomy); formData.append('taxonomy', uploadStream); return formData; }; }