@modern-js/plugin-express 2.5.0 → 2.7.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/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @modern-js/plugin-express
2
2
 
3
+ ## 2.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 84bfb439b8: feat: support custom apiDir, lambdaDir and style of writing for bff
8
+ feat: 支持定制 api 目录,lambda 目录,bff 的写法
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies [0f15fc597c]
13
+ - Updated dependencies [dcad887024]
14
+ - Updated dependencies [a4672f7c16]
15
+ - Updated dependencies [7bb1554194]
16
+ - Updated dependencies [7fff9020e1]
17
+ - Updated dependencies [1eea234fdd]
18
+ - Updated dependencies [84bfb439b8]
19
+ - @modern-js/utils@2.7.0
20
+ - @modern-js/types@2.7.0
21
+ - @modern-js/bff-core@2.7.0
22
+ - @modern-js/bff-runtime@2.7.0
23
+
24
+ ## 2.6.0
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies [e1f799e]
29
+ - Updated dependencies [7915ab3]
30
+ - Updated dependencies [49fa0b1]
31
+ - Updated dependencies [0fe658a]
32
+ - Updated dependencies [62930b9]
33
+ - @modern-js/utils@2.6.0
34
+ - @modern-js/types@2.6.0
35
+ - @modern-js/bff-core@2.6.0
36
+ - @modern-js/bff-runtime@2.6.0
37
+
3
38
  ## 2.5.0
4
39
 
5
40
  ### Patch Changes
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
package/dist/cjs/index.js CHANGED
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
18
18
  };
19
19
  var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
20
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
25
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
26
  mod
