Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 1 addition & 26 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,6 @@ export const create = ({ http, params }) => {
}
}

export const exportObject = ({ http }) => {
return async function (param) {
const headers = {
params: {
...cloneDeep(param)
},
headers: {
...cloneDeep(this.stackHeaders)
}
} || {}
try {
const response = await http.get(this.urlPath, headers)
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
}

export const query = ({ http, wrapperCollection }) => {
return function (params = {}) {
const headers = {
Expand Down Expand Up @@ -181,9 +158,7 @@ export const deleteEntity = (http, force = false, params = {}) => {
try {
const headers = {
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) },
params: {
...cloneDeep(param)
}
params: { ...cloneDeep(param) }
} || {}
if (force === true) {
headers.params.force = true
Expand Down
34 changes: 17 additions & 17 deletions lib/stack/bulkOperation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ export function BulkOperation (http, data = {}) {
* // Bulk nested publish
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* {
* environments:["{{env_uid}}","{{env_uid}}"],
* locales:["en-us"],
* items:[
* {
* _content_type_uid: '{{content_type_uid}}',
* uid: '{{entry_uid}}'
* },
* {
* _content_type_uid: '{{content_type_uid}}',
* uid: '{{entry_uid}}'
* },
* {
* _content_type_uid: '{{content_type_uid}}',
* uid: '{{entry_uid}}'
* }
* ]
* const publishDetails = {
* environments:["{{env_uid}}","{{env_uid}}"],
* locales:["en-us"],
* entries:[
* {
* _content_type_uid: '{{content_type_uid}}',
* uid: '{{entry_uid}}'
* },
* {
* _content_type_uid: '{{content_type_uid}}',
* uid: '{{entry_uid}}'
* },
* {
* _content_type_uid: '{{content_type_uid}}',
* uid: '{{entry_uid}}'
* }
* ]
* }
* client.stack({ api_key: 'api_key'}).bulkOperation().publish({ details: publishDetails, is_nested: true })
* .then((response) => { console.log(response.notice) })
Expand Down
74 changes: 37 additions & 37 deletions lib/stack/contentType/entry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,46 +266,46 @@ export function Entry (http, data) {
}
}
} else {
/**
* @description The Create an entry call creates a new entry for the selected content type.
* @memberof Entry
* @func create
* @returns {Promise<Entry.Entry>} Promise for Entry instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const entry = {
* title: 'Sample Entry',
* url: '/sampleEntry',
* file: 'asset_uid', // for single asset pass asset uid to entry asset field value
* multiple_file = ['asset_uid_1', 'asset_uid_2'], // for multiple asset pass array of asset uid to entry asset field values
* reference: reference.uid, // for single reference pass reference uid to entry reference field value
* multiple_reference: ['reference_uid_1', 'reference_uid_2'], // for multiple reference pass array of reference uid to entry reference field values
* multiple_content_type_reference: [{_content_type_uid: 'content_type_uid_1', uid: 'reference_uid_1'}, {_content_type_uid: 'content_type_uid_2', uid: 'reference_uid_2'}] // for multiple reference pass array of reference uid to entry reference field values
* }
* client.stack().contentType('content_type_uid').entry().create({ entry })
* .then((entry) => console.log(entry))
*/
/**
* @description The Create an entry call creates a new entry for the selected content type.
* @memberof Entry
* @func create
* @returns {Promise<Entry.Entry>} Promise for Entry instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const entry = {
* title: 'Sample Entry',
* url: '/sampleEntry',
* file: 'asset_uid', // for single asset pass asset uid to entry asset field value
* multiple_file = ['asset_uid_1', 'asset_uid_2'], // for multiple asset pass array of asset uid to entry asset field values
* reference: reference.uid, // for single reference pass reference uid to entry reference field value
* multiple_reference: ['reference_uid_1', 'reference_uid_2'], // for multiple reference pass array of reference uid to entry reference field values
* multiple_content_type_reference: [{_content_type_uid: 'content_type_uid_1', uid: 'reference_uid_1'}, {_content_type_uid: 'content_type_uid_2', uid: 'reference_uid_2'}] // for multiple reference pass array of reference uid to entry reference field values
* }
* client.stack().contentType('content_type_uid').entry().create({ entry })
* .then((entry) => console.log(entry))
*/
this.create = create({ http: http })

/**
* @description The Query on Entry will allow to fetch details of all or specific Entry
* @memberof Entry
* @func query
* @param {Int} locale Enter the code of the language of which the entries need to be included. Only the entries published in this locale will be displayed.
* @param {Int} include_workflow Enter 'true' to include the workflow details of the entry.
* @param {Int} include_publish_details Enter 'true' to include the publish details of the entry.
* @param {Object} query Queries that you can use to fetch filtered results.
* @returns {Array<Entry>} Array of Entry.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().contentType('content_type_uid').entry().query({ query: { title: 'Entry title' } }).find()
* .then((entries) => console.log(entries))
*/
* @description The Query on Entry will allow to fetch details of all or specific Entry
* @memberof Entry
* @func query
* @param {Int} locale Enter the code of the language of which the entries need to be included. Only the entries published in this locale will be displayed.
* @param {Int} include_workflow Enter 'true' to include the workflow details of the entry.
* @param {Int} include_publish_details Enter 'true' to include the publish details of the entry.
* @param {Object} query Queries that you can use to fetch filtered results.
* @returns {Array<Entry>} Array of Entry.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().contentType('content_type_uid').entry().query({ query: { title: 'Entry title' } }).find()
* .then((entries) => console.log(entries))
*/
this.query = query({ http: http, wrapperCollection: EntryCollection })
}

Expand Down
9 changes: 5 additions & 4 deletions lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function Stack (http, data) {
if (data && data.stack && data.stack.api_key) {
this.stackHeaders = { api_key: this.api_key }
if (this.management_token && this.management_token !== undefined) {
this.stackHeaders.authorization = this.management_token
// this.stackHeaders.authorization = this.management_token
http.defaults.headers.authorization = this.management_token
delete this.management_token
}

Expand Down Expand Up @@ -452,17 +453,17 @@ export function Stack (http, data) {
* .then((response) => console.log(response.notice))
*
*/
this.updateUsersRoles = async (users) => {
this.updateUsersRoles = async (data) => {
try {
const response = await http.post(`${this.urlPath}/users/roles`,
{ users },
{ users: data },
{
headers: {
...cloneDeep(this.stackHeaders)
}
})
if (response.data) {
return UserCollection(http, response.data.stack)
return response.data
} else {
return error(response)
}
Expand Down
10 changes: 5 additions & 5 deletions test/api/stack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Stack api Test', () => {
})

it('Update Stack details', done => {
const name = 'My New Stack Update Name'
const name = 'Conference Demo'
const description = 'My New description stack'
client.stack({ api_key: stacks.api_key })
.fetch().then((stack) => {
Expand Down Expand Up @@ -130,13 +130,13 @@ describe('Stack api Test', () => {

it('Get query stack', done => {
client.stack()
.query({ query: { name: 'Conference Demo' } })
.query({ query: { name: 'My New Stack Update Name' } })
.find()
.then((response) => {
expect(response.items.length).to.be.equal(1)
for (const index in response.items) {
const stack = response.items[index]
expect(stack.name).to.be.equal('Conference Demo')
expect(stack.name).to.be.equal('My New Stack Update Name')
}
done()
})
Expand All @@ -145,12 +145,12 @@ describe('Stack api Test', () => {

it('Find one stack', done => {
client.stack()
.query({ query: { name: 'Conference Demo' } })
.query({ query: { name: 'My New Stack Update Name' } })
.findOne()
.then((response) => {
const stack = response.items[0]
expect(response.items.length).to.be.equal(1)
expect(stack.name).to.be.equal('Conference Demo')
expect(stack.name).to.be.equal('My New Stack Update Name')
done()
})
.catch(done)
Expand Down
2 changes: 1 addition & 1 deletion test/api/user-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var loggedinUserID = ''
var client = contentstackClient()
describe('Contentstack User Session api Test', () => {
it('User login wrong credentials', done => {
contentstackClient().login({ email: process.env.EMAIL, password: process.env.PASSWORDWRONG })
contentstackClient().login({ email: process.env.EMAIL, password: process.env.PASSWORD })
.then((response) => {
console.log(response)
done()
Expand Down
69 changes: 22 additions & 47 deletions test/unit/ContentstackClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import axios from 'axios'
import httpAdapter from 'axios/lib/adapters/http'
import ContentstackClient from '../../lib/contentstackClient'
import { expect } from 'chai'
import { describe, it, after, beforeEach } from 'mocha'
import nock from 'nock'
import { describe, it, beforeEach } from 'mocha'
import MockAdapter from 'axios-mock-adapter'
var host = 'http://localhost/'

describe('Contentstack Client', () => {
Expand All @@ -12,10 +12,6 @@ describe('Contentstack Client', () => {
axios.defaults.host = host
axios.defaults.adapter = httpAdapter
})
after('all', function () {
nock.restore()
nock.cleanAll()
})

it('Contentstack Client Object successful', done => {
var contentstackClient = ContentstackClient({ http: axios })
Expand All @@ -24,30 +20,12 @@ describe('Contentstack Client', () => {
})

it('Contentstack Client login success', done => {
nock(host)
.post('/user-session', {})
.reply(200, {
user: {
authtoken: 'Test Auth'
}
})
ContentstackClient({ http: axios })
.login()
.then((response) => {
expect(response.user.authtoken).to.be.equal('Test Auth')
done()
})
.catch(done)
})

it('Contentstack Client login success', done => {
nock(host)
.post('/user-session', {})
.reply(200, {
user: {
authtoken: 'Test Auth'
}
})
var mock = new MockAdapter(axios)
mock.onPost('/user-session').reply(200, {
user: {
authtoken: 'Test Auth'
}
})
ContentstackClient({ http: axios })
.login()
.then((response) => {
Expand All @@ -58,13 +36,12 @@ describe('Contentstack Client', () => {
})

it('Contentstack Client get user info', done => {
nock(host)
.get('/user')
.reply(200, {
user: {
uid: 'test uid'
}
})
var mock = new MockAdapter(axios)
mock.onGet('/user').reply(200, {
user: {
uid: 'test uid'
}
})
ContentstackClient({ http: axios })
.getUser()
.then((user) => {
Expand All @@ -75,11 +52,10 @@ describe('Contentstack Client', () => {
})

it('Contentstack Client Logout with Authtoken', done => {
nock(host)
.delete('/user-session')
.reply(200, {
notice: 'You\'ve logged out successfully'
})
var mock = new MockAdapter(axios)
mock.onDelete('/user-session').reply(200, {
notice: 'You\'ve logged out successfully'
})
ContentstackClient({ http: axios })
.logout('Test Auth')
.then((response) => {
Expand All @@ -90,11 +66,10 @@ describe('Contentstack Client', () => {
})

it('Contentstack Client Logout', done => {
nock(host)
.delete('/user-session')
.reply(200, {
notice: 'You\'ve logged out successfully'
})
var mock = new MockAdapter(axios)
mock.onDelete('/user-session').reply(200, {
notice: 'You\'ve logged out successfully'
})
axios.defaults.headers = {
common: {
authtoken: 'Authtoken'
Expand Down
27 changes: 22 additions & 5 deletions test/unit/Util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,28 @@ describe('Get User Agent', () => {
platform: 'MacIntel'
}
}
const userAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
expect(userAgent.match(headerRegEx).length).to.be.equal(5)
expect(userAgent.indexOf('platform browser')).to.not.equal(-1)
expect(userAgent.indexOf('os macOS;')).to.not.equal(-1)
const macUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
expect(macUserAgent.match(headerRegEx).length).to.be.equal(5)
expect(macUserAgent.indexOf('platform browser')).to.not.equal(-1)
expect(macUserAgent.indexOf('os macOS;')).to.not.equal(-1)

global.window.navigator.platform = 'Windows'
const windowsUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
expect(windowsUserAgent.match(headerRegEx).length).to.be.equal(5)
expect(windowsUserAgent.indexOf('platform browser')).to.not.equal(-1)
expect(windowsUserAgent.indexOf('os Windows;')).to.not.equal(-1)

global.window.navigator = { userAgent: 'Android' }
const androidUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
expect(androidUserAgent.match(headerRegEx).length).to.be.equal(5)
expect(androidUserAgent.indexOf('platform browser')).to.not.equal(-1)
expect(androidUserAgent.indexOf('os Android;')).to.not.equal(-1)

global.window.navigator = { platform: 'Linux' }
const linuxUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
expect(linuxUserAgent.match(headerRegEx).length).to.be.equal(5)
expect(linuxUserAgent.indexOf('platform browser')).to.not.equal(-1)
expect(linuxUserAgent.indexOf('os Linux;')).to.not.equal(-1)
done()

getUserAgentRewireApi.__ResetDependency__('isNode')
Expand All @@ -40,7 +58,6 @@ describe('Get User Agent', () => {
global.window = {}

const userAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')

expect(userAgent.match(headerRegEx).length).to.be.equal(3)
expect(userAgent.indexOf('platform browser')).to.equal(-1)
expect(userAgent.indexOf('os macOS;')).to.equal(-1)
Expand Down
Loading