-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from contentstack/feat/cs-41208-taxonomy-support
feat/cs 41208 Taxonomy support
- Loading branch information
Showing
7 changed files
with
370 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import cloneDeep from 'lodash/cloneDeep' | ||
import { | ||
create, | ||
fetch, | ||
query, | ||
update, | ||
deleteEntity | ||
} from '../../entity' | ||
|
||
export function Taxonomy (http, data) { | ||
this.stackHeaders = data.stackHeaders | ||
this.urlPath = `/taxonomies` | ||
|
||
if (data.taxonomy) { | ||
Object.assign(this, cloneDeep(data.taxonomy)) | ||
this.urlPath = `/taxonomies/${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('taxonomy_uid').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('taxonomy_uid').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('taxonomy_uid').fetch() | ||
* .then((taxonomy) => console.log(taxonomy)) | ||
* | ||
*/ | ||
this.fetch = fetch(http, 'taxonomy') | ||
} 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 }) | ||
|
||
/** | ||
* @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 }) | ||
} | ||
} | ||
export function TaxonomyCollection (http, data) { | ||
const obj = cloneDeep(data.taxonomy) || [] | ||
const taxonomyCollection = obj.map((userdata) => { | ||
return new Taxonomy(http, { taxonomy: userdata, stackHeaders: data.stackHeaders }) | ||
}) | ||
return taxonomyCollection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { expect } from 'chai' | ||
import { describe, it, setup } from 'mocha' | ||
import { jsonReader } from '../utility/fileOperations/readwrite' | ||
import { contentstackClient } from '../utility/ContentstackClient.js' | ||
|
||
var client = {} | ||
var stack = {} | ||
|
||
const taxonomy = { | ||
uid: 'taxonomy_testing1', | ||
name: 'taxonomy testing', | ||
description: 'Description for Taxonomy testing' | ||
} | ||
|
||
var taxonomyUID = '' | ||
var taxonomyDelUID = 'taxonomy_testing' | ||
|
||
describe('taxonomy api Test', () => { | ||
setup(() => { | ||
const user = jsonReader('loggedinuser.json') | ||
stack = jsonReader('stack.json') | ||
client = contentstackClient(user.authtoken) | ||
}) | ||
|
||
it('Create taxonomy', done => { | ||
makeTaxonomy() | ||
.create([{ taxonomy }]) | ||
.then((taxonomyResponse) => { | ||
expect(taxonomyResponse.name).to.be.equal(taxonomy.name) | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
|
||
it('Fetch taxonomy from uid', done => { | ||
makeTaxonomy(taxonomyUID) | ||
.fetch() | ||
.then((taxonomyResponse) => { | ||
expect(taxonomyResponse.uid).to.be.equal(taxonomyUID) | ||
expect(taxonomyResponse.name).to.be.not.equal(null) | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
|
||
it('Update taxonomy from uid', done => { | ||
makeTaxonomy(taxonomyUID) | ||
.fetch() | ||
.then((taxonomyResponse) => { | ||
taxonomyResponse.name = 'Updated Name' | ||
return taxonomyResponse.update() | ||
}) | ||
.then((taxonomyResponse) => { | ||
expect(taxonomyResponse.uid).to.be.equal(taxonomyUID) | ||
expect(taxonomyResponse.name).to.be.equal('Updated Name') | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
|
||
it('Delete taxonomy from uid', done => { | ||
makeTaxonomy(taxonomyDelUID) | ||
.delete() | ||
.then((taxonomyResponse) => { | ||
expect(taxonomyResponse.notice).to.be.equal('Taxonomy deleted successfully.') | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
|
||
it('Query to get all taxonomies', async () => { | ||
makeTaxonomy() | ||
.query() | ||
.find() | ||
.then((response) => { | ||
response.items.forEach((taxonomyResponse) => { | ||
expect(taxonomyResponse.uid).to.be.not.equal(null) | ||
expect(taxonomyResponse.name).to.be.not.equal(null) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
function makeTaxonomy (uid = null) { | ||
return client.stack({ api_key: stack.api_key }).taxonomy(uid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.