Skip to content

Commit

Permalink
Merge pull request #68 from contentstack/fix/DX-1565
Browse files Browse the repository at this point in the history
fix: added support for arrays in only and except
  • Loading branch information
vkalta authored Dec 1, 2024
2 parents abcd42d + bcbc9d1 commit 9ce7bb6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
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.2",
"version": "4.4.3",
"type": "module",
"license": "MIT",
"main": "./dist/legacy/index.cjs",
Expand Down
1 change: 0 additions & 1 deletion src/lib/base-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ export class BaseQuery extends Pagination {
'x-cs-variant-uid': this._variants
};
}

const response = await getData(this._client, this._urlPath, getRequestOptions);

return response as FindResponse<T>;
Expand Down
25 changes: 20 additions & 5 deletions src/lib/entry-queryable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ export class EntryQueryable extends BaseQuery {
* @param {string} fieldUid - field uid to select
* @returns {EntryQueryable} - returns EntryQueryable object for chaining method calls
*/
only(fieldUid: string): EntryQueryable {
this._queryParams['only[BASE][]'] = fieldUid;

only(fieldUid: string|string[]): EntryQueryable {
if (Array.isArray(fieldUid)) {
let i = 0;
for (const uid of fieldUid) {
this._queryParams[`only[BASE][${i}]`] = uid;
i++;
}
} else {
this._queryParams["only[BASE][]"] = fieldUid;
}
return this;
}

Expand All @@ -34,8 +41,16 @@ export class EntryQueryable extends BaseQuery {
* @param {string} fieldUid - field uid to exclude
* @returns {EntryQueryable} - returns EntryQueryable object for chaining method calls
*/
except(fieldUid: string): EntryQueryable {
this._queryParams['except[BASE][]'] = fieldUid;
except(fieldUid: string|string[]): EntryQueryable {
if (Array.isArray(fieldUid)) {
let i = 0;
for (const uid of fieldUid) {
this._queryParams[`except[BASE][${i}]`] = uid;
i++;
}
} else {
this._queryParams["except[BASE][]"] = fieldUid;
}

return this;
}
Expand Down
22 changes: 22 additions & 0 deletions test/api/entry-queryables.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,28 @@ describe('Query Operators API test cases', () => {

}
});

it('should check for projected fields after only filter is applied', async () => {
const query = makeEntries('contenttype_uid2').only(['title', 'reference'])
const result = await query.find<TEntry>();
if (result.entries) {
expect(result.entries.length).toBeGreaterThan(0);
expect(result.entries[0].reference).toBeDefined();
expect(result.entries[0].title).toBeDefined();
expect(result.entries[0]._version).toBeUndefined();
}
});

it('should ignore fields after except filter is applied', async () => {
const query = makeEntries('contenttype_uid2').except(['title', 'reference'])
const result = await query.find<TEntry>();
if (result.entries) {
expect(result.entries.length).toBeGreaterThan(0);
expect(result.entries[0].reference).toBeUndefined();
expect(result.entries[0].title).toBeUndefined();
expect(result.entries[0]._version).toBeDefined();
}
});
});

function makeEntries(contentTypeUid = ''): Entries {
Expand Down

0 comments on commit 9ce7bb6

Please sign in to comment.