Skip to content
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

fix: added support for arrays in only and except #68

Merged
merged 8 commits into from
Dec 1, 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: 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 {
vkalta marked this conversation as resolved.
Show resolved Hide resolved
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 {
vkalta marked this conversation as resolved.
Show resolved Hide resolved
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
Loading