minimonolith 0.2.12 → 0.3.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "minimonolith",
3
3
  "type": "module",
4
- "version": "0.2.12",
4
+ "version": "0.3.0",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -0,0 +1,14 @@
1
+ const getUpperSnakeCase = str => {
2
+ return str
3
+ .replace(/([a-z])([A-Z])/g, '$1_$2')
4
+ .replace(/([A-Z])([A-Z][a-z])/g, '$1_$2')
5
+ .toUpperCase();
6
+ }
7
+
8
+ const getMethodRouteCode = (SERVICE_NAME, METHOD_NAME) => {
9
+ const serviceName = getUpperSnakeCase(SERVICE_NAME);
10
+ const methodName = getUpperSnakeCase(METHOD_NAME);
11
+ return serviceName + '_' + methodName;
12
+ }
13
+
14
+ export { getMethodRouteCode };
@@ -0,0 +1,12 @@
1
+ const methodsAvailable = ['post', 'get', 'patch', 'put', 'delete'];
2
+
3
+ const getMethodType = METHOD_NAME => {
4
+ const methodMatched = METHOD_NAME.match(/^[a-z]*/);
5
+ if (!methodMatched || !methodsAvailable.includes(methodMatched[0])) {
6
+ console.log('methodMatched "'+methodMatched[0]+'"');
7
+ throw new Error('UNKNOWN_METHOD_TYPE: ' + METHOD_NAME);
8
+ }
9
+ return methodMatched[0];
10
+ }
11
+
12
+ export { getMethodType };
@@ -3,6 +3,8 @@ import url from 'url'
3
3
  import { getProjectRoot } from '../pathHandler/index.js';
4
4
  import { registerModel } from '../databaseHandler/index.js';
5
5
  import { methodHandler } from './methodHandler.js';
6
+ import { getMethodType } from './getMethodType.js';
7
+ import { getMethodRouteCode } from './getMethodRouteCode.js';
6
8
  import { registerMethods } from './registerMethods.js';
7
9
 
8
10
  const serviceHandler = async (API, SERVICE_NAME, SRC_FOLDER='', MODULE_FOLDER='node_modules/') => {
@@ -17,8 +19,9 @@ const serviceHandler = async (API, SERVICE_NAME, SRC_FOLDER='', MODULE_FOLDER='n
17
19
  const SERVICE_METHODS = await registerMethods(SERVICE_MODULE.methods)(SERVICE_URL);
18
20
 
19
21
  Object.keys(SERVICE_METHODS).forEach(METHOD_NAME => {
20
- API[METHOD_NAME](`/${SERVICE_NAME}/${METHOD_NAME}`, async (req, res) => {
21
- const ROUTE_CODE = SERVICE_NAME.toUpperCase()+'_'+METHOD_NAME.toUpperCase();
22
+ const methodType = getMethodType(METHOD_NAME);
23
+ API[methodType](`/${SERVICE_NAME}/${METHOD_NAME}`, async (req, res) => {
24
+ const ROUTE_CODE = getMethodRouteCode(SERVICE_NAME, METHOD_NAME);
22
25
  const { statusCode, body } =
23
26
  await methodHandler(SERVICE_METHODS[METHOD_NAME], ROUTE_CODE, req);
24
27
  res.status(statusCode).json(body);