@scx-js/scx-http 0.0.1 → 0.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/FetchError.js +13 -13
- package/HttpFieldName.js +7 -7
- package/HttpMethod.js +10 -10
- package/JsonVOError.js +11 -11
- package/MediaType.js +10 -10
- package/ResponseNotOKError.js +13 -13
- package/ScxFetch.js +178 -178
- package/ScxFetchHelper.js +73 -73
- package/ScxFetchOptions.js +45 -45
- package/ScxFetchPromise.js +33 -33
- package/ScxFetchResponse.js +68 -68
- package/ScxFetchResponseType.js +42 -42
- package/ScxReq.js +91 -91
- package/ScxReqHelper.js +45 -45
- package/index.js +14 -14
- package/package.json +17 -17
package/ScxFetchOptions.js
CHANGED
@@ -1,45 +1,45 @@
|
|
1
|
-
class ScxFetchOptions {
|
2
|
-
|
3
|
-
/**
|
4
|
-
* 方法
|
5
|
-
*/
|
6
|
-
method;
|
7
|
-
|
8
|
-
/**
|
9
|
-
* 请求头
|
10
|
-
*/
|
11
|
-
headers;
|
12
|
-
|
13
|
-
/**
|
14
|
-
* 默认想如何处理返回值 取值如下 [auto, arrayBuffer, blob, formData, json, text, ]
|
15
|
-
*
|
16
|
-
*/
|
17
|
-
responseType;
|
18
|
-
|
19
|
-
/**
|
20
|
-
* 是否使用 BodyPromise 如果为 true 则 body 为 Promise 对象, false 则会将 body 数据读完再返回
|
21
|
-
* @type {boolean}
|
22
|
-
*/
|
23
|
-
useBodyPromise;
|
24
|
-
|
25
|
-
/**
|
26
|
-
* 前置处理器
|
27
|
-
* @type {boolean}
|
28
|
-
*/
|
29
|
-
usePreInterceptor;
|
30
|
-
|
31
|
-
/**
|
32
|
-
* 后置处理器
|
33
|
-
* @type {boolean}
|
34
|
-
*/
|
35
|
-
usePostInterceptor;
|
36
|
-
|
37
|
-
/**
|
38
|
-
* 字符集
|
39
|
-
*/
|
40
|
-
charset;
|
41
|
-
}
|
42
|
-
|
43
|
-
export {
|
44
|
-
ScxFetchOptions,
|
45
|
-
};
|
1
|
+
class ScxFetchOptions {
|
2
|
+
|
3
|
+
/**
|
4
|
+
* 方法
|
5
|
+
*/
|
6
|
+
method;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* 请求头
|
10
|
+
*/
|
11
|
+
headers;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* 默认想如何处理返回值 取值如下 [auto, arrayBuffer, blob, formData, json, text, ]
|
15
|
+
*
|
16
|
+
*/
|
17
|
+
responseType;
|
18
|
+
|
19
|
+
/**
|
20
|
+
* 是否使用 BodyPromise 如果为 true 则 body 为 Promise 对象, false 则会将 body 数据读完再返回
|
21
|
+
* @type {boolean}
|
22
|
+
*/
|
23
|
+
useBodyPromise;
|
24
|
+
|
25
|
+
/**
|
26
|
+
* 前置处理器
|
27
|
+
* @type {boolean}
|
28
|
+
*/
|
29
|
+
usePreInterceptor;
|
30
|
+
|
31
|
+
/**
|
32
|
+
* 后置处理器
|
33
|
+
* @type {boolean}
|
34
|
+
*/
|
35
|
+
usePostInterceptor;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* 字符集
|
39
|
+
*/
|
40
|
+
charset;
|
41
|
+
}
|
42
|
+
|
43
|
+
export {
|
44
|
+
ScxFetchOptions,
|
45
|
+
};
|
package/ScxFetchPromise.js
CHANGED
@@ -1,33 +1,33 @@
|
|
1
|
-
class ScxFetchPromise extends Promise {
|
2
|
-
|
3
|
-
abortController;
|
4
|
-
|
5
|
-
then(onfulfilled, onrejected) {
|
6
|
-
const s = super.then(onfulfilled, onrejected);
|
7
|
-
s.abortController = this.abortController;
|
8
|
-
return s;
|
9
|
-
}
|
10
|
-
|
11
|
-
catch(onrejected) {
|
12
|
-
const s = super.catch(onrejected);
|
13
|
-
s.abortController = this.abortController;
|
14
|
-
return s;
|
15
|
-
}
|
16
|
-
|
17
|
-
finally(onFinally) {
|
18
|
-
const s = super.finally(onFinally);
|
19
|
-
s.abortController = this.abortController;
|
20
|
-
return s;
|
21
|
-
}
|
22
|
-
|
23
|
-
cancel(reason) {
|
24
|
-
if (this.abortController) {
|
25
|
-
this.abortController.abort(reason);
|
26
|
-
}
|
27
|
-
}
|
28
|
-
|
29
|
-
}
|
30
|
-
|
31
|
-
export {
|
32
|
-
ScxFetchPromise,
|
33
|
-
};
|
1
|
+
class ScxFetchPromise extends Promise {
|
2
|
+
|
3
|
+
abortController;
|
4
|
+
|
5
|
+
then(onfulfilled, onrejected) {
|
6
|
+
const s = super.then(onfulfilled, onrejected);
|
7
|
+
s.abortController = this.abortController;
|
8
|
+
return s;
|
9
|
+
}
|
10
|
+
|
11
|
+
catch(onrejected) {
|
12
|
+
const s = super.catch(onrejected);
|
13
|
+
s.abortController = this.abortController;
|
14
|
+
return s;
|
15
|
+
}
|
16
|
+
|
17
|
+
finally(onFinally) {
|
18
|
+
const s = super.finally(onFinally);
|
19
|
+
s.abortController = this.abortController;
|
20
|
+
return s;
|
21
|
+
}
|
22
|
+
|
23
|
+
cancel(reason) {
|
24
|
+
if (this.abortController) {
|
25
|
+
this.abortController.abort(reason);
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
export {
|
32
|
+
ScxFetchPromise,
|
33
|
+
};
|
package/ScxFetchResponse.js
CHANGED
@@ -1,68 +1,68 @@
|
|
1
|
-
import {ScxFetchResponseType} from "./ScxFetchResponseType.js";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* ScxFetch 响应体
|
5
|
-
*/
|
6
|
-
class ScxFetchResponse {
|
7
|
-
|
8
|
-
headers;
|
9
|
-
type;
|
10
|
-
url;
|
11
|
-
responseType;
|
12
|
-
body;
|
13
|
-
|
14
|
-
constructor(headers, type, url, body, responseType) {
|
15
|
-
this.headers = headers;
|
16
|
-
this.type = type;
|
17
|
-
this.url = url;
|
18
|
-
this.body = body;
|
19
|
-
this.responseType = responseType;
|
20
|
-
}
|
21
|
-
|
22
|
-
/**
|
23
|
-
* 根据 响应类型 获取对应的 body 处理器
|
24
|
-
* @param responseType
|
25
|
-
* @param response
|
26
|
-
* @return {Promise<ArrayBuffer>|ArrayBuffer|Promise<Blob>|Blob|*|Promise<FormData>}
|
27
|
-
*/
|
28
|
-
static getBodyPromise(responseType, response) {
|
29
|
-
if (responseType === ScxFetchResponseType.JSON) {
|
30
|
-
return response.json();
|
31
|
-
} else if (responseType === ScxFetchResponseType.ARRAY_BUFFER) {
|
32
|
-
return response.arrayBuffer();
|
33
|
-
} else if (responseType === ScxFetchResponseType.BLOB) {
|
34
|
-
return response.blob();
|
35
|
-
} else if (responseType === ScxFetchResponseType.FORM_DATA) {
|
36
|
-
return response.formData();
|
37
|
-
} else if (responseType === ScxFetchResponseType.TEXT) {
|
38
|
-
return response.text();
|
39
|
-
} else {
|
40
|
-
return response.arrayBuffer();
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
/**
|
45
|
-
* 创建 ScxFetchResponse
|
46
|
-
* @param response
|
47
|
-
* @param defaultResponseType
|
48
|
-
* @param useBodyPromise
|
49
|
-
* @return {Promise<unknown>}
|
50
|
-
*/
|
51
|
-
static create(response, defaultResponseType, useBodyPromise) {
|
52
|
-
return new Promise((resolve, reject) => {
|
53
|
-
const responseType = ScxFetchResponseType.getByHeaders(response.headers, defaultResponseType);
|
54
|
-
if (useBodyPromise) {
|
55
|
-
resolve(new ScxFetchResponse(response.headers, response.type, response.url, ScxFetchResponse.getBodyPromise(responseType, response), responseType));
|
56
|
-
} else {
|
57
|
-
ScxFetchResponse.getBodyPromise(responseType, response)
|
58
|
-
.then(body => resolve(new ScxFetchResponse(response.headers, response.type, response.url, body, responseType)))
|
59
|
-
.catch(e => reject(e));
|
60
|
-
}
|
61
|
-
});
|
62
|
-
}
|
63
|
-
|
64
|
-
}
|
65
|
-
|
66
|
-
export {
|
67
|
-
ScxFetchResponse,
|
68
|
-
};
|
1
|
+
import {ScxFetchResponseType} from "./ScxFetchResponseType.js";
|
2
|
+
|
3
|
+
/**
|
4
|
+
* ScxFetch 响应体
|
5
|
+
*/
|
6
|
+
class ScxFetchResponse {
|
7
|
+
|
8
|
+
headers;
|
9
|
+
type;
|
10
|
+
url;
|
11
|
+
responseType;
|
12
|
+
body;
|
13
|
+
|
14
|
+
constructor(headers, type, url, body, responseType) {
|
15
|
+
this.headers = headers;
|
16
|
+
this.type = type;
|
17
|
+
this.url = url;
|
18
|
+
this.body = body;
|
19
|
+
this.responseType = responseType;
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
* 根据 响应类型 获取对应的 body 处理器
|
24
|
+
* @param responseType
|
25
|
+
* @param response
|
26
|
+
* @return {Promise<ArrayBuffer>|ArrayBuffer|Promise<Blob>|Blob|*|Promise<FormData>}
|
27
|
+
*/
|
28
|
+
static getBodyPromise(responseType, response) {
|
29
|
+
if (responseType === ScxFetchResponseType.JSON) {
|
30
|
+
return response.json();
|
31
|
+
} else if (responseType === ScxFetchResponseType.ARRAY_BUFFER) {
|
32
|
+
return response.arrayBuffer();
|
33
|
+
} else if (responseType === ScxFetchResponseType.BLOB) {
|
34
|
+
return response.blob();
|
35
|
+
} else if (responseType === ScxFetchResponseType.FORM_DATA) {
|
36
|
+
return response.formData();
|
37
|
+
} else if (responseType === ScxFetchResponseType.TEXT) {
|
38
|
+
return response.text();
|
39
|
+
} else {
|
40
|
+
return response.arrayBuffer();
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* 创建 ScxFetchResponse
|
46
|
+
* @param response
|
47
|
+
* @param defaultResponseType
|
48
|
+
* @param useBodyPromise
|
49
|
+
* @return {Promise<unknown>}
|
50
|
+
*/
|
51
|
+
static create(response, defaultResponseType, useBodyPromise) {
|
52
|
+
return new Promise((resolve, reject) => {
|
53
|
+
const responseType = ScxFetchResponseType.getByHeaders(response.headers, defaultResponseType);
|
54
|
+
if (useBodyPromise) {
|
55
|
+
resolve(new ScxFetchResponse(response.headers, response.type, response.url, ScxFetchResponse.getBodyPromise(responseType, response), responseType));
|
56
|
+
} else {
|
57
|
+
ScxFetchResponse.getBodyPromise(responseType, response)
|
58
|
+
.then(body => resolve(new ScxFetchResponse(response.headers, response.type, response.url, body, responseType)))
|
59
|
+
.catch(e => reject(e));
|
60
|
+
}
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
}
|
65
|
+
|
66
|
+
export {
|
67
|
+
ScxFetchResponse,
|
68
|
+
};
|
package/ScxFetchResponseType.js
CHANGED
@@ -1,42 +1,42 @@
|
|
1
|
-
import {HttpFieldName} from "./HttpFieldName.js";
|
2
|
-
import {MediaType} from "./MediaType.js";
|
3
|
-
|
4
|
-
class ScxFetchResponseType {
|
5
|
-
|
6
|
-
static ARRAY_BUFFER = "arrayBuffer";
|
7
|
-
static BLOB = "blob";
|
8
|
-
static FORM_DATA = "formData";
|
9
|
-
static JSON = "json";
|
10
|
-
static TEXT = "text";
|
11
|
-
static AUTO = "auto";
|
12
|
-
|
13
|
-
/**
|
14
|
-
* 获取 header 中的 CONTENT_TYPE 判断相应的类型
|
15
|
-
* @param headers
|
16
|
-
* @param defaultType 优先的返回值
|
17
|
-
*/
|
18
|
-
static getByHeaders(headers, defaultType) {
|
19
|
-
if (defaultType && defaultType !== ScxFetchResponseType.AUTO) {
|
20
|
-
return defaultType;
|
21
|
-
}
|
22
|
-
let contentType = headers.get(HttpFieldName.CONTENT_TYPE);
|
23
|
-
if (contentType == null) {
|
24
|
-
return ScxFetchResponseType.ARRAY_BUFFER;
|
25
|
-
}
|
26
|
-
contentType = contentType.toLowerCase();
|
27
|
-
if (contentType.startsWith(MediaType.APPLICATION_JSON)) {
|
28
|
-
return ScxFetchResponseType.JSON;
|
29
|
-
} else if (contentType.startsWith(MediaType.TEXT_ANY) || contentType.startsWith(MediaType.APPLICATION_XML)) {
|
30
|
-
return ScxFetchResponseType.TEXT;
|
31
|
-
} else if (contentType.startsWith(MediaType.MULTIPART_FORM_DATA)) {
|
32
|
-
return ScxFetchResponseType.FORM_DATA;
|
33
|
-
} else {
|
34
|
-
return ScxFetchResponseType.ARRAY_BUFFER;
|
35
|
-
}
|
36
|
-
}
|
37
|
-
|
38
|
-
}
|
39
|
-
|
40
|
-
export {
|
41
|
-
ScxFetchResponseType,
|
42
|
-
};
|
1
|
+
import {HttpFieldName} from "./HttpFieldName.js";
|
2
|
+
import {MediaType} from "./MediaType.js";
|
3
|
+
|
4
|
+
class ScxFetchResponseType {
|
5
|
+
|
6
|
+
static ARRAY_BUFFER = "arrayBuffer";
|
7
|
+
static BLOB = "blob";
|
8
|
+
static FORM_DATA = "formData";
|
9
|
+
static JSON = "json";
|
10
|
+
static TEXT = "text";
|
11
|
+
static AUTO = "auto";
|
12
|
+
|
13
|
+
/**
|
14
|
+
* 获取 header 中的 CONTENT_TYPE 判断相应的类型
|
15
|
+
* @param headers
|
16
|
+
* @param defaultType 优先的返回值
|
17
|
+
*/
|
18
|
+
static getByHeaders(headers, defaultType) {
|
19
|
+
if (defaultType && defaultType !== ScxFetchResponseType.AUTO) {
|
20
|
+
return defaultType;
|
21
|
+
}
|
22
|
+
let contentType = headers.get(HttpFieldName.CONTENT_TYPE);
|
23
|
+
if (contentType == null) {
|
24
|
+
return ScxFetchResponseType.ARRAY_BUFFER;
|
25
|
+
}
|
26
|
+
contentType = contentType.toLowerCase();
|
27
|
+
if (contentType.startsWith(MediaType.APPLICATION_JSON)) {
|
28
|
+
return ScxFetchResponseType.JSON;
|
29
|
+
} else if (contentType.startsWith(MediaType.TEXT_ANY) || contentType.startsWith(MediaType.APPLICATION_XML)) {
|
30
|
+
return ScxFetchResponseType.TEXT;
|
31
|
+
} else if (contentType.startsWith(MediaType.MULTIPART_FORM_DATA)) {
|
32
|
+
return ScxFetchResponseType.FORM_DATA;
|
33
|
+
} else {
|
34
|
+
return ScxFetchResponseType.ARRAY_BUFFER;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
}
|
39
|
+
|
40
|
+
export {
|
41
|
+
ScxFetchResponseType,
|
42
|
+
};
|
package/ScxReq.js
CHANGED
@@ -1,91 +1,91 @@
|
|
1
|
-
import {ScxFetch} from "./ScxFetch.js";
|
2
|
-
import {inject} from "vue";
|
3
|
-
import {jsonVoProcessor, setResponseType} from "./ScxReqHelper.js";
|
4
|
-
|
5
|
-
/**
|
6
|
-
* 针对后台的 JsonVo 和 DataJsonVo 对 ScxFetch 进行一次包装
|
7
|
-
* 1, 将 JsonVo.ok() 和 JsonVo.fail() 区分开
|
8
|
-
* 2, 解构 JsonVo 返回的 data 字段
|
9
|
-
*/
|
10
|
-
class ScxReq {
|
11
|
-
|
12
|
-
scxFetch;
|
13
|
-
|
14
|
-
baseURL;
|
15
|
-
|
16
|
-
constructor(scxFetch) {
|
17
|
-
if (scxFetch instanceof ScxFetch) {
|
18
|
-
this.scxFetch = scxFetch;
|
19
|
-
} else {
|
20
|
-
this.scxFetch = new ScxFetch(scxFetch);
|
21
|
-
}
|
22
|
-
this.baseURL = this.scxFetch.baseURL;
|
23
|
-
}
|
24
|
-
|
25
|
-
/**
|
26
|
-
* GET 方法
|
27
|
-
* @param url {URL | string}
|
28
|
-
* @param body {Object}
|
29
|
-
* @param options {ScxFetchOptions}
|
30
|
-
* @returns {ScxFetchPromise<unknown>}
|
31
|
-
*/
|
32
|
-
get(url, body = null, options = {}) {
|
33
|
-
return jsonVoProcessor(this.scxFetch.get(url, body, setResponseType(options)));
|
34
|
-
}
|
35
|
-
|
36
|
-
/**
|
37
|
-
* POST 方法
|
38
|
-
* @param url {URL | string}
|
39
|
-
* @param body {Object}
|
40
|
-
* @param options {ScxFetchOptions}
|
41
|
-
* @returns {ScxFetchPromise<unknown>}
|
42
|
-
*/
|
43
|
-
post(url, body = null, options = {}) {
|
44
|
-
return jsonVoProcessor(this.scxFetch.post(url, body, setResponseType(options)));
|
45
|
-
}
|
46
|
-
|
47
|
-
/**
|
48
|
-
* PUT 方法
|
49
|
-
* @param url {URL | string}
|
50
|
-
* @param body {Object}
|
51
|
-
* @param options {ScxFetchOptions}
|
52
|
-
* @returns {ScxFetchPromise<unknown>}
|
53
|
-
*/
|
54
|
-
put(url, body = null, options = {}) {
|
55
|
-
return jsonVoProcessor(this.scxFetch.put(url, body, setResponseType(options)));
|
56
|
-
}
|
57
|
-
|
58
|
-
/**
|
59
|
-
* DELETE 方法
|
60
|
-
* @param url {URL | string}
|
61
|
-
* @param body {Object}
|
62
|
-
* @param options {ScxFetchOptions}
|
63
|
-
* @returns {ScxFetchPromise<unknown>}
|
64
|
-
*/
|
65
|
-
delete(url, body = null, options = {}) {
|
66
|
-
return jsonVoProcessor(this.scxFetch.delete(url, body, setResponseType(options)));
|
67
|
-
}
|
68
|
-
|
69
|
-
|
70
|
-
install(app) {
|
71
|
-
app.provide(scxReqKey, this);
|
72
|
-
}
|
73
|
-
|
74
|
-
}
|
75
|
-
|
76
|
-
|
77
|
-
/**
|
78
|
-
*
|
79
|
-
* @type {string}
|
80
|
-
*/
|
81
|
-
const scxReqKey = "scx-req";
|
82
|
-
|
83
|
-
/**
|
84
|
-
*
|
85
|
-
* @returns {ScxReq}
|
86
|
-
*/
|
87
|
-
function useScxReq() {
|
88
|
-
return inject(scxReqKey);
|
89
|
-
}
|
90
|
-
|
91
|
-
export {ScxReq, useScxReq};
|
1
|
+
import {ScxFetch} from "./ScxFetch.js";
|
2
|
+
import {inject} from "vue";
|
3
|
+
import {jsonVoProcessor, setResponseType} from "./ScxReqHelper.js";
|
4
|
+
|
5
|
+
/**
|
6
|
+
* 针对后台的 JsonVo 和 DataJsonVo 对 ScxFetch 进行一次包装
|
7
|
+
* 1, 将 JsonVo.ok() 和 JsonVo.fail() 区分开
|
8
|
+
* 2, 解构 JsonVo 返回的 data 字段
|
9
|
+
*/
|
10
|
+
class ScxReq {
|
11
|
+
|
12
|
+
scxFetch;
|
13
|
+
|
14
|
+
baseURL;
|
15
|
+
|
16
|
+
constructor(scxFetch) {
|
17
|
+
if (scxFetch instanceof ScxFetch) {
|
18
|
+
this.scxFetch = scxFetch;
|
19
|
+
} else {
|
20
|
+
this.scxFetch = new ScxFetch(scxFetch);
|
21
|
+
}
|
22
|
+
this.baseURL = this.scxFetch.baseURL;
|
23
|
+
}
|
24
|
+
|
25
|
+
/**
|
26
|
+
* GET 方法
|
27
|
+
* @param url {URL | string}
|
28
|
+
* @param body {Object}
|
29
|
+
* @param options {ScxFetchOptions}
|
30
|
+
* @returns {ScxFetchPromise<unknown>}
|
31
|
+
*/
|
32
|
+
get(url, body = null, options = {}) {
|
33
|
+
return jsonVoProcessor(this.scxFetch.get(url, body, setResponseType(options)));
|
34
|
+
}
|
35
|
+
|
36
|
+
/**
|
37
|
+
* POST 方法
|
38
|
+
* @param url {URL | string}
|
39
|
+
* @param body {Object}
|
40
|
+
* @param options {ScxFetchOptions}
|
41
|
+
* @returns {ScxFetchPromise<unknown>}
|
42
|
+
*/
|
43
|
+
post(url, body = null, options = {}) {
|
44
|
+
return jsonVoProcessor(this.scxFetch.post(url, body, setResponseType(options)));
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
* PUT 方法
|
49
|
+
* @param url {URL | string}
|
50
|
+
* @param body {Object}
|
51
|
+
* @param options {ScxFetchOptions}
|
52
|
+
* @returns {ScxFetchPromise<unknown>}
|
53
|
+
*/
|
54
|
+
put(url, body = null, options = {}) {
|
55
|
+
return jsonVoProcessor(this.scxFetch.put(url, body, setResponseType(options)));
|
56
|
+
}
|
57
|
+
|
58
|
+
/**
|
59
|
+
* DELETE 方法
|
60
|
+
* @param url {URL | string}
|
61
|
+
* @param body {Object}
|
62
|
+
* @param options {ScxFetchOptions}
|
63
|
+
* @returns {ScxFetchPromise<unknown>}
|
64
|
+
*/
|
65
|
+
delete(url, body = null, options = {}) {
|
66
|
+
return jsonVoProcessor(this.scxFetch.delete(url, body, setResponseType(options)));
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
install(app) {
|
71
|
+
app.provide(scxReqKey, this);
|
72
|
+
}
|
73
|
+
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
/**
|
78
|
+
*
|
79
|
+
* @type {string}
|
80
|
+
*/
|
81
|
+
const scxReqKey = "scx-req";
|
82
|
+
|
83
|
+
/**
|
84
|
+
*
|
85
|
+
* @returns {ScxReq}
|
86
|
+
*/
|
87
|
+
function useScxReq() {
|
88
|
+
return inject(scxReqKey);
|
89
|
+
}
|
90
|
+
|
91
|
+
export {ScxReq, useScxReq};
|
package/ScxReqHelper.js
CHANGED
@@ -1,45 +1,45 @@
|
|
1
|
-
import {isNull} from "@scx-js/scx-common";
|
2
|
-
import {ScxFetchResponseType} from "./ScxFetchResponseType.js";
|
3
|
-
import {ScxFetchPromise} from "./ScxFetchPromise.js";
|
4
|
-
import {JsonVOError} from "./JsonVOError.js";
|
5
|
-
|
6
|
-
/**
|
7
|
-
* 通过 scx-req 发送的请求我们默认都是 json 格式的
|
8
|
-
* @param {ScxFetchOptions} options
|
9
|
-
* @returns {ScxFetchOptions}
|
10
|
-
*/
|
11
|
-
function setResponseType(options = {}) {
|
12
|
-
if (isNull(options.responseType)) {
|
13
|
-
options.responseType = ScxFetchResponseType.JSON;
|
14
|
-
}
|
15
|
-
return options;
|
16
|
-
}
|
17
|
-
|
18
|
-
|
19
|
-
function jsonVoProcessor(r) {
|
20
|
-
//这里是特殊处理
|
21
|
-
const result = new ScxFetchPromise((resolve, reject) => r.then(res => {
|
22
|
-
const {
|
23
|
-
responseType,
|
24
|
-
body,
|
25
|
-
} = res;
|
26
|
-
if (responseType === ScxFetchResponseType.JSON) {
|
27
|
-
if (body.message === "ok") {
|
28
|
-
resolve(body.data);
|
29
|
-
} else {
|
30
|
-
reject(new JsonVOError(body));
|
31
|
-
}
|
32
|
-
} else {
|
33
|
-
resolve(res);
|
34
|
-
}
|
35
|
-
}).catch(error => reject(error)));
|
36
|
-
|
37
|
-
result.abortController = r.abortController;
|
38
|
-
|
39
|
-
return result;
|
40
|
-
}
|
41
|
-
|
42
|
-
export {
|
43
|
-
setResponseType,
|
44
|
-
jsonVoProcessor,
|
45
|
-
};
|
1
|
+
import {isNull} from "@scx-js/scx-common";
|
2
|
+
import {ScxFetchResponseType} from "./ScxFetchResponseType.js";
|
3
|
+
import {ScxFetchPromise} from "./ScxFetchPromise.js";
|
4
|
+
import {JsonVOError} from "./JsonVOError.js";
|
5
|
+
|
6
|
+
/**
|
7
|
+
* 通过 scx-req 发送的请求我们默认都是 json 格式的
|
8
|
+
* @param {ScxFetchOptions} options
|
9
|
+
* @returns {ScxFetchOptions}
|
10
|
+
*/
|
11
|
+
function setResponseType(options = {}) {
|
12
|
+
if (isNull(options.responseType)) {
|
13
|
+
options.responseType = ScxFetchResponseType.JSON;
|
14
|
+
}
|
15
|
+
return options;
|
16
|
+
}
|
17
|
+
|
18
|
+
|
19
|
+
function jsonVoProcessor(r) {
|
20
|
+
//这里是特殊处理
|
21
|
+
const result = new ScxFetchPromise((resolve, reject) => r.then(res => {
|
22
|
+
const {
|
23
|
+
responseType,
|
24
|
+
body,
|
25
|
+
} = res;
|
26
|
+
if (responseType === ScxFetchResponseType.JSON) {
|
27
|
+
if (body.message === "ok") {
|
28
|
+
resolve(body.data);
|
29
|
+
} else {
|
30
|
+
reject(new JsonVOError(body));
|
31
|
+
}
|
32
|
+
} else {
|
33
|
+
resolve(res);
|
34
|
+
}
|
35
|
+
}).catch(error => reject(error)));
|
36
|
+
|
37
|
+
result.abortController = r.abortController;
|
38
|
+
|
39
|
+
return result;
|
40
|
+
}
|
41
|
+
|
42
|
+
export {
|
43
|
+
setResponseType,
|
44
|
+
jsonVoProcessor,
|
45
|
+
};
|