piper-utils 1.0.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.
Files changed (38) hide show
  1. package/.babelrc +12 -0
  2. package/README.md +119 -0
  3. package/bin/main.js +1003 -0
  4. package/bin/main.js.LICENSE.txt +151 -0
  5. package/bin/main.js.map +1 -0
  6. package/bitbucket-pipelines.yml +14 -0
  7. package/jasmine.json +7 -0
  8. package/package.json +62 -0
  9. package/src/database/dbSetUp/migrations.js +45 -0
  10. package/src/database/dbUtils/queryStringUtils/accessRightsUtils.js +149 -0
  11. package/src/database/dbUtils/queryStringUtils/accessRightsUtils.test.js +177 -0
  12. package/src/database/dbUtils/queryStringUtils/createFilters.js +92 -0
  13. package/src/database/dbUtils/queryStringUtils/createFilters.test.js +37 -0
  14. package/src/database/dbUtils/queryStringUtils/createIncludes.js +45 -0
  15. package/src/database/dbUtils/queryStringUtils/createIncludes.test.js +20 -0
  16. package/src/database/dbUtils/queryStringUtils/createSort.js +36 -0
  17. package/src/database/dbUtils/queryStringUtils/createSort.test.js +60 -0
  18. package/src/database/dbUtils/queryStringUtils/defaultFilters.js +108 -0
  19. package/src/database/dbUtils/queryStringUtils/defaultFilters.test.js +50 -0
  20. package/src/database/dbUtils/queryStringUtils/findAll.js +42 -0
  21. package/src/database/dbUtils/queryStringUtils/findAll.test.js +79 -0
  22. package/src/database/dbUtils/queryStringUtils/mocks/mocks.js +127 -0
  23. package/src/dynamo/dynamoUtils.js +16 -0
  24. package/src/eventManager/handleEvents.js +32 -0
  25. package/src/eventManager/handleEvents.test.js +92 -0
  26. package/src/eventManager/handleFile.js +76 -0
  27. package/src/eventManager/handleFile.test.js +352 -0
  28. package/src/eventManager/publishEvents.js +99 -0
  29. package/src/eventManager/publishEvents.test.js +161 -0
  30. package/src/eventManager/watchBucket.js +41 -0
  31. package/src/eventManager/watchBucket.test.js +137 -0
  32. package/src/index.js +40 -0
  33. package/src/requestResonse/errorCodes.js +132 -0
  34. package/src/requestResonse/requestResponse.js +198 -0
  35. package/src/requestResonse/requestResponse.test.js +241 -0
  36. package/src/s3/S3Utils.js/S3Utils.js +20 -0
  37. package/src/sns/SNSUtils.js +18 -0
  38. package/webpack.config.js +50 -0
