@docaohuynh/ielts-api-utils 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -5039,10 +5039,98 @@ var getCurrentDate = (overrideDateISO) => {
|
|
|
5039
5039
|
var getCurrentDateStr = (overrideDateISO) => {
|
|
5040
5040
|
return getCurrentDate(overrideDateISO).toISOString().split("T")[0];
|
|
5041
5041
|
};
|
|
5042
|
+
// src/utils/request/fetch.ts
|
|
5043
|
+
async function sendGet(url, options = {}) {
|
|
5044
|
+
const { headers = {}, timeout: timeout3 = 30000 } = options;
|
|
5045
|
+
const controller = new AbortController;
|
|
5046
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout3);
|
|
5047
|
+
try {
|
|
5048
|
+
const response = await fetch(url, {
|
|
5049
|
+
method: "GET",
|
|
5050
|
+
headers: {
|
|
5051
|
+
"Content-Type": "application/json",
|
|
5052
|
+
...headers
|
|
5053
|
+
},
|
|
5054
|
+
signal: controller.signal
|
|
5055
|
+
});
|
|
5056
|
+
clearTimeout(timeoutId);
|
|
5057
|
+
if (!response.ok) {
|
|
5058
|
+
throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`);
|
|
5059
|
+
}
|
|
5060
|
+
const data = await response.json();
|
|
5061
|
+
return {
|
|
5062
|
+
data,
|
|
5063
|
+
status: response.status,
|
|
5064
|
+
statusText: response.statusText,
|
|
5065
|
+
headers: response.headers
|
|
5066
|
+
};
|
|
5067
|
+
} catch (error) {
|
|
5068
|
+
clearTimeout(timeoutId);
|
|
5069
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
5070
|
+
throw new Error(`Request timeout after ${timeout3}ms`);
|
|
5071
|
+
}
|
|
5072
|
+
throw error;
|
|
5073
|
+
}
|
|
5074
|
+
}
|
|
5075
|
+
async function sendPost(url, body, options = {}) {
|
|
5076
|
+
const { headers = {}, timeout: timeout3 = 30000 } = options;
|
|
5077
|
+
const controller = new AbortController;
|
|
5078
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout3);
|
|
5079
|
+
try {
|
|
5080
|
+
const response = await fetch(url, {
|
|
5081
|
+
method: "POST",
|
|
5082
|
+
headers: {
|
|
5083
|
+
"Content-Type": "application/json",
|
|
5084
|
+
...headers
|
|
5085
|
+
},
|
|
5086
|
+
body: JSON.stringify(body),
|
|
5087
|
+
signal: controller.signal
|
|
5088
|
+
});
|
|
5089
|
+
clearTimeout(timeoutId);
|
|
5090
|
+
if (!response.ok) {
|
|
5091
|
+
throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`);
|
|
5092
|
+
}
|
|
5093
|
+
const data = await response.json();
|
|
5094
|
+
return {
|
|
5095
|
+
data,
|
|
5096
|
+
status: response.status,
|
|
5097
|
+
statusText: response.statusText,
|
|
5098
|
+
headers: response.headers
|
|
5099
|
+
};
|
|
5100
|
+
} catch (error) {
|
|
5101
|
+
clearTimeout(timeoutId);
|
|
5102
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
5103
|
+
throw new Error(`Request timeout after ${timeout3}ms`);
|
|
5104
|
+
}
|
|
5105
|
+
throw error;
|
|
5106
|
+
}
|
|
5107
|
+
}
|
|
5108
|
+
async function sendGetWithBearer(url, token, options = {}) {
|
|
5109
|
+
return sendGet(url, {
|
|
5110
|
+
...options,
|
|
5111
|
+
headers: {
|
|
5112
|
+
...options.headers,
|
|
5113
|
+
Authorization: `Bearer ${token}`
|
|
5114
|
+
}
|
|
5115
|
+
});
|
|
5116
|
+
}
|
|
5117
|
+
async function sendPostWithBearer(url, body, token, options = {}) {
|
|
5118
|
+
return sendPost(url, body, {
|
|
5119
|
+
...options,
|
|
5120
|
+
headers: {
|
|
5121
|
+
...options.headers,
|
|
5122
|
+
Authorization: `Bearer ${token}`
|
|
5123
|
+
}
|
|
5124
|
+
});
|
|
5125
|
+
}
|
|
5042
5126
|
export {
|
|
5043
5127
|
withTimeout,
|
|
5044
5128
|
splitIntoSentencesSimple,
|
|
5045
5129
|
splitIntoSentences,
|
|
5130
|
+
sendPostWithBearer,
|
|
5131
|
+
sendPost,
|
|
5132
|
+
sendGetWithBearer,
|
|
5133
|
+
sendGet,
|
|
5046
5134
|
replaceWordInList,
|
|
5047
5135
|
normalizeText,
|
|
5048
5136
|
getCurrentDateStr,
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP request utility functions using Bun's fetch API
|
|
3
|
+
*/
|
|
4
|
+
export interface RequestOptions {
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ResponseData<T = any> {
|
|
9
|
+
data: T;
|
|
10
|
+
status: number;
|
|
11
|
+
statusText: string;
|
|
12
|
+
headers: Headers;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Send a GET request
|
|
16
|
+
* @param url - The URL to send the request to
|
|
17
|
+
* @param options - Request options including headers and timeout
|
|
18
|
+
* @returns Promise with response data
|
|
19
|
+
*/
|
|
20
|
+
export declare function sendGet<T = any>(url: string, options?: RequestOptions): Promise<ResponseData<T>>;
|
|
21
|
+
/**
|
|
22
|
+
* Send a POST request
|
|
23
|
+
* @param url - The URL to send the request to
|
|
24
|
+
* @param body - The request body
|
|
25
|
+
* @param options - Request options including headers and timeout
|
|
26
|
+
* @returns Promise with response data
|
|
27
|
+
*/
|
|
28
|
+
export declare function sendPost<T = any>(url: string, body: any, options?: RequestOptions): Promise<ResponseData<T>>;
|
|
29
|
+
/**
|
|
30
|
+
* Send a GET request with Bearer token authentication
|
|
31
|
+
* @param url - The URL to send the request to
|
|
32
|
+
* @param token - The Bearer token
|
|
33
|
+
* @param options - Request options including headers and timeout
|
|
34
|
+
* @returns Promise with response data
|
|
35
|
+
*/
|
|
36
|
+
export declare function sendGetWithBearer<T = any>(url: string, token: string, options?: RequestOptions): Promise<ResponseData<T>>;
|
|
37
|
+
/**
|
|
38
|
+
* Send a POST request with Bearer token authentication
|
|
39
|
+
* @param url - The URL to send the request to
|
|
40
|
+
* @param body - The request body
|
|
41
|
+
* @param token - The Bearer token
|
|
42
|
+
* @param options - Request options including headers and timeout
|
|
43
|
+
* @returns Promise with response data
|
|
44
|
+
*/
|
|
45
|
+
export declare function sendPostWithBearer<T = any>(url: string, body: any, token: string, options?: RequestOptions): Promise<ResponseData<T>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './fetch';
|