Skip to content

Commit 141c121

Browse files
Added include reference support for entry UID (#97)
1 parent 814aff8 commit 141c121

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Version: 4.4.4
2+
#### Date: January-06-2025
3+
Enh: Include References on Entry UID
4+
15
### Version: 4.4.3
26
#### Date: November-30-2024
37
Fix: regex method fixed for validation

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.4.3",
3+
"version": "4.4.4",
44
"type": "module",
55
"license": "MIT",
66
"main": "./dist/legacy/index.cjs",

src/lib/entry.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ export class Entry {
9191
return this;
9292
}
9393

94+
/**
95+
* @method includeReference
96+
* @memberof Entry
97+
* @description To include the content of the referred entry in your response,
98+
* you need to use the include[] parameter and specify the UID of the reference field as value.
99+
* This function sets the include parameter to a reference field UID in the API request.
100+
* @example
101+
* const stack = contentstack.stack("apiKey", "deliveryKey", "environment");
102+
* const query = stack.contentType("contentTypeUid").entry(entry_uid).includeReference("brand").fetch()
103+
*
104+
* @param {string} referenceFieldUid - UID of the reference field to include.
105+
* @returns {Entry} - Returns the Entry instance for chaining.
106+
*/
107+
includeReference(...referenceFieldUid: (string | string[])[]): Entry {
108+
if (referenceFieldUid.length) {
109+
referenceFieldUid.forEach(value => {
110+
if (!Array.isArray(this._queryParams['include[]'])) {
111+
this._queryParams['include[]'] = [];
112+
}
113+
(this._queryParams['include[]'] as string[]).push(...(Array.isArray(value) ? value : [value]));
114+
});
115+
} else {
116+
console.error("Argument should be a String or an Array.");
117+
}
118+
return this;
119+
}
120+
94121
/**
95122
* @method includeContentType
96123
* @memberof Entry

test/api/entry.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ describe('Entry API tests', () => {
5050
expect(result.created_by).toBeDefined();
5151
expect(result.updated_by).toBeDefined();
5252
});
53+
it('should check for include reference', async () => {
54+
const result = await makeEntry(entryUid).includeReference('author').fetch<TEntry>();
55+
expect(result.title).toBeDefined();
56+
expect(result.author).toBeDefined();
57+
expect(result.title).toBeDefined();
58+
expect(result.url).toBeDefined();
59+
expect(result._version).toBeDefined();
60+
expect(result.publish_details).toBeDefined();
61+
});
5362
});
5463
function makeEntry(uid = ''): Entry {
5564
const entry = stack.contentType('author').entry(uid);

test/unit/entry.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ describe('Entry class', () => {
3737
expect(entry._queryParams.include_fallback).toBe('true');
3838
});
3939

40+
it('should set the include parameter to the given reference field UID', () => {
41+
const referenceFieldUid = 'referenceFieldUid';
42+
const returnedValue = entry.includeReference(referenceFieldUid);
43+
expect(returnedValue).toBeInstanceOf(Entry);
44+
expect(entry._queryParams['include[]']).toContain(referenceFieldUid);
45+
});
46+
4047
it('should add "include_metadata" in _queryParams when includeMetadata method is called', () => {
4148
const returnedValue = entry.includeMetadata();
4249
expect(returnedValue).toBeInstanceOf(Entry);

0 commit comments

Comments
 (0)