@modern-js/create-request 1.1.2 → 1.2.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.
@@ -23,7 +23,9 @@ export const configure = options => {
23
23
  realAllowedHeaders = allowedHeaders;
24
24
  }
25
25
  };
26
- export const createRequest = (path, method, port, fetch = originFetch) => {
26
+ export const createRequest = (path, method, port, // 后续可能要修改,暂时先保留
27
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
28
+ fetch = originFetch) => {
27
29
  const getFinalPath = compile(path, {
28
30
  encode: encodeURIComponent
29
31
  });
@@ -31,7 +33,7 @@ export const createRequest = (path, method, port, fetch = originFetch) => {
31
33
  pathToRegexp(path, keys);
32
34
 
33
35
  const sender = async (...args) => {
34
- const fetcher = realRequest || fetch;
36
+ const fetcher = realRequest || originFetch;
35
37
  const payload = typeof args[args.length - 1] === 'object' ? args[args.length - 1] : {};
36
38
  payload.params = payload.params || {};
37
39
  keys.forEach((key, index) => {
@@ -25,7 +25,9 @@ export const configure = options => {
25
25
  realAllowedHeaders = allowedHeaders;
26
26
  }
27
27
  };
28
- export const createRequest = (path, method, port, fetch = nodeFetch) => {
28
+ export const createRequest = (path, method, port, // 后续可能要修改,暂时先保留
29
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
30
+ fetch = nodeFetch) => {
29
31
  const getFinalPath = compile(path, {
30
32
  encode: encodeURIComponent
31
33
  });
@@ -43,10 +45,12 @@ export const createRequest = (path, method, port, fetch = nodeFetch) => {
43
45
  const plainPath = getFinalPath(payload.params);
44
46
  const finalPath = payload.query ? `${plainPath}?${qs.stringify(payload.query)}` : plainPath;
45
47
  const headers = payload.headers || {};
46
- let body; // BFF 内网请求需要携带 SSR 请求中的 cookie 和 traceId, 如果有压测头,则添加压测头
48
+ let body;
47
49
 
48
50
  for (const key of realAllowedHeaders) {
49
- headers[key] = webRequestHeaders[key];
51
+ if (typeof webRequestHeaders[key] !== 'undefined') {
52
+ headers[key] = webRequestHeaders[key];
53
+ }
50
54
  }
51
55
 
52
56
  if (payload.data) {
@@ -71,7 +75,7 @@ export const createRequest = (path, method, port, fetch = nodeFetch) => {
71
75
  }
72
76
 
73
77
  const url = `http://localhost:${port}${finalPath}`;
74
- const fetcher = realRequest || fetch;
78
+ const fetcher = realRequest || originFetch;
75
79
  return fetcher(url, {
76
80
  method,
77
81
  body,
@@ -35,7 +35,9 @@ const configure = options => {
35
35
 
36
36
  exports.configure = configure;
37
37
 
38
- const createRequest = (path, method, port, fetch = originFetch) => {
38
+ const createRequest = (path, method, port, // 后续可能要修改,暂时先保留
39
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
40
+ fetch = originFetch) => {
39
41
  const getFinalPath = (0, _pathToRegexp.compile)(path, {
40
42
  encode: encodeURIComponent
41
43
  });
@@ -43,7 +45,7 @@ const createRequest = (path, method, port, fetch = originFetch) => {
43
45
  (0, _pathToRegexp.pathToRegexp)(path, keys);
44
46
 
45
47
  const sender = async (...args) => {
46
- const fetcher = realRequest || fetch;
48
+ const fetcher = realRequest || originFetch;
47
49
  const payload = typeof args[args.length - 1] === 'object' ? args[args.length - 1] : {};
48
50
  payload.params = payload.params || {};
49
51
  keys.forEach((key, index) => {
@@ -42,7 +42,9 @@ const configure = options => {
42
42
 
43
43
  exports.configure = configure;
44
44
 
45
- const createRequest = (path, method, port, fetch = _nodeFetch.default) => {
45
+ const createRequest = (path, method, port, // 后续可能要修改,暂时先保留
46
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
47
+ fetch = _nodeFetch.default) => {
46
48
  const getFinalPath = (0, _pathToRegexp.compile)(path, {
47
49
  encode: encodeURIComponent
48
50
  });
@@ -60,10 +62,12 @@ const createRequest = (path, method, port, fetch = _nodeFetch.default) => {
60
62
  const plainPath = getFinalPath(payload.params);
61
63
  const finalPath = payload.query ? `${plainPath}?${_querystring.default.stringify(payload.query)}` : plainPath;
62
64
  const headers = payload.headers || {};
63
- let body; // BFF 内网请求需要携带 SSR 请求中的 cookie 和 traceId, 如果有压测头,则添加压测头
65
+ let body;
64
66
 
65
67
  for (const key of realAllowedHeaders) {
66
- headers[key] = webRequestHeaders[key];
68
+ if (typeof webRequestHeaders[key] !== 'undefined') {
69
+ headers[key] = webRequestHeaders[key];
70
+ }
67
71
  }
68
72
 
69
73
  if (payload.data) {
@@ -88,7 +92,7 @@ const createRequest = (path, method, port, fetch = _nodeFetch.default) => {
88
92
  }
89
93
 
90
94
  const url = `http://localhost:${port}${finalPath}`;
91
- const fetcher = realRequest || fetch;
95
+ const fetcher = realRequest || originFetch;
92
96
  return fetcher(url, {
93
97
  method,
94
98
  body,
@@ -60,7 +60,7 @@ export var createRequest = function createRequest(path, method, port) {
60
60
  args[_key] = _args[_key];
61
61
  }
62
62
 
63
- fetcher = realRequest || fetch;
63
+ fetcher = realRequest || originFetch;
64
64
  payload = _typeof(args[args.length - 1]) === 'object' ? args[args.length - 1] : {};
65
65
  payload.params = payload.params || {};
66
66
  keys.forEach(function (key, index) {
@@ -56,7 +56,7 @@ export var createRequest = function createRequest(path, method, port) {
56
56
  var plainPath = getFinalPath(payload.params);
57
57
  var finalPath = payload.query ? "".concat(plainPath, "?").concat(qs.stringify(payload.query)) : plainPath;
58
58
  var headers = payload.headers || {};
59
- var body; // BFF 内网请求需要携带 SSR 请求中的 cookie 和 traceId, 如果有压测头,则添加压测头
59
+ var body;
60
60
 
61
61
  var _iterator = _createForOfIteratorHelper(realAllowedHeaders),
62
62
  _step;
@@ -64,7 +64,10 @@ export var createRequest = function createRequest(path, method, port) {
64
64
  try {
65
65
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
66
66
  var key = _step.value;
67
- headers[key] = webRequestHeaders[key];
67
+
68
+ if (typeof webRequestHeaders[key] !== 'undefined') {
69
+ headers[key] = webRequestHeaders[key];
70
+ }
68
71
  }
69
72
  } catch (err) {
70
73
  _iterator.e(err);
@@ -94,7 +97,7 @@ export var createRequest = function createRequest(path, method, port) {
94
97
  }
95
98
 
96
99
  var url = "http://localhost:".concat(port).concat(finalPath);
97
- var fetcher = realRequest || fetch;
100
+ var fetcher = realRequest || originFetch;
98
101
  return fetcher(url, {
99
102
  method: method,
100
103
  body: body,
@@ -9,12 +9,12 @@ export declare type BFFRequestPayload = {
9
9
  headers?: Record<string, any>;
10
10
  cookies?: Record<string, any>;
11
11
  };
12
- export declare type Fetch = typeof nodeFetch | typeof fetch;
12
+ export declare type Fetch = typeof fetch | typeof nodeFetch;
13
13
  export declare type Sender = ((...args: any[]) => Promise<any>) & {
14
14
  fetch?: Fetch;
15
15
  };
16
16
  export declare type RequestCreator = (path: string, method: string, port: number, fetch?: Fetch, headerWhiteList?: string[]) => Sender;
17
- export declare type IOptions<F = typeof fetch> = {
17
+ export declare type IOptions<F = Fetch> = {
18
18
  request?: F;
19
19
  interceptor?: (request: F) => F;
20
20
  allowedHeaders?: string[];
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.1.2",
14
+ "version": "1.2.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,26 +25,51 @@
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": "./dist/js/treeshaking/browser.js",
34
- "./modern": "./dist/js/modern/browser.js",
35
- "./server": "./dist/js/node/node.js",
36
- "./hook": "./dist/js/node/hook.js"
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
+ "client": [
57
+ "./dist/types/browser.d.ts"
58
+ ],
59
+ "server": [
60
+ "./dist/types/node.d.ts"
61
+ ]
62
+ }
37
63
  },
38
64
  "dependencies": {
39
65
  "@babel/runtime": "^7",
40
- "@modern-js/utils": "^1.1.6",
66
+ "@modern-js/utils": "^1.2.0",
41
67
  "node-fetch": "^2.6.1",
42
68
  "path-to-regexp": "^6.2.0"
43
69
  },
44
70
  "devDependencies": {
45
- "@modern-js/module-tools": "^1.1.4",
46
- "@modern-js/plugin-ssr": "^1.1.3",
47
- "@modern-js/plugin-testing": "^1.2.2",
71
+ "@scripts/build": "0.0.0",
72
+ "@modern-js/plugin-ssr": "^1.2.0",
48
73
  "@types/jest": "^27.0.3",
49
74
  "@types/node": "^14",
50
75
  "@types/node-fetch": "^2.5.12",
@@ -52,20 +77,23 @@
52
77
  "@types/react-dom": "^17",
53
78
  "isomorphic-fetch": "^3.0.0",
54
79
  "nock": "^13.2.1",
55
- "typescript": "^4"
80
+ "typescript": "^4",
81
+ "jest": "^27",
82
+ "@scripts/jest-config": "0.0.0"
56
83
  },
57
84
  "peerDependencies": {
58
- "@modern-js/plugin-ssr": "^1.1.3"
85
+ "@modern-js/plugin-ssr": "^1.2.0"
59
86
  },
60
87
  "sideEffects": false,
61
88
  "publishConfig": {
62
89
  "registry": "https://registry.npmjs.org/",
63
- "access": "public"
90
+ "access": "public",
91
+ "types": "./dist/types/index.d.ts"
64
92
  },
65
93
  "scripts": {
66
94
  "new": "modern new",
67
95
  "build": "modern build",
68
- "test": "modern test --passWithNoTests"
96
+ "test": "jest --passWithNoTests"
69
97
  },
70
98
  "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"
71
99
  }