From 18c0e83fa1eb9b49d38ed4abacafa4c1da396d33 Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Fri, 6 Sep 2024 17:01:00 +0530 Subject: [PATCH] Added array of values in includereference method (#50) --- CHANGELOG.md | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- src/lib/entries.ts | 14 +++++++++++--- src/lib/entry.ts | 2 +- src/lib/internal-types.ts | 2 +- test/unit/entries.spec.ts | 4 ++-- test/utils/mocks.ts | 38 +++++++++++++++++++------------------- 8 files changed, 41 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c92f049..4703151 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 947f608..923ed75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/delivery-sdk", - "version": "4.2.0", + "version": "4.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/delivery-sdk", - "version": "4.2.0", + "version": "4.3.0", "dependencies": { "@contentstack/core": "^1.1.0", "@contentstack/utils": "^1.3.8", diff --git a/package.json b/package.json index b212054..c34286a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/entries.ts b/src/lib/entries.ts index b886ab9..f5640ae 100644 --- a/src/lib/entries.ts +++ b/src/lib/entries.ts @@ -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; } diff --git a/src/lib/entry.ts b/src/lib/entry.ts index e764645..b712f29 100644 --- a/src/lib/entry.ts +++ b/src/lib/entry.ts @@ -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; diff --git a/src/lib/internal-types.ts b/src/lib/internal-types.ts index aa5912a..5c998d8 100644 --- a/src/lib/internal-types.ts +++ b/src/lib/internal-types.ts @@ -3,5 +3,5 @@ export type params = { } export type queryParams = { - [key: string]: string | boolean | number + [key: string]: string | boolean | number | string[] } diff --git a/test/unit/entries.spec.ts b/test/unit/entries.spec.ts index 9e7aa4d..793d1d9 100644 --- a/test/unit/entries.spec.ts +++ b/test/unit/entries.spec.ts @@ -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(); diff --git a/test/utils/mocks.ts b/test/utils/mocks.ts index a265d0c..e902fb8 100644 --- a/test/utils/mocks.ts +++ b/test/utils/mocks.ts @@ -81,7 +81,7 @@ const assetQueryFindResponseDataMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } }, { @@ -122,7 +122,7 @@ const assetQueryFindResponseDataMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } }, { @@ -163,7 +163,7 @@ const assetQueryFindResponseDataMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } }, { @@ -204,7 +204,7 @@ const assetQueryFindResponseDataMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } }, { @@ -245,7 +245,7 @@ const assetQueryFindResponseDataMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } }, { @@ -286,7 +286,7 @@ const assetQueryFindResponseDataMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } }, ] @@ -1077,7 +1077,7 @@ const assetFetchDataMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } } @@ -1226,7 +1226,7 @@ const entryFindMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:56.964Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } } ], @@ -1291,7 +1291,7 @@ const entryFindMock = { "ACL": {}, "uid": "blt4f1fd991ec80e52f", "created_by": "bltcd82b2c6bf913241", - "updated_by": "***REMOVED***", + "updated_by": "blt42e55757d70d5f81026a2b9f", "created_at": "2019-08-16T08:19:25.397Z", "updated_at": "2019-08-23T13:02:21.457Z", "_version": 4, @@ -1300,7 +1300,7 @@ const entryFindMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-23T13:02:25.439Z", - "user": "***REMOVED***" + "user": "blt42e55757d70d5f81026a2b9f" } }, { @@ -1328,7 +1328,7 @@ const entryFindMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } } ], @@ -1387,7 +1387,7 @@ const entryFindMock = { "ACL": {}, "uid": "blta278bb5672180c94", "created_by": "bltcd82b2c6bf913241", - "updated_by": "***REMOVED***", + "updated_by": "blt42e55757d70d5f81026a2b9f", "created_at": "2019-08-16T08:19:27.182Z", "updated_at": "2019-08-23T13:01:19.866Z", "_version": 4, @@ -1396,7 +1396,7 @@ const entryFindMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-23T13:01:23.290Z", - "user": "***REMOVED***" + "user": "blt42e55757d70d5f81026a2b9f" } }, { @@ -1424,7 +1424,7 @@ const entryFindMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:56.964Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } } ], @@ -1476,7 +1476,7 @@ const entryFindMock = { "ACL": {}, "uid": "bltbd92ac498e3d5f96", "created_by": "bltcd82b2c6bf913241", - "updated_by": "***REMOVED***", + "updated_by": "blt42e55757d70d5f81026a2b9f", "created_at": "2019-08-16T08:19:20.072Z", "updated_at": "2019-08-23T12:50:53.424Z", "_version": 13, @@ -1485,7 +1485,7 @@ const entryFindMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-23T12:50:56.727Z", - "user": "***REMOVED***" + "user": "blt42e55757d70d5f81026a2b9f" } } ] @@ -1517,7 +1517,7 @@ const entryFetchMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-19T12:28:47.432Z", - "user": "***REMOVED***" + "user": "blt587a89fc7883c56700a95bfe" } } ], @@ -1576,7 +1576,7 @@ const entryFetchMock = { "ACL": {}, "uid": "blta278bb5672180c94", "created_by": "bltcd82b2c6bf913241", - "updated_by": "***REMOVED***", + "updated_by": "blt42e55757d70d5f81026a2b9f", "created_at": "2019-08-16T08:19:27.182Z", "updated_at": "2019-08-23T13:01:19.866Z", "_version": 4, @@ -1585,7 +1585,7 @@ const entryFetchMock = { "environment": "blta39a4441696e35e0", "locale": "en-us", "time": "2019-08-23T13:01:23.290Z", - "user": "***REMOVED***" + "user": "blt42e55757d70d5f81026a2b9f" } } }