@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/browser.js
CHANGED
|
@@ -1,146 +1,139 @@
|
|
|
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
|
-
|
|
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
|
+
createUploader: ()=>createUploader,
|
|
28
|
+
configure: ()=>configure,
|
|
29
|
+
createRequest: ()=>createRequest
|
|
24
30
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if ((init === null || init === void 0 ? void 0 : (_init_method = init.method) === null || _init_method === void 0 ? void 0 : _init_method.toLowerCase()) === "get") {
|
|
37
|
-
init.body = void 0;
|
|
38
|
-
}
|
|
39
|
-
return fetch(url, init).then(import_handleRes.handleRes);
|
|
31
|
+
const external_path_to_regexp_namespaceObject = require("path-to-regexp");
|
|
32
|
+
const external_qs_namespaceObject = require("qs");
|
|
33
|
+
const external_handleRes_js_namespaceObject = require("./handleRes.js");
|
|
34
|
+
const external_utiles_js_namespaceObject = require("./utiles.js");
|
|
35
|
+
const realRequest = new Map();
|
|
36
|
+
const realAllowedHeaders = new Map();
|
|
37
|
+
const domainMap = new Map();
|
|
38
|
+
const originFetch = (...params)=>{
|
|
39
|
+
const [url, init] = params;
|
|
40
|
+
if (init?.method?.toLowerCase() === 'get') init.body = void 0;
|
|
41
|
+
return fetch(url, init).then(external_handleRes_js_namespaceObject.handleRes);
|
|
40
42
|
};
|
|
41
|
-
const configure = (options)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
realAllowedHeaders.set(requestId, allowedHeaders);
|
|
49
|
-
}
|
|
50
|
-
if (setDomain) {
|
|
51
|
-
domainMap.set(requestId, setDomain({
|
|
52
|
-
target: "browser"
|
|
43
|
+
const configure = (options)=>{
|
|
44
|
+
const { request, interceptor, allowedHeaders, setDomain, requestId = 'default' } = options;
|
|
45
|
+
let configuredRequest = request || originFetch;
|
|
46
|
+
if (interceptor && !request) configuredRequest = interceptor(fetch);
|
|
47
|
+
if (Array.isArray(allowedHeaders)) realAllowedHeaders.set(requestId, allowedHeaders);
|
|
48
|
+
if (setDomain) domainMap.set(requestId, setDomain({
|
|
49
|
+
target: 'browser'
|
|
53
50
|
}));
|
|
54
|
-
|
|
55
|
-
realRequest.set(requestId, configuredRequest);
|
|
51
|
+
realRequest.set(requestId, configuredRequest);
|
|
56
52
|
};
|
|
57
|
-
const createRequest = ({ path, method, port, httpMethodDecider =
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
} else {
|
|
77
|
-
const payload = typeof args[args.length - 1] === "object" ? args[args.length - 1] : {};
|
|
78
|
-
payload.params = payload.params || {};
|
|
79
|
-
const requestParams = args[0];
|
|
80
|
-
if (typeof requestParams === "object" && requestParams.params) {
|
|
81
|
-
const { params } = requestParams;
|
|
82
|
-
keys.forEach((key) => {
|
|
83
|
-
payload.params[key.name] = params[key.name];
|
|
84
|
-
});
|
|
85
|
-
} else {
|
|
86
|
-
keys.forEach((key, index) => {
|
|
87
|
-
payload.params[key.name] = args[index];
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
const finalPath = getFinalPath(payload.params);
|
|
91
|
-
finalURL = payload.query ? `${finalPath}?${(0, import_qs.stringify)(payload.query)}` : finalPath;
|
|
92
|
-
headers = payload.headers || {};
|
|
93
|
-
body = payload.data && typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.body;
|
|
94
|
-
if (payload.data) {
|
|
95
|
-
headers["Content-Type"] = "application/json";
|
|
96
|
-
body = typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.body;
|
|
97
|
-
} else if (payload.body) {
|
|
98
|
-
headers["Content-Type"] = "text/plain";
|
|
99
|
-
body = payload.body;
|
|
100
|
-
} else if (payload.formData) {
|
|
101
|
-
body = payload.formData;
|
|
102
|
-
} else if (payload.formUrlencoded) {
|
|
103
|
-
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
104
|
-
if (typeof payload.formUrlencoded === "object" && // @ts-expect-error
|
|
105
|
-
!(payload.formUrlencoded instanceof URLSearchParams)) {
|
|
106
|
-
body = (0, import_qs.stringify)(payload.formUrlencoded);
|
|
53
|
+
const createRequest = ({ path, method, port, httpMethodDecider = 'functionName', fetch: fetch1 = originFetch, domain, requestId = 'default' })=>{
|
|
54
|
+
const getFinalPath = (0, external_path_to_regexp_namespaceObject.compile)(path, {
|
|
55
|
+
encode: encodeURIComponent
|
|
56
|
+
});
|
|
57
|
+
const keys = [];
|
|
58
|
+
(0, external_path_to_regexp_namespaceObject.pathToRegexp)(path, keys);
|
|
59
|
+
const sender = async (...args)=>{
|
|
60
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
61
|
+
let body;
|
|
62
|
+
let finalURL;
|
|
63
|
+
let headers;
|
|
64
|
+
if ('inputParams' === httpMethodDecider) {
|
|
65
|
+
finalURL = path;
|
|
66
|
+
body = JSON.stringify({
|
|
67
|
+
args
|
|
68
|
+
});
|
|
69
|
+
headers = {
|
|
70
|
+
'Content-Type': 'application/json'
|
|
71
|
+
};
|
|
107
72
|
} else {
|
|
108
|
-
|
|
73
|
+
const payload = 'object' == typeof args[args.length - 1] ? args[args.length - 1] : {};
|
|
74
|
+
payload.params = payload.params || {};
|
|
75
|
+
const requestParams = args[0];
|
|
76
|
+
if ('object' == typeof requestParams && requestParams.params) {
|
|
77
|
+
const { params } = requestParams;
|
|
78
|
+
keys.forEach((key)=>{
|
|
79
|
+
payload.params[key.name] = params[key.name];
|
|
80
|
+
});
|
|
81
|
+
} else keys.forEach((key, index)=>{
|
|
82
|
+
payload.params[key.name] = args[index];
|
|
83
|
+
});
|
|
84
|
+
const finalPath = getFinalPath(payload.params);
|
|
85
|
+
finalURL = payload.query ? `${finalPath}?${(0, external_qs_namespaceObject.stringify)(payload.query)}` : finalPath;
|
|
86
|
+
headers = payload.headers || {};
|
|
87
|
+
body = payload.data && 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
|
|
88
|
+
if (payload.data) {
|
|
89
|
+
headers['Content-Type'] = 'application/json';
|
|
90
|
+
body = 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
|
|
91
|
+
} else if (payload.body) {
|
|
92
|
+
headers['Content-Type'] = 'text/plain';
|
|
93
|
+
body = payload.body;
|
|
94
|
+
} else if (payload.formData) body = payload.formData;
|
|
95
|
+
else if (payload.formUrlencoded) {
|
|
96
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
97
|
+
body = 'object' != typeof payload.formUrlencoded || payload.formUrlencoded instanceof URLSearchParams ? payload.formUrlencoded : (0, external_qs_namespaceObject.stringify)(payload.formUrlencoded);
|
|
98
|
+
}
|
|
109
99
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
};
|
|
121
|
-
return sender;
|
|
100
|
+
headers.accept = "application/json,*/*;q=0.8";
|
|
101
|
+
const configDomain = domainMap.get(requestId);
|
|
102
|
+
finalURL = `${configDomain || domain || ''}${finalURL}`;
|
|
103
|
+
return fetcher(finalURL, {
|
|
104
|
+
method,
|
|
105
|
+
body,
|
|
106
|
+
headers
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
return sender;
|
|
122
110
|
};
|
|
123
|
-
const createUploader = ({ path, domain, requestId =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
});
|
|
127
|
-
const sender = (...args) => {
|
|
128
|
-
const fetcher = realRequest.get(requestId) || originFetch;
|
|
129
|
-
const { body, headers, params } = (0, import_utiles.getUploadPayload)(args);
|
|
130
|
-
const finalPath = getFinalPath(params);
|
|
131
|
-
const configDomain = domainMap.get(requestId);
|
|
132
|
-
const finalURL = `${configDomain || domain || ""}${finalPath}`;
|
|
133
|
-
return fetcher(finalURL, {
|
|
134
|
-
method: "POST",
|
|
135
|
-
body,
|
|
136
|
-
headers
|
|
111
|
+
const createUploader = ({ path, domain, requestId = 'default' })=>{
|
|
112
|
+
const getFinalPath = (0, external_path_to_regexp_namespaceObject.compile)(path, {
|
|
113
|
+
encode: encodeURIComponent
|
|
137
114
|
});
|
|
138
|
-
|
|
139
|
-
|
|
115
|
+
const sender = (...args)=>{
|
|
116
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
117
|
+
const { body, headers, params } = (0, external_utiles_js_namespaceObject.getUploadPayload)(args);
|
|
118
|
+
const finalPath = getFinalPath(params);
|
|
119
|
+
const configDomain = domainMap.get(requestId);
|
|
120
|
+
const finalURL = `${configDomain || domain || ''}${finalPath}`;
|
|
121
|
+
return fetcher(finalURL, {
|
|
122
|
+
method: 'POST',
|
|
123
|
+
body,
|
|
124
|
+
headers
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
return sender;
|
|
140
128
|
};
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
129
|
+
exports.configure = __webpack_exports__.configure;
|
|
130
|
+
exports.createRequest = __webpack_exports__.createRequest;
|
|
131
|
+
exports.createUploader = __webpack_exports__.createUploader;
|
|
132
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
133
|
+
"configure",
|
|
134
|
+
"createRequest",
|
|
135
|
+
"createUploader"
|
|
136
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
137
|
+
Object.defineProperty(exports, '__esModule', {
|
|
138
|
+
value: true
|
|
146
139
|
});
|
package/dist/cjs/handleRes.js
CHANGED
|
@@ -1,51 +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
|
+
handleRes: ()=>handleRes
|
|
22
28
|
});
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return res.
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
const handleRes = async (res)=>{
|
|
30
|
+
const contentType = res.headers.get('content-type');
|
|
31
|
+
if (!res.ok) {
|
|
32
|
+
const data = await res.json();
|
|
33
|
+
res.data = data;
|
|
34
|
+
throw res;
|
|
35
|
+
}
|
|
36
|
+
if (contentType?.includes('application/json') || contentType?.includes('text/json')) return res.json();
|
|
37
|
+
if (contentType?.includes('text/html') || contentType?.includes('text/plain')) return res.text();
|
|
38
|
+
if ((contentType?.includes('application/x-www-form-urlencoded') || contentType?.includes('multipart/form-data')) && res instanceof Response) return res.formData();
|
|
39
|
+
if (contentType?.includes('application/octet-stream')) return res.arrayBuffer();
|
|
40
|
+
if (contentType?.includes('image/png')) return res;
|
|
35
41
|
return res.text();
|
|
36
|
-
}
|
|
37
|
-
if (((contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/x-www-form-urlencoded")) || (contentType === null || contentType === void 0 ? void 0 : contentType.includes("multipart/form-data"))) && res instanceof Response) {
|
|
38
|
-
return res.formData();
|
|
39
|
-
}
|
|
40
|
-
if (contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/octet-stream")) {
|
|
41
|
-
return res.arrayBuffer();
|
|
42
|
-
}
|
|
43
|
-
if (contentType === null || contentType === void 0 ? void 0 : contentType.includes("image/png")) {
|
|
44
|
-
return res;
|
|
45
|
-
}
|
|
46
|
-
return res.text();
|
|
47
42
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
exports.handleRes = __webpack_exports__.handleRes;
|
|
44
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
45
|
+
"handleRes"
|
|
46
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
47
|
+
Object.defineProperty(exports, '__esModule', {
|
|
48
|
+
value: true
|
|
51
49
|
});
|