@creator.co/wapi 1.2.2 → 1.2.4

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 (135) hide show
  1. package/.eslintrc.cjs +29 -22
  2. package/.github/workflows/npmpublish.yml +2 -5
  3. package/.github/workflows/prs.yml +1 -1
  4. package/README.md +1 -3
  5. package/dist/index.d.ts +11 -0
  6. package/dist/index.js +24 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/jest.config.d.ts +3 -0
  9. package/dist/jest.config.js +34 -0
  10. package/dist/jest.config.js.map +1 -0
  11. package/dist/package.json +64 -0
  12. package/dist/src/API/Request.d.ts +140 -0
  13. package/dist/src/API/Request.js +182 -0
  14. package/dist/src/API/Request.js.map +1 -0
  15. package/dist/src/API/Response.d.ts +256 -0
  16. package/dist/src/API/Response.js +398 -0
  17. package/dist/src/API/Response.js.map +1 -0
  18. package/dist/src/API/Utils.d.ts +63 -0
  19. package/dist/src/API/Utils.js +104 -0
  20. package/dist/src/API/Utils.js.map +1 -0
  21. package/dist/src/BaseEvent/EventProcessor.d.ts +81 -0
  22. package/dist/src/BaseEvent/EventProcessor.js +182 -0
  23. package/dist/src/BaseEvent/EventProcessor.js.map +1 -0
  24. package/dist/src/BaseEvent/Process.d.ts +74 -0
  25. package/dist/src/BaseEvent/Process.js +142 -0
  26. package/dist/src/BaseEvent/Process.js.map +1 -0
  27. package/dist/src/BaseEvent/Transaction.d.ts +29 -0
  28. package/dist/src/BaseEvent/Transaction.js +244 -0
  29. package/dist/src/BaseEvent/Transaction.js.map +1 -0
  30. package/dist/src/Config/Configuration.d.ts +131 -0
  31. package/dist/src/Config/Configuration.js +153 -0
  32. package/dist/src/Config/Configuration.js.map +1 -0
  33. package/dist/src/Config/EnvironmentVar.d.ts +101 -0
  34. package/dist/src/Config/EnvironmentVar.js +213 -0
  35. package/dist/src/Config/EnvironmentVar.js.map +1 -0
  36. package/dist/src/Crypto/Crypto.d.ts +57 -0
  37. package/dist/src/Crypto/Crypto.js +126 -0
  38. package/dist/src/Crypto/Crypto.js.map +1 -0
  39. package/dist/src/Crypto/JWT.d.ts +64 -0
  40. package/dist/src/Crypto/JWT.js +74 -0
  41. package/dist/src/Crypto/JWT.js.map +1 -0
  42. package/dist/src/Globals.d.ts +161 -0
  43. package/dist/src/Globals.js +173 -0
  44. package/dist/src/Globals.js.map +1 -0
  45. package/dist/src/Logger/Logger.d.ts +180 -0
  46. package/dist/src/Logger/Logger.js +412 -0
  47. package/dist/src/Logger/Logger.js.map +1 -0
  48. package/dist/src/Mailer/Mailer.d.ts +107 -0
  49. package/dist/src/Mailer/Mailer.js +313 -0
  50. package/dist/src/Mailer/Mailer.js.map +1 -0
  51. package/dist/src/Publisher/Publisher.d.ts +47 -0
  52. package/dist/src/Publisher/Publisher.js +141 -0
  53. package/dist/src/Publisher/Publisher.js.map +1 -0
  54. package/dist/src/Server/RouteResolver.d.ts +41 -0
  55. package/dist/src/Server/RouteResolver.js +135 -0
  56. package/dist/src/Server/RouteResolver.js.map +1 -0
  57. package/dist/src/Server/Router.d.ts +104 -0
  58. package/dist/src/Server/Router.js +45 -0
  59. package/dist/src/Server/Router.js.map +1 -0
  60. package/dist/src/Server/lib/ContainerServer.d.ts +58 -0
  61. package/dist/src/Server/lib/ContainerServer.js +143 -0
  62. package/dist/src/Server/lib/ContainerServer.js.map +1 -0
  63. package/dist/src/Server/lib/Server.d.ts +60 -0
  64. package/dist/src/Server/lib/Server.js +137 -0
  65. package/dist/src/Server/lib/Server.js.map +1 -0
  66. package/dist/src/Server/lib/container/GenericHandler.d.ts +4 -0
  67. package/dist/src/Server/lib/container/GenericHandler.js +138 -0
  68. package/dist/src/Server/lib/container/GenericHandler.js.map +1 -0
  69. package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +67 -0
  70. package/dist/src/Server/lib/container/GenericHandlerEvent.js +189 -0
  71. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -0
  72. package/dist/src/Server/lib/container/HealthHandler.d.ts +3 -0
  73. package/dist/src/Server/lib/container/HealthHandler.js +45 -0
  74. package/dist/src/Server/lib/container/HealthHandler.js.map +1 -0
  75. package/dist/src/Server/lib/container/Proxy.d.ts +95 -0
  76. package/dist/src/Server/lib/container/Proxy.js +201 -0
  77. package/dist/src/Server/lib/container/Proxy.js.map +1 -0
  78. package/dist/src/Server/lib/container/Utils.d.ts +18 -0
  79. package/dist/src/Server/lib/container/Utils.js +84 -0
  80. package/dist/src/Server/lib/container/Utils.js.map +1 -0
  81. package/dist/src/Validation/Validator.d.ts +21 -0
  82. package/dist/src/Validation/Validator.js +48 -0
  83. package/dist/src/Validation/Validator.js.map +1 -0
  84. package/index.ts +12 -10
  85. package/jest.config.ts +14 -21
  86. package/package.json +3 -3
  87. package/src/API/Request.ts +17 -35
  88. package/src/API/Response.ts +24 -42
  89. package/src/API/Utils.ts +5 -7
  90. package/src/BaseEvent/EventProcessor.ts +16 -24
  91. package/src/BaseEvent/Process.ts +7 -12
  92. package/src/BaseEvent/Transaction.ts +25 -43
  93. package/src/Config/Configuration.ts +8 -14
  94. package/src/Config/EnvironmentVar.ts +10 -20
  95. package/src/Crypto/Crypto.ts +10 -10
  96. package/src/Crypto/JWT.ts +4 -10
  97. package/src/Globals.ts +19 -25
  98. package/src/Logger/Logger.ts +36 -51
  99. package/src/Mailer/Mailer.ts +19 -31
  100. package/src/Publisher/Publisher.ts +7 -12
  101. package/src/Server/RouteResolver.ts +5 -6
  102. package/src/Server/Router.ts +7 -12
  103. package/src/Server/lib/ContainerServer.ts +5 -5
  104. package/src/Server/lib/Server.ts +26 -38
  105. package/src/Server/lib/container/GenericHandler.ts +8 -13
  106. package/src/Server/lib/container/GenericHandlerEvent.ts +21 -35
  107. package/src/Server/lib/container/HealthHandler.ts +2 -2
  108. package/src/Server/lib/container/Proxy.ts +26 -38
  109. package/src/Server/lib/container/Utils.ts +2 -2
  110. package/src/Validation/Validator.ts +6 -6
  111. package/tests/API/Request.test.ts +107 -111
  112. package/tests/API/Response.test.ts +86 -91
  113. package/tests/API/Utils.test.ts +64 -64
  114. package/tests/BaseEvent/EventProcessor.test.ts +68 -84
  115. package/tests/BaseEvent/Process.test.ts +11 -11
  116. package/tests/BaseEvent/Transaction.test.ts +44 -53
  117. package/tests/Config/Config.test.ts +50 -50
  118. package/tests/Config/EnvironmentVar.test.ts +50 -59
  119. package/tests/Crypto/Crypto.test.ts +20 -22
  120. package/tests/Crypto/JWT.test.ts +40 -40
  121. package/tests/Logger/Logger.test.ts +24 -36
  122. package/tests/Mailer/Mailer.test.ts +21 -29
  123. package/tests/Publisher/Publisher.test.ts +18 -18
  124. package/tests/Server/RouteResolver.test.ts +56 -59
  125. package/tests/Server/Router.test.ts +16 -16
  126. package/tests/Server/lib/ContainerServer.test.ts +83 -85
  127. package/tests/Server/lib/Server.test.ts +4 -4
  128. package/tests/Server/lib/container/GenericHandler.test.ts +31 -41
  129. package/tests/Server/lib/container/GenericHandlerEvent.test.ts +35 -36
  130. package/tests/Server/lib/container/HealthHandler.test.ts +7 -7
  131. package/tests/Server/lib/container/Proxy.test.ts +66 -79
  132. package/tests/Server/lib/container/Utils.test.ts +16 -17
  133. package/tests/Test.utils.ts +9 -9
  134. package/tests/Validation/Validator.test.ts +28 -40
  135. package/tests/main.test.ts +2 -2
