@midwayjs/web 3.11.12 → 3.12.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.
@@ -1,7 +1,11 @@
1
1
  'use strict';
2
2
  const rc = Symbol('Context#RequestContext');
3
3
  const ctxLogger = Symbol('Context#ContextLogger');
4
- const { MidwayRequestContainer } = require('@midwayjs/core');
4
+ const {
5
+ MidwayRequestContainer,
6
+ MidwayWebRouterService,
7
+ httpError,
8
+ } = require('@midwayjs/core');
5
9
 
6
10
  module.exports = {
7
11
  get requestContext() {
@@ -37,4 +41,27 @@ module.exports = {
37
41
  getAttr(key) {
38
42
  return this.requestContext.getAttr(key);
39
43
  },
44
+
45
+ async forward(url) {
46
+ const routerService = this.requestContext.get(MidwayWebRouterService);
47
+ const matchedUrlRouteInfo = await routerService.getMatchedRouterInfo(
48
+ url,
49
+ this.method
50
+ );
51
+
52
+ if (matchedUrlRouteInfo) {
53
+ if (matchedUrlRouteInfo.controllerClz) {
54
+ // normal class controller router
55
+ const controllerInstance = await this.requestContext.getAsync(
56
+ matchedUrlRouteInfo.controllerClz
57
+ );
58
+ return controllerInstance[matchedUrlRouteInfo.method](this);
59
+ } else if (typeof matchedUrlRouteInfo.method === 'function') {
60
+ // dynamic router
61
+ return matchedUrlRouteInfo.method(this);
62
+ }
63
+ } else {
64
+ throw new httpError.NotFoundError(`Forward url ${url} Not Found`);
65
+ }
66
+ },
40
67
  };
package/dist/cluster.js CHANGED
@@ -3,7 +3,6 @@ var _a, _b;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  const core_1 = require("@midwayjs/core");
5
5
  const path_1 = require("path");
6
- const utils_1 = require("./utils");
7
6
  const util_1 = require("util");
8
7
  const debug = (0, util_1.debuglog)('midway:debug');
9
8
  function runInAgent() {
@@ -16,13 +15,13 @@ const isAgent = runInAgent();
16
15
  debug('[egg]: run with egg-scripts in worker and init midway container in cluster mode');
17
16
  const appDir = (_b = (_a = JSON.parse(process.argv[2])) === null || _a === void 0 ? void 0 : _a.baseDir) !== null && _b !== void 0 ? _b : process.cwd();
18
17
  let baseDir;
19
- if ((0, utils_1.isTypeScriptEnvironment)()) {
18
+ if ((0, core_1.isTypeScriptEnvironment)()) {
20
19
  baseDir = (0, path_1.join)(appDir, 'src');
21
20
  }
22
21
  else {
23
22
  baseDir = (0, path_1.join)(appDir, 'dist');
24
23
  }
25
- (0, core_1.prepareGlobalApplicationContext)({
24
+ (0, core_1.prepareGlobalApplicationContextSync)({
26
25
  appDir,
27
26
  baseDir,
28
27
  ignore: ['**/app/extend/**', '**/app/public/**'],
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export declare function isTypeScriptEnvironment(): boolean;
2
1
  export declare const parseNormalDir: (baseDir: string, isTypescript?: boolean) => {
3
2
  baseDir: string;
4
3
  appDir: string;
package/dist/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initializeAgentApplicationContext = exports.getCurrentDateString = exports.findLernaRoot = exports.parseNormalDir = exports.isTypeScriptEnvironment = void 0;
3
+ exports.initializeAgentApplicationContext = exports.getCurrentDateString = exports.findLernaRoot = exports.parseNormalDir = void 0;
4
4
  const path_1 = require("path");
5
5
  const find_up_1 = require("find-up");
6
6
  const fs_1 = require("fs");
@@ -10,22 +10,13 @@ const web_1 = require("./framework/web");
10
10
  const util_1 = require("util");
11
11
  const interface_1 = require("./interface");
12
12
  const debug = (0, util_1.debuglog)('midway:debug');
13
- function isTypeScriptEnvironment() {
14
- const TS_MODE_PROCESS_FLAG = process.env.MIDWAY_TS_MODE;
15
- if ('false' === TS_MODE_PROCESS_FLAG) {
16
- return false;
17
- }
18
- // eslint-disable-next-line node/no-deprecated-api
19
- return TS_MODE_PROCESS_FLAG === 'true' || !!require.extensions['.ts'];
20
- }
21
- exports.isTypeScriptEnvironment = isTypeScriptEnvironment;
22
13
  const parseNormalDir = (baseDir, isTypescript = true) => {
23
14
  if (isTypescript) {
24
15
  // 这里要么就是 src 目录,要么就已经是根目录
25
16
  if (!(0, fs_1.existsSync)((0, path_1.join)(baseDir, 'package.json'))) {
26
17
  baseDir = (0, path_1.basename)(baseDir);
27
18
  }
28
- const isTypeScriptEnv = isTypeScriptEnvironment();
19
+ const isTypeScriptEnv = (0, core_1.isTypeScriptEnvironment)();
29
20
  if (isTypeScriptEnv) {
30
21
  return {
31
22
  baseDir: (0, path_1.join)(baseDir, process.env.MIDWAY_SOURCE_DIR || 'src'),
package/index.d.ts CHANGED
@@ -29,6 +29,7 @@ declare module 'egg' {
29
29
 
30
30
  interface Context<ResponseBodyT = any> extends IMidwayBaseContext {
31
31
  getLogger(name?: string): ILogger;
32
+ forward: (url: string) => void;
32
33
  }
33
34
  }
34
35
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/web",
3
- "version": "3.11.12",
3
+ "version": "3.12.0",
4
4
  "description": "Midway Web Framework for Egg.js",
5
5
  "main": "dist/index",
6
6
  "typings": "index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "license": "MIT",
30
30
  "devDependencies": {
31
31
  "@midwayjs/logger": "^2.15.0",
32
- "@midwayjs/mock": "^3.11.12",
32
+ "@midwayjs/mock": "^3.12.0",
33
33
  "dayjs": "1.11.9",
34
34
  "egg-logger": "2.9.1",
35
35
  "egg-mock": "4.2.1",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@eggjs/router": "^2.0.0",
48
- "@midwayjs/core": "^3.11.11",
48
+ "@midwayjs/core": "^3.12.0",
49
49
  "egg": "^2.28.0",
50
50
  "egg-cluster": "^1.27.1",
51
51
  "find-up": "5.0.0",
@@ -59,5 +59,5 @@
59
59
  "engines": {
60
60
  "node": ">=12"
61
61
  },
62
- "gitHead": "7454a59cd57e693a04e4c887535c9193354e2980"
62
+ "gitHead": "0e9a08cd078c4c4dedfa753a8c1025806cc0b0a2"
63
63
  }