@modern-js/create-request 2.0.0-beta.0 → 2.0.0-beta.2

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.
@@ -3,9 +3,7 @@ import qs from 'query-string';
3
3
  import { handleRes } from "./handleRes";
4
4
  let realRequest;
5
5
  let realAllowedHeaders;
6
-
7
6
  const originFetch = (...params) => fetch(...params).then(handleRes);
8
-
9
7
  export const configure = options => {
10
8
  const {
11
9
  request,
@@ -13,17 +11,16 @@ export const configure = options => {
13
11
  allowedHeaders
14
12
  } = options;
15
13
  realRequest = request || originFetch;
16
-
17
14
  if (interceptor && !request) {
18
15
  realRequest = interceptor(fetch);
19
16
  }
20
-
21
17
  if (Array.isArray(allowedHeaders)) {
22
18
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
19
  realAllowedHeaders = allowedHeaders;
24
20
  }
25
21
  };
26
- export const createRequest = (path, method, port, // 后续可能要修改,暂时先保留
22
+ export const createRequest = (path, method, port,
23
+ // 后续可能要修改,暂时先保留
27
24
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
28
25
  fetch = originFetch) => {
29
26
  const getFinalPath = compile(path, {
@@ -31,13 +28,12 @@ fetch = originFetch) => {
31
28
  });
32
29
  const keys = [];
33
30
  pathToRegexp(path, keys);
34
-
35
31
  const sender = async (...args) => {
36
32
  const fetcher = realRequest || originFetch;
37
33
  const payload = typeof args[args.length - 1] === 'object' ? args[args.length - 1] : {};
38
34
  payload.params = payload.params || {};
39
- const requestParams = args[0]; // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
40
-
35
+ const requestParams = args[0];
36
+ // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
41
37
  if (typeof requestParams === 'object' && requestParams.params) {
42
38
  const {
43
39
  params
@@ -50,27 +46,26 @@ fetch = originFetch) => {
50
46
  payload.params[key.name] = args[index];
51
47
  });
52
48
  }
53
-
54
49
  const finalPath = getFinalPath(payload.params);
55
50
  const finalURL = payload.query ? `${finalPath}?${qs.stringify(payload.query)}` : finalPath;
56
51
  const headers = payload.headers || {};
57
52
  let body = payload.data && typeof payload.data === 'object' ? JSON.stringify(payload.data) : payload.body;
58
-
59
53
  if (payload.data) {
60
54
  headers['Content-Type'] = 'application/json';
61
55
  body = typeof payload.data === 'object' ? JSON.stringify(payload.data) : payload.body;
62
56
  } else if (payload.body) {
63
- headers['Content-Type'] = 'text/plain'; // eslint-disable-next-line prefer-destructuring
64
-
57
+ headers['Content-Type'] = 'text/plain';
58
+ // eslint-disable-next-line prefer-destructuring
65
59
  body = payload.body;
66
60
  } else if (payload.formData) {
67
- body = payload.formData; // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
61
+ body = payload.formData;
62
+ // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
68
63
  // need multipart boundary aotu attached by browser when multipart is true
69
64
  // headers['Content-Type'] = 'multipart/form-data';
70
65
  } else if (payload.formUrlencoded) {
71
66
  headers['Content-Type'] = 'application/x-www-form-urlencoded';
72
-
73
- if (typeof payload.formUrlencoded === 'object' && // eslint-disable-next-line @typescript-eslint/ban-ts-comment
67
+ if (typeof payload.formUrlencoded === 'object' &&
68
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
74
69
  // @ts-expect-error
75
70
  // eslint-disable-next-line node/prefer-global/url-search-params,node/no-unsupported-features/node-builtins
76
71
  !(payload.formUrlencoded instanceof URLSearchParams)) {
@@ -79,13 +74,11 @@ fetch = originFetch) => {
79
74
  body = payload.formUrlencoded;
80
75
  }
81
76
  }
82
-
83
77
  return fetcher(finalURL, {
84
78
  method,
85
79
  body,
86
80
  headers
87
81
  });
88
82
  };
89
-
90
83
  return sender;
91
84
  };
@@ -1,29 +1,24 @@
1
1
  const handleRes = async res => {
2
- const contentType = res.headers.get('content-type'); // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
2
+ const contentType = res.headers.get('content-type');
3
3
 
4
+ // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
4
5
  if (!res.ok) {
5
6
  const data = await res.json();
6
7
  res.data = data;
7
8
  throw res;
8
9
  }
9
-
10
10
  if (contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json')) {
11
11
  return res.json();
12
12
  }
13
-
14
13
  if (contentType !== null && contentType !== void 0 && contentType.includes('text/html') || contentType !== null && contentType !== void 0 && contentType.includes('text/plain')) {
15
14
  return res.text();
16
15
  }
17
-
18
16
  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) {
19
17
  return res.formData();
20
18
  }
21
-
22
19
  if (contentType !== null && contentType !== void 0 && contentType.includes('application/octet-stream')) {
23
20
  return res.arrayBuffer();
24
21
  }
25
-
26
22
  return res.text();
27
23
  };
28
-
29
24
  export { handleRes };
@@ -1,7 +1,5 @@
1
1
  import { isBrowser } from '@modern-js/utils';
2
2
  import { createRequest as browser } from "./browser";
3
3
  import { createRequest as node } from "./node";
4
-
5
4
  const createRequest = (...args) => isBrowser() ? browser(...args) : node(...args);
6
-
7
5
  export default createRequest;
@@ -5,9 +5,7 @@ import qs from 'query-string';
5
5
  import { handleRes } from "./handleRes";
6
6
  let realRequest;
7
7
  let realAllowedHeaders = [];
8
-
9
8
  const originFetch = (...params) => nodeFetch(...params).then(handleRes);
10
-
11
9
  export const configure = options => {
12
10
  const {
13
11
  request,
@@ -15,16 +13,15 @@ export const configure = options => {
15
13
  allowedHeaders
16
14
  } = options;
17
15
  realRequest = request || originFetch;
18
-
19
16
  if (interceptor && !request) {
20
17
  realRequest = interceptor(nodeFetch);
21
18
  }
22
-
23
19
  if (Array.isArray(allowedHeaders)) {
24
20
  realAllowedHeaders = allowedHeaders;
25
21
  }
26
22
  };
27
- export const createRequest = (path, method, port, // 后续可能要修改,暂时先保留
23
+ export const createRequest = (path, method, port,
24
+ // 后续可能要修改,暂时先保留
28
25
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
29
26
  fetch = nodeFetch) => {
30
27
  const getFinalPath = compile(path, {
@@ -32,13 +29,12 @@ fetch = nodeFetch) => {
32
29
  });
33
30
  const keys = [];
34
31
  pathToRegexp(path, keys);
35
-
36
32
  const sender = (...args) => {
37
33
  const webRequestHeaders = useHeaders();
38
34
  const payload = typeof args[args.length - 1] === 'object' ? args[args.length - 1] : {};
39
35
  payload.params = payload.params || {};
40
- const requestParams = args[0]; // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
41
-
36
+ const requestParams = args[0];
37
+ // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
42
38
  if (typeof requestParams === 'object' && requestParams.params) {
43
39
  const {
44
40
  params
@@ -51,39 +47,35 @@ fetch = nodeFetch) => {
51
47
  payload.params[key.name] = args[index];
52
48
  });
53
49
  }
54
-
55
50
  const plainPath = getFinalPath(payload.params);
56
51
  const finalPath = payload.query ? `${plainPath}?${qs.stringify(payload.query)}` : plainPath;
57
52
  const headers = payload.headers || {};
58
53
  let body;
59
-
60
54
  for (const key of realAllowedHeaders) {
61
55
  if (typeof webRequestHeaders[key] !== 'undefined') {
62
56
  headers[key] = webRequestHeaders[key];
63
57
  }
64
58
  }
65
-
66
59
  if (payload.data) {
67
60
  headers['Content-Type'] = 'application/json';
68
61
  body = typeof payload.data === 'object' ? JSON.stringify(payload.data) : payload.body;
69
62
  } else if (payload.body) {
70
- headers['Content-Type'] = 'text/plain'; // eslint-disable-next-line prefer-destructuring
71
-
63
+ headers['Content-Type'] = 'text/plain';
64
+ // eslint-disable-next-line prefer-destructuring
72
65
  body = payload.body;
73
66
  } else if (payload.formData) {
74
- body = payload.formData; // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
67
+ body = payload.formData;
68
+ // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
75
69
  // need multipart boundary auto attached by node-fetch when multipart is true
76
70
  // headers['Content-Type'] = 'multipart/form-data';
77
71
  } else if (payload.formUrlencoded) {
78
72
  headers['Content-Type'] = 'application/x-www-form-urlencoded';
79
-
80
73
  if (typeof payload.formUrlencoded === 'object') {
81
74
  body = qs.stringify(payload.formUrlencoded);
82
75
  } else {
83
76
  body = payload.formUrlencoded;
84
77
  }
85
78
  }
86
-
87
79
  const url = `http://localhost:${port}${finalPath}`;
88
80
  const fetcher = realRequest || originFetch;
89
81
  return fetcher(url, {
@@ -92,6 +84,5 @@ fetch = nodeFetch) => {
92
84
  headers
93
85
  });
94
86
  };
95
-
96
87
  return sender;
97
88
  };
@@ -4,20 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.createRequest = exports.configure = void 0;
7
-
8
7
  var _pathToRegexp = require("path-to-regexp");
9
-
10
8
  var _queryString = _interopRequireDefault(require("query-string"));
11
-
12
9
  var _handleRes = require("./handleRes");
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  let realRequest;
17
12
  let realAllowedHeaders;
18
-
19
13
  const originFetch = (...params) => fetch(...params).then(_handleRes.handleRes);
20
-
21
14
  const configure = options => {
22
15
  const {
23
16
  request,
@@ -25,20 +18,17 @@ const configure = options => {
25
18
  allowedHeaders
26
19
  } = options;
27
20
  realRequest = request || originFetch;
28
-
29
21
  if (interceptor && !request) {
30
22
  realRequest = interceptor(fetch);
31
23
  }
32
-
33
24
  if (Array.isArray(allowedHeaders)) {
34
25
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
35
26
  realAllowedHeaders = allowedHeaders;
36
27
  }
37
28
  };
38
-
39
29
  exports.configure = configure;
40
-
41
- const createRequest = (path, method, port, // 后续可能要修改,暂时先保留
30
+ const createRequest = (path, method, port,
31
+ // 后续可能要修改,暂时先保留
42
32
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
33
  fetch = originFetch) => {
44
34
  const getFinalPath = (0, _pathToRegexp.compile)(path, {
@@ -46,13 +36,12 @@ fetch = originFetch) => {
46
36
  });
47
37
  const keys = [];
48
38
  (0, _pathToRegexp.pathToRegexp)(path, keys);
49
-
50
39
  const sender = async (...args) => {
51
40
  const fetcher = realRequest || originFetch;
52
41
  const payload = typeof args[args.length - 1] === 'object' ? args[args.length - 1] : {};
53
42
  payload.params = payload.params || {};
54
- const requestParams = args[0]; // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
55
-
43
+ const requestParams = args[0];
44
+ // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
56
45
  if (typeof requestParams === 'object' && requestParams.params) {
57
46
  const {
58
47
  params
@@ -65,27 +54,26 @@ fetch = originFetch) => {
65
54
  payload.params[key.name] = args[index];
66
55
  });
67
56
  }
68
-
69
57
  const finalPath = getFinalPath(payload.params);
70
58
  const finalURL = payload.query ? `${finalPath}?${_queryString.default.stringify(payload.query)}` : finalPath;
71
59
  const headers = payload.headers || {};
72
60
  let body = payload.data && typeof payload.data === 'object' ? JSON.stringify(payload.data) : payload.body;
73
-
74
61
  if (payload.data) {
75
62
  headers['Content-Type'] = 'application/json';
76
63
  body = typeof payload.data === 'object' ? JSON.stringify(payload.data) : payload.body;
77
64
  } else if (payload.body) {
78
- headers['Content-Type'] = 'text/plain'; // eslint-disable-next-line prefer-destructuring
79
-
65
+ headers['Content-Type'] = 'text/plain';
66
+ // eslint-disable-next-line prefer-destructuring
80
67
  body = payload.body;
81
68
  } else if (payload.formData) {
82
- body = payload.formData; // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
69
+ body = payload.formData;
70
+ // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
83
71
  // need multipart boundary aotu attached by browser when multipart is true
84
72
  // headers['Content-Type'] = 'multipart/form-data';
85
73
  } else if (payload.formUrlencoded) {
86
74
  headers['Content-Type'] = 'application/x-www-form-urlencoded';
87
-
88
- if (typeof payload.formUrlencoded === 'object' && // eslint-disable-next-line @typescript-eslint/ban-ts-comment
75
+ if (typeof payload.formUrlencoded === 'object' &&
76
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
89
77
  // @ts-expect-error
90
78
  // eslint-disable-next-line node/prefer-global/url-search-params,node/no-unsupported-features/node-builtins
91
79
  !(payload.formUrlencoded instanceof URLSearchParams)) {
@@ -94,15 +82,12 @@ fetch = originFetch) => {
94
82
  body = payload.formUrlencoded;
95
83
  }
96
84
  }
97
-
98
85
  return fetcher(finalURL, {
99
86
  method,
100
87
  body,
101
88
  headers
102
89
  });
103
90
  };
104
-
105
91
  return sender;
106
92
  };
107
-
108
93
  exports.createRequest = createRequest;
@@ -4,33 +4,27 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.handleRes = void 0;
7
-
8
7
  const handleRes = async res => {
9
- const contentType = res.headers.get('content-type'); // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
8
+ const contentType = res.headers.get('content-type');
10
9
 
10
+ // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
11
11
  if (!res.ok) {
12
12
  const data = await res.json();
13
13
  res.data = data;
14
14
  throw res;
15
15
  }
16
-
17
16
  if (contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json')) {
18
17
  return res.json();
19
18
  }
20
-
21
19
  if (contentType !== null && contentType !== void 0 && contentType.includes('text/html') || contentType !== null && contentType !== void 0 && contentType.includes('text/plain')) {
22
20
  return res.text();
23
21
  }
24
-
25
22
  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) {
26
23
  return res.formData();
27
24
  }
28
-
29
25
  if (contentType !== null && contentType !== void 0 && contentType.includes('application/octet-stream')) {
30
26
  return res.arrayBuffer();
31
27
  }
32
-
33
28
  return res.text();
34
29
  };
35
-
36
30
  exports.handleRes = handleRes;
@@ -4,14 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _utils = require("@modern-js/utils");
9
-
10
8
  var _browser = require("./browser");
11
-
12
9
  var _node = require("./node");
13
-
14
10
  const createRequest = (...args) => (0, _utils.isBrowser)() ? (0, _browser.createRequest)(...args) : (0, _node.createRequest)(...args);
15
-
16
11
  var _default = createRequest;
17
12
  exports.default = _default;
@@ -4,24 +4,15 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.createRequest = exports.configure = void 0;
7
-
8
7
  var _nodeFetch = _interopRequireDefault(require("node-fetch"));
9
-
10
8
  var _pathToRegexp = require("path-to-regexp");
11
-
12
9
  var _ssr = require("@modern-js/utils/ssr");
13
-
14
10
  var _queryString = _interopRequireDefault(require("query-string"));
15
-
16
11
  var _handleRes = require("./handleRes");
17
-
18
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
13
  let realRequest;
21
14
  let realAllowedHeaders = [];
22
-
23
15
  const originFetch = (...params) => (0, _nodeFetch.default)(...params).then(_handleRes.handleRes);
24
-
25
16
  const configure = options => {
26
17
  const {
27
18
  request,
@@ -29,19 +20,16 @@ const configure = options => {
29
20
  allowedHeaders
30
21
  } = options;
31
22
  realRequest = request || originFetch;
32
-
33
23
  if (interceptor && !request) {
34
24
  realRequest = interceptor(_nodeFetch.default);
35
25
  }
36
-
37
26
  if (Array.isArray(allowedHeaders)) {
38
27
  realAllowedHeaders = allowedHeaders;
39
28
  }
40
29
  };
41
-
42
30
  exports.configure = configure;
43
-
44
- const createRequest = (path, method, port, // 后续可能要修改,暂时先保留
31
+ const createRequest = (path, method, port,
32
+ // 后续可能要修改,暂时先保留
45
33
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
46
34
  fetch = _nodeFetch.default) => {
47
35
  const getFinalPath = (0, _pathToRegexp.compile)(path, {
@@ -49,13 +37,12 @@ fetch = _nodeFetch.default) => {
49
37
  });
50
38
  const keys = [];
51
39
  (0, _pathToRegexp.pathToRegexp)(path, keys);
52
-
53
40
  const sender = (...args) => {
54
41
  const webRequestHeaders = (0, _ssr.useHeaders)();
55
42
  const payload = typeof args[args.length - 1] === 'object' ? args[args.length - 1] : {};
56
43
  payload.params = payload.params || {};
57
- const requestParams = args[0]; // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
58
-
44
+ const requestParams = args[0];
45
+ // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
59
46
  if (typeof requestParams === 'object' && requestParams.params) {
60
47
  const {
61
48
  params
@@ -68,39 +55,35 @@ fetch = _nodeFetch.default) => {
68
55
  payload.params[key.name] = args[index];
69
56
  });
70
57
  }
71
-
72
58
  const plainPath = getFinalPath(payload.params);
73
59
  const finalPath = payload.query ? `${plainPath}?${_queryString.default.stringify(payload.query)}` : plainPath;
74
60
  const headers = payload.headers || {};
75
61
  let body;
76
-
77
62
  for (const key of realAllowedHeaders) {
78
63
  if (typeof webRequestHeaders[key] !== 'undefined') {
79
64
  headers[key] = webRequestHeaders[key];
80
65
  }
81
66
  }
82
-
83
67
  if (payload.data) {
84
68
  headers['Content-Type'] = 'application/json';
85
69
  body = typeof payload.data === 'object' ? JSON.stringify(payload.data) : payload.body;
86
70
  } else if (payload.body) {
87
- headers['Content-Type'] = 'text/plain'; // eslint-disable-next-line prefer-destructuring
88
-
71
+ headers['Content-Type'] = 'text/plain';
72
+ // eslint-disable-next-line prefer-destructuring
89
73
  body = payload.body;
90
74
  } else if (payload.formData) {
91
- body = payload.formData; // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
75
+ body = payload.formData;
76
+ // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
92
77
  // need multipart boundary auto attached by node-fetch when multipart is true
93
78
  // headers['Content-Type'] = 'multipart/form-data';
94
79
  } else if (payload.formUrlencoded) {
95
80
  headers['Content-Type'] = 'application/x-www-form-urlencoded';
96
-
97
81
  if (typeof payload.formUrlencoded === 'object') {
98
82
  body = _queryString.default.stringify(payload.formUrlencoded);
99
83
  } else {
100
84
  body = payload.formUrlencoded;
101
85
  }
102
86
  }
103
-
104
87
  const url = `http://localhost:${port}${finalPath}`;
105
88
  const fetcher = realRequest || originFetch;
106
89
  return fetcher(url, {
@@ -109,8 +92,6 @@ fetch = _nodeFetch.default) => {
109
92
  headers
110
93
  });
111
94
  };
112
-
113
95
  return sender;
114
96
  };
115
-
116
97
  exports.createRequest = createRequest;
@@ -6,21 +6,17 @@ import qs from 'query-string';
6
6
  import { handleRes } from "./handleRes";
7
7
  var realRequest;
8
8
  var realAllowedHeaders;
9
-
10
9
  var originFetch = function originFetch() {
11
10
  return fetch.apply(void 0, arguments).then(handleRes);
12
11
  };
13
-
14
12
  export var configure = function configure(options) {
15
13
  var request = options.request,
16
- interceptor = options.interceptor,
17
- allowedHeaders = options.allowedHeaders;
14
+ interceptor = options.interceptor,
15
+ allowedHeaders = options.allowedHeaders;
18
16
  realRequest = request || originFetch;
19
-
20
17
  if (interceptor && !request) {
21
18
  realRequest = interceptor(fetch);
22
19
  }
23
-
24
20
  if (Array.isArray(allowedHeaders)) {
25
21
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
26
22
  realAllowedHeaders = allowedHeaders;
@@ -33,22 +29,20 @@ export var createRequest = function createRequest(path, method, port) {
33
29
  });
34
30
  var keys = [];
35
31
  pathToRegexp(path, keys);
36
-
37
32
  var sender = /*#__PURE__*/function () {
38
33
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
39
34
  var _len,
40
- args,
41
- _key,
42
- fetcher,
43
- payload,
44
- requestParams,
45
- params,
46
- finalPath,
47
- finalURL,
48
- headers,
49
- body,
50
- _args = arguments;
51
-
35
+ args,
36
+ _key,
37
+ fetcher,
38
+ payload,
39
+ requestParams,
40
+ params,
41
+ finalPath,
42
+ finalURL,
43
+ headers,
44
+ body,
45
+ _args = arguments;
52
46
  return _regeneratorRuntime().wrap(function _callee$(_context) {
53
47
  while (1) {
54
48
  switch (_context.prev = _context.next) {
@@ -56,12 +50,10 @@ export var createRequest = function createRequest(path, method, port) {
56
50
  for (_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
57
51
  args[_key] = _args[_key];
58
52
  }
59
-
60
53
  fetcher = realRequest || originFetch;
61
54
  payload = _typeof(args[args.length - 1]) === 'object' ? args[args.length - 1] : {};
62
55
  payload.params = payload.params || {};
63
56
  requestParams = args[0]; // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
64
-
65
57
  if (_typeof(requestParams) === 'object' && requestParams.params) {
66
58
  params = requestParams.params;
67
59
  keys.forEach(function (key) {
@@ -72,27 +64,26 @@ export var createRequest = function createRequest(path, method, port) {
72
64
  payload.params[key.name] = args[index];
73
65
  });
74
66
  }
75
-
76
67
  finalPath = getFinalPath(payload.params);
77
68
  finalURL = payload.query ? "".concat(finalPath, "?").concat(qs.stringify(payload.query)) : finalPath;
78
69
  headers = payload.headers || {};
79
70
  body = payload.data && _typeof(payload.data) === 'object' ? JSON.stringify(payload.data) : payload.body;
80
-
81
71
  if (payload.data) {
82
72
  headers['Content-Type'] = 'application/json';
83
73
  body = _typeof(payload.data) === 'object' ? JSON.stringify(payload.data) : payload.body;
84
74
  } else if (payload.body) {
85
- headers['Content-Type'] = 'text/plain'; // eslint-disable-next-line prefer-destructuring
86
-
75
+ headers['Content-Type'] = 'text/plain';
76
+ // eslint-disable-next-line prefer-destructuring
87
77
  body = payload.body;
88
78
  } else if (payload.formData) {
89
- body = payload.formData; // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
79
+ body = payload.formData;
80
+ // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
90
81
  // need multipart boundary aotu attached by browser when multipart is true
91
82
  // headers['Content-Type'] = 'multipart/form-data';
92
83
  } else if (payload.formUrlencoded) {
93
84
  headers['Content-Type'] = 'application/x-www-form-urlencoded';
94
-
95
- if (_typeof(payload.formUrlencoded) === 'object' && // eslint-disable-next-line @typescript-eslint/ban-ts-comment
85
+ if (_typeof(payload.formUrlencoded) === 'object' &&
86
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
96
87
  // @ts-expect-error
97
88
  // eslint-disable-next-line node/prefer-global/url-search-params,node/no-unsupported-features/node-builtins
98
89
  !(payload.formUrlencoded instanceof URLSearchParams)) {
@@ -101,13 +92,11 @@ export var createRequest = function createRequest(path, method, port) {
101
92
  body = payload.formUrlencoded;
102
93
  }
103
94
  }
104
-
105
95
  return _context.abrupt("return", fetcher(finalURL, {
106
96
  method: method,
107
97
  body: body,
108
98
  headers: headers
109
99
  }));
110
-
111
100
  case 12:
112
101
  case "end":
113
102
  return _context.stop();
@@ -115,11 +104,9 @@ export var createRequest = function createRequest(path, method, port) {
115
104
  }
116
105
  }, _callee);
117
106
  }));
118
-
119
107
  return function sender() {
120
108
  return _ref.apply(this, arguments);
121
109
  };
122
110
  }();
123
-
124
111
  return sender;
125
112
  };
@@ -1,6 +1,5 @@
1
1
  import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
-
4
3
  var handleRes = /*#__PURE__*/function () {
5
4
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(res) {
6
5
  var contentType, data;
@@ -9,55 +8,42 @@ var handleRes = /*#__PURE__*/function () {
9
8
  switch (_context.prev = _context.next) {
10
9
  case 0:
11
10
  contentType = res.headers.get('content-type'); // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
12
-
13
11
  if (res.ok) {
14
12
  _context.next = 7;
15
13
  break;
16
14
  }
17
-
18
15
  _context.next = 4;
19
16
  return res.json();
20
-
21
17
  case 4:
22
18
  data = _context.sent;
23
19
  res.data = data;
24
20
  throw res;
25
-
26
21
  case 7:
27
22
  if (!(contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json'))) {
28
23
  _context.next = 9;
29
24
  break;
30
25
  }
31
-
32
26
  return _context.abrupt("return", res.json());
33
-
34
27
  case 9:
35
28
  if (!(contentType !== null && contentType !== void 0 && contentType.includes('text/html') || contentType !== null && contentType !== void 0 && contentType.includes('text/plain'))) {
36
29
  _context.next = 11;
37
30
  break;
38
31
  }
39
-
40
32
  return _context.abrupt("return", res.text());
41
-
42
33
  case 11:
43
34
  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)) {
44
35
  _context.next = 13;
45
36
  break;
46
37
  }
47
-
48
38
  return _context.abrupt("return", res.formData());
49
-
50
39
  case 13:
51
40
  if (!(contentType !== null && contentType !== void 0 && contentType.includes('application/octet-stream'))) {
52
41
  _context.next = 15;
53
42
  break;
54
43
  }
55
-
56
44
  return _context.abrupt("return", res.arrayBuffer());
57
-
58
45
  case 15:
59
46
  return _context.abrupt("return", res.text());
60
-
61
47
  case 16:
62
48
  case "end":
63
49
  return _context.stop();
@@ -65,10 +51,8 @@ var handleRes = /*#__PURE__*/function () {
65
51
  }
66
52
  }, _callee);
67
53
  }));
68
-
69
54
  return function handleRes(_x) {
70
55
  return _ref.apply(this, arguments);
71
56
  };
72
57
  }();
73
-
74
58
  export { handleRes };
@@ -1,9 +1,7 @@
1
1
  import { isBrowser } from '@modern-js/utils';
2
2
  import { createRequest as browser } from "./browser";
3
3
  import { createRequest as node } from "./node";
4
-
5
4
  var createRequest = function createRequest() {
6
5
  return isBrowser() ? browser.apply(void 0, arguments) : node.apply(void 0, arguments);
7
6
  };
8
-
9
7
  export default createRequest;
@@ -7,21 +7,17 @@ import qs from 'query-string';
7
7
  import { handleRes } from "./handleRes";
8
8
  var realRequest;
9
9
  var realAllowedHeaders = [];
10
-
11
10
  var originFetch = function originFetch() {
12
11
  return nodeFetch.apply(void 0, arguments).then(handleRes);
13
12
  };
14
-
15
13
  export var configure = function configure(options) {
16
14
  var request = options.request,
17
- interceptor = options.interceptor,
18
- allowedHeaders = options.allowedHeaders;
15
+ interceptor = options.interceptor,
16
+ allowedHeaders = options.allowedHeaders;
19
17
  realRequest = request || originFetch;
20
-
21
18
  if (interceptor && !request) {
22
19
  realRequest = interceptor(nodeFetch);
23
20
  }
24
-
25
21
  if (Array.isArray(allowedHeaders)) {
26
22
  realAllowedHeaders = allowedHeaders;
27
23
  }
@@ -33,17 +29,15 @@ export var createRequest = function createRequest(path, method, port) {
33
29
  });
34
30
  var keys = [];
35
31
  pathToRegexp(path, keys);
36
-
37
32
  var sender = function sender() {
38
33
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
39
34
  args[_key] = arguments[_key];
40
35
  }
41
-
42
36
  var webRequestHeaders = useHeaders();
43
37
  var payload = _typeof(args[args.length - 1]) === 'object' ? args[args.length - 1] : {};
44
38
  payload.params = payload.params || {};
45
- var requestParams = args[0]; // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
46
-
39
+ var requestParams = args[0];
40
+ // 这种场景下是使用 schema,所以 params 要从 args[0] 中获取
47
41
  if (_typeof(requestParams) === 'object' && requestParams.params) {
48
42
  var params = requestParams.params;
49
43
  keys.forEach(function (key) {
@@ -54,19 +48,15 @@ export var createRequest = function createRequest(path, method, port) {
54
48
  payload.params[key.name] = args[index];
55
49
  });
56
50
  }
57
-
58
51
  var plainPath = getFinalPath(payload.params);
59
52
  var finalPath = payload.query ? "".concat(plainPath, "?").concat(qs.stringify(payload.query)) : plainPath;
60
53
  var headers = payload.headers || {};
61
54
  var body;
62
-
63
55
  var _iterator = _createForOfIteratorHelper(realAllowedHeaders),
64
- _step;
65
-
56
+ _step;
66
57
  try {
67
58
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
68
59
  var key = _step.value;
69
-
70
60
  if (typeof webRequestHeaders[key] !== 'undefined') {
71
61
  headers[key] = webRequestHeaders[key];
72
62
  }
@@ -76,28 +66,26 @@ export var createRequest = function createRequest(path, method, port) {
76
66
  } finally {
77
67
  _iterator.f();
78
68
  }
79
-
80
69
  if (payload.data) {
81
70
  headers['Content-Type'] = 'application/json';
82
71
  body = _typeof(payload.data) === 'object' ? JSON.stringify(payload.data) : payload.body;
83
72
  } else if (payload.body) {
84
- headers['Content-Type'] = 'text/plain'; // eslint-disable-next-line prefer-destructuring
85
-
73
+ headers['Content-Type'] = 'text/plain';
74
+ // eslint-disable-next-line prefer-destructuring
86
75
  body = payload.body;
87
76
  } else if (payload.formData) {
88
- body = payload.formData; // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
77
+ body = payload.formData;
78
+ // https://stackoverflow.com/questions/44919424/bad-content-type-header-no-multipart-boundary-nodejs
89
79
  // need multipart boundary auto attached by node-fetch when multipart is true
90
80
  // headers['Content-Type'] = 'multipart/form-data';
91
81
  } else if (payload.formUrlencoded) {
92
82
  headers['Content-Type'] = 'application/x-www-form-urlencoded';
93
-
94
83
  if (_typeof(payload.formUrlencoded) === 'object') {
95
84
  body = qs.stringify(payload.formUrlencoded);
96
85
  } else {
97
86
  body = payload.formUrlencoded;
98
87
  }
99
88
  }
100
-
101
89
  var url = "http://localhost:".concat(port).concat(finalPath);
102
90
  var fetcher = realRequest || originFetch;
103
91
  return fetcher(url, {
@@ -106,6 +94,5 @@ export var createRequest = function createRequest(path, method, port) {
106
94
  headers: headers
107
95
  });
108
96
  };
109
-
110
97
  return sender;
111
98
  };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.0.0-beta.0",
14
+ "version": "2.0.0-beta.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -63,21 +63,21 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "@babel/runtime": "^7.18.0",
66
- "@modern-js/utils": "2.0.0-beta.0",
67
66
  "node-fetch": "^2.6.1",
68
67
  "path-to-regexp": "^6.2.0",
69
- "query-string": "^7.1.1"
68
+ "query-string": "^7.1.1",
69
+ "@modern-js/utils": "2.0.0-beta.2"
70
70
  },
71
71
  "devDependencies": {
72
- "@scripts/build": "2.0.0-beta.0",
73
- "@scripts/jest-config": "2.0.0-beta.0",
74
72
  "@types/jest": "^27",
75
73
  "@types/node": "^14",
76
74
  "@types/node-fetch": "^2.6.1",
77
75
  "isomorphic-fetch": "^3.0.0",
78
76
  "jest": "^27",
79
77
  "nock": "^13.2.1",
80
- "typescript": "^4"
78
+ "typescript": "^4",
79
+ "@scripts/build": "2.0.0-beta.2",
80
+ "@scripts/jest-config": "2.0.0-beta.2"
81
81
  },
82
82
  "sideEffects": false,
83
83
  "publishConfig": {
@@ -85,8 +85,8 @@
85
85
  "access": "public"
86
86
  },
87
87
  "scripts": {
88
- "new": "modern new",
89
- "build": "modern build",
88
+ "new": "modern-lib new",
89
+ "build": "modern-lib build",
90
90
  "test": "jest --passWithNoTests"
91
91
  }
92
92
  }