@contentstack/management
Version:
The Content Management API is used to manage the content of your Contentstack account
264 lines (258 loc) • 9.6 kB
JavaScript
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.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<Taxonomy.Taxonomy>} 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.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
* @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance
* @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]]);
}));
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.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 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 {Array<Taxonomy>} Array of Taxonomy.
*
* @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' import a single entry by uploading JSON or CSV files.
* @memberof Taxonomy
* @func import
* @param {String} data.taxonomy path to file
* @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 _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(data) {
var params,
response,
_args2 = arguments,
_t2;
return _regeneratorRuntime.wrap(function (_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
params = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
_context2.prev = 1;
_context2.next = 2;
return upload({
http: http,
urlPath: "".concat(this.urlPath, "/import"),
stackHeaders: this.stackHeaders,
formData: createFormData(data),
params: params
});
case 2:
response = _context2.sent;
if (!response.data) {
_context2.next = 3;
break;
}
return _context2.abrupt("return", new this.constructor(http, parseData(response, this.stackHeaders)));
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, this, [[1, 5]]);
}));
return function (_x) {
return _ref2.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;
};
}