From 240d7f5472ce1c843128ba028a9fe3f70efc8dfd Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Mon, 13 Oct 2025 14:00:32 +0530 Subject: [PATCH] feat: Implement Taxonomy class and update TaxonomyQuery for enhanced taxonomy management --- src/index.ts | 1 + src/lib/stack.ts | 8 +++++++- src/lib/taxonomy-query.ts | 35 ++++++++++++++++++++++++++++------- src/lib/taxonomy.ts | 23 +++++++++++++++++++++++ test/api/taxonomy.spec.ts | 22 ++++++++++++++++++++++ test/api/types.ts | 17 +++++++++++++++++ test/unit/taxonomy.spec.ts | 26 ++++++++++++++++++++++++++ test/utils/mocks.ts | 26 +++++++++++++++++++++++++- 8 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 src/lib/taxonomy.ts create mode 100644 test/api/taxonomy.spec.ts create mode 100644 test/unit/taxonomy.spec.ts diff --git a/src/index.ts b/src/index.ts index d108292..704c9bd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,5 +11,6 @@ export type { ImageTransform } from './lib/image-transform'; export type { AssetQuery } from './lib/asset-query'; export type { TaxonomyQuery } from './lib/taxonomy-query'; export type { ContentTypeQuery } from './lib/contenttype-query'; +export type { Taxonomy } from './lib/taxonomy'; export default contentstack; diff --git a/src/lib/stack.ts b/src/lib/stack.ts index 324533d..815697d 100644 --- a/src/lib/stack.ts +++ b/src/lib/stack.ts @@ -8,6 +8,7 @@ import { synchronization } from './synchronization'; import {TaxonomyQuery} from './taxonomy-query'; import { GlobalFieldQuery } from './global-field-query'; import { GlobalField } from './global-field'; +import { Taxonomy } from './taxonomy'; export class Stack { readonly config: StackConfig; @@ -27,6 +28,7 @@ export class Stack { * @returns {Asset} * @example * import contentstack from '@contentstack/delivery-sdk' +import { Taxonomy } from './taxonomy'; * * const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" }); * const asset = stack.asset() // For collection of asset @@ -77,7 +79,11 @@ export class Stack { * const taxonomy = stack.taxonomy() // For taxonomy query object */ - taxonomy(): TaxonomyQuery { + taxonomy(): TaxonomyQuery; + taxonomy(uid: string): Taxonomy; + taxonomy(uid?: string): Taxonomy | TaxonomyQuery { + if (uid) return new Taxonomy(this._client, uid); + return new TaxonomyQuery(this._client); } diff --git a/src/lib/taxonomy-query.ts b/src/lib/taxonomy-query.ts index 7ece897..ec8ae9d 100644 --- a/src/lib/taxonomy-query.ts +++ b/src/lib/taxonomy-query.ts @@ -1,10 +1,31 @@ import { Query } from "./query"; -import { AxiosInstance } from "@contentstack/core"; +import { AxiosInstance, getData } from "@contentstack/core"; +import { FindResponse } from "./types"; export class TaxonomyQuery extends Query { - constructor(client: AxiosInstance) { - super(client, {}, {}); // will need make changes to Query class so that CT uid is not mandatory - this._client = client; - this._urlPath = `/taxonomies/entries`; - } -}; \ No newline at end of file + constructor(client: AxiosInstance) { + super(client, {}, {}); // will need make changes to Query class so that CT uid is not mandatory + this._client = client; + this._urlPath = `/taxonomies/entries`; + } + /** + * @method find + * @memberof TaxonomyQuery + * @description Fetches all taxonomies of the stack using /taxonomy-manager endpoint + * @returns {Promise>} + * @example + * import contentstack from '@contentstack/delivery-sdk' + * + * const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" }); + * const taxonomyQuery = stack.taxonomy(); + * const result = await taxonomyQuery.find(); + */ + override async find(): Promise> { + this._urlPath = "/taxonomy-manager"; // TODO: change to /taxonomies + const response = await getData(this._client, this._urlPath, { + params: this._queryParams, + }); + + return response as FindResponse; + } +} \ No newline at end of file diff --git a/src/lib/taxonomy.ts b/src/lib/taxonomy.ts new file mode 100644 index 0000000..6e7a874 --- /dev/null +++ b/src/lib/taxonomy.ts @@ -0,0 +1,23 @@ +import { AxiosInstance, getData } from '@contentstack/core'; + +export class Taxonomy { + private _client: AxiosInstance; + private _taxonomyUid: string; + private _urlPath: string; + + _queryParams: { [key: string]: string | number } = {}; + + constructor(client: AxiosInstance, taxonomyUid: string) { + this._client = client; + this._taxonomyUid = taxonomyUid; + this._urlPath = `/taxonomy-manager/${this._taxonomyUid}`; // TODO: change to /taxonomies/${this._taxonomyUid} + } + + async fetch(): Promise { + const response = await getData(this._client, this._urlPath); + + if (response.taxonomy) return response.taxonomy as T; + + return response; + } +} diff --git a/test/api/taxonomy.spec.ts b/test/api/taxonomy.spec.ts new file mode 100644 index 0000000..8685073 --- /dev/null +++ b/test/api/taxonomy.spec.ts @@ -0,0 +1,22 @@ +/* eslint-disable no-console */ +/* eslint-disable promise/always-return */ +import { stackInstance } from '../utils/stack-instance'; +import { TTaxonomies } from './types'; +import dotenv from 'dotenv'; +import { TaxonomyQuery } from '../../src/lib/taxonomy-query'; + +dotenv.config() + +const stack = stackInstance(); +describe('ContentType API test cases', () => { + it('should give taxonomies when taxonomies method is called', async () => { + const result = await makeTaxonomy().find(); + expect(result).toBeDefined(); + }); +}); + +function makeTaxonomy(): TaxonomyQuery { + const taxonomy = stack.taxonomy(); + + return taxonomy; +} diff --git a/test/api/types.ts b/test/api/types.ts index 776e3b2..11197b6 100644 --- a/test/api/types.ts +++ b/test/api/types.ts @@ -86,3 +86,20 @@ export interface TContentType { export interface TContentTypes { content_types: TContentType[]; } + +export interface TTaxonomies { + taxonomies: TTaxonomy[]; +} + +export interface TTaxonomy { + uid: string; + name: string; + description?: string; + terms_count: number; + created_at: string; + updated_at: string; + created_by: string; + updated_by: string; + type: string; + publish_details: PublishDetails; +} \ No newline at end of file diff --git a/test/unit/taxonomy.spec.ts b/test/unit/taxonomy.spec.ts new file mode 100644 index 0000000..5d37ac4 --- /dev/null +++ b/test/unit/taxonomy.spec.ts @@ -0,0 +1,26 @@ +import { TaxonomyQuery } from '../../src/lib/taxonomy-query'; +import { AxiosInstance, httpClient } from '@contentstack/core'; +import MockAdapter from 'axios-mock-adapter'; +import { taxonomyFindResponseDataMock } from '../utils/mocks'; +import { MOCK_CLIENT_OPTIONS } from '../utils/constant'; + +describe('ta class', () => { + let taxonomy: TaxonomyQuery; + let client: AxiosInstance; + let mockClient: MockAdapter; + + beforeAll(() => { + client = httpClient(MOCK_CLIENT_OPTIONS); + mockClient = new MockAdapter(client as any); + }); + + beforeEach(() => { + taxonomy = new TaxonomyQuery(client); + }); + + it('should return response data when successful', async () => { + mockClient.onGet('/taxonomy-manager').reply(200, taxonomyFindResponseDataMock); //TODO: change to /taxonomies + const response = await taxonomy.find(); + expect(response).toEqual(taxonomyFindResponseDataMock); + }); +}); diff --git a/test/utils/mocks.ts b/test/utils/mocks.ts index a265d0c..27e79be 100644 --- a/test/utils/mocks.ts +++ b/test/utils/mocks.ts @@ -1676,6 +1676,29 @@ const gfieldQueryFindResponseDataMock = { ] } +const taxonomyFindResponseDataMock = { + "taxonomies": [ + { + "uid": "taxonomy_testing", + "name": "taxonomy testing", + "description": "", + "terms_count": 1, + "created_at": "2025-10-10T06:42:48.644Z", + "updated_at": "2025-10-10T06:42:48.644Z", + "created_by": "created_by", + "updated_by": "updated_by", + "type": "TAXONOMY", + "ACL": {}, + "publish_details": { + "time": "2025-10-10T08:01:48.174Z", + "user": "user", + "environment": "env", + "locale": "en-us" + } + } + ] +} + const syncResult: any = { ...axiosGetMock.data }; export { @@ -1688,5 +1711,6 @@ export { entryFindMock, entryFetchMock, gfieldFetchDataMock, - gfieldQueryFindResponseDataMock + gfieldQueryFindResponseDataMock, + taxonomyFindResponseDataMock };