Skip to content

Commit ed590e2

Browse files
refactor: reorganize taxonomy imports and add Taxonomy and Term classes
1 parent 071aef5 commit ed590e2

5 files changed

Lines changed: 164 additions & 2 deletions

File tree

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type { ImageTransform } from './assets';
1313
export type { AssetQuery } from './query';
1414
export type { TaxonomyQuery } from './query';
1515
export type { ContentTypeQuery } from './query';
16-
export type { Taxonomy } from './taxonomy/taxonomy';
16+
export type { Taxonomy } from './taxonomy';
1717
export { ErrorMessages, ErrorCode } from './common/error-messages';
1818

1919
export default contentstack;

src/query/term-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AxiosInstance, getData } from '@contentstack/core';
2-
import { FindResponse } from './types';
2+
import { FindResponse } from '../common/types';
33

44
/**
55
* @class TermQuery

src/stack/stack.ts

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

1213
export class Stack {
1314
readonly config: StackConfig;

src/taxonomy/index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { AxiosInstance, getData } from '@contentstack/core';
2+
import { TermQuery } from '../query/term-query';
3+
import { Term } from './term';
4+
5+
/**
6+
* @class Taxonomy
7+
* @description Represents a published taxonomy with methods to fetch taxonomy data and manage terms. Requires taxonomy_publish feature flag to be enabled.
8+
*/
9+
export class Taxonomy {
10+
private _client: AxiosInstance;
11+
private _taxonomyUid: string;
12+
private _urlPath: string;
13+
14+
_queryParams: { [key: string]: string | number } = {};
15+
16+
/**
17+
* @constructor
18+
* @param {AxiosInstance} client - The HTTP client instance
19+
* @param {string} taxonomyUid - The taxonomy UID
20+
*/
21+
constructor(client: AxiosInstance, taxonomyUid: string) {
22+
this._client = client;
23+
this._taxonomyUid = taxonomyUid;
24+
this._urlPath = `/taxonomies/${this._taxonomyUid}`;
25+
}
26+
27+
/**
28+
* @method term
29+
* @memberof Taxonomy
30+
* @description Gets a specific term or creates a term query
31+
* @param {string} [uid] - Optional term UID. If provided, returns a Term instance. If not provided, returns a TermQuery instance.
32+
* @returns {Term | TermQuery}
33+
* @example
34+
* import contentstack from '@contentstack/delivery-sdk'
35+
*
36+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
37+
* // Get a specific term
38+
* const term = stack.taxonomy('taxonomy_uid').term('term_uid');
39+
* // Get all terms
40+
* const termQuery = stack.taxonomy('taxonomy_uid').term();
41+
*/
42+
term(uid: string): Term;
43+
term(): TermQuery;
44+
term(uid?: string): Term | TermQuery {
45+
if (uid) return new Term(this._client, this._taxonomyUid, uid);
46+
47+
return new TermQuery(this._client, this._taxonomyUid);
48+
}
49+
50+
/**
51+
* @method fetch
52+
* @memberof Taxonomy
53+
* @description Fetches the taxonomy data by UID
54+
* @returns {Promise<T>}
55+
* @example
56+
* import contentstack from '@contentstack/delivery-sdk'
57+
*
58+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
59+
* const result = await stack.taxonomy('taxonomy_uid').fetch();
60+
*/
61+
async fetch<T>(): Promise<T> {
62+
const response = await getData(this._client, this._urlPath);
63+
64+
if (response.taxonomy) return response.taxonomy as T;
65+
66+
return response;
67+
}
68+
}

src/taxonomy/term.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { AxiosInstance, getData } from "@contentstack/core";
2+
3+
/**
4+
* @class Term
5+
* @description Represents a published taxonomy term with methods to fetch term data, locales, ancestors, and descendants. Requires taxonomy_publish feature flag to be enabled.
6+
*/
7+
export class Term {
8+
protected _client: AxiosInstance;
9+
private _taxonomyUid: string;
10+
private _termUid: string;
11+
private _urlPath: string;
12+
13+
/**
14+
* @constructor
15+
* @param {AxiosInstance} client - The HTTP client instance
16+
* @param {string} taxonomyUid - The taxonomy UID
17+
* @param {string} termUid - The term UID
18+
*/
19+
constructor(client: AxiosInstance, taxonomyUid: string, termUid: string) {
20+
this._client = client;
21+
this._taxonomyUid = taxonomyUid;
22+
this._termUid = termUid;
23+
this._urlPath = `/taxonomies/${this._taxonomyUid}/terms/${this._termUid}`;
24+
}
25+
26+
/**
27+
* @method locales
28+
* @memberof Term
29+
* @description Fetches all published, localized versions of a single term.
30+
* @returns {Promise<T>}
31+
* @example
32+
* import contentstack from '@contentstack/delivery-sdk'
33+
*
34+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
35+
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').locales();
36+
*/
37+
async locales<T>(): Promise<T> {
38+
const response = await getData(this._client, `${this._urlPath}/locales`);
39+
if (response.locales) return response.locales as T;
40+
return response;
41+
}
42+
43+
/**
44+
* @method ancestors
45+
* @memberof Term
46+
* @description Fetches all ancestors of a single published term, up to the root.
47+
* @returns {Promise<T>}
48+
* @example
49+
* import contentstack from '@contentstack/delivery-sdk'
50+
*
51+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
52+
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').ancestors();
53+
*/
54+
async ancestors<T>(): Promise<T> {
55+
const response = await getData(this._client, `${this._urlPath}/ancestors`);
56+
if (response.ancestors) return response.ancestors as T;
57+
return response;
58+
}
59+
60+
/**
61+
* @method descendants
62+
* @memberof Term
63+
* @description Fetches all descendants of a single published term.
64+
* @returns {Promise<T>}
65+
* @example
66+
* import contentstack from '@contentstack/delivery-sdk'
67+
*
68+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
69+
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').descendants();
70+
*/
71+
async descendants<T>(): Promise<T> {
72+
const response = await getData(this._client, `${this._urlPath}/descendants`);
73+
if (response.descendants) return response.descendants as T;
74+
return response;
75+
}
76+
77+
/**
78+
* @method fetch
79+
* @memberof Term
80+
* @description Fetches all descendants of a single published term.
81+
* @returns {Promise<T>}
82+
* @example
83+
* import contentstack from '@contentstack/delivery-sdk'
84+
*
85+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
86+
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').fetch();
87+
*/
88+
async fetch<T>(): Promise<T> {
89+
const response = await getData(this._client, this._urlPath);
90+
if (response.term) return response.term as T;
91+
return response;
92+
}
93+
}

0 commit comments

Comments
 (0)