@@ -1,7 +1,7 @@
1
- import type { Context, APIGatewayEvent } from "aws-lambda"
1
+ import type { Context, APIGatewayEvent } from 'aws-lambda'
2
2
 
3
- import Utils from "./Utils"
4
- import Logger from "../Logger/Logger"
3
+ import Utils from './Utils'
4
+ import Logger from '../Logger/Logger'
5
5
  /**
6
6
  * ${1:Description placeholder}
7
7
  *
@@ -36,8 +36,8 @@ export default class Request<InputType> {
36
36
  constructor(requestEvent: APIGatewayEvent, context: Context, logger: Logger) {
37
37
  this.requestEvent = requestEvent
38
38
  this.context = context
39
- logger.debug("Request info:", JSON.stringify(requestEvent))
40
- logger.debug("Request context:", JSON.stringify(context))
39
+ logger.debug('Request info:', JSON.stringify(requestEvent))
40
+ logger.debug('Request context:', JSON.stringify(context))
41
41
  }
42
42
  /**
43
43
  * ${1:Description placeholder}
@@ -58,10 +58,7 @@ export default class Request<InputType> {
58
58
  * @returns {(string | null)}
59
59
  */
60
60
  public getQueryParam(paramName: string): string | null {
61
- return Utils.caseInsensitiveObjectForKey(
62
- this.requestEvent.queryStringParameters,
63
- paramName,
64
- )
61
+ return Utils.caseInsensitiveObjectForKey(this.requestEvent.queryStringParameters, paramName)
65
62
  }
