lesgo 1.0.0 → 2.1.1

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 (315) hide show
  1. package/README.md +5 -4
  2. package/bin/lesgo-scripts.sh +4 -4
  3. package/dist/config/app.d.ts +7 -0
  4. package/dist/config/app.js +9 -0
  5. package/dist/config/aws.d.ts +5 -0
  6. package/dist/config/aws.js +4 -0
  7. package/dist/config/basicAuth.d.ts +7 -0
  8. package/dist/config/basicAuth.js +8 -0
  9. package/dist/config/crypto.d.ts +10 -0
  10. package/dist/config/crypto.js +9 -0
  11. package/dist/config/dynamodb.d.ts +8 -0
  12. package/dist/config/dynamodb.js +14 -0
  13. package/dist/config/elasticache.d.ts +7 -0
  14. package/dist/config/elasticache.js +6 -0
  15. package/dist/config/index.d.ts +11 -0
  16. package/dist/config/index.js +11 -0
  17. package/dist/config/jwt.d.ts +12 -0
  18. package/dist/config/jwt.js +16 -0
  19. package/dist/config/rds.d.ts +12 -0
  20. package/dist/config/rds.js +13 -0
  21. package/dist/config/s3.d.ts +5 -0
  22. package/dist/config/s3.js +5 -0
  23. package/dist/config/secretsManager.d.ts +4 -0
  24. package/dist/config/secretsManager.js +4 -0
  25. package/dist/config/sqs.d.ts +9 -0
  26. package/dist/config/sqs.js +18 -0
  27. package/dist/exceptions/LesgoException.d.ts +6 -0
  28. package/{src → dist}/exceptions/LesgoException.js +5 -4
  29. package/dist/exceptions/index.d.ts +1 -0
  30. package/dist/exceptions/index.js +1 -0
  31. package/dist/middlewares/disconnectMiddleware.d.ts +8 -0
  32. package/dist/middlewares/disconnectMiddleware.js +78 -0
  33. package/dist/middlewares/httpMiddleware.d.ts +12 -0
  34. package/dist/middlewares/httpMiddleware.js +74 -0
  35. package/dist/middlewares/httpResponseMiddleware.d.ts +7 -0
  36. package/dist/middlewares/httpResponseMiddleware.js +128 -0
  37. package/dist/middlewares/index.d.ts +7 -0
  38. package/dist/middlewares/index.js +7 -0
  39. package/dist/middlewares/invokeCommandMiddleware.d.ts +12 -0
  40. package/dist/middlewares/invokeCommandMiddleware.js +70 -0
  41. package/dist/middlewares/sqsMiddleware.d.ts +10 -0
  42. package/dist/middlewares/sqsMiddleware.js +70 -0
  43. package/dist/middlewares/verifyBasicAuthMiddleware.d.ts +8 -0
  44. package/dist/middlewares/verifyBasicAuthMiddleware.js +59 -0
  45. package/dist/middlewares/verifyJwtMiddleware.d.ts +6 -0
  46. package/dist/middlewares/verifyJwtMiddleware.js +37 -0
  47. package/dist/services/DynamoDbService/deleteRecord.d.ts +8 -0
  48. package/dist/services/DynamoDbService/deleteRecord.js +69 -0
  49. package/dist/services/DynamoDbService/getClient.d.ts +7 -0
  50. package/dist/services/DynamoDbService/getClient.js +30 -0
  51. package/dist/services/DynamoDbService/getTableName.d.ts +2 -0
  52. package/dist/services/DynamoDbService/getTableName.js +15 -0
  53. package/dist/services/DynamoDbService/index.d.ts +6 -0
  54. package/dist/services/DynamoDbService/index.js +6 -0
  55. package/dist/services/DynamoDbService/putRecord.d.ts +8 -0
  56. package/dist/services/DynamoDbService/putRecord.js +64 -0
  57. package/dist/services/DynamoDbService/query.d.ts +7 -0
  58. package/dist/services/DynamoDbService/query.js +78 -0
  59. package/dist/services/DynamoDbService/scan.d.ts +7 -0
  60. package/dist/services/DynamoDbService/scan.js +60 -0
  61. package/dist/services/DynamoDbService/updateRecord.d.ts +9 -0
  62. package/dist/services/DynamoDbService/updateRecord.js +89 -0
  63. package/dist/services/ElastiCacheRedisService/deleteRedisCache.d.ts +3 -0
  64. package/dist/services/ElastiCacheRedisService/deleteRedisCache.js +62 -0
  65. package/dist/services/ElastiCacheRedisService/disconnectElastiCacheRedisClient.d.ts +11 -0
  66. package/dist/services/ElastiCacheRedisService/disconnectElastiCacheRedisClient.js +69 -0
  67. package/dist/services/ElastiCacheRedisService/getElastiCacheRedisClient.d.ts +12 -0
  68. package/dist/services/ElastiCacheRedisService/getElastiCacheRedisClient.js +114 -0
  69. package/dist/services/ElastiCacheRedisService/getRedisCache.d.ts +3 -0
  70. package/dist/services/ElastiCacheRedisService/getRedisCache.js +70 -0
  71. package/dist/services/ElastiCacheRedisService/index.d.ts +5 -0
  72. package/dist/services/ElastiCacheRedisService/index.js +5 -0
  73. package/dist/services/ElastiCacheRedisService/setRedisCache.d.ts +6 -0
  74. package/dist/services/ElastiCacheRedisService/setRedisCache.js +77 -0
  75. package/dist/services/JWTService/decodeJwt.d.ts +7 -0
  76. package/dist/services/JWTService/decodeJwt.js +9 -0
  77. package/dist/services/JWTService/getJwtSecret.d.ts +13 -0
  78. package/dist/services/JWTService/getJwtSecret.js +54 -0
  79. package/dist/services/JWTService/index.d.ts +2 -0
  80. package/dist/services/JWTService/index.js +2 -0
  81. package/dist/services/JWTService/sign.d.ts +3 -0
  82. package/dist/services/JWTService/sign.js +31 -0
  83. package/dist/services/JWTService/verify.d.ts +7 -0
  84. package/dist/services/JWTService/verify.js +61 -0
  85. package/dist/services/LoggerService.d.ts +46 -0
  86. package/{src → dist}/services/LoggerService.js +29 -60
  87. package/dist/services/RDSAuroraMySQLProxyService/disconnectMySQLProxyClient.d.ts +2 -0
  88. package/dist/services/RDSAuroraMySQLProxyService/disconnectMySQLProxyClient.js +60 -0
  89. package/dist/services/RDSAuroraMySQLProxyService/getMySQLProxyClient.d.ts +8 -0
  90. package/dist/services/RDSAuroraMySQLProxyService/getMySQLProxyClient.js +87 -0
  91. package/dist/services/RDSAuroraMySQLProxyService/index.d.ts +3 -0
  92. package/dist/services/RDSAuroraMySQLProxyService/index.js +3 -0
  93. package/dist/services/RDSAuroraMySQLProxyService/query.d.ts +4 -0
  94. package/dist/services/RDSAuroraMySQLProxyService/query.js +63 -0
  95. package/dist/services/S3Service/getClient.d.ts +4 -0
  96. package/dist/services/S3Service/getClient.js +28 -0
  97. package/dist/services/S3Service/getDownloadSignedUrl.d.ts +7 -0
  98. package/dist/services/S3Service/getDownloadSignedUrl.js +58 -0
  99. package/dist/services/S3Service/getHeadObject.d.ts +13 -0
  100. package/dist/services/S3Service/getHeadObject.js +83 -0
  101. package/dist/services/S3Service/getObject.d.ts +10 -0
  102. package/dist/services/S3Service/getObject.js +89 -0
  103. package/dist/services/S3Service/getUploadSignedUrl.d.ts +7 -0
  104. package/dist/services/S3Service/getUploadSignedUrl.js +61 -0
  105. package/dist/services/S3Service/index.d.ts +6 -0
  106. package/dist/services/S3Service/index.js +6 -0
  107. package/dist/services/S3Service/putObject.d.ts +9 -0
  108. package/dist/services/S3Service/putObject.js +77 -0
  109. package/dist/services/SQSService/deleteMessage.d.ts +9 -0
  110. package/dist/services/SQSService/deleteMessage.js +69 -0
  111. package/dist/services/SQSService/dispatch.d.ts +9 -0
  112. package/dist/services/SQSService/dispatch.js +71 -0
  113. package/dist/services/SQSService/getClient.d.ts +4 -0
  114. package/dist/services/SQSService/getClient.js +28 -0
  115. package/dist/services/SQSService/getQueueUrl.d.ts +7 -0
  116. package/dist/services/SQSService/getQueueUrl.js +25 -0
  117. package/dist/services/SQSService/index.d.ts +4 -0
  118. package/dist/services/SQSService/index.js +4 -0
  119. package/dist/services/SQSService/receiveMessages.d.ts +8 -0
  120. package/dist/services/SQSService/receiveMessages.js +65 -0
  121. package/dist/services/SecretsManagerService/getClient.d.ts +7 -0
  122. package/dist/services/SecretsManagerService/getClient.js +28 -0
  123. package/dist/services/SecretsManagerService/getSecretValue.d.ts +7 -0
  124. package/dist/services/SecretsManagerService/getSecretValue.js +65 -0
  125. package/dist/services/SecretsManagerService/index.d.ts +2 -0
  126. package/dist/services/SecretsManagerService/index.js +2 -0
  127. package/dist/services/index.d.ts +1 -0
  128. package/dist/services/index.js +1 -0
  129. package/dist/types/aws.d.ts +8 -0
  130. package/dist/types/aws.js +1 -0
  131. package/dist/utils/cache/redis/deleteCache.d.ts +19 -0
  132. package/dist/utils/cache/redis/deleteCache.js +21 -0
  133. package/dist/utils/cache/redis/getCache.d.ts +22 -0
  134. package/dist/utils/cache/redis/getCache.js +24 -0
  135. package/dist/utils/cache/redis/getClient.d.ts +30 -0
  136. package/dist/utils/cache/redis/getClient.js +32 -0
  137. package/dist/utils/cache/redis/index.d.ts +4 -0
  138. package/dist/utils/cache/redis/index.js +4 -0
  139. package/dist/utils/cache/redis/setCache.d.ts +25 -0
  140. package/dist/utils/cache/redis/setCache.js +26 -0
  141. package/dist/utils/crypto/decrypt.d.ts +33 -0
  142. package/dist/utils/crypto/decrypt.js +59 -0
  143. package/dist/utils/crypto/encrypt.d.ts +31 -0
  144. package/dist/utils/crypto/encrypt.js +41 -0
  145. package/dist/utils/crypto/hash.d.ts +31 -0
  146. package/dist/utils/crypto/hash.js +56 -0
  147. package/dist/utils/crypto/index.d.ts +3 -0
  148. package/dist/utils/crypto/index.js +3 -0
  149. package/dist/utils/crypto/validateEncryptionFields.d.ts +19 -0
  150. package/dist/utils/crypto/validateEncryptionFields.js +71 -0
  151. package/dist/utils/db/mysql/proxy/disconnect.d.ts +2 -0
  152. package/dist/utils/db/mysql/proxy/disconnect.js +5 -0
  153. package/dist/utils/db/mysql/proxy/disconnectDb.d.ts +2 -0
  154. package/dist/utils/db/mysql/proxy/disconnectDb.js +5 -0
  155. package/dist/utils/db/mysql/proxy/getClient.d.ts +4 -0
  156. package/dist/utils/db/mysql/proxy/getClient.js +5 -0
  157. package/dist/utils/db/mysql/proxy/index.d.ts +3 -0
  158. package/dist/utils/db/mysql/proxy/index.js +3 -0
  159. package/dist/utils/db/mysql/proxy/query.d.ts +4 -0
  160. package/dist/utils/db/mysql/proxy/query.js +45 -0
  161. package/dist/utils/dynamodb/deleteRecord.d.ts +5 -0
  162. package/dist/utils/dynamodb/deleteRecord.js +39 -0
  163. package/dist/utils/dynamodb/getClient.d.ts +3 -0
  164. package/dist/utils/dynamodb/getClient.js +5 -0
  165. package/dist/utils/dynamodb/index.d.ts +6 -0
  166. package/dist/utils/dynamodb/index.js +6 -0
  167. package/dist/utils/dynamodb/putRecord.d.ts +4 -0
  168. package/dist/utils/dynamodb/putRecord.js +39 -0
  169. package/dist/utils/dynamodb/query.d.ts +5 -0
  170. package/dist/utils/dynamodb/query.js +52 -0
  171. package/dist/utils/dynamodb/scan.d.ts +4 -0
  172. package/dist/utils/dynamodb/scan.js +40 -0
  173. package/dist/utils/dynamodb/updateRecord.d.ts +5 -0
  174. package/dist/utils/dynamodb/updateRecord.js +53 -0
  175. package/dist/utils/formatUnixTimestamp.d.ts +8 -0
  176. package/dist/utils/formatUnixTimestamp.js +17 -0
  177. package/dist/utils/generateUid.d.ts +13 -0
  178. package/{src → dist}/utils/generateUid.js +9 -4
  179. package/dist/utils/getCurrentDatetime.d.ts +2 -0
  180. package/dist/utils/getCurrentDatetime.js +4 -0
  181. package/dist/utils/getCurrentTimestamp.d.ts +7 -0
  182. package/dist/utils/getCurrentTimestamp.js +9 -0
  183. package/dist/utils/getJwtSubFromAuthHeader.d.ts +9 -0
  184. package/dist/utils/getJwtSubFromAuthHeader.js +24 -0
  185. package/dist/utils/index.d.ts +9 -0
  186. package/dist/utils/index.js +9 -0
  187. package/dist/utils/isDecimal.d.ts +8 -0
  188. package/dist/utils/isDecimal.js +9 -0
  189. package/dist/utils/isEmail.d.ts +9 -0
  190. package/{src → dist}/utils/isEmail.js +9 -4
  191. package/dist/utils/isEmpty.d.ts +8 -0
  192. package/dist/utils/isEmpty.js +19 -0
  193. package/dist/utils/jwt/index.d.ts +2 -0
  194. package/dist/utils/jwt/index.js +2 -0
  195. package/dist/utils/jwt/sign.d.ts +3 -0
  196. package/dist/utils/jwt/sign.js +5 -0
  197. package/dist/utils/jwt/verify.d.ts +3 -0
  198. package/dist/utils/jwt/verify.js +12 -0
  199. package/dist/utils/logger.d.ts +20 -0
  200. package/dist/utils/logger.js +38 -0
  201. package/dist/utils/s3/getClient.d.ts +3 -0
  202. package/dist/utils/s3/getClient.js +5 -0
  203. package/dist/utils/s3/getDownloadSignedUrl.d.ts +5 -0
  204. package/dist/utils/s3/getDownloadSignedUrl.js +39 -0
  205. package/dist/utils/s3/getHeadObject.d.ts +10 -0
  206. package/dist/utils/s3/getHeadObject.js +39 -0
  207. package/dist/utils/s3/getObject.d.ts +13 -0
  208. package/dist/utils/s3/getObject.js +51 -0
  209. package/dist/utils/s3/getUploadSignedUrl.d.ts +5 -0
  210. package/dist/utils/s3/getUploadSignedUrl.js +39 -0
  211. package/dist/utils/s3/index.d.ts +6 -0
  212. package/dist/utils/s3/index.js +6 -0
  213. package/dist/utils/s3/putObject.d.ts +5 -0
  214. package/dist/utils/s3/putObject.js +39 -0
  215. package/dist/utils/secretsmanager/getClient.d.ts +3 -0
  216. package/dist/utils/secretsmanager/getClient.js +5 -0
  217. package/dist/utils/secretsmanager/getSecretValue.d.ts +4 -0
  218. package/dist/utils/secretsmanager/getSecretValue.js +68 -0
  219. package/dist/utils/secretsmanager/index.d.ts +2 -0
  220. package/dist/utils/secretsmanager/index.js +2 -0
  221. package/dist/utils/sqs/deleteMessage.d.ts +5 -0
  222. package/dist/utils/sqs/deleteMessage.js +39 -0
  223. package/dist/utils/sqs/dispatch.d.ts +5 -0
  224. package/dist/utils/sqs/dispatch.js +39 -0
  225. package/dist/utils/sqs/getClient.d.ts +3 -0
  226. package/dist/utils/sqs/getClient.js +5 -0
  227. package/dist/utils/sqs/index.d.ts +4 -0
  228. package/dist/utils/sqs/index.js +4 -0
  229. package/dist/utils/sqs/receiveMessages.d.ts +5 -0
  230. package/dist/utils/sqs/receiveMessages.js +39 -0
  231. package/dist/utils/validateFields.d.ts +22 -0
  232. package/{src → dist}/utils/validateFields.js +13 -19
  233. package/package.json +145 -34
  234. package/src/exceptions/__tests__/LesgoException.spec.js +0 -24
  235. package/src/exceptions/index.js +0 -4
  236. package/src/index.js +0 -4
  237. package/src/middlewares/__mocks__/ValidationErrorException.js +0 -18
  238. package/src/middlewares/__tests__/basicAuthMiddleware.spec.js +0 -264
  239. package/src/middlewares/__tests__/clientAuthMiddleware.spec.js +0 -235
  240. package/src/middlewares/__tests__/errorHttpResponseMiddleware.spec.js +0 -203
  241. package/src/middlewares/__tests__/gzipHttpResponse.spec.js +0 -185
  242. package/src/middlewares/__tests__/httpNoOutputMiddleware.spec.js +0 -201
  243. package/src/middlewares/__tests__/normalizeHttpRequestMiddleware.spec.js +0 -181
  244. package/src/middlewares/__tests__/normalizeSQSMessageMiddleware.spec.js +0 -120
  245. package/src/middlewares/__tests__/successHttpResponseMiddleware.spec.js +0 -172
  246. package/src/middlewares/__tests__/verifyJwtMiddleware.spec.js +0 -297
  247. package/src/middlewares/basicAuthMiddleware.js +0 -125
  248. package/src/middlewares/clientAuthMiddleware.js +0 -103
  249. package/src/middlewares/errorHttpResponseMiddleware.js +0 -101
  250. package/src/middlewares/gzipHttpResponse.js +0 -98
  251. package/src/middlewares/httpMiddleware.js +0 -19
  252. package/src/middlewares/httpNoOutputMiddleware.js +0 -91
  253. package/src/middlewares/index.js +0 -19
  254. package/src/middlewares/normalizeHttpRequestMiddleware.js +0 -101
  255. package/src/middlewares/normalizeSQSMessageMiddleware.js +0 -67
  256. package/src/middlewares/successHttpResponseMiddleware.js +0 -85
  257. package/src/middlewares/verifyJwtMiddleware.js +0 -73
  258. package/src/services/AuroraDbRDSProxyService.js +0 -182
  259. package/src/services/AuroraDbService.js +0 -111
  260. package/src/services/DynamoDbService.js +0 -177
  261. package/src/services/ElastiCacheService.js +0 -17
  262. package/src/services/ElasticsearchService.js +0 -181
  263. package/src/services/FirebaseAdminService.js +0 -108
  264. package/src/services/JwtService.js +0 -52
  265. package/src/services/S3Service.js +0 -59
  266. package/src/services/SQSService.js +0 -65
  267. package/src/services/__tests__/AuroraDbRDSProxyService.spec.js +0 -278
  268. package/src/services/__tests__/AuroraDbService.spec.js +0 -211
  269. package/src/services/__tests__/AwsElasticsearchConnection.spec.js +0 -89
  270. package/src/services/__tests__/DynamoDbService.spec.js +0 -314
  271. package/src/services/__tests__/ElasticsearchService.spec.js +0 -201
  272. package/src/services/__tests__/FirebaseAdminService.spec.js +0 -356
  273. package/src/services/__tests__/JwtService.spec.js +0 -35
  274. package/src/services/__tests__/LengthAwarePaginator.spec.js +0 -223
  275. package/src/services/__tests__/LoggerService.spec.js +0 -267
  276. package/src/services/__tests__/Paginator.spec.js +0 -383
  277. package/src/services/__tests__/S3Service.spec.js +0 -55
  278. package/src/services/__tests__/SQSService.spec.js +0 -47
  279. package/src/services/__tests__/SignedRequest.spec.js +0 -103
  280. package/src/services/aws/AwsElasticsearchConnection.js +0 -59
  281. package/src/services/aws/SignedRequest.js +0 -158
  282. package/src/services/index.js +0 -19
  283. package/src/services/pagination/LengthAwarePaginator.js +0 -49
  284. package/src/services/pagination/Paginator.js +0 -260
  285. package/src/utils/__mocks__/db.js +0 -109
  286. package/src/utils/__tests__/cache.spec.js +0 -131
  287. package/src/utils/__tests__/crypto.spec.js +0 -122
  288. package/src/utils/__tests__/db.spec.js +0 -70
  289. package/src/utils/__tests__/dynamodb.spec.js +0 -27
  290. package/src/utils/__tests__/elasticsearch.spec.js +0 -43
  291. package/src/utils/__tests__/generateUid.spec.js +0 -36
  292. package/src/utils/__tests__/getJwtSubFromAuthHeader.spec.js +0 -20
  293. package/src/utils/__tests__/isDecimal.spec.js +0 -12
  294. package/src/utils/__tests__/isEmail.spec.js +0 -28
  295. package/src/utils/__tests__/isEmpty.spec.js +0 -31
  296. package/src/utils/__tests__/logger.spec.js +0 -11
  297. package/src/utils/__tests__/objectStore.spec.js +0 -67
  298. package/src/utils/__tests__/prepSQLInsertParams.spec.js +0 -46
  299. package/src/utils/__tests__/prepSQLUpdateParams.spec.js +0 -36
  300. package/src/utils/__tests__/queue.spec.js +0 -54
  301. package/src/utils/__tests__/validateFields.spec.js +0 -374
  302. package/src/utils/cache.js +0 -215
  303. package/src/utils/crypto.js +0 -77
  304. package/src/utils/db.js +0 -17
  305. package/src/utils/dynamodb.js +0 -6
  306. package/src/utils/elasticsearch.js +0 -20
  307. package/src/utils/getJwtSubFromAuthHeader.js +0 -18
  308. package/src/utils/index.js +0 -26
  309. package/src/utils/isDecimal.js +0 -2
  310. package/src/utils/isEmpty.js +0 -6
  311. package/src/utils/logger.js +0 -25
  312. package/src/utils/objectStore.js +0 -11
  313. package/src/utils/prepSQLInsertParams.js +0 -21
  314. package/src/utils/prepSQLUpdateParams.js +0 -25
  315. package/src/utils/queue.js +0 -11
