@modern-js/create-request 2.63.6 → 2.64.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 +15 -11
- package/dist/cjs/node.js +17 -12
- package/dist/esm/browser.js +17 -12
- package/dist/esm/node.js +19 -13
- package/dist/esm-node/browser.js +15 -11
- package/dist/esm-node/node.js +17 -12
- package/dist/types/browser.d.ts +2 -2
- package/dist/types/node.d.ts +2 -2
- package/dist/types/types.d.ts +17 -2
- package/package.json +16 -6
package/dist/cjs/browser.js
CHANGED
|
@@ -27,8 +27,8 @@ var import_path_to_regexp = require("path-to-regexp");
|
|
|
27
27
|
var import_query_string = require("query-string");
|
|
28
28
|
var import_handleRes = require("./handleRes");
|
|
29
29
|
var import_utiles = require("./utiles");
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const realRequest = /* @__PURE__ */ new Map();
|
|
31
|
+
const realAllowedHeaders = /* @__PURE__ */ new Map();
|
|
32
32
|
const originFetch = (...params) => {
|
|
33
33
|
var _init_method;
|
|
34
34
|
const [url, init] = params;
|
|
@@ -38,23 +38,24 @@ const originFetch = (...params) => {
|
|
|
38
38
|
return fetch(url, init).then(import_handleRes.handleRes);
|
|
39
39
|
};
|
|
40
40
|
const configure = (options) => {
|
|
41
|
-
const { request, interceptor, allowedHeaders } = options;
|
|
42
|
-
|
|
41
|
+
const { request, interceptor, allowedHeaders, requestId = "default" } = options;
|
|
42
|
+
let configuredRequest = request || originFetch;
|
|
43
43
|
if (interceptor && !request) {
|
|
44
|
-
|
|
44
|
+
configuredRequest = interceptor(fetch);
|
|
45
45
|
}
|
|
46
46
|
if (Array.isArray(allowedHeaders)) {
|
|
47
|
-
realAllowedHeaders
|
|
47
|
+
realAllowedHeaders.set(requestId, allowedHeaders);
|
|
48
48
|
}
|
|
49
|
+
realRequest.set(requestId, configuredRequest);
|
|
49
50
|
};
|
|
50
|
-
const createRequest = (path, method, port, httpMethodDecider = "functionName", fetch1 = originFetch) => {
|
|
51
|
+
const createRequest = ({ path, method, port, httpMethodDecider = "functionName", fetch: fetch1 = originFetch, domain, requestId = "default" }) => {
|
|
51
52
|
const getFinalPath = (0, import_path_to_regexp.compile)(path, {
|
|
52
53
|
encode: encodeURIComponent
|
|
53
54
|
});
|
|
54
55
|
const keys = [];
|
|
55
56
|
(0, import_path_to_regexp.pathToRegexp)(path, keys);
|
|
56
57
|
const sender = async (...args) => {
|
|
57
|
-
const fetcher = realRequest || originFetch;
|
|
58
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
58
59
|
let body;
|
|
59
60
|
let finalURL;
|
|
60
61
|
let headers;
|
|
@@ -103,6 +104,9 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
103
104
|
}
|
|
104
105
|
}
|
|
105
106
|
headers.accept = `application/json,*/*;q=0.8`;
|
|
107
|
+
if (domain) {
|
|
108
|
+
finalURL = `${domain}${finalURL}`;
|
|
109
|
+
}
|
|
106
110
|
return fetcher(finalURL, {
|
|
107
111
|
method,
|
|
108
112
|
body,
|
|
@@ -111,11 +115,11 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
111
115
|
};
|
|
112
116
|
return sender;
|
|
113
117
|
};
|
|
114
|
-
const createUploader = (path) => {
|
|
118
|
+
const createUploader = ({ path, domain, requestId = "default" }) => {
|
|
115
119
|
const sender = (...args) => {
|
|
116
|
-
const fetcher = realRequest || originFetch;
|
|
120
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
117
121
|
const { body, headers } = (0, import_utiles.getUploadPayload)(args);
|
|
118
|
-
return fetcher(path, {
|
|
122
|
+
return fetcher(domain ? `${domain}${path}` : path, {
|
|
119
123
|
method: "POST",
|
|
120
124
|
body,
|
|
121
125
|
headers
|
package/dist/cjs/node.js
CHANGED
|
@@ -39,8 +39,8 @@ var import_path_to_regexp = require("path-to-regexp");
|
|
|
39
39
|
var import_query_string = require("query-string");
|
|
40
40
|
var import_handleRes = require("./handleRes");
|
|
41
41
|
var import_utiles = require("./utiles");
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const realRequest = /* @__PURE__ */ new Map();
|
|
43
|
+
const realAllowedHeaders = /* @__PURE__ */ new Map();
|
|
44
44
|
const originFetch = (...params) => {
|
|
45
45
|
var _init_method;
|
|
46
46
|
const [, init] = params;
|
|
@@ -50,23 +50,27 @@ const originFetch = (...params) => {
|
|
|
50
50
|
return (0, import_node_fetch.default)(...params).then(import_handleRes.handleRes);
|
|
51
51
|
};
|
|
52
52
|
const configure = (options) => {
|
|
53
|
-
const { request, interceptor, allowedHeaders } = options;
|
|
54
|
-
|
|
53
|
+
const { request, interceptor, allowedHeaders, requestId = "default" } = options;
|
|
54
|
+
let configuredRequest = request || originFetch;
|
|
55
55
|
if (interceptor && !request) {
|
|
56
|
-
|
|
56
|
+
configuredRequest = interceptor(import_node_fetch.default);
|
|
57
57
|
}
|
|
58
58
|
if (Array.isArray(allowedHeaders)) {
|
|
59
|
-
realAllowedHeaders
|
|
59
|
+
realAllowedHeaders.set(requestId, allowedHeaders);
|
|
60
60
|
}
|
|
61
|
+
realRequest.set(requestId, configuredRequest);
|
|
61
62
|
};
|
|
62
|
-
const createRequest = (path, method, port, httpMethodDecider = "functionName", fetch =
|
|
63
|
+
const createRequest = ({ path, method, port, httpMethodDecider = "functionName", fetch = originFetch, requestId = "default" }) => {
|
|
63
64
|
const getFinalPath = (0, import_path_to_regexp.compile)(path, {
|
|
64
65
|
encode: encodeURIComponent
|
|
65
66
|
});
|
|
66
67
|
const keys = [];
|
|
67
68
|
(0, import_path_to_regexp.pathToRegexp)(path, keys);
|
|
68
69
|
const sender = (...args) => {
|
|
69
|
-
|
|
70
|
+
let webRequestHeaders = [];
|
|
71
|
+
if (requestId === "default") {
|
|
72
|
+
webRequestHeaders = (0, import_node.useHeaders)();
|
|
73
|
+
}
|
|
70
74
|
let body;
|
|
71
75
|
let headers;
|
|
72
76
|
let url;
|
|
@@ -93,7 +97,8 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
93
97
|
const plainPath = getFinalPath(payload.params);
|
|
94
98
|
const finalPath = payload.query ? `${plainPath}?${(0, import_query_string.stringify)(payload.query)}` : plainPath;
|
|
95
99
|
headers = payload.headers || {};
|
|
96
|
-
|
|
100
|
+
const targetAllowedHeaders = realAllowedHeaders.get(requestId) || [];
|
|
101
|
+
for (const key of targetAllowedHeaders) {
|
|
97
102
|
if (typeof webRequestHeaders[key] !== "undefined") {
|
|
98
103
|
headers[key] = webRequestHeaders[key];
|
|
99
104
|
}
|
|
@@ -116,7 +121,7 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
116
121
|
}
|
|
117
122
|
url = `http://127.0.0.1:${port}${finalPath}`;
|
|
118
123
|
}
|
|
119
|
-
const fetcher = realRequest || originFetch;
|
|
124
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
120
125
|
if (method.toLowerCase() === "get") {
|
|
121
126
|
body = void 0;
|
|
122
127
|
}
|
|
@@ -129,9 +134,9 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
129
134
|
};
|
|
130
135
|
return sender;
|
|
131
136
|
};
|
|
132
|
-
const createUploader = (path) => {
|
|
137
|
+
const createUploader = ({ path, requestId = "default" }) => {
|
|
133
138
|
const sender = (...args) => {
|
|
134
|
-
const fetcher = realRequest || originFetch;
|
|
139
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
135
140
|
const { body, headers } = (0, import_utiles.getUploadPayload)(args);
|
|
136
141
|
return fetcher(path, {
|
|
137
142
|
method: "POST",
|
package/dist/esm/browser.js
CHANGED
|
@@ -7,8 +7,8 @@ import { compile, pathToRegexp } from "path-to-regexp";
|
|
|
7
7
|
import { stringify } from "query-string";
|
|
8
8
|
import { handleRes } from "./handleRes";
|
|
9
9
|
import { getUploadPayload } from "./utiles";
|
|
10
|
-
var realRequest;
|
|
11
|
-
var realAllowedHeaders;
|
|
10
|
+
var realRequest = /* @__PURE__ */ new Map();
|
|
11
|
+
var realAllowedHeaders = /* @__PURE__ */ new Map();
|
|
12
12
|
var originFetch = function() {
|
|
13
13
|
for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
14
14
|
params[_key] = arguments[_key];
|
|
@@ -21,17 +21,18 @@ var originFetch = function() {
|
|
|
21
21
|
return fetch(url, init).then(handleRes);
|
|
22
22
|
};
|
|
23
23
|
var configure = function(options) {
|
|
24
|
-
var request = options.request, interceptor = options.interceptor, allowedHeaders = options.allowedHeaders;
|
|
25
|
-
|
|
24
|
+
var request = options.request, interceptor = options.interceptor, allowedHeaders = options.allowedHeaders, _options_requestId = options.requestId, requestId = _options_requestId === void 0 ? "default" : _options_requestId;
|
|
25
|
+
var configuredRequest = request || originFetch;
|
|
26
26
|
if (interceptor && !request) {
|
|
27
|
-
|
|
27
|
+
configuredRequest = interceptor(fetch);
|
|
28
28
|
}
|
|
29
29
|
if (Array.isArray(allowedHeaders)) {
|
|
30
|
-
realAllowedHeaders
|
|
30
|
+
realAllowedHeaders.set(requestId, allowedHeaders);
|
|
31
31
|
}
|
|
32
|
+
realRequest.set(requestId, configuredRequest);
|
|
32
33
|
};
|
|
33
|
-
var createRequest = function(
|
|
34
|
-
var
|
|
34
|
+
var createRequest = function(param) {
|
|
35
|
+
var path = param.path, method = param.method, port = param.port, _param_httpMethodDecider = param.httpMethodDecider, httpMethodDecider = _param_httpMethodDecider === void 0 ? "functionName" : _param_httpMethodDecider, _param_fetch = param.fetch, fetch1 = _param_fetch === void 0 ? originFetch : _param_fetch, domain = param.domain, _param_requestId = param.requestId, requestId = _param_requestId === void 0 ? "default" : _param_requestId;
|
|
35
36
|
var getFinalPath = compile(path, {
|
|
36
37
|
encode: encodeURIComponent
|
|
37
38
|
});
|
|
@@ -45,7 +46,7 @@ var createRequest = function(path, method, port) {
|
|
|
45
46
|
for (_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
46
47
|
args[_key] = _arguments[_key];
|
|
47
48
|
}
|
|
48
|
-
fetcher = realRequest || originFetch;
|
|
49
|
+
fetcher = realRequest.get(requestId) || originFetch;
|
|
49
50
|
if (httpMethodDecider === "inputParams") {
|
|
50
51
|
finalURL = path;
|
|
51
52
|
body = JSON.stringify({
|
|
@@ -91,6 +92,9 @@ var createRequest = function(path, method, port) {
|
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
94
|
headers.accept = "application/json,*/*;q=0.8";
|
|
95
|
+
if (domain) {
|
|
96
|
+
finalURL = "".concat(domain).concat(finalURL);
|
|
97
|
+
}
|
|
94
98
|
return [
|
|
95
99
|
2,
|
|
96
100
|
fetcher(finalURL, {
|
|
@@ -107,14 +111,15 @@ var createRequest = function(path, method, port) {
|
|
|
107
111
|
}();
|
|
108
112
|
return sender;
|
|
109
113
|
};
|
|
110
|
-
var createUploader = function(
|
|
114
|
+
var createUploader = function(param) {
|
|
115
|
+
var path = param.path, domain = param.domain, _param_requestId = param.requestId, requestId = _param_requestId === void 0 ? "default" : _param_requestId;
|
|
111
116
|
var sender = function() {
|
|
112
117
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
113
118
|
args[_key] = arguments[_key];
|
|
114
119
|
}
|
|
115
|
-
var fetcher = realRequest || originFetch;
|
|
120
|
+
var fetcher = realRequest.get(requestId) || originFetch;
|
|
116
121
|
var _getUploadPayload = getUploadPayload(args), body = _getUploadPayload.body, headers = _getUploadPayload.headers;
|
|
117
|
-
return fetcher(path, {
|
|
122
|
+
return fetcher(domain ? "".concat(domain).concat(path) : path, {
|
|
118
123
|
method: "POST",
|
|
119
124
|
body,
|
|
120
125
|
headers
|
package/dist/esm/node.js
CHANGED
|
@@ -7,8 +7,8 @@ import { compile, pathToRegexp } from "path-to-regexp";
|
|
|
7
7
|
import { stringify } from "query-string";
|
|
8
8
|
import { handleRes } from "./handleRes";
|
|
9
9
|
import { getUploadPayload } from "./utiles";
|
|
10
|
-
var realRequest;
|
|
11
|
-
var realAllowedHeaders =
|
|
10
|
+
var realRequest = /* @__PURE__ */ new Map();
|
|
11
|
+
var realAllowedHeaders = /* @__PURE__ */ new Map();
|
|
12
12
|
var originFetch = function() {
|
|
13
13
|
for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
14
14
|
params[_key] = arguments[_key];
|
|
@@ -21,17 +21,18 @@ var originFetch = function() {
|
|
|
21
21
|
return nodeFetch.apply(void 0, _to_consumable_array(params)).then(handleRes);
|
|
22
22
|
};
|
|
23
23
|
var configure = function(options) {
|
|
24
|
-
var request = options.request, interceptor = options.interceptor, allowedHeaders = options.allowedHeaders;
|
|
25
|
-
|
|
24
|
+
var request = options.request, interceptor = options.interceptor, allowedHeaders = options.allowedHeaders, _options_requestId = options.requestId, requestId = _options_requestId === void 0 ? "default" : _options_requestId;
|
|
25
|
+
var configuredRequest = request || originFetch;
|
|
26
26
|
if (interceptor && !request) {
|
|
27
|
-
|
|
27
|
+
configuredRequest = interceptor(nodeFetch);
|
|
28
28
|
}
|
|
29
29
|
if (Array.isArray(allowedHeaders)) {
|
|
30
|
-
realAllowedHeaders
|
|
30
|
+
realAllowedHeaders.set(requestId, allowedHeaders);
|
|
31
31
|
}
|
|
32
|
+
realRequest.set(requestId, configuredRequest);
|
|
32
33
|
};
|
|
33
|
-
var createRequest = function(
|
|
34
|
-
var
|
|
34
|
+
var createRequest = function(param) {
|
|
35
|
+
var path = param.path, method = param.method, port = param.port, _param_httpMethodDecider = param.httpMethodDecider, httpMethodDecider = _param_httpMethodDecider === void 0 ? "functionName" : _param_httpMethodDecider, _param_fetch = param.fetch, fetch = _param_fetch === void 0 ? originFetch : _param_fetch, _param_requestId = param.requestId, requestId = _param_requestId === void 0 ? "default" : _param_requestId;
|
|
35
36
|
var getFinalPath = compile(path, {
|
|
36
37
|
encode: encodeURIComponent
|
|
37
38
|
});
|
|
@@ -41,7 +42,10 @@ var createRequest = function(path, method, port) {
|
|
|
41
42
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
42
43
|
args[_key] = arguments[_key];
|
|
43
44
|
}
|
|
44
|
-
var webRequestHeaders =
|
|
45
|
+
var webRequestHeaders = [];
|
|
46
|
+
if (requestId === "default") {
|
|
47
|
+
webRequestHeaders = useHeaders();
|
|
48
|
+
}
|
|
45
49
|
var body;
|
|
46
50
|
var headers;
|
|
47
51
|
var url;
|
|
@@ -68,9 +72,10 @@ var createRequest = function(path, method, port) {
|
|
|
68
72
|
var plainPath = getFinalPath(payload.params);
|
|
69
73
|
var finalPath = payload.query ? "".concat(plainPath, "?").concat(stringify(payload.query)) : plainPath;
|
|
70
74
|
headers = payload.headers || {};
|
|
75
|
+
var targetAllowedHeaders = realAllowedHeaders.get(requestId) || [];
|
|
71
76
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
72
77
|
try {
|
|
73
|
-
for (var _iterator =
|
|
78
|
+
for (var _iterator = targetAllowedHeaders[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
74
79
|
var key = _step.value;
|
|
75
80
|
if (typeof webRequestHeaders[key] !== "undefined") {
|
|
76
81
|
headers[key] = webRequestHeaders[key];
|
|
@@ -108,7 +113,7 @@ var createRequest = function(path, method, port) {
|
|
|
108
113
|
}
|
|
109
114
|
url = "http://127.0.0.1:".concat(port).concat(finalPath);
|
|
110
115
|
}
|
|
111
|
-
var fetcher = realRequest || originFetch;
|
|
116
|
+
var fetcher = realRequest.get(requestId) || originFetch;
|
|
112
117
|
if (method.toLowerCase() === "get") {
|
|
113
118
|
body = void 0;
|
|
114
119
|
}
|
|
@@ -121,12 +126,13 @@ var createRequest = function(path, method, port) {
|
|
|
121
126
|
};
|
|
122
127
|
return sender;
|
|
123
128
|
};
|
|
124
|
-
var createUploader = function(
|
|
129
|
+
var createUploader = function(param) {
|
|
130
|
+
var path = param.path, _param_requestId = param.requestId, requestId = _param_requestId === void 0 ? "default" : _param_requestId;
|
|
125
131
|
var sender = function() {
|
|
126
132
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
127
133
|
args[_key] = arguments[_key];
|
|
128
134
|
}
|
|
129
|
-
var fetcher = realRequest || originFetch;
|
|
135
|
+
var fetcher = realRequest.get(requestId) || originFetch;
|
|
130
136
|
var _getUploadPayload = getUploadPayload(args), body = _getUploadPayload.body, headers = _getUploadPayload.headers;
|
|
131
137
|
return fetcher(path, {
|
|
132
138
|
method: "POST",
|
package/dist/esm-node/browser.js
CHANGED
|
@@ -2,8 +2,8 @@ import { compile, pathToRegexp } from "path-to-regexp";
|
|
|
2
2
|
import { stringify } from "query-string";
|
|
3
3
|
import { handleRes } from "./handleRes";
|
|
4
4
|
import { getUploadPayload } from "./utiles";
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const realRequest = /* @__PURE__ */ new Map();
|
|
6
|
+
const realAllowedHeaders = /* @__PURE__ */ new Map();
|
|
7
7
|
const originFetch = (...params) => {
|
|
8
8
|
var _init_method;
|
|
9
9
|
const [url, init] = params;
|
|
@@ -13,23 +13,24 @@ const originFetch = (...params) => {
|
|
|
13
13
|
return fetch(url, init).then(handleRes);
|
|
14
14
|
};
|
|
15
15
|
const configure = (options) => {
|
|
16
|
-
const { request, interceptor, allowedHeaders } = options;
|
|
17
|
-
|
|
16
|
+
const { request, interceptor, allowedHeaders, requestId = "default" } = options;
|
|
17
|
+
let configuredRequest = request || originFetch;
|
|
18
18
|
if (interceptor && !request) {
|
|
19
|
-
|
|
19
|
+
configuredRequest = interceptor(fetch);
|
|
20
20
|
}
|
|
21
21
|
if (Array.isArray(allowedHeaders)) {
|
|
22
|
-
realAllowedHeaders
|
|
22
|
+
realAllowedHeaders.set(requestId, allowedHeaders);
|
|
23
23
|
}
|
|
24
|
+
realRequest.set(requestId, configuredRequest);
|
|
24
25
|
};
|
|
25
|
-
const createRequest = (path, method, port, httpMethodDecider = "functionName", fetch1 = originFetch) => {
|
|
26
|
+
const createRequest = ({ path, method, port, httpMethodDecider = "functionName", fetch: fetch1 = originFetch, domain, requestId = "default" }) => {
|
|
26
27
|
const getFinalPath = compile(path, {
|
|
27
28
|
encode: encodeURIComponent
|
|
28
29
|
});
|
|
29
30
|
const keys = [];
|
|
30
31
|
pathToRegexp(path, keys);
|
|
31
32
|
const sender = async (...args) => {
|
|
32
|
-
const fetcher = realRequest || originFetch;
|
|
33
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
33
34
|
let body;
|
|
34
35
|
let finalURL;
|
|
35
36
|
let headers;
|
|
@@ -78,6 +79,9 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
headers.accept = `application/json,*/*;q=0.8`;
|
|
82
|
+
if (domain) {
|
|
83
|
+
finalURL = `${domain}${finalURL}`;
|
|
84
|
+
}
|
|
81
85
|
return fetcher(finalURL, {
|
|
82
86
|
method,
|
|
83
87
|
body,
|
|
@@ -86,11 +90,11 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
86
90
|
};
|
|
87
91
|
return sender;
|
|
88
92
|
};
|
|
89
|
-
const createUploader = (path) => {
|
|
93
|
+
const createUploader = ({ path, domain, requestId = "default" }) => {
|
|
90
94
|
const sender = (...args) => {
|
|
91
|
-
const fetcher = realRequest || originFetch;
|
|
95
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
92
96
|
const { body, headers } = getUploadPayload(args);
|
|
93
|
-
return fetcher(path, {
|
|
97
|
+
return fetcher(domain ? `${domain}${path}` : path, {
|
|
94
98
|
method: "POST",
|
|
95
99
|
body,
|
|
96
100
|
headers
|
package/dist/esm-node/node.js
CHANGED
|
@@ -4,8 +4,8 @@ import { compile, pathToRegexp } from "path-to-regexp";
|
|
|
4
4
|
import { stringify } from "query-string";
|
|
5
5
|
import { handleRes } from "./handleRes";
|
|
6
6
|
import { getUploadPayload } from "./utiles";
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const realRequest = /* @__PURE__ */ new Map();
|
|
8
|
+
const realAllowedHeaders = /* @__PURE__ */ new Map();
|
|
9
9
|
const originFetch = (...params) => {
|
|
10
10
|
var _init_method;
|
|
11
11
|
const [, init] = params;
|
|
@@ -15,23 +15,27 @@ const originFetch = (...params) => {
|
|
|
15
15
|
return nodeFetch(...params).then(handleRes);
|
|
16
16
|
};
|
|
17
17
|
const configure = (options) => {
|
|
18
|
-
const { request, interceptor, allowedHeaders } = options;
|
|
19
|
-
|
|
18
|
+
const { request, interceptor, allowedHeaders, requestId = "default" } = options;
|
|
19
|
+
let configuredRequest = request || originFetch;
|
|
20
20
|
if (interceptor && !request) {
|
|
21
|
-
|
|
21
|
+
configuredRequest = interceptor(nodeFetch);
|
|
22
22
|
}
|
|
23
23
|
if (Array.isArray(allowedHeaders)) {
|
|
24
|
-
realAllowedHeaders
|
|
24
|
+
realAllowedHeaders.set(requestId, allowedHeaders);
|
|
25
25
|
}
|
|
26
|
+
realRequest.set(requestId, configuredRequest);
|
|
26
27
|
};
|
|
27
|
-
const createRequest = (path, method, port, httpMethodDecider = "functionName", fetch =
|
|
28
|
+
const createRequest = ({ path, method, port, httpMethodDecider = "functionName", fetch = originFetch, requestId = "default" }) => {
|
|
28
29
|
const getFinalPath = compile(path, {
|
|
29
30
|
encode: encodeURIComponent
|
|
30
31
|
});
|
|
31
32
|
const keys = [];
|
|
32
33
|
pathToRegexp(path, keys);
|
|
33
34
|
const sender = (...args) => {
|
|
34
|
-
|
|
35
|
+
let webRequestHeaders = [];
|
|
36
|
+
if (requestId === "default") {
|
|
37
|
+
webRequestHeaders = useHeaders();
|
|
38
|
+
}
|
|
35
39
|
let body;
|
|
36
40
|
let headers;
|
|
37
41
|
let url;
|
|
@@ -58,7 +62,8 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
58
62
|
const plainPath = getFinalPath(payload.params);
|
|
59
63
|
const finalPath = payload.query ? `${plainPath}?${stringify(payload.query)}` : plainPath;
|
|
60
64
|
headers = payload.headers || {};
|
|
61
|
-
|
|
65
|
+
const targetAllowedHeaders = realAllowedHeaders.get(requestId) || [];
|
|
66
|
+
for (const key of targetAllowedHeaders) {
|
|
62
67
|
if (typeof webRequestHeaders[key] !== "undefined") {
|
|
63
68
|
headers[key] = webRequestHeaders[key];
|
|
64
69
|
}
|
|
@@ -81,7 +86,7 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
81
86
|
}
|
|
82
87
|
url = `http://127.0.0.1:${port}${finalPath}`;
|
|
83
88
|
}
|
|
84
|
-
const fetcher = realRequest || originFetch;
|
|
89
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
85
90
|
if (method.toLowerCase() === "get") {
|
|
86
91
|
body = void 0;
|
|
87
92
|
}
|
|
@@ -94,9 +99,9 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
94
99
|
};
|
|
95
100
|
return sender;
|
|
96
101
|
};
|
|
97
|
-
const createUploader = (path) => {
|
|
102
|
+
const createUploader = ({ path, requestId = "default" }) => {
|
|
98
103
|
const sender = (...args) => {
|
|
99
|
-
const fetcher = realRequest || originFetch;
|
|
104
|
+
const fetcher = realRequest.get(requestId) || originFetch;
|
|
100
105
|
const { body, headers } = getUploadPayload(args);
|
|
101
106
|
return fetcher(path, {
|
|
102
107
|
method: "POST",
|
package/dist/types/browser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IOptions, RequestCreator,
|
|
1
|
+
import type { IOptions, RequestCreator, UploadCreator } from './types';
|
|
2
2
|
export declare const configure: (options: IOptions) => void;
|
|
3
3
|
export declare const createRequest: RequestCreator;
|
|
4
|
-
export declare const createUploader:
|
|
4
|
+
export declare const createUploader: UploadCreator;
|
package/dist/types/node.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import nodeFetch from 'node-fetch';
|
|
2
|
-
import type { IOptions, RequestCreator,
|
|
2
|
+
import type { IOptions, RequestCreator, UploadCreator } from './types';
|
|
3
3
|
export declare const configure: (options: IOptions<typeof nodeFetch>) => void;
|
|
4
4
|
export declare const createRequest: RequestCreator<typeof nodeFetch>;
|
|
5
|
-
export declare const createUploader:
|
|
5
|
+
export declare const createUploader: UploadCreator;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -13,10 +13,25 @@ export type BFFRequestPayload = {
|
|
|
13
13
|
export type Sender<F = typeof fetch> = ((...args: any[]) => Promise<any>) & {
|
|
14
14
|
fetch?: F;
|
|
15
15
|
};
|
|
16
|
-
export type
|
|
17
|
-
|
|
16
|
+
export type RequestOptions<F = typeof fetch> = {
|
|
17
|
+
path: string;
|
|
18
|
+
method: string;
|
|
19
|
+
port: number;
|
|
20
|
+
httpMethodDecider?: HttpMethodDecider;
|
|
21
|
+
domain?: string;
|
|
22
|
+
fetch?: F;
|
|
23
|
+
requestId?: string;
|
|
24
|
+
};
|
|
25
|
+
export type RequestCreator<F = typeof fetch> = (options: RequestOptions<F>) => Sender;
|
|
26
|
+
export type UploadOptions = {
|
|
27
|
+
path: string;
|
|
28
|
+
domain?: string;
|
|
29
|
+
requestId?: string;
|
|
30
|
+
};
|
|
31
|
+
export type UploadCreator = (options: UploadOptions) => Sender;
|
|
18
32
|
export type IOptions<F = typeof fetch> = {
|
|
19
33
|
request?: F;
|
|
20
34
|
interceptor?: (request: F) => F;
|
|
21
35
|
allowedHeaders?: string[];
|
|
36
|
+
requestId?: string;
|
|
22
37
|
};
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.
|
|
18
|
+
"version": "2.64.0",
|
|
19
19
|
"jsnext:source": "./src/node.ts",
|
|
20
20
|
"types": "./dist/types/browser.d.ts",
|
|
21
21
|
"main": "./dist/cjs/node.js",
|
|
@@ -44,6 +44,16 @@
|
|
|
44
44
|
"types": "./dist/types/node.d.ts",
|
|
45
45
|
"jsnext:source": "./src/node.ts",
|
|
46
46
|
"default": "./dist/cjs/node.js"
|
|
47
|
+
},
|
|
48
|
+
"./browser": {
|
|
49
|
+
"types": "./dist/types/browser.d.ts",
|
|
50
|
+
"jsnext:source": "./src/browser.ts",
|
|
51
|
+
"default": "./dist/esm/browser.js"
|
|
52
|
+
},
|
|
53
|
+
"./default": {
|
|
54
|
+
"types": "./dist/types/node.d.ts",
|
|
55
|
+
"jsnext:source": "./src/node.ts",
|
|
56
|
+
"default": "./dist/cjs/node.js"
|
|
47
57
|
}
|
|
48
58
|
},
|
|
49
59
|
"typesVersions": {
|
|
@@ -65,8 +75,8 @@
|
|
|
65
75
|
"node-fetch": "^2.6.1",
|
|
66
76
|
"path-to-regexp": "^6.2.0",
|
|
67
77
|
"query-string": "^7.1.1",
|
|
68
|
-
"@modern-js/runtime-utils": "2.
|
|
69
|
-
"@modern-js/utils": "2.
|
|
78
|
+
"@modern-js/runtime-utils": "2.64.0",
|
|
79
|
+
"@modern-js/utils": "2.64.0"
|
|
70
80
|
},
|
|
71
81
|
"devDependencies": {
|
|
72
82
|
"@types/jest": "^29",
|
|
@@ -76,9 +86,9 @@
|
|
|
76
86
|
"jest": "^29",
|
|
77
87
|
"nock": "^13.2.1",
|
|
78
88
|
"typescript": "^5",
|
|
79
|
-
"@modern-js/types": "2.
|
|
80
|
-
"@scripts/build": "2.
|
|
81
|
-
"@scripts/jest-config": "2.
|
|
89
|
+
"@modern-js/types": "2.64.0",
|
|
90
|
+
"@scripts/build": "2.64.0",
|
|
91
|
+
"@scripts/jest-config": "2.64.0"
|
|
82
92
|
},
|
|
83
93
|
"sideEffects": false,
|
|
84
94
|
"publishConfig": {
|