@@ -0,0 +1,241 @@
1
+ import {
2
+ failure, success, successHtml,
3
+ getCurrentUser, getCurrentUserNameFromCognitoEvent, parseBody, parseEvent, detectSequelizeError
4
+ } from './requestResponse.js';
5
+
6
+ describe('requestResponse', () => {
7
+ describe('success', () => {
8
+ it('should return 200', () => {
9
+ const res = success({ foo: 'bar' }, {});
10
+ expect(res).toEqual({
11
+ statusCode: 200, headers: {
12
+ 'Access-Control-Allow-Origin': '*',
13
+ 'Access-Control-Allow-Credentials': true
14
+ }, body: '{"foo":"bar"}'
15
+ });
16
+ });
17
+ });
18
+ describe('success', () => {
19
+ it('should call dbclose', () => {
20
+ const options = {
21
+ dbClose: () => {
22
+
23
+ }
24
+ };
25
+ const closeSpy = spyOn(options, 'dbClose');
26
+ const res = success({ foo: 'bar' }, options);
27
+ expect(closeSpy).toHaveBeenCalled();
28
+ });
29
+ });
30
+ describe('successHtml', () => {
31
+ it('should return 200', () => {
32
+ const res = successHtml('<html>test</html>', {});
33
+ expect(res).toEqual({
34
+ statusCode: 200, headers: {
35
+ 'Content-Type': 'text/html',
36
+ 'Access-Control-Allow-Origin': '*',
37
+ 'Access-Control-Allow-Credentials': true
38
+ }, body: '<html>test</html>'
39
+ });
40
+ });
41
+ it('should call dbclose', () => {
42
+ const options = {
43
+ dbClose: () => {
44
+
45
+ }
46
+ };
47
+ const closeSpy = spyOn(options, 'dbClose');
48
+ const res = successHtml('<html>test</html>', options);
49
+ expect(closeSpy).toHaveBeenCalled();
50
+ });
51
+ });
52
+ describe('getCurrentUserNameFromCognitoEvent', () => {
53
+ it('should return username', () => {
54
+ const event = {
55
+ request: {
56
+ userAttributes: {
57
+ 'cognito:email_alias': 'kevin@test.com'
58
+ }
59
+ }
60
+ };
61
+ const res = getCurrentUserNameFromCognitoEvent(event);
62
+ expect(res).toEqual('kevin@test.com');
63
+ });
64
+ it('should return username', () => {
65
+ const event = {
66
+ request: {
67
+ userAttributes: {}
68
+ }
69
+ };
70
+ const res = getCurrentUserNameFromCognitoEvent(event);
71
+ expect(res).toEqual('UNKNOWN USER');
72
+ });
73
+ });
74
+ describe('getCurrentUser', () => {
75
+ it('should return username', () => {
76
+ const event = {
77
+ requestContext: {
78
+ authorizer: {
79
+ 'custom:UID': '1',
80
+ 'email': 'kevin@test.com'
81
+ }
82
+ }
83
+ };
84
+ const res = getCurrentUser(event);
85
+ expect(res).toEqual({
86
+ username: 'kevin@test.com',
87
+ id: 1
88
+ });
89
+ });
90
+ it('should return username', () => {
91
+ const event = {
92
+ requestContext: {
93
+ authorizer: {}
94
+ }
95
+ };
96
+ const res = getCurrentUser(event);
97
+ expect(res).toEqual({
98
+ username: 'LOCAL TEST USER',
99
+ id: 0
100
+ });
101
+ });
102
+ });
103
+ describe('failure', () => {
104
+ it('should return an error', () => {
105
+ const res = failure({ foo: 'bar' }, {});
106
+ expect(res).toEqual({
107
+ statusCode: 500, headers: {
108
+ 'Access-Control-Allow-Origin': '*',
109
+ 'Access-Control-Allow-Credentials': true
110
+ }, body: '{"statusCode":500,"errorCode":"5XX","message":""}'
111
+ });
112
+ });
113
+ it('should look for errors in details return an error', () => {
114
+ const res = failure({ details: 'bar' }, {});
115
+ expect(res).toEqual({
116
+ statusCode: 400, headers: {
117
+ 'Access-Control-Allow-Origin': '*',
118
+ 'Access-Control-Allow-Credentials': true
119
+ }, body: '{"statusCode":400,"errorCode":"4000","message":"INTERNAL UTIL ERROR"}'
120
+ });
121
+ });
122
+ it('should deal with error code on body, but no details', () => {
123
+ const res = failure({ errorCode: 333 }, {});
124
+ expect(res).toEqual({
125
+ statusCode: 500, headers: {
126
+ 'Access-Control-Allow-Origin': '*',
127
+ 'Access-Control-Allow-Credentials': true
128
+ }, body: '{"statusCode":500,"errorCode":"5XX","message":""}'
129
+ });
130
+ });
131
+ it('should no details and no codes', () => {
132
+ const res = failure({ errorCode: 333, statusCode: 222 }, {});
133
+ expect(res).toEqual({
134
+ statusCode: 222, headers: {
135
+ 'Access-Control-Allow-Origin': '*',
136
+ 'Access-Control-Allow-Credentials': true
137
+ }, body: '{"statusCode":222,"errorCode":333,"message":"INTERNAL UTIL ERROR"}'
138
+ });
139
+ });
140
+ });
141
+ describe('parseBody', () => {
142
+ it('parse a json string', () => {
143
+ const event = {
144
+ body: '{"foo":"bar"}'
145
+ };
146
+
147
+ const res = parseBody(event);
148
+ expect(res).toEqual({ foo: 'bar' });
149
+ });
150
+ it('should handle no parseable things', () => {
151
+ const event = {
152
+ body: 'DRY DUCK'
153
+ };
154
+
155
+ try {
156
+ parseBody(event);
157
+ } catch (e) {
158
+ expect(e.message).toEqual('INVALID JSON');
159
+ }
160
+
161
+ });
162
+ });
163
+ describe('parseEvent', () => {
164
+ it('parse a json string', () => {
165
+ const event = '{"foo":"bar"}';
166
+
167
+ const res = parseEvent(event, () => {
168
+ });
169
+ expect(res).toEqual({ foo: 'bar' });
170
+ });
171
+ it('should handle non parsable things', () => {
172
+ const event = {
173
+ body: 'DRY DUCK'
174
+ };
175
+
176
+ try {
177
+ parseEvent(event, () => {
178
+ });
179
+ } catch (e) {
180
+ expect(e.message).toEqual('INVALID JSON');
181
+ }
182
+
183
+ });
184
+ it('should handle no parsable things', () => {
185
+ const event = 'DRY DUCK';
186
+
187
+ try {
188
+ parseEvent(event, () => {
189
+ });
190
+ } catch (e) {
191
+ expect(e.message).toEqual('INVALID JSON');
192
+ }
193
+ });
194
+ it('should handle no call back', () => {
195
+ const event = 'DRY DUCK';
196
+
197
+ try {
198
+ parseEvent(event);
199
+ } catch (e) {
200
+ expect(e.message).toEqual('INVALID JSON');
201
+ }
202
+ });
203
+ it('should handle event already an object', () => {
204
+ const event = { good: 'boy' };
205
+
206
+ const res = parseEvent(event);
207
+ expect(res).toEqual({ good: 'boy' });
208
+ });
209
+ it('should handle parsable stuff', () => {
210
+ const event = '{"good":"boy"}';
211
+
212
+ const res = parseEvent(event);
213
+ expect(res).toEqual({ good: 'boy' });
214
+ });
215
+ it('should handle undefined', () => {
216
+ const event = undefined;
217
+
218
+ const res = parseEvent(event);
219
+ expect(res).toEqual({});
220
+ });
221
+ });
222
+ describe('detectSequelizeError', () => {
223
+ it('should find sequelize errors', () => {
224
+ const body = {
225
+ errors: [
226
+ {
227
+ message: 'I'
228
+ }
229
+ ],
230
+ parent: 'eggs',
231
+ original: {
232
+ detail: 'like'
233
+ },
234
+ TypeError: 'green'
235
+ };
236
+
237
+ const res = detectSequelizeError(body);
238
+ expect(res).toEqual({ message: 'Ilikegreeneggs' });
239
+ });
240
+ });
241
+ });
@@ -0,0 +1,20 @@
1
+ import { S3 } from '@aws-sdk/client-s3';
2
+
3
+ export function s3Utils(action, params) {
4
+ const s3 = new S3();
5
+ return s3[action](params);
6
+ }
7
+
8
+ const S3Util = {
9
+ getObject: (params) => getS3(params),
10
+ deleteObject: (params) => s3Utils('deleteObject', params),
11
+ putObject: (params) => s3Utils('putObject', params),
12
+ listObjectsV2: (params) => s3Utils('listObjectsV2', params)
13
+ };
14
+ export default S3Util;
15
+
16
+ async function getS3(params) {
17
+ const s3 = new S3();
18
+ const response = await s3.getObject(params);
19
+ return response.Body.transformToString('utf-8');
20
+ }
@@ -0,0 +1,18 @@
1
+ import { SNS } from '@aws-sdk/client-sns';
2
+
3
+ const SNSUtil = {
4
+ publish: (params) => publish(params),
5
+ createTopic: (params) => createTopic(params)
6
+ };
7
+
8
+ async function publish(params) {
9
+ const sns = new SNS();
10
+ return await sns.publish(params);
11
+ }
12
+
13
+ async function createTopic(params) {
14
+ const sns = new SNS();
15
+ return await sns.createTopic(params);
16
+ }
17
+
18
+ export default SNSUtil;
@@ -0,0 +1,50 @@
1
+ const path = require('path');
2
+ const nodeExternals = require('webpack-node-externals');
3
+ const TerserPlugin = require("terser-webpack-plugin");
4
+ const isProd = process.env.BUILD_ENV === 'production';
5
+ console.log('----------->BUILD TYPE:', isProd);
6
+
7
+ module.exports = {
8
+ entry: './src/index.js',
9
+ target: 'node',
10
+ mode: isProd ? 'production' : 'development',
11
+ externals: [nodeExternals()],
12
+ optimization: {
13
+ minimize: isProd,
14
+ minimizer: [
15
+ new TerserPlugin({
16
+ extractComments: false,
17
+ terserOptions: {
18
+ keep_fnames: true,
19
+ mangle:false,
20
+ format: {
21
+ comments: true,
22
+ },
23
+ }
24
+ })
25
+ ]
26
+ },
27
+ module: {
28
+ rules: [
29
+ {
30
+ test: /\.js$/,
31
+ use: [
32
+ {
33
+ loader: 'babel-loader',
34
+ options: {
35
+ retainLines: true
36
+ }
37
+ }
38
+ ],
39
+ include: __dirname,
40
+ exclude: /node_modules/
41
+ }
42
+ ]
43
+ },
44
+ output: {
45
+ libraryTarget: 'commonjs',
46
+ path: path.join(__dirname, './bin'),
47
+ filename: '[name].js'
48
+ },
49
+ devtool: isProd ? 'nosources-source-map' : 'inline-source-map'
50
+ };