@izara_project/izara-core-generate-service-code 1.0.39 → 1.0.41

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/package.json +6 -6
  2. package/src/generateCode/generateFlowSchema/event/ownTopicComponent/flowSchemaComponent/handler/template.ejs +6 -3
  3. package/src/generateCode/generateFlowSchema/event/ownTopicComponent/flowSchemaComponent/mainFunction/template.ejs +7 -3
  4. package/src/generateCode/generateFlowSchema/event/s3Component/upload/createObject/handler/template.ejs +2 -1
  5. package/src/generateCode/generateFlowSchema/event/s3Component/upload/createObjectComplete/mainFunction/template.ejs +3 -2
  6. package/src/generateCode/generateFlowSchema/event/s3Component/upload/preSignUrl/reservedLimit/mainFunction/template.ejs +3 -2
  7. package/src/generateCode/generateFlowSchema/registerComponent/complete/mainFunction/template.ejs +2 -2
  8. package/src/generateCode/generateFlowSchema/registerComponent/wbs/handler/template.ejs +3 -1
  9. package/src/generateCode/generateFlowSchema/registerComponent/wbs/mainFunction/template.ejs +3 -4
  10. package/src/generateCode/generateFlowSchema/webSocketComponent/webSocketComplete/mainFunction/template.ejs +2 -1
  11. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/handler/data.js +56 -0
  12. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/handler/templateAwaitingStep.ejs +105 -0
  13. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/handler/templatePaginated.ejs +137 -0
  14. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/mainFunction/data.js +57 -0
  15. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/mainFunction/templateAwaitingStep.ejs +129 -0
  16. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/mainFunction/templatePaginated.ejs +59 -0
  17. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/sns-sqs/data.js +50 -0
  18. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/sns-sqs/queueNoTopic.ejs +43 -0
  19. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/sns-sqs/snsTemplate.ejs +58 -0
  20. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/sns-sqs/sqsTemplate.ejs +43 -0
  21. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/yaml/data.js +47 -0
  22. package/src/generateCode/generateFlowStepWithPlugIn/byConfig/yaml/template.ejs +43 -0
  23. package/src/generateCode/generateFlowStepWithPlugIn/firstFlowStep/handler/data.js +7 -1
  24. package/src/generateCode/generateFlowStepWithPlugIn/firstFlowStep/mainFunction/data.js +38 -6
  25. package/src/generateCode/generateFlowStepWithPlugIn/firstFlowStep/mainFunction/template.ejs +5 -16
  26. package/src/generateCode/generateFlowStepWithPlugIn/firstFlowStep/mainFunction/templateByConfig/awaitingSteps.ejs +63 -0
  27. package/src/generateCode/generateFlowStepWithPlugIn/firstFlowStep/mainFunction/templateByConfig/templateSendPlugin.ejs +18 -0
  28. package/src/generateCode/generateFlowStepWithPlugIn/firstFlowStep/queueNtopic/sqsTemplate.ejs +7 -7
  29. package/src/generateCode/generateFlowStepWithPlugIn/firstFlowStep/topicOut/data.js +1 -1
  30. package/src/generateCode/generateFlowStepWithPlugIn/index(old).js +290 -0
  31. package/src/generateCode/generateFlowStepWithPlugIn/index.js +413 -155
  32. package/src/generateCode/generateInitialSetup/externalServiceComponent/lambdaRole/data.js +22 -0
  33. package/src/generateCode/generateInitialSetup/externalServiceComponent/snsTopicSubscriptions/data.js +16 -0
  34. package/src/generateCode/generateSchema/actionEndpointCompleteComponent/create/mainFunction/template.ejs +44 -4
  35. package/src/generateCode/generateSchema/actionEndpointComponent/mainFunction/create/main/template.ejs +58 -40
  36. package/src/libs/Libs.js +21 -15
  37. package/src/generateCode/generateInitialSetup/externalServiceComponent/lambdaRole/request.json +0 -18
  38. package/src/generateCode/generateInitialSetup/externalServiceComponent/snsTopicSubscriptions/request.json +0 -12
