minimonolith 0.2.12 → 0.4.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/index.js +2 -5
- package/package.json +1 -1
- package/src/apiHandler/createAPI.js +12 -0
- package/src/databaseHandler/modelsHandler.js +4 -1
- package/src/serviceHandler/getMethodRouteCode.js +14 -0
- package/src/serviceHandler/getMethodType.js +12 -0
- package/src/serviceHandler/serviceHandler.js +6 -3
- package/todoList/delete/valid.js +8 -0
- package/todoList/get/valid.js +6 -0
- package/todoList/index.js +5 -0
- package/todoList/model.js +17 -0
- package/todoList/post/valid.js +8 -0
package/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { healthHandler } from './src/healthHandler/index.js';
|
|
5
|
-
import { createAPI, apiHandler } from './src/apiHandler/index.js';
|
|
3
|
+
import { createAPI } from './src/apiHandler/index.js';
|
|
6
4
|
import { runLambdaServer, loadEnvVariables } from './src/lambdaServer/index.js';
|
|
7
5
|
|
|
8
|
-
export { runLambdaServer, loadEnvVariables,
|
|
9
|
-
createAPI, apiHandler, serviceHandler, healthHandler, z };
|
|
6
|
+
export { runLambdaServer, loadEnvVariables, createAPI, z };
|
package/package.json
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import lambdaAPICreateAPI from 'lambda-api';
|
|
2
2
|
|
|
3
|
+
import { healthHandler } from '../healthHandler/index.js';
|
|
4
|
+
import { serviceHandler } from '../serviceHandler/index.js';
|
|
5
|
+
import { apiHandler } from './apiHandler.js';
|
|
6
|
+
|
|
3
7
|
const addCORS = API => {
|
|
4
8
|
API.use((req, res, next) => { res.cors(); next(); });
|
|
5
9
|
API.use((err, req, res, next) => { res.cors(); next(); });
|
|
@@ -18,6 +22,14 @@ const createAPI = () => {
|
|
|
18
22
|
|
|
19
23
|
addCORS(API);
|
|
20
24
|
|
|
25
|
+
API.registerService = (SERVICE_NAME, SRC_FOLDER='', MODULE_FOLDER='node_modules/') =>
|
|
26
|
+
serviceHandler(API, SERVICE_NAME, SRC_FOLDER, MODULE_FOLDER);
|
|
27
|
+
|
|
28
|
+
API.registerHealthService = () =>
|
|
29
|
+
healthHandler(API);
|
|
30
|
+
|
|
31
|
+
API.handler = () => apiHandler(API);
|
|
32
|
+
|
|
21
33
|
return API;
|
|
22
34
|
}
|
|
23
35
|
|
|
@@ -5,8 +5,11 @@ export const registerModel = (serviceName, modelSchema) => {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
export const loadModels = (orm, types) => {
|
|
8
|
-
|
|
8
|
+
const MODELS = Object.keys(modelSchemas).reduce((loadedModels, serviceName) => {
|
|
9
9
|
loadedModels[serviceName] = modelSchemas[serviceName](orm, types);
|
|
10
10
|
return loadedModels;
|
|
11
11
|
}, {});
|
|
12
|
+
|
|
13
|
+
Object.entries(MODELS).forEach(([serviceName, model]) => { model.associate(MODELS); });
|
|
14
|
+
return MODELS;
|
|
12
15
|
}
|
|
@@ -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,9 +3,11 @@ 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
|
-
const serviceHandler = async (API, SERVICE_NAME, SRC_FOLDER
|
|
10
|
+
const serviceHandler = async (API, SERVICE_NAME, SRC_FOLDER, MODULE_FOLDER) => {
|
|
9
11
|
try {
|
|
10
12
|
const PROJECT_ROOT_PATH = getProjectRoot(import.meta.url, MODULE_FOLDER)+'/';
|
|
11
13
|
const PROJECT_RELATIVE_SERVICE_PATH = './' + SRC_FOLDER + `${SERVICE_NAME}/`;
|
|
@@ -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
|
-
|
|
21
|
-
|
|
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);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const modelSchema = serviceName => (sequelize, type) => {
|
|
2
|
+
const schema = sequelize.define(serviceName, {
|
|
3
|
+
name: {
|
|
4
|
+
type: type.STRING,
|
|
5
|
+
allowNull: false
|
|
6
|
+
},
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
schema.associate = MODELS => {
|
|
10
|
+
schema.hasMany(MODELS.todo, {
|
|
11
|
+
foreignKey: 'todoListId',
|
|
12
|
+
as: 'todoElements',
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return schema;
|
|
17
|
+
};
|