66
63
  /**
67
64
  * ${1:Description placeholder}
@@ -71,10 +68,7 @@ export default class Request<InputType> {
71
68
  * @returns {(string | null)}
72
69
  */
73
70
  public getHeader(headerName: string): string | null {
74
- return Utils.caseInsensitiveObjectForKey(
75
- this.requestEvent.headers,
76
- headerName,
77
- )
71
+ return Utils.caseInsensitiveObjectForKey(this.requestEvent.headers, headerName)
78
72
  }
79
73
  //context params
80
74
  /**
@@ -85,10 +79,7 @@ export default class Request<InputType> {
85
79
  * @returns {(any | null)}
86
80
  */
87
81
  public getContextParam(cxtParam: string): any | null {
88
- return Utils.caseInsensitiveObjectForKey(
89
- this.requestEvent.requestContext,
90
- cxtParam,
91
- )
82
+ return Utils.caseInsensitiveObjectForKey(this.requestEvent.requestContext, cxtParam)
92
83
  }
93
84
  /**
94
85
  * ${1:Description placeholder}
@@ -109,10 +100,7 @@ export default class Request<InputType> {
109
100
  * @returns {(string | null)}
110
101
  */
111
102
  public getPathParam(paramName: string): string | null {
112
- return Utils.caseInsensitiveObjectForKey(
113
- this.requestEvent.pathParameters,
114
- paramName,
115
- )
103
+ return Utils.caseInsensitiveObjectForKey(this.requestEvent.pathParameters, paramName)
116
104
  }
117
105
  /**
118
106
  * ${1:Description placeholder}
@@ -124,10 +112,9 @@ export default class Request<InputType> {
124
112
  let b: any = null
125
113
  try {
126
114
  b = this.requestEvent.body
127
- if (typeof b === "string" || b instanceof String)
128
- b = JSON.parse(b as string)
115
+ if (typeof b === 'string' || b instanceof String) b = JSON.parse(b as string)
129
116
  } catch (e) {
130
- console.error("Error while getting request body!", e)
117
+ console.error('Error while getting request body!', e)
131
118
  }
132
119
  return b
133
120
  }
@@ -165,7 +152,7 @@ export default class Request<InputType> {
165
152
  * @returns {(string | null)}
166
153
  */
167
154
  public getAuthorizationHeader(): string | null {
168
- return this.getHeader("Authorization")
155
+ return this.getHeader('Authorization')
169
156
  }
170
157
  /**
171
158
  * ${1:Description placeholder}
@@ -175,9 +162,7 @@ export default class Request<InputType> {
175
162
  */
176
163
  public getRequestID(): string {
177
164
  if (this.context.awsRequestId) return this.context.awsRequestId
178
- return this.requestEvent.requestContext
179
- ? this.requestEvent.requestContext.requestId
180
- : "unknown"
165
+ return this.requestEvent.requestContext ? this.requestEvent.requestContext.requestId : 'unknown'
181
166
  }
182
167
  /**
183
168
  * ${1:Description placeholder}
@@ -186,9 +171,9 @@ export default class Request<InputType> {
186
171
  * @returns {string}
187
172
  */
188
173
  public getOriginIP(): string {
189
- const origin = this.getContextParam("identity")?.sourceIp
190
- const hOrigin = this.getHeader("X-Forwarded-For")
191
- return origin ? origin : hOrigin ? hOrigin : "unknown"
174
+ const origin = this.getContextParam('identity')?.sourceIp
175
+ const hOrigin = this.getHeader('X-Forwarded-For')
176
+ return origin ? origin : hOrigin ? hOrigin : 'unknown'
192
177
  }
193
178
  /**
194
179
  * ${1:Description placeholder}
@@ -199,9 +184,6 @@ export default class Request<InputType> {
199
184
  */
