@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/FetchError.js
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
class FetchError {
|
2
|
-
|
3
|
-
cause;
|
4
|
-
|
5
|
-
constructor(error) {
|
6
|
-
this.cause = error;
|
7
|
-
}
|
8
|
-
|
9
|
-
}
|
10
|
-
|
11
|
-
export {
|
12
|
-
FetchError,
|
13
|
-
};
|
1
|
+
class FetchError {
|
2
|
+
|
3
|
+
cause;
|
4
|
+
|
5
|
+
constructor(error) {
|
6
|
+
this.cause = error;
|
7
|
+
}
|
8
|
+
|
9
|
+
}
|
10
|
+
|
11
|
+
export {
|
12
|
+
FetchError,
|
13
|
+
};
|
package/HttpFieldName.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
class HttpFieldName {
|
2
|
-
static CONTENT_TYPE = "Content-Type";
|
3
|
-
}
|
4
|
-
|
5
|
-
export {
|
6
|
-
HttpFieldName,
|
7
|
-
};
|
1
|
+
class HttpFieldName {
|
2
|
+
static CONTENT_TYPE = "Content-Type";
|
3
|
+
}
|
4
|
+
|
5
|
+
export {
|
6
|
+
HttpFieldName,
|
7
|
+
};
|
package/HttpMethod.js
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
class HttpMethod {
|
2
|
-
static GET = "GET";
|
3
|
-
static POST = "POST";
|
4
|
-
static PUT = "PUT";
|
5
|
-
static DELETE = "DELETE";
|
6
|
-
}
|
7
|
-
|
8
|
-
export {
|
9
|
-
HttpMethod,
|
10
|
-
};
|
1
|
+
class HttpMethod {
|
2
|
+
static GET = "GET";
|
3
|
+
static POST = "POST";
|
4
|
+
static PUT = "PUT";
|
5
|
+
static DELETE = "DELETE";
|
6
|
+
}
|
7
|
+
|
8
|
+
export {
|
9
|
+
HttpMethod,
|
10
|
+
};
|
package/JsonVOError.js
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
class JsonVOError {
|
2
|
-
|
3
|
-
constructor(error) {
|
4
|
-
Object.assign(this, error);
|
5
|
-
}
|
6
|
-
|
7
|
-
}
|
8
|
-
|
9
|
-
export {
|
10
|
-
JsonVOError,
|
11
|
-
};
|
1
|
+
class JsonVOError {
|
2
|
+
|
3
|
+
constructor(error) {
|
4
|
+
Object.assign(this, error);
|
5
|
+
}
|
6
|
+
|
7
|
+
}
|
8
|
+
|
9
|
+
export {
|
10
|
+
JsonVOError,
|
11
|
+
};
|
package/MediaType.js
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
class MediaType {
|
2
|
-
static APPLICATION_JSON = "application/json";
|
3
|
-
static APPLICATION_XML = "application/xml";
|
4
|
-
static MULTIPART_FORM_DATA = "multipart/form-data";
|
5
|
-
static TEXT_ANY = "text/";
|
6
|
-
}
|
7
|
-
|
8
|
-
export {
|
9
|
-
MediaType,
|
10
|
-
};
|
1
|
+
class MediaType {
|
2
|
+
static APPLICATION_JSON = "application/json";
|
3
|
+
static APPLICATION_XML = "application/xml";
|
4
|
+
static MULTIPART_FORM_DATA = "multipart/form-data";
|
5
|
+
static TEXT_ANY = "text/";
|
6
|
+
}
|
7
|
+
|
8
|
+
export {
|
9
|
+
MediaType,
|
10
|
+
};
|
package/ResponseNotOKError.js
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
class ResponseNotOKError {
|
2
|
-
|
3
|
-
cause;
|
4
|
-
|
5
|
-
constructor(error) {
|
6
|
-
this.cause = error;
|
7
|
-
}
|
8
|
-
|
9
|
-
}
|
10
|
-
|
11
|
-
export {
|
12
|
-
ResponseNotOKError,
|
13
|
-
};
|
1
|
+
class ResponseNotOKError {
|
2
|
+
|
3
|
+
cause;
|
4
|
+
|
5
|
+
constructor(error) {
|
6
|
+
this.cause = error;
|
7
|
+
}
|
8
|
+
|
9
|
+
}
|
10
|
+
|
11
|
+
export {
|
12
|
+
ResponseNotOKError,
|
13
|
+
};
|
package/ScxFetch.js
CHANGED
@@ -1,178 +1,178 @@
|
|
1
|
-
import {notNull} from "@scx-js/scx-common";
|
2
|
-
import {inject} from "vue";
|
3
|
-
import {
|
4
|
-
createRequestInit,
|
5
|
-
initDefaultOptions,
|
6
|
-
mixinOptions,
|
7
|
-
setMethod,
|
8
|
-
setRequestBody,
|
9
|
-
setRequestHeaders,
|
10
|
-
} from "./ScxFetchHelper.js";
|
11
|
-
import {ScxFetchPromise} from "./ScxFetchPromise.js";
|
12
|
-
import {ScxFetchResponse} from "./ScxFetchResponse.js";
|
13
|
-
import {ResponseNotOKError} from "./ResponseNotOKError.js";
|
14
|
-
import {FetchError} from "./FetchError.js";
|
15
|
-
import {HttpMethod} from "./HttpMethod.js";
|
16
|
-
|
17
|
-
/**
|
18
|
-
* ScxFetch : 针对 fetch 的简单封装
|
19
|
-
*/
|
20
|
-
class ScxFetch {
|
21
|
-
|
22
|
-
/**
|
23
|
-
* 默认配置
|
24
|
-
*
|
25
|
-
*/
|
26
|
-
defaultOptions = initDefaultOptions();
|
27
|
-
|
28
|
-
/**
|
29
|
-
* 基本路径
|
30
|
-
*/
|
31
|
-
baseURL;
|
32
|
-
|
33
|
-
/**
|
34
|
-
*
|
35
|
-
*
|
36
|
-
* @param {string|URL} baseURL
|
37
|
-
*/
|
38
|
-
constructor(baseURL = null) {
|
39
|
-
if (notNull(baseURL)) {
|
40
|
-
this.baseURL = new URL(baseURL);
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
/**
|
45
|
-
* 基本的 req
|
46
|
-
* @param url {URL | string}
|
47
|
-
* @param body {Object}
|
48
|
-
* @param options {ScxFetchOptions}
|
49
|
-
* @returns {ScxFetchPromise<ScxFetchResponse>}
|
50
|
-
*/
|
51
|
-
fetch(url, body = {}, options = {}) {
|
52
|
-
const {
|
53
|
-
method,
|
54
|
-
headers,
|
55
|
-
responseType,
|
56
|
-
usePreInterceptor,
|
57
|
-
usePostInterceptor,
|
58
|
-
charset,
|
59
|
-
useBodyPromise,
|
60
|
-
} = mixinOptions(this.defaultOptions, options);
|
61
|
-
|
62
|
-
const requestInit = createRequestInit(method);//初始化 fetch 参数 , 此处携带 cookie
|
63
|
-
|
64
|
-
const finalURL = notNull(this.baseURL) ? new URL(url, this.baseURL) : new URL(url);//创建 url
|
65
|
-
|
66
|
-
setRequestBody(requestInit, body, finalURL, charset);//设置 body 并根据 body 类型设置请求头
|
67
|
-
|
68
|
-
setRequestHeaders(requestInit, headers);//设置请求头 放在 setRequestBody 后以保证 options 中的 headers 可以覆盖 setRequestBody 中设置的值
|
69
|
-
|
70
|
-
const finalInit = usePreInterceptor ? this.preInterceptor(requestInit) : requestInit;
|
71
|
-
|
72
|
-
//用于取消
|
73
|
-
let abortController = new AbortController();
|
74
|
-
finalInit.signal = abortController.signal;
|
75
|
-
|
76
|
-
//此处进行特殊处理 1, 处理返回结果 2, 将非 2xx 的状态码表示为错误
|
77
|
-
const result = new ScxFetchPromise((resolve, reject) => fetch(finalURL, finalInit).then(res => {
|
78
|
-
if (res.ok) {
|
79
|
-
// resolve 的参数是 Promise 时会直接调用 参数的 resolve
|
80
|
-
resolve(ScxFetchResponse.create(res, responseType, useBodyPromise));
|
81
|
-
} else {
|
82
|
-
reject(new ResponseNotOKError(res));
|
83
|
-
}
|
84
|
-
}).catch(error => reject(new FetchError(error))));
|
85
|
-
|
86
|
-
result.abortController = abortController;
|
87
|
-
|
88
|
-
return usePostInterceptor ? this.postInterceptor(result) : result;
|
89
|
-
}
|
90
|
-
|
91
|
-
/**
|
92
|
-
* 前置处理器
|
93
|
-
* @param request {RequestInit}
|
94
|
-
* @returns {RequestInit}
|
95
|
-
*/
|
96
|
-
preInterceptor(request) {
|
97
|
-
return request;
|
98
|
-
}
|
99
|
-
|
100
|
-
/**
|
101
|
-
* 后置处理器
|
102
|
-
* @param response {ScxFetchPromise<ScxFetchResponse>}
|
103
|
-
* @returns {ScxFetchPromise<*>}
|
104
|
-
*/
|
105
|
-
postInterceptor(response) {
|
106
|
-
return response;
|
107
|
-
}
|
108
|
-
|
109
|
-
/**
|
110
|
-
* GET 方法
|
111
|
-
* @param url {URL | string}
|
112
|
-
* @param body {Object}
|
113
|
-
* @param options {ScxFetchOptions}
|
114
|
-
* @returns {ScxFetchPromise<unknown>}
|
115
|
-
*/
|
116
|
-
get(url, body = null, options = {}) {
|
117
|
-
return this.fetch(url, body, setMethod(HttpMethod.GET, options));
|
118
|
-
}
|
119
|
-
|
120
|
-
/**
|
121
|
-
* POST 方法
|
122
|
-
* @param url {URL | string}
|
123
|
-
* @param body {Object}
|
124
|
-
* @param options {ScxFetchOptions}
|
125
|
-
* @returns {ScxFetchPromise<unknown>}
|
126
|
-
*/
|
127
|
-
post(url, body = null, options = {}) {
|
128
|
-
return this.fetch(url, body, setMethod(HttpMethod.POST, options));
|
129
|
-
}
|
130
|
-
|
131
|
-
/**
|
132
|
-
* PUT 方法
|
133
|
-
* @param url {URL | string}
|
134
|
-
* @param body {Object}
|
135
|
-
* @param options {ScxFetchOptions}
|
136
|
-
* @returns {ScxFetchPromise<unknown>}
|
137
|
-
*/
|
138
|
-
put(url, body = null, options = {}) {
|
139
|
-
return this.fetch(url, body, setMethod(HttpMethod.PUT, options));
|
140
|
-
}
|
141
|
-
|
142
|
-
/**
|
143
|
-
* DELETE 方法
|
144
|
-
* @param {URL | string} url
|
145
|
-
* @param {Object} body
|
146
|
-
* @param options {ScxFetchOptions}
|
147
|
-
* @returns {ScxFetchPromise<unknown>}
|
148
|
-
*/
|
149
|
-
delete(url, body = null, options = {}) {
|
150
|
-
return this.fetch(url, body, setMethod(HttpMethod.DELETE, options));
|
151
|
-
}
|
152
|
-
|
153
|
-
/**
|
154
|
-
* For VUE
|
155
|
-
* @param app
|
156
|
-
*/
|
157
|
-
install(app) {
|
158
|
-
app.provide(scxFetchKey, this);
|
159
|
-
}
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
//************* 针对 vue ********************
|
164
|
-
|
165
|
-
const scxFetchKey = "scx-fetch";
|
166
|
-
|
167
|
-
/**
|
168
|
-
*
|
169
|
-
* @returns {ScxFetch}
|
170
|
-
*/
|
171
|
-
function useScxFetch() {
|
172
|
-
return inject(scxFetchKey);
|
173
|
-
}
|
174
|
-
|
175
|
-
export {
|
176
|
-
ScxFetch,
|
177
|
-
useScxFetch,
|
178
|
-
};
|
1
|
+
import {notNull} from "@scx-js/scx-common";
|
2
|
+
import {inject} from "vue";
|
3
|
+
import {
|
4
|
+
createRequestInit,
|
5
|
+
initDefaultOptions,
|
6
|
+
mixinOptions,
|
7
|
+
setMethod,
|
8
|
+
setRequestBody,
|
9
|
+
setRequestHeaders,
|
10
|
+
} from "./ScxFetchHelper.js";
|
11
|
+
import {ScxFetchPromise} from "./ScxFetchPromise.js";
|
12
|
+
import {ScxFetchResponse} from "./ScxFetchResponse.js";
|
13
|
+
import {ResponseNotOKError} from "./ResponseNotOKError.js";
|
14
|
+
import {FetchError} from "./FetchError.js";
|
15
|
+
import {HttpMethod} from "./HttpMethod.js";
|
16
|
+
|
17
|
+
/**
|
18
|
+
* ScxFetch : 针对 fetch 的简单封装
|
19
|
+
*/
|
20
|
+
class ScxFetch {
|
21
|
+
|
22
|
+
/**
|
23
|
+
* 默认配置
|
24
|
+
*
|
25
|
+
*/
|
26
|
+
defaultOptions = initDefaultOptions();
|
27
|
+
|
28
|
+
/**
|
29
|
+
* 基本路径
|
30
|
+
*/
|
31
|
+
baseURL;
|
32
|
+
|
33
|
+
/**
|
34
|
+
*
|
35
|
+
*
|
36
|
+
* @param {string|URL} baseURL
|
37
|
+
*/
|
38
|
+
constructor(baseURL = null) {
|
39
|
+
if (notNull(baseURL)) {
|
40
|
+
this.baseURL = new URL(baseURL);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* 基本的 req
|
46
|
+
* @param url {URL | string}
|
47
|
+
* @param body {Object}
|
48
|
+
* @param options {ScxFetchOptions}
|
49
|
+
* @returns {ScxFetchPromise<ScxFetchResponse>}
|
50
|
+
*/
|
51
|
+
fetch(url, body = {}, options = {}) {
|
52
|
+
const {
|
53
|
+
method,
|
54
|
+
headers,
|
55
|
+
responseType,
|
56
|
+
usePreInterceptor,
|
57
|
+
usePostInterceptor,
|
58
|
+
charset,
|
59
|
+
useBodyPromise,
|
60
|
+
} = mixinOptions(this.defaultOptions, options);
|
61
|
+
|
62
|
+
const requestInit = createRequestInit(method);//初始化 fetch 参数 , 此处携带 cookie
|
63
|
+
|
64
|
+
const finalURL = notNull(this.baseURL) ? new URL(url, this.baseURL) : new URL(url);//创建 url
|
65
|
+
|
66
|
+
setRequestBody(requestInit, body, finalURL, charset);//设置 body 并根据 body 类型设置请求头
|
67
|
+
|
68
|
+
setRequestHeaders(requestInit, headers);//设置请求头 放在 setRequestBody 后以保证 options 中的 headers 可以覆盖 setRequestBody 中设置的值
|
69
|
+
|
70
|
+
const finalInit = usePreInterceptor ? this.preInterceptor(requestInit) : requestInit;
|
71
|
+
|
72
|
+
//用于取消
|
73
|
+
let abortController = new AbortController();
|
74
|
+
finalInit.signal = abortController.signal;
|
75
|
+
|
76
|
+
//此处进行特殊处理 1, 处理返回结果 2, 将非 2xx 的状态码表示为错误
|
77
|
+
const result = new ScxFetchPromise((resolve, reject) => fetch(finalURL, finalInit).then(res => {
|
78
|
+
if (res.ok) {
|
79
|
+
// resolve 的参数是 Promise 时会直接调用 参数的 resolve
|
80
|
+
resolve(ScxFetchResponse.create(res, responseType, useBodyPromise));
|
81
|
+
} else {
|
82
|
+
reject(new ResponseNotOKError(res));
|
83
|
+
}
|
84
|
+
}).catch(error => reject(new FetchError(error))));
|
85
|
+
|
86
|
+
result.abortController = abortController;
|
87
|
+
|
88
|
+
return usePostInterceptor ? this.postInterceptor(result) : result;
|
89
|
+
}
|
90
|
+
|
91
|
+
/**
|
92
|
+
* 前置处理器
|
93
|
+
* @param request {RequestInit}
|
94
|
+
* @returns {RequestInit}
|
95
|
+
*/
|
96
|
+
preInterceptor(request) {
|
97
|
+
return request;
|
98
|
+
}
|
99
|
+
|
100
|
+
/**
|
101
|
+
* 后置处理器
|
102
|
+
* @param response {ScxFetchPromise<ScxFetchResponse>}
|
103
|
+
* @returns {ScxFetchPromise<*>}
|
104
|
+
*/
|
105
|
+
postInterceptor(response) {
|
106
|
+
return response;
|
107
|
+
}
|
108
|
+
|
109
|
+
/**
|
110
|
+
* GET 方法
|
111
|
+
* @param url {URL | string}
|
112
|
+
* @param body {Object}
|
113
|
+
* @param options {ScxFetchOptions}
|
114
|
+
* @returns {ScxFetchPromise<unknown>}
|
115
|
+
*/
|
116
|
+
get(url, body = null, options = {}) {
|
117
|
+
return this.fetch(url, body, setMethod(HttpMethod.GET, options));
|
118
|
+
}
|
119
|
+
|
120
|
+
/**
|
121
|
+
* POST 方法
|
122
|
+
* @param url {URL | string}
|
123
|
+
* @param body {Object}
|
124
|
+
* @param options {ScxFetchOptions}
|
125
|
+
* @returns {ScxFetchPromise<unknown>}
|
126
|
+
*/
|
127
|
+
post(url, body = null, options = {}) {
|
128
|
+
return this.fetch(url, body, setMethod(HttpMethod.POST, options));
|
129
|
+
}
|
130
|
+
|
131
|
+
/**
|
132
|
+
* PUT 方法
|
133
|
+
* @param url {URL | string}
|
134
|
+
* @param body {Object}
|
135
|
+
* @param options {ScxFetchOptions}
|
136
|
+
* @returns {ScxFetchPromise<unknown>}
|
137
|
+
*/
|
138
|
+
put(url, body = null, options = {}) {
|
139
|
+
return this.fetch(url, body, setMethod(HttpMethod.PUT, options));
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
* DELETE 方法
|
144
|
+
* @param {URL | string} url
|
145
|
+
* @param {Object} body
|
146
|
+
* @param options {ScxFetchOptions}
|
147
|
+
* @returns {ScxFetchPromise<unknown>}
|
148
|
+
*/
|
149
|
+
delete(url, body = null, options = {}) {
|
150
|
+
return this.fetch(url, body, setMethod(HttpMethod.DELETE, options));
|
151
|
+
}
|
152
|
+
|
153
|
+
/**
|
154
|
+
* For VUE
|
155
|
+
* @param app
|
156
|
+
*/
|
157
|
+
install(app) {
|
158
|
+
app.provide(scxFetchKey, this);
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
//************* 针对 vue ********************
|
164
|
+
|
165
|
+
const scxFetchKey = "scx-fetch";
|
166
|
+
|
167
|
+
/**
|
168
|
+
*
|
169
|
+
* @returns {ScxFetch}
|
170
|
+
*/
|
171
|
+
function useScxFetch() {
|
172
|
+
return inject(scxFetchKey);
|
173
|
+
}
|
174
|
+
|
175
|
+
export {
|
176
|
+
ScxFetch,
|
177
|
+
useScxFetch,
|
178
|
+
};
|
package/ScxFetchHelper.js
CHANGED
@@ -1,73 +1,73 @@
|
|
1
|
-
import {notNull} from "@scx-js/scx-common";
|
2
|
-
import {ScxFetchOptions} from "./ScxFetchOptions.js";
|
3
|
-
import {HttpMethod} from "./HttpMethod.js";
|
4
|
-
import {ScxFetchResponseType} from "./ScxFetchResponseType.js";
|
5
|
-
import {HttpFieldName} from "./HttpFieldName.js";
|
6
|
-
import {MediaType} from "./MediaType.js";
|
7
|
-
|
8
|
-
function createRequestInit(method) {
|
9
|
-
return {
|
10
|
-
method,
|
11
|
-
headers: new Headers(),
|
12
|
-
credentials: "include",
|
13
|
-
body: null,
|
14
|
-
};
|
15
|
-
}
|
16
|
-
|
17
|
-
function setMethod(method, options = {}) {
|
18
|
-
options.method = method;
|
19
|
-
return options;
|
20
|
-
}
|
21
|
-
|
22
|
-
function setRequestHeaders(requestInit, headers) {
|
23
|
-
//循环设置 headers
|
24
|
-
if (notNull(headers)) {
|
25
|
-
if (headers instanceof Headers) {
|
26
|
-
headers.forEach((k, v) => requestInit.headers.set(k, v));
|
27
|
-
} else {
|
28
|
-
for (const [key, value] of Object.entries(headers)) {
|
29
|
-
requestInit.headers.set(key, String(value));
|
30
|
-
}
|
31
|
-
}
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
function setRequestBody(requestInit, body, url, charset) {
|
36
|
-
if (notNull(body)) {
|
37
|
-
if (body instanceof FormData) {
|
38
|
-
requestInit.body = body;
|
39
|
-
} else if (requestInit.method === HttpMethod.GET) {
|
40
|
-
for (const [key, value] of Object.entries(body)) {
|
41
|
-
url.searchParams.set(key, String(value));
|
42
|
-
}
|
43
|
-
} else {
|
44
|
-
requestInit.headers.set(HttpFieldName.CONTENT_TYPE, `${MediaType.APPLICATION_JSON};charset=${charset}`);
|
45
|
-
requestInit.body = JSON.stringify(body);
|
46
|
-
}
|
47
|
-
}
|
48
|
-
}
|
49
|
-
|
50
|
-
function mixinOptions(defaultOptions, options) {
|
51
|
-
return {...defaultOptions, ...options};
|
52
|
-
}
|
53
|
-
|
54
|
-
function initDefaultOptions() {
|
55
|
-
const temp = new ScxFetchOptions();
|
56
|
-
temp.method = HttpMethod.GET;
|
57
|
-
temp.headers = null;
|
58
|
-
temp.responseType = ScxFetchResponseType.AUTO;
|
59
|
-
temp.useBodyPromise = false;
|
60
|
-
temp.usePreInterceptor = false;
|
61
|
-
temp.usePostInterceptor = false;
|
62
|
-
temp.charset = "utf-8";
|
63
|
-
return temp;
|
64
|
-
}
|
65
|
-
|
66
|
-
export {
|
67
|
-
createRequestInit,
|
68
|
-
setMethod,
|
69
|
-
setRequestHeaders,
|
70
|
-
setRequestBody,
|
71
|
-
mixinOptions,
|
72
|
-
initDefaultOptions,
|
73
|
-
};
|
1
|
+
import {notNull} from "@scx-js/scx-common";
|
2
|
+
import {ScxFetchOptions} from "./ScxFetchOptions.js";
|
3
|
+
import {HttpMethod} from "./HttpMethod.js";
|
4
|
+
import {ScxFetchResponseType} from "./ScxFetchResponseType.js";
|
5
|
+
import {HttpFieldName} from "./HttpFieldName.js";
|
6
|
+
import {MediaType} from "./MediaType.js";
|
7
|
+
|
8
|
+
function createRequestInit(method) {
|
9
|
+
return {
|
10
|
+
method,
|
11
|
+
headers: new Headers(),
|
12
|
+
credentials: "include",
|
13
|
+
body: null,
|
14
|
+
};
|
15
|
+
}
|
16
|
+
|
17
|
+
function setMethod(method, options = {}) {
|
18
|
+
options.method = method;
|
19
|
+
return options;
|
20
|
+
}
|
21
|
+
|
22
|
+
function setRequestHeaders(requestInit, headers) {
|
23
|
+
//循环设置 headers
|
24
|
+
if (notNull(headers)) {
|
25
|
+
if (headers instanceof Headers) {
|
26
|
+
headers.forEach((k, v) => requestInit.headers.set(k, v));
|
27
|
+
} else {
|
28
|
+
for (const [key, value] of Object.entries(headers)) {
|
29
|
+
requestInit.headers.set(key, String(value));
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
function setRequestBody(requestInit, body, url, charset) {
|
36
|
+
if (notNull(body)) {
|
37
|
+
if (body instanceof FormData) {
|
38
|
+
requestInit.body = body;
|
39
|
+
} else if (requestInit.method === HttpMethod.GET) {
|
40
|
+
for (const [key, value] of Object.entries(body)) {
|
41
|
+
url.searchParams.set(key, String(value));
|
42
|
+
}
|
43
|
+
} else {
|
44
|
+
requestInit.headers.set(HttpFieldName.CONTENT_TYPE, `${MediaType.APPLICATION_JSON};charset=${charset}`);
|
45
|
+
requestInit.body = JSON.stringify(body);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
function mixinOptions(defaultOptions, options) {
|
51
|
+
return {...defaultOptions, ...options};
|
52
|
+
}
|
53
|
+
|
54
|
+
function initDefaultOptions() {
|
55
|
+
const temp = new ScxFetchOptions();
|
56
|
+
temp.method = HttpMethod.GET;
|
57
|
+
temp.headers = null;
|
58
|
+
temp.responseType = ScxFetchResponseType.AUTO;
|
59
|
+
temp.useBodyPromise = false;
|
60
|
+
temp.usePreInterceptor = false;
|
61
|
+
temp.usePostInterceptor = false;
|
62
|
+
temp.charset = "utf-8";
|
63
|
+
return temp;
|
64
|
+
}
|
65
|
+
|
66
|
+
export {
|
67
|
+
createRequestInit,
|
68
|
+
setMethod,
|
69
|
+
setRequestHeaders,
|
70
|
+
setRequestBody,
|
71
|
+
mixinOptions,
|
72
|
+
initDefaultOptions,
|
73
|
+
};
|