Skip to content

Commit 15a5250

Browse files
fix: Fixed URI To Long error
1 parent 0c861e0 commit 15a5250

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

src/lib/request.ts

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

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

2885
if (response && response.data) {
2986
return response.data;

0 commit comments

Comments
 (0)