Skip to content

Added array of values in includereference method #51

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
Sep 12, 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,5 +1,9 @@
## Change log

### Version: 4.3.0
#### Date: Septmber-09-2024
Feat: Include refernce accepts array of values

### Version: 4.2.0
#### Date: Septmber-04-2024
Feat: Variants support added
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.2.0",
"version": "4.3.0",
"type": "commonjs",
"main": "./dist/cjs/src/index.js",
"types": "./dist/types/src/index.d.ts",
Expand Down
14 changes: 11 additions & 3 deletions src/lib/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,17 @@ export class Entries extends EntryQueryable {
* @param {string} referenceFieldUid - UID of the reference field to include.
* @returns {Entries} - Returns the Entries instance for chaining.
*/
includeReference(referenceFieldUid: string): Entries {
this._queryParams['include[]'] = referenceFieldUid;

includeReference(...referenceFieldUid: (string | string[])[]): Entries {
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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Entry {
private _contentTypeUid: string;
private _entryUid: string;
private _urlPath: string;
_queryParams: { [key: string]: string | number } = {};
_queryParams: { [key: string]: string | number | string[] } = {};

constructor(client: AxiosInstance, contentTypeUid: string, entryUid: string) {
this._client = client;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/internal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export type params = {
}

export type queryParams = {
[key: string]: string | boolean | number
[key: string]: string | boolean | number | string[]
}
4 changes: 2 additions & 2 deletions test/unit/entries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ describe('Entries class', () => {
it('should set the include parameter to the given reference field UID', () => {
const referenceFieldUid = 'referenceFieldUid';
entry.includeReference(referenceFieldUid);
expect(entry._queryParams['include[]']).toContain(referenceFieldUid);
});

expect(entry._queryParams['include[]']).toBe(referenceFieldUid);
});

it('should add "include_fallback" in _queryParams when includeFallback method is called', () => {
const returnedValue = entry.includeFallback();
Expand Down
Loading