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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 7 additions & 1 deletion src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
35 changes: 28 additions & 7 deletions src/lib/taxonomy-query.ts
Original file line number Diff line number Diff line change
@@ -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`;
}
};
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<FindResponse<T>>}
* @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<T>(): Promise<FindResponse<T>> {
this._urlPath = "/taxonomy-manager"; // TODO: change to /taxonomies
const response = await getData(this._client, this._urlPath, {
params: this._queryParams,
});

return response as FindResponse<T>;
}
}
23 changes: 23 additions & 0 deletions src/lib/taxonomy.ts
Original file line number Diff line number Diff line change
@@ -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<T>(): Promise<T> {
const response = await getData(this._client, this._urlPath);

if (response.taxonomy) return response.taxonomy as T;

return response;
}
}
22 changes: 22 additions & 0 deletions test/api/taxonomy.spec.ts
Original file line number Diff line number Diff line change
@@ -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<TTaxonomies>();
expect(result).toBeDefined();
});
});

function makeTaxonomy(): TaxonomyQuery {
const taxonomy = stack.taxonomy();

return taxonomy;
}
17 changes: 17 additions & 0 deletions test/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
26 changes: 26 additions & 0 deletions test/unit/taxonomy.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
26 changes: 25 additions & 1 deletion test/utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -1688,5 +1711,6 @@ export {
entryFindMock,
entryFetchMock,
gfieldFetchDataMock,
gfieldQueryFindResponseDataMock
gfieldQueryFindResponseDataMock,
taxonomyFindResponseDataMock
};