Skip to content

DX | 09-01-2025 | Release #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version: 4.4.4
#### Date: January-06-2025
Enh: Include References on Entry UID

### Version: 4.4.3
#### Date: November-30-2024
Fix: regex method fixed for validation
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/delivery-sdk",
"version": "4.4.3",
"version": "4.4.4",
"type": "module",
"license": "MIT",
"main": "./dist/legacy/index.cjs",
Expand Down
27 changes: 27 additions & 0 deletions src/lib/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ export class Entry {
return this;
}

/**
* @method includeReference
* @memberof Entry
* @description To include the content of the referred entry in your response,
* you need to use the include[] parameter and specify the UID of the reference field as value.
* This function sets the include parameter to a reference field UID in the API request.
* @example
* const stack = contentstack.stack("apiKey", "deliveryKey", "environment");
* const query = stack.contentType("contentTypeUid").entry(entry_uid).includeReference("brand").fetch()
*
* @param {string} referenceFieldUid - UID of the reference field to include.
* @returns {Entry} - Returns the Entry instance for chaining.
*/
includeReference(...referenceFieldUid: (string | string[])[]): Entry {
if (referenceFieldUid.length) {
referenceFieldUid.forEach(value => {
if (!Array.isArray(this._queryParams['include[]'])) {
this._queryParams['include[]'] = [];
}
(this._queryParams['include[]'] as string[]).push(...(Array.isArray(value) ? value : [value]));
});
} else {
console.error("Argument should be a String or an Array.");
}
return this;
}

/**
* @method includeContentType
* @memberof Entry
Expand Down
9 changes: 9 additions & 0 deletions test/api/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ describe('Entry API tests', () => {
expect(result.created_by).toBeDefined();
expect(result.updated_by).toBeDefined();
});
it('should check for include reference', async () => {
const result = await makeEntry(entryUid).includeReference('author').fetch<TEntry>();
expect(result.title).toBeDefined();
expect(result.author).toBeDefined();
expect(result.title).toBeDefined();
expect(result.url).toBeDefined();
expect(result._version).toBeDefined();
expect(result.publish_details).toBeDefined();
});
});
function makeEntry(uid = ''): Entry {
const entry = stack.contentType('author').entry(uid);
Expand Down
7 changes: 7 additions & 0 deletions test/unit/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ describe('Entry class', () => {
expect(entry._queryParams.include_fallback).toBe('true');
});

it('should set the include parameter to the given reference field UID', () => {
const referenceFieldUid = 'referenceFieldUid';
const returnedValue = entry.includeReference(referenceFieldUid);
expect(returnedValue).toBeInstanceOf(Entry);
expect(entry._queryParams['include[]']).toContain(referenceFieldUid);
});

it('should add "include_metadata" in _queryParams when includeMetadata method is called', () => {
const returnedValue = entry.includeMetadata();
expect(returnedValue).toBeInstanceOf(Entry);
Expand Down
Loading