@contentstack/management
Version:
The Content Management API is used to manage the content of your Contentstack account
195 lines (190 loc) • 7.64 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; }
import cloneDeep from 'lodash/cloneDeep';
import { update, deleteEntity } from '../entity';
import error from '../core/contentstackError';
import { OrganizationCollection } from '../organization';
/**
* All accounts registered with Contentstack are known as Users. A stack can have many users with varying permissions and roles. Read Users to learn more.
* @namespace User
*/
export function User(http, data) {
Object.assign(this, cloneDeep(data.user));
this.urlPath = '/user';
if (this.authtoken) {
/**
* @description The Update User API Request updates the details of an existing user account.
* @memberof User
* @func update
* @returns {Promise<User.User>} Promise for User instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* user.first_name = 'FirstName'
* user.last_name = 'LastName'
* user.company = 'Company Name'
* return user.update()
* })
* .then((user) => console.log(user))
*/
this.update = update(http, 'user');
/**
* @description The Delete user call deletes the current authenticated user permanently from your Contentstack account.
* @memberof User
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* return user.delete()
* })
* .then((response) => console.log(response.notice))
*/
this["delete"] = deleteEntity(http);
/**
* @description The Request for a password call sends a request for a temporary password to log in to an account in case a user has forgotten the login password.
* @memberof User
* @func requestPassword
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* return user.requestPassword()
* })
* .then((response) => console.log(response.notice))
*
*/
this.requestPassword = function () {
return http.post('/user/forgot_password', {
user: {
email: this.email
}
}).then(function (response) {
return response.data;
}, error);
};
/**
* @description The Reset password call sends a request for resetting the password of your Contentstack account.
* @memberof User
* @func resetPassword
* @param {Object} credentials - Password reset credentials.
* @prop {string} credentials.resetPasswordToken - Reset password token.
* @prop {string} credentials.password - New password.
* @prop {string} credentials.passwordConfirm - Password confirmation.
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* return user.resetPassword({ resetPasswordToken: 'token', password: 'new_password', passwordConfirm: 'new_password' })
* })
* .then((response) => console.log(response.notice))
*
*/
this.resetPassword = function (_ref) {
var resetPasswordToken = _ref.resetPasswordToken,
password = _ref.password,
passwordConfirm = _ref.passwordConfirm;
return http.post('/user/reset_password', {
user: {
reset_password_token: resetPasswordToken,
password: password,
password_confirmation: passwordConfirm
}
}).then(function (response) {
return response.data;
}, error);
};
if (this.organizations) {
this.organizations = new OrganizationCollection(http, {
organizations: this.organizations
});
}
/**
* @description The Get all Tasks request retrieves a list of all tasks assigned to you.
* @memberof User
* @func getTasks
* @async
* @param {Object} params - Query parameters
* @prop {Object} params.query - Enter the actual query that will be executed to retrieve the tasks. This query should be in JSON format.
* @prop {Object} params.sort - Enter the field UID on the basis of which you want to sort your tasks.
* @prop {Int} params.limit - Enter the maximum number of tasks that you want to retrieve in the response.
* @prop {Int} params.skip - Enter the number of tasks to be skipped.
* @returns {Promise<Object>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* return user.getTasks()
* })
* .then((response) => console.log(response.assignments))
*
*/
this.getTasks = /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
var headers, response, _t;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
headers = {};
if (params) {
headers.params = _objectSpread({}, cloneDeep(params));
}
_context.prev = 1;
_context.next = 2;
return http.get("/user/assignments", 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]]);
}));
return function (_x) {
return _ref2.apply(this, arguments);
};
}();
}
return this;
}
export function UserCollection(http, data) {
var users = data.collaborators || data.shares || [];
var obj = cloneDeep(users);
var userCollection = obj.map(function (userdata) {
return new User(http, {
user: userdata
});
});
return userCollection;
}