Skip to content

Commit 519c7a3

Browse files
feat: Enhance Taxonomy and TermQuery classes with detailed documentation and fetch methods
1 parent 4e97212 commit 519c7a3

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

src/lib/taxonomy.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,43 @@ import { AxiosInstance, getData } from '@contentstack/core';
22
import { TermQuery } from './term-query';
33
import { Term } from './term';
44

5+
/**
6+
* @class Taxonomy
7+
* @description Represents a taxonomy with methods to fetch taxonomy data and manage terms
8+
*/
59
export class Taxonomy {
610
private _client: AxiosInstance;
711
private _taxonomyUid: string;
812
private _urlPath: string;
913

1014
_queryParams: { [key: string]: string | number } = {};
1115

16+
/**
17+
* @constructor
18+
* @param {AxiosInstance} client - The HTTP client instance
19+
* @param {string} taxonomyUid - The taxonomy UID
20+
*/
1221
constructor(client: AxiosInstance, taxonomyUid: string) {
1322
this._client = client;
1423
this._taxonomyUid = taxonomyUid;
1524
this._urlPath = `/taxonomy-manager/${this._taxonomyUid}`; // TODO: change to /taxonomies/${this._taxonomyUid}
1625
}
1726

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+
*/
1842
term(uid: string): Term;
1943
term(): TermQuery;
2044
term(uid?: string): Term | TermQuery {
@@ -23,6 +47,17 @@ export class Taxonomy {
2347
return new TermQuery(this._client, this._taxonomyUid);
2448
}
2549

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+
*/
2661
async fetch<T>(): Promise<T> {
2762
const response = await getData(this._client, this._urlPath);
2863

src/lib/term-query.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
import { AxiosInstance, getData } from '@contentstack/core';
22
import { FindResponse } from './types';
33

4+
/**
5+
* @class TermQuery
6+
* @description Represents a query for fetching multiple terms from a taxonomy
7+
*/
48
export class TermQuery {
59
private _taxonomyUid: string;
610
private _client: AxiosInstance;
711
private _urlPath: string;
812
_queryParams: { [key: string]: string | number } = {};
913

14+
/**
15+
* @constructor
16+
* @param {AxiosInstance} client - The HTTP client instance
17+
* @param {string} taxonomyUid - The taxonomy UID
18+
*/
1019
constructor(client: AxiosInstance, taxonomyUid: string) {
1120
this._client = client;
1221
this._taxonomyUid = taxonomyUid;
1322
this._urlPath = `/taxonomy-manager/${this._taxonomyUid}/terms`;
1423
}
1524

25+
/**
26+
* @method find
27+
* @memberof TermQuery
28+
* @description Fetches all terms from the taxonomy
29+
* @returns {Promise<FindResponse<T>>}
30+
* @example
31+
* import contentstack from '@contentstack/delivery-sdk'
32+
*
33+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
34+
* const result = await stack.taxonomy('taxonomy_uid').term().find();
35+
*/
1636
async find<T>(): Promise<FindResponse<T>> {
1737
const response = await getData(this._client, this._urlPath, { params: this._queryParams });
1838
return response as FindResponse<T>;

src/lib/term.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { AxiosInstance, getData } from "@contentstack/core";
22

3+
/**
4+
* @class Term
5+
* @description Represents a taxonomy term with methods to fetch term data, locales, ancestors, and descendants
6+
*/
37
export class Term {
48
protected _client: AxiosInstance;
59
private _taxonomyUid: string;
610
private _termUid: string;
711
private _urlPath: string;
812

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+
*/
919
constructor(client: AxiosInstance, taxonomyUid: string, termUid: string) {
1020
this._client = client;
1121
this._taxonomyUid = taxonomyUid;
@@ -64,6 +74,17 @@ export class Term {
6474
return response;
6575
}
6676

77+
/**
78+
* @method fetch
79+
* @memberof Term
80+
* @description Fetches the term data by UID
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+
*/
6788
async fetch<T>(): Promise<T> {
6889
const response = await getData(this._client, this._urlPath);
6990
if (response.term) return response.term as T;

0 commit comments

Comments
 (0)