@modern-js/create-request 2.23.1 → 2.24.1-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 +4 -6
- package/dist/cjs/node.js +3 -3
- package/dist/esm/browser.js +4 -5
- package/dist/esm/node.js +3 -3
- package/dist/esm-node/browser.js +4 -5
- package/dist/esm-node/node.js +3 -3
- package/dist/js/modern/browser.js +87 -0
- package/dist/js/modern/handleRes.js +44 -0
- package/dist/js/modern/index.js +8 -0
- package/dist/js/modern/node.js +72 -0
- package/dist/js/modern/types.js +0 -0
- package/dist/js/node/browser.js +117 -0
- package/dist/js/node/handleRes.js +67 -0
- package/dist/js/node/index.js +29 -0
- package/dist/js/node/node.js +102 -0
- package/dist/js/node/types.js +15 -0
- package/dist/js/treeshaking/browser.js +243 -0
- package/dist/js/treeshaking/handleRes.js +187 -0
- package/dist/js/treeshaking/index.js +36 -0
- package/dist/js/treeshaking/node.js +126 -0
- package/dist/js/treeshaking/types.js +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
function _arrayLikeToArray(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _arrayWithoutHoles(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
8
|
+
}
|
|
9
|
+
function _iterableToArray(iter) {
|
|
10
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
11
|
+
}
|
|
12
|
+
function _nonIterableSpread() {
|
|
13
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
14
|
+
}
|
|
15
|
+
function _toConsumableArray(arr) {
|
|
16
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
17
|
+
}
|
|
18
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
19
|
+
if (!o) return;
|
|
20
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
21
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
22
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
23
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
24
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
25
|
+
}
|
|
26
|
+
import nodeFetch from "node-fetch";
|
|
27
|
+
import { compile, pathToRegexp } from "path-to-regexp";
|
|
28
|
+
import { useHeaders } from "@modern-js/utils/ssr";
|
|
29
|
+
import qs from "query-string";
|
|
30
|
+
import { handleRes } from "./handleRes";
|
|
31
|
+
var realRequest;
|
|
32
|
+
var realAllowedHeaders = [];
|
|
33
|
+
var originFetch = function() {
|
|
34
|
+
for(var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++){
|
|
35
|
+
params[_key] = arguments[_key];
|
|
36
|
+
}
|
|
37
|
+
return nodeFetch.apply(void 0, _toConsumableArray(params)).then(handleRes);
|
|
38
|
+
};
|
|
39
|
+
var configure = function(options) {
|
|
40
|
+
var request = options.request, interceptor = options.interceptor, allowedHeaders = options.allowedHeaders;
|
|
41
|
+
realRequest = request || originFetch;
|
|
42
|
+
if (interceptor && !request) {
|
|
43
|
+
realRequest = interceptor(nodeFetch);
|
|
44
|
+
}
|
|
45
|
+
if (Array.isArray(allowedHeaders)) {
|
|
46
|
+
realAllowedHeaders = allowedHeaders;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var createRequest = function(path, method, port) {
|
|
50
|
+
var fetch = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : nodeFetch;
|
|
51
|
+
var getFinalPath = compile(path, {
|
|
52
|
+
encode: encodeURIComponent
|
|
53
|
+
});
|
|
54
|
+
var keys = [];
|
|
55
|
+
pathToRegexp(path, keys);
|
|
56
|
+
var sender = function() {
|
|
57
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
58
|
+
args[_key] = arguments[_key];
|
|
59
|
+
}
|
|
60
|
+
var webRequestHeaders = useHeaders();
|
|
61
|
+
var payload = typeof args[args.length - 1] === "object" ? args[args.length - 1] : {};
|
|
62
|
+
payload.params = payload.params || {};
|
|
63
|
+
var requestParams = args[0];
|
|
64
|
+
if (typeof requestParams === "object" && requestParams.params) {
|
|
65
|
+
var params = requestParams.params;
|
|
66
|
+
keys.forEach(function(key) {
|
|
67
|
+
payload.params[key.name] = params[key.name];
|
|
68
|
+
});
|
|
69
|
+
} else {
|
|
70
|
+
keys.forEach(function(key, index) {
|
|
71
|
+
payload.params[key.name] = args[index];
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
var plainPath = getFinalPath(payload.params);
|
|
75
|
+
var finalPath = payload.query ? "".concat(plainPath, "?").concat(qs.stringify(payload.query)) : plainPath;
|
|
76
|
+
var headers = payload.headers || {};
|
|
77
|
+
var body;
|
|
78
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
79
|
+
try {
|
|
80
|
+
for(var _iterator = realAllowedHeaders[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
81
|
+
var key = _step.value;
|
|
82
|
+
if (typeof webRequestHeaders[key] !== "undefined") {
|
|
83
|
+
headers[key] = webRequestHeaders[key];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
} catch (err) {
|
|
87
|
+
_didIteratorError = true;
|
|
88
|
+
_iteratorError = err;
|
|
89
|
+
} finally{
|
|
90
|
+
try {
|
|
91
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
92
|
+
_iterator.return();
|
|
93
|
+
}
|
|
94
|
+
} finally{
|
|
95
|
+
if (_didIteratorError) {
|
|
96
|
+
throw _iteratorError;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (payload.data) {
|
|
101
|
+
headers["Content-Type"] = "application/json";
|
|
102
|
+
body = typeof payload.data === "object" ? 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) {
|
|
107
|
+
body = payload.formData;
|
|
108
|
+
} else if (payload.formUrlencoded) {
|
|
109
|
+
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
110
|
+
if (typeof payload.formUrlencoded === "object") {
|
|
111
|
+
body = qs.stringify(payload.formUrlencoded);
|
|
112
|
+
} else {
|
|
113
|
+
body = payload.formUrlencoded;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
var url = "http://localhost:".concat(port).concat(finalPath);
|
|
117
|
+
var fetcher = realRequest || originFetch;
|
|
118
|
+
return fetcher(url, {
|
|
119
|
+
method: method,
|
|
120
|
+
body: body,
|
|
121
|
+
headers: headers
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
return sender;
|
|
125
|
+
};
|
|
126
|
+
export { configure, createRequest };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.
|
|
18
|
+
"version": "2.24.1-alpha.0",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"path-to-regexp": "^6.2.0",
|
|
65
65
|
"query-string": "^7.1.1",
|
|
66
66
|
"@swc/helpers": "0.5.1",
|
|
67
|
-
"@modern-js/utils": "2.
|
|
67
|
+
"@modern-js/utils": "2.24.0"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
70
|
"@types/jest": "^29",
|
|
@@ -74,9 +74,9 @@
|
|
|
74
74
|
"jest": "^29",
|
|
75
75
|
"nock": "^13.2.1",
|
|
76
76
|
"typescript": "^5",
|
|
77
|
-
"@
|
|
78
|
-
"@scripts/
|
|
79
|
-
"@
|
|
77
|
+
"@modern-js/types": "2.24.0",
|
|
78
|
+
"@scripts/build": "2.24.0",
|
|
79
|
+
"@scripts/jest-config": "2.24.0"
|
|
80
80
|
},
|
|
81
81
|
"sideEffects": false,
|
|
82
82
|
"publishConfig": {
|