200
185
  public setFixedPathParams(keys: any[], result: any[]): void {
201
186
  this.requestEvent.pathParameters = {}
202
- keys.forEach(
203
- (key, index) =>
204
- (this.requestEvent.pathParameters[key.name] = result[index + 1]),
205
- )
187
+ keys.forEach((key, index) => (this.requestEvent.pathParameters[key.name] = result[index + 1]))
206
188
  }
207
189
  }
@@ -1,7 +1,7 @@
1
- import type { Context } from "aws-lambda"
1
+ import type { Context } from 'aws-lambda'
2
2
 
3
- import Transaction from "../BaseEvent/Transaction"
4
- import Globals from "../Globals"
3
+ import Transaction from '../BaseEvent/Transaction'
4
+ import Globals from '../Globals'
5
5
  //
6
6
  /**
7
7
  * ${1:Description placeholder}
@@ -18,8 +18,8 @@ class CustomError extends Error {
18
18
  * @param {*} body
19
19
  */
20
20
  constructor(body: any) {
21
- super(body.message || "Unknown error!")
22
- this.name = body.error || "UnknownError"
21
+ super(body.message || 'Unknown error!')
22
+ this.name = body.error || 'UnknownError'
23
23
  }
24
24
  }
25
25
  //
@@ -128,15 +128,15 @@ export default class Response<BodyType = null> {
128
128
  throwOnErrors?: boolean
129
129
  disableTransactionID?: boolean
130
130
  }
