-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.ts
More file actions
101 lines (90 loc) · 3.04 KB
/
request.ts
File metadata and controls
101 lines (90 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { AxiosInstance } from './types';
import { serialize } from './param-serializer';
import { APIError } from './api-error';
import { ERROR_MESSAGES } from './error-messages';
/**
* Handles array parameters properly with & separators
* React Native compatible implementation without URLSearchParams.set()
*/
function serializeParams(params: any): string {
if (!params) return '';
return serialize(params);
}
/**
* Builds the full URL with query parameters
*/
function buildFullUrl(baseURL: string | undefined, url: string, queryString: string): string {
if (url.startsWith('http://') || url.startsWith('https://')) {
return `${url}?${queryString}`;
}
const base = baseURL || '';
return `${base}${url}?${queryString}`;
}
/**
* Makes the HTTP request with proper URL handling
*/
async function makeRequest(
instance: AxiosInstance,
url: string,
requestConfig: any,
actualFullUrl: string
): Promise<any> {
// If URL is too long, use direct axios request with full URL
if (actualFullUrl.length > 2000) {
return await instance.request({
method: 'get',
url: actualFullUrl,
headers: instance.defaults.headers,
maxContentLength: Infinity,
maxBodyLength: Infinity,
});
} else {
return await instance.get(url, requestConfig);
}
}
/**
* Handles and formats errors from Axios requests
* @param err - The error object from Axios
* @returns Formatted error object with meaningful information
*/
function handleRequestError(err: any): Error {
return APIError.fromAxiosError(err);
}
export async function getData(instance: AxiosInstance, url: string, data?: any) {
try {
if (instance.stackConfig && instance.stackConfig.live_preview) {
const livePreviewParams = instance.stackConfig.live_preview;
if (livePreviewParams.enable) {
data.live_preview = livePreviewParams.live_preview || 'init';
}
if (livePreviewParams.preview_token) {
instance.defaults.headers.preview_token = livePreviewParams.preview_token;
instance.defaults.headers.live_preview = livePreviewParams.live_preview;
}
if (livePreviewParams.enable) {
// adds protocol so host is replaced and not appended
if (livePreviewParams.live_preview && livePreviewParams.live_preview !== 'init') {
if (!livePreviewParams.host) {
throw new Error(ERROR_MESSAGES.REQUEST.HOST_REQUIRED_FOR_LIVE_PREVIEW);
}
url = (livePreviewParams.host.startsWith('https://') ? '' : 'https://') + livePreviewParams.host + url;
}
}
}
const requestConfig = {
...data,
maxContentLength: Infinity,
maxBodyLength: Infinity,
};
const queryString = serializeParams(requestConfig.params);
const actualFullUrl = buildFullUrl(instance.defaults.baseURL, url, queryString);
const response = await makeRequest(instance, url, requestConfig, actualFullUrl);
if (response && response.data) {
return response.data;
} else {
throw response;
}
} catch (err: any) {
throw handleRequestError(err);
}
}