@modern-js/create-request 2.69.4 → 3.0.0-alpha.0
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/cjs/browser.js +128 -135
- package/dist/cjs/handleRes.js +44 -46
- package/dist/cjs/node.js +139 -153
- package/dist/cjs/qs.js +36 -27
- package/dist/cjs/types.js +17 -15
- package/dist/cjs/utiles.js +45 -46
- package/dist/esm/browser.mjs +99 -0
- package/dist/esm/handleRes.mjs +15 -0
- package/dist/esm/node.mjs +99 -0
- package/dist/esm/qs.mjs +2 -0
- package/dist/esm/utiles.mjs +15 -0
- package/dist/esm-node/browser.mjs +99 -0
- package/dist/esm-node/handleRes.mjs +15 -0
- package/dist/esm-node/node.mjs +99 -0
- package/dist/esm-node/qs.mjs +2 -0
- package/dist/esm-node/utiles.mjs +15 -0
- package/package.json +37 -21
- package/dist/esm/browser.js +0 -145
- package/dist/esm/handleRes.js +0 -68
- package/dist/esm/node.js +0 -158
- package/dist/esm/qs.js +0 -5
- package/dist/esm/utiles.js +0 -46
- package/dist/esm-node/browser.js +0 -120
- package/dist/esm-node/handleRes.js +0 -27
- package/dist/esm-node/node.js +0 -127
- package/dist/esm-node/qs.js +0 -5
- package/dist/esm-node/utiles.js +0 -26
- /package/dist/esm/{types.js → types.mjs} +0 -0
- /package/dist/esm-node/{types.js → types.mjs} +0 -0
package/dist/cjs/node.js
CHANGED
|
@@ -1,163 +1,149 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.n = (module)=>{
|
|
5
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
6
|
+
__webpack_require__.d(getter, {
|
|
7
|
+
a: getter
|
|
8
|
+
});
|
|
9
|
+
return getter;
|
|
10
|
+
};
|
|
11
|
+
})();
|
|
12
|
+
(()=>{
|
|
13
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
14
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: definition[key]
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
})();
|
|
20
|
+
(()=>{
|
|
21
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
22
|
+
})();
|
|
23
|
+
(()=>{
|
|
24
|
+
__webpack_require__.r = (exports1)=>{
|
|
25
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
26
|
+
value: 'Module'
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
29
|
+
value: true
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
})();
|
|
33
|
+
var __webpack_exports__ = {};
|
|
34
|
+
__webpack_require__.r(__webpack_exports__);
|
|
35
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
36
|
+
createUploader: ()=>createUploader,
|
|
37
|
+
configure: ()=>configure,
|
|
38
|
+
createRequest: ()=>createRequest
|
|
34
39
|
});
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const realRequest =
|
|
43
|
-
const realAllowedHeaders =
|
|
44
|
-
const domainMap =
|
|
45
|
-
const originFetch = (...params)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
init.body = void 0;
|
|
50
|
-
}
|
|
51
|
-
return (0, import_node_fetch.default)(...params).then(import_handleRes.handleRes);
|
|
40
|
+
const node_namespaceObject = require("@modern-js/runtime-utils/node");
|
|
41
|
+
const external_node_fetch_namespaceObject = require("node-fetch");
|
|
42
|
+
var external_node_fetch_default = /*#__PURE__*/ __webpack_require__.n(external_node_fetch_namespaceObject);
|
|
43
|
+
const external_path_to_regexp_namespaceObject = require("path-to-regexp");
|
|
44
|
+
const external_qs_namespaceObject = require("qs");
|
|
45
|
+
const external_handleRes_js_namespaceObject = require("./handleRes.js");
|
|
46
|
+
const external_utiles_js_namespaceObject = require("./utiles.js");
|
|
47
|
+
const realRequest = new Map();
|
|
48
|
+
const realAllowedHeaders = new Map();
|
|
49
|
+
const domainMap = new Map();
|
|
50
|
+
const originFetch = (...params)=>{
|
|
51
|
+
const [, init] = params;
|
|
52
|
+
if (init?.method?.toLowerCase() === 'get') init.body = void 0;
|
|
53
|
+
return external_node_fetch_default()(...params).then(external_handleRes_js_namespaceObject.handleRes);
|
|
52
54
|
};
|
|
53
|
-
const configure = (options)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
realAllowedHeaders.set(requestId, allowedHeaders);
|
|
61
|
-
}
|
|
62
|
-
if (setDomain) {
|
|
63
|
-
domainMap.set(requestId, setDomain({
|
|
64
|
-
target: "server"
|
|
55
|
+
const configure = (options)=>{
|
|
56
|
+
const { request, interceptor, allowedHeaders, setDomain, requestId = 'default' } = options;
|
|
57
|
+
let configuredRequest = request || originFetch;
|
|
58
|
+
if (interceptor && !request) configuredRequest = interceptor(external_node_fetch_default());
|
|
59
|
+
if (Array.isArray(allowedHeaders)) realAllowedHeaders.set(requestId, allowedHeaders);
|
|
60
|
+
if (setDomain) domainMap.set(requestId, setDomain({
|
|
61
|
+
target: 'server'
|
|
65
62
|
}));
|
|
66
|
-
|
|
67
|
-
realRequest.set(requestId, configuredRequest);
|
|
63
|
+
realRequest.set(requestId, configuredRequest);
|
|
68
64
|
};
|
|
69
|
-
const createRequest = ({ path, method, port, httpMethodDecider =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
"Content-Type": "application/json"
|
|
88
|
-
};
|
|
89
|
-
} else {
|
|
90
|
-
const payload = typeof args[args.length - 1] === "object" ? args[args.length - 1] : {};
|
|
91
|
-
payload.params = payload.params || {};
|
|
92
|
-
const requestParams = args[0];
|
|
93
|
-
if (typeof requestParams === "object" && requestParams.params) {
|
|
94
|
-
const { params } = requestParams;
|
|
95
|
-
keys.forEach((key) => {
|
|
96
|
-
payload.params[key.name] = params[key.name];
|
|
97
|
-
});
|
|
98
|
-
} else {
|
|
99
|
-
keys.forEach((key, index) => {
|
|
100
|
-
payload.params[key.name] = args[index];
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
const plainPath = getFinalPath(payload.params);
|
|
104
|
-
const finalPath = payload.query ? `${plainPath}?${(0, import_qs.stringify)(payload.query)}` : plainPath;
|
|
105
|
-
headers = payload.headers || {};
|
|
106
|
-
const targetAllowedHeaders = realAllowedHeaders.get(requestId) || [];
|
|
107
|
-
for (const key of targetAllowedHeaders) {
|
|
108
|
-
if (typeof webRequestHeaders[key] !== "undefined") {
|
|
109
|
-
headers[key] = webRequestHeaders[key];
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (payload.data) {
|
|
113
|
-
headers["Content-Type"] = "application/json";
|
|
114
|
-
body = typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.body;
|
|
115
|
-
} else if (payload.body) {
|
|
116
|
-
headers["Content-Type"] = "text/plain";
|
|
117
|
-
body = payload.body;
|
|
118
|
-
} else if (payload.formData) {
|
|
119
|
-
body = payload.formData;
|
|
120
|
-
} else if (payload.formUrlencoded) {
|
|
121
|
-
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
122
|
-
if (typeof payload.formUrlencoded === "object") {
|
|
123
|
-
body = (0, import_qs.stringify)(payload.formUrlencoded);
|
|
65
|
+
const createRequest = ({ path, method, port, httpMethodDecider = 'functionName', fetch = originFetch, requestId = 'default' })=>{
|
|
66
|
+
const getFinalPath = (0, external_path_to_regexp_namespaceObject.compile)(path, {
|
|
67
|
+
encode: encodeURIComponent
|
|
68
|
+
});
|
|
69
|
+
const keys = [];
|
|
70
|
+
(0, external_path_to_regexp_namespaceObject.pathToRegexp)(path, keys);
|
|
71
|
+
const sender = (...args)=>{
|
|
72
|
+
let webRequestHeaders = [];
|
|
73
|
+
if ('default' === requestId) webRequestHeaders = node_namespaceObject.storage.useContext().headers;
|
|
74
|
+
let body;
|
|
75
|
+
let headers;
|
|
76
|
+
let url;
|
|
77
|
+
if ('inputParams' === httpMethodDecider) {
|
|
78
|
+
url = path;
|
|
79
|
+
body = args;
|
|
80
|
+
headers = {
|
|
81
|
+
'Content-Type': 'application/json'
|
|
82
|
+
};
|
|
124
83
|
} else {
|
|
125
|
-
|
|
84
|
+
const payload = 'object' == typeof args[args.length - 1] ? args[args.length - 1] : {};
|
|
85
|
+
payload.params = payload.params || {};
|
|
86
|
+
const requestParams = args[0];
|
|
87
|
+
if ('object' == typeof requestParams && requestParams.params) {
|
|
88
|
+
const { params } = requestParams;
|
|
89
|
+
keys.forEach((key)=>{
|
|
90
|
+
payload.params[key.name] = params[key.name];
|
|
91
|
+
});
|
|
92
|
+
} else keys.forEach((key, index)=>{
|
|
93
|
+
payload.params[key.name] = args[index];
|
|
94
|
+
});
|
|
95
|
+
const plainPath = getFinalPath(payload.params);
|
|
96
|
+
const finalPath = payload.query ? `${plainPath}?${(0, external_qs_namespaceObject.stringify)(payload.query)}` : plainPath;
|
|
97
|
+
headers = payload.headers || {};
|
|
98
|
+
const targetAllowedHeaders = realAllowedHeaders.get(requestId) || [];
|
|
99
|
+
for (const key of targetAllowedHeaders)if (void 0 !== webRequestHeaders[key]) headers[key] = webRequestHeaders[key];
|
|
100
|
+
if (payload.data) {
|
|
101
|
+
headers['Content-Type'] = 'application/json';
|
|
102
|
+
body = 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
|
|
103
|
+
} else if (payload.body) {
|
|
104
|
+
headers['Content-Type'] = 'text/plain';
|
|
105
|
+
body = payload.body;
|
|
106
|
+
} else if (payload.formData) body = payload.formData;
|
|
107
|
+
else if (payload.formUrlencoded) {
|
|
108
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
109
|
+
body = 'object' == typeof payload.formUrlencoded ? (0, external_qs_namespaceObject.stringify)(payload.formUrlencoded) : payload.formUrlencoded;
|
|
110
|
+
}
|
|
111
|
+
const configDomain = domainMap.get(requestId);
|
|
112
|
+
url = `${configDomain || `http://127.0.0.1:${port}`}${finalPath}`;
|
|
126
113
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return
|
|
137
|
-
method,
|
|
138
|
-
body,
|
|
139
|
-
headers
|
|
140
|
-
});
|
|
141
|
-
};
|
|
142
|
-
return sender;
|
|
114
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
115
|
+
if ('get' === method.toLowerCase()) body = void 0;
|
|
116
|
+
headers.accept = "application/json,*/*;q=0.8";
|
|
117
|
+
return fetcher(url, {
|
|
118
|
+
method,
|
|
119
|
+
body,
|
|
120
|
+
headers
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
return sender;
|
|
143
124
|
};
|
|
144
|
-
const createUploader = ({ path, requestId =
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
125
|
+
const createUploader = ({ path, requestId = 'default' })=>{
|
|
126
|
+
const sender = (...args)=>{
|
|
127
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
128
|
+
const { body, headers } = (0, external_utiles_js_namespaceObject.getUploadPayload)(args);
|
|
129
|
+
const configDomain = domainMap.get(requestId);
|
|
130
|
+
const finalURL = `${configDomain || ''}${path}`;
|
|
131
|
+
return fetcher(finalURL, {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
body,
|
|
134
|
+
headers
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
return sender;
|
|
157
138
|
};
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
139
|
+
exports.configure = __webpack_exports__.configure;
|
|
140
|
+
exports.createRequest = __webpack_exports__.createRequest;
|
|
141
|
+
exports.createUploader = __webpack_exports__.createUploader;
|
|
142
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
143
|
+
"configure",
|
|
144
|
+
"createRequest",
|
|
145
|
+
"createUploader"
|
|
146
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
147
|
+
Object.defineProperty(exports, '__esModule', {
|
|
148
|
+
value: true
|
|
163
149
|
});
|
package/dist/cjs/qs.js
CHANGED
|
@@ -1,30 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
stringify: ()=>external_qs_namespaceObject.stringify,
|
|
28
|
+
parse: ()=>external_qs_namespaceObject.parse
|
|
23
29
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
const external_qs_namespaceObject = require("qs");
|
|
31
|
+
exports.parse = __webpack_exports__.parse;
|
|
32
|
+
exports.stringify = __webpack_exports__.stringify;
|
|
33
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
34
|
+
"parse",
|
|
35
|
+
"stringify"
|
|
36
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
37
|
+
Object.defineProperty(exports, '__esModule', {
|
|
38
|
+
value: true
|
|
30
39
|
});
|
package/dist/cjs/types.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
var
|
|
16
|
-
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.r = (exports1)=>{
|
|
5
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
+
value: 'Module'
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
var __webpack_exports__ = {};
|
|
14
|
+
__webpack_require__.r(__webpack_exports__);
|
|
15
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
+
Object.defineProperty(exports, '__esModule', {
|
|
17
|
+
value: true
|
|
18
|
+
});
|
package/dist/cjs/utiles.js
CHANGED
|
@@ -1,50 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
getUploadPayload: ()=>getUploadPayload
|
|
22
28
|
});
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
} else {
|
|
37
|
-
formdata.append(key, value);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const body = formdata;
|
|
41
|
-
return {
|
|
42
|
-
body,
|
|
43
|
-
headers: payload.headers,
|
|
44
|
-
params: payload.params
|
|
45
|
-
};
|
|
29
|
+
const getUploadPayload = (args)=>{
|
|
30
|
+
const payload = 'object' == typeof args[args.length - 1] ? args[args.length - 1] : {};
|
|
31
|
+
const files = payload.files;
|
|
32
|
+
if (!files) throw new Error('no files');
|
|
33
|
+
const formdata = new FormData();
|
|
34
|
+
for (const [key, value] of Object.entries(files))if (value instanceof FileList) for(let i = 0; i < value.length; i++)formdata.append(key, value[i]);
|
|
35
|
+
else formdata.append(key, value);
|
|
36
|
+
const body = formdata;
|
|
37
|
+
return {
|
|
38
|
+
body,
|
|
39
|
+
headers: payload.headers,
|
|
40
|
+
params: payload.params
|
|
41
|
+
};
|
|
46
42
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
exports.getUploadPayload = __webpack_exports__.getUploadPayload;
|
|
44
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
45
|
+
"getUploadPayload"
|
|
46
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
47
|
+
Object.defineProperty(exports, '__esModule', {
|
|
48
|
+
value: true
|
|
50
49
|
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { compile, pathToRegexp } from "path-to-regexp";
|
|
2
|
+
import { stringify } from "qs";
|
|
3
|
+
import { handleRes } from "./handleRes";
|
|
4
|
+
import { getUploadPayload } from "./utiles";
|
|
5
|
+
const realRequest = new Map();
|
|
6
|
+
const realAllowedHeaders = new Map();
|
|
7
|
+
const domainMap = new Map();
|
|
8
|
+
const originFetch = (...params)=>{
|
|
9
|
+
const [url, init] = params;
|
|
10
|
+
if (init?.method?.toLowerCase() === 'get') init.body = void 0;
|
|
11
|
+
return fetch(url, init).then(handleRes);
|
|
12
|
+
};
|
|
13
|
+
const configure = (options)=>{
|
|
14
|
+
const { request, interceptor, allowedHeaders, setDomain, requestId = 'default' } = options;
|
|
15
|
+
let configuredRequest = request || originFetch;
|
|
16
|
+
if (interceptor && !request) configuredRequest = interceptor(fetch);
|
|
17
|
+
if (Array.isArray(allowedHeaders)) realAllowedHeaders.set(requestId, allowedHeaders);
|
|
18
|
+
if (setDomain) domainMap.set(requestId, setDomain({
|
|
19
|
+
target: 'browser'
|
|
20
|
+
}));
|
|
21
|
+
realRequest.set(requestId, configuredRequest);
|
|
22
|
+
};
|
|
23
|
+
const createRequest = ({ path, method, port, httpMethodDecider = 'functionName', fetch: fetch1 = originFetch, domain, requestId = 'default' })=>{
|
|
24
|
+
const getFinalPath = compile(path, {
|
|
25
|
+
encode: encodeURIComponent
|
|
26
|
+
});
|
|
27
|
+
const keys = [];
|
|
28
|
+
pathToRegexp(path, keys);
|
|
29
|
+
const sender = async (...args)=>{
|
|
30
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
31
|
+
let body;
|
|
32
|
+
let finalURL;
|
|
33
|
+
let headers;
|
|
34
|
+
if ('inputParams' === httpMethodDecider) {
|
|
35
|
+
finalURL = path;
|
|
36
|
+
body = JSON.stringify({
|
|
37
|
+
args
|
|
38
|
+
});
|
|
39
|
+
headers = {
|
|
40
|
+
'Content-Type': 'application/json'
|
|
41
|
+
};
|
|
42
|
+
} else {
|
|
43
|
+
const payload = 'object' == typeof args[args.length - 1] ? args[args.length - 1] : {};
|
|
44
|
+
payload.params = payload.params || {};
|
|
45
|
+
const requestParams = args[0];
|
|
46
|
+
if ('object' == typeof requestParams && requestParams.params) {
|
|
47
|
+
const { params } = requestParams;
|
|
48
|
+
keys.forEach((key)=>{
|
|
49
|
+
payload.params[key.name] = params[key.name];
|
|
50
|
+
});
|
|
51
|
+
} else keys.forEach((key, index)=>{
|
|
52
|
+
payload.params[key.name] = args[index];
|
|
53
|
+
});
|
|
54
|
+
const finalPath = getFinalPath(payload.params);
|
|
55
|
+
finalURL = payload.query ? `${finalPath}?${stringify(payload.query)}` : finalPath;
|
|
56
|
+
headers = payload.headers || {};
|
|
57
|
+
body = payload.data && 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
|
|
58
|
+
if (payload.data) {
|
|
59
|
+
headers['Content-Type'] = 'application/json';
|
|
60
|
+
body = 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
|
|
61
|
+
} else if (payload.body) {
|
|
62
|
+
headers['Content-Type'] = 'text/plain';
|
|
63
|
+
body = payload.body;
|
|
64
|
+
} else if (payload.formData) body = payload.formData;
|
|
65
|
+
else if (payload.formUrlencoded) {
|
|
66
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
67
|
+
body = 'object' != typeof payload.formUrlencoded || payload.formUrlencoded instanceof URLSearchParams ? payload.formUrlencoded : stringify(payload.formUrlencoded);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
headers.accept = "application/json,*/*;q=0.8";
|
|
71
|
+
const configDomain = domainMap.get(requestId);
|
|
72
|
+
finalURL = `${configDomain || domain || ''}${finalURL}`;
|
|
73
|
+
return fetcher(finalURL, {
|
|
74
|
+
method,
|
|
75
|
+
body,
|
|
76
|
+
headers
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
return sender;
|
|
80
|
+
};
|
|
81
|
+
const createUploader = ({ path, domain, requestId = 'default' })=>{
|
|
82
|
+
const getFinalPath = compile(path, {
|
|
83
|
+
encode: encodeURIComponent
|
|
84
|
+
});
|
|
85
|
+
const sender = (...args)=>{
|
|
86
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
87
|
+
const { body, headers, params } = getUploadPayload(args);
|
|
88
|
+
const finalPath = getFinalPath(params);
|
|
89
|
+
const configDomain = domainMap.get(requestId);
|
|
90
|
+
const finalURL = `${configDomain || domain || ''}${finalPath}`;
|
|
91
|
+
return fetcher(finalURL, {
|
|
92
|
+
method: 'POST',
|
|
93
|
+
body,
|
|
94
|
+
headers
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
return sender;
|
|
98
|
+
};
|
|
99
|
+
export { configure, createRequest, createUploader };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const handleRes = async (res)=>{
|
|
2
|
+
const contentType = res.headers.get('content-type');
|
|
3
|
+
if (!res.ok) {
|
|
4
|
+
const data = await res.json();
|
|
5
|
+
res.data = data;
|
|
6
|
+
throw res;
|
|
7
|
+
}
|
|
8
|
+
if (contentType?.includes('application/json') || contentType?.includes('text/json')) return res.json();
|
|
9
|
+
if (contentType?.includes('text/html') || contentType?.includes('text/plain')) return res.text();
|
|
10
|
+
if ((contentType?.includes('application/x-www-form-urlencoded') || contentType?.includes('multipart/form-data')) && res instanceof Response) return res.formData();
|
|
11
|
+
if (contentType?.includes('application/octet-stream')) return res.arrayBuffer();
|
|
12
|
+
if (contentType?.includes('image/png')) return res;
|
|
13
|
+
return res.text();
|
|
14
|
+
};
|
|
15
|
+
export { handleRes };
|