Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

taxonomy and terms implementation in types #86

Merged
merged 9 commits into from
Oct 13, 2023
2 changes: 1 addition & 1 deletion lib/stack/taxonomy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function Taxonomy (http, data = {}) {
}
}
export function TaxonomyCollection (http, data) {
const obj = cloneDeep(data.taxonomy) || []
const obj = cloneDeep(data.taxonomies) || []
const taxonomyCollection = obj.map((userdata) => {
return new Taxonomy(http, { taxonomy: userdata, stackHeaders: data.stackHeaders })
})
Expand Down
2 changes: 1 addition & 1 deletion lib/stack/taxonomy/terms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export function Terms (http, data) {
const headers = {
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) }
}
const response = await http.get(`taxonomies/${this.taxonomy_uid}/terms?term=${term}`, headers)
const response = await http.get(`taxonomies/$all/terms?typeahead=${term}`, headers)
return parseData(response, this.stackHeaders)
} catch (err) {
console.error(err)
Expand Down
5 changes: 5 additions & 0 deletions test/typescript/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { createApp, deleteApp, fetchApp, installation, updateApp, updateAuth } f
import { deployment, hosting } from './hosting';
import { orgAppRequest } from './app-request';
import { authorization } from './authorization';
import { testTaxonomy } from './taxonomy';
import { testTerm } from './terms';
dotenv.config()
jest.setTimeout(10000);

Expand Down Expand Up @@ -100,6 +102,9 @@ describe('Typescript API test', () => {
deleteAsset(stack)
queryOnAsset(stack)

testTaxonomy(stack)
testTerm(stack)

deleteEnvironment(stack)

logout(client)
Expand Down
68 changes: 68 additions & 0 deletions test/typescript/taxonomy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expect } from "chai";
import { Stack } from "../../types/stack";

var taxonomyUID = ''
export function testTaxonomy(stack: Stack) {
describe('Taxonomy API test', () => {
test('Create taxonomy', done => {
const taxonomy = {
uid: 'uid',
name: 'taxonomy',
description: 'Description for Taxonomy'
}
stack.taxonomy().create({taxonomy})
.then((taxonomyResponse) => {
console.log(taxonomyResponse)
expect(taxonomyResponse.uid).to.be.equal(taxonomy.uid)
expect(taxonomyResponse.name).to.be.equal(taxonomy.name)
done()
})
.catch(done)
})
test('Fetch taxonomy from uid', done => {
stack.taxonomy(taxonomyUID).fetch()
.then((taxonomyResponse) => {
console.log(taxonomyResponse)
expect(taxonomyResponse.uid).to.be.equal(taxonomyUID)
expect(taxonomyResponse.name).not.to.be.equal('a')
done()
})
.catch(done)
})
test('Update taxonomy from uid', done => {
stack.taxonomy(taxonomyUID)
.fetch()
.then((taxonomyResponse) => {
taxonomyResponse.name = 'updated name'
return taxonomyResponse.update()
})
.then((taxonomyResponse) => {
console.log(taxonomyResponse)
expect(taxonomyResponse.uid).to.be.equal(taxonomyUID)
expect(taxonomyResponse.name).to.be.equal('updated name')
done()
})
.catch(done)
})
test('Delete taxonomy from uid', done => {
stack.taxonomy(taxonomyUID)
.delete()
.then((taxonomyResponse) => {
expect(taxonomyResponse.notice).to.be.equal('Taxonomy deleted successfully.')
done()
})
.catch(done)
})
test('Query to get all taxonomies', async () => {
await stack.taxonomy()
.query()
.find()
.then((response) => {
response.items.forEach((taxonomyResponse) => {
expect(taxonomyResponse.uid).to.be.not.equal(null)
expect(taxonomyResponse.name).to.be.not.equal(null)
})
})
})
})
}
118 changes: 118 additions & 0 deletions test/typescript/terms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { expect } from "chai";
import { Stack } from "../../types/stack";
var taxonomyUID = ''
var termUID = ''
export function testTerm(stack: Stack) {
describe('Term API test', () => {
test('Create Term', done => {
const term = {
uid: 'term_uid',
name: 'term name',
parent_uid: 'parent_uid',
order: 1
}
stack.taxonomy(taxonomyUID).terms().create({term})
.then((termResponse) => {
expect(termResponse.uid).to.be.equal(term.uid)
expect(termResponse.name).to.be.equal(term.name)
done()
})
.catch((err) => {
console.log(err)
})
})
test('Fetch term from uid', done => {
stack.taxonomy(taxonomyUID).terms(termUID).fetch()
.then((termResponse) => {
expect(termResponse.uid).to.be.equal(termUID)
expect(termResponse.name).not.to.be.equal(null)
done()
})
.catch(done)
})
test('Update term from uid', done => {
stack.taxonomy(taxonomyUID).terms(termUID)
.fetch()
.then((termResponse) => {
termResponse.name = 'Updated Name'
return termResponse.update()
})
.then((termResponse) => {
expect(termResponse.uid).to.be.equal(termUID)
expect(termResponse.name).to.be.equal('Updated Name')
done()
})
.catch(done)
})
test('Delete term from uid', done => {
stack.taxonomy(taxonomyUID).terms(termUID)
.delete()
.then((termResponse) => {
expect(termResponse.notice).to.be.equal('Term deleted successfully.')
done()
})
.catch(done)
})
test('Query to get all Terms', done => {
stack.taxonomy(taxonomyUID).terms()
.query()
.find()
.then((response) => {
response.items.forEach((termResponse) => {
expect(termResponse.uid).to.be.not.equal(null)
expect(termResponse.name).to.be.not.equal(null)
})
done()
})
.catch(done)
})
test('Ancestors of the term given', done => {
stack.taxonomy(taxonomyUID).terms(termUID)
.ancestors()
.then((termResponse) => {
expect(termResponse.terms[0].uid).not.to.be.equal(null)
expect(termResponse.terms[0].name).not.to.be.equal(null)
expect(termResponse.terms[0].created_by).not.to.be.equal(null)
expect(termResponse.terms[0].updated_by).not.to.be.equal(null)
done()
})
.catch(done)
})
test('Descendants of the term given', done => {
stack.taxonomy(taxonomyUID).terms(termUID)
.descendants()
.then((termResponse) => {
expect(termResponse.terms[0].uid).not.to.be.equal(null)
expect(termResponse.terms[0].name).not.to.be.equal(null)
expect(termResponse.terms[0].created_by).not.to.be.equal(null)
expect(termResponse.terms[0].updated_by).not.to.be.equal(null)
done()
})
.catch(done)
})
it('search term', done => {
const typeahead = 'term_string'
stack.taxonomy('$all').terms()
.search(typeahead)
.then((termResponse) => {
expect(termResponse.terms).to.be.an('array')
done()
})
.catch((err) => {console.log(err)})

})
it('move term', done => {
const term = {
parent_uid: 'parent_uid',
order: 2
}
stack.taxonomy(taxonomyUID).terms(termUID)
.move({ term })
.then((termResponse) => {
expect(termResponse.notice).to.be.equal('Term moved successfully.')
done()
})
.catch(done)
})
})
}
2 changes: 1 addition & 1 deletion test/unit/taxonomy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Contentstack Taxonomy test', () => {
it('Taxonomies query test', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/taxonomies').reply(200, {
taxonomy: [
taxonomies: [
taxonomyMock
]
})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/terms-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('Contentstack Term test', () => {
})
it('term search test', done => {
var mock = new MockAdapter(Axios)
mock.onGet(`/taxonomies/taxonomy_uid/terms?term=UID`).reply(200, {
mock.onGet(`/taxonomies/$all/terms?typeahead=UID`).reply(200, {
term: {
...termsMock
}
Expand Down
4 changes: 4 additions & 0 deletions types/stack/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Release, Releases } from "./release";
import { Role, Roles } from "./role";
import { Webhook, Webhooks } from "./webhook";
import { Workflow, Workflows } from "./workflow";
import { Taxonomy, Taxonomies } from "./taxonomy";

export interface StackConfig {
api_key:string
Expand Down Expand Up @@ -92,4 +93,7 @@ export interface Stack extends SystemFields {
unShare(email: string): Promise<Response>
role(): Roles
role(uid: string): Role

taxonomy(): Taxonomies
taxonomy(uid: string): Taxonomy
}
20 changes: 20 additions & 0 deletions types/stack/taxonomy/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { AnyProperty, SystemFields } from "../../utility/fields";
import { Creatable, Queryable, SystemFunction } from "../../utility/operations";
import { Term, Terms } from "../taxonomy/terms"

export interface Taxonomy extends SystemFields, SystemFunction<Taxonomy> {
terms(): Terms
terms(uid: string): Term
}

export interface Taxonomies extends Queryable<Taxonomy, {taxonomy: TaxonomyData}> {
}

export interface Taxonomies extends Creatable<Taxonomy, {taxonomy: TaxonomyData}> {
}

export interface TaxonomyData extends AnyProperty {
name: string
nadeem-cs marked this conversation as resolved.
Show resolved Hide resolved
uid: string
description: string
}
24 changes: 24 additions & 0 deletions types/stack/taxonomy/terms/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AnyProperty, SystemFields } from "../../../utility/fields";
import { Creatable, Queryable, Searchable, SystemFunction } from "../../../utility/operations";

export interface Term extends SystemFields, SystemFunction<Term> {
ancestors(data?: { include_children_count?: boolean, include_referenced_entries_count?: boolean, include_count?: boolean, skip?: number, limit?: number}): Promise<Term>
descendants(data?: { include_children_count?: boolean, include_referenced_entries_count?: boolean, include_count?: boolean, skip?: number, limit?: number}): Promise<Term>
move(data: { term: { parent_uid?: string, order: number } }, force?: boolean): Promise<Term>
}

export interface Terms extends Creatable<Term, {term: TermData}> {
}

export interface Terms extends Searchable<Term, string> {
}

export interface Terms extends Queryable<Term, {term: TermData}> {
}

export interface TermData extends AnyProperty {
name: string
uid: string
parent_uid?: string
order: number
}
4 changes: 4 additions & 0 deletions types/utility/operations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface Creatable<T, D> {
create(data: D, param?: AnyProperty): Promise<T>
}

export interface Searchable<T, D> {
search(string: D, param?: AnyProperty): Promise<T>
}

export interface SystemFunction<T> {
update(param?: AnyProperty): Promise<T>
fetch(param?: AnyProperty): Promise<T>
Expand Down
Loading