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

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,20 +1,61 @@
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 {
39
+ httpMethods,
40
+ isWithMetaHandler,
41
+ HttpMetadata,
42
+ ResponseMetaType,
43
+ ValidationError
44
+ } from "@modern-js/bff-core";
45
+ import { isSchemaHandler } from "@modern-js/bff-runtime";
46
+ import typeIs from "type-is";
47
+ import formidable from "formidable";
9
48
  const handleResponseMeta = (res, handler) => {
10
- const responseMeta = Reflect.getMetadata(HttpMetadata.Response, handler);
49
+ const responseMeta = Reflect.getMetadata(
50
+ HttpMetadata.Response,
51
+ handler
52
+ );
11
53
  if (Array.isArray(responseMeta)) {
12
54
  for (const meta of responseMeta) {
13
55
  const metaType = meta.type;
14
56
  const metaValue = meta.value;
15
57
  switch (metaType) {
16
58
  case ResponseMetaType.Headers:
17
- // eslint-disable-next-line @typescript-eslint/ban-types
18
59
  for (const [key, value] of Object.entries(metaValue)) {
19
60
  res.append(key, value);
20
61
  }
@@ -31,24 +72,22 @@ const handleResponseMeta = (res, handler) => {
31
72
  }
32
73
  }
33
74
  };
34
- export const createRouteHandler = handler => {
35
- const apiHandler = async (req, res, next) => {
36
- const input = await getInputFromRequest(req);
75
+ const createRouteHandler = (handler) => {
76
+ const apiHandler = (req, res, next) => __async(void 0, null, function* () {
77
+ const input = yield getInputFromRequest(req);
37
78
  if (isWithMetaHandler(handler)) {
38
79
  try {
39
80
  handleResponseMeta(res, handler);
40
81
  if (res.headersSent) {
41
82
  return;
42
83
  }
43
- const result = await handler(input);
44
- if (result && typeof result === 'object') {
45
- // eslint-disable-next-line consistent-return
84
+ const result = yield handler(input);
85
+ if (result && typeof result === "object") {
46
86
  return res.json(result);
47
87
  }
48
88
  } catch (error) {
49
89
  if (error instanceof ValidationError) {
50
90
  res.status(error.status);
51
- // eslint-disable-next-line consistent-return
52
91
  return res.json({
53
92
  message: error.message
54
93
  });
@@ -56,72 +95,70 @@ export const createRouteHandler = handler => {
56
95
  throw error;
57
96
  }
58
97
  } else if (isSchemaHandler(handler)) {
59
- const result = await handler(input);
60
- if (result.type !== 'HandleSuccess') {
61
- if (result.type === 'InputValidationError') {
98
+ const result = yield handler(input);
99
+ if (result.type !== "HandleSuccess") {
100
+ if (result.type === "InputValidationError") {
62
101
  res.status(400);
63
102
  } else {
64
103
  res.status(500);
65
104
  }
66
- // eslint-disable-next-line consistent-return
67
105
  return res.json(result.message);
68
106
  } else {
69
107
  res.status(200);
70
- // eslint-disable-next-line consistent-return
71
108
  return res.json(result.value);
72
109
  }
73
110
  } else {
74
111
  const args = Object.values(input.params).concat(input);
75
112
  try {
76
- const body = await handler(...args);
77
-
78
- // this should never happen
113
+ const body = yield handler(...args);
79
114
  if (res.headersSent) {
80
- // eslint-disable-next-line consistent-return
81
- return await Promise.resolve();
115
+ return yield Promise.resolve();
82
116
  }
83
- if (typeof body !== 'undefined') {
84
- // eslint-disable-next-line consistent-return
117
+ if (typeof body !== "undefined") {
85
118
  return res.json(body);
86
119
  }
87
120
  } catch (e) {
88
- // eslint-disable-next-line consistent-return
89
121
  return next(e);
90
122
  }
91
123
  }
92
- };
93
- Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
124
+ });
125
+ Object.defineProperties(
126
+ apiHandler,
127
+ Object.getOwnPropertyDescriptors(handler)
128
+ );
94
129
  return apiHandler;
95
130
  };
96
- export const isNormalMethod = httpMethod => httpMethods.includes(httpMethod);
97
- const getInputFromRequest = async request => {
131
+ const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
132
+ const getInputFromRequest = (request) => __async(void 0, null, function* () {
98
133
  const draft = {
99
134
  params: request.params,
100
135
  query: request.query,
101
136
  headers: request.headers,
102
137
  cookies: request.headers.cookie
103
138
  };
104
- if (typeIs(request, ['application/json'])) {
139
+ if (typeIs(request, ["application/json"])) {
105
140
  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'])) {
141
+ } else if (typeIs(request, ["multipart/form-data"])) {
142
+ draft.formData = yield resolveFormData(request);
143
+ } else if (typeIs(request, ["application/x-www-form-urlencoded"])) {
109
144
  draft.formUrlencoded = request.body;
110
145
  } else {
111
146
  draft.body = request.body;
112
147
  }
113
148
  return draft;
114
- };
115
- const resolveFormData = request => {
116
- const form = formidable({
117
- multiples: true
118
- });
149
+ });
150
+ const resolveFormData = (request) => {
151
+ const form = formidable({ multiples: true });
119
152
  return new Promise((resolve, reject) => {
120
153
  form.parse(request, (err, fields, files) => {
121
154
  if (err) {
122
155
  reject(err);
123
156
  }
124
- resolve(_objectSpread(_objectSpread({}, fields), files));
157
+ resolve(__spreadValues(__spreadValues({}, fields), files));
125
158
  });
126
159
  });
127
- };
160
+ };
161
+ export {
162
+ createRouteHandler,
163
+ isNormalMethod
164
+ };
@@ -1,37 +1,59 @@
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 stdin_exports = {};
25
+ __export(stdin_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(stdin_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)(
43
+ appContext.internalDirectory,
44
+ "server"
45
+ );
27
46
  const serverRuntimePath = bffExportsUtils.getPath();
28
- const relativeRuntimePath = (0, _bffCore.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
29
- if (process.env.NODE_ENV === 'production') {
47
+ const relativeRuntimePath = (0, import_bff_core.getRelativeRuntimePath)(
48
+ appDirectory,
49
+ serverRuntimePath
50
+ );
51
+ if (process.env.NODE_ENV === "production") {
30
52
  return {
31
53
  source: {
32
54
  alias: {
33
- '@modern-js/runtime/server': relativeRuntimePath,
34
- '@modern-js/runtime/express': relativeRuntimePath
55
+ "@modern-js/runtime/server": relativeRuntimePath,
56
+ "@modern-js/runtime/express": relativeRuntimePath
35
57
  }
36
58
  }
37
59
  };
@@ -39,8 +61,8 @@ var _default = () => ({
39
61
  return {
40
62
  source: {
41
63
  alias: {
42
- '@modern-js/runtime/server': serverRuntimePath,
43
- '@modern-js/runtime/express': serverRuntimePath
64
+ "@modern-js/runtime/server": serverRuntimePath,
65
+ "@modern-js/runtime/express": serverRuntimePath
44
66
  }
45
67
  }
46
68
  };
@@ -48,8 +70,14 @@ var _default = () => ({
48
70
  },
49
71
  addRuntimeExports(input) {
50
72
  const currentFile = bffExportsUtils.getPath();
51
- const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
52
- const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve('express'));
73
+ const relativeRuntimeModulePath = path.relative(
74
+ path.dirname(currentFile),
75
+ runtimeModulePath
76
+ );
77
+ const relativeFramePath = path.relative(
78
+ path.dirname(currentFile),
79
+ require.resolve("express")
80
+ );
53
81
  bffExportsUtils.addExport(`const pluginRuntime = require('${relativeRuntimeModulePath}');
54
82
  const express = require('${relativeFramePath}')
55
83
  module.exports = {
@@ -62,4 +90,3 @@ var _default = () => ({
62
90
  };
63
91
  }
64
92
  });
65
- exports.default = _default;
@@ -1,13 +1,25 @@
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 stdin_exports = {};
19
+ __export(stdin_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 {
9
- run,
10
- useContext
11
- } = (0, _bffCore.createStorage)();
12
- exports.useContext = useContext;
13
- exports.run = run;
23
+ module.exports = __toCommonJS(stdin_exports);
24
+ var import_bff_core = require("@modern-js/bff-core");
25
+ const { run, useContext } = (0, import_bff_core.createStorage)();
@@ -1,23 +1,32 @@
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 stdin_exports = {};
26
+ __export(stdin_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(stdin_exports);
30
+ var import_plugin = __toESM(require("./plugin"));
31
+ __reExport(stdin_exports, require("./context"), module.exports);
32
+ var src_default = import_plugin.default;