131
- | undefined,
131
+ | undefined
132
132
  ) {
133
133
  // response
134
134
  this._statusCode = statusCode
135
135
  this._body = body
136
136
  this._headers = {
137
- "Access-Control-Allow-Origin": "*",
138
- "Access-Control-Allow-Credentials": true,
139
- "Content-Type": "application/json",
137
+ 'Access-Control-Allow-Origin': '*',
138
+ 'Access-Control-Allow-Credentials': true,
139
+ 'Content-Type': 'application/json',
140
140
  }
141
141
  // behaviour
142
142
  this._isPipingOut = false //internal -- flag to indicate streaming out has started and avoid double stream call
@@ -197,19 +197,15 @@ export default class Response<BodyType = null> {
197
197
  public async build(
198
198
  context: Context,
199
199
  transaction: Transaction<any, any, any>,
200
- _optDoNotCallContext: boolean,
200
+ _optDoNotCallContext: boolean
201
201
  ): Promise<void> {
202
202
  //Stream support
203
203
  if (this._isPipingOut) return
204
204
  if (this.shouldStream) return this._pipe(context)
205
205
 
206
206
  //append default fields
207
- if (
208
- transaction.request.getRequestID() &&
209
- this._body &&
210
- !this.disableTransactionID
211
- )
212
- this.appendIntoBody("transactionID", transaction.request.getRequestID()) //append transaction ID
207
+ if (transaction.request.getRequestID() && this._body && !this.disableTransactionID)
208
+ this.appendIntoBody('transactionID', transaction.request.getRequestID()) //append transaction ID
213
209
  //Raw response support
214
210
  if (this.rawBody) return this._rawContext(context, transaction)
215
211
 
@@ -251,14 +247,10 @@ export default class Response<BodyType = null> {
251
247
  * @param {Context} context
252
248
  * @param {Transaction<null, BodyType>} transaction
253
249
  */
254
- private _rawContext(
255
- context: Context,
256
- transaction: Transaction<null, BodyType>,
257
- ): void {
250
+ private _rawContext(context: Context, transaction: Transaction<null, BodyType>): void {
258
251
  //log response and respond to context
259
252
  transaction.logger.debug(this._body)
260
- if (this.getCode() <= 200 && this.getCode() <= 299)
261
- context.succeed(this._body)
253
+ if (this.getCode() <= 200 && this.getCode() <= 299) context.succeed(this._body)
262
254
  else {
263
255
  if (!this.throwOnErrors) context.fail(new CustomError(this._body))
264
256
  else throw new CustomError(this._body)
@@ -274,9 +266,7 @@ export default class Response<BodyType = null> {
274
266
  * @param {string} paramName
275
267
  * @returns {Response<ResponseErrorType>}
276
268
  */
277
- public static MissingParamResponse(
278
- paramName: string,
279
- ): Response<ResponseErrorType> {
269
+ public static MissingParamResponse(paramName: string): Response<ResponseErrorType> {
280
270
  console.warn(`Invalid request - Path parameter ${paramName} is missing.`)
281
271
  return new Response<ResponseErrorType>(400, {
282
272
  err: `Invalid request. Path parameter ${paramName} is missing.`,
@@ -291,9 +281,7 @@ export default class Response<BodyType = null> {
291
281
  * @param {string} paramName
292
282
  * @returns {Response<ResponseErrorType>}
293
283
  */
294
- public static MissingQueryResponse(
295
- paramName: string,
296
- ): Response<ResponseErrorType> {
284
+ public static MissingQueryResponse(paramName: string): Response<ResponseErrorType> {
297
285
  console.warn(`Invalid request - Query parameter ${paramName} is missing.`)
298
286
  return new Response<ResponseErrorType>(400, {
299
287
  err: `Invalid request. Query parameter ${paramName} is missing.`,
@@ -313,7 +301,7 @@ export default class Response<BodyType = null> {
313
301
  public static BadRequestResponse(
314
302
  msg?: string,
315
303
  errCode?: string,
316
- optBody?: any,
304
+ optBody?: any
317
305
  ): Response<ResponseErrorType> {
318
306
  console.warn(`Bad request - ${msg}`)
319
307
  return new Response<ResponseErrorType>(400, {
@@ -335,7 +323,7 @@ export default class Response<BodyType = null> {
335
323
  public static BadRequestResponseWithRollback(
336
324
  msg: string,
337
325
  errCode?: string,
338
- optBody?: any,
326
+ optBody?: any
339
327
  ): Response<ResponseErrorType> {
340
328
  console.warn(`Bad request - ${msg}`)
341
329
  return new Response<ResponseErrorType>(400, {
@@ -354,10 +342,7 @@ export default class Response<BodyType = null> {
354
342
  * @param {?string} [errCode]
355
343
  * @returns {Response<ResponseErrorType>}
356
344
  */
357
- public static UnauthorizedResponse(
358
- msg: string,
359
- errCode?: string,
360
- ): Response<ResponseErrorType> {
345
+ public static UnauthorizedResponse(msg: string, errCode?: string): Response<ResponseErrorType> {
361
346
  console.warn(`Denying request - ${msg}`)
362
347
  return new Response<ResponseErrorType>(401, {
363
348
  err: msg,
@@ -386,7 +371,7 @@ export default class Response<BodyType = null> {
386
371
  */
387
372
  public static RedirectResponse(url: string): Response<null> {
388
373
  const resp = new Response<null>(302, null)
389
- resp.appendHeader("Location", url)
374
+ resp.appendHeader('Location', url)
390
375
  return resp
391
376
  }
392
377
  /**
@@ -408,15 +393,12 @@ export default class Response<BodyType = null> {
408
393
  * @param {string} contentType
409
394
  * @returns {Response}
410
395
  */
411
- public static SuccessStreamResponse(
412
- stream: any,
413
- contentType: string,
414
- ): Response {
396
+ public static SuccessStreamResponse(stream: any, contentType: string): Response {
415
397
  const resp = new Response(200, stream, {
416
398
  shouldStream: true,
417
399
  })
418
- resp.appendHeader("Connection", "keep-alive")
419
- if (contentType) resp.appendHeader("Content-Type", contentType)
400
+ resp.appendHeader('Connection', 'keep-alive')
401
+ if (contentType) resp.appendHeader('Content-Type', contentType)
420
402
  return resp
421
403
  }
422
404
  /**
@@ -431,7 +413,7 @@ export default class Response<BodyType = null> {
431
413
  */
432
414
  public static SimpleResponse<BodyType>(
433
415
  body: BodyType,
434
- optionalCode?: number,
416
+ optionalCode?: number
435
417
  ): Response<BodyType> {
436
418
  const resp = new Response<BodyType>(optionalCode || 200, body)
437
419
  return resp
package/src/API/Utils.ts CHANGED
@@ -14,7 +14,7 @@ export default class Utils {
14
14
  * @returns {boolean}
15
15
  */
16
16
  public static isHybridlessContainer(): boolean {
17
- return process.env.HYBRIDLESS_RUNTIME == "true"
17
+ return process.env.HYBRIDLESS_RUNTIME == 'true'
18
18
  }
19
19
  /**
20
20
  * ${1:Description placeholder}
@@ -68,9 +68,9 @@ export default class Utils {
68
68
  public static isValidNumber(number: string): boolean {
69
69
  let validNumb = NaN
70
70
  try {
71
- validNumb = parseInt(number + "")
71
+ validNumb = parseInt(number + '')
72
72
  } catch (e) {
73
- console.error("Error while validating number", e)
73
+ console.error('Error while validating number', e)
74
74
  }
75
75
  return !isNaN(validNumb) && !Array.isArray(number)
76
76
  }
@@ -85,10 +85,8 @@ export default class Utils {
85
85
  */
86
86
  public static caseInsensitiveObjectForKey(obj: any, key: string): any | null {
87
87
  if (!obj) return null
88
- const insensitiveKey = Object.keys(obj).find(
89
- (k) => k.toLowerCase() === key.toLowerCase(),
90
- )
91
- if (insensitiveKey && insensitiveKey != "") return obj[insensitiveKey]
88
+ const insensitiveKey = Object.keys(obj).find(k => k.toLowerCase() === key.toLowerCase())
89
+ if (insensitiveKey && insensitiveKey != '') return obj[insensitiveKey]
92
90
  return null
93
91
  }
94
92
  }
@@ -1,8 +1,8 @@
1
- import type { Context, SQSBatchResponse, SQSEvent } from "aws-lambda"
1
+ import type { Context, SQSBatchResponse, SQSEvent } from 'aws-lambda'
2
2
 
3
- import Transaction, { TransactionConfig } from "./Transaction"
4
- import Response, { ResponseErrorType } from "../API/Response"
5
- import Globals from "../Globals"
3
+ import Transaction, { TransactionConfig } from './Transaction'
4
+ import Response, { ResponseErrorType } from '../API/Response'
5
+ import Globals from '../Globals'
6
6
 
7
7
  // Handler
8
8
  /**
@@ -13,12 +13,8 @@ import Globals from "../Globals"
13
13
  * @template ResponseInnerType
14
14
  */
15
15
  export type EventProcessorExecution<ResponseInnerType> = (
16
- transaction: Transaction<
17
- null,
18
- ResponseInnerType | ResponseErrorType,
19
- SQSBatchResponse
20
- >,
21
- recordContent: string | object,
16
+ transaction: Transaction<null, ResponseInnerType | ResponseErrorType, SQSBatchResponse>,
17
+ recordContent: string | object
22
18
  ) => Promise<Response<ResponseInnerType | ResponseErrorType> | SQSBatchResponse>
23
19
 
24
20
  /**
@@ -75,7 +71,7 @@ export default class EventProcessor<ResponseInnerType> {
75
71
  event: SQSEvent,
76
72
  context: Context,
77
73
  config?: TransactionConfig,
78
- allowFailure?: boolean,
74
+ allowFailure?: boolean
79
75
  ) {
80
76
  this._event = event
81
77
  this._context = context
@@ -92,7 +88,7 @@ export default class EventProcessor<ResponseInnerType> {
92
88
  */
93
89
  async processEvent(
94
90
  execution: EventProcessorExecution<ResponseInnerType>,
95
- doNotDecodeMessage?: boolean,
91
+ doNotDecodeMessage?: boolean
96
92
  ): Promise<Response<ResponseErrorType> | null | SQSBatchResponse> {
97
93
  const resp = await this._processRawEvent(execution, !!doNotDecodeMessage)
98
94
  if (
@@ -115,7 +111,7 @@ export default class EventProcessor<ResponseInnerType> {
115
111
  */
116
112
  async _processRawEvent(
117
113
  execution: EventProcessorExecution<ResponseInnerType>,
118
- doNotDecodeMessage: boolean,
114
+ doNotDecodeMessage: boolean
119
115
  ): Promise<Response<ResponseErrorType> | null | SQSBatchResponse> {
120
116
  if (this._event.Records && this._event.Records.length > 0) {
121
117
  //safe check for empty events?
@@ -126,14 +122,11 @@ export default class EventProcessor<ResponseInnerType> {
126
122
  {
127
123
  ...this._apiConfig,
128
124
  syncReturn: true,
129
- },
130
- ).execute(async (transaction) => {
125
+ }
126
+ ).execute(async transaction => {
131
127
  //Map records with decoded message when required
132
- const decodedRecords: string[] | object[] = this._event.Records.map(
133
- (eventRecord) =>
134
- doNotDecodeMessage
135
- ? eventRecord.body
136
- : JSON.parse(eventRecord.body),
128
+ const decodedRecords: string[] | object[] = this._event.Records.map(eventRecord =>
129
+ doNotDecodeMessage ? eventRecord.body : JSON.parse(eventRecord.body)
137
130
  )
138
131
 
139
132
  //for each available event
@@ -146,8 +139,7 @@ export default class EventProcessor<ResponseInnerType> {
146
139
  //check for failure
147
140
  if (
148
141
  !resp ||
149
- (resp instanceof Response &&
150
- !(resp?.getCode() >= 200 && resp?.getCode() < 300))
142
+ (resp instanceof Response && !(resp?.getCode() >= 200 && resp?.getCode() < 300))
151
143
  ) {
152
144
  //response with failures or fail hard at first
153
145
  if (this._allowFailure) failureIDs.push(message.messageId)
@@ -157,14 +149,14 @@ export default class EventProcessor<ResponseInnerType> {
157
149
  //not errored and loop ended - succeeded (might have failures)
158
150
  if (this._allowFailure)
159
151
  return {
160
- batchItemFailures: failureIDs.map((id) => ({ itemIdentifier: id })),
152
+ batchItemFailures: failureIDs.map(id => ({ itemIdentifier: id })),
161
153
  }
162
154
  return Response.SuccessResponse(null)
163
155
  })
164
156
  } else
165
157
  return Response.BadRequestResponse(
166
158
  Globals.ErrorResponseNoRecords,
167
- Globals.ErrorCode_NoRecords,
159
+ Globals.ErrorCode_NoRecords
168
160
  ) //no event to be processed?
169
161
  }
170
162
  }
@@ -1,6 +1,6 @@
1
- import { TransactionConfig } from "./Transaction"
2
- import Logger from "../Logger/Logger"
3
- import Publisher from "../Publisher/Publisher"
1
+ import { TransactionConfig } from './Transaction'
2
+ import Logger from '../Logger/Logger'
3
+ import Publisher from '../Publisher/Publisher'
4
4
 
5
5
  // Config
6
6
  /**
@@ -9,10 +9,7 @@ import Publisher from "../Publisher/Publisher"
9
9
  * @export
10
10
  * @typedef {ProcessConfig}
11
11
  */
12
- export type ProcessConfig = Omit<
13
- TransactionConfig,
14
- "throwOnErrors" | "syncReturn"
15
- >
12
+ export type ProcessConfig = Omit<TransactionConfig, 'throwOnErrors' | 'syncReturn'>
16
13
 
17
14
  /**
18
15
  * ${1:Description placeholder}
@@ -62,7 +59,7 @@ export default class Process {
62
59
  */
63
60
  constructor(config: ProcessConfig, interval: number) {
64
61
  this._interval = interval
65
- this.logger = new Logger(config.logger, "long-running-process")
62
+ this.logger = new Logger(config.logger, 'long-running-process')
66
63
  this.publisher = new Publisher(config.publisher)
67
64
  }
68
65
 
@@ -75,7 +72,7 @@ export default class Process {
75
72
  * @returns {*}
76
73
  */
77
74
  async execute(executionFunc) {
78
- this.logger.debug("Starting main process code")
75
+ this.logger.debug('Starting main process code')
79
76
  //Connect DB
80
77
  // if (this.db) await this.db.connect();
81
78
  //Program loop
@@ -105,9 +102,7 @@ export default class Process {
105
102
  executionFailed = false
106
103
  } catch (e) {
107
104
  /*EXECUTION FAIL*/
108
- this.logger.error(
109
- "Exception when executing main process code. Rolling back DB!",
110
- )
105
+ this.logger.error('Exception when executing main process code. Rolling back DB!')
111
106
  this.logger.exception(e)
112
107
  //Rollback DB
113
108
  // if (this.db) await this.db.rollback();
@@ -1,21 +1,15 @@
1
- import type { APIGatewayEvent, Context, SQSEvent } from "aws-lambda"
1
+ import type { APIGatewayEvent, Context, SQSEvent } from 'aws-lambda'
2
2
 
3
- import Request from "../API/Request"
4
- import Response, { ResponseErrorType } from "../API/Response"
5
- import Globals from "../Globals"
6
- import Logger, { LoggerConfig } from "../Logger/Logger"
7
- import Publisher, { PublisherConfig } from "../Publisher/Publisher"
3
+ import Request from '../API/Request'
4
+ import Response, { ResponseErrorType } from '../API/Response'
5
+ import Globals from '../Globals'
6
+ import Logger, { LoggerConfig } from '../Logger/Logger'
7
+ import Publisher, { PublisherConfig } from '../Publisher/Publisher'
8
8
 
9
9
  // Request
10
- export type TransactionExecution<
11
- TransactionType,
12
- ResponseInnerType,
13
- MiscRespType = null,
14
- > = (
15
- transaction: TransactionType,
16
- ) => Promise<
17
- Response<ResponseInnerType> | Response<ResponseErrorType> | MiscRespType
18
- >
10
+ export type TransactionExecution<TransactionType, ResponseInnerType, MiscRespType = null> = (
11
+ transaction: TransactionType
12
+ ) => Promise<Response<ResponseInnerType> | Response<ResponseErrorType> | MiscRespType>
19
13
  // Config
20
14
  export type TransactionConfig = {
21
15
  throwOnErrors?: boolean
@@ -32,10 +26,7 @@ export default class Transaction<
32
26
  > {
33
27
  private event: any
34
28
  private context: Context
35
- private response:
36
- | Response<ResponseInnerType | ResponseErrorType>
37
- | MiscRespType
38
- | null
29
+ private response: Response<ResponseInnerType | ResponseErrorType> | MiscRespType | null
39
30
  private syncReturn: boolean
40
31
  private retrowErrors: boolean
41
32
  //
@@ -44,16 +35,12 @@ export default class Transaction<
44
35
  public readonly publisher: Publisher
45
36
  public responseProxy: (response: Response<ResponseInnerType>) => Promise<void>
46
37
  //
47
- constructor(
48
- event: APIGatewayEvent | SQSEvent,
49
- context: Context,
50
- config?: TransactionConfig,
51
- ) {
38
+ constructor(event: APIGatewayEvent | SQSEvent, context: Context, config?: TransactionConfig) {
52
39
  const transactionId = context.awsRequestId
53
40
  ? context.awsRequestId
54
41
  : (<APIGatewayEvent>event).requestContext
55
42
  ? (<APIGatewayEvent>event).requestContext.requestId
56
- : "unknown"
43
+ : 'unknown'
57
44
  // transaction ctx
58
45
  this.event = event
59
46
  this.context = context
@@ -75,7 +62,7 @@ export default class Transaction<
75
62
  Transaction<InputType, ResponseInnerType, MiscRespType>,
76
63
  ResponseInnerType,
77
64
  MiscRespType
78
- >,
65
+ >
79
66
  ): Promise<Response<ResponseInnerType | ResponseErrorType> | MiscRespType> {
80
67
  await this.executeLoggerFlush(async () => {
81
68
  await this.executeDBTransactions(async () => {
@@ -93,42 +80,40 @@ export default class Transaction<
93
80
  Transaction<InputType, ResponseInnerType, MiscRespType>,
94
81
  ResponseInnerType,
95
82
  MiscRespType
96
- >,
83
+ >
97
84
  ): Promise<boolean> {
98
85
  let executionFailed = true //failed til we say no!
99
86
  //safe execution handler
100
87
  try {
101
88
  //Execute
102
- this.logger.debug("Starting main request code")
89
+ this.logger.debug('Starting main request code')
103
90
  this.response = await executionFunc(this)
104
91
  //Answer client
105
92
  if (this.response && this.response instanceof Response) {
106
93
  await this.response.build(this.context, this, this.syncReturn)
107
- executionFailed = !!(
108
- this.response.getBody() && this.response.getBody()["rollback"]
109
- )
94
+ executionFailed = !!(this.response.getBody() && this.response.getBody()['rollback'])
110
95
  } else if (this.syncReturn && this.response) {
111
- this.logger.log("Sync return with different response object")
96
+ this.logger.log('Sync return with different response object')
112
97
  this.logger.debug(this.response)
113
98
  executionFailed = false
114
99
  } else {
115
100
  this.response = this.getErrorResponse(
116
101
  Globals.ErrorResponseInvalidServerResponse,
117
- Globals.ErrorCode_APIError,
102
+ Globals.ErrorCode_APIError
118
103
  )
119
104
  await this.response.build(this.context, this, this.syncReturn)
120
- this.logger.error("Invalid response object from main request code.")
105
+ this.logger.error('Invalid response object from main request code.')
121
106
  }
122
107
  } catch (e) {
123
108
  /*EXECUTION FAIL*/
124
- this.logger.error("Exception when executing main request code.")
109
+ this.logger.error('Exception when executing main request code.')
125
110
  this.logger.exception(e)
126
111
  //retrow?
127
112
  if (this.retrowErrors) throw e
128
113
  //envelope exception?
129
114
  this.response = this.getErrorResponse(
130
115
  Globals.ErrorResponseUnhandledError,
131
- Globals.ErrorCode_APIError,
116
+ Globals.ErrorCode_APIError
132
117
  )
133
118
  await this.response.build(this.context, this, this.syncReturn)
134
119
  }
@@ -153,7 +138,7 @@ export default class Transaction<
153
138
  // //Cleanup DB after execution
154
139
  // if (this.db) await this.db.cleanup();
155
140
  } catch (e) {
156
- this.logger.error("Exception when executing DB transactions.")
141
+ this.logger.error('Exception when executing DB transactions.')
157
142
  this.logger.log(e.stack)
158
143
  //retrow?
159
144
  if (this.retrowErrors) throw e
@@ -163,19 +148,16 @@ export default class Transaction<
163
148
  try {
164
149
  await safeExecution()
165
150
  } catch (e) {
166
- this.logger.error("Exception when flushing logs.")
151
+ this.logger.error('Exception when flushing logs.')
167
152
  this.logger.exception(e)
168
153
  //retrow?
169
154
  if (this.retrowErrors) throw e
170
155
  } finally {
171
- this.logger.debug("Transaction ended")
156
+ this.logger.debug('Transaction ended')
172
157
  }
173
158
  }
174
159
  /* Response support */
175
- private getErrorResponse(
176
- error: string,
177
- code: string,
178
- ): Response<ResponseErrorType> {
160
+ private getErrorResponse(error: string, code: string): Response<ResponseErrorType> {
179
161
  return Response.BadRequestResponseWithRollback(error, code)
180
162
  }
181
163
  }