zz-shopify-components 0.0.2 → 0.0.3
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/package.json +1 -1
- package/assets/http-request.js +0 -73
package/package.json
CHANGED
package/assets/http-request.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
(function () {
|
|
2
|
-
const DEFAULT_BASE_URL = 'https://h130-app-server-us.hoverx1.cn';
|
|
3
|
-
|
|
4
|
-
const DEFAULT_TIMEOUT = 60000;
|
|
5
|
-
|
|
6
|
-
function getToken() {
|
|
7
|
-
return localStorage.getItem('token') || '';
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function buildUrl(url, params = {}) {
|
|
11
|
-
const queryString = new URLSearchParams(params).toString();
|
|
12
|
-
return queryString ? `${url}?${queryString}` : url;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function timeoutFetch(fetchPromise, timeout = DEFAULT_TIMEOUT) {
|
|
16
|
-
return Promise.race([
|
|
17
|
-
fetchPromise,
|
|
18
|
-
new Promise((_, reject) =>
|
|
19
|
-
setTimeout(() => reject(new Error('请求超时,请重试')), timeout)
|
|
20
|
-
),
|
|
21
|
-
]);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function request(method, url, params = {}, options = {}) {
|
|
25
|
-
const baseUrl = options.baseUrl || DEFAULT_BASE_URL;
|
|
26
|
-
|
|
27
|
-
const timeout = options.timeout || DEFAULT_TIMEOUT;
|
|
28
|
-
|
|
29
|
-
const fullUrl = baseUrl + url;
|
|
30
|
-
|
|
31
|
-
let fetchOptions = {
|
|
32
|
-
method: method.toUpperCase(),
|
|
33
|
-
headers: {
|
|
34
|
-
'Content-Type': 'application/json',
|
|
35
|
-
...(options.headers || {}),
|
|
36
|
-
},
|
|
37
|
-
...options,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
if (method.toLowerCase() === 'get') {
|
|
41
|
-
return timeoutFetch(
|
|
42
|
-
fetch(buildUrl(fullUrl, params), fetchOptions),
|
|
43
|
-
timeout
|
|
44
|
-
).then(handleResponse);
|
|
45
|
-
} else {
|
|
46
|
-
fetchOptions.body = JSON.stringify(params);
|
|
47
|
-
return timeoutFetch(fetch(fullUrl, fetchOptions), timeout).then(
|
|
48
|
-
handleResponse
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function handleResponse(response) {
|
|
54
|
-
if (!response.ok) {
|
|
55
|
-
if (response.status === 401) {
|
|
56
|
-
// 未登录或登录过期,即将跳转登录页
|
|
57
|
-
}
|
|
58
|
-
return response.json().then((err) => {
|
|
59
|
-
throw err;
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
return response.json();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
window.httpRequest = {
|
|
66
|
-
get: function (url, params = {}, options = {}) {
|
|
67
|
-
return request('get', url, params, options);
|
|
68
|
-
},
|
|
69
|
-
post: function (url, params = {}, options = {}) {
|
|
70
|
-
return request('post', url, params, options);
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
})();
|