Skip to content

Commit db4b21a

Browse files
Merge pull request #134 from contentstack/fix/URLSearchParams.set-issue
fix: React native compatibility fix
2 parents 1f16911 + 4e89e76 commit db4b21a

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

.talismanrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ fileignoreconfig:
88
checksum: 5baabd7d2c391648163f9371f0e5e9484f8fb90fa2284cfc378732ec3192c193
99
- filename: test/request.spec.ts
1010
checksum: 87afd3bb570fd52437404cbe69a39311ad8a8c73bca9d075ecf88652fd3e13f6
11+
- filename: src/lib/request.ts
12+
checksum: 86d761c4f50fcf377e52c98e0c4db6f06be955790fc5a0f2ba8fe32a88c60825
1113
version: ""

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Change log
22

3+
### Version: 1.3.1
4+
#### Date: Sept-01-2025
5+
- Fix: Replace URLSearchParams.set() with React Native compatible implementation
6+
37
### Version: 1.3.0
48
#### Date: Aug-25-2025
59
- Fix: Remove custom error object and throw axios error

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/core",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"type": "commonjs",
55
"main": "./dist/cjs/src/index.js",
66
"types": "./dist/cjs/src/index.d.ts",

src/lib/request.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ import { AxiosInstance } from './types';
22

33
/**
44
* Handles array parameters properly with & separators
5+
* React Native compatible implementation without URLSearchParams.set()
56
*/
67
function serializeParams(params: any): string {
78
if (!params) return '';
8-
const urlParams = new URLSearchParams();
9+
10+
const parts: string[] = [];
911
Object.keys(params).forEach(key => {
1012
const value = params[key];
1113
if (Array.isArray(value)) {
1214
value.forEach(item => {
13-
urlParams.append(key, item);
15+
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(item)}`);
1416
});
15-
} else {
16-
urlParams.set(key, value);
17+
} else if (value !== null && value !== undefined) {
18+
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
1719
}
1820
});
1921

20-
return urlParams.toString();
22+
return parts.join('&');
2123
}
2224

2325
/**

test/request.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,29 @@ describe('Request tests', () => {
319319
const result = await getData(client, url, requestData);
320320
expect(result).toEqual(mockResponse);
321321
});
322+
323+
it('should handle React Native compatibility by avoiding URLSearchParams.set()', async () => {
324+
const client = httpClient({});
325+
const mock = new MockAdapter(client as any);
326+
const url = '/your-api-endpoint';
327+
const mockResponse = { data: 'mocked' };
328+
const requestData = {
329+
params: {
330+
limit: 10,
331+
skip: 0,
332+
include: ['field1', 'field2'],
333+
query: { title: 'test' },
334+
nullValue: null,
335+
undefinedValue: undefined,
336+
emptyString: '',
337+
specialChars: 'hello world & more'
338+
}
339+
};
340+
341+
mock.onGet(url).reply(200, mockResponse);
342+
343+
// The test passes if no "URLSearchParams.set is not implemented" error is thrown
344+
const result = await getData(client, url, requestData);
345+
expect(result).toEqual(mockResponse);
346+
});
322347
});

0 commit comments

Comments
 (0)