Skip to content

Commit 77caa3b

Browse files
Merge pull request #240 from contentstack/feat/dx-3628-get-all-taxonomy
feat dx-3628 get all taxonomies
2 parents f7dc776 + 240d7f5 commit 77caa3b

File tree

8 files changed

+149
-9
lines changed

8 files changed

+149
-9
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export type { ImageTransform } from './lib/image-transform';
1111
export type { AssetQuery } from './lib/asset-query';
1212
export type { TaxonomyQuery } from './lib/taxonomy-query';
1313
export type { ContentTypeQuery } from './lib/contenttype-query';
14+
export type { Taxonomy } from './lib/taxonomy';
1415

1516
export default contentstack;

src/lib/stack.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { synchronization } from './synchronization';
88
import {TaxonomyQuery} from './taxonomy-query';
99
import { GlobalFieldQuery } from './global-field-query';
1010
import { GlobalField } from './global-field';
11+
import { Taxonomy } from './taxonomy';
1112

1213
export class Stack {
1314
readonly config: StackConfig;
@@ -27,6 +28,7 @@ export class Stack {
2728
* @returns {Asset}
2829
* @example
2930
* import contentstack from '@contentstack/delivery-sdk'
31+
import { Taxonomy } from './taxonomy';
3032
*
3133
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
3234
* const asset = stack.asset() // For collection of asset
@@ -77,7 +79,11 @@ export class Stack {
7779
7880
* const taxonomy = stack.taxonomy() // For taxonomy query object
7981
*/
80-
taxonomy(): TaxonomyQuery {
82+
taxonomy(): TaxonomyQuery;
83+
taxonomy(uid: string): Taxonomy;
84+
taxonomy(uid?: string): Taxonomy | TaxonomyQuery {
85+
if (uid) return new Taxonomy(this._client, uid);
86+
8187
return new TaxonomyQuery(this._client);
8288
}
8389

src/lib/taxonomy-query.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
import { Query } from "./query";
2-
import { AxiosInstance } from "@contentstack/core";
2+
import { AxiosInstance, getData } from "@contentstack/core";
3+
import { FindResponse } from "./types";
34

45
export class TaxonomyQuery extends Query {
5-
constructor(client: AxiosInstance) {
6-
super(client, {}, {}); // will need make changes to Query class so that CT uid is not mandatory
7-
this._client = client;
8-
this._urlPath = `/taxonomies/entries`;
9-
}
10-
};
6+
constructor(client: AxiosInstance) {
7+
super(client, {}, {}); // will need make changes to Query class so that CT uid is not mandatory
8+
this._client = client;
9+
this._urlPath = `/taxonomies/entries`;
10+
}
11+
/**
12+
* @method find
13+
* @memberof TaxonomyQuery
14+
* @description Fetches all taxonomies of the stack using /taxonomy-manager endpoint
15+
* @returns {Promise<FindResponse<T>>}
16+
* @example
17+
* import contentstack from '@contentstack/delivery-sdk'
18+
*
19+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
20+
* const taxonomyQuery = stack.taxonomy();
21+
* const result = await taxonomyQuery.find();
22+
*/
23+
override async find<T>(): Promise<FindResponse<T>> {
24+
this._urlPath = "/taxonomy-manager"; // TODO: change to /taxonomies
25+
const response = await getData(this._client, this._urlPath, {
26+
params: this._queryParams,
27+
});
28+
29+
return response as FindResponse<T>;
30+
}
31+
}

src/lib/taxonomy.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { AxiosInstance, getData } from '@contentstack/core';
2+
3+
export class Taxonomy {
4+
private _client: AxiosInstance;
5+
private _taxonomyUid: string;
6+
private _urlPath: string;
7+
8+
_queryParams: { [key: string]: string | number } = {};
9+
10+
constructor(client: AxiosInstance, taxonomyUid: string) {
11+
this._client = client;
12+
this._taxonomyUid = taxonomyUid;
13+
this._urlPath = `/taxonomy-manager/${this._taxonomyUid}`; // TODO: change to /taxonomies/${this._taxonomyUid}
14+
}
15+
16+
async fetch<T>(): Promise<T> {
17+
const response = await getData(this._client, this._urlPath);
18+
19+
if (response.taxonomy) return response.taxonomy as T;
20+
21+
return response;
22+
}
23+
}

test/api/taxonomy.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable no-console */
2+
/* eslint-disable promise/always-return */
3+
import { stackInstance } from '../utils/stack-instance';
4+
import { TTaxonomies } from './types';
5+
import dotenv from 'dotenv';
6+
import { TaxonomyQuery } from '../../src/lib/taxonomy-query';
7+
8+
dotenv.config()
9+
10+
const stack = stackInstance();
11+
describe('ContentType API test cases', () => {
12+
it('should give taxonomies when taxonomies method is called', async () => {
13+
const result = await makeTaxonomy().find<TTaxonomies>();
14+
expect(result).toBeDefined();
15+
});
16+
});
17+
18+
function makeTaxonomy(): TaxonomyQuery {
19+
const taxonomy = stack.taxonomy();
20+
21+
return taxonomy;
22+
}

test/api/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,20 @@ export interface TContentType {
8686
export interface TContentTypes {
8787
content_types: TContentType[];
8888
}
89+
90+
export interface TTaxonomies {
91+
taxonomies: TTaxonomy[];
92+
}
93+
94+
export interface TTaxonomy {
95+
uid: string;
96+
name: string;
97+
description?: string;
98+
terms_count: number;
99+
created_at: string;
100+
updated_at: string;
101+
created_by: string;
102+
updated_by: string;
103+
type: string;
104+
publish_details: PublishDetails;
105+
}

test/unit/taxonomy.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { TaxonomyQuery } from '../../src/lib/taxonomy-query';
2+
import { AxiosInstance, httpClient } from '@contentstack/core';
3+
import MockAdapter from 'axios-mock-adapter';
4+
import { taxonomyFindResponseDataMock } from '../utils/mocks';
5+
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';
6+
7+
describe('ta class', () => {
8+
let taxonomy: TaxonomyQuery;
9+
let client: AxiosInstance;
10+
let mockClient: MockAdapter;
11+
12+
beforeAll(() => {
13+
client = httpClient(MOCK_CLIENT_OPTIONS);
14+
mockClient = new MockAdapter(client as any);
15+
});
16+
17+
beforeEach(() => {
18+
taxonomy = new TaxonomyQuery(client);
19+
});
20+
21+
it('should return response data when successful', async () => {
22+
mockClient.onGet('/taxonomy-manager').reply(200, taxonomyFindResponseDataMock); //TODO: change to /taxonomies
23+
const response = await taxonomy.find();
24+
expect(response).toEqual(taxonomyFindResponseDataMock);
25+
});
26+
});

test/utils/mocks.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,29 @@ const gfieldQueryFindResponseDataMock = {
16761676
]
16771677
}
16781678

1679+
const taxonomyFindResponseDataMock = {
1680+
"taxonomies": [
1681+
{
1682+
"uid": "taxonomy_testing",
1683+
"name": "taxonomy testing",
1684+
"description": "",
1685+
"terms_count": 1,
1686+
"created_at": "2025-10-10T06:42:48.644Z",
1687+
"updated_at": "2025-10-10T06:42:48.644Z",
1688+
"created_by": "created_by",
1689+
"updated_by": "updated_by",
1690+
"type": "TAXONOMY",
1691+
"ACL": {},
1692+
"publish_details": {
1693+
"time": "2025-10-10T08:01:48.174Z",
1694+
"user": "user",
1695+
"environment": "env",
1696+
"locale": "en-us"
1697+
}
1698+
}
1699+
]
1700+
}
1701+
16791702
const syncResult: any = { ...axiosGetMock.data };
16801703

16811704
export {
@@ -1688,5 +1711,6 @@ export {
16881711
entryFindMock,
16891712
entryFetchMock,
16901713
gfieldFetchDataMock,
1691-
gfieldQueryFindResponseDataMock
1714+
gfieldQueryFindResponseDataMock,
1715+
taxonomyFindResponseDataMock
16921716
};

0 commit comments

Comments
 (0)