@modern-js/bff-core 2.25.1 → 2.25.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @modern-js/bff-core
2
2
 
3
+ ## 2.25.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 358ed24: fix: support configuration ts-node and avoid to register ts-node unnecessarily
8
+ fix: 支持配置 ts-node,避免对 ts-node 不必要的注册
9
+ - Updated dependencies [63d8247]
10
+ - Updated dependencies [6651684]
11
+ - Updated dependencies [272646c]
12
+ - Updated dependencies [358ed24]
13
+ - @modern-js/utils@2.25.2
14
+ - @modern-js/bff-runtime@2.25.2
15
+
3
16
  ## 2.25.1
4
17
 
5
18
  ### Patch Changes
@@ -22,7 +22,7 @@ const _path = /* @__PURE__ */ _interop_require_wildcard._(require("path"));
22
22
  const _router = require("../router");
23
23
  const _result = require("./result");
24
24
  const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
25
- const generateClient = async ({ resourcePath, apiDir, lambdaDir, prefix, port, target, requestCreator, fetcher, requireResolve = require.resolve, httpMethodDecider }) => {
25
+ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix, port, target, requestCreator, fetcher, requireResolve = require.resolve, httpMethodDecider }) => {
26
26
  if (!requestCreator) {
27
27
  requestCreator = requireResolve(`${DEFAULT_CLIENT_REQUEST_CREATOR}${target ? `/${target}` : ""}`).replace(/\\/g, "/");
28
28
  } else {
@@ -34,6 +34,7 @@ const generateClient = async ({ resourcePath, apiDir, lambdaDir, prefix, port, t
34
34
  requestCreator = `${resolvedPath}${target ? `/${target}` : ""}`.replace(/\\/g, "/");
35
35
  }
36
36
  const apiRouter = new _router.ApiRouter({
37
+ appDir,
37
38
  apiDir,
38
39
  lambdaDir,
39
40
  prefix,
@@ -46,7 +47,7 @@ const generateClient = async ({ resourcePath, apiDir, lambdaDir, prefix, port, t
46
47
  let handlersCode = "";
47
48
  for (const handlerInfo of handlerInfos) {
48
49
  const { name, httpMethod, routePath } = handlerInfo;
49
- let exportStatement = `const ${name} =`;
50
+ let exportStatement = `var ${name} =`;
50
51
  if (name.toLowerCase() === "default") {
51
52
  exportStatement = "default";
52
53
  }
@@ -20,6 +20,35 @@ const _constants = _export_star._(require("./constants"), exports);
20
20
  const _utils2 = require("./utils");
21
21
  _export_star._(require("./types"), exports);
22
22
  class ApiRouter {
23
+ enableRegister() {
24
+ const existTsLoader = Boolean(require.extensions[".ts"]);
25
+ if (!existTsLoader && (process.env.NODE_ENV !== "production" || this.isBuild)) {
26
+ try {
27
+ const projectSearchDir = this.appDir || this.apiDir;
28
+ const tsNode = require("ts-node");
29
+ tsNode.register({
30
+ projectSearchDir,
31
+ compilerOptions: {
32
+ allowJs: false
33
+ },
34
+ scope: true,
35
+ transpileOnly: true,
36
+ ignore: [
37
+ "(?:^|/)node_modules/"
38
+ ]
39
+ });
40
+ const tsConfigPaths = require("tsconfig-paths");
41
+ const loaderRes = tsConfigPaths.loadConfig(projectSearchDir);
42
+ if (loaderRes.resultType === "success") {
43
+ tsConfigPaths.register({
44
+ baseUrl: loaderRes.absoluteBaseUrl,
45
+ paths: loaderRes.paths
46
+ });
47
+ }
48
+ } catch (error) {
49
+ }
50
+ }
51
+ }
23
52
  isExistLambda() {
24
53
  return this.existLambdaDir;
25
54
  }
@@ -223,14 +252,16 @@ class ApiRouter {
223
252
  }
224
253
  return `${prefix}${finalRouteName}`;
225
254
  }
226
- constructor({ apiDir, lambdaDir, prefix, httpMethodDecider = "functionName" }) {
255
+ constructor({ appDir, apiDir, lambdaDir, prefix, isBuild, httpMethodDecider = "functionName" }) {
227
256
  _define_property._(this, "apiMode", void 0);
257
+ _define_property._(this, "appDir", void 0);
228
258
  _define_property._(this, "apiDir", void 0);
229
259
  _define_property._(this, "existLambdaDir", void 0);
230
260
  _define_property._(this, "httpMethodDecider", void 0);
231
261
  _define_property._(this, "lambdaDir", void 0);
232
262
  _define_property._(this, "prefix", void 0);
233
263
  _define_property._(this, "apiFiles", []);
264
+ _define_property._(this, "isBuild", void 0);
234
265
  _define_property._(this, "getExactApiMode", (apiDir2, lambdaDir2) => {
235
266
  const exist = this.createExistChecker(apiDir2);
236
267
  const existLambdaDir = lambdaDir2 && _utils.fs.pathExistsSync(lambdaDir2) || exist(_constants.FRAMEWORK_MODE_LAMBDA_DIR);
@@ -251,11 +282,14 @@ class ApiRouter {
251
282
  this.validateAbsolute(apiDir, "apiDir");
252
283
  this.validateAbsolute(lambdaDir, "lambdaDir");
253
284
  this.prefix = this.initPrefix(prefix);
285
+ this.appDir = appDir;
254
286
  this.apiDir = apiDir;
255
287
  this.httpMethodDecider = httpMethodDecider;
288
+ this.isBuild = isBuild;
256
289
  this.apiMode = this.getExactApiMode(apiDir, lambdaDir);
257
290
  this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
258
291
  this.existLambdaDir = _utils.fs.existsSync(this.lambdaDir);
259
292
  (0, _utils1.debug)(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
293
+ this.enableRegister();
260
294
  }
261
295
  }
@@ -60,47 +60,8 @@ const clearRouteName = (routeName) => {
60
60
  return finalRouteName;
61
61
  };
62
62
  const isHandler = (input) => input && typeof input === "function";
63
- const enableRegister = (requireFn) => {
64
- let existTsLoader = false;
65
- let firstCall = true;
66
- return (modulePath) => {
67
- if (firstCall) {
68
- existTsLoader = Boolean(require.extensions[".ts"]);
69
- firstCall = false;
70
- }
71
- if (!existTsLoader) {
72
- try {
73
- const projectSearchDir = _path.default.dirname(modulePath);
74
- const tsNode = require("ts-node");
75
- tsNode.register({
76
- projectSearchDir,
77
- compilerOptions: {
78
- allowJs: false
79
- },
80
- scope: true,
81
- transpileOnly: true,
82
- ignore: [
83
- "(?:^|/)node_modules/"
84
- ]
85
- });
86
- existTsLoader = true;
87
- const tsConfigPaths = require("tsconfig-paths");
88
- const loaderRes = tsConfigPaths.loadConfig(projectSearchDir);
89
- if (loaderRes.resultType === "success") {
90
- tsConfigPaths.register({
91
- baseUrl: loaderRes.absoluteBaseUrl,
92
- paths: loaderRes.paths
93
- });
94
- }
95
- } catch (e) {
96
- }
97
- }
98
- const requiredModule = requireFn(modulePath);
99
- return requiredModule;
100
- };
101
- };
102
63
  const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
103
- const requireHandlerModule = enableRegister((modulePath) => {
64
+ const requireHandlerModule = (modulePath) => {
104
65
  const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
105
66
  const module = originRequire(modulePath);
106
67
  if (isFunction(module)) {
@@ -109,7 +70,7 @@ const requireHandlerModule = enableRegister((modulePath) => {
109
70
  };
110
71
  }
111
72
  return module;
112
- });
73
+ };
113
74
  const routeValue = (routePath) => {
114
75
  if (routePath.includes(":")) {
115
76
  return 11;
@@ -2,7 +2,7 @@ import * as path from "path";
2
2
  import { ApiRouter } from "../router";
3
3
  import { Ok, Err } from "./result";
4
4
  export const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
5
- export const generateClient = async ({ resourcePath, apiDir, lambdaDir, prefix, port, target, requestCreator, fetcher, requireResolve = require.resolve, httpMethodDecider }) => {
5
+ export const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix, port, target, requestCreator, fetcher, requireResolve = require.resolve, httpMethodDecider }) => {
6
6
  if (!requestCreator) {
7
7
  requestCreator = requireResolve(`${DEFAULT_CLIENT_REQUEST_CREATOR}${target ? `/${target}` : ""}`).replace(/\\/g, "/");
8
8
  } else {
@@ -14,6 +14,7 @@ export const generateClient = async ({ resourcePath, apiDir, lambdaDir, prefix,
14
14
  requestCreator = `${resolvedPath}${target ? `/${target}` : ""}`.replace(/\\/g, "/");
15
15
  }
16
16
  const apiRouter = new ApiRouter({
17
+ appDir,
17
18
  apiDir,
18
19
  lambdaDir,
19
20
  prefix,
@@ -26,7 +27,7 @@ export const generateClient = async ({ resourcePath, apiDir, lambdaDir, prefix,
26
27
  let handlersCode = "";
27
28
  for (const handlerInfo of handlerInfos) {
28
29
  const { name, httpMethod, routePath } = handlerInfo;
29
- let exportStatement = `const ${name} =`;
30
+ let exportStatement = `var ${name} =`;
30
31
  if (name.toLowerCase() === "default") {
31
32
  exportStatement = "default";
32
33
  }
@@ -9,6 +9,35 @@ import { getFiles, getPathFromFilename, requireHandlerModule, sortRoutes } from
9
9
  export * from "./types";
10
10
  export * from "./constants";
11
11
  export class ApiRouter {
12
+ enableRegister() {
13
+ const existTsLoader = Boolean(require.extensions[".ts"]);
14
+ if (!existTsLoader && (process.env.NODE_ENV !== "production" || this.isBuild)) {
15
+ try {
16
+ const projectSearchDir = this.appDir || this.apiDir;
17
+ const tsNode = require("ts-node");
18
+ tsNode.register({
19
+ projectSearchDir,
20
+ compilerOptions: {
21
+ allowJs: false
22
+ },
23
+ scope: true,
24
+ transpileOnly: true,
25
+ ignore: [
26
+ "(?:^|/)node_modules/"
27
+ ]
28
+ });
29
+ const tsConfigPaths = require("tsconfig-paths");
30
+ const loaderRes = tsConfigPaths.loadConfig(projectSearchDir);
31
+ if (loaderRes.resultType === "success") {
32
+ tsConfigPaths.register({
33
+ baseUrl: loaderRes.absoluteBaseUrl,
34
+ paths: loaderRes.paths
35
+ });
36
+ }
37
+ } catch (error) {
38
+ }
39
+ }
40
+ }
12
41
  isExistLambda() {
13
42
  return this.existLambdaDir;
14
43
  }
@@ -212,14 +241,16 @@ export class ApiRouter {
212
241
  }
213
242
  return `${prefix}${finalRouteName}`;
214
243
  }
215
- constructor({ apiDir, lambdaDir, prefix, httpMethodDecider = "functionName" }) {
244
+ constructor({ appDir, apiDir, lambdaDir, prefix, isBuild, httpMethodDecider = "functionName" }) {
216
245
  _define_property(this, "apiMode", void 0);
246
+ _define_property(this, "appDir", void 0);
217
247
  _define_property(this, "apiDir", void 0);
218
248
  _define_property(this, "existLambdaDir", void 0);
219
249
  _define_property(this, "httpMethodDecider", void 0);
220
250
  _define_property(this, "lambdaDir", void 0);
221
251
  _define_property(this, "prefix", void 0);
222
252
  _define_property(this, "apiFiles", []);
253
+ _define_property(this, "isBuild", void 0);
223
254
  _define_property(this, "getExactApiMode", (apiDir2, lambdaDir2) => {
224
255
  const exist = this.createExistChecker(apiDir2);
225
256
  const existLambdaDir = lambdaDir2 && fs.pathExistsSync(lambdaDir2) || exist(FRAMEWORK_MODE_LAMBDA_DIR);
@@ -240,11 +271,14 @@ export class ApiRouter {
240
271
  this.validateAbsolute(apiDir, "apiDir");
241
272
  this.validateAbsolute(lambdaDir, "lambdaDir");
242
273
  this.prefix = this.initPrefix(prefix);
274
+ this.appDir = appDir;
243
275
  this.apiDir = apiDir;
244
276
  this.httpMethodDecider = httpMethodDecider;
277
+ this.isBuild = isBuild;
245
278
  this.apiMode = this.getExactApiMode(apiDir, lambdaDir);
246
279
  this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
247
280
  this.existLambdaDir = fs.existsSync(this.lambdaDir);
248
281
  debug(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
282
+ this.enableRegister();
249
283
  }
250
284
  }
@@ -31,47 +31,8 @@ const clearRouteName = (routeName) => {
31
31
  return finalRouteName;
32
32
  };
33
33
  export const isHandler = (input) => input && typeof input === "function";
34
- const enableRegister = (requireFn) => {
35
- let existTsLoader = false;
36
- let firstCall = true;
37
- return (modulePath) => {
38
- if (firstCall) {
39
- existTsLoader = Boolean(require.extensions[".ts"]);
40
- firstCall = false;
41
- }
42
- if (!existTsLoader) {
43
- try {
44
- const projectSearchDir = path.dirname(modulePath);
45
- const tsNode = require("ts-node");
46
- tsNode.register({
47
- projectSearchDir,
48
- compilerOptions: {
49
- allowJs: false
50
- },
51
- scope: true,
52
- transpileOnly: true,
53
- ignore: [
54
- "(?:^|/)node_modules/"
55
- ]
56
- });
57
- existTsLoader = true;
58
- const tsConfigPaths = require("tsconfig-paths");
59
- const loaderRes = tsConfigPaths.loadConfig(projectSearchDir);
60
- if (loaderRes.resultType === "success") {
61
- tsConfigPaths.register({
62
- baseUrl: loaderRes.absoluteBaseUrl,
63
- paths: loaderRes.paths
64
- });
65
- }
66
- } catch (e) {
67
- }
68
- }
69
- const requiredModule = requireFn(modulePath);
70
- return requiredModule;
71
- };
72
- };
73
34
  const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
74
- export const requireHandlerModule = enableRegister((modulePath) => {
35
+ export const requireHandlerModule = (modulePath) => {
75
36
  const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
76
37
  const module = originRequire(modulePath);
77
38
  if (isFunction(module)) {
@@ -80,7 +41,7 @@ export const requireHandlerModule = enableRegister((modulePath) => {
80
41
  };
81
42
  }
82
43
  return module;
83
- });
44
+ };
84
45
  const routeValue = (routePath) => {
85
46
  if (routePath.includes(":")) {
86
47
  return 11;
@@ -4,6 +4,7 @@ export type GenClientResult = Result<string>;
4
4
  export type GenClientOptions = {
5
5
  resourcePath: string;
6
6
  source: string;
7
+ appDir: string;
7
8
  apiDir: string;
8
9
  lambdaDir: string;
9
10
  prefix: string;
@@ -16,6 +17,7 @@ export type GenClientOptions = {
16
17
  };
17
18
  export declare const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
18
19
  export declare const generateClient: ({
20
+ appDir,
19
21
  resourcePath,
20
22
  apiDir,
21
23
  lambdaDir,
@@ -7,23 +7,30 @@ export * from './types';
7
7
  export * from './constants';
8
8
  export declare class ApiRouter {
9
9
  private apiMode;
10
+ private appDir?;
10
11
  private apiDir;
11
12
  private existLambdaDir;
12
13
  private httpMethodDecider;
13
14
  private lambdaDir;
14
15
  private prefix;
15
16
  private apiFiles;
17
+ private isBuild?;
16
18
  constructor({
19
+ appDir,
17
20
  apiDir,
18
21
  lambdaDir,
19
22
  prefix,
23
+ isBuild,
20
24
  httpMethodDecider
21
25
  }: {
26
+ appDir?: string;
22
27
  apiDir: string;
23
28
  lambdaDir?: string;
24
29
  prefix?: string;
30
+ isBuild?: boolean;
25
31
  httpMethodDecider?: HttpMethodDecider;
26
32
  });
33
+ private enableRegister;
27
34
  isExistLambda(): boolean;
28
35
  getApiMode(): APIMode;
29
36
  getLambdaDir(): string;
@@ -1,10 +1,10 @@
1
- import { APIHandlerInfo, HandlerModule } from './types';
1
+ import { APIHandlerInfo } from './types';
2
2
  type MaybeAsync<I> = I | Promise<I>;
3
3
  export type NormalHandler = (...args: any[]) => any;
4
4
  export type Handler<I, O> = (input: I) => MaybeAsync<O>;
5
5
  export declare const getFiles: (lambdaDir: string, rules: string | string[]) => string[];
6
6
  export declare const getPathFromFilename: (baseDir: string, filename: string) => string;
7
7
  export declare const isHandler: (input: any) => input is Handler<any, any>;
8
- export declare const requireHandlerModule: (modulePath: string) => HandlerModule;
8
+ export declare const requireHandlerModule: (modulePath: string) => any;
9
9
  export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
10
10
  export {};
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.25.1",
18
+ "version": "2.25.2",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -31,8 +31,8 @@
31
31
  "koa-compose": "^4.1.0",
32
32
  "reflect-metadata": "^0.1.13",
33
33
  "@swc/helpers": "0.5.1",
34
- "@modern-js/bff-runtime": "2.25.1",
35
- "@modern-js/utils": "2.25.1"
34
+ "@modern-js/bff-runtime": "2.25.2",
35
+ "@modern-js/utils": "2.25.2"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/jest": "^29",
@@ -44,9 +44,9 @@
44
44
  "type-fest": "2.15.0",
45
45
  "typescript": "^5",
46
46
  "zod": "^3.17.3",
47
- "@modern-js/types": "2.25.1",
48
- "@scripts/build": "2.25.1",
49
- "@scripts/jest-config": "2.25.1"
47
+ "@scripts/jest-config": "2.25.2",
48
+ "@modern-js/types": "2.25.2",
49
+ "@scripts/build": "2.25.2"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "zod": "^3.17.3",