@modern-js/bff-core 2.7.0 → 2.7.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.
Files changed (38) hide show
  1. package/dist/js/modern/api.js +69 -0
  2. package/dist/js/modern/client/generate-client.js +79 -0
  3. package/dist/js/modern/client/index.js +1 -0
  4. package/dist/js/modern/client/result.js +22 -0
  5. package/dist/js/modern/errors/http.js +16 -0
  6. package/dist/js/modern/index.js +23 -0
  7. package/dist/js/modern/operators/http.js +210 -0
  8. package/dist/js/modern/router/constants.js +32 -0
  9. package/dist/js/modern/router/index.js +236 -0
  10. package/dist/js/modern/router/types.js +0 -0
  11. package/dist/js/modern/router/utils.js +83 -0
  12. package/dist/js/modern/types.js +45 -0
  13. package/dist/js/modern/utils/alias.js +76 -0
  14. package/dist/js/modern/utils/debug.js +5 -0
  15. package/dist/js/modern/utils/index.js +8 -0
  16. package/dist/js/modern/utils/meta.js +8 -0
  17. package/dist/js/modern/utils/storage.js +42 -0
  18. package/dist/js/modern/utils/validate.js +43 -0
  19. package/dist/js/node/api.js +98 -0
  20. package/dist/js/node/client/generate-client.js +109 -0
  21. package/dist/js/node/client/index.js +17 -0
  22. package/dist/js/node/client/result.js +46 -0
  23. package/dist/js/node/errors/http.js +40 -0
  24. package/dist/js/node/index.js +48 -0
  25. package/dist/js/node/operators/http.js +241 -0
  26. package/dist/js/node/router/constants.js +61 -0
  27. package/dist/js/node/router/index.js +256 -0
  28. package/dist/js/node/router/types.js +15 -0
  29. package/dist/js/node/router/utils.js +116 -0
  30. package/dist/js/node/types.js +73 -0
  31. package/dist/js/node/utils/alias.js +107 -0
  32. package/dist/js/node/utils/debug.js +28 -0
  33. package/dist/js/node/utils/index.js +32 -0
  34. package/dist/js/node/utils/meta.js +32 -0
  35. package/dist/js/node/utils/storage.js +71 -0
  36. package/dist/js/node/utils/validate.js +74 -0
  37. package/dist/types/types.d.ts +1 -1
  38. package/package.json +8 -8
