Skip to content

Commit e0364b1

Browse files
Vikram KaltaVikram Kalta
authored andcommitted
fix: resolved conflicts
2 parents f06fb98 + 1ca5a94 commit e0364b1

File tree

9 files changed

+636
-360
lines changed

9 files changed

+636
-360
lines changed

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fileignoreconfig:
22
- filename: package-lock.json
3-
checksum: d388091773e9515cd3c0a5b881644775aa7b8233a405642e49133f296a4ceeeb
3+
checksum: c27c6a4629a6b1cec5e01b5db15d0e12646a1250e0cf1292ac58c561e9bc4993
44
- filename: test/unit/image-transform.spec.ts
55
checksum: 7beabdd07bd35d620668fcd97e1a303b9cbc40170bf3008a376d75ce0895de2a
66
- filename: test/utils/mocks.ts

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
### Version: 4.4.4
2+
#### Date: January-06-2025
3+
Enh: Include References on Entry UID
4+
5+
### Version: 4.4.3
6+
#### Date: November-30-2024
7+
Fix: regex method fixed for validation
8+
19
### Version: 4.4.2
210
#### Date: November-16-2024
311
Fix: Variants reset issue fix on query call

package-lock.json

Lines changed: 495 additions & 348 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
},
3535
"dependencies": {
3636
"@contentstack/core": "^1.1.4",
37-
"@contentstack/utils": "^1.3.14",
37+
"@contentstack/utils": "^1.3.15",
3838
"@types/humps": "^2.0.6",
39-
"axios": "^1.7.8",
40-
"dotenv": "^16.4.5",
39+
"axios": "^1.7.9",
40+
"dotenv": "^16.4.7",
4141
"humps": "^2.0.1",
4242
"path-browserify": "^1.0.1"
4343
},
@@ -52,7 +52,7 @@
5252
"@types/node-localstorage": "^1.3.3",
5353
"axios-mock-adapter": "^1.22.0",
5454
"babel-jest": "^29.7.0",
55-
"esbuild-plugin-file-path-extensions": "^2.1.3",
55+
"esbuild-plugin-file-path-extensions": "^2.1.4",
5656
"ignore-loader": "^0.1.2",
5757
"jest": "^29.7.0",
5858
"jest-environment-jsdom": "^29.7.0",

src/lib/entry.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ export class Entry {
9191
return this;
9292
}
9393

