@modern-js/plugin-express 2.0.0-beta.3 → 2.0.0-beta.6

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,11 +1,44 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
3
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
- import 'reflect-metadata';
5
- import { httpMethods, isWithMetaHandler, HttpMetadata, ResponseMetaType, ValidationError } from '@modern-js/bff-core';
6
- import { isSchemaHandler } from '@modern-js/bff-runtime';
7
- import typeIs from 'type-is';
8
- import formidable from 'formidable';
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+ var __async = (__this, __arguments, generator) => {
18
+ return new Promise((resolve, reject) => {
19
+ var fulfilled = (value) => {
20
+ try {
21
+ step(generator.next(value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ };
26
+ var rejected = (value) => {
27
+ try {
28
+ step(generator.throw(value));
29
+ } catch (e) {
30
+ reject(e);
31
+ }
32
+ };
33
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
34
+ step((generator = generator.apply(__this, __arguments)).next());
35
+ });
36
+ };
37
+ import "reflect-metadata";
38
+ import { httpMethods, isWithMetaHandler, HttpMetadata, ResponseMetaType, ValidationError } from "@modern-js/bff-core";
39
+ import { isSchemaHandler } from "@modern-js/bff-runtime";
40
+ import typeIs from "type-is";
41
+ import formidable from "formidable";
9
42
  const handleResponseMeta = (res, handler) => {
10
43
  const responseMeta = Reflect.getMetadata(HttpMetadata.Response, handler);
11
44
  if (Array.isArray(responseMeta)) {
@@ -14,7 +47,6 @@ const handleResponseMeta = (res, handler) => {
14
47
  const metaValue = meta.value;
15
48
  switch (metaType) {
16
49
  case ResponseMetaType.Headers:
17
- // eslint-disable-next-line @typescript-eslint/ban-types
18
50
  for (const [key, value] of Object.entries(metaValue)) {
19
51
  res.append(key, value);
20
52
  }
@@ -31,24 +63,22 @@ const handleResponseMeta = (res, handler) => {
31
63
  }
32
64
  }
33
65
  };
34
- export const createRouteHandler = handler => {
35
- const apiHandler = async (req, res, next) => {
36
- const input = await getInputFromRequest(req);
66
+ const createRouteHandler = (handler) => {
67
+ const apiHandler = (req, res, next) => __async(void 0, null, function* () {
68
+ const input = yield getInputFromRequest(req);
37
69
  if (isWithMetaHandler(handler)) {
38
70
  try {
39
71
  handleResponseMeta(res, handler);
40
72
  if (res.headersSent) {
41
73
  return;
42
74
  }
43
- const result = await handler(input);
44
- if (result && typeof result === 'object') {
45
- // eslint-disable-next-line consistent-return
75
+ const result = yield handler(input);
76
+ if (result && typeof result === "object") {
46
77
  return res.json(result);
47
78
  }
48
79
  } catch (error) {
49
80
  if (error instanceof ValidationError) {
50
81
  res.status(error.status);
51
- // eslint-disable-next-line consistent-return
52
82
  return res.json({
53
83
  message: error.message
54
84
  });
@@ -56,63 +86,62 @@ export const createRouteHandler = handler => {
56
86
  throw error;
57
87
  }
58
88
  } else if (isSchemaHandler(handler)) {
59
- const result = await handler(input);
60
- if (result.type !== 'HandleSuccess') {
61
- if (result.type === 'InputValidationError') {
89
+ const result1 = yield handler(input);
90
+ if (result1.type !== "HandleSuccess") {
91
+ if (result1.type === "InputValidationError") {
62
92
  res.status(400);
63
93
  } else {
64
94
  res.status(500);
65
95
  }
66
- // eslint-disable-next-line consistent-return
67
- return res.json(result.message);
96
+ return res.json(result1.message);
68
97
  } else {
69
98
  res.status(200);
70
- // eslint-disable-next-line consistent-return
71
- return res.json(result.value);
99
+ return res.json(result1.value);
72
100
  }
73
101
  } else {
74
102
  const args = Object.values(input.params).concat(input);
75
103
  try {
76
- const body = await handler(...args);
77
-
78
- // this should never happen
104
+ const body = yield handler(...args);
79
105
  if (res.headersSent) {
80
- // eslint-disable-next-line consistent-return
81
- return await Promise.resolve();
106
+ return yield Promise.resolve();
82
107
  }
83
- if (typeof body !== 'undefined') {
84
- // eslint-disable-next-line consistent-return
108
+ if (typeof body !== "undefined") {
85
109
  return res.json(body);
86
110
  }
87
111
  } catch (e) {
88
- // eslint-disable-next-line consistent-return
89
112
  return next(e);
90
113
  }
91
114
  }
92
- };
115
+ });
93
116
  Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
94
117
  return apiHandler;
95
118
  };
96
- export const isNormalMethod = httpMethod => httpMethods.includes(httpMethod);
97
- const getInputFromRequest = async request => {
119
+ const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
120
+ const getInputFromRequest = (request) => __async(void 0, null, function* () {
98
121
  const draft = {
99
122
  params: request.params,
100
123
  query: request.query,
101
124
  headers: request.headers,
102
125
  cookies: request.headers.cookie
103
126
  };
104
- if (typeIs(request, ['application/json'])) {
127
+ if (typeIs(request, [
128
+ "application/json"
129
+ ])) {
105
130
  draft.data = request.body;
106
- } else if (typeIs(request, ['multipart/form-data'])) {
107
- draft.formData = await resolveFormData(request);
108
- } else if (typeIs(request, ['application/x-www-form-urlencoded'])) {
131
+ } else if (typeIs(request, [
132
+ "multipart/form-data"
133
+ ])) {
134
+ draft.formData = yield resolveFormData(request);
135
+ } else if (typeIs(request, [
136
+ "application/x-www-form-urlencoded"
137
+ ])) {
109
138
  draft.formUrlencoded = request.body;
110
139
  } else {
111
140
  draft.body = request.body;
112
141
  }
113
142
  return draft;
114
- };
115
- const resolveFormData = request => {
143
+ });
144
+ const resolveFormData = (request) => {
116
145
  const form = formidable({
117
146
  multiples: true
118
147
  });
@@ -121,7 +150,11 @@ const resolveFormData = request => {
121
150
  if (err) {
122
151
  reject(err);
123
152
  }
124
- resolve(_objectSpread(_objectSpread({}, fields), files));
153
+ resolve(__spreadValues(__spreadValues({}, fields), files));
125
154
  });
126
155
  });
127
- };
156
+ };
157
+ export {
158
+ createRouteHandler,
159
+ isNormalMethod
160
+ };
@@ -1,37 +1,53 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+ var cli_exports = {};
25
+ __export(cli_exports, {
26
+ default: () => cli_default
5
27
  });
6
- exports.default = void 0;
7
- var path = _interopRequireWildcard(require("path"));
8
- var _utils = require("@modern-js/utils");
9
- var _bffCore = require("@modern-js/bff-core");
10
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12
- var _default = () => ({
13
- name: '@modern-js/plugin-express',
14
- setup: api => {
28
+ module.exports = __toCommonJS(cli_exports);
29
+ var path = __toESM(require("path"));
30
+ var import_utils = require("@modern-js/utils");
31
+ var import_bff_core = require("@modern-js/bff-core");
32
+ var cli_default = () => ({
33
+ name: "@modern-js/plugin-express",
34
+ setup: (api) => {
15
35
  let bffExportsUtils;
16
- const {
17
- useAppContext
18
- } = api;
19
- const runtimeModulePath = path.resolve(__dirname, '../runtime');
36
+ const { useAppContext } = api;
37
+ const runtimeModulePath = path.resolve(__dirname, "../runtime");
20
38
  return {
21
39
  config() {
22
40
  const appContext = useAppContext();
23
- const {
24
- appDirectory
25
- } = appContext;
26
- bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'server');
41
+ const { appDirectory } = appContext;
42
+ bffExportsUtils = (0, import_utils.createRuntimeExportsUtils)(appContext.internalDirectory, "server");
27
43
  const serverRuntimePath = bffExportsUtils.getPath();
28
- const relativeRuntimePath = (0, _bffCore.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
29
- if (process.env.NODE_ENV === 'production') {
44
+ const relativeRuntimePath = (0, import_bff_core.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
45
+ if (process.env.NODE_ENV === "production") {
30
46
  return {
31
47
  source: {
32
48
  alias: {
33
- '@modern-js/runtime/server': relativeRuntimePath,
34
- '@modern-js/runtime/express': relativeRuntimePath
49
+ "@modern-js/runtime/server": relativeRuntimePath,
50
+ "@modern-js/runtime/express": relativeRuntimePath
35
51
  }
36
52
  }
37
53
  };
@@ -39,17 +55,25 @@ var _default = () => ({
39
55
  return {
40
56
  source: {
41
57
  alias: {
42
- '@modern-js/runtime/server': serverRuntimePath,
43
- '@modern-js/runtime/express': serverRuntimePath
58
+ "@modern-js/runtime/server": serverRuntimePath,
59
+ "@modern-js/runtime/express": serverRuntimePath
44
60
  }
45
61
  }
46
62
  };
47
63
  }
48
64
  },
65
+ collectServerPlugins({ plugins }) {
66
+ plugins.push({
67
+ "@modern-js/plugin-express": "@modern-js/plugin-express/server"
68
+ });
69
+ return {
70
+ plugins
71
+ };
72
+ },
49
73
  addRuntimeExports(input) {
50
74
  const currentFile = bffExportsUtils.getPath();
51
75
  const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
52
- const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve('express'));
76
+ const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve("express"));
53
77
  bffExportsUtils.addExport(`const pluginRuntime = require('${relativeRuntimeModulePath}');
54
78
  const express = require('${relativeFramePath}')
55
79
  module.exports = {
@@ -62,4 +86,5 @@ var _default = () => ({
62
86
  };
63
87
  }
64
88
  });
65
- exports.default = _default;
89
+ // Annotate the CommonJS export names for ESM import in node:
90
+ 0 && (module.exports = {});
@@ -1,13 +1,30 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var context_exports = {};
19
+ __export(context_exports, {
20
+ run: () => run,
21
+ useContext: () => useContext
5
22
  });
6
- exports.useContext = exports.run = void 0;
7
- var _bffCore = require("@modern-js/bff-core");
8
- const {
23
+ module.exports = __toCommonJS(context_exports);
24
+ var import_bff_core = require("@modern-js/bff-core");
25
+ const { run, useContext } = (0, import_bff_core.createStorage)();
26
+ // Annotate the CommonJS export names for ESM import in node:
27
+ 0 && (module.exports = {
9
28
  run,
10
29
  useContext
11
- } = (0, _bffCore.createStorage)();
12
- exports.useContext = useContext;
13
- exports.run = run;
30
+ });
@@ -1,23 +1,34 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var src_exports = {};
26
+ __export(src_exports, {
27
+ default: () => src_default
5
28
  });
6
- var _exportNames = {};
7
- exports.default = void 0;
8
- var _plugin = _interopRequireDefault(require("./plugin"));
9
- var _context = require("./context");
10
- Object.keys(_context).forEach(function (key) {
11
- if (key === "default" || key === "__esModule") return;
12
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
13
- if (key in exports && exports[key] === _context[key]) return;
14
- Object.defineProperty(exports, key, {
15
- enumerable: true,
16
- get: function () {
17
- return _context[key];
18
- }
19
- });
20
- });
21
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
- var _default = _plugin.default;
23
- exports.default = _default;
29
+ module.exports = __toCommonJS(src_exports);
30
+ var import_plugin = __toESM(require("./plugin"));
31
+ __reExport(src_exports, require("./context"), module.exports);
32
+ var src_default = import_plugin.default;
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {});