@modern-js/create-request 1.1.0 → 1.1.3-beta.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/js/modern/browser.js +28 -14
- package/dist/js/modern/handleRes.js +23 -0
- package/dist/js/modern/index.js +3 -1
- package/dist/js/modern/node.js +29 -7
- package/dist/js/modern/types.js +0 -0
- package/dist/js/node/browser.js +32 -15
- package/dist/js/node/handleRes.js +30 -0
- package/dist/js/node/index.js +3 -1
- package/dist/js/node/node.js +34 -8
- package/dist/js/node/types.js +0 -0
- package/dist/js/treeshaking/browser.js +28 -16
- package/dist/js/treeshaking/handleRes.js +23 -0
- package/dist/js/treeshaking/index.js +3 -1
- package/dist/js/treeshaking/node.js +28 -8
- package/dist/js/treeshaking/types.js +0 -0
- package/dist/types/browser.d.ts +2 -1
- package/dist/types/handleRes.d.ts +3 -0
- package/dist/types/index.d.ts +2 -15
- package/dist/types/node.d.ts +3 -1
- package/dist/types/types.d.ts +21 -0
- package/package.json +42 -13
- package/hook.d.ts +0 -1
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
import { compile, pathToRegexp } from 'path-to-regexp';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { handleRes } from "./handleRes";
|
|
3
|
+
let realRequest;
|
|
4
|
+
let realAllowedHeaders;
|
|
5
|
+
|
|
6
|
+
const originFetch = (...params) => fetch(...params) // eslint-disable-next-line promise/prefer-await-to-then
|
|
7
|
+
.then(handleRes);
|
|
8
|
+
|
|
9
|
+
export const configure = options => {
|
|
10
|
+
const {
|
|
11
|
+
request,
|
|
12
|
+
interceptor,
|
|
13
|
+
allowedHeaders
|
|
14
|
+
} = options;
|
|
15
|
+
realRequest = request || originFetch;
|
|
16
|
+
|
|
17
|
+
if (interceptor && !request) {
|
|
18
|
+
realRequest = interceptor(fetch);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (Array.isArray(allowedHeaders)) {
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
+
realAllowedHeaders = allowedHeaders;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export const createRequest = (path, method, port, fetch = originFetch) => {
|
|
4
27
|
const getFinalPath = compile(path, {
|
|
5
28
|
encode: encodeURIComponent
|
|
6
29
|
});
|
|
@@ -8,7 +31,7 @@ export const createRequest = (path, method, port, fetch = globalFetch) => {
|
|
|
8
31
|
pathToRegexp(path, keys);
|
|
9
32
|
|
|
10
33
|
const sender = async (...args) => {
|
|
11
|
-
const fetcher =
|
|
34
|
+
const fetcher = realRequest || fetch;
|
|
12
35
|
const payload = typeof args[args.length - 1] === 'object' ? args[args.length - 1] : {};
|
|
13
36
|
payload.params = payload.params || {};
|
|
14
37
|
keys.forEach((key, index) => {
|
|
@@ -46,22 +69,13 @@ export const createRequest = (path, method, port, fetch = globalFetch) => {
|
|
|
46
69
|
return fetcher(finalURL, {
|
|
47
70
|
method,
|
|
48
71
|
body,
|
|
49
|
-
headers
|
|
50
|
-
|
|
51
|
-
}).then(res => res.json());
|
|
72
|
+
headers
|
|
73
|
+
});
|
|
52
74
|
};
|
|
53
75
|
|
|
54
76
|
return sender;
|
|
55
77
|
};
|
|
56
78
|
|
|
57
|
-
const getFetcher = sender => {
|
|
58
|
-
if (sender.fetch) {
|
|
59
|
-
return sender.fetch;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return fetch;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
79
|
const qsStringify = input => {
|
|
66
80
|
const tupleList = [];
|
|
67
81
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const handleRes = res => {
|
|
2
|
+
const contentType = res.headers.get('content-type');
|
|
3
|
+
|
|
4
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json')) {
|
|
5
|
+
return res.json();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('text/html') || contentType !== null && contentType !== void 0 && contentType.includes('text/plain')) {
|
|
9
|
+
return res.text();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if ((contentType !== null && contentType !== void 0 && contentType.includes('application/x-www-form-urlencoded') || contentType !== null && contentType !== void 0 && contentType.includes('multipart/form-data')) && res instanceof Response) {
|
|
13
|
+
return res.formData();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('application/octet-stream')) {
|
|
17
|
+
return res.arrayBuffer();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return res.text();
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { handleRes };
|
package/dist/js/modern/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
// eslint-disable-next-line
|
|
1
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
2
|
+
|
|
3
|
+
/* eslint-disable filenames/match-exported */
|
|
2
4
|
import { isBrowser } from '@modern-js/utils';
|
|
3
5
|
import { createRequest as browser } from "./browser";
|
|
4
6
|
import { createRequest as node } from "./node";
|
package/dist/js/modern/node.js
CHANGED
|
@@ -2,8 +2,30 @@ import qs from 'querystring';
|
|
|
2
2
|
import nodeFetch from 'node-fetch';
|
|
3
3
|
import { compile, pathToRegexp } from 'path-to-regexp';
|
|
4
4
|
import { useHeaders } from '@modern-js/plugin-ssr/node';
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
import { handleRes } from "./handleRes";
|
|
6
|
+
let realRequest;
|
|
7
|
+
let realAllowedHeaders = [];
|
|
8
|
+
|
|
9
|
+
const originFetch = (...params) => nodeFetch(...params) // eslint-disable-next-line promise/prefer-await-to-then
|
|
10
|
+
.then(handleRes);
|
|
11
|
+
|
|
12
|
+
export const configure = options => {
|
|
13
|
+
const {
|
|
14
|
+
request,
|
|
15
|
+
interceptor,
|
|
16
|
+
allowedHeaders
|
|
17
|
+
} = options;
|
|
18
|
+
realRequest = request || originFetch;
|
|
19
|
+
|
|
20
|
+
if (interceptor && !request) {
|
|
21
|
+
realRequest = interceptor(nodeFetch);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (Array.isArray(allowedHeaders)) {
|
|
25
|
+
realAllowedHeaders = allowedHeaders;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
export const createRequest = (path, method, port, fetch = nodeFetch) => {
|
|
7
29
|
const getFinalPath = compile(path, {
|
|
8
30
|
encode: encodeURIComponent
|
|
9
31
|
});
|
|
@@ -23,7 +45,7 @@ export const createRequest = (path, method, port, fetch = nodeFetch, headerWhite
|
|
|
23
45
|
const headers = payload.headers || {};
|
|
24
46
|
let body; // BFF 内网请求需要携带 SSR 请求中的 cookie 和 traceId, 如果有压测头,则添加压测头
|
|
25
47
|
|
|
26
|
-
for (const key of
|
|
48
|
+
for (const key of realAllowedHeaders) {
|
|
27
49
|
headers[key] = webRequestHeaders[key];
|
|
28
50
|
}
|
|
29
51
|
|
|
@@ -48,13 +70,13 @@ export const createRequest = (path, method, port, fetch = nodeFetch, headerWhite
|
|
|
48
70
|
}
|
|
49
71
|
}
|
|
50
72
|
|
|
51
|
-
const url = `http://localhost:${port}${finalPath}`;
|
|
52
|
-
|
|
53
|
-
return
|
|
73
|
+
const url = `http://localhost:${port}${finalPath}`;
|
|
74
|
+
const fetcher = realRequest || fetch;
|
|
75
|
+
return fetcher(url, {
|
|
54
76
|
method,
|
|
55
77
|
body,
|
|
56
78
|
headers
|
|
57
|
-
})
|
|
79
|
+
});
|
|
58
80
|
};
|
|
59
81
|
|
|
60
82
|
return sender;
|
|
File without changes
|
package/dist/js/node/browser.js
CHANGED
|
@@ -3,13 +3,39 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.createRequest = void 0;
|
|
6
|
+
exports.createRequest = exports.configure = void 0;
|
|
7
7
|
|
|
8
8
|
var _pathToRegexp = require("path-to-regexp");
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
var _handleRes = require("./handleRes");
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
let realRequest;
|
|
13
|
+
let realAllowedHeaders;
|
|
14
|
+
|
|
15
|
+
const originFetch = (...params) => fetch(...params) // eslint-disable-next-line promise/prefer-await-to-then
|
|
16
|
+
.then(_handleRes.handleRes);
|
|
17
|
+
|
|
18
|
+
const configure = options => {
|
|
19
|
+
const {
|
|
20
|
+
request,
|
|
21
|
+
interceptor,
|
|
22
|
+
allowedHeaders
|
|
23
|
+
} = options;
|
|
24
|
+
realRequest = request || originFetch;
|
|
25
|
+
|
|
26
|
+
if (interceptor && !request) {
|
|
27
|
+
realRequest = interceptor(fetch);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (Array.isArray(allowedHeaders)) {
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
32
|
+
realAllowedHeaders = allowedHeaders;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.configure = configure;
|
|
37
|
+
|
|
38
|
+
const createRequest = (path, method, port, fetch = originFetch) => {
|
|
13
39
|
const getFinalPath = (0, _pathToRegexp.compile)(path, {
|
|
14
40
|
encode: encodeURIComponent
|
|
15
41
|
});
|
|
@@ -17,7 +43,7 @@ const createRequest = (path, method, port, fetch = globalFetch) => {
|
|
|
17
43
|
(0, _pathToRegexp.pathToRegexp)(path, keys);
|
|
18
44
|
|
|
19
45
|
const sender = async (...args) => {
|
|
20
|
-
const fetcher =
|
|
46
|
+
const fetcher = realRequest || fetch;
|
|
21
47
|
const payload = typeof args[args.length - 1] === 'object' ? args[args.length - 1] : {};
|
|
22
48
|
payload.params = payload.params || {};
|
|
23
49
|
keys.forEach((key, index) => {
|
|
@@ -55,9 +81,8 @@ const createRequest = (path, method, port, fetch = globalFetch) => {
|
|
|
55
81
|
return fetcher(finalURL, {
|
|
56
82
|
method,
|
|
57
83
|
body,
|
|
58
|
-
headers
|
|
59
|
-
|
|
60
|
-
}).then(res => res.json());
|
|
84
|
+
headers
|
|
85
|
+
});
|
|
61
86
|
};
|
|
62
87
|
|
|
63
88
|
return sender;
|
|
@@ -65,14 +90,6 @@ const createRequest = (path, method, port, fetch = globalFetch) => {
|
|
|
65
90
|
|
|
66
91
|
exports.createRequest = createRequest;
|
|
67
92
|
|
|
68
|
-
const getFetcher = sender => {
|
|
69
|
-
if (sender.fetch) {
|
|
70
|
-
return sender.fetch;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return fetch;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
93
|
const qsStringify = input => {
|
|
77
94
|
const tupleList = [];
|
|
78
95
|
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.handleRes = void 0;
|
|
7
|
+
|
|
8
|
+
const handleRes = res => {
|
|
9
|
+
const contentType = res.headers.get('content-type');
|
|
10
|
+
|
|
11
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json')) {
|
|
12
|
+
return res.json();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('text/html') || contentType !== null && contentType !== void 0 && contentType.includes('text/plain')) {
|
|
16
|
+
return res.text();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if ((contentType !== null && contentType !== void 0 && contentType.includes('application/x-www-form-urlencoded') || contentType !== null && contentType !== void 0 && contentType.includes('multipart/form-data')) && res instanceof Response) {
|
|
20
|
+
return res.formData();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('application/octet-stream')) {
|
|
24
|
+
return res.arrayBuffer();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return res.text();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
exports.handleRes = handleRes;
|
package/dist/js/node/index.js
CHANGED
|
@@ -11,7 +11,9 @@ var _browser = require("./browser");
|
|
|
11
11
|
|
|
12
12
|
var _node = require("./node");
|
|
13
13
|
|
|
14
|
-
// eslint-disable-next-line
|
|
14
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
15
|
+
|
|
16
|
+
/* eslint-disable filenames/match-exported */
|
|
15
17
|
const createRequest = (...args) => (0, _utils.isBrowser)() ? (0, _browser.createRequest)(...args) : (0, _node.createRequest)(...args);
|
|
16
18
|
|
|
17
19
|
var _default = createRequest;
|
package/dist/js/node/node.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.createRequest = void 0;
|
|
6
|
+
exports.createRequest = exports.configure = void 0;
|
|
7
7
|
|
|
8
8
|
var _querystring = _interopRequireDefault(require("querystring"));
|
|
9
9
|
|
|
@@ -13,10 +13,36 @@ var _pathToRegexp = require("path-to-regexp");
|
|
|
13
13
|
|
|
14
14
|
var _node = require("@modern-js/plugin-ssr/node");
|
|
15
15
|
|
|
16
|
+
var _handleRes = require("./handleRes");
|
|
17
|
+
|
|
16
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
let realRequest;
|
|
21
|
+
let realAllowedHeaders = [];
|
|
22
|
+
|
|
23
|
+
const originFetch = (...params) => (0, _nodeFetch.default)(...params) // eslint-disable-next-line promise/prefer-await-to-then
|
|
24
|
+
.then(_handleRes.handleRes);
|
|
25
|
+
|
|
26
|
+
const configure = options => {
|
|
27
|
+
const {
|
|
28
|
+
request,
|
|
29
|
+
interceptor,
|
|
30
|
+
allowedHeaders
|
|
31
|
+
} = options;
|
|
32
|
+
realRequest = request || originFetch;
|
|
33
|
+
|
|
34
|
+
if (interceptor && !request) {
|
|
35
|
+
realRequest = interceptor(_nodeFetch.default);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (Array.isArray(allowedHeaders)) {
|
|
39
|
+
realAllowedHeaders = allowedHeaders;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
exports.configure = configure;
|
|
44
|
+
|
|
45
|
+
const createRequest = (path, method, port, fetch = _nodeFetch.default) => {
|
|
20
46
|
const getFinalPath = (0, _pathToRegexp.compile)(path, {
|
|
21
47
|
encode: encodeURIComponent
|
|
22
48
|
});
|
|
@@ -36,7 +62,7 @@ const createRequest = (path, method, port, fetch = _nodeFetch.default, headerWhi
|
|
|
36
62
|
const headers = payload.headers || {};
|
|
37
63
|
let body; // BFF 内网请求需要携带 SSR 请求中的 cookie 和 traceId, 如果有压测头,则添加压测头
|
|
38
64
|
|
|
39
|
-
for (const key of
|
|
65
|
+
for (const key of realAllowedHeaders) {
|
|
40
66
|
headers[key] = webRequestHeaders[key];
|
|
41
67
|
}
|
|
42
68
|
|
|
@@ -61,13 +87,13 @@ const createRequest = (path, method, port, fetch = _nodeFetch.default, headerWhi
|
|
|
61
87
|
}
|
|
62
88
|
}
|
|
63
89
|
|
|
64
|
-
const url = `http://localhost:${port}${finalPath}`;
|
|
65
|
-
|
|
66
|
-
return
|
|
90
|
+
const url = `http://localhost:${port}${finalPath}`;
|
|
91
|
+
const fetcher = realRequest || fetch;
|
|
92
|
+
return fetcher(url, {
|
|
67
93
|
method,
|
|
68
94
|
body,
|
|
69
95
|
headers
|
|
70
|
-
})
|
|
96
|
+
});
|
|
71
97
|
};
|
|
72
98
|
|
|
73
99
|
return sender;
|
|
File without changes
|
|
@@ -1,15 +1,38 @@
|
|
|
1
1
|
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
2
2
|
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof";
|
|
3
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
4
|
|
|
5
5
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
6
6
|
|
|
7
7
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
8
8
|
|
|
9
9
|
import { compile, pathToRegexp } from 'path-to-regexp';
|
|
10
|
-
|
|
10
|
+
import { handleRes } from "./handleRes";
|
|
11
|
+
var realRequest;
|
|
12
|
+
var realAllowedHeaders;
|
|
13
|
+
|
|
14
|
+
var originFetch = function originFetch() {
|
|
15
|
+
return fetch.apply(void 0, arguments) // eslint-disable-next-line promise/prefer-await-to-then
|
|
16
|
+
.then(handleRes);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export var configure = function configure(options) {
|
|
20
|
+
var request = options.request,
|
|
21
|
+
interceptor = options.interceptor,
|
|
22
|
+
allowedHeaders = options.allowedHeaders;
|
|
23
|
+
realRequest = request || originFetch;
|
|
24
|
+
|
|
25
|
+
if (interceptor && !request) {
|
|
26
|
+
realRequest = interceptor(fetch);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (Array.isArray(allowedHeaders)) {
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
31
|
+
realAllowedHeaders = allowedHeaders;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
11
34
|
export var createRequest = function createRequest(path, method, port) {
|
|
12
|
-
var fetch = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] :
|
|
35
|
+
var fetch = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : originFetch;
|
|
13
36
|
var getFinalPath = compile(path, {
|
|
14
37
|
encode: encodeURIComponent
|
|
15
38
|
});
|
|
@@ -37,7 +60,7 @@ export var createRequest = function createRequest(path, method, port) {
|
|
|
37
60
|
args[_key] = _args[_key];
|
|
38
61
|
}
|
|
39
62
|
|
|
40
|
-
fetcher =
|
|
63
|
+
fetcher = realRequest || fetch;
|
|
41
64
|
payload = _typeof(args[args.length - 1]) === 'object' ? args[args.length - 1] : {};
|
|
42
65
|
payload.params = payload.params || {};
|
|
43
66
|
keys.forEach(function (key, index) {
|
|
@@ -75,10 +98,7 @@ export var createRequest = function createRequest(path, method, port) {
|
|
|
75
98
|
return _context.abrupt("return", fetcher(finalURL, {
|
|
76
99
|
method: method,
|
|
77
100
|
body: body,
|
|
78
|
-
headers: headers
|
|
79
|
-
|
|
80
|
-
}).then(function (res) {
|
|
81
|
-
return res.json();
|
|
101
|
+
headers: headers
|
|
82
102
|
}));
|
|
83
103
|
|
|
84
104
|
case 11:
|
|
@@ -97,14 +117,6 @@ export var createRequest = function createRequest(path, method, port) {
|
|
|
97
117
|
return sender;
|
|
98
118
|
};
|
|
99
119
|
|
|
100
|
-
var getFetcher = function getFetcher(sender) {
|
|
101
|
-
if (sender.fetch) {
|
|
102
|
-
return sender.fetch;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return fetch;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
120
|
var qsStringify = function qsStringify(input) {
|
|
109
121
|
var tupleList = [];
|
|
110
122
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
var handleRes = function handleRes(res) {
|
|
2
|
+
var contentType = res.headers.get('content-type');
|
|
3
|
+
|
|
4
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json')) {
|
|
5
|
+
return res.json();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('text/html') || contentType !== null && contentType !== void 0 && contentType.includes('text/plain')) {
|
|
9
|
+
return res.text();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if ((contentType !== null && contentType !== void 0 && contentType.includes('application/x-www-form-urlencoded') || contentType !== null && contentType !== void 0 && contentType.includes('multipart/form-data')) && res instanceof Response) {
|
|
13
|
+
return res.formData();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (contentType !== null && contentType !== void 0 && contentType.includes('application/octet-stream')) {
|
|
17
|
+
return res.arrayBuffer();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return res.text();
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { handleRes };
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
// eslint-disable-next-line
|
|
1
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
2
|
+
|
|
3
|
+
/* eslint-disable filenames/match-exported */
|
|
2
4
|
import { isBrowser } from '@modern-js/utils';
|
|
3
5
|
import { createRequest as browser } from "./browser";
|
|
4
6
|
import { createRequest as node } from "./node";
|
|
@@ -4,15 +4,37 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
|
|
|
4
4
|
|
|
5
5
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
6
6
|
|
|
7
|
-
function _typeof(obj) { "@babel/helpers - typeof";
|
|
7
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
8
8
|
|
|
9
9
|
import qs from 'querystring';
|
|
10
10
|
import nodeFetch from 'node-fetch';
|
|
11
11
|
import { compile, pathToRegexp } from 'path-to-regexp';
|
|
12
12
|
import { useHeaders } from '@modern-js/plugin-ssr/node';
|
|
13
|
+
import { handleRes } from "./handleRes";
|
|
14
|
+
var realRequest;
|
|
15
|
+
var realAllowedHeaders = [];
|
|
16
|
+
|
|
17
|
+
var originFetch = function originFetch() {
|
|
18
|
+
return nodeFetch.apply(void 0, arguments) // eslint-disable-next-line promise/prefer-await-to-then
|
|
19
|
+
.then(handleRes);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export var configure = function configure(options) {
|
|
23
|
+
var request = options.request,
|
|
24
|
+
interceptor = options.interceptor,
|
|
25
|
+
allowedHeaders = options.allowedHeaders;
|
|
26
|
+
realRequest = request || originFetch;
|
|
27
|
+
|
|
28
|
+
if (interceptor && !request) {
|
|
29
|
+
realRequest = interceptor(nodeFetch);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(allowedHeaders)) {
|
|
33
|
+
realAllowedHeaders = allowedHeaders;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
13
36
|
export var createRequest = function createRequest(path, method, port) {
|
|
14
37
|
var fetch = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : nodeFetch;
|
|
15
|
-
var headerWhiteList = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
|
|
16
38
|
var getFinalPath = compile(path, {
|
|
17
39
|
encode: encodeURIComponent
|
|
18
40
|
});
|
|
@@ -36,7 +58,7 @@ export var createRequest = function createRequest(path, method, port) {
|
|
|
36
58
|
var headers = payload.headers || {};
|
|
37
59
|
var body; // BFF 内网请求需要携带 SSR 请求中的 cookie 和 traceId, 如果有压测头,则添加压测头
|
|
38
60
|
|
|
39
|
-
var _iterator = _createForOfIteratorHelper(
|
|
61
|
+
var _iterator = _createForOfIteratorHelper(realAllowedHeaders),
|
|
40
62
|
_step;
|
|
41
63
|
|
|
42
64
|
try {
|
|
@@ -71,14 +93,12 @@ export var createRequest = function createRequest(path, method, port) {
|
|
|
71
93
|
}
|
|
72
94
|
}
|
|
73
95
|
|
|
74
|
-
var url = "http://localhost:".concat(port).concat(finalPath);
|
|
75
|
-
|
|
76
|
-
return
|
|
96
|
+
var url = "http://localhost:".concat(port).concat(finalPath);
|
|
97
|
+
var fetcher = realRequest || fetch;
|
|
98
|
+
return fetcher(url, {
|
|
77
99
|
method: method,
|
|
78
100
|
body: body,
|
|
79
101
|
headers: headers
|
|
80
|
-
}).then(function (res) {
|
|
81
|
-
return res.json();
|
|
82
102
|
});
|
|
83
103
|
};
|
|
84
104
|
|
|
File without changes
|
package/dist/types/browser.d.ts
CHANGED
package/dist/types/index.d.ts
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
params?: Record<string, any>;
|
|
3
|
-
query?: Record<string, any>;
|
|
4
|
-
body?: string;
|
|
5
|
-
formUrlencoded?: never;
|
|
6
|
-
formData?: FormData;
|
|
7
|
-
data?: Record<string, any>;
|
|
8
|
-
headers?: Record<string, any>;
|
|
9
|
-
cookies?: Record<string, any>;
|
|
10
|
-
};
|
|
11
|
-
export declare type Fetcher = typeof fetch;
|
|
12
|
-
export declare type Sender = ((...args: any[]) => Promise<any>) & {
|
|
13
|
-
fetch?: Fetcher;
|
|
14
|
-
};
|
|
15
|
-
export declare type RequestCreator = (path: string, method: string, port: number, fetch?: Fetcher, headerWhiteList?: string[]) => Sender;
|
|
1
|
+
import { RequestCreator, IOptions } from './types';
|
|
16
2
|
declare const createRequest: RequestCreator;
|
|
3
|
+
export declare const configure: (options: IOptions) => void;
|
|
17
4
|
export default createRequest;
|
package/dist/types/node.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import nodeFetch from 'node-fetch';
|
|
2
|
+
import type { RequestCreator, IOptions } from './types';
|
|
3
|
+
export declare const configure: (options: IOptions<typeof nodeFetch>) => void;
|
|
2
4
|
export declare const createRequest: RequestCreator;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type nodeFetch from 'node-fetch';
|
|
2
|
+
export declare type BFFRequestPayload = {
|
|
3
|
+
params?: Record<string, any>;
|
|
4
|
+
query?: Record<string, any>;
|
|
5
|
+
body?: string;
|
|
6
|
+
formUrlencoded?: never;
|
|
7
|
+
formData?: FormData;
|
|
8
|
+
data?: Record<string, any>;
|
|
9
|
+
headers?: Record<string, any>;
|
|
10
|
+
cookies?: Record<string, any>;
|
|
11
|
+
};
|
|
12
|
+
export declare type Fetch = typeof nodeFetch | typeof fetch;
|
|
13
|
+
export declare type Sender = ((...args: any[]) => Promise<any>) & {
|
|
14
|
+
fetch?: Fetch;
|
|
15
|
+
};
|
|
16
|
+
export declare type RequestCreator = (path: string, method: string, port: number, fetch?: Fetch, headerWhiteList?: string[]) => Sender;
|
|
17
|
+
export declare type IOptions<F = typeof fetch> = {
|
|
18
|
+
request?: F;
|
|
19
|
+
interceptor?: (request: F) => F;
|
|
20
|
+
allowedHeaders?: string[];
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.1.0",
|
|
14
|
+
"version": "1.1.3-beta.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -25,35 +25,63 @@
|
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
27
27
|
"node": {
|
|
28
|
+
"jsnext:source": "./src/node.ts",
|
|
28
29
|
"import": "./dist/js/modern/node.js",
|
|
29
30
|
"require": "./dist/js/node/node.js"
|
|
30
31
|
},
|
|
31
32
|
"default": "./dist/js/treeshaking/browser.js"
|
|
32
33
|
},
|
|
33
|
-
"./client":
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
"./client": {
|
|
35
|
+
"jsnext:source": "./src/browser.ts",
|
|
36
|
+
"default": "./dist/js/treeshaking/browser.js"
|
|
37
|
+
},
|
|
38
|
+
"./modern": {
|
|
39
|
+
"jsnext:source": "./src/browser.ts",
|
|
40
|
+
"default": "./dist/js/modern/browser.js"
|
|
41
|
+
},
|
|
42
|
+
"./server": {
|
|
43
|
+
"jsnext:source": "./src/node.ts",
|
|
44
|
+
"default": "./dist/js/node/node.js"
|
|
45
|
+
},
|
|
46
|
+
"./hook": {
|
|
47
|
+
"jsnext:source": "./src/hook.ts",
|
|
48
|
+
"default": "./dist/js/node/hook.js"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"typesVersions": {
|
|
52
|
+
"*": {
|
|
53
|
+
".": [
|
|
54
|
+
"./dist/types/index.d.ts"
|
|
55
|
+
],
|
|
56
|
+
"browser": [
|
|
57
|
+
"./dist/types/browser.d.ts"
|
|
58
|
+
],
|
|
59
|
+
"node": [
|
|
60
|
+
"./dist/types/node.d.ts"
|
|
61
|
+
]
|
|
62
|
+
}
|
|
37
63
|
},
|
|
38
64
|
"dependencies": {
|
|
39
65
|
"@babel/runtime": "^7",
|
|
40
|
-
"@modern-js/utils": "^1.1.
|
|
66
|
+
"@modern-js/utils": "^1.1.6",
|
|
41
67
|
"node-fetch": "^2.6.1",
|
|
42
68
|
"path-to-regexp": "^6.2.0"
|
|
43
69
|
},
|
|
44
70
|
"devDependencies": {
|
|
45
|
-
"@
|
|
71
|
+
"@modern-js/module-tools": "^1.1.5",
|
|
72
|
+
"@modern-js/plugin-ssr": "^1.1.3",
|
|
73
|
+
"@modern-js/plugin-testing": "^1.1.1",
|
|
74
|
+
"@types/jest": "^27.0.3",
|
|
46
75
|
"@types/node": "^14",
|
|
47
76
|
"@types/node-fetch": "^2.5.12",
|
|
48
77
|
"@types/react": "^17",
|
|
49
78
|
"@types/react-dom": "^17",
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"@modern-js/plugin-ssr": "^1.1.0"
|
|
79
|
+
"isomorphic-fetch": "^3.0.0",
|
|
80
|
+
"nock": "^13.2.1",
|
|
81
|
+
"typescript": "^4"
|
|
54
82
|
},
|
|
55
83
|
"peerDependencies": {
|
|
56
|
-
"@modern-js/plugin-ssr": "^1.1.
|
|
84
|
+
"@modern-js/plugin-ssr": "^1.1.3"
|
|
57
85
|
},
|
|
58
86
|
"sideEffects": false,
|
|
59
87
|
"publishConfig": {
|
|
@@ -64,5 +92,6 @@
|
|
|
64
92
|
"new": "modern new",
|
|
65
93
|
"build": "modern build",
|
|
66
94
|
"test": "modern test --passWithNoTests"
|
|
67
|
-
}
|
|
95
|
+
},
|
|
96
|
+
"readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
|
|
68
97
|
}
|
package/hook.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './dist/types/hook';
|