minimonolith 0.25.23 → 0.26.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.
Files changed (183) hide show
  1. package/README.md +55 -80
  2. package/backtest/back.js +54 -0
  3. package/backtest/backtest.js +131 -0
  4. package/backtest/handler.js +23 -0
  5. package/backtest/handlerCompiletimeError.js +10 -0
  6. package/backtest/handlerCompiletimeError2.js +10 -0
  7. package/backtest/handlerRuntimeError.js +24 -0
  8. package/backtest/localDocker/Dockerfile +11 -0
  9. package/backtest/localDocker/docker-compose.yml +34 -0
  10. package/backtest/localDocker/run.sh +4 -0
  11. package/backtest/localDocker/shutdown.sh +2 -0
  12. package/backtest/localDocker/update.sh +2 -0
  13. package/backtest/server.js +7 -0
  14. package/backtest/todo/delete/index.js +7 -0
  15. package/backtest/todo/delete/valid.js +8 -0
  16. package/backtest/todo/get/index.js +3 -0
  17. package/backtest/todo/getAttributes/index.js +5 -0
  18. package/backtest/todo/getAttributes/valid.js +5 -0
  19. package/backtest/todo/getOne/index.js +3 -0
  20. package/backtest/todo/getOne/valid.js +8 -0
  21. package/backtest/todo/getOneAlias/index.js +3 -0
  22. package/backtest/todo/getOneAlias/valid.js +3 -0
  23. package/backtest/todo/index.js +1 -0
  24. package/backtest/todo/model.js +19 -0
  25. package/backtest/todo/patch/index.js +6 -0
  26. package/backtest/todo/patch/valid.js +12 -0
  27. package/backtest/todo/post/index.js +5 -0
  28. package/backtest/todo/post/valid.js +5 -0
  29. package/backtest/todo/postSeed/index.js +9 -0
  30. package/backtest/todo/postSeed/seed.js +4 -0
  31. package/backtest/todoCompiletimeError/got/index.js +1 -0
  32. package/backtest/todoCompiletimeError/index.js +1 -0
  33. package/backtest/todoList/delete/handler.js +7 -0
  34. package/backtest/todoList/delete/valid.js +5 -0
  35. package/backtest/todoList/get/handler.js +5 -0
  36. package/backtest/todoList/index.js +1 -0
  37. package/backtest/todoList/model.js +17 -0
  38. package/backtest/todoList/post/handler.js +9 -0
  39. package/backtest/todoList/post/valid.js +6 -0
  40. package/backtest/todoRuntimeError/get/index.js +1 -0
  41. package/index.js +13 -22
  42. package/package.json +7 -8
  43. package/src/api/get/index.js +17 -0
  44. package/src/api/getNewAPI/index.js +11 -0
  45. package/src/api/getSyncedHandler/index.js +23 -0
  46. package/src/api/index.js +4 -0
  47. package/src/api/postService/index.js +29 -0
  48. package/src/autotest/getMethodTest/back.index.js +29 -0
  49. package/{autotest/getMethodTest/handler.js → src/autotest/getMethodTest/index.js} +1 -3
  50. package/src/autotest/index.js +3 -0
  51. package/src/database/index.js +4 -0
  52. package/src/database/post/index.js +38 -0
  53. package/src/database/postConnection/index.js +25 -0
  54. package/src/error/index.js +4 -0
  55. package/src/error/postCompiletime/index.js +13 -0
  56. package/src/error/postRuntime/index.js +7 -0
  57. package/src/event/getParsedEvent/index.js +9 -0
  58. package/src/event/index.js +3 -0
  59. package/src/log/index.js +5 -0
  60. package/src/log/post/index.js +3 -0
  61. package/src/log/postERROR/index.js +3 -0
  62. package/src/log/postINFO/index.js +3 -0
  63. package/src/method/getParsedCode/index.js +13 -0
  64. package/{service/getResponseCode/handler.js → src/method/getResponseCode/index.js} +1 -3
  65. package/src/method/getRouteCode/index.js +12 -0
  66. package/src/method/getType/index.js +9 -0
  67. package/src/method/index.js +7 -0
  68. package/src/method/post/index.js +44 -0
  69. package/src/method/postResponse/index.js +29 -0
  70. package/src/middleware/index.js +4 -0
  71. package/src/middleware/postCors/index.js +20 -0
  72. package/src/middleware/postError/index.js +10 -0
  73. package/src/model/getSynced/index.js +23 -0
  74. package/src/model/index.js +4 -0
  75. package/src/model/post/index.js +16 -0
  76. package/{server → src/server}/getNodejsServerHandler/index.js +13 -7
  77. package/src/server/getServerFactory/index.js +12 -0
  78. package/src/server/index.js +3 -0
  79. package/api/api.js +0 -5
  80. package/api/getModels/handler.js +0 -5
  81. package/api/getModels/out.js +0 -3
  82. package/api/getNewAPI/handler.js +0 -46
  83. package/api/getNewAPI/in.js +0 -8
  84. package/api/getNewAPI/out.js +0 -3
  85. package/api/getORM/handler.js +0 -5
  86. package/api/getORM/out.js +0 -3
  87. package/api/getSchemas/handler.js +0 -5
  88. package/api/getSchemas/out.js +0 -3
  89. package/api/getService/handler.js +0 -8
  90. package/api/getService/in.js +0 -6
  91. package/api/getService/out.js +0 -3
  92. package/api/getServices/handler.js +0 -5
  93. package/api/getServices/out.js +0 -3
  94. package/api/getSyncedHandler/handler.js +0 -28
  95. package/api/getSyncedHandler/in.js +0 -3
  96. package/api/getSyncedHandler/out.js +0 -3
  97. package/api/postDebug/handler.js +0 -5
  98. package/api/postDebug/in.js +0 -3
  99. package/api/postError/handler.js +0 -5
  100. package/api/postError/in.js +0 -3
  101. package/api/postInfo/handler.js +0 -5
  102. package/api/postInfo/in.js +0 -3
  103. package/api/postModel/handler.js +0 -8
  104. package/api/postModel/in.js +0 -6
  105. package/api/postModule/handler.js +0 -31
  106. package/api/postModule/in.js +0 -7
  107. package/api/postORM/handler.js +0 -7
  108. package/api/postORM/in.js +0 -5
  109. package/api/postRawModule/handler.js +0 -9
  110. package/api/postRawModule/in.js +0 -9
  111. package/api/postRoute/handler.js +0 -9
  112. package/api/postRoute/in.js +0 -7
  113. package/api/postSchema/handler.js +0 -8
  114. package/api/postSchema/in.js +0 -6
  115. package/api/services.js +0 -18
  116. package/autotest/getMethodTest/in.js +0 -3
  117. package/autotest/getMethodTest/out.js +0 -3
  118. package/autotest/services.js +0 -1
  119. package/control/get/handler.js +0 -8
  120. package/control/get/in.js +0 -6
  121. package/control/get/out.js +0 -3
  122. package/database/post/handler.js +0 -24
  123. package/database/post/in.js +0 -12
  124. package/database/postConnection/handler.js +0 -25
  125. package/database/postConnection/in.js +0 -5
  126. package/database/postConnection/out.js +0 -3
  127. package/database/services.js +0 -1
  128. package/environment/getDBEnvVars/handler.js +0 -6
  129. package/environment/getDBEnvVars/in.js +0 -3
  130. package/environment/getDBEnvVars/out.js +0 -3
  131. package/environment/getENVEnvVars/handler.js +0 -7
  132. package/environment/getENVEnvVars/in.js +0 -3
  133. package/environment/getENVEnvVars/out.js +0 -3
  134. package/environment/postEnvFile/handler.js +0 -16
  135. package/environment/postEnvFile/in.js +0 -6
  136. package/environment/postTestEnvironment/index.js +0 -6
  137. package/environment/services.js +0 -5
  138. package/health/post/handler.js +0 -11
  139. package/health/services.js +0 -1
  140. package/model/getSynced/handler.js +0 -29
  141. package/model/post/handler.js +0 -14
  142. package/model/post/in.js +0 -6
  143. package/model/services.js +0 -1
  144. package/modules.js +0 -19
  145. package/server/getServerFactory/handler.js +0 -17
  146. package/server/getServerFactory/in.js +0 -8
  147. package/server/getServerFactory/out.js +0 -3
  148. package/server/services.js +0 -1
  149. package/service/getParsedCode/handler.js +0 -19
  150. package/service/getParsedCode/in.js +0 -6
  151. package/service/getParsedCode/out.js +0 -6
  152. package/service/getParsedEvent/handler.js +0 -12
  153. package/service/getParsedEvent/in.js +0 -5
  154. package/service/getParsedEvent/out.js +0 -6
  155. package/service/getResponseCode/in.js +0 -3
  156. package/service/getResponseCode/out.js +0 -3
  157. package/service/getRouteCode/handler.js +0 -15
  158. package/service/getRouteCode/in.js +0 -6
  159. package/service/getRouteCode/out.js +0 -3
  160. package/service/getType/handler.js +0 -11
  161. package/service/getType/in.js +0 -5
  162. package/service/getType/out.js +0 -3
  163. package/service/postRoute/handler.js +0 -105
  164. package/service/postRoute/in.js +0 -6
  165. package/service/services.js +0 -9
  166. package/setupTesting.js +0 -17
  167. package/validation/getParsedAsync/handler.js +0 -37
  168. package/validation/getParsedAsync/in.js +0 -9
  169. package/validation/getParsedAsync/out.js +0 -6
  170. package/validation/services.js +0 -1
  171. package/zdb/components.js +0 -1
  172. package/zdb/getAssertExists/handler.js +0 -16
  173. package/zdb/getAssertExists/in.js +0 -6
  174. package/zdb/getAssertExists/out.js +0 -3
  175. package/zdb/getAssertNotExists/handler.js +0 -16
  176. package/zdb/getAssertNotExists/in.js +0 -6
  177. package/zdb/getAssertNotExists/out.js +0 -3
  178. package/zdb/getAssertSafeInt/handler.js +0 -15
  179. package/zdb/getAssertSafeInt/in.js +0 -6
  180. package/zdb/getAssertSafeInt/out.js +0 -3
  181. /package/{control/services.js → backtest/todoRuntimeError/index.js} +0 -0
  182. /package/{autotest → src/autotest}/getTestServerResponse/index.js +0 -0
  183. /package/{api → src/middleware/postCors}/corsHeaders.js +0 -0
