melonykit 0.4.1 → 0.4.2
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,12 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
2
3
|
import { globSync } from 'glob';
|
|
3
4
|
|
|
4
5
|
export default async function importModules(
|
|
5
|
-
srcFolder,
|
|
6
|
+
srcFolder = 'src',
|
|
6
7
|
filePathPatterns,
|
|
7
8
|
callback
|
|
8
9
|
) {
|
|
9
|
-
const cwd =
|
|
10
|
+
const cwd = resolve(process.cwd(), srcFolder);
|
|
10
11
|
const filePaths = globSync(filePathPatterns, { cwd, absolute: true });
|
|
11
12
|
|
|
12
13
|
for (const filePath of filePaths) {
|
|
@@ -1,17 +1,40 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { globSync } from 'glob';
|
|
1
|
+
import importModules from './_import-modules.js';
|
|
3
2
|
|
|
4
|
-
export default async function
|
|
3
|
+
export default async function importRestEndpoints(
|
|
5
4
|
srcFolder,
|
|
6
5
|
filePathPatterns,
|
|
7
|
-
|
|
6
|
+
app
|
|
8
7
|
) {
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
await importModules(srcFolder, filePathPatterns, module => {
|
|
9
|
+
const endpoints = Array.isArray(module.default)
|
|
10
|
+
? module.default
|
|
11
|
+
: [module.default];
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
for (const endpoint of endpoints) {
|
|
14
|
+
app[endpoint.method](
|
|
15
|
+
endpoint.path,
|
|
16
|
+
_createEndpointHandler(endpoint.handler)
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// ----------------------------------------------------------------------------- Private
|
|
23
|
+
function _createEndpointHandler(expressHandler) {
|
|
24
|
+
return async function handler(req, res) {
|
|
25
|
+
const context = {
|
|
26
|
+
req,
|
|
27
|
+
res,
|
|
28
|
+
respondSuccess(data = {}, statusCode = 200) {
|
|
29
|
+
return res.status(statusCode).json(data);
|
|
30
|
+
},
|
|
31
|
+
respondError(error, statusCode = 500) {
|
|
32
|
+
debug(error);
|
|
33
|
+
return res.status(statusCode).json(null);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
14
36
|
|
|
15
|
-
await
|
|
16
|
-
|
|
37
|
+
try { return await expressHandler(context); }
|
|
38
|
+
catch (error) { return context.respondError(error); }
|
|
39
|
+
};
|
|
17
40
|
};
|