23
27
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -85,9 +89,10 @@ var plugin_default = () => ({
85
89
  setup: (api) => ({
86
90
  async prepareApiServer({ pwd, config }) {
87
91
  let app;
88
- const apiDir = path.join(pwd, "./api");
89
92
  const appContext = api.useAppContext();
90
93
  const apiHandlerInfos = appContext.apiHandlerInfos;
94
+ const apiDirectory = appContext.apiDirectory;
95
+ const apiDir = apiDirectory || path.join(pwd, "./api");
91
96
  const mode = appContext.apiMode;
92
97
  if (mode === "framework") {
93
98
  const appModule = await findAppModule(apiDir);
@@ -25,6 +25,7 @@ var import_context = require("../context");
25
25
  const Pipe = (func) => {
26
26
  return {
27
27
  name: "pipe",
28
+ // eslint-disable-next-line consistent-return
28
29
  async execute(executeHelper, next) {
29
30
  const { inputs } = executeHelper;
30
31
  const ctx = (0, import_context.useContext)();
package/dist/cjs/utils.js CHANGED
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -58,6 +62,7 @@ const handleResponseMeta = (res, handler) => {
58
62
  };
59
63
  const createRouteHandler = (handler) => {
60
64
  const apiHandler = async (req, res, next) => {
65
+ var _a;
61
66
  const input = await getInputFromRequest(req);
62
67
  if ((0, import_bff_core.isWithMetaHandler)(handler)) {
63
68
  try {
@@ -91,19 +96,32 @@ const createRouteHandler = (handler) => {
91
96
  res.status(200);
92
97
  return res.json(result1.value);
93
98
  }
94
- } else {
95
- const args = Object.values(input.params).concat(input);
99
+ } else if ((0, import_bff_core.isInputParamsDeciderHandler)(handler)) {
96
100
  try {
101
+ const args = ((_a = input == null ? void 0 : input.data) == null ? void 0 : _a.args) || [];
97
102
  const body = await handler(...args);
98
- if (res.headersSent) {
99
- return await Promise.resolve();
100
- }
101
103
  if (typeof body !== "undefined") {
102
- return res.json(body);
104
+ if (typeof body === "object") {
105
+ return res.json(body);
106
+ }
107
+ return res.send(body);
103
108
  }
104
109
  } catch (e) {
105
110
  return next(e);
106
111
  }
112
+ } else {
113
+ const args1 = Object.values(input.params).concat(input);
114
+ try {
115
+ const body1 = await handler(...args1);
116
+ if (res.headersSent) {
117
+ return await Promise.resolve();
118
+ }
119
+ if (typeof body1 !== "undefined") {
120
+ return res.json(body1);
121
+ }
122
+ } catch (e1) {
123
+ return next(e1);
124
+ }
107
125
  }
108
126
  };
109
127
  Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
@@ -257,13 +257,14 @@ var plugin_default = function() {
257
257
  prepareApiServer: function prepareApiServer(param) {
258
258
  var pwd = param.pwd, config = param.config;
259
259
  return _asyncToGenerator(function() {
260
- var app, apiDir, appContext, apiHandlerInfos, mode, appModule, hooks, middleware, afterLambdaRegisted, middleware1;
260
+ var app, appContext, apiHandlerInfos, apiDirectory, apiDir, mode, appModule, hooks, middleware, afterLambdaRegisted, middleware1;
261
261
  return __generator(this, function(_state) {
262
262
  switch(_state.label){
263
263
  case 0:
264
- apiDir = path.join(pwd, "./api");
265
264
  appContext = api.useAppContext();
266
265
  apiHandlerInfos = appContext.apiHandlerInfos;
266
+ apiDirectory = appContext.apiDirectory;
267
+ apiDir = apiDirectory || path.join(pwd, "./api");
267
268
  mode = appContext.apiMode;
268
269
  if (!(mode === "framework")) return [
269
270
  3,
@@ -126,7 +126,8 @@ import { useContext } from "../context";
126
126
  var Pipe = function(func) {
127
127
  return {
128
128
  name: "pipe",
129
- execute: function execute(executeHelper, next) {
129
+ execute: // eslint-disable-next-line consistent-return
130
+ function execute(executeHelper, next) {
130
131
  return _asyncToGenerator(function() {
131
132
  var inputs, ctx, res, isPiped, end, output;
132
133
  return __generator(this, function(_state) {
package/dist/esm/utils.js CHANGED
@@ -216,7 +216,7 @@ var __generator = this && this.__generator || function(thisArg, body) {
216
216
  }
217
217
  };
218
218
  import "reflect-metadata";
219
- import { httpMethods, isWithMetaHandler, HttpMetadata, ResponseMetaType, ValidationError } from "@modern-js/bff-core";
219
+ import { httpMethods, isWithMetaHandler, isInputParamsDeciderHandler, HttpMetadata, ResponseMetaType, ValidationError } from "@modern-js/bff-core";
220
220
  import { isSchemaHandler } from "@modern-js/bff-runtime";
221
221
  import typeIs from "type-is";
222
222
  import formidable from "formidable";
@@ -281,7 +281,7 @@ var handleResponseMeta = function(res, handler) {
281
281
  var createRouteHandler = function(handler) {
282
282
  var apiHandler = function() {
283
283
  var _ref = _asyncToGenerator(function(req, res, next) {
284
- var input, result, error, result1, args, body, e;
284
+ var input, result, error, result1, _input_data, args, body, e, args1, body1, e1;
285
285
  return __generator(this, function(_state) {
286
286
  switch(_state.label){
287
287
  case 0:
@@ -340,7 +340,7 @@ var createRouteHandler = function(handler) {
340
340
  case 5:
341
341
  return [
342
342
  3,
343
- 14
343
+ 19
344
344
  ];
345
345
  case 6:
346
346
  if (!isSchemaHandler(handler)) return [
@@ -372,55 +372,102 @@ var createRouteHandler = function(handler) {
372
372
  }
373
373
  return [
374
374
  3,
375
- 14
375
+ 19
376
376
  ];
377
377
  case 8:
378
- args = Object.values(input.params).concat(input);
378
+ if (!isInputParamsDeciderHandler(handler)) return [
379
+ 3,
380
+ 13
381
+ ];
379
382
  _state.label = 9;
380
383
  case 9:
381
384
  _state.trys.push([
382
385
  9,
383
- 13,
386
+ 11,
384
387
  ,
385
- 14
388
+ 12
386
389
  ]);
390
+ args = (input === null || input === void 0 ? void 0 : (_input_data = input.data) === null || _input_data === void 0 ? void 0 : _input_data.args) || [];
387
391
  return [
388
392
  4,
389
393
  handler.apply(void 0, _toConsumableArray(args))
390
394
  ];
391
395
  case 10:
392
396
  body = _state.sent();
393
- if (!res.headersSent) return [
397
+ if (typeof body !== "undefined") {
398
+ if (typeof body === "object") {
399
+ return [
400
+ 2,
401
+ res.json(body)
402
+ ];
403
+ }
404
+ return [
405
+ 2,
406
+ res.send(body)
407
+ ];
408
+ }
409
+ return [
394
410
  3,
395
411
  12
396
412
  ];
413
+ case 11:
414
+ e = _state.sent();
415
+ return [
416
+ 2,
417
+ next(e)
418
+ ];
419
+ case 12:
420
+ return [
421
+ 3,
422
+ 19
423
+ ];
424
+ case 13:
425
+ args1 = Object.values(input.params).concat(input);
426
+ _state.label = 14;
427
+ case 14:
428
+ _state.trys.push([
429
+ 14,
430
+ 18,
431
+ ,
432
+ 19
433
+ ]);
434
+ return [
435
+ 4,
436
+ handler.apply(void 0, _toConsumableArray(args1))
437
+ ];
438
+ case 15:
439
+ body1 = _state.sent();
440
+ if (!res.headersSent) return [
441
+ 3,
442
+ 17
443
+ ];
397
444
  return [
398
445
  4,
399
446
  Promise.resolve()
400
447
  ];
401
- case 11:
448
+ case 16:
402
449
  return [
403
450
  2,
404
451
  _state.sent()
405
452
  ];
406
- case 12:
407
- if (typeof body !== "undefined") {
453
+ case 17:
454
+ if (typeof body1 !== "undefined") {
408
455
  return [
409
456
  2,
410
- res.json(body)
457
+ res.json(body1)
411
458
  ];
412
459
  }
413
460
  return [
414
461
  3,
415
- 14
462
+ 19
416
463
  ];
417
- case 13:
418
- e = _state.sent();
464
+ case 18:
465
+ e1 = _state.sent();
419
466
  return [
420
467
  2,
421
- next(e)
468
+ next(e1)
422
469
  ];
423
- case 14:
470
+ case 19:
424
471
  return [
425
472
  2
426
473
  ];
@@ -57,9 +57,10 @@ var plugin_default = () => ({
57
57
  setup: (api) => ({
58
58
  async prepareApiServer({ pwd, config }) {
59
59
  let app;
60
- const apiDir = path.join(pwd, "./api");
61
60
  const appContext = api.useAppContext();
62
61
  const apiHandlerInfos = appContext.apiHandlerInfos;
62
+ const apiDirectory = appContext.apiDirectory;
63
+ const apiDir = apiDirectory || path.join(pwd, "./api");
63
64
  const mode = appContext.apiMode;
64
65
  if (mode === "framework") {
65
66
  const appModule = await findAppModule(apiDir);
@@ -2,6 +2,7 @@ import { useContext } from "../context";
2
2
  const Pipe = (func) => {
3
3
  return {
4
4
  name: "pipe",
5
+ // eslint-disable-next-line consistent-return
5
6
  async execute(executeHelper, next) {
6
7
  const { inputs } = executeHelper;
7
8
  const ctx = useContext();
@@ -1,5 +1,5 @@
1
1
  import "reflect-metadata";
2
- import { httpMethods, isWithMetaHandler, HttpMetadata, ResponseMetaType, ValidationError } from "@modern-js/bff-core";
2
+ import { httpMethods, isWithMetaHandler, isInputParamsDeciderHandler, HttpMetadata, ResponseMetaType, ValidationError } from "@modern-js/bff-core";
3
3
  import { isSchemaHandler } from "@modern-js/bff-runtime";
4
4
  import typeIs from "type-is";
5
5
  import formidable from "formidable";
@@ -29,6 +29,7 @@ const handleResponseMeta = (res, handler) => {
29
29
  };
30
30
  const createRouteHandler = (handler) => {
31
31
  const apiHandler = async (req, res, next) => {
32
+ var _a;
32
33
  const input = await getInputFromRequest(req);
33
34
  if (isWithMetaHandler(handler)) {
34
35
  try {
@@ -62,19 +63,32 @@ const createRouteHandler = (handler) => {
62
63
  res.status(200);
63
64
  return res.json(result1.value);
64
65
  }
65
- } else {
66
- const args = Object.values(input.params).concat(input);
66
+ } else if (isInputParamsDeciderHandler(handler)) {
67
67
  try {
68
+ const args = ((_a = input == null ? void 0 : input.data) == null ? void 0 : _a.args) || [];
68
69
  const body = await handler(...args);
69
- if (res.headersSent) {
70
- return await Promise.resolve();
71
- }
72
70
  if (typeof body !== "undefined") {
73
- return res.json(body);
71
+ if (typeof body === "object") {
72
+ return res.json(body);
73
+ }
74
+ return res.send(body);
74
75
  }
75
76
  } catch (e) {
76
77
  return next(e);
77
78
  }
79
+ } else {
80
+ const args1 = Object.values(input.params).concat(input);
81
+ try {
82
+ const body1 = await handler(...args1);
83
+ if (res.headersSent) {
84
+ return await Promise.resolve();
85
+ }
86
+ if (typeof body1 !== "undefined") {
87
+ return res.json(body1);
88
+ }
89
+ } catch (e1) {
90
+ return next(e1);
91
+ }
78
92
  }
79
93
  };
80
94
  Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.5.0",
14
+ "version": "2.7.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/cli/index.d.ts",
17
17
  "main": "./dist/cjs/cli/index.js",
@@ -42,10 +42,10 @@
42
42
  "formidable": "^1.2.2",
43
43
  "reflect-metadata": "^0.1.13",
44
44
  "type-is": "^1.6.18",
45
- "@modern-js/bff-core": "2.5.0",
46
- "@modern-js/bff-runtime": "2.5.0",
47
- "@modern-js/types": "2.5.0",
48
- "@modern-js/utils": "2.5.0"
45
+ "@modern-js/bff-runtime": "2.7.0",
46
+ "@modern-js/types": "2.7.0",
47
+ "@modern-js/bff-core": "2.7.0",
48
+ "@modern-js/utils": "2.7.0"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@types/cookie-parser": "^1.4.2",
@@ -61,11 +61,11 @@
61
61
  "supertest": "^6.1.6",
62
62
  "typescript": "^4",
63
63
  "zod": "^3.17.3",
64
- "@modern-js/core": "2.5.0",
65
- "@modern-js/app-tools": "2.5.0",
66
- "@modern-js/server-core": "2.5.0",
67
- "@scripts/build": "2.5.0",
68
- "@scripts/jest-config": "2.5.0"
64
+ "@modern-js/core": "2.7.0",
65
+ "@modern-js/app-tools": "2.7.0",
66
+ "@modern-js/server-core": "2.7.0",
67
+ "@scripts/jest-config": "2.7.0",
68
+ "@scripts/build": "2.7.0"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "express": "^4.17.1"
@@ -76,6 +76,7 @@
76
76
  },
77
77
  "scripts": {
78
78
  "new": "modern-lib new",
79
+ "dev": "modern-lib build --watch",
79
80
  "build": "modern-lib build",
80
81
  "test": "jest --passWithNoTests"
81
82
  }