@@ -0,0 +1,290 @@
1
+ /*
2
+ Copyright (C) 2020 Sven Mason <http://izara.io>
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Affero General Public License as
6
+ published by the Free Software Foundation, either version 3 of the
7
+ License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Affero General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Affero General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import path from 'path'
19
+ import yaml from 'yaml';
20
+ import { consts as coreConsts } from '@izara_project/izara-core-library-service-schemas';
21
+ import libs from '#libs/Libs.js';
22
+ const { createSnsResource, createSqsResource, defaultIamRolePerAction, getLocalOrS3ObjectSchema, getLocalOrS3FlowSchema, resourceNames } = libs;
23
+ import utils from '#libs/Utils.js';
24
+ const { firstLetterUpperCase: upperCase, firstLetterLowerCase: lowerCase } = utils;
25
+ import consts from '#libs/Consts.js';
26
+ const { SOURCE_PATH, HANDLER, RESOURCE_CLASSES } = consts;
27
+
28
+ import yamlFlowStep from './firstFlowStep/yaml/data.js';
29
+ import handlerFlowStep from './firstFlowStep/handler/data.js';
30
+ import mainFunctionFlowStep from './firstFlowStep/mainFunction/data.js';
31
+ import sqsFlowStep from './firstFlowStep/queueNtopic/data.js';
32
+ import snsOut from './firstFlowStep/topicOut/data.js';
33
+
34
+ import yamlRecievePlugIn from './afterFirstFlowStep/recievePlugIn/yaml/data.js';
35
+ import handlerRecievePlugIn from './afterFirstFlowStep/recievePlugIn/handler/data.js';
36
+ import mainFunctionRecievePlugIn from './afterFirstFlowStep/recievePlugIn/mainFunction/data.js';
37
+
38
+ // import yamlSendPlugIn from './afterFirstFlowStep/sendPlugIn/yaml/data.js';
39
+ // import handlerSendPlugIn from './afterFirstFlowStep/sendPlugIn/handler/data.js';
40
+ // import mainFunctionSendPlugIn from './afterFirstFlowStep/sendPlugIn/mainFunction/data.js';
41
+
42
+
43
+ /**
44
+ * Generates code from templates based on object schemas
45
+ * @param {Object} _izContext
46
+ * @param {string} appPath
47
+ * @param {Array} allObjSchemas
48
+ * @param {Array} allRelationshipSchemas
49
+ * @param {Object} settings
50
+ * @returns {Promise<Array>}
51
+ */
52
+ async function generatePlunIg(
53
+ _izContext,
54
+ appPath,
55
+ allObjSchemas,
56
+ allLocalFlowSchemas,
57
+ ) {
58
+ const allCreateSourceParams = [];
59
+
60
+ try {
61
+ const startAllMs = Date.now();
62
+ const totalSchemas = Array.isArray(allObjSchemas)
63
+ ? allObjSchemas.length
64
+ : 0;
65
+
66
+
67
+ console.info(`[plugIn] start totalSchemas=${totalSchemas}`);
68
+ if (totalSchemas === 0) return [];
69
+
70
+ const configPath = path.join(appPath, '../config/serverless.config.yml');
71
+ const serverlessConfig = fs.readFileSync(configPath, 'utf8');
72
+ const config = yaml.parse(serverlessConfig);
73
+ const serviceTag = config.main_config.iz_serviceTag;
74
+
75
+ for (const flowSchema of allLocalFlowSchemas) {
76
+ for (const [flowStepName, flowStepConfig] of Object.entries(flowSchema.flowSteps)) {
77
+ if (flowStepName === "In" || flowStepName === "Out") {
78
+ continue;
79
+ } else {
80
+ const flowTag = flowSchema.flowTag;
81
+ const roleName = upperCase(flowTag);
82
+ const resourceApis = [];
83
+ let handler;
84
+ const additionalResourcePermission = defaultIamRolePerAction();
85
+
86
+ const data = {
87
+ additionalResourcePermission: additionalResourcePermission,
88
+ flowStepName: flowStepName,
89
+ flowTag: flowTag,
90
+ resourceApis: resourceApis,
91
+ hookTagSetting: `${flowSchema.flowTag}FunctionSetting`,
92
+ functionName: upperCase(flowSchema.flowTag) + upperCase(flowStepName),
93
+ roleName: roleName,
94
+ };
95
+
96
+ if (flowStepConfig.event.includes("ownTopic")) {
97
+ data['eventFlow'] = 'ownTopic'
98
+ handler = HANDLER.hdrSqs;
99
+ additionalResourcePermission.push(
100
+ createSnsResource(
101
+ [
102
+ upperCase(flowSchema.flowTag) + upperCase(flowStepName) + "_In"
103
+ ]
104
+ ),
105
+ createSqsResource(
106
+ [
107
+ upperCase(flowSchema.flowTag) + upperCase(flowStepName) + upperCase(handler),
108
+ upperCase(flowSchema.flowTag) + upperCase(flowStepName) + upperCase(handler) + "DSQ"
109
+ ]
110
+ )
111
+ );
112
+ } else if (flowStepConfig.event.includes("extTopic")) {
113
+ data['eventFlow'] = 'extTopic'
114
+ handler = HANDLER.hdrSqs;
115
+ additionalResourcePermission.push(
116
+ createSqsResource(
117
+ [
118
+ upperCase(flowSchema.flowTag) + upperCase(flowStepName) + upperCase(handler),
119
+ upperCase(flowSchema.flowTag) + upperCase(flowStepName) + upperCase(handler) + "DSQ"
120
+ ]
121
+ )
122
+ );
123
+ } else if (flowStepConfig.event.includes("lambdaSyncInv")) {
124
+ handler = HANDLER.hdrInv;
125
+ } else if (flowStepConfig.event.includes("lambdaSyncApi")) {
126
+ handler = HANDLER.hdrApi;
127
+ resourceApis.push({
128
+ path: `${serviceTag}/${lowerCase(flowTag)}${lowerCase(flowStepName)}`,
129
+ pathWithUser: `${serviceTag}/${lowerCase(flowTag)}${lowerCase(flowStepName)}/{targetUserId}`,
130
+ method: 'post',
131
+ hookTagApp: `${upperCase(flowStepName)}${upperCase(handler)}AppLevelAuthorizer`,
132
+ hookTagUser: `${upperCase(flowStepName)}${upperCase(handler)}UserLevelAuthorizer`
133
+ })
134
+ } else if (flowStepConfig.event.includes("queue")) {
135
+ handler = HANDLER.hdrDsq;
136
+ additionalResourcePermission.push(
137
+ createSqsResource(
138
+ [
139
+ upperCase(flowSchema.flowTag) + upperCase(flowStepName) + upperCase(handler),
140
+ upperCase(flowSchema.flowTag) + upperCase(flowStepName) + upperCase(handler) + "DSQ"
141
+ ]
142
+ )
143
+ );
144
+ };
145
+
146
+ Object.assign(data, {
147
+ handlerType: handler,
148
+ handlerPath: path.join(
149
+ SOURCE_PATH.resourceLocationFlowSchema,
150
+ upperCase(flowSchema.flowTag),
151
+ upperCase(flowStepName),
152
+ 'source/',
153
+ `${upperCase(flowTag)}${upperCase(flowStepName)}_${upperCase(handler)}`,
154
+ ),
155
+ functionNameConfig: upperCase(flowSchema.flowTag) + upperCase(flowStepName) + upperCase(handler),
156
+ event: resourceNames(RESOURCE_CLASSES.sqs, upperCase(flowSchema.flowTag) + upperCase(flowStepName) + upperCase(handler)),
157
+ queueName: upperCase(flowSchema.flowTag) + upperCase(flowStepName),
158
+ })
159
+
160
+ const yamlFlowStepRes = await yamlFlowStep(_izContext, data, appPath);
161
+ const handlerResult = await handlerFlowStep(_izContext, data, appPath);
162
+ allCreateSourceParams.push(yamlFlowStepRes, handlerResult);
163
+
164
+ if (handler === HANDLER.hdrSqs || handler === HANDLER.hdrDsq) {
165
+ const sqsResult = await sqsFlowStep(_izContext, data, appPath);
166
+ allCreateSourceParams.push(sqsResult);
167
+ }
168
+
169
+ const snsOutResult = await snsOut(_izContext, data, appPath);
170
+ allCreateSourceParams.push(snsOutResult);
171
+
172
+ let mainResult;
173
+
174
+ if (flowStepConfig.hasOwnProperty("plugInHooks")) {
175
+ // for (const plugInHookFlow of flowStepConfig.plugInHooks) {
176
+ for (let i = 0; i < flowStepConfig.plugInHooks.length; i++) {
177
+ if (i === 0) { // create main function for first flowStep
178
+ const [objSchema] = await getLocalOrS3ObjectSchema(
179
+ _izContext,
180
+ flowStepConfig.plugInHooks[i].objType,
181
+ path.join(appPath, './src/schemas')
182
+ );
183
+
184
+ let targetPlugIn = null;
185
+ if (objSchema.hasOwnProperty('plugIns')) {
186
+ for (const plugInConfig of Object.values(objSchema.plugIns)) {
187
+ targetPlugIn = plugInConfig.plugInHooks.find(h =>
188
+ h.plugInHookTagType.plugInHookTag === flowStepConfig.plugInHooks[i].plugInHookTag);
189
+ if (targetPlugIn) {
190
+ break;
191
+ }
192
+ };
193
+
194
+ if (targetPlugIn) {
195
+ let targetUrl = targetPlugIn.plugInHookTagType.targetUrl;
196
+ if (targetPlugIn.invocationType === 'sync') {
197
+ targetUrl += '/recievePlugInRequestSync'
198
+ }
199
+
200
+ mainResult = await mainFunctionFlowStep(
201
+ _izContext,
202
+ {
203
+ ...data,
204
+ invokeType: targetPlugIn.invocationType, // Ensure this matches "sync" or "async"
205
+ havePlugIn: true,
206
+ targetUrl: targetUrl
207
+ },
208
+ appPath
209
+ );
210
+ } else {
211
+ mainResult = await mainFunctionFlowStep(
212
+ _izContext,
213
+ {
214
+ ...data,
215
+ havePlugIn: false
216
+ },
217
+ appPath
218
+ );
219
+ }
220
+ };
221
+ }
222
+ }
223
+ } else {
224
+ mainResult = await mainFunctionFlowStep(
225
+ _izContext,
226
+ {
227
+ ...data,
228
+ havePlugIn: false
229
+ },
230
+ appPath
231
+ );
232
+ };
233
+
234
+ allCreateSourceParams.push(mainResult);
235
+
236
+ for (const objSchema of allObjSchemas) {
237
+ if (!objSchema.hasOwnProperty("recievePlugInHookTag"))
238
+ continue;
239
+ for (const recieveConfig of Object.values(objSchema.recievePlugInHookTag)) {
240
+ if (recieveConfig.hasOwnProperty('flowType')) {
241
+ data['recieveType'] = "async",
242
+ data['flowType'] = recieveConfig.flowType
243
+ } else {
244
+ data['recieveType'] = 'sync'
245
+ }
246
+
247
+ const yamlRecievePlugInResult = await yamlRecievePlugIn(_izContext, data, appPath);
248
+ const handlerRecievePlugInResult = await handlerRecievePlugIn(_izContext, data, appPath);
249
+ const mainFunctionRecievePlugInResult = await mainFunctionRecievePlugIn(_izContext, data, appPath);
250
+ // console.log("mainFunctionRecievePlugInResult", mainFunctionRecievePlugInResult)
251
+ allCreateSourceParams.push(
252
+ ...yamlRecievePlugInResult,
253
+ ...handlerRecievePlugInResult,
254
+ mainFunctionRecievePlugInResult
255
+ );
256
+ }
257
+ }
258
+ }
259
+ }
260
+ };
261
+
262
+
263
+
264
+ const uniqueSortedTargetFiles = Array.from(
265
+ new Set(
266
+ allCreateSourceParams
267
+ .filter(
268
+ item => item?.setting?.savePath && item?.setting?.saveFileName
269
+ )
270
+ .map(
271
+ item =>
272
+ `${item.setting.savePath.replace(/\/+$/, '')}/${item.setting.saveFileName}${item.setting.fileExtension || ''}`
273
+ )
274
+ )
275
+ ).sort();
276
+
277
+ console.info(
278
+ `[plugIn] completed total=${allCreateSourceParams.length} uniqueFiles=${uniqueSortedTargetFiles.length} elapsedMs=${Date.now() - startAllMs}`
279
+ );
280
+ if (uniqueSortedTargetFiles.length > 0 && _izContext.logger?.debug)
281
+ console.log('[plugIn] targetFiles=', uniqueSortedTargetFiles);
282
+
283
+ return allCreateSourceParams;
284
+ } catch (error) {
285
+ console.error('[plugIn] error:', error);
286
+ return [];
287
+ }
288
+ }
289
+
290
+ export default generatePlunIg;