@@ -0,0 +1,5 @@
1
+ import { z, zdb } from '../../../index.js';
2
+
3
+ export default ({ MODELS }) => ({
4
+ name: z.string().superRefine(zdb.notExists(MODELS.todo, 'name')),
5
+ });
@@ -0,0 +1,9 @@
1
+ import seed from './seed.js';
2
+
3
+ export default async ({ MODELS }) => {
4
+ for (const todo of seed) await MODELS.todo.create(todo);
5
+
6
+ const todos = await MODELS.todo.findAll();
7
+
8
+ return todos.map((item) => item.toJSON());
9
+ };
@@ -0,0 +1,4 @@
1
+ export default [
2
+ { name: 'example1', },
3
+ { name: 'example2', },
4
+ ];
@@ -0,0 +1 @@
1
+ //export default async () => 'ERROR';
@@ -0,0 +1 @@
1
+ export default ['got'];
@@ -0,0 +1,7 @@
1
+ export default async ({ body, MODELS }) => {
2
+
3
+ const todo = await MODELS.todo.findOne({ where: { id: body.id } });
4
+ await todo.destroy();
5
+
6
+ return undefined;
7
+ }
@@ -0,0 +1,5 @@
1
+ import { z } from 'zod';
2
+
3
+ export default (MODELS, ROUTE_CODE) => ({
4
+ id: z.number(),
5
+ });
@@ -0,0 +1,5 @@
1
+ export default async ({ MODELS }) => {
2
+ const todos = await MODELS.todo.findAll();
3
+
4
+ return todos;
5
+ }
@@ -0,0 +1 @@
1
+ export const methods = ['get', 'post', 'delete'];
@@ -0,0 +1,17 @@
1
+ export const modelSchema = serviceName => (orm, types) => {
2
+ const schema = orm.define(serviceName, {
3
+ name: {
4
+ type: types.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
+ };
@@ -0,0 +1,9 @@
1
+ export default async ({ body, MODELS, SERVICES }) => {
2
+
3
+ const todoList = await MODELS.todoList.create({ name: body.name });
4
+ const todo = SERVICES.todo.post({ name: body.name });
5
+
6
+ todoList.addTodoElement(todo);
7
+
8
+ return todoList.toJSON();
9
+ };
@@ -0,0 +1,6 @@
1
+ import { z } from 'zod';
2
+
3
+ export default (MODELS, ROUTE_CODE) => ({
4
+ name: z.string(),
5
+ });
6
+
@@ -0,0 +1 @@
1
+ export default async () => { throw Error(); }
package/index.js CHANGED
@@ -1,22 +1,13 @@
1
- import path from 'path';
2
- import { getNewLib, z } from '@minimonolith/lib';
3
-
4
- import modules from './modules.js';
5
-
6
- const LIB = await getNewLib({ LOG_LEVEL: 1 });
7
-
8
- await Promise.all(modules.map(module => LIB.postModule(
9
- module,
10
- 'minimonolith' + path.sep,
11
- path.join('@minimonolith', 'lib') + path.sep
12
- )));
13
-
14
-
15
- const { SERVICES, COMPONENTS } = LIB;
16
- const zdb = COMPONENTS.zdb;
17
- const database = SERVICES.database;
18
- const getServerFactory = SERVICES.server.getServerFactory.handler;
19
- const autotest = SERVICES.autotest;
20
- const getNewAPI = SERVICES.api.getNewAPI.handler;
21
-
22
- export { z, zdb, database, getServerFactory, autotest, getNewAPI };
1
+ import API_SERVICE from './src/api/index.js';
2
+ import SERVER_SERVICE from './src/server/index.js';
3
+ import AUTOTEST_SERVICE from './src/autotest/index.js';
4
+
5
+ const { getNewAPI } = API_SERVICE;
6
+ const { getServerFactory } = SERVER_SERVICE;
7
+ const { getMethodTest } = AUTOTEST_SERVICE;
8
+
9
+ export {
10
+ getServerFactory,
11
+ getNewAPI,
12
+ getMethodTest,
13
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "minimonolith",
3
3
  "type": "module",
4
- "version": "0.25.23",
4
+ "version": "0.26.2",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -10,24 +10,23 @@
10
10
  },
11
11
  "keywords": [
12
12
  "lambda-api",
13
- "zod",
14
13
  "sequelize"
15
14
  ],
16
15
  "dependencies": {
17
- "@minimonolith/lib": "^0.14.9",
18
- "lambda-api": "^1.0.3",
19
- "sequelize": "^6.30.0",
20
- "dotenv": "^16.0.3",
21
- "zod": "^3.21.4"
16
+ "lambda-api": "^1.0.1",
17
+ "sequelize": "^6.30.0"
22
18
  },
23
19
  "devDependencies": {
24
20
  "@aws-sdk/client-s3": "^3.304.0",
25
21
  "@aws-sdk/s3-request-presigner": "^3.304.0",
22
+ "@jest/globals": "^30.2.0",
26
23
  "cross-env": "^7.0.3",
27
24
  "cross-fetch": "^3.1.5",
25
+ "dotenv": "^16.0.3",
28
26
  "jest": "^29.5.0",
29
27
  "mysql2": "^3.2.0",
30
28
  "nodemon": "^2.0.22",
31
- "sqlite3": "^5.1.6"
29
+ "sqlite3": "^5.1.6",
30
+ "zod": "^3.21.4"
32
31
  }
33
32
  }
@@ -0,0 +1,17 @@
1
+ import getLambdaAPI from 'lambda-api';
2
+
3
+ import LOG_SERVICE from '../../log/index.js';
4
+ import DATABASE_SERVICE from '../../database/index.js';
5
+ import MIDDLEWARE_SERVICE from '../../middleware/index.js';
6
+
7
+ import postService from '../postService/index.js';
8
+ import getSyncedHandler from '../getSyncedHandler/index.js';
9
+
10
+ const API = getLambdaAPI();
11
+ API.postCORSMiddleware = MIDDLEWARE_SERVICE.postCors;
12
+ API.postService = postService;
13
+ API.postDatabaseService = DATABASE_SERVICE.post;
14
+ API.postErrorMiddleware = MIDDLEWARE_SERVICE.postError;
15
+ API.getSyncedHandler = getSyncedHandler;
16
+
17
+ export default () => API;
@@ -0,0 +1,11 @@
1
+ import getAPI from '../get/index.js';
2
+
3
+ export default ({ ERROR_LEVEL=0 } = {}) => {
4
+ getAPI().SERVICES = {};
5
+ getAPI().SCHEMAS = {};
6
+ getAPI().MODELS = {};
7
+ getAPI().ORM = undefined;
8
+ getAPI().ERROR_LEVEL = ERROR_LEVEL;
9
+ getAPI().postCORSMiddleware();
10
+ return getAPI();
11
+ };
@@ -0,0 +1,23 @@
1
+ import MODEL_SERVICE from '../../model/index.js';
2
+ import LOG_SERVICE from '../../log/index.js';
3
+
4
+ import getAPI from '../get/index.js';
5
+
6
+ const META_SERVICE='API', META_METHOD='GET_SYNCED_HANDLER', META_ROUTE=META_SERVICE+'_'+META_METHOD;
7
+
8
+ export default async () => {
9
+ getAPI().postErrorMiddleware();
10
+ if (getAPI().ORM) await MODEL_SERVICE.getSynced();
11
+
12
+ LOG_SERVICE.post({ META_ROUTE, INFO: 'LISTENING' });
13
+
14
+ return async (event, context) => {
15
+ LOG_SERVICE.post({
16
+ ROUTE_CODE: 'LAMBDA_EVENT',
17
+ PATH: event.requestContext?.http?.path,
18
+ METHOD: event.requestContext?.http?.method,
19
+ });
20
+
21
+ return await getAPI().run(event, context);
22
+ };
23
+ };
@@ -0,0 +1,4 @@
1
+ import get from './get/index.js';
2
+ import getNewAPI from './getNewAPI/index.js';
3
+
4
+ export default { get, getNewAPI };
@@ -0,0 +1,29 @@
1
+ import fs from 'fs';
2
+
3
+ import MODEL_SERVICE from '../../model/index.js';
4
+ import LOG_SERVICE from '../../log/index.js';
5
+ import METHOD_SERVICE from '../../method/index.js';
6
+ import ERROR_SERVICE from '../../error/index.js';
7
+
8
+ import getAPI from '../get/index.js';
9
+
10
+ const META_SERVICE_NAME = 'API', META_METHOD_NAME = 'POST_SERVICE';
11
+ const GREEN = '\x1b[32m', END = '\x1b[0m'
12
+
13
+ export default async (SERVICE_NAME, SERVICE, MODEL) => {
14
+
15
+ LOG_SERVICE.post(`${META_SERVICE_NAME} ${META_METHOD_NAME} ${GREEN}${SERVICE_NAME}${END}`);
16
+
17
+ getAPI().SERVICES[SERVICE_NAME] = {};
18
+ try {
19
+
20
+ await MODEL_SERVICE.post(SERVICE_NAME, MODEL);
21
+
22
+ for (const METHOD_KEY in SERVICE) {
23
+ await METHOD_SERVICE.post(SERVICE_NAME, METHOD_KEY, SERVICE[METHOD_KEY]);
24
+ }
25
+
26
+ } catch (META_METHOD_ERROR) {
27
+ ERROR_SERVICE.postCompiletime({ META_SERVICE_NAME, META_METHOD_NAME, META_METHOD_ERROR });
28
+ }
29
+ };
@@ -0,0 +1,29 @@
1
+ import url from 'url';
2
+ import fetch from 'cross-fetch';
3
+
4
+ import getTestServerResponse from '../getTestServerResponse/index.js';
5
+
6
+ export default (serverHandler, expectedCode) => async params => {
7
+ const URL = new url.URL(params.path, 'http://localhost/');
8
+
9
+ const request = {
10
+ method: params.method,
11
+ url: URL.pathname + URL.search,
12
+ headers: {
13
+ ...params.headers,
14
+ 'Content-Type': 'application/json',
15
+ },
16
+ on(eventName, callback) {
17
+ if (eventName === 'data' && params.body) {
18
+ callback(Buffer.from(JSON.stringify(params.body)));
19
+ }
20
+ if (eventName === 'end') { callback(); }
21
+ },
22
+ };
23
+
24
+ const response = getTestServerResponse();
25
+ serverHandler(request, response);
26
+
27
+ const result = await response.asyncResult();
28
+ expect(result.statusCode).toBe(expectedCode);
29
+ }
@@ -1,6 +1,4 @@
1
- export default async ({ body }) => {
2
- const server = body;
3
-
1
+ export default async server => {
4
2
  const fetch = (await import('cross-fetch')).default;
5
3
 
6
4
  return async ({ path, method, expectedCode, headers, body }) => {
@@ -0,0 +1,3 @@
1
+ import getMethodTest from './getMethodTest/index.js';
2
+
3
+ export default { getMethodTest };
@@ -0,0 +1,4 @@
1
+ import post from './post/index.js';
2
+ import postConnection from './postConnection/index.js';
3
+
4
+ export default { post, postConnection };
@@ -0,0 +1,38 @@
1
+ import Sequelize from 'sequelize';
2
+ //import SequelizeDynamo from 'dynamo-sequelize';
3
+
4
+ import API_SERVICE from '../../api/index.js';
5
+ import LOG_SERVICE from '../../log/index.js';
6
+ import ERROR_SERVICE from '../../error/index.js';
7
+
8
+ import postConnection from '../postConnection/index.js'
9
+
10
+ const META_SERVICE='DATABASE', META_METHOD='POST', META_ROUTE=META_SERVICE+'_'+META_METHOD;
11
+
12
+ export default async ({ DB_DIALECT='sqlite', DB_HOST=undefined, DB_PORT=undefined, DB_DB=undefined,
13
+ DB_USER=undefined, DB_PASS=undefined, DB_STORAGE=':memory:'} = {}) => {
14
+
15
+ try {
16
+ if (DB_DIALECT!=='sqlite' && (!DB_HOST || !DB_PORT || !DB_DB || !DB_USER || !DB_PASS)) {
17
+ throw new Error(
18
+ 'One of these DB parameters not given: [DB_HOST, DB_PORT, DB_DB, DB_USER, DB_PASS]');
19
+ }
20
+
21
+ const SEQUELIZE_OPTIONS = { dialect: DB_DIALECT, host: DB_HOST,
22
+ port: DB_PORT, storage: DB_STORAGE, logging: false };
23
+
24
+ const DB_VARS = Object.fromEntries(
25
+ Object.entries({
26
+ DB_DIALECT, DB_HOST, DB_PORT, DB_DB, DB_USER, DB_PASS, DB_STORAGE })
27
+ .filter(([key, val]) => val != null)
28
+ );
29
+
30
+ LOG_SERVICE.postINFO({ META_ROUTE, DB_VARS });
31
+
32
+ API_SERVICE.get().ORM = new Sequelize(DB_DB, DB_USER, DB_PASS, SEQUELIZE_OPTIONS);
33
+ await postConnection(API_SERVICE.get().ORM);
34
+
35
+ } catch (META_METHOD_ERROR) {
36
+ ERROR_SERVICE.postCompiletime({ META_ROUTE, META_METHOD_ERROR });
37
+ }
38
+ };
@@ -0,0 +1,25 @@
1
+ import LOG_SERVICE from '../../log/index.js';
2
+
3
+ const META_SERVICE_NAME='DATABASE', META_METHOD_NAME='POST_CONNECTION';
4
+
5
+ export default async ORM => {
6
+ const MAX_RETRIES = 5, INITIAL_WAIT = 100;
7
+ let connectionRetries = 0, waitTime = INITIAL_WAIT;
8
+
9
+ while (connectionRetries < MAX_RETRIES) {
10
+ try {
11
+ LOG_SERVICE.post({ META_SERVICE_NAME, META_METHOD_NAME, AUTH_INTENT: connectionRetries });
12
+ await ORM.authenticate();
13
+ break;
14
+
15
+ } catch (META_METHOD_ERROR) {
16
+ await new Promise(resolve => setTimeout(resolve, waitTime));
17
+
18
+ connectionRetries += 1;
19
+ const jitter = Math.random() * 0.3 + 0.7;
20
+ waitTime = Math.min(2 * waitTime * jitter, INITIAL_WAIT * Math.pow(2, MAX_RETRIES));
21
+ }
22
+ }
23
+
24
+ return connectionRetries;
25
+ };
@@ -0,0 +1,4 @@
1
+ import postCompiletime from './postCompiletime/index.js';
2
+ import postRuntime from './postRuntime/index.js';
3
+
4
+ export default { postCompiletime, postRuntime };
@@ -0,0 +1,13 @@
1
+ import API_SERVICE from '../../api/index.js';
2
+ import LOG_SERVICE from '../../log/index.js';
3
+
4
+ export default ({ META_SERVICE_NAME, META_METHOD_NAME, ROUTE_CODE, META_METHOD_ERROR }) => {
5
+ LOG_SERVICE.postERROR({
6
+ META_SERVICE_NAME,
7
+ META_METHOD_NAME,
8
+ ROUTE_CODE,
9
+ META_METHOD_ERROR,
10
+ STACK_TRACE: META_METHOD_ERROR?.stack,
11
+ });
12
+ if (API_SERVICE.get().DEV_ENV==='TRUE') throw META_METHOD_ERROR;
13
+ };
@@ -0,0 +1,7 @@
1
+ import LOG_SERVICE from '../../log/index.js';
2
+
3
+ export default (res, ROUTE_CODE, METHOD_ERROR) => {
4
+ const MESSAGE = { ROUTE_CODE, METHOD_ERROR: METHOD_ERROR.stack.toString() };
5
+ LOG_SERVICE.postERROR(MESSAGE);
6
+ res.status(500).json(MESSAGE);
7
+ }
@@ -0,0 +1,9 @@
1
+ export default event => {
2
+ const { body, params, query, requestContext } = event;
3
+ const claims = requestContext.authorizer?.jwt?.claims
4
+
5
+ const tempUnifiedBody = { ...body, ...params, ...query };
6
+ const unifiedBody = Object.keys(tempUnifiedBody).length > 0 ? tempUnifiedBody : undefined;
7
+
8
+ return { body: unifiedBody, claims };
9
+ }
@@ -0,0 +1,3 @@
1
+ import getParsedEvent from './getParsedEvent/index.js';
2
+
3
+ export default { getParsedEvent };
@@ -0,0 +1,5 @@
1
+ import post from './post/index.js';
2
+ import postERROR from './postERROR/index.js';
3
+ import postINFO from './postINFO/index.js';
4
+
5
+ export default { post, postERROR, postINFO };
@@ -0,0 +1,3 @@
1
+ import API_SERVICE from '../../api/index.js';
2
+
3
+ export default (...MESSAGE) => { console.log(...MESSAGE); };
@@ -0,0 +1,3 @@
1
+ import API_SERVICE from '../../api/index.js';
2
+
3
+ export default (...ERROR) => { console.error(...ERROR); };
@@ -0,0 +1,3 @@
1
+ import API_SERVICE from '../../api/index.js';
2
+
3
+ export default (...MESSAGE) => { if (API_SERVICE.get().ERROR_LEVEL===0) { console.log(...MESSAGE); } };
@@ -0,0 +1,13 @@
1
+ export default (SERVICE_NAME, METHOD_CODE) => {
2
+ const methodPathParts = METHOD_CODE.split(":");
3
+ const pathParams = methodPathParts.slice(1).map(param => `:${param}`);
4
+ const rawMethodName = methodPathParts[0];
5
+ const METHOD_NAME = rawMethodName[0] === '!' ? rawMethodName.substring(1) : rawMethodName;
6
+
7
+ const relativeMethodRoute = `${rawMethodName}${pathParams.length > 0 ? '/' : ''}${pathParams.join("/")}`;
8
+ const METHOD_ROUTE = relativeMethodRoute[0]==='!' ?
9
+ `/${relativeMethodRoute.substring(1)}/${SERVICE_NAME}` :
10
+ `/${SERVICE_NAME}/${relativeMethodRoute}`;
11
+
12
+ return { METHOD_NAME, METHOD_ROUTE };
13
+ };
@@ -3,9 +3,7 @@ const typeToResponseCode = {
3
3
  delete: 204,
4
4
  }
5
5
 
6
- export default ({ body }) => {
7
- const methodType = body;
8
-
6
+ export default methodType => {
9
7
  if (methodType in typeToResponseCode) return typeToResponseCode[methodType];
10
8
  else return 200;
11
9
  }
@@ -0,0 +1,12 @@
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
+ export default (SERVICE_NAME, METHOD_NAME) => {
9
+ const snakeServiceName = getUpperSnakeCase(SERVICE_NAME);
10
+ const snakeMethodName = getUpperSnakeCase(METHOD_NAME);
11
+ return snakeServiceName + '_' + snakeMethodName;
12
+ };
@@ -0,0 +1,9 @@
1
+ const methodsAvailable = ['post', 'get', 'patch', 'put', 'delete'];
2
+
3
+ export default METHOD_NAME => {
4
+ const methodMatched = METHOD_NAME.match(/^[a-z]*/);
5
+ if (!methodMatched || !methodsAvailable.includes(methodMatched[0]))
6
+ throw new Error('UNKNOWN_METHOD_TYPE: ' + METHOD_NAME);
7
+
8
+ return methodMatched[0];
9
+ }
@@ -0,0 +1,7 @@
1
+ import post from './post/index.js';
2
+ import getParsedCode from './getParsedCode/index.js';
3
+ import getType from './getType/index.js';
4
+ import getRouteCode from './getRouteCode/index.js';
5
+ import postResponse from './postResponse/index.js';
6
+
7
+ export default { post, getParsedCode, getType, getRouteCode, postResponse };
@@ -0,0 +1,44 @@
1
+ import API_SERVICE from '../../api/index.js';
2
+ import LOG_SERVICE from '../../log/index.js';
3
+ import EVENT_SERVICE from '../../event/index.js';
4
+ import ERROR_SERVICE from '../../error/index.js';
5
+
6
+ import getParsedCode from '../getParsedCode/index.js';
7
+ import getRouteCode from '../getRouteCode/index.js';
8
+ import getType from '../getType/index.js';
9
+ import postResponse from '../postResponse/index.js';
10
+
11
+ const META_SERVICE_NAME='METHOD', META_METHOD_NAME='POST';
12
+ const CYAN = '\x1b[36m', YELLOW = '\x1b[33m', END = '\x1b[0m';
13
+
14
+ export default async (SERVICE_NAME, METHOD_KEY, METHOD) => {
15
+ const { METHOD_NAME, METHOD_ROUTE } = getParsedCode(SERVICE_NAME, METHOD_KEY);
16
+ const ROUTE_CODE = getRouteCode(SERVICE_NAME, METHOD_NAME);
17
+ //LOG_SERVICE.post(` FOUND_METHOD: ${CYAN}${METHOD_NAME}${END}`);
18
+
19
+ try {
20
+
21
+ // Register service within API
22
+ API_SERVICE.get().SERVICES[SERVICE_NAME][METHOD_NAME] = async (body, claims=undefined) => {
23
+ const { MODELS, SERVICES } = API_SERVICE.get();
24
+ return await METHOD({ MODELS, SERVICES, ROUTE_CODE, body, claims });
25
+ }
26
+
27
+ const METHOD_TYPE = getType(METHOD_NAME);
28
+
29
+ // Register route forn lAMBDA API
30
+ LOG_SERVICE.post(` · ${YELLOW}${METHOD_ROUTE}${END}`);
31
+
32
+ API_SERVICE.get()[METHOD_TYPE](METHOD_ROUTE, async (event, res) => {
33
+ const { body, claims } = EVENT_SERVICE.getParsedEvent(event);
34
+
35
+ try {
36
+ await postResponse(res, ROUTE_CODE, METHOD, METHOD_TYPE, body, claims);
37
+ }
38
+ catch (METHOD_ERROR) { ERROR_SERVICE.postRuntime(res, ROUTE_CODE, METHOD_ERROR); }
39
+ });
40
+
41
+ } catch (META_METHOD_ERROR) {
42
+ ERROR_SERVICE.postCompiletime({ META_SERVICE_NAME, META_METHOD_NAME, ROUTE_CODE, META_METHOD_ERROR });
43
+ }
44
+ };
@@ -0,0 +1,29 @@
1
+ import API_SERVICE from '../../api/index.js';
2
+ import LOG_SERVICE from '../../log/index.js';
3
+
4
+ import getResponseCode from '../getResponseCode/index.js';
5
+
6
+ export default async (res, ROUTE_CODE, METHOD, METHOD_TYPE, body, claims) => {
7
+
8
+ const { MODELS, SERVICES } = API_SERVICE.get();
9
+
10
+ const METHOD_RESPONSE = await METHOD({ MODELS, SERVICES, body, claims });
11
+ const { status, body: responseBody, cookies, headers } = METHOD_RESPONSE;
12
+
13
+ // status
14
+ let METHOD_RESPONSE_CODE = status;
15
+ if (!status) METHOD_RESPONSE_CODE = getResponseCode(METHOD_TYPE);
16
+
17
+ // headers
18
+ if (headers) Object.entries(headers).forEach(([k, v]) => res.header(k, v));
19
+
20
+ // cookies
21
+ if (cookies && Array.isArray(cookies))
22
+ cookies.forEach(c => {
23
+ //console.log('cookie', c.name);
24
+ res.cookie(c.name, c.value, c.options || {}); });
25
+
26
+ LOG_SERVICE.post({ ROUTE_CODE, METHOD_RESPONSE });
27
+
28
+ res.status(METHOD_RESPONSE_CODE).json(responseBody);
29
+ };
@@ -0,0 +1,4 @@
1
+ import postCors from './postCors/index.js';
2
+ import postError from './postError/index.js';
3
+
4
+ export default { postCors, postError };
@@ -0,0 +1,20 @@
1
+ import API_SERVICE from '../../api/index.js';
2
+ import LOG_SERVICE from '../../log/index.js';
3
+
4
+ import CORS_HEADERS from './corsHeaders.js';
5
+
6
+ export default () => {
7
+
8
+ API_SERVICE.get().use((req, res, next) => {
9
+ if (req.headers['x-trigger-error']) {
10
+ LOG_SERVICE.post({ ROUTE_CODE: 'LAMBDA_API', MESSAGE: 'X_TRIGGER_ERROR_HEADER' });
11
+ throw new Error('X_TRIGGER_ERROR_HEADER');
12
+ }
13
+ res.cors(); next();
14
+ });
15
+
16
+ API_SERVICE.get().options('/*', (req, res) => {
17
+ for (let k in CORS_HEADERS) { res.header(k, CORS_HEADERS[k]); }
18
+ res.status(200).send({})
19
+ });
20
+ };
@@ -0,0 +1,10 @@
1
+ import API_SERVICE from '../../api/index.js';
2
+ import LOG_SERVICE from '../../log/index.js';
3
+
4
+ export default () => {
5
+ API_SERVICE.get().use((err, req, res, next) => {
6
+ LOG_SERVICE.postERROR({ ROUTE_CODE: 'LAMBDA_API', ERROR: err });
7
+ res.cors(); //next();
8
+ res.status(500).send({ ROUTE_CODE: 'LAMBDA_API', ERROR: err.message });
9
+ });
10
+ };
@@ -0,0 +1,23 @@
1
+ import { DataTypes } from 'sequelize';
2
+
3
+ import API_SERVICE from '../../api/index.js';
4
+ import LOG_SERVICE from '../../log/index.js';
5
+
6
+ const META_SERVICE='MODEL', META_METHOD='GET_SYNCED', META_ROUTE=META_SERVICE+'_'+META_METHOD;
7
+
8
+ export default async () => {
9
+ for (const SERVICE_NAME of Object.keys(API_SERVICE.get().SCHEMAS)) {
10
+ LOG_SERVICE.post({ META_ROUTE, LOADING: SERVICE_NAME });
11
+ API_SERVICE.get().MODELS[SERVICE_NAME] = API_SERVICE.get().SCHEMAS[SERVICE_NAME](API_SERVICE.get().ORM, DataTypes);
12
+ }
13
+
14
+ for (const [SERVICE_NAME, MODEL] of Object.entries(API_SERVICE.get().MODELS)) {
15
+ LOG_SERVICE.post({ META_ROUTE, ASSOCIATING: SERVICE_NAME });
16
+ MODEL.associate(API_SERVICE.get().MODELS);
17
+ }
18
+
19
+ LOG_SERVICE.post({ META_ROUTE, SYNCING_ORM: 'WAITING_ORM_SYNCING' });
20
+ //await API_SERVICE.get().ORM.sync({ alter: process.env.MM_API_DEV_ENV==='TRUE' ? true : false });
21
+ await API_SERVICE.get().ORM.sync();
22
+ LOG_SERVICE.post({ META_ROUTE, SYNCING_ORM: 'DONE_ORM_SYNCING' });
23
+ };