@contentstack/management
Version:
The Content Management API is used to manage the content of your Contentstack account
213 lines (207 loc) • 8.69 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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, update, deleteEntity, fetch, query, upload, parseData } from '../../entity';
import error from '../../core/contentstackError';
import FormData from 'form-data';
import { createReadStream } from 'fs';
/**
* GlobalField defines the structure or schema of a page or a section of your web or mobile property. To create global Fields for your application, you are required to first create a global field. Read more about <a href='https://www.contentstack.com/docs/guide/global-fields'>Global Fields</a>.
* @namespace GlobalField
*/
export function GlobalField(http) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var rawHeaders = data.stackHeaders || {};
this.stackHeaders = _objectSpread(_objectSpread(_objectSpread({}, cloneDeep(rawHeaders)), data.apiVersion ? {
api_version: data.apiVersion
} : {}), data.branch ? {
branch: data.branch
} : {});
this.urlPath = "/global_fields";
if (data.global_field && data.global_field.uid) {
Object.assign(this, cloneDeep(data.global_field));
this.urlPath = "/global_fields/".concat(this.uid);
/**
* @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
* @memberof GlobalField
* @func update
* @returns {Promise<GlobalField>} Promise for GlobalField instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').fetch()
* .then((globalField) => {
* globalField.title = 'My New Content Type'
* globalField.description = 'Content Type description'
* return globalField.update()
* })
* .then((globalField) => console.log(globalField))
*
*/
this.update = update(http, 'global_field');
/**
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
* @memberof GlobalField
* @func delete
* @returns {Promise<Object>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
* .then((response) => console.log(response.notice))
*/
this["delete"] = deleteEntity(http);
/**
* @description The fetch GlobalField call fetches GlobalField details.
* @memberof GlobalField
* @func fetch
* @returns {Promise<GlobalField>} Promise for GlobalField instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').fetch()
* .then((globalField) => console.log(globalField))
*
*/
this.fetch = fetch(http, 'global_field');
} else {
/**
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
* @memberof GlobalField
* @func create
* @returns {Promise<GlobalField>} Promise for GlobalField instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const global_field = {
* title: 'First',
* uid: 'first',
* schema: [{
* display_name: 'Name',
* uid: 'name',
* data_type: 'text'
* }]
* }
* client.stack().globalField().create({ global_field })
* .then((globalField) => console.log(globalField))
*/
this.create = create({
http: http
});
/**
* @description The Query on GlobalField will allow you to fetch details of all or specific GlobalFields.
* @memberof GlobalField
* @func query
* @param {Object} params - Query parameters
* @prop {Object} params.query - Queries that you can use to fetch filtered results.
* @returns {Object} Query builder object with find(), count(), and findOne() methods.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().globalField().query({ query: { name: 'Global Field Name' } }).find()
* .then((globalFields) => console.log(globalFields))
*/
this.query = query({
http: http,
wrapperCollection: GlobalFieldCollection,
apiVersion: this.apiVersion
});
/**
* @description The Import a global field call imports a global field into a stack.
* @memberof GlobalField
* @func import
* @async
* @param {Object} data - Import data object.
* @prop {string} data.global_field - Path to global field file.
* @param {Object=} params - Optional request parameters.
* @param {Object=} headers - Optional request headers.
* @returns {Promise<GlobalField>} Promise for GlobalField instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const data = {
* global_field: 'path/to/file.json',
* }
* client.stack({ api_key: 'api_key'}).globalField().import(data, { overwrite: true })
* .then((globalField) => console.log(globalField))
*
*/
this["import"] = /*#__PURE__*/function () {
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(data) {
var params,
headers,
response,
_args = arguments,
_t;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
params = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
headers = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
_context.prev = 1;
this.stackHeaders = _objectSpread(_objectSpread({}, this.stackHeaders), headers);
_context.next = 2;
return upload({
http: http,
urlPath: "".concat(this.urlPath, "/import"),
stackHeaders: this.stackHeaders,
formData: createFormData(data),
params: params
});
case 2:
response = _context.sent;
if (!response.data) {
_context.next = 3;
break;
}
return _context.abrupt("return", new this.constructor(http, parseData(response, this.stackHeaders)));
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, this, [[1, 5]]);
}));
return function (_x) {
return _ref.apply(this, arguments);
};
}();
}
return this;
}
export function GlobalFieldCollection(http, data) {
var obj = cloneDeep(data.global_fields) || [];
var globalFieldCollection = obj.map(function (userdata) {
return new GlobalField(http, {
global_field: userdata,
stackHeaders: data.stackHeaders
});
});
return globalFieldCollection;
}
export function createFormData(data) {
return function () {
var formData = new FormData();
var uploadStream = createReadStream(data.global_field);
formData.append('global_field', uploadStream);
return formData;
};
}