@modern-js/bff-core 1.0.1-beta.2 → 1.0.1-beta.3

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.
@@ -25,17 +25,6 @@ export function Api(...args) {
25
25
  const validateHandlers = operators.filter(operator => operator.validate).map(operator => operator.validate);
26
26
 
27
27
  async function runner(inputs) {
28
- // 错误组合传给用户哪些信息?
29
- // 取出来所有的 schema,然后做校验
30
- // 包裹中间件,怎么根据不同框架去包裹?
31
- // 需要满足的需求,可以关闭某一样 schema 的校验
32
- // 可以关闭某一个字段的校验
33
- // 可以设置校验函数以替换 zod ?类型怎么搞?
34
- // 全局可配置校验函数
35
- // middleware 多框架实现要支持在路由上配,但这样 middleware 就要在校验之前先执行了
36
- // 简易模式适配怎么处理?
37
- // 不同框架适配,响应怎么处理?都传入 co ntext 给这个函数?
38
- // 如何不依赖 zod 库?支持用户自定义 schema 库? Query,Data 实现可以统一代码,类型好像无解,除非这些函数是创建出来的,保证只有 http 中依赖 zod 吧
39
28
  const executeHelper = {
40
29
  result: null,
41
30
 
@@ -29,13 +29,12 @@ export const generateClient = async ({
29
29
 
30
30
  const apiRouter = new ApiRouter({
31
31
  apiDir,
32
- prefix,
33
- lambdaDir: apiDir
32
+ prefix
34
33
  });
35
34
  const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
36
35
 
37
36
  if (!handlerInfos) {
38
- return Err(`Cannot require module ${resourcePath}`);
37
+ return Err(`generate client error: Cannot require module ${resourcePath}`);
39
38
  }
40
39
 
41
40
  let handlersCode = '';
@@ -60,87 +60,6 @@ export class ApiRouter {
60
60
  this.apiDir = _apiDir;
61
61
  this.lambdaDir = _lambdaDir || this.getLambdaDir(this.apiDir);
62
62
  }
63
- /**
64
- * 如果用户未传入或传入空串,默认为 /api
65
- * 如果传入 /,则 prefix 为 /
66
- */
67
-
68
-
69
- initPrefix(prefix) {
70
- if (prefix === '/') {
71
- return '';
72
- }
73
-
74
- return prefix || '/api';
75
- }
76
-
77
- validateAbsolute(filename, paramsName) {
78
- if (!path.isAbsolute(filename)) {
79
- throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
80
- }
81
- }
82
-
83
- getModuleInfos(filenames) {
84
- return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
85
- }
86
-
87
- getModuleInfo(filename) {
88
- try {
89
- const module = requireHandlerModule(filename);
90
- return {
91
- filename,
92
- module
93
- };
94
- } catch (err) {
95
- if (process.env.NODE_ENV === 'production') {
96
- throw err;
97
- } else {
98
- console.error(err);
99
- return null;
100
- }
101
- }
102
- }
103
-
104
- getHandlerInfos(moduleInfos) {
105
- let apiHandlers = [];
106
- moduleInfos.forEach(moduleInfo => {
107
- const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
108
-
109
- if (handlerInfos) {
110
- apiHandlers = apiHandlers.concat(handlerInfos);
111
- }
112
- });
113
- const sortedHandlers = sortRoutes(apiHandlers);
114
- return sortedHandlers;
115
- }
116
-
117
- getModuleHandlerInfos(moduleInfo) {
118
- const {
119
- module,
120
- filename
121
- } = moduleInfo;
122
- return Object.entries(module).filter(([, handler]) => typeof handler === 'function').map(([key]) => {
123
- const handler = module[key];
124
- const handlerInfo = this.getHandlerInfo(filename, key, handler);
125
- return handlerInfo;
126
- }).filter(handlerInfo => Boolean(handlerInfo));
127
- }
128
-
129
- validateValidApifile(filename) {
130
- if (!this.apiFiles.includes(filename)) {
131
- throw new Error(`The ${filename} is not a valid api file.`);
132
- }
133
- }
134
-
135
- getRoutePath(prefix, routeName) {
136
- const finalRouteName = routeName === '/' ? '' : routeName;
137
-
138
- if (prefix === '' && finalRouteName === '') {
139
- return '/';
140
- }
141
-
142
- return `${prefix}${finalRouteName}`;
143
- }
144
63
 
145
64
  isApiFile(filename) {
146
65
  if (!this.apiFiles.includes(filename)) {
@@ -271,5 +190,86 @@ export class ApiRouter {
271
190
  debug('apiHandlers', apiHandlers);
272
191
  return apiHandlers;
273
192
  }
193
+ /**
194
+ * 如果用户未传入或传入空串,默认为 /api
195
+ * 如果传入 /,则 prefix 为 /
196
+ */
197
+
198
+
199
+ initPrefix(prefix) {
200
+ if (prefix === '/') {
201
+ return '';
202
+ }
203
+
204
+ return prefix || '/api';
205
+ }
206
+
207
+ validateAbsolute(filename, paramsName) {
208
+ if (!path.isAbsolute(filename)) {
209
+ throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
210
+ }
211
+ }
212
+
213
+ getModuleInfos(filenames) {
214
+ return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
215
+ }
216
+
217
+ getModuleInfo(filename) {
218
+ try {
219
+ const module = requireHandlerModule(filename);
220
+ return {
221
+ filename,
222
+ module
223
+ };
224
+ } catch (err) {
225
+ if (process.env.NODE_ENV === 'production') {
226
+ throw err;
227
+ } else {
228
+ console.error(err);
229
+ return null;
230
+ }
231
+ }
232
+ }
233
+
234
+ getHandlerInfos(moduleInfos) {
235
+ let apiHandlers = [];
236
+ moduleInfos.forEach(moduleInfo => {
237
+ const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
238
+
239
+ if (handlerInfos) {
240
+ apiHandlers = apiHandlers.concat(handlerInfos);
241
+ }
242
+ });
243
+ const sortedHandlers = sortRoutes(apiHandlers);
244
+ return sortedHandlers;
245
+ }
246
+
247
+ getModuleHandlerInfos(moduleInfo) {
248
+ const {
249
+ module,
250
+ filename
251
+ } = moduleInfo;
252
+ return Object.entries(module).filter(([, handler]) => typeof handler === 'function').map(([key]) => {
253
+ const handler = module[key];
254
+ const handlerInfo = this.getHandlerInfo(filename, key, handler);
255
+ return handlerInfo;
256
+ }).filter(handlerInfo => Boolean(handlerInfo));
257
+ }
258
+
259
+ validateValidApifile(filename) {
260
+ if (!this.apiFiles.includes(filename)) {
261
+ throw new Error(`The ${filename} is not a valid api file.`);
262
+ }
263
+ }
264
+
265
+ getRoutePath(prefix, routeName) {
266
+ const finalRouteName = routeName === '/' ? '' : routeName;
267
+
268
+ if (prefix === '' && finalRouteName === '') {
269
+ return '/';
270
+ }
271
+
272
+ return `${prefix}${finalRouteName}`;
273
+ }
274
274
 
275
275
  }
@@ -37,17 +37,6 @@ function Api(...args) {
37
37
  const validateHandlers = operators.filter(operator => operator.validate).map(operator => operator.validate);
38
38
 
39
39
  async function runner(inputs) {
40
- // 错误组合传给用户哪些信息?
41
- // 取出来所有的 schema,然后做校验
42
- // 包裹中间件,怎么根据不同框架去包裹?
43
- // 需要满足的需求,可以关闭某一样 schema 的校验
44
- // 可以关闭某一个字段的校验
45
- // 可以设置校验函数以替换 zod ?类型怎么搞?
46
- // 全局可配置校验函数
47
- // middleware 多框架实现要支持在路由上配,但这样 middleware 就要在校验之前先执行了
48
- // 简易模式适配怎么处理?
49
- // 不同框架适配,响应怎么处理?都传入 co ntext 给这个函数?
50
- // 如何不依赖 zod 库?支持用户自定义 schema 库? Query,Data 实现可以统一代码,类型好像无解,除非这些函数是创建出来的,保证只有 http 中依赖 zod 吧
51
40
  const executeHelper = {
52
41
  result: null,
53
42
 
@@ -45,13 +45,12 @@ const generateClient = async ({
45
45
 
46
46
  const apiRouter = new _router.ApiRouter({
47
47
  apiDir,
48
- prefix,
49
- lambdaDir: apiDir
48
+ prefix
50
49
  });
51
50
  const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
52
51
 
53
52
  if (!handlerInfos) {
54
- return (0, _result.Err)(`Cannot require module ${resourcePath}`);
53
+ return (0, _result.Err)(`generate client error: Cannot require module ${resourcePath}`);
55
54
  }
56
55
 
57
56
  let handlersCode = '';
@@ -104,87 +104,6 @@ class ApiRouter {
104
104
  this.apiDir = _apiDir;
105
105
  this.lambdaDir = _lambdaDir || this.getLambdaDir(this.apiDir);
106
106
  }
107
- /**
108
- * 如果用户未传入或传入空串,默认为 /api
109
- * 如果传入 /,则 prefix 为 /
110
- */
111
-
112
-
113
- initPrefix(prefix) {
114
- if (prefix === '/') {
115
- return '';
116
- }
117
-
118
- return prefix || '/api';
119
- }
120
-
121
- validateAbsolute(filename, paramsName) {
122
- if (!_path.default.isAbsolute(filename)) {
123
- throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
124
- }
125
- }
126
-
127
- getModuleInfos(filenames) {
128
- return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
129
- }
130
-
131
- getModuleInfo(filename) {
132
- try {
133
- const module = (0, _utils3.requireHandlerModule)(filename);
134
- return {
135
- filename,
136
- module
137
- };
138
- } catch (err) {
139
- if (process.env.NODE_ENV === 'production') {
140
- throw err;
141
- } else {
142
- console.error(err);
143
- return null;
144
- }
145
- }
146
- }
147
-
148
- getHandlerInfos(moduleInfos) {
149
- let apiHandlers = [];
150
- moduleInfos.forEach(moduleInfo => {
151
- const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
152
-
153
- if (handlerInfos) {
154
- apiHandlers = apiHandlers.concat(handlerInfos);
155
- }
156
- });
157
- const sortedHandlers = (0, _utils3.sortRoutes)(apiHandlers);
158
- return sortedHandlers;
159
- }
160
-
161
- getModuleHandlerInfos(moduleInfo) {
162
- const {
163
- module,
164
- filename
165
- } = moduleInfo;
166
- return Object.entries(module).filter(([, handler]) => typeof handler === 'function').map(([key]) => {
167
- const handler = module[key];
168
- const handlerInfo = this.getHandlerInfo(filename, key, handler);
169
- return handlerInfo;
170
- }).filter(handlerInfo => Boolean(handlerInfo));
171
- }
172
-
173
- validateValidApifile(filename) {
174
- if (!this.apiFiles.includes(filename)) {
175
- throw new Error(`The ${filename} is not a valid api file.`);
176
- }
177
- }
178
-
179
- getRoutePath(prefix, routeName) {
180
- const finalRouteName = routeName === '/' ? '' : routeName;
181
-
182
- if (prefix === '' && finalRouteName === '') {
183
- return '/';
184
- }
185
-
186
- return `${prefix}${finalRouteName}`;
187
- }
188
107
 
189
108
  isApiFile(filename) {
190
109
  if (!this.apiFiles.includes(filename)) {
@@ -316,6 +235,87 @@ class ApiRouter {
316
235
  (0, _utils2.debug)('apiHandlers', apiHandlers);
317
236
  return apiHandlers;
318
237
  }
238
+ /**
239
+ * 如果用户未传入或传入空串,默认为 /api
240
+ * 如果传入 /,则 prefix 为 /
241
+ */
242
+
243
+
244
+ initPrefix(prefix) {
245
+ if (prefix === '/') {
246
+ return '';
247
+ }
248
+
249
+ return prefix || '/api';
250
+ }
251
+
252
+ validateAbsolute(filename, paramsName) {
253
+ if (!_path.default.isAbsolute(filename)) {
254
+ throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
255
+ }
256
+ }
257
+
258
+ getModuleInfos(filenames) {
259
+ return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
260
+ }
261
+
262
+ getModuleInfo(filename) {
263
+ try {
264
+ const module = (0, _utils3.requireHandlerModule)(filename);
265
+ return {
266
+ filename,
267
+ module
268
+ };
269
+ } catch (err) {
270
+ if (process.env.NODE_ENV === 'production') {
271
+ throw err;
272
+ } else {
273
+ console.error(err);
274
+ return null;
275
+ }
276
+ }
277
+ }
278
+
279
+ getHandlerInfos(moduleInfos) {
280
+ let apiHandlers = [];
281
+ moduleInfos.forEach(moduleInfo => {
282
+ const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
283
+
284
+ if (handlerInfos) {
285
+ apiHandlers = apiHandlers.concat(handlerInfos);
286
+ }
287
+ });
288
+ const sortedHandlers = (0, _utils3.sortRoutes)(apiHandlers);
289
+ return sortedHandlers;
290
+ }
291
+
292
+ getModuleHandlerInfos(moduleInfo) {
293
+ const {
294
+ module,
295
+ filename
296
+ } = moduleInfo;
297
+ return Object.entries(module).filter(([, handler]) => typeof handler === 'function').map(([key]) => {
298
+ const handler = module[key];
299
+ const handlerInfo = this.getHandlerInfo(filename, key, handler);
300
+ return handlerInfo;
301
+ }).filter(handlerInfo => Boolean(handlerInfo));
302
+ }
303
+
304
+ validateValidApifile(filename) {
305
+ if (!this.apiFiles.includes(filename)) {
306
+ throw new Error(`The ${filename} is not a valid api file.`);
307
+ }
308
+ }
309
+
310
+ getRoutePath(prefix, routeName) {
311
+ const finalRouteName = routeName === '/' ? '' : routeName;
312
+
313
+ if (prefix === '' && finalRouteName === '') {
314
+ return '/';
315
+ }
316
+
317
+ return `${prefix}${finalRouteName}`;
318
+ }
319
319
 
320
320
  }
321
321
 
@@ -18,6 +18,15 @@ export declare class ApiRouter {
18
18
  lambdaDir?: string;
19
19
  prefix?: string;
20
20
  });
21
+ isApiFile(filename: string): boolean;
22
+ getSingleModuleHandlers(filename: string): APIHandlerInfo[] | null;
23
+ getHandlerInfo(filename: string, originFuncName: string, handler: ApiHandler): APIHandlerInfo | null;
24
+ getSafeRoutePath(filename: string, handler?: ApiHandler): string;
25
+ getRouteName(filename: string, handler?: ApiHandler): string;
26
+ getHttpMethod(originHandlerName: string, handler?: ApiHandler): HttpMethod | null;
27
+ loadApiFiles(): string[];
28
+ getApiFiles(): string[];
29
+ getApiHandlers(): APIHandlerInfo[];
21
30
  /**
22
31
  * 如果用户未传入或传入空串,默认为 /api
23
32
  * 如果传入 /,则 prefix 为 /
@@ -34,13 +43,4 @@ export declare class ApiRouter {
34
43
  private getModuleHandlerInfos;
35
44
  private validateValidApifile;
36
45
  private getRoutePath;
37
- isApiFile(filename: string): boolean;
38
- getSingleModuleHandlers(filename: string): APIHandlerInfo[] | null;
39
- getHandlerInfo(filename: string, originFuncName: string, handler: ApiHandler): APIHandlerInfo | null;
40
- getSafeRoutePath(filename: string, handler?: ApiHandler): string;
41
- getRouteName(filename: string, handler?: ApiHandler): string;
42
- getHttpMethod(originHandlerName: string, handler?: ApiHandler): HttpMethod | null;
43
- loadApiFiles(): string[];
44
- getApiFiles(): string[];
45
- getApiHandlers(): APIHandlerInfo[];
46
46
  }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.0.1-beta.2",
14
+ "version": "1.0.1-beta.3",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -47,6 +47,11 @@
47
47
  "peerDependencies": {
48
48
  "zod": "^3.17.3"
49
49
  },
50
+ "peerDependenciesMeta": {
51
+ "zod": {
52
+ "optional": true
53
+ }
54
+ },
50
55
  "modernConfig": {
51
56
  "output": {
52
57
  "packageMode": "node-js"