94+
/**
95+
* @method includeReference
96+
* @memberof Entry
97+
* @description To include the content of the referred entry in your response,
98+
* you need to use the include[] parameter and specify the UID of the reference field as value.
99+
* This function sets the include parameter to a reference field UID in the API request.
100+
* @example
101+
* const stack = contentstack.stack("apiKey", "deliveryKey", "environment");
102+
* const query = stack.contentType("contentTypeUid").entry(entry_uid).includeReference("brand").fetch()
103+
*
104+
* @param {string} referenceFieldUid - UID of the reference field to include.
105+
* @returns {Entry} - Returns the Entry instance for chaining.
106+
*/
107+
includeReference(...referenceFieldUid: (string | string[])[]): Entry {
108+
if (referenceFieldUid.length) {
109+
referenceFieldUid.forEach(value => {
110+
if (!Array.isArray(this._queryParams['include[]'])) {
111+
this._queryParams['include[]'] = [];
112+
}
113+
(this._queryParams['include[]'] as string[]).push(...(Array.isArray(value) ? value : [value]));
114+
});
115+
} else {
116+
console.error("Argument should be a String or an Array.");
117+
}
118+
return this;
119+
}
120+
94121
/**
95122
* @method includeContentType
96123
* @memberof Entry

src/lib/query.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { BaseQuery } from './base-query';
33
import { BaseQueryParameters, QueryOperation, QueryOperator, TaxonomyQueryOperation } from './types';
44
import { params, queryParams } from './internal-types';
55

6-
const safePatterns: RegExp[] = [
7-
/^[a-zA-Z0-9_.-]+$/, // Alphanumeric with underscores, periods, and dashes
8-
];
9-
106
export class Query extends BaseQuery {
117
private _contentTypeUid?: string;
128

@@ -34,10 +30,16 @@ export class Query extends BaseQuery {
3430

3531
// Validate if input matches any of the safe, pre-approved patterns
3632
private isValidRegexPattern(input: string): boolean {
37-
if (!this.isValidAlphanumeric(input)) {
38-
return false;
33+
const validRegex = /^[a-zA-Z0-9|^$.*+?()[\]{}\\-]+$/; // Allow only safe regex characters
34+
if (!validRegex.test(input)) {
35+
return false;
36+
}
37+
try {
38+
new RegExp(input);
39+
return true;
40+
} catch (e) {
41+
return false;
3942
}
40-
return safePatterns.some(pattern => pattern.test(input));
4143
}
4244

4345
private isValidValue(value: any[]): boolean {

test/api/entry.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ describe('Entry API tests', () => {
5050
expect(result.created_by).toBeDefined();
5151
expect(result.updated_by).toBeDefined();
5252
});
53+
it('should check for include reference', async () => {
54+
const result = await makeEntry(entryUid).includeReference('author').fetch<TEntry>();
55+
expect(result.title).toBeDefined();
56+
expect(result.author).toBeDefined();
57+
expect(result.title).toBeDefined();
58+
expect(result.url).toBeDefined();
59+
expect(result._version).toBeDefined();
60+
expect(result.publish_details).toBeDefined();
61+
});
5362
});
5463
function makeEntry(uid = ''): Entry {
5564
const entry = stack.contentType('author').entry(uid);

test/unit/entry.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ describe('Entry class', () => {
3737
expect(entry._queryParams.include_fallback).toBe('true');
3838
});
3939

40+
it('should set the include parameter to the given reference field UID', () => {
41+
const referenceFieldUid = 'referenceFieldUid';
42+
const returnedValue = entry.includeReference(referenceFieldUid);
43+
expect(returnedValue).toBeInstanceOf(Entry);
44+
expect(entry._queryParams['include[]']).toContain(referenceFieldUid);
45+
});
46+
4047
it('should add "include_metadata" in _queryParams when includeMetadata method is called', () => {
4148
const returnedValue = entry.includeMetadata();
4249
expect(returnedValue).toBeInstanceOf(Entry);

test/unit/query.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,82 @@ describe('Query class', () => {
8383
expect(mainQuery2._parameters).toHaveProperty('$and', [subQuery1._parameters, subQuery2._parameters]);
8484
});
8585

86+
it('should result in error when regex method is called with invalid regex', async () => {
87+
const regexQuery = getQueryObject(client, 'your-referenced-content-type-uid');
88+
expect(() => regexQuery.regex("fieldUid", "[a-z")).toThrow("Invalid regexPattern: Must be a valid regular expression");
89+
});
90+
91+
it('should add a regex parameter to _parameters when regex method is called with valid regex', () => {
92+
query.regex('fieldUid', '^ABCXYZ123');
93+
expect(query._parameters['fieldUid']).toEqual({ $regex: '^ABCXYZ123' });
94+
});
95+
96+
it('should add a containedIn parameter to _parameters', () => {
97+
query.containedIn('fieldUid', ['value1', 'value2']);
98+
expect(query._parameters['fieldUid']).toEqual({ '$in': ['value1', 'value2'] });
99+
});
100+
101+
it('should add a notContainedIn parameter to _parameters', () => {
102+
query.notContainedIn('fieldUid', ['value1', 'value2']);
103+
expect(query._parameters['fieldUid']).toEqual({ '$nin': ['value1', 'value2'] });
104+
});
105+
106+
it('should add an exists parameter to _parameters', () => {
107+
query.exists('fieldUid');
108+
expect(query._parameters['fieldUid']).toEqual({ '$exists': true });
109+
});
110+
111+
it('should add a notExists parameter to _parameters', () => {
112+
query.notExists('fieldUid');
113+
expect(query._parameters['fieldUid']).toEqual({ '$exists': false });
114+
});
115+
116+
it('should add an equalTo parameter to _parameters', () => {
117+
query.equalTo('fieldUid', 'value');
118+
expect(query._parameters['fieldUid']).toEqual('value');
119+
});
120+
121+
it('should add a notEqualTo parameter to _parameters', () => {
122+
query.notEqualTo('fieldUid', 'value');
123+
expect(query._parameters['fieldUid']).toEqual({ '$ne': 'value' });
124+
});
125+
126+
it('should add a lessThan parameter to _parameters', () => {
127+
query.lessThan('fieldUid', 10);
128+
expect(query._parameters['fieldUid']).toEqual({ '$lt': 10 });
129+
});
130+
131+
it('should add a lessThanOrEqualTo parameter to _parameters', () => {
132+
query.lessThanOrEqualTo('fieldUid', 10);
133+
expect(query._parameters['fieldUid']).toEqual({ '$lte': 10 });
134+
});
135+
136+
it('should add a greaterThan parameter to _parameters', () => {
137+
query.greaterThan('fieldUid', 10);
138+
expect(query._parameters['fieldUid']).toEqual({ '$gt': 10 });
139+
});
140+
141+
it('should add a greaterThanOrEqualTo parameter to _parameters', () => {
142+
query.greaterThanOrEqualTo('fieldUid', 10);
143+
expect(query._parameters['fieldUid']).toEqual({ '$gte': 10 });
144+
});
145+
146+
it('should add a tags parameter to _parameters', () => {
147+
query.tags(['tag1', 'tag2']);
148+
expect(query._parameters['tags']).toEqual(['tag1', 'tag2']);
149+
});
150+
151+
it('should add a search parameter to _queryParams', () => {
152+
query.search('searchKey');
153+
expect(query._queryParams['typeahead']).toEqual('searchKey');
154+
});
155+
156+
it('should provide proper response when find method is called', async () => {
157+
mockClient.onGet(`/content_types/contentTypeUid/entries`).reply(200, entryFindMock);
158+
const returnedValue = await query.find();
159+
expect(returnedValue).toEqual(entryFindMock);
160+
});
161+
86162
it('should provide proper response when find method is called', async () => {
87163
mockClient.onGet(`/content_types/contentTypeUid/entries`).reply(200, entryFindMock);
88164
const returnedValue = await query.find();

0 commit comments

Comments
 (0)