|
| 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