Skip to content

Commit 93990ad

Browse files
Merge pull request #126 from contentstack/fix/dx-3440
fix: Fixed URI To Long error
2 parents 0f894e6 + 276993a commit 93990ad

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

src/lib/request.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
import { AxiosInstance } from './types';
22

3+
/**
4+
* Handles array parameters properly with & separators
5+
*/
6+
function serializeParams(params: any): string {
7+
if (!params) return '';
8+
const urlParams = new URLSearchParams();
9+
Object.keys(params).forEach(key => {
10+
const value = params[key];
11+
if (Array.isArray(value)) {
12+
value.forEach(item => {
13+
urlParams.append(key, item);
14+
});
15+
} else {
16+
urlParams.set(key, value);
17+
}
18+
});
19+
20+
return urlParams.toString();
21+
}
22+
23+
/**
24+
* Builds the full URL with query parameters
25+
*/
26+
function buildFullUrl(baseURL: string | undefined, url: string, queryString: string): string {
27+
const base = baseURL || '';
28+
return `${base}${url}?${queryString}`;
29+
}
30+
31+
/**
32+
* Makes the HTTP request with proper URL handling
33+
*/
34+
async function makeRequest(instance: AxiosInstance, url: string, requestConfig: any, actualFullUrl: string): Promise<any> {
35+
// If URL is too long, use direct axios request with full URL
36+
if (actualFullUrl.length > 2000) {
37+
return await instance.request({
38+
method: 'get',
39+
url: actualFullUrl,
40+
headers: instance.defaults.headers,
41+
maxContentLength: Infinity,
42+
maxBodyLength: Infinity,
43+
});
44+
} else {
45+
return await instance.get(url, requestConfig);
46+
}
47+
}
48+
349
export async function getData(instance: AxiosInstance, url: string, data?: any) {
450
try {
551
if (instance.stackConfig && instance.stackConfig.live_preview) {
@@ -23,7 +69,15 @@ export async function getData(instance: AxiosInstance, url: string, data?: any)
2369
}
2470
}
2571
}
26-
const response = await instance.get(url, data);
72+
73+
const requestConfig = {
74+
...data,
75+
maxContentLength: Infinity,
76+
maxBodyLength: Infinity
77+
};
78+
const queryString = serializeParams(requestConfig.params);
79+
const actualFullUrl = buildFullUrl(instance.defaults.baseURL, url, queryString);
80+
const response = await makeRequest(instance, url, requestConfig, actualFullUrl);
2781

2882
if (response && response.data) {
2983
return response.data;

0 commit comments

Comments
 (0)