Skip to content

Commit b816bf2

Browse files
authored
solve utils
1 parent 9fa5b3b commit b816bf2

File tree

3 files changed

+17
-47
lines changed

3 files changed

+17
-47
lines changed

lib/utils/fetch-wrapper.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ export const fetchWrapper = {
44

55
async function ajax(method, url, headers, params, options) {
66
const base_header = new Headers(headers);
7-
base_header.append("Content-Type", "application/json");
87

9-
if (process.env.NODE_ENV != "production") {
10-
base_header.append("x-debug", "SuyPBUZPau1F4Jd")
8+
if (!headers["Content-Type"]) {
9+
base_header.append("Content-Type", "application/json");
1110
}
1211

1312
let requestOptions = <any>{
@@ -17,9 +16,13 @@ async function ajax(method, url, headers, params, options) {
1716

1817
if (params) {
1918
if (method == 'GET') {
20-
url += '?'+ new URLSearchParams(params)
19+
url += '?' + new URLSearchParams(params)
2120
} else {
22-
requestOptions.body = JSON.stringify(params)
21+
if (headers["Content-Type"] && headers["Content-Type"] == "application/x-www-form-urlencoded") {
22+
requestOptions.body = new URLSearchParams(params)
23+
} else {
24+
requestOptions.body = JSON.stringify(params)
25+
}
2326
}
2427
}
2528

@@ -29,11 +32,12 @@ async function ajax(method, url, headers, params, options) {
2932
requestOptions.headers = { ...requestOptions.headers, ...headers }
3033
}
3134
}
32-
const response = await fetch(url, requestOptions);
33-
return response
34-
}
3535

36-
async function handleResponse(response) {
37-
const json = await response.json()
38-
return json
39-
}
36+
try {
37+
const response = await fetch(url, requestOptions)
38+
return response
39+
} catch (err) {
40+
let r = new Response(JSON.stringify({error: err.message}), { status: 500, headers: new Headers({"Content-Type": "application/json"}) })
41+
return r
42+
}
43+
}

lib/utils/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ export * from "./fetch-wrapper";
33
export * from "./marginCalculator";
44
export * from "./matchEventCalculator";
55
export * from "./mathFunction";
6-
export * from "./stringsFunctions";
7-
export * from "./useInfiniteScroll"
6+
export * from "./stringsFunctions";

lib/utils/useInfiniteScroll.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)