Skip to content

Commit dae3f07

Browse files
Merge pull request #374 from contentstack/enhc/taxonomy-localisation-2
feat: add locale and includeFallback support to taxonomy CDA delivery
2 parents cf621e8 + 3c987b6 commit dae3f07

6 files changed

Lines changed: 53 additions & 12 deletions

File tree

.talismanrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ fileignoreconfig:
6464
checksum: 9185df498914e2966d78d9d216acaaa910d43cd7ac9a5e9a26e7241ac9edc9b5
6565
- filename: test/reporting/generate-unified-report.js
6666
checksum: 9e7a4696561b790cb93f3be8406a70ec6fdc90a3f8bbb9739504495690158fe3
67+
- filename: src/query/term-query.ts
68+
checksum: 1f5b23177460d562076d93cf28b375106b19123a5ab135ffef75f4b2bb332d35
6769
version: "1.0"

src/query/term-query.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@ export class TermQuery {
2222
this._urlPath = `/taxonomies/${this._taxonomyUid}/terms`;
2323
}
2424

25+
/**
26+
* @method locale
27+
* @memberof TermQuery
28+
* @description Retrieves terms published in the specified locale.
29+
* @param {string} locale - The locale code (e.g. 'hi-in', 'en-us')
30+
* @returns {TermQuery}
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().locale('hi-in').find();
36+
*/
37+
locale(locale: string): TermQuery {
38+
this._queryParams.locale = locale;
39+
return this;
40+
}
41+
42+
/**
43+
* @method includeFallback
44+
* @memberof TermQuery
45+
* @description When a term is not localized in the requested locale, falls back to the master locale.
46+
* @returns {TermQuery}
47+
* @example
48+
* import contentstack from '@contentstack/delivery-sdk'
49+
*
50+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
51+
* const result = await stack.taxonomy('taxonomy_uid').term().locale('hi-in').includeFallback().find();
52+
*/
53+
includeFallback(): TermQuery {
54+
this._queryParams.include_fallback = 'true';
55+
return this;
56+
}
57+
2558
/**
2659
* @method find
2760
* @memberof TermQuery

src/taxonomy/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,19 @@ export class Taxonomy {
5050
/**
5151
* @method fetch
5252
* @memberof Taxonomy
53-
* @description Fetches the taxonomy data by UID
53+
* @description Fetches the taxonomy data by UID. Pass a locale code to retrieve the localized version.
54+
* @param {string} [locale] - Optional locale code (e.g. 'hi-in'). Omit to retrieve the master locale.
5455
* @returns {Promise<T>}
5556
* @example
5657
* import contentstack from '@contentstack/delivery-sdk'
5758
*
5859
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
5960
* const result = await stack.taxonomy('taxonomy_uid').fetch();
61+
* const localized = await stack.taxonomy('taxonomy_uid').fetch('hi-in');
6062
*/
61-
async fetch<T>(): Promise<T> {
62-
const response = await getData(this._client, this._urlPath);
63+
async fetch<T>(locale?: string): Promise<T> {
64+
if (locale) this._queryParams.locale = locale;
65+
const response = await getData(this._client, this._urlPath, { params: this._queryParams });
6366

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

src/taxonomy/term.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,19 @@ export class Term {
7777
/**
7878
* @method fetch
7979
* @memberof Term
80-
* @description Fetches all descendants of a single published term.
80+
* @description Fetches a single published term. Pass a locale code to retrieve the localized version.
81+
* @param {string} [locale] - Optional locale code (e.g. 'mr-in'). Omit to retrieve the master locale.
8182
* @returns {Promise<T>}
8283
* @example
8384
* import contentstack from '@contentstack/delivery-sdk'
8485
*
8586
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
8687
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').fetch();
88+
* const localized = await stack.taxonomy('taxonomy_uid').term('term_uid').fetch('mr-in');
8789
*/
88-
async fetch<T>(): Promise<T> {
89-
const response = await getData(this._client, this._urlPath);
90+
async fetch<T>(locale?: string): Promise<T> {
91+
const params = locale ? { locale } : undefined;
92+
const response = await getData(this._client, this._urlPath, params ? { params } : undefined);
9093
if (response.term) return response.term as T;
9194
return response;
9295
}

test/unit/taxonomy.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { TaxonomyQuery } from '../../src/lib/taxonomy-query';
2-
import { Taxonomy } from '../../src/lib/taxonomy';
1+
import { TaxonomyQuery } from '../../src/query/taxonomy-query';
2+
import { Taxonomy } from '../../src/taxonomy';
33
import { AxiosInstance, httpClient } from '@contentstack/core';
44
import MockAdapter from 'axios-mock-adapter';
55
import { taxonomyFindResponseDataMock } from '../utils/mocks';
66
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';
7-
import { Term } from '../../src/lib/term';
8-
import { TermQuery } from '../../src/lib/term-query';
7+
import { Term } from '../../src/taxonomy/term';
8+
import { TermQuery } from '../../src/query/term-query';
99

1010
describe('ta class', () => {
1111
let taxonomies: TaxonomyQuery;

test/unit/term.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { AxiosInstance, httpClient } from '@contentstack/core';
22
import MockAdapter from 'axios-mock-adapter';
33
import { termQueryFindResponseDataMock, termLocalesResponseDataMock, termAncestorsResponseDataMock, termDescendantsResponseDataMock } from '../utils/mocks';
44
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';
5-
import { Term } from '../../src/lib/term';
6-
import { Taxonomy } from '../../src/lib/taxonomy';
5+
import { Term } from '../../src/taxonomy/term';
6+
import { Taxonomy } from '../../src/taxonomy';
77

88
describe('Term class', () => {
99
let term: Term;

0 commit comments

Comments
 (0)