melonykit 0.4.0 → 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.
@@ -0,0 +1,5 @@
1
+ export default function debug(error) {
2
+ if (!process.env.DEBUG === 'true') return;
3
+
4
+ console.log('[DEBUG] ', error);
5
+ };
@@ -1,12 +1,13 @@
1
- import { fileURLToPath, pathToFileURL } from 'node:url';
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 = fileURLToPath(new URL(srcFolder, import.meta.url));
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) {
@@ -0,0 +1,40 @@
1
+ import importModules from './_import-modules.js';
2
+
3
+ export default async function importRestEndpoints(
4
+ srcFolder,
5
+ filePathPatterns,
6
+ app
7
+ ) {
8
+ await importModules(srcFolder, filePathPatterns, module => {
9
+ const endpoints = Array.isArray(module.default)
10
+ ? module.default
11
+ : [module.default];
12
+
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
+ };
36
+
37
+ try { return await expressHandler(context); }
38
+ catch (error) { return context.respondError(error); }
39
+ };
40
+ };
@@ -1 +1,3 @@
1
- export { default as importServerModules } from './_import-server-modules.js';
1
+ export { default as importModules } from './_import-modules.js';
2
+ export { default as importRestEndpoints } from './_import-rest-endpoints.js';
3
+ export { default as debug } from './_debug.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Vadim Hermann",
3
3
  "name": "melonykit",
4
- "version": "0.4.0",
4
+ "version": "0.4.2",
5
5
  "type": "module",
6
6
  "description": "Preact based ui and utility functions toolkit",
7
7
  "main": "./dist/index.js",