@creator.co/wapi 1.2.1-beta5 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/.github/workflows/npmpublish.yml +11 -19
  2. package/.github/workflows/prs.yml +13 -0
  3. package/jest.config.ts +39 -0
  4. package/package.json +15 -4
  5. package/src/API/Request.ts +120 -10
  6. package/src/API/Response.ts +236 -8
  7. package/src/API/Utils.ts +56 -1
  8. package/src/BaseEvent/EventProcessor.ts +84 -11
  9. package/src/BaseEvent/Process.ts +62 -1
  10. package/src/BaseEvent/Transaction.ts +5 -8
  11. package/src/Config/Configuration.ts +111 -5
  12. package/src/Config/EnvironmentVar.ts +90 -1
  13. package/src/Crypto/Crypto.ts +77 -18
  14. package/src/Crypto/JWT.ts +49 -3
  15. package/src/Globals.ts +141 -3
  16. package/src/Logger/Logger.ts +173 -19
  17. package/src/Mailer/Mailer.ts +95 -0
  18. package/src/Publisher/Publisher.ts +50 -4
  19. package/src/Server/RouteResolver.ts +142 -0
  20. package/src/Server/Router.ts +77 -0
  21. package/src/Server/lib/ContainerServer.ts +48 -1
  22. package/src/Server/lib/Server.ts +78 -38
  23. package/src/Server/lib/container/GenericHandler.ts +7 -5
  24. package/src/Server/lib/container/GenericHandlerEvent.ts +59 -17
  25. package/src/Server/lib/container/HealthHandler.ts +1 -1
  26. package/src/Server/lib/container/Proxy.ts +92 -11
  27. package/src/Server/lib/container/Utils.ts +16 -25
  28. package/src/Validation/Validator.ts +18 -2
  29. package/tests/API/Request.test.ts +263 -0
  30. package/tests/API/Response.test.ts +372 -0
  31. package/tests/API/Utils.test.ts +157 -0
  32. package/tests/BaseEvent/EventProcessor.test.ts +278 -0
  33. package/tests/BaseEvent/Process.test.ts +49 -0
  34. package/tests/BaseEvent/Transaction.test.ts +231 -0
  35. package/tests/Config/Config.test.ts +193 -0
  36. package/tests/Config/EnvironmentVar.test.ts +223 -0
  37. package/tests/Crypto/Crypto.test.ts +90 -0
  38. package/tests/Crypto/JWT.test.ts +92 -0
  39. package/tests/Logger/Logger.test.ts +108 -0
  40. package/tests/Mailer/Mailer.test.ts +67 -0
  41. package/tests/Publisher/Publisher.test.ts +60 -0
  42. package/tests/Server/RouteResolver.test.ts +106 -0
  43. package/tests/Server/Router.test.ts +38 -0
  44. package/tests/Server/lib/ContainerServer.test.ts +329 -0
  45. package/tests/Server/lib/Server.test.ts +12 -0
  46. package/tests/Server/lib/container/GenericHandler.test.ts +141 -0
  47. package/tests/Server/lib/container/GenericHandlerEvent.test.ts +103 -0
  48. package/tests/Server/lib/container/HealthHandler.test.ts +30 -0
  49. package/tests/Server/lib/container/Proxy.test.ts +278 -0
  50. package/tests/Server/lib/container/Utils.test.ts +48 -0
  51. package/tests/Test.utils.ts +95 -0
  52. package/tests/Validation/Validator.test.ts +88 -0
  53. package/tests/main.test.ts +15 -0
  54. package/tsconfig.json +1 -0
  55. package/dist/index.d.ts +0 -11
  56. package/dist/index.js +0 -24
  57. package/dist/index.js.map +0 -1
  58. package/dist/package.json +0 -53
  59. package/dist/src/API/Request.d.ts +0 -21
  60. package/dist/src/API/Request.js +0 -86
  61. package/dist/src/API/Request.js.map +0 -1
  62. package/dist/src/API/Response.d.ts +0 -39
  63. package/dist/src/API/Response.js +0 -232
  64. package/dist/src/API/Response.js.map +0 -1
  65. package/dist/src/API/Utils.d.ts +0 -8
  66. package/dist/src/API/Utils.js +0 -49
  67. package/dist/src/API/Utils.js.map +0 -1
  68. package/dist/src/BaseEvent/EventProcessor.d.ts +0 -13
  69. package/dist/src/BaseEvent/EventProcessor.js +0 -151
  70. package/dist/src/BaseEvent/EventProcessor.js.map +0 -1
  71. package/dist/src/BaseEvent/Process.d.ts +0 -12
  72. package/dist/src/BaseEvent/Process.js +0 -114
  73. package/dist/src/BaseEvent/Process.js.map +0 -1
  74. package/dist/src/BaseEvent/Transaction.d.ts +0 -29
  75. package/dist/src/BaseEvent/Transaction.js +0 -248
  76. package/dist/src/BaseEvent/Transaction.js.map +0 -1
  77. package/dist/src/Config/Configuration.d.ts +0 -34
  78. package/dist/src/Config/Configuration.js +0 -93
  79. package/dist/src/Config/Configuration.js.map +0 -1
  80. package/dist/src/Config/EnvironmentVar.d.ts +0 -17
  81. package/dist/src/Config/EnvironmentVar.js +0 -152
  82. package/dist/src/Config/EnvironmentVar.js.map +0 -1
  83. package/dist/src/Crypto/Crypto.d.ts +0 -8
  84. package/dist/src/Crypto/Crypto.js +0 -84
  85. package/dist/src/Crypto/Crypto.js.map +0 -1
  86. package/dist/src/Crypto/JWT.d.ts +0 -16
  87. package/dist/src/Crypto/JWT.js +0 -49
  88. package/dist/src/Crypto/JWT.js.map +0 -1
  89. package/dist/src/Globals.d.ts +0 -21
  90. package/dist/src/Globals.js +0 -35
  91. package/dist/src/Globals.js.map +0 -1
  92. package/dist/src/Logger/Logger.d.ts +0 -34
  93. package/dist/src/Logger/Logger.js +0 -345
  94. package/dist/src/Logger/Logger.js.map +0 -1
  95. package/dist/src/Mailer/Mailer.d.ts +0 -12
  96. package/dist/src/Mailer/Mailer.js +0 -234
  97. package/dist/src/Mailer/Mailer.js.map +0 -1
  98. package/dist/src/Publisher/Publisher.d.ts +0 -10
  99. package/dist/src/Publisher/Publisher.js +0 -109
  100. package/dist/src/Publisher/Publisher.js.map +0 -1
  101. package/dist/src/Server/Router.d.ts +0 -27
  102. package/dist/src/Server/Router.js +0 -22
  103. package/dist/src/Server/Router.js.map +0 -1
  104. package/dist/src/Server/lib/ContainerServer.d.ts +0 -11
  105. package/dist/src/Server/lib/ContainerServer.js +0 -103
  106. package/dist/src/Server/lib/ContainerServer.js.map +0 -1
  107. package/dist/src/Server/lib/Server.d.ts +0 -9
  108. package/dist/src/Server/lib/Server.js +0 -141
  109. package/dist/src/Server/lib/Server.js.map +0 -1
  110. package/dist/src/Server/lib/container/GenericHandler.d.ts +0 -4
  111. package/dist/src/Server/lib/container/GenericHandler.js +0 -136
  112. package/dist/src/Server/lib/container/GenericHandler.js.map +0 -1
  113. package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +0 -14
  114. package/dist/src/Server/lib/container/GenericHandlerEvent.js +0 -164
  115. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +0 -1
  116. package/dist/src/Server/lib/container/HealthHandler.d.ts +0 -3
  117. package/dist/src/Server/lib/container/HealthHandler.js +0 -44
  118. package/dist/src/Server/lib/container/HealthHandler.js.map +0 -1
  119. package/dist/src/Server/lib/container/Proxy.d.ts +0 -15
  120. package/dist/src/Server/lib/container/Proxy.js +0 -157
  121. package/dist/src/Server/lib/container/Proxy.js.map +0 -1
  122. package/dist/src/Server/lib/container/Utils.d.ts +0 -6
  123. package/dist/src/Server/lib/container/Utils.js +0 -109
  124. package/dist/src/Server/lib/container/Utils.js.map +0 -1
  125. package/dist/src/Validation/Validator.d.ts +0 -5
  126. package/dist/src/Validation/Validator.js +0 -31
  127. package/dist/src/Validation/Validator.js.map +0 -1
