@modern-js/plugin-bff 2.5.0 → 2.5.1-alpha.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/dist/cjs/cli.js CHANGED
@@ -42,6 +42,8 @@ var cli_default = () => ({
42
42
  return import_utils.PLUGIN_SCHEMAS["@modern-js/plugin-bff"];
43
43
  },
44
44
  config() {
45
+ const configContext = api.useConfigContext();
46
+ console.log("22222222222222222", configContext.bff);
45
47
  return {
46
48
  tools: {
47
49
  webpackChain: (chain, { name, CHAIN_ID }) => {
@@ -49,11 +51,13 @@ var cli_default = () => ({
49
51
  const modernConfig = api.useResolvedConfigContext();
50
52
  const { bff } = modernConfig || {};
51
53
  const prefix = (bff == null ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
54
+ const { httpMethodDecider } = bff;
52
55
  const rootDir = import_path.default.resolve(appDirectory, import_utils.API_DIR);
53
56
  chain.resolve.alias.set("@api", rootDir);
54
57
  const apiRouter = new import_bff_core.ApiRouter({
55
58
  apiDir: rootDir,
56
- prefix
59
+ prefix,
60
+ httpMethodDecider
57
61
  });
58
62
  const lambdaDir = apiRouter.getLambdaDir();
59
63
  const existLambda = apiRouter.isExistLambda();
@@ -67,7 +71,8 @@ var cli_default = () => ({
67
71
  lambdaDir,
68
72
  existLambda,
69
73
  port,
70
- target: name
74
+ target: name,
75
+ httpMethodDecider
71
76
  });
72
77
  }
73
78
  },
@@ -39,7 +39,8 @@ async function loader(source) {
39
39
  target: draftOptions.target,
40
40
  port: Number(draftOptions.port),
41
41
  source,
42
- resourcePath
42
+ resourcePath,
43
+ httpMethodDecider: draftOptions.httpMethodDecider
43
44
  };
44
45
  const { lambdaDir } = draftOptions;
45
46
  if (!resourcePath.startsWith(lambdaDir)) {
@@ -73,12 +73,13 @@ var server_default = () => ({
73
73
  });
74
74
  },
75
75
  prepareApiServer(props, next) {
76
- const { pwd, prefix } = props;
76
+ const { pwd, prefix, httpMethodDecider } = props;
77
77
  const apiDir = import_path.default.resolve(pwd, import_utils.API_DIR);
78
78
  const appContext = api.useAppContext();
79
79
  const apiRouter = new import_bff_core.ApiRouter({
80
80
  apiDir,
81
- prefix
81
+ prefix,
82
+ httpMethodDecider
82
83
  });
83
84
  const apiMode = apiRouter.getApiMode();
84
85
  const apiHandlerInfos = apiRouter.getApiHandlers();
package/dist/esm/cli.js CHANGED
@@ -216,6 +216,8 @@ var cli_default = function() {
216
216
  return PLUGIN_SCHEMAS["@modern-js/plugin-bff"];
217
217
  },
218
218
  config: function config() {
219
+ var configContext = api.useConfigContext();
220
+ console.log("22222222222222222", configContext.bff);
219
221
  return {
220
222
  tools: {
221
223
  webpackChain: function(chain, param) {
@@ -224,11 +226,13 @@ var cli_default = function() {
224
226
  var modernConfig = api.useResolvedConfigContext();
225
227
  var bff = (modernConfig || {}).bff;
226
228
  var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
229
+ var httpMethodDecider = bff.httpMethodDecider;
227
230
  var rootDir = path.resolve(appDirectory, API_DIR);
228
231
  chain.resolve.alias.set("@api", rootDir);
229
232
  var apiRouter = new ApiRouter({
230
233
  apiDir: rootDir,
231
- prefix: prefix
234
+ prefix: prefix,
235
+ httpMethodDecider: httpMethodDecider
232
236
  });
233
237
  var lambdaDir = apiRouter.getLambdaDir();
234
238
  var existLambda = apiRouter.isExistLambda();
@@ -240,7 +244,8 @@ var cli_default = function() {
240
244
  lambdaDir: lambdaDir,
241
245
  existLambda: existLambda,
242
246
  port: port,
243
- target: name
247
+ target: name,
248
+ httpMethodDecider: httpMethodDecider
244
249
  });
245
250
  }
246
251
  },
@@ -151,7 +151,8 @@ function _loader() {
151
151
  target: draftOptions.target,
152
152
  port: Number(draftOptions.port),
153
153
  source: source,
154
- resourcePath: resourcePath
154
+ resourcePath: resourcePath,
155
+ httpMethodDecider: draftOptions.httpMethodDecider
155
156
  };
156
157
  lambdaDir = draftOptions.lambdaDir;
157
158
  if (!resourcePath.startsWith(lambdaDir)) {
@@ -128,12 +128,13 @@ var server_default = function() {
128
128
  });
129
129
  },
130
130
  prepareApiServer: function prepareApiServer(props, next) {
131
- var pwd = props.pwd, prefix = props.prefix;
131
+ var pwd = props.pwd, prefix = props.prefix, httpMethodDecider = props.httpMethodDecider;
132
132
  var apiDir = path.resolve(pwd, API_DIR);
133
133
  var appContext = api.useAppContext();
134
134
  var apiRouter = new ApiRouter({
135
135
  apiDir: apiDir,
136
- prefix: prefix
136
+ prefix: prefix,
137
+ httpMethodDecider: httpMethodDecider
137
138
  });
138
139
  var apiMode = apiRouter.getApiMode();
139
140
  var apiHandlerInfos = apiRouter.getApiHandlers();
@@ -21,6 +21,8 @@ var cli_default = () => ({
21
21
  return PLUGIN_SCHEMAS["@modern-js/plugin-bff"];
22
22
  },
23
23
  config() {
24
+ const configContext = api.useConfigContext();
25
+ console.log("22222222222222222", configContext.bff);
24
26
  return {
25
27
  tools: {
26
28
  webpackChain: (chain, { name, CHAIN_ID }) => {
@@ -28,11 +30,13 @@ var cli_default = () => ({
28
30
  const modernConfig = api.useResolvedConfigContext();
29
31
  const { bff } = modernConfig || {};
30
32
  const prefix = (bff == null ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
33
+ const { httpMethodDecider } = bff;
31
34
  const rootDir = path.resolve(appDirectory, API_DIR);
32
35
  chain.resolve.alias.set("@api", rootDir);
33
36
  const apiRouter = new ApiRouter({
34
37
  apiDir: rootDir,
35
- prefix
38
+ prefix,
39
+ httpMethodDecider
36
40
  });
37
41
  const lambdaDir = apiRouter.getLambdaDir();
38
42
  const existLambda = apiRouter.isExistLambda();
@@ -46,7 +50,8 @@ var cli_default = () => ({
46
50
  lambdaDir,
47
51
  existLambda,
48
52
  port,
49
- target: name
53
+ target: name,
54
+ httpMethodDecider
50
55
  });
51
56
  }
52
57
  },
@@ -17,7 +17,8 @@ async function loader(source) {
17
17
  target: draftOptions.target,
18
18
  port: Number(draftOptions.port),
19
19
  source,
20
- resourcePath
20
+ resourcePath,
21
+ httpMethodDecider: draftOptions.httpMethodDecider
21
22
  };
22
23
  const { lambdaDir } = draftOptions;
23
24
  if (!resourcePath.startsWith(lambdaDir)) {
@@ -45,12 +45,13 @@ var server_default = () => ({
45
45
  });
46
46
  },
47
47
  prepareApiServer(props, next) {
48
- const { pwd, prefix } = props;
48
+ const { pwd, prefix, httpMethodDecider } = props;
49
49
  const apiDir = path.resolve(pwd, API_DIR);
50
50
  const appContext = api.useAppContext();
51
51
  const apiRouter = new ApiRouter({
52
52
  apiDir,
53
- prefix
53
+ prefix,
54
+ httpMethodDecider
54
55
  });
55
56
  const apiMode = apiRouter.getApiMode();
56
57
  const apiHandlerInfos = apiRouter.getApiHandlers();
@@ -0,0 +1,184 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __async = (__this, __arguments, generator) => {
21
+ return new Promise((resolve, reject) => {
22
+ var fulfilled = (value) => {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ };
29
+ var rejected = (value) => {
30
+ try {
31
+ step(generator.throw(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ };
36
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
+ step((generator = generator.apply(__this, __arguments)).next());
38
+ });
39
+ };
40
+ import path from "path";
41
+ import {
42
+ fs,
43
+ API_DIR,
44
+ PLUGIN_SCHEMAS,
45
+ normalizeOutputPath,
46
+ SHARED_DIR,
47
+ isProd
48
+ } from "@modern-js/utils";
49
+ import { compile } from "@modern-js/server-utils";
50
+ import { ApiRouter } from "@modern-js/bff-core";
51
+ import { registerModernRuntimePath } from "./helper";
52
+ const DEFAULT_API_PREFIX = "/api";
53
+ const TS_CONFIG_FILENAME = "tsconfig.json";
54
+ var cli_default = () => ({
55
+ name: "@modern-js/plugin-bff",
56
+ setup: (api) => {
57
+ let unRegisterResolveRuntimePath = null;
58
+ return {
59
+ validateSchema() {
60
+ return PLUGIN_SCHEMAS["@modern-js/plugin-bff"];
61
+ },
62
+ config() {
63
+ return {
64
+ tools: {
65
+ webpackChain: (chain, { name, CHAIN_ID }) => {
66
+ const { appDirectory, port } = api.useAppContext();
67
+ const modernConfig = api.useResolvedConfigContext();
68
+ const { bff } = modernConfig || {};
69
+ const prefix = (bff == null ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
70
+ const rootDir = path.resolve(appDirectory, API_DIR);
71
+ chain.resolve.alias.set("@api", rootDir);
72
+ const apiRouter = new ApiRouter({
73
+ apiDir: rootDir,
74
+ prefix
75
+ });
76
+ const lambdaDir = apiRouter.getLambdaDir();
77
+ const existLambda = apiRouter.isExistLambda();
78
+ const apiRegexp = new RegExp(
79
+ normalizeOutputPath(`${rootDir}${path.sep}.*(.[tj]s)$`)
80
+ );
81
+ chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(apiRegexp);
82
+ chain.module.rule(CHAIN_ID.RULE.JS_BFF_API).test(apiRegexp).use("custom-loader").loader(require.resolve("./loader").replace(/\\/g, "/")).options({
83
+ prefix,
84
+ apiDir: rootDir,
85
+ lambdaDir,
86
+ existLambda,
87
+ port,
88
+ target: name
89
+ });
90
+ }
91
+ },
92
+ source: {
93
+ moduleScopes: [`./${API_DIR}`, /create-request/]
94
+ }
95
+ };
96
+ },
97
+ modifyServerRoutes({ routes }) {
98
+ const modernConfig = api.useResolvedConfigContext();
99
+ const { bff } = modernConfig || {};
100
+ const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
101
+ const prefixList = [];
102
+ if (Array.isArray(prefix)) {
103
+ prefixList.push(...prefix);
104
+ } else {
105
+ prefixList.push(prefix);
106
+ }
107
+ const apiServerRoutes = prefixList.map((pre) => ({
108
+ urlPath: pre,
109
+ isApi: true,
110
+ entryPath: "",
111
+ isSPA: false,
112
+ isSSR: false
113
+ }));
114
+ if (bff == null ? void 0 : bff.enableHandleWeb) {
115
+ return {
116
+ routes: routes.map((route) => {
117
+ return __spreadProps(__spreadValues({}, route), {
118
+ isApi: true
119
+ });
120
+ }).concat(apiServerRoutes)
121
+ };
122
+ }
123
+ return { routes: routes.concat(apiServerRoutes) };
124
+ },
125
+ collectServerPlugins({ plugins }) {
126
+ plugins.push({
127
+ "@modern-js/plugin-bff": "@modern-js/plugin-bff/server"
128
+ });
129
+ return { plugins };
130
+ },
131
+ beforeBuild() {
132
+ return __async(this, null, function* () {
133
+ if (isProd()) {
134
+ const { internalDirectory } = api.useAppContext();
135
+ unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
136
+ }
137
+ });
138
+ },
139
+ afterBuild() {
140
+ return __async(this, null, function* () {
141
+ if (unRegisterResolveRuntimePath) {
142
+ unRegisterResolveRuntimePath();
143
+ }
144
+ const { appDirectory, distDirectory } = api.useAppContext();
145
+ const modernConfig = api.useResolvedConfigContext();
146
+ const distDir = path.resolve(distDirectory);
147
+ const apiDir = path.resolve(appDirectory, API_DIR);
148
+ const sharedDir = path.resolve(appDirectory, SHARED_DIR);
149
+ const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
150
+ const sourceDirs = [];
151
+ if (fs.existsSync(apiDir)) {
152
+ sourceDirs.push(apiDir);
153
+ }
154
+ if (fs.existsSync(sharedDir)) {
155
+ sourceDirs.push(sharedDir);
156
+ }
157
+ const { server } = modernConfig;
158
+ const { alias, define, globalVars } = modernConfig.source;
159
+ const { babel } = modernConfig.tools;
160
+ if (sourceDirs.length > 0) {
161
+ yield compile(
162
+ appDirectory,
163
+ {
164
+ server,
165
+ alias,
166
+ define,
167
+ globalVars,
168
+ babelConfig: babel
169
+ },
170
+ {
171
+ sourceDirs,
172
+ distDir,
173
+ tsconfigPath
174
+ }
175
+ );
176
+ }
177
+ });
178
+ }
179
+ };
180
+ }
181
+ });
182
+ export {
183
+ cli_default as default
184
+ };
@@ -0,0 +1,14 @@
1
+ const API_APP_NAME = "_app";
2
+ const BUILD_FILES = [
3
+ "**/*.[tj]sx?",
4
+ "!**/*.test.jsx?",
5
+ "!**/*.test.tsx?",
6
+ "!**/*.spec.jsx?",
7
+ "!**/*.spec.tsx?",
8
+ "!__tests__/*.tsx?",
9
+ "!__tests__/*.jsx?"
10
+ ];
11
+ export {
12
+ API_APP_NAME,
13
+ BUILD_FILES
14
+ };
@@ -0,0 +1,14 @@
1
+ import * as path from "path";
2
+ import { registerPaths } from "@modern-js/bff-core";
3
+ const serverRuntimeAlias = "@modern-js/runtime/server";
4
+ const serverRuntimePath = ".runtime-exports/server";
5
+ const registerModernRuntimePath = (internalDirectory) => {
6
+ const paths = {
7
+ [serverRuntimeAlias]: path.join(internalDirectory, serverRuntimePath)
8
+ };
9
+ const unRegister = registerPaths(paths);
10
+ return unRegister;
11
+ };
12
+ export {
13
+ registerModernRuntimePath
14
+ };
@@ -0,0 +1 @@
1
+ export * from "./constants";
@@ -0,0 +1,67 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import { generateClient } from "@modern-js/bff-core";
22
+ import { logger } from "@modern-js/utils";
23
+ function loader(source) {
24
+ return __async(this, null, function* () {
25
+ this.cacheable();
26
+ const callback = this.async();
27
+ const draftOptions = this.getOptions();
28
+ const { resourcePath } = this;
29
+ const warning = `The file ${resourcePath} is not allowd to be imported in src directory, only API definition files are allowed.`;
30
+ if (!draftOptions.existLambda) {
31
+ logger.warn(warning);
32
+ callback(null, `throw new Error('${warning}')`);
33
+ return;
34
+ }
35
+ const options = {
36
+ prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
37
+ apiDir: draftOptions.apiDir,
38
+ target: draftOptions.target,
39
+ port: Number(draftOptions.port),
40
+ source,
41
+ resourcePath
42
+ };
43
+ const { lambdaDir } = draftOptions;
44
+ if (!resourcePath.startsWith(lambdaDir)) {
45
+ logger.warn(warning);
46
+ callback(null, `throw new Error('${warning}')`);
47
+ return;
48
+ }
49
+ if (draftOptions.fetcher) {
50
+ options.fetcher = draftOptions.fetcher;
51
+ }
52
+ if (draftOptions.requestCreator) {
53
+ options.requestCreator = draftOptions.requestCreator;
54
+ }
55
+ options.requireResolve = require.resolve;
56
+ const result = yield generateClient(options);
57
+ if (result.isOk) {
58
+ callback(void 0, result.value);
59
+ } else {
60
+ callback(void 0, `throw new Error('${result.value}')`);
61
+ }
62
+ });
63
+ }
64
+ var loader_default = loader;
65
+ export {
66
+ loader_default as default
67
+ };
@@ -0,0 +1,88 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ import path from "path";
21
+ import { ApiRouter } from "@modern-js/bff-core";
22
+ import { API_DIR, isProd, requireExistModule } from "@modern-js/utils";
23
+ import { API_APP_NAME } from "./constants";
24
+ class Storage {
25
+ constructor() {
26
+ this.middlewares = [];
27
+ }
28
+ reset() {
29
+ this.middlewares = [];
30
+ }
31
+ }
32
+ const createTransformAPI = (storage) => ({
33
+ addMiddleware(fn) {
34
+ storage.middlewares.push(fn);
35
+ }
36
+ });
37
+ var server_default = () => ({
38
+ name: "@modern-js/plugin-bff",
39
+ setup: (api) => {
40
+ const storage = new Storage();
41
+ const transformAPI = createTransformAPI(storage);
42
+ let apiAppPath = "";
43
+ return {
44
+ prepare() {
45
+ const { appDirectory, distDirectory } = api.useAppContext();
46
+ const root = isProd() ? distDirectory : appDirectory;
47
+ const apiPath = path.resolve(root || process.cwd(), API_DIR);
48
+ apiAppPath = path.resolve(apiPath, API_APP_NAME);
49
+ const apiMod = requireExistModule(apiAppPath);
50
+ if (apiMod && typeof apiMod === "function") {
51
+ apiMod(transformAPI);
52
+ }
53
+ },
54
+ reset() {
55
+ storage.reset();
56
+ const newApiModule = requireExistModule(apiAppPath);
57
+ if (newApiModule && typeof newApiModule === "function") {
58
+ newApiModule(transformAPI);
59
+ }
60
+ },
61
+ gather({ addAPIMiddleware }) {
62
+ storage.middlewares.forEach((mid) => {
63
+ addAPIMiddleware(mid);
64
+ });
65
+ },
66
+ prepareApiServer(props, next) {
67
+ const { pwd, prefix } = props;
68
+ const apiDir = path.resolve(pwd, API_DIR);
69
+ const appContext = api.useAppContext();
70
+ const apiRouter = new ApiRouter({
71
+ apiDir,
72
+ prefix
73
+ });
74
+ const apiMode = apiRouter.getApiMode();
75
+ const apiHandlerInfos = apiRouter.getApiHandlers();
76
+ api.setAppContext(__spreadProps(__spreadValues({}, appContext), {
77
+ apiRouter,
78
+ apiHandlerInfos,
79
+ apiMode
80
+ }));
81
+ return next(props);
82
+ }
83
+ };
84
+ }
85
+ });
86
+ export {
87
+ server_default as default
88
+ };