@@ -1,54 +0,0 @@
1
- import aws from 'Config/aws'; // eslint-disable-line import/no-unresolved
2
- import queue, { dispatch } from '../queue';
3
- import LesgoException from '../../exceptions/LesgoException';
4
-
5
- describe('UtilsGroup: test queue utils', () => {
6
- it('test queue.dispatch', () => {
7
- return expect(
8
- // eslint-disable-next-line import/no-named-as-default-member
9
- queue.dispatch({ someData: 'someValue' }, 'pingQueue')
10
- ).resolves.toMatchObject({
11
- MessageId: 'MessageId',
12
- mocked: {
13
- opts: {},
14
- params: {
15
- MessageBody: '{"someData":"someValue"}',
16
- QueueUrl: `${aws.sqs.queues.pingQueue.url}`,
17
- },
18
- },
19
- });
20
- });
21
-
22
- it('test queue dispatch', () => {
23
- return expect(
24
- dispatch({ someData: 'someValue' }, 'pingQueue')
25
- ).resolves.toMatchObject({
26
- MessageId: 'MessageId',
27
- mocked: {
28
- opts: {},
29
- params: {
30
- MessageBody: '{"someData":"someValue"}',
31
- QueueUrl: `${aws.sqs.queues.pingQueue.url}`,
32
- },
33
- },
34
- });
35
- });
36
-
37
- it('test queue dispatch with empty payload', () => {
38
- return expect(() => dispatch()).toThrow(
39
- new LesgoException(
40
- 'payload is undefined in dispatch()',
41
- 'SQSSERVICE_DISPATCH_PAYLOAD_UNDEFINED'
42
- )
43
- );
44
- });
45
-
46
- it('test queue dispatch with empty queueName', () => {
47
- return expect(() => dispatch({ someData: 'someValue' })).toThrow(
48
- new LesgoException(
49
- 'queueName is undefined in dispatch()',
50
- 'SQSSERVICE_DISPATCH_QUEUENAME_UNDEFINED'
51
- )
52
- );
53
- });
54
- });
@@ -1,374 +0,0 @@
1
- import LesgoException from '../../exceptions/LesgoException';
2
- import validateFields from '../validateFields';
3
-
4
- const FILE = 'Utils/validateFields';
5
-
6
- const params = {
7
- Id: 99,
8
- name: 'name',
9
- email: 'email@mail.com',
10
- roles: { admin: 'admin' },
11
- listItem: ['apple', 'banana'],
12
- status: 'active',
13
- decimalCheck: 1.99,
14
- totalRecord: 99,
15
- functionCheck: () => {},
16
- jsonCheck: JSON.stringify({
17
- test: 'json value',
18
- testArray: ['withvalues', 'here'],
19
- testNum: 1,
20
- }),
21
- statusCollection: ['active', 'inactive', 'active'],
22
- };
23
-
24
- const validFields = [
25
- { key: 'Id', type: 'number', required: true },
26
- { key: 'name', type: 'string', required: true },
27
- { key: 'email', type: 'email', required: true },
28
- { key: 'roles', type: 'object', required: true },
29
- { key: 'listItem', type: 'array', required: true },
30
- {
31
- key: 'status',
32
- type: 'enum',
33
- enumValues: ['active', 'inactive'],
34
- required: true,
35
- },
36
- {
37
- key: 'decimalCheck',
38
- type: 'decimal',
39
- required: true,
40
- },
41
- { key: 'totalRecord', type: 'number', required: true },
42
- {
43
- key: 'functionCheck',
44
- type: 'function',
45
- required: true,
46
- },
47
- {
48
- key: 'jsonCheck',
49
- type: 'json',
50
- required: true,
51
- },
52
- {
53
- key: 'statusCollection',
54
- type: 'enum',
55
- enumValues: ['active', 'inactive'],
56
- required: true,
57
- isCollection: true,
58
- },
59
- ];
60
-
61
- describe('test Utils/validateFields', () => {
62
- it('should return validated fields', () => {
63
- expect(validateFields(params, validFields)).toMatchObject(params);
64
- });
65
-
66
- it('should throw missing required field when not present', () => {
67
- const newParams = { ...params };
68
- delete newParams.email;
69
-
70
- try {
71
- const validated = validateFields(newParams, validFields);
72
-
73
- expect(validated).toThrow();
74
- } catch (e) {
75
- expect(e.name).toEqual('LesgoException');
76
- expect(e.message).toEqual("Missing required 'email'");
77
- expect(e.code).toEqual(`${FILE}::MISSING_REQUIRED_EMAIL`);
78
- expect(e.extra).toStrictEqual({
79
- field: validFields[2],
80
- });
81
- }
82
- });
83
-
84
- it('should throw invalid type when non-string value check', () => {
85
- const newParams = { ...params, name: 123 };
86
-
87
- try {
88
- const validated = validateFields(newParams, validFields);
89
-
90
- expect(validated).toThrow();
91
- } catch (e) {
92
- expect(e.name).toEqual('LesgoException');
93
- expect(e.message).toEqual(`Invalid type for 'name', expecting 'string'`);
94
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_NAME`);
95
- expect(e.extra).toStrictEqual({
96
- field: validFields[1],
97
- value: 123,
98
- });
99
- }
100
- });
101
-
102
- it('should throw invalid type when non-object value check', () => {
103
- const newParams = { ...params, roles: 1597929335 };
104
-
105
- try {
106
- const validated = validateFields(newParams, validFields);
107
-
108
- expect(validated).toThrow();
109
- } catch (e) {
110
- expect(e.name).toEqual('LesgoException');
111
- expect(e.message).toEqual(`Invalid type for 'roles', expecting 'object'`);
112
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_ROLES`);
113
- expect(e.extra).toStrictEqual({
114
- field: validFields[3],
115
- value: 1597929335,
116
- });
117
- }
118
- });
119
-
120
- it('should throw invalid type when non-number value check', () => {
121
- const newParams = { ...params, Id: '123' };
122
-
123
- try {
124
- const validated = validateFields(newParams, validFields);
125
-
126
- expect(validated).toThrow();
127
- } catch (e) {
128
- expect(e.name).toEqual('LesgoException');
129
- expect(e.message).toEqual(`Invalid type for 'Id', expecting 'number'`);
130
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_ID`);
131
- expect(e.extra).toStrictEqual({
132
- field: validFields[0],
133
- value: '123',
134
- });
135
- }
136
- });
137
-
138
- it('should throw invalid type when non-array value check', () => {
139
- const newParams = { ...params, listItem: { created_at: 1597929335 } };
140
-
141
- try {
142
- const validated = validateFields(newParams, validFields);
143
-
144
- expect(validated).toThrow();
145
- } catch (e) {
146
- expect(e.name).toEqual('LesgoException');
147
- expect(e.message).toEqual(
148
- `Invalid type for 'listItem', expecting 'array'`
149
- );
150
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_LISTITEM`);
151
- expect(e.extra).toStrictEqual({
152
- field: validFields[4],
153
- value: { created_at: 1597929335 },
154
- });
155
- }
156
- });
157
-
158
- it('should throw required when array value is empty but required', () => {
159
- const newParams = { ...params, listItem: [] };
160
-
161
- expect(() => validateFields(newParams, validFields)).toThrow(
162
- new LesgoException(
163
- `Missing required 'listItem'`,
164
- `${FILE}::MISSING_REQUIRED_LISTITEM`
165
- )
166
- );
167
- });
168
-
169
- it('should not throw invalid type when array value is empty and not required', () => {
170
- const newParams = { ...params, listItem: [] };
171
- const newValidFields = [
172
- { key: 'listItem', type: 'array', required: false },
173
- ];
174
-
175
- expect(validateFields(newParams, newValidFields)).toMatchObject({
176
- listItem: [],
177
- });
178
- });
179
-
180
- it('should throw invalid type when non-enum value check', async () => {
181
- const newParams = { ...params, status: 'private' };
182
-
183
- expect(() => validateFields(newParams, validFields)).toThrow(
184
- new LesgoException(
185
- `Invalid type for 'status', expecting 'enum'`,
186
- `Invalid type for 'status', expecting 'enum'`
187
- )
188
- );
189
-
190
- try {
191
- const validated = validateFields(newParams, validFields);
192
-
193
- expect(validated).toThrow();
194
- } catch (e) {
195
- expect(e.name).toEqual('LesgoException');
196
- expect(e.message).toEqual(`Invalid type for 'status', expecting 'enum'`);
197
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_STATUS`);
198
- expect(e.extra).toStrictEqual({
199
- field: validFields[5],
200
- value: 'private',
201
- });
202
- }
203
- });
204
-
205
- it('should throw invalid type when non-function value check', async () => {
206
- const newParams = {
207
- ...params,
208
- functionCheck: { not: 'function' },
209
- };
210
-
211
- try {
212
- const validated = validateFields(newParams, validFields);
213
-
214
- expect(validated).toThrow();
215
- } catch (e) {
216
- expect(e.name).toEqual('LesgoException');
217
- expect(e.message).toEqual(
218
- `Invalid type for 'functionCheck', expecting 'function'`
219
- );
220
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_FUNCTIONCHECK`);
221
- expect(e.extra).toStrictEqual({
222
- field: validFields[8],
223
- value: { not: 'function' },
224
- });
225
- }
226
- });
227
-
228
- it.each`
229
- value
230
- ${'not a json'}
231
- ${3}
232
- ${'"invalid'}
233
- ${{ not: 'json' }}
234
- ${'{"missing":"bracket"}}'}
235
- `(
236
- 'should throw invalid type when non-json $value check',
237
- async ({ value }) => {
238
- const newParams = {
239
- ...params,
240
- jsonCheck: value,
241
- };
242
-
243
- try {
244
- const validated = validateFields(newParams, validFields);
245
-
246
- expect(validated).toThrow();
247
- } catch (e) {
248
- expect(e.name).toEqual('LesgoException');
249
- expect(e.message).toEqual(
250
- `Invalid type for 'jsonCheck', expecting 'json'`
251
- );
252
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_JSONCHECK`);
253
- expect(e.extra).toStrictEqual({
254
- field: validFields[9],
255
- value,
256
- });
257
- }
258
- }
259
- );
260
-
261
- it('should throw invalid type when non-collection value check', async () => {
262
- const newParams = {
263
- ...params,
264
- statusCollection: 'active',
265
- };
266
-
267
- try {
268
- const validated = validateFields(newParams, validFields);
269
-
270
- expect(validated).toThrow();
271
- } catch (e) {
272
- expect(e.name).toEqual('LesgoException');
273
- expect(e.message).toEqual(
274
- `Invalid type for 'statusCollection', expecting collection of 'enum'`
275
- );
276
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_STATUSCOLLECTION`);
277
- expect(e.extra).toStrictEqual({
278
- field: validFields[10],
279
- value: 'active',
280
- });
281
- }
282
- });
283
-
284
- it('should ignore type when collection value check is non-required', async () => {
285
- const newParams = { ...params };
286
- const newValidFields = [...validFields];
287
- delete newParams.statusCollection;
288
- newValidFields[10].required = false;
289
-
290
- const validated = validateFields(newParams, newValidFields);
291
-
292
- expect(validated).toStrictEqual(newParams);
293
- });
294
-
295
- it('should throw invalid type when non-enum collection value check', async () => {
296
- const newParams = {
297
- ...params,
298
- statusCollection: ['archive'],
299
- };
300
-
301
- try {
302
- const validated = validateFields(newParams, validFields);
303
-
304
- expect(validated).toThrow();
305
- } catch (e) {
306
- expect(e.name).toEqual('LesgoException');
307
- expect(e.message).toEqual(
308
- `Invalid type for 'statusCollection', expecting collection of 'enum'`
309
- );
310
- expect(e.code).toEqual(`${FILE}::INVALID_TYPE_STATUSCOLLECTION`);
311
- expect(e.extra).toStrictEqual({
312
- field: validFields[10],
313
- value: 'archive',
314
- });
315
- }
316
- });
317
-
318
- it('should return only valid and allowed fields when other fields are received', () => {
319
- const newParams = { ...params, someOtherKey: 'someOtherValue' };
320
- const validated = validateFields(newParams, validFields);
321
-
322
- expect(validated).toMatchObject(params);
323
- expect(validated.Id).toBeDefined();
324
- expect(validated.someOtherKey).toBeUndefined();
325
- });
326
-
327
- it('should return success with validated data for non-required fields', () => {
328
- const newParams = { ...params };
329
- delete newParams.listItem;
330
-
331
- validFields[4].required = false;
332
- const validated = validateFields(newParams, validFields);
333
-
334
- expect(validated).toMatchObject(newParams);
335
- expect(validated.Id).toBeDefined();
336
- expect(validated.listItem).toBeUndefined();
337
-
338
- validFields[4].required = true;
339
- });
340
-
341
- it('should return success with validated data for 0 number', () => {
342
- const newParams = { ...params, totalRecord: 0 };
343
- const validated = validateFields(newParams, validFields);
344
-
345
- expect(validated).toMatchObject(newParams);
346
- expect(validated.totalRecord).toBeDefined();
347
- expect(validated.totalRecord).toEqual(0);
348
- });
349
-
350
- it('should return success with validated data for number without required', () => {
351
- const newParams = { ...params };
352
- validFields[7].required = false;
353
-
354
- const validated = validateFields(newParams, validFields);
355
-
356
- expect(validated).toMatchObject(newParams);
357
- expect(validated.totalRecord).toBeDefined();
358
- expect(validated.totalRecord).toEqual(99);
359
-
360
- validFields[7].required = true;
361
- });
362
-
363
- it('should return error with missing required number', () => {
364
- const newParams = { ...params };
365
- delete newParams.totalRecord;
366
-
367
- expect(() => validateFields(newParams, validFields)).toThrow(
368
- new LesgoException(
369
- `Missing required 'totalRecord'`,
370
- `${FILE}::MISSING_REQUIRED_TOTALRECORD`
371
- )
372
- );
373
- });
374
- });
@@ -1,215 +0,0 @@
1
- import config from 'Config/cache'; // eslint-disable-line import/no-unresolved
2
- import ElastiCacheService from '../services/ElastiCacheService';
3
- import LesgoException from '../exceptions/LesgoException';
4
- import logger from './logger';
5
- import isEmpty from './isEmpty';
6
-
7
- const FILE = 'Lesgo/utils/cache';
8
-
9
- /**
10
- * Reusable instance
11
- */
12
- const singleton = {};
13
-
14
- /**
15
- * Instantiate the Cache Service
16
- *
17
- * @param {String} connectionName The name of the driver config to connect to
18
- * @param {boolean} persists To use persistent connection or otherwise
19
- *
20
- * @return {object} Returns the driver
21
- */
22
- const ec = (connectionName = '', persists = false) => {
23
- const conn = connectionName || config.default;
24
- if (singleton[conn]) {
25
- return singleton[conn];
26
- }
27
-
28
- let { options } = config.connections[conn];
29
-
30
- if (config.connections[conn].url) {
31
- // this is a legacy config with memcached-elasticache package
32
- // remap to new config
33
- options = {
34
- ...options,
35
- hosts: [config.connections[conn].url],
36
- };
37
- }
38
-
39
- const { driver } = new ElastiCacheService(options);
40
-
41
- if (persists) {
42
- singleton[conn] = driver;
43
- }
44
-
45
- return driver;
46
- };
47
-
48
- /**
49
- * Persistent connection if declared in the handler
50
- */
51
- const pConnect = (connectionName = '') => {
52
- return ec(connectionName, true);
53
- };
54
-
55
- /**
56
- * Fetch data from cache by key
57
- *
58
- * @param {string} key Cache key to fetch data.
59
- * @return {promise} Returns promised.
60
- */
61
- const get = async key => {
62
- try {
63
- const client = ec();
64
- const data = await client.get(key);
65
- logger.debug(`${FILE}::Fetched cache data`, { key, data });
66
- if (isEmpty(singleton)) {
67
- logger.debug(`${FILE}::Ending cache connection`);
68
- await client.disconnect();
69
- logger.debug(`${FILE}::Ended cache connection`);
70
- }
71
- return data;
72
- } catch (err) {
73
- throw new LesgoException(err.message, 'CACHE_GET_EXCEPTION', 500, err);
74
- }
75
- };
76
-
77
- /**
78
- * Fetch data from cache by multiple keys
79
- *
80
- * @param {array} keys Array of Cache keys to fetch data.
81
- * @return {promise} Returns promised.
82
- */
83
- const getMulti = async keys => {
84
- try {
85
- const client = ec();
86
- const data = await client.getMulti(keys);
87
- logger.debug(`${FILE}::Fetched cache data`, { keys, data });
88
- if (isEmpty(singleton)) {
89
- logger.debug(`${FILE}::Ending cache connection`);
90
- await client.disconnect();
91
- logger.debug(`${FILE}::Ended cache connection`);
92
- }
93
- return data;
94
- } catch (err) {
95
- throw new LesgoException(
96
- err.message,
97
- 'CACHE_GET_MULTI_EXCEPTION',
98
- 500,
99
- err
100
- );
101
- }
102
- };
103
-
104
- /**
105
- * Save data to cache
106
- *
107
- * @param {string} key Cache key to save data.
108
- * @param {string|object} val Value of data to cache.
109
- * @param {integer} lifetime Time in seconds to expire cache.
110
- * @return {promise} Returns promised.
111
- */
112
- const set = async (key, val, lifetime) => {
113
- try {
114
- const client = ec();
115
- await client.set(key, val, lifetime);
116
- logger.debug(`${FILE}::Cache stored`, { key, val, lifetime });
117
- if (isEmpty(singleton)) {
118
- logger.debug(`${FILE}::Ending cache connection`);
119
- await client.disconnect();
120
- logger.debug(`${FILE}::Ended cache connection`);
121
- }
122
- } catch (err) {
123
- throw new LesgoException(err.message, 'CACHE_SET_EXCEPTION', 500, err);
124
- }
125
- };
126
-
127
- /**
128
- * Remove the key from cache
129
- *
130
- * @param {string} key Cache key to delete.
131
- * @return {promise} Returns promised.
132
- */
133
- const del = async key => {
134
- try {
135
- const client = ec();
136
- await client.delete(key);
137
- logger.debug(`${FILE}::Key deleted from cache`, { key });
138
- if (isEmpty(singleton)) {
139
- logger.debug(`${FILE}::Ending cache connection`);
140
- await client.disconnect();
141
- logger.debug(`${FILE}::Ended cache connection`);
142
- }
143
- } catch (err) {
144
- throw new LesgoException(err.message, 'CACHE_DEL_EXCEPTION', 500, err);
145
- }
146
- };
147
-
148
- /**
149
- * Remove data from cache by multiple keys
150
- *
151
- * @param {array} keys Array of Cache keys to delete data.
152
- * @return {promise} Returns promised.
153
- */
154
- const delMulti = async keys => {
155
- try {
156
- const client = ec();
157
- await client.deleteMulti(keys);
158
- logger.debug(`${FILE}::Keys deleted from cache`, { keys });
159
- if (isEmpty(singleton)) {
160
- logger.debug(`${FILE}::Ending cache connection`);
161
- await client.disconnect();
162
- logger.debug(`${FILE}::Ended cache connection`);
163
- }
164
- } catch (err) {
165
- throw new LesgoException(
166
- err.message,
167
- 'CACHE_DEL_MULTI_EXCEPTION',
168
- 500,
169
- err
170
- );
171
- }
172
- };
173
-
174
- /**
175
- * Ends the connection
176
- *
177
- * @return {promise} Returns promised.
178
- */
179
- const end = async () => {
180
- try {
181
- logger.debug(`${FILE}::Ending cache connection`);
182
- await ec().disconnect();
183
- logger.debug(`${FILE}::Ended cache connection`);
184
-
185
- if (!isEmpty(Object.keys(singleton))) {
186
- Object.keys(singleton).forEach(key => {
187
- delete singleton[key];
188
- });
189
- }
190
- } catch (err) {
191
- throw new LesgoException(err.message, 'CACHE_END_EXCEPTION', 500, err);
192
- }
193
- };
194
-
195
- /**
196
- * Alias of end()
197
- *
198
- * @return {promise} Returns promised.
199
- */
200
- const disconnect = () => {
201
- return end();
202
- };
203
-
204
- export default {
205
- pConnect,
206
- ec,
207
- get,
208
- getMulti,
209
- set,
210
- del,
211
- delMulti,
212
- end,
213
- disconnect,
214
- singleton,
215
- };
@@ -1,77 +0,0 @@
1
- import crypto from 'crypto';
2
- import cryptoConfig from 'Config/crypto'; // eslint-disable-line import/no-unresolved
3
- import LesgoException from '../exceptions/LesgoException';
4
- import isEmpty from './isEmpty';
5
-
6
- const { algorithm, secretKey } = cryptoConfig;
7
- const ivLength = 16;
8
-
9
- export const encrypt = text => {
10
- if (isEmpty(text)) {
11
- throw new LesgoException(
12
- 'Empty parameter supplied on encrypt',
13
- 'CRYPTO_ENCRYPT_EMPTY_PARAMETER'
14
- );
15
- }
16
-
17
- const iv = crypto.randomBytes(ivLength);
18
- const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
19
-
20
- let encrypted = cipher.update(text);
21
- encrypted = Buffer.concat([encrypted, cipher.final()]);
22
-
23
- return `${iv.toString('hex')}:${encrypted.toString('hex')}`;
24
- };
25
-
26
- export const decrypt = text => {
27
- if (isEmpty(text)) {
28
- throw new LesgoException(
29
- 'Empty parameter supplied on decryp',
30
- 'CRYPTO_DECRYPT_EMPTY_PARAMETER'
31
- );
32
- }
33
-
34
- const textParts = text.split(':');
35
- const iv = Buffer.from(textParts.shift(), 'hex');
36
- const encryptedText = Buffer.from(textParts.join(':'), 'hex');
37
- const decipher = crypto.createDecipheriv(algorithm, secretKey, iv);
38
-
39
- let decrypted = decipher.update(encryptedText);
40
- decrypted = Buffer.concat([decrypted, decipher.final()]);
41
-
42
- return decrypted.toString();
43
- };
44
-
45
- export const hash = data => {
46
- if (isEmpty(data)) {
47
- throw new LesgoException(
48
- 'Empty parameter supplied on hash',
49
- 'CRYPTO_HASH_EMPTY_PARAMETER'
50
- );
51
- }
52
-
53
- const hashedValue = crypto
54
- .createHash('sha256', 'utf8')
55
- .update(data)
56
- .digest('hex');
57
-
58
- return hashedValue;
59
- };
60
-
61
- export const hashMD5 = data => {
62
- if (isEmpty(data)) {
63
- throw new LesgoException(
64
- 'Empty parameter supplied on hashMD5',
65
- 'CRYPTO_HASHMD5_EMPTY_PARAMETER'
66
- );
67
- }
68
-
69
- const hashedValue = crypto
70
- .createHash('md5')
71
- .update(data)
72
- .digest('hex');
73
-
74
- return hashedValue;
75
- };
76
-
77
- export default { encrypt, decrypt, hash, hashMD5 };