@@ -0,0 +1,278 @@
1
+ import { expect as j_expect } from "@jest/globals"
2
+ import { expect } from "chai"
3
+
4
+ import Response from "../../src/API/Response"
5
+ import EventProcessor from "../../src/BaseEvent/EventProcessor"
6
+ import Globals from "../../src/Globals"
7
+ import { emptyQueueEvent, observableContext } from "../Test.utils"
8
+
9
+ describe("EventProcessor success invocation path", () => {
10
+ test("Simple success", async () => {
11
+ const b = { name: "123" }
12
+ const context = observableContext()
13
+ const transaction = new EventProcessor(
14
+ emptyQueueEvent({
15
+ Records: [{ body: JSON.stringify(b) }, { body: JSON.stringify(b) }],
16
+ }),
17
+ context,
18
+ {},
19
+ )
20
+ let count = 0
21
+ const handlerResp = await transaction.processEvent(
22
+ async (transaction, eventRecord) => {
23
+ expect(eventRecord).to.be.deep.equal(b)
24
+ count++
25
+ return Response.SuccessResponse(null)
26
+ },
27
+ )
28
+ // check resp
29
+ expect(count).to.be.equals(2)
30
+ expect(handlerResp).to.be.an.instanceof(Response)
31
+ if (handlerResp instanceof Response) {
32
+ expect(handlerResp?.getBody()).to.be.deep.equal({
33
+ transactionID: "unknown",
34
+ })
35
+ expect(handlerResp.getCode()).to.be.equal(200)
36
+ }
37
+ // ctx
38
+ j_expect(context.fail).not.toBeCalled()
39
+ j_expect(context.done).not.toBeCalled()
40
+ j_expect(context.succeed).not.toBeCalled()
41
+ })
42
+
43
+ test("Simple success - do not decode", async () => {
44
+ const b = { name: "123" }
45
+ const context = observableContext()
46
+ const transaction = new EventProcessor(
47
+ emptyQueueEvent({
48
+ Records: [{ body: JSON.stringify(b) }, { body: JSON.stringify(b) }],
49
+ }),
50
+ context,
51
+ {},
52
+ )
53
+ let count = 0
54
+ const handlerResp = await transaction.processEvent(
55
+ async (transaction, eventRecord) => {
56
+ expect(eventRecord).to.be.deep.equal(JSON.stringify(b))
57
+ count++
58
+ return Response.SuccessResponse(null)
59
+ },
60
+ true,
61
+ )
62
+ // check resp
63
+ expect(count).to.be.equals(2)
64
+ expect(handlerResp).to.be.an.instanceof(Response)
65
+ if (handlerResp instanceof Response) {
66
+ expect(handlerResp?.getBody()).to.be.deep.equal({
67
+ transactionID: "unknown",
68
+ })
69
+ expect(handlerResp.getCode()).to.be.equal(200)
70
+ }
71
+ // ctx
72
+ j_expect(context.fail).not.toBeCalled()
73
+ j_expect(context.done).not.toBeCalled()
74
+ j_expect(context.succeed).not.toBeCalled()
75
+ })
76
+ })
77
+
78
+ describe("EventProcessor failure invocation path", () => {
79
+ test("Simple failure", async () => {
80
+ const b = { name: "123" }
81
+ const context = observableContext()
82
+ const transaction = new EventProcessor(
83
+ emptyQueueEvent({
84
+ Records: [
85
+ { body: JSON.stringify(b) },
86
+ { messageId: "123", body: JSON.stringify(b) },
87
+ { body: JSON.stringify(b) },
88
+ ],
89
+ }),
90
+ context,
91
+ {},
92
+ )
93
+ let count = 0
94
+ let handlerResp: any = null,
95
+ err: any = null
96
+ try {
97
+ handlerResp = await transaction.processEvent(
98
+ async (transaction, eventRecord) => {
99
+ expect(eventRecord).to.be.deep.equal(b)
100
+ count++
101
+ return count == 1
102
+ ? Response.SuccessResponse(null)
103
+ : Response.BadRequestResponse("Failed!")
104
+ },
105
+ )
106
+ } catch (e) {
107
+ err = e
108
+ }
109
+ // check resp
110
+ expect(count).to.be.equals(2)
111
+ expect(handlerResp).to.be.null
112
+ expect(err).to.be.deep.equal(
113
+ new Error(
114
+ JSON.stringify({
115
+ err: "Failed!",
116
+ transactionID: "unknown",
117
+ }),
118
+ ),
119
+ )
120
+ // ctx
121
+ j_expect(context.fail).not.toBeCalled()
122
+ j_expect(context.done).not.toBeCalled()
123
+ j_expect(context.succeed).not.toBeCalled()
124
+ })
125
+
126
+ test("Simple failure w/ null", async () => {
127
+ const b = { name: "123" }
128
+ const context = observableContext()
129
+ const transaction = new EventProcessor(
130
+ emptyQueueEvent({
131
+ Records: [
132
+ { body: JSON.stringify(b) },
133
+ { messageId: "123", body: JSON.stringify(b) },
134
+ { body: JSON.stringify(b) },
135
+ ],
136
+ }),
137
+ context,
138
+ {},
139
+ )
140
+ let count = 0
141
+ let handlerResp: any = null,
142
+ err: any = null
143
+ try {
144
+ handlerResp = await transaction.processEvent(
145
+ // @ts-ignore
146
+ async (transaction, eventRecord) => {
147
+ expect(eventRecord).to.be.deep.equal(b)
148
+ count++
149
+ return null
150
+ },
151
+ )
152
+ } catch (e) {
153
+ err = e
154
+ }
155
+ // check resp
156
+ expect(count).to.be.equals(1)
157
+ expect(handlerResp).to.be.null
158
+ expect(err).to.be.deep.equal(
159
+ new Error(
160
+ JSON.stringify({
161
+ err: Globals.ErrorResponseInvalidServerResponse,
162
+ rollback: true,
163
+ errCode: Globals.ErrorCode_APIError,
164
+ transactionID: "unknown",
165
+ }),
166
+ ),
167
+ )
168
+ // ctx
169
+ j_expect(context.fail).not.toBeCalled()
170
+ j_expect(context.done).not.toBeCalled()
171
+ j_expect(context.succeed).not.toBeCalled()
172
+ })
173
+
174
+ test("Simple failure - allow failures", async () => {
175
+ const b = { name: "123" }
176
+ const context = observableContext()
177
+ const transaction = new EventProcessor(
178
+ emptyQueueEvent({
179
+ Records: [
180
+ { body: JSON.stringify(b) },
181
+ { messageId: "123", body: JSON.stringify(b) },
182
+ ],
183
+ }),
184
+ context,
185
+ {},
186
+ true,
187
+ )
188
+ let count = 0
189
+ const handlerResp = await transaction.processEvent(
190
+ async (transaction, eventRecord) => {
191
+ expect(eventRecord).to.be.deep.equal(b)
192
+ count++
193
+ return count == 1
194
+ ? Response.SuccessResponse(null)
195
+ : Response.BadRequestResponse("Failed!")
196
+ },
197
+ )
198
+ // check resp
199
+ expect(count).to.be.equals(2)
200
+ expect(handlerResp).to.be.deep.equal({
201
+ batchItemFailures: [{ itemIdentifier: "123" }],
202
+ })
203
+ // ctx
204
+ j_expect(context.fail).not.toBeCalled()
205
+ j_expect(context.done).not.toBeCalled()
206
+ j_expect(context.succeed).not.toBeCalled()
207
+ })
208
+
209
+ test("Simple failure w/ null response - allow failures", async () => {
210
+ const b = { name: "123" }
211
+ const context = observableContext()
212
+ const transaction = new EventProcessor(
213
+ emptyQueueEvent({
214
+ Records: [
215
+ { messageId: "456", body: JSON.stringify(b) },
216
+ { messageId: "123", body: JSON.stringify(b) },
217
+ ],
218
+ }),
219
+ context,
220
+ {},
221
+ true,
222
+ )
223
+ let count = 0
224
+ const handlerResp = await transaction.processEvent(
225
+ // @ts-ignore
226
+ async (transaction, eventRecord) => {
227
+ expect(eventRecord).to.be.deep.equal(b)
228
+ count++
229
+ return null
230
+ },
231
+ )
232
+ // check resp
233
+ expect(count).to.be.equals(2)
234
+ expect(handlerResp).to.be.deep.equal({
235
+ batchItemFailures: [{ itemIdentifier: "456" }, { itemIdentifier: "123" }],
236
+ })
237
+ // ctx
238
+ j_expect(context.fail).not.toBeCalled()
239
+ j_expect(context.done).not.toBeCalled()
240
+ j_expect(context.succeed).not.toBeCalled()
241
+ })
242
+
243
+ test("Simple failure no records", async () => {
244
+ const context = observableContext()
245
+ const transaction = new EventProcessor(emptyQueueEvent(), context, {})
246
+ let count = 0
247
+ let handlerResp: any = null,
248
+ err: any = null
249
+ try {
250
+ handlerResp = await transaction.processEvent(
251
+ // @ts-ignore
252
+ async () => {
253
+ count++
254
+ return null
255
+ },
256
+ )
257
+ } catch (e) {
258
+ err = e
259
+ }
260
+ // check resp
261
+ expect(count).to.be.equals(0)
262
+ expect(handlerResp).to.be.null
263
+ expect(err).to.be.deep.equal(
264
+ new Error(
265
+ JSON.stringify({
266
+ err: Globals.ErrorResponseNoRecords,
267
+ errCode: Globals.ErrorCode_NoRecords,
268
+ }),
269
+ ),
270
+ )
271
+ // ctx
272
+ j_expect(context.fail).not.toBeCalled()
273
+ j_expect(context.done).not.toBeCalled()
274
+ j_expect(context.succeed).not.toBeCalled()
275
+ })
276
+ })
277
+
278
+ export {}
@@ -0,0 +1,49 @@
1
+ import { expect } from "chai"
2
+
3
+ import Response from "../../src/API/Response"
4
+ import Process from "../../src/BaseEvent/Process"
5
+
6
+ describe("Process success invocation path", () => {
7
+ test("Simple success", async () => {
8
+ const b = { name: "123" }
9
+ const proc = new Process({}, 100)
10
+ let count = 0
11
+ const handlerResp = await proc.execute(async () => {
12
+ count++
13
+ return Response.SuccessResponse(b)
14
+ })
15
+ // check resp
16
+ expect(handlerResp).to.be.undefined
17
+ // check iterations
18
+ return new Promise((resolve) => {
19
+ setTimeout(() => {
20
+ expect(count).to.be.equals(2)
21
+ clearInterval(proc.interval)
22
+ resolve(null)
23
+ }, 250)
24
+ })
25
+ })
26
+ })
27
+
28
+ describe("Process failure invocation path", () => {
29
+ test("Simple failure", async () => {
30
+ const proc = new Process({}, 100)
31
+ let count = 0
32
+ const handlerResp = await proc.execute(async () => {
33
+ count++
34
+ throw new Error("Failed!")
35
+ })
36
+ // check resp
37
+ expect(handlerResp).to.be.undefined
38
+ // check iterations
39
+ return new Promise((resolve) => {
40
+ setTimeout(() => {
41
+ expect(count).to.be.equals(2)
42
+ clearInterval(proc.interval)
43
+ resolve(null)
44
+ }, 250)
45
+ })
46
+ })
47
+ })
48
+
49
+ export {}
@@ -0,0 +1,231 @@
1
+ import { expect as j_expect } from "@jest/globals"
2
+ import { expect } from "chai"
3
+
4
+ import Response from "../../src/API/Response"
5
+ import Transaction from "../../src/BaseEvent/Transaction"
6
+ import Globals from "../../src/Globals"
7
+ import { defaultHeaders, emptyEvent, observableContext } from "../Test.utils"
8
+
9
+ describe("Transaction success invocation path", () => {
10
+ test("Simple success", async () => {
11
+ const b = { name: "123" }
12
+ const context = observableContext()
13
+ const transaction = new Transaction<null, typeof b>(emptyEvent(), context)
14
+ const handlerResp = await transaction.execute(async () => {
15
+ return Response.SuccessResponse(b)
16
+ })
17
+ // check resp
18
+ expect(handlerResp).to.be.null
19
+ // ctx
20
+ j_expect(context.fail).not.toBeCalled()
21
+ j_expect(context.done).not.toBeCalled()
22
+ j_expect(context.succeed).toBeCalledWith({
23
+ statusCode: 200,
24
+ body: JSON.stringify(b),
25
+ headers: defaultHeaders,
26
+ })
27
+ })
28
+
29
+ test("Simple success - sync return", async () => {
30
+ const b = { name: "123" }
31
+ const context = observableContext()
32
+ const transaction = new Transaction<null, typeof b>(emptyEvent(), context, {
33
+ syncReturn: true,
34
+ })
35
+ const handlerResp = await transaction.execute(async () => {
36
+ return Response.SuccessResponse(b)
37
+ })
38
+ // check resp
39
+ expect(handlerResp).to.be.an.instanceof(Response)
40
+ expect(handlerResp?.getBody()).to.be.deep.equals(b)
41
+ expect(handlerResp?.getCode()).to.be.equals(200)
42
+ // ctx
43
+ j_expect(context.fail).not.toBeCalled()
44
+ j_expect(context.done).not.toBeCalled()
45
+ j_expect(context.succeed).not.toBeCalled()
46
+ })
47
+ })
48
+
49
+ describe("Transaction error invocation path without response", () => {
50
+ test("Not valid response returned success", async () => {
51
+ const b = {
52
+ err: Globals.ErrorResponseInvalidServerResponse,
53
+ rollback: true,
54
+ errCode: Globals.ErrorCode_APIError,
55
+ transactionID: "123",
56
+ }
57
+ const context = observableContext({ awsRequestId: "123" })
58
+ const transaction = new Transaction<null, typeof b>(emptyEvent(), context)
59
+ const handlerResp = await transaction.execute(async () => {
60
+ return null
61
+ })
62
+ // check resp
63
+ expect(handlerResp).to.be.null
64
+ // ctx
65
+ j_expect(context.fail).not.toBeCalled()
66
+ j_expect(context.done).not.toBeCalled()
67
+ j_expect(context.succeed).toBeCalledWith({
68
+ statusCode: 400,
69
+ body: JSON.stringify(b),
70
+ headers: defaultHeaders,
71
+ })
72
+ })
73
+
74
+ test("Not valid response returned success - sync return", async () => {
75
+ const b = {
76
+ err: Globals.ErrorResponseInvalidServerResponse,
77
+ rollback: true,
78
+ errCode: Globals.ErrorCode_APIError,
79
+ transactionID: "123",
80
+ }
81
+ const context = observableContext()
82
+ const transaction = new Transaction<null, typeof b>(
83
+ emptyEvent({
84
+ requestContext: { requestId: "123" },
85
+ }),
86
+ context,
87
+ {
88
+ syncReturn: true,
89
+ },
90
+ )
91
+ const handlerResp = await transaction.execute(async () => {
92
+ return null
93
+ })
94
+ // check resp
95
+ expect(handlerResp).to.be.an.instanceof(Response)
96
+ expect(handlerResp?.getBody()).to.be.deep.equals(b)
97
+ expect(handlerResp?.getCode()).to.be.equals(400)
98
+ // ctx
99
+ j_expect(context.fail).not.toBeCalled()
100
+ j_expect(context.done).not.toBeCalled()
101
+ j_expect(context.succeed).not.toBeCalled()
102
+ })
103
+ })
104
+
105
+ describe("Transaction error invocation path with non-Response class response", () => {
106
+ test("Not a response-class response returned success", async () => {
107
+ const b = {
108
+ err: Globals.ErrorResponseInvalidServerResponse,
109
+ rollback: true,
110
+ errCode: Globals.ErrorCode_APIError,
111
+ transactionID: "unknown",
112
+ }
113
+ const b2 = { name: "abc" }
114
+ const context = observableContext()
115
+ const transaction = new Transaction<null, null, typeof b2>(
116
+ emptyEvent(),
117
+ context,
118
+ )
119
+ const handlerResp = await transaction.execute(async () => {
120
+ return b2
121
+ })
122
+ // check resp
123
+ expect(handlerResp).to.be.null
124
+ // ctx
125
+ j_expect(context.fail).not.toBeCalled()
126
+ j_expect(context.done).not.toBeCalled()
127
+ j_expect(context.succeed).toBeCalledWith({
128
+ statusCode: 400,
129
+ body: JSON.stringify(b),
130
+ headers: defaultHeaders,
131
+ })
132
+ })
133
+
134
+ test("Not a response-class response returned success - sync return", async () => {
135
+ const b2 = { name: "abc" }
136
+ const context = observableContext()
137
+ const transaction = new Transaction<null, null, typeof b2>(
138
+ emptyEvent(),
139
+ context,
140
+ { syncReturn: true },
141
+ )
142
+ const handlerResp = await transaction.execute(async () => {
143
+ return b2
144
+ })
145
+ // check resp
146
+ expect(handlerResp).to.be.deep.equals(b2)
147
+ // ctx
148
+ j_expect(context.fail).not.toBeCalled()
149
+ j_expect(context.done).not.toBeCalled()
150
+ j_expect(context.succeed).not.toBeCalled()
151
+ })
152
+ })
153
+
154
+ describe("Transaction error invocation path with exception", () => {
155
+ test("Transaction exception", async () => {
156
+ const b = {
157
+ err: Globals.ErrorResponseUnhandledError,
158
+ rollback: true,
159
+ errCode: Globals.ErrorCode_APIError,
160
+ transactionID: "unknown",
161
+ }
162
+ const context = observableContext()
163
+ const transaction = new Transaction<null, null, null>(emptyEvent(), context)
164
+ const handlerResp = await transaction.execute(async () => {
165
+ throw new Error("Failed!")
166
+ })
167
+ // check resp
168
+ expect(handlerResp).to.be.null
169
+ // ctx
170
+ j_expect(context.fail).not.toBeCalled()
171
+ j_expect(context.done).not.toBeCalled()
172
+ j_expect(context.succeed).toBeCalledWith({
173
+ statusCode: 400,
174
+ body: JSON.stringify(b),
175
+ headers: defaultHeaders,
176
+ })
177
+ })
178
+
179
+ test("Transaction exception - sync return", async () => {
180
+ const b = {
181
+ err: Globals.ErrorResponseUnhandledError,
182
+ rollback: true,
183
+ errCode: Globals.ErrorCode_APIError,
184
+ transactionID: "unknown",
185
+ }
186
+ const context = observableContext()
187
+ const transaction = new Transaction<null, null, null>(
188
+ emptyEvent(),
189
+ context,
190
+ { syncReturn: true },
191
+ )
192
+ const handlerResp = await transaction.execute(async () => {
193
+ throw new Error("Failed!")
194
+ })
195
+ // check resp
196
+ expect(handlerResp?.getBody()).to.be.deep.equals(b)
197
+ expect(handlerResp?.getCode()).to.be.equals(400)
198
+ // ctx
199
+ j_expect(context.fail).not.toBeCalled()
200
+ j_expect(context.done).not.toBeCalled()
201
+ j_expect(context.succeed).not.toBeCalled()
202
+ })
203
+
204
+ test("Transaction exception - sync return", async () => {
205
+ const context = observableContext()
206
+ const transaction = new Transaction<null, null, null>(
207
+ emptyEvent(),
208
+ context,
209
+ { throwOnErrors: true },
210
+ )
211
+ const error = new Error("Failed!")
212
+ let handlerResp: any = null,
213
+ err: any = null
214
+ try {
215
+ handlerResp = await transaction.execute(async () => {
216
+ throw error
217
+ })
218
+ } catch (e) {
219
+ err = e
220
+ }
221
+ // check resp
222
+ expect(handlerResp).to.be.null
223
+ expect(err).to.be.deep.equals(error)
224
+ // ctx
225
+ j_expect(context.fail).not.toBeCalled()
226
+ j_expect(context.done).not.toBeCalled()
227
+ j_expect(context.succeed).not.toBeCalled()
228
+ })
229
+ })
230
+
231
+ export {}