@@ -0,0 +1,69 @@
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 "reflect-metadata";
22
+ import compose from "koa-compose";
23
+ import { validateFunction, HANDLER_WITH_META } from "./utils";
24
+ function Api(...args) {
25
+ const handler = args.pop();
26
+ validateFunction(handler, "Apihandler");
27
+ const operators = args;
28
+ const metadataHelper = {
29
+ getMetadata(key) {
30
+ return Reflect.getMetadata(key, runner);
31
+ },
32
+ setMetadata(key, value) {
33
+ return Reflect.defineMetadata(key, value, runner);
34
+ }
35
+ };
36
+ for (const operator of operators) {
37
+ if (operator.metadata) {
38
+ operator.metadata(metadataHelper);
39
+ }
40
+ }
41
+ const validateHandlers = operators.filter((operator) => operator.validate).map((operator) => operator.validate);
42
+ const pipeHandlers = operators.filter((operator) => operator.execute).map((operator) => operator.execute);
43
+ function runner(inputs) {
44
+ return __async(this, null, function* () {
45
+ const executeHelper = {
46
+ result: null,
47
+ get inputs() {
48
+ return inputs;
49
+ },
50
+ set inputs(val) {
51
+ inputs = val;
52
+ }
53
+ };
54
+ const stack = [...validateHandlers, ...pipeHandlers];
55
+ stack.push((helper, next) => __async(this, null, function* () {
56
+ const res = yield handler(helper.inputs);
57
+ helper.result = res;
58
+ return next();
59
+ }));
60
+ yield compose(stack)(executeHelper);
61
+ return executeHelper.result;
62
+ });
63
+ }
64
+ runner[HANDLER_WITH_META] = true;
65
+ return runner;
66
+ }
67
+ export {
68
+ Api
69
+ };
@@ -0,0 +1,79 @@
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 * as path from "path";
22
+ import { ApiRouter } from "../router";
23
+ import { Ok, Err } from "./result";
24
+ const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
25
+ const generateClient = (_0) => __async(void 0, [_0], function* ({
26
+ resourcePath,
27
+ apiDir,
28
+ prefix,
29
+ port,
30
+ target,
31
+ requestCreator,
32
+ fetcher,
33
+ requireResolve = require.resolve
34
+ }) {
35
+ if (!requestCreator) {
36
+ requestCreator = requireResolve(
37
+ `${DEFAULT_CLIENT_REQUEST_CREATOR}${target ? `/${target}` : ""}`
38
+ ).replace(/\\/g, "/");
39
+ } else {
40
+ let resolvedPath = requestCreator;
41
+ try {
42
+ resolvedPath = path.dirname(requireResolve(requestCreator));
43
+ } catch (error) {
44
+ }
45
+ requestCreator = `${resolvedPath}${target ? `/${target}` : ""}`.replace(
46
+ /\\/g,
47
+ "/"
48
+ );
49
+ }
50
+ const apiRouter = new ApiRouter({
51
+ apiDir,
52
+ prefix
53
+ });
54
+ const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
55
+ if (!handlerInfos) {
56
+ return Err(`generate client error: Cannot require module ${resourcePath}`);
57
+ }
58
+ let handlersCode = "";
59
+ for (const handlerInfo of handlerInfos) {
60
+ const { name, httpMethod, routePath } = handlerInfo;
61
+ let exportStatement = `const ${name} =`;
62
+ if (name.toLowerCase() === "default") {
63
+ exportStatement = "default";
64
+ }
65
+ const upperHttpMethod = httpMethod.toUpperCase();
66
+ const routeName = routePath;
67
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
68
+ `;
69
+ }
70
+ const importCode = `import { createRequest } from '${requestCreator}';
71
+ ${fetcher ? `import { fetch } from '${fetcher}';
72
+ ` : ""}`;
73
+ return Ok(`${importCode}
74
+ ${handlersCode}`);
75
+ });
76
+ export {
77
+ DEFAULT_CLIENT_REQUEST_CREATOR,
78
+ generateClient
79
+ };
@@ -0,0 +1 @@
1
+ export * from "./generate-client";
@@ -0,0 +1,22 @@
1
+ const Err = (value) => {
2
+ const err = {
3
+ kind: "Err",
4
+ value,
5
+ isErr: true,
6
+ isOk: false
7
+ };
8
+ return err;
9
+ };
10
+ const Ok = (value) => {
11
+ const ok = {
12
+ kind: "Ok",
13
+ value,
14
+ isErr: false,
15
+ isOk: true
16
+ };
17
+ return ok;
18
+ };
19
+ export {
20
+ Err,
21
+ Ok
22
+ };
@@ -0,0 +1,16 @@
1
+ class HttpError extends Error {
2
+ constructor(status, message) {
3
+ super(message);
4
+ this.status = status;
5
+ }
6
+ }
7
+ class ValidationError extends HttpError {
8
+ constructor(status, message) {
9
+ super(status, message);
10
+ this.code = "VALIDATION_ERROR";
11
+ }
12
+ }
13
+ export {
14
+ HttpError,
15
+ ValidationError
16
+ };
@@ -0,0 +1,23 @@
1
+ import { Api } from "./api";
2
+ import { HttpError, ValidationError } from "./errors/http";
3
+ export * from "./router";
4
+ export * from "./types";
5
+ export * from "./client";
6
+ export * from "./operators/http";
7
+ import {
8
+ getRelativeRuntimePath,
9
+ HANDLER_WITH_META,
10
+ isWithMetaHandler,
11
+ createStorage,
12
+ registerPaths
13
+ } from "./utils";
14
+ export {
15
+ Api,
16
+ HANDLER_WITH_META,
17
+ HttpError,
18
+ ValidationError,
19
+ createStorage,
20
+ getRelativeRuntimePath,
21
+ isWithMetaHandler,
22
+ registerPaths
23
+ };
@@ -0,0 +1,210 @@
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 {
41
+ HttpMetadata,
42
+ OperatorType,
43
+ HttpMethod,
44
+ TriggerType,
45
+ ResponseMetaType
46
+ } from "../types";
47
+ import { ValidationError } from "../errors/http";
48
+ const validateInput = (schema, input) => __async(void 0, null, function* () {
49
+ try {
50
+ return yield schema.parseAsync(input);
51
+ } catch (error) {
52
+ const { z: zod } = require("zod");
53
+ if (error instanceof zod.ZodError) {
54
+ throw new ValidationError(400, error.message);
55
+ }
56
+ throw error;
57
+ }
58
+ });
59
+ const createHttpOperator = (method) => {
60
+ return (urlPath) => {
61
+ return {
62
+ name: method,
63
+ metadata({ setMetadata }) {
64
+ setMetadata(OperatorType.Trigger, {
65
+ type: TriggerType.Http,
66
+ path: urlPath,
67
+ method
68
+ });
69
+ }
70
+ };
71
+ };
72
+ };
73
+ const Get = createHttpOperator(HttpMethod.Get);
74
+ const Post = createHttpOperator(HttpMethod.Post);
75
+ const Put = createHttpOperator(HttpMethod.Put);
76
+ const Delete = createHttpOperator(HttpMethod.Delete);
77
+ const Connect = createHttpOperator(HttpMethod.Connect);
78
+ const Trace = createHttpOperator(HttpMethod.Trace);
79
+ const Patch = createHttpOperator(HttpMethod.Patch);
80
+ const Option = createHttpOperator(HttpMethod.Option);
81
+ const Head = createHttpOperator(HttpMethod.Head);
82
+ const Data = (schema) => {
83
+ return {
84
+ name: HttpMetadata.Data,
85
+ metadata({ setMetadata }) {
86
+ setMetadata(HttpMetadata.Data, schema);
87
+ },
88
+ validate(helper, next) {
89
+ return __async(this, null, function* () {
90
+ const {
91
+ inputs: { data }
92
+ } = helper;
93
+ helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
94
+ data: yield validateInput(schema, data)
95
+ });
96
+ return next();
97
+ });
98
+ }
99
+ };
100
+ };
101
+ const Query = (schema) => {
102
+ return {
103
+ name: HttpMetadata.Query,
104
+ metadata({ setMetadata }) {
105
+ setMetadata(HttpMetadata.Query, schema);
106
+ },
107
+ validate(helper, next) {
108
+ return __async(this, null, function* () {
109
+ const {
110
+ inputs: { query }
111
+ } = helper;
112
+ helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
113
+ query: yield validateInput(schema, query)
114
+ });
115
+ return next();
116
+ });
117
+ }
118
+ };
119
+ };
120
+ const Params = (schema) => {
121
+ return {
122
+ name: HttpMetadata.Params,
123
+ metadata({ setMetadata }) {
124
+ setMetadata(HttpMetadata.Params, schema);
125
+ },
126
+ validate(helper, next) {
127
+ return __async(this, null, function* () {
128
+ const {
129
+ inputs: { params }
130
+ } = helper;
131
+ helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
132
+ params: yield validateInput(schema, params)
133
+ });
134
+ return next();
135
+ });
136
+ }
137
+ };
138
+ };
139
+ const Headers = (schema) => {
140
+ return {
141
+ name: HttpMetadata.Headers,
142
+ metadata({ setMetadata }) {
143
+ setMetadata(HttpMetadata.Headers, schema);
144
+ },
145
+ validate(helper, next) {
146
+ return __async(this, null, function* () {
147
+ const {
148
+ inputs: { headers }
149
+ } = helper;
150
+ helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
151
+ headers: yield validateInput(schema, headers)
152
+ });
153
+ return next();
154
+ });
155
+ }
156
+ };
157
+ };
158
+ const setResponseMeta = (helper, type, value) => {
159
+ const responseMetaData = helper.getMetadata(HttpMetadata.Response) || [];
160
+ helper.setMetadata(HttpMetadata.Response, [
161
+ ...responseMetaData,
162
+ {
163
+ type,
164
+ value
165
+ }
166
+ ]);
167
+ };
168
+ const HttpCode = (statusCode) => {
169
+ return {
170
+ name: "HttpCode",
171
+ metadata(helper) {
172
+ setResponseMeta(helper, ResponseMetaType.StatusCode, statusCode);
173
+ }
174
+ };
175
+ };
176
+ const SetHeaders = (headers) => {
177
+ return {
178
+ name: "SetHeaders",
179
+ metadata(helper) {
180
+ setResponseMeta(helper, ResponseMetaType.Headers, headers);
181
+ }
182
+ };
183
+ };
184
+ const Redirect = (url) => {
185
+ return {
186
+ name: "Redirect",
187
+ metadata(helper) {
188
+ setResponseMeta(helper, ResponseMetaType.Redirect, url);
189
+ }
190
+ };
191
+ };
192
+ export {
193
+ Connect,
194
+ Data,
195
+ Delete,
196
+ Get,
197
+ Head,
198
+ Headers,
199
+ HttpCode,
200
+ Option,
201
+ Params,
202
+ Patch,
203
+ Post,
204
+ Put,
205
+ Query,
206
+ Redirect,
207
+ SetHeaders,
208
+ Trace,
209
+ createHttpOperator
210
+ };
@@ -0,0 +1,32 @@
1
+ import { HttpMethod } from "../types";
2
+ const AllHttpMethods = Object.values(HttpMethod);
3
+ var APIMode = /* @__PURE__ */ ((APIMode2) => {
4
+ APIMode2["FARMEWORK"] = "framework";
5
+ APIMode2["FUNCTION"] = "function";
6
+ return APIMode2;
7
+ })(APIMode || {});
8
+ const FRAMEWORK_MODE_LAMBDA_DIR = "lambda";
9
+ const FRAMEWORK_MODE_APP_DIR = "app";
10
+ const INDEX_SUFFIX = "index";
11
+ const API_DIR = "api";
12
+ const API_FILE_RULES = [
13
+ "**/*.[tj]s",
14
+ "!**/_*",
15
+ "!**/_*/**/*.[tj]s",
16
+ "!**/*.test.js",
17
+ "!**/*.test.ts",
18
+ "!**/*.d.ts",
19
+ "!__test__/*.ts",
20
+ "!__tests__/*.ts",
21
+ "!node_modules/**",
22
+ "!bootstrap.js"
23
+ ];
24
+ export {
25
+ APIMode,
26
+ API_DIR,
27
+ API_FILE_RULES,
28
+ AllHttpMethods,
29
+ FRAMEWORK_MODE_APP_DIR,
30
+ FRAMEWORK_MODE_LAMBDA_DIR,
31
+ INDEX_SUFFIX
32
+ };
@@ -0,0 +1,236 @@
1
+ import path from "path";
2
+ import { fs, logger } from "@modern-js/utils";
3
+ import "reflect-metadata";
4
+ import { HttpMethod, httpMethods, OperatorType, TriggerType } from "../types";
5
+ import { debug } from "../utils";
6
+ import {
7
+ APIMode,
8
+ FRAMEWORK_MODE_LAMBDA_DIR,
9
+ API_FILE_RULES,
10
+ FRAMEWORK_MODE_APP_DIR
11
+ } from "./constants";
12
+ import {
13
+ getFiles,
14
+ getPathFromFilename,
15
+ requireHandlerModule,
16
+ sortRoutes
17
+ } from "./utils";
18
+ export * from "./types";
19
+ export * from "./constants";
20
+ class ApiRouter {
21
+ constructor({
22
+ apiDir,
23
+ lambdaDir,
24
+ prefix
25
+ }) {
26
+ this.apiFiles = [];
27
+ this.getExactApiMode = (apiDir) => {
28
+ const exist = this.createExistChecker(apiDir);
29
+ const existLambdaDir = exist(FRAMEWORK_MODE_LAMBDA_DIR);
30
+ const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
31
+ const existAppFile = exist("app.ts") || exist("app.js");
32
+ if (existLambdaDir || existAppDir || existAppFile) {
33
+ return APIMode.FARMEWORK;
34
+ }
35
+ return APIMode.FUNCTION;
36
+ };
37
+ this.createExistChecker = (base) => (target) => fs.pathExistsSync(path.resolve(base, target));
38
+ this.getExactLambdaDir = (apiDir) => {
39
+ if (this.lambdaDir) {
40
+ return this.lambdaDir;
41
+ }
42
+ const lambdaDir = this.apiMode === APIMode.FARMEWORK ? path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
43
+ return lambdaDir;
44
+ };
45
+ this.validateAbsolute(apiDir, "apiDir");
46
+ this.validateAbsolute(lambdaDir, "lambdaDir");
47
+ this.prefix = this.initPrefix(prefix);
48
+ this.apiDir = apiDir;
49
+ this.apiMode = this.getExactApiMode(apiDir);
50
+ this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
51
+ this.existLambdaDir = fs.existsSync(this.lambdaDir);
52
+ }
53
+ isExistLambda() {
54
+ return this.existLambdaDir;
55
+ }
56
+ getApiMode() {
57
+ return this.apiMode;
58
+ }
59
+ getLambdaDir() {
60
+ return this.lambdaDir;
61
+ }
62
+ isApiFile(filename) {
63
+ if (this.existLambdaDir) {
64
+ return false;
65
+ }
66
+ if (!this.apiFiles.includes(filename)) {
67
+ return false;
68
+ }
69
+ return true;
70
+ }
71
+ getSingleModuleHandlers(filename) {
72
+ const moduleInfo = this.getModuleInfo(filename);
73
+ if (moduleInfo) {
74
+ return this.getModuleHandlerInfos(moduleInfo);
75
+ }
76
+ return null;
77
+ }
78
+ getHandlerInfo(filename, originFuncName, handler) {
79
+ const httpMethod = this.getHttpMethod(originFuncName, handler);
80
+ const routeName = this.getRouteName(filename, handler);
81
+ if (httpMethod && routeName) {
82
+ return {
83
+ handler,
84
+ name: originFuncName,
85
+ httpMethod,
86
+ routeName,
87
+ filename,
88
+ routePath: this.getRoutePath(this.prefix, routeName)
89
+ };
90
+ }
91
+ return null;
92
+ }
93
+ getSafeRoutePath(filename, handler) {
94
+ this.loadApiFiles();
95
+ this.validateValidApifile(filename);
96
+ return this.getRouteName(filename, handler);
97
+ }
98
+ getRouteName(filename, handler) {
99
+ if (handler) {
100
+ const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
101
+ if (trigger && trigger.type === TriggerType.Http) {
102
+ if (!trigger.path) {
103
+ throw new Error(
104
+ `The http trigger ${trigger.name} needs to specify a path`
105
+ );
106
+ }
107
+ return trigger.path;
108
+ }
109
+ }
110
+ const routePath = getPathFromFilename(this.lambdaDir, filename);
111
+ return routePath;
112
+ }
113
+ getHttpMethod(originHandlerName, handler) {
114
+ if (handler) {
115
+ const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
116
+ if (trigger && httpMethods.includes(trigger.method)) {
117
+ return trigger.method;
118
+ }
119
+ }
120
+ const upperName = originHandlerName.toUpperCase();
121
+ switch (upperName) {
122
+ case "GET":
123
+ return HttpMethod.Get;
124
+ case "POST":
125
+ return HttpMethod.Post;
126
+ case "PUT":
127
+ return HttpMethod.Put;
128
+ case "DELETE":
129
+ case "DEL":
130
+ return HttpMethod.Delete;
131
+ case "CONNECT":
132
+ return HttpMethod.Connect;
133
+ case "TRACE":
134
+ return HttpMethod.Trace;
135
+ case "PATCH":
136
+ return HttpMethod.Patch;
137
+ case "OPTION":
138
+ return HttpMethod.Option;
139
+ case "DEFAULT": {
140
+ return HttpMethod.Get;
141
+ }
142
+ default:
143
+ logger.warn(
144
+ `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
145
+ );
146
+ return null;
147
+ }
148
+ }
149
+ loadApiFiles() {
150
+ if (!this.existLambdaDir) {
151
+ return [];
152
+ }
153
+ const apiFiles = this.apiFiles = getFiles(this.lambdaDir, API_FILE_RULES);
154
+ return apiFiles;
155
+ }
156
+ getApiFiles() {
157
+ if (!this.existLambdaDir) {
158
+ return [];
159
+ }
160
+ if (this.apiFiles.length > 0) {
161
+ return this.apiFiles;
162
+ }
163
+ return this.loadApiFiles();
164
+ }
165
+ getApiHandlers() {
166
+ const filenames = this.getApiFiles();
167
+ const moduleInfos = this.getModuleInfos(filenames);
168
+ const apiHandlers = this.getHandlerInfos(moduleInfos);
169
+ debug("apiHandlers", apiHandlers.length, apiHandlers);
170
+ return apiHandlers;
171
+ }
172
+ initPrefix(prefix) {
173
+ if (prefix === "/") {
174
+ return "";
175
+ }
176
+ return prefix || "/api";
177
+ }
178
+ validateAbsolute(filename, paramsName) {
179
+ if (typeof filename === "string" && !path.isAbsolute(filename)) {
180
+ throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
181
+ }
182
+ }
183
+ getModuleInfos(filenames) {
184
+ return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
185
+ }
186
+ getModuleInfo(filename) {
187
+ try {
188
+ const module = requireHandlerModule(filename);
189
+ return {
190
+ filename,
191
+ module
192
+ };
193
+ } catch (err) {
194
+ if (process.env.NODE_ENV === "production") {
195
+ throw err;
196
+ } else {
197
+ console.error(err);
198
+ return null;
199
+ }
200
+ }
201
+ }
202
+ getHandlerInfos(moduleInfos) {
203
+ let apiHandlers = [];
204
+ moduleInfos.forEach((moduleInfo) => {
205
+ const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
206
+ if (handlerInfos) {
207
+ apiHandlers = apiHandlers.concat(handlerInfos);
208
+ }
209
+ });
210
+ const sortedHandlers = sortRoutes(apiHandlers);
211
+ return sortedHandlers;
212
+ }
213
+ getModuleHandlerInfos(moduleInfo) {
214
+ const { module, filename } = moduleInfo;
215
+ return Object.entries(module).filter(([, handler]) => typeof handler === "function").map(([key]) => {
216
+ const handler = module[key];
217
+ const handlerInfo = this.getHandlerInfo(filename, key, handler);
218
+ return handlerInfo;
219
+ }).filter((handlerInfo) => Boolean(handlerInfo));
220
+ }
221
+ validateValidApifile(filename) {
222
+ if (!this.apiFiles.includes(filename)) {
223
+ throw new Error(`The ${filename} is not a valid api file.`);
224
+ }
225
+ }
226
+ getRoutePath(prefix, routeName) {
227
+ const finalRouteName = routeName === "/" ? "" : routeName;
228
+ if (prefix === "" && finalRouteName === "") {
229
+ return "/";
230
+ }
231
+ return `${prefix}${finalRouteName}`;
232
+ }
233
+ }
234
+ export {
235
+ ApiRouter
236
+ };
File without changes