@creator.co/wapi 1.2.2 → 1.2.3
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.
- package/.eslintrc.cjs +29 -22
- package/.github/workflows/npmpublish.yml +1 -1
- package/.github/workflows/prs.yml +1 -1
- package/index.ts +12 -10
- package/jest.config.ts +19 -19
- package/package.json +1 -1
- package/src/API/Request.ts +17 -35
- package/src/API/Response.ts +24 -42
- package/src/API/Utils.ts +5 -7
- package/src/BaseEvent/EventProcessor.ts +16 -24
- package/src/BaseEvent/Process.ts +7 -12
- package/src/BaseEvent/Transaction.ts +25 -43
- package/src/Config/Configuration.ts +8 -14
- package/src/Config/EnvironmentVar.ts +10 -20
- package/src/Crypto/Crypto.ts +10 -10
- package/src/Crypto/JWT.ts +4 -10
- package/src/Globals.ts +19 -25
- package/src/Logger/Logger.ts +36 -51
- package/src/Mailer/Mailer.ts +19 -31
- package/src/Publisher/Publisher.ts +7 -12
- package/src/Server/RouteResolver.ts +5 -6
- package/src/Server/Router.ts +7 -12
- package/src/Server/lib/ContainerServer.ts +5 -5
- package/src/Server/lib/Server.ts +26 -38
- package/src/Server/lib/container/GenericHandler.ts +8 -13
- package/src/Server/lib/container/GenericHandlerEvent.ts +21 -35
- package/src/Server/lib/container/HealthHandler.ts +2 -2
- package/src/Server/lib/container/Proxy.ts +26 -38
- package/src/Server/lib/container/Utils.ts +2 -2
- package/src/Validation/Validator.ts +6 -6
- package/tests/API/Request.test.ts +107 -111
- package/tests/API/Response.test.ts +86 -91
- package/tests/API/Utils.test.ts +64 -64
- package/tests/BaseEvent/EventProcessor.test.ts +68 -84
- package/tests/BaseEvent/Process.test.ts +11 -11
- package/tests/BaseEvent/Transaction.test.ts +44 -53
- package/tests/Config/Config.test.ts +50 -50
- package/tests/Config/EnvironmentVar.test.ts +50 -59
- package/tests/Crypto/Crypto.test.ts +20 -22
- package/tests/Crypto/JWT.test.ts +40 -40
- package/tests/Logger/Logger.test.ts +24 -36
- package/tests/Mailer/Mailer.test.ts +21 -29
- package/tests/Publisher/Publisher.test.ts +18 -18
- package/tests/Server/RouteResolver.test.ts +56 -59
- package/tests/Server/Router.test.ts +16 -16
- package/tests/Server/lib/ContainerServer.test.ts +83 -85
- package/tests/Server/lib/Server.test.ts +4 -4
- package/tests/Server/lib/container/GenericHandler.test.ts +31 -41
- package/tests/Server/lib/container/GenericHandlerEvent.test.ts +35 -36
- package/tests/Server/lib/container/HealthHandler.test.ts +7 -7
- package/tests/Server/lib/container/Proxy.test.ts +66 -79
- package/tests/Server/lib/container/Utils.test.ts +16 -17
- package/tests/Test.utils.ts +9 -9
- package/tests/Validation/Validator.test.ts +28 -40
- package/tests/main.test.ts +2 -2
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
import { expect as j_expect } from
|
|
2
|
-
import { APIGatewayEvent, Context } from
|
|
3
|
-
import { expect } from
|
|
1
|
+
import { expect as j_expect } from '@jest/globals'
|
|
2
|
+
import { APIGatewayEvent, Context } from 'aws-lambda'
|
|
3
|
+
import { expect } from 'chai'
|
|
4
4
|
|
|
5
|
-
import Response from
|
|
6
|
-
import Transaction from
|
|
7
|
-
import Globals from
|
|
8
|
-
import { observableTransaction, defaultHeaders } from
|
|
5
|
+
import Response from '../../src/API/Response'
|
|
6
|
+
import Transaction from '../../src/BaseEvent/Transaction'
|
|
7
|
+
import Globals from '../../src/Globals'
|
|
8
|
+
import { observableTransaction, defaultHeaders } from '../Test.utils'
|
|
9
9
|
|
|
10
|
-
async function testResponse(
|
|
11
|
-
r: Response<any>,
|
|
12
|
-
body: any,
|
|
13
|
-
optCode?: number,
|
|
14
|
-
optHeaders?: any,
|
|
15
|
-
) {
|
|
10
|
+
async function testResponse(r: Response<any>, body: any, optCode?: number, optHeaders?: any) {
|
|
16
11
|
const t = observableTransaction()
|
|
17
|
-
const context = t[
|
|
12
|
+
const context = t['context']
|
|
18
13
|
const buildR = await r.build(context, t, false)
|
|
19
14
|
expect(buildR).to.be.undefined
|
|
20
15
|
j_expect(t.responseProxy).toBeCalledTimes(1)
|
|
@@ -30,46 +25,46 @@ async function testResponse(
|
|
|
30
25
|
? {
|
|
31
26
|
body: JSON.stringify({
|
|
32
27
|
...body,
|
|
33
|
-
transactionID:
|
|
28
|
+
transactionID: 'unknown',
|
|
34
29
|
}),
|
|
35
30
|
}
|
|
36
31
|
: {}),
|
|
37
32
|
})
|
|
38
33
|
}
|
|
39
34
|
|
|
40
|
-
describe(
|
|
41
|
-
test(
|
|
35
|
+
describe('Response basics', () => {
|
|
36
|
+
test('Null body', () => {
|
|
42
37
|
const r = new Response<null>(200, null)
|
|
43
38
|
expect(r.getCode()).to.be.equals(200)
|
|
44
39
|
expect(r.getBody()).to.be.null
|
|
45
40
|
})
|
|
46
41
|
|
|
47
|
-
test(
|
|
48
|
-
const r = new Response<string>(200,
|
|
42
|
+
test('String body', () => {
|
|
43
|
+
const r = new Response<string>(200, 'abc')
|
|
49
44
|
expect(r.getCode()).to.be.equals(200)
|
|
50
|
-
expect(r.getBody()).to.be.equals(
|
|
45
|
+
expect(r.getBody()).to.be.equals('abc')
|
|
51
46
|
})
|
|
52
47
|
|
|
53
|
-
test(
|
|
54
|
-
const r = new Response<any>(200, { name:
|
|
55
|
-
r.appendIntoBody(
|
|
48
|
+
test('Generic body', () => {
|
|
49
|
+
const r = new Response<any>(200, { name: 'abc' })
|
|
50
|
+
r.appendIntoBody('name2', '123')
|
|
56
51
|
expect(r.getCode()).to.be.equals(200)
|
|
57
|
-
expect(r.getBody().name).to.be.equals(
|
|
58
|
-
expect(r.getBody().name2).to.be.equals(
|
|
52
|
+
expect(r.getBody().name).to.be.equals('abc')
|
|
53
|
+
expect(r.getBody().name2).to.be.equals('123')
|
|
59
54
|
})
|
|
60
55
|
|
|
61
|
-
test(
|
|
56
|
+
test('Append/validate header', () => {
|
|
62
57
|
const r = new Response<any>(200, {})
|
|
63
|
-
r.appendHeader(
|
|
64
|
-
expect(r[
|
|
58
|
+
r.appendHeader('name2', '123')
|
|
59
|
+
expect(r['_headers']['name2']).to.be.equals('123')
|
|
65
60
|
})
|
|
66
61
|
})
|
|
67
62
|
|
|
68
|
-
describe(
|
|
69
|
-
test(
|
|
70
|
-
const b = { name:
|
|
63
|
+
describe('Response build', () => {
|
|
64
|
+
test('Succeeds to transaction context', async () => {
|
|
65
|
+
const b = { name: 'abc' }
|
|
71
66
|
const r = new Response<any>(200, b)
|
|
72
|
-
r.appendHeader(
|
|
67
|
+
r.appendHeader('abc', '123')
|
|
73
68
|
// do not use default, so we dont use proxy response for once
|
|
74
69
|
const t = new Transaction(
|
|
75
70
|
<APIGatewayEvent>(<unknown>{}),
|
|
@@ -77,9 +72,9 @@ describe("Response build", () => {
|
|
|
77
72
|
done: jest.fn(),
|
|
78
73
|
fail: jest.fn(),
|
|
79
74
|
succeed: jest.fn(),
|
|
80
|
-
})
|
|
75
|
+
})
|
|
81
76
|
)
|
|
82
|
-
const context = t[
|
|
77
|
+
const context = t['context']
|
|
83
78
|
const buildR = await r.build(context, t, false)
|
|
84
79
|
expect(buildR).to.be.undefined
|
|
85
80
|
j_expect(context.fail).not.toBeCalled()
|
|
@@ -88,20 +83,20 @@ describe("Response build", () => {
|
|
|
88
83
|
statusCode: 200,
|
|
89
84
|
headers: {
|
|
90
85
|
...defaultHeaders,
|
|
91
|
-
abc:
|
|
86
|
+
abc: '123',
|
|
92
87
|
},
|
|
93
88
|
body: JSON.stringify({
|
|
94
89
|
...b,
|
|
95
|
-
transactionID:
|
|
90
|
+
transactionID: 'unknown',
|
|
96
91
|
}),
|
|
97
92
|
})
|
|
98
93
|
})
|
|
99
94
|
|
|
100
|
-
test(
|
|
101
|
-
const b = { name:
|
|
95
|
+
test('Succeeds with no context call', async () => {
|
|
96
|
+
const b = { name: 'abc' }
|
|
102
97
|
const r = new Response<any>(200, b)
|
|
103
98
|
const t = observableTransaction()
|
|
104
|
-
const context = t[
|
|
99
|
+
const context = t['context']
|
|
105
100
|
const buildR = await r.build(context, t, true)
|
|
106
101
|
expect(buildR).to.be.undefined
|
|
107
102
|
j_expect(context.fail).not.toBeCalled()
|
|
@@ -109,30 +104,30 @@ describe("Response build", () => {
|
|
|
109
104
|
j_expect(context.succeed).not.toBeCalled()
|
|
110
105
|
})
|
|
111
106
|
|
|
112
|
-
test(
|
|
113
|
-
const b = { name:
|
|
107
|
+
test('Succeeds to transaction context as RAW', async () => {
|
|
108
|
+
const b = { name: 'abc' }
|
|
114
109
|
const r = new Response<any>(200, b, {
|
|
115
110
|
rawBody: true,
|
|
116
111
|
})
|
|
117
112
|
const t = observableTransaction()
|
|
118
|
-
const context = t[
|
|
113
|
+
const context = t['context']
|
|
119
114
|
const buildR = await r.build(context, t, false)
|
|
120
115
|
expect(buildR).to.be.undefined
|
|
121
116
|
j_expect(context.fail).not.toBeCalled()
|
|
122
117
|
j_expect(context.done).not.toBeCalled()
|
|
123
118
|
j_expect(context.succeed).toHaveBeenCalledWith({
|
|
124
119
|
...b,
|
|
125
|
-
transactionID:
|
|
120
|
+
transactionID: 'unknown',
|
|
126
121
|
})
|
|
127
122
|
})
|
|
128
123
|
|
|
129
|
-
test(
|
|
130
|
-
const b = { name:
|
|
124
|
+
test('Succeeds to transaction piping', async () => {
|
|
125
|
+
const b = { name: 'abc' }
|
|
131
126
|
const r = new Response<any>(200, b, {
|
|
132
127
|
shouldStream: true,
|
|
133
128
|
})
|
|
134
129
|
const t = observableTransaction()
|
|
135
|
-
const context = t[
|
|
130
|
+
const context = t['context']
|
|
136
131
|
const buildR = await r.build(context, t, false)
|
|
137
132
|
await r.build(context, t, false) //pipe twice, should not affect
|
|
138
133
|
expect(buildR).to.be.undefined
|
|
@@ -150,13 +145,13 @@ describe("Response build", () => {
|
|
|
150
145
|
})
|
|
151
146
|
})
|
|
152
147
|
|
|
153
|
-
test(
|
|
154
|
-
const b = { name:
|
|
148
|
+
test('Failure to transaction context as RAW', async () => {
|
|
149
|
+
const b = { name: 'abc' }
|
|
155
150
|
const r = new Response<any>(400, b, {
|
|
156
151
|
rawBody: true,
|
|
157
152
|
})
|
|
158
153
|
const t = observableTransaction()
|
|
159
|
-
const context = t[
|
|
154
|
+
const context = t['context']
|
|
160
155
|
const buildR = await r.build(context, t, false)
|
|
161
156
|
expect(buildR).to.be.undefined
|
|
162
157
|
j_expect(context.done).not.toBeCalled()
|
|
@@ -164,14 +159,14 @@ describe("Response build", () => {
|
|
|
164
159
|
j_expect(context.fail).toHaveBeenCalledTimes(1)
|
|
165
160
|
})
|
|
166
161
|
|
|
167
|
-
test(
|
|
168
|
-
const b = { name:
|
|
162
|
+
test('Failure exception to transaction context as RAW', async () => {
|
|
163
|
+
const b = { name: 'abc' }
|
|
169
164
|
const r = new Response<any>(400, b, {
|
|
170
165
|
rawBody: true,
|
|
171
166
|
throwOnErrors: true,
|
|
172
167
|
})
|
|
173
168
|
const t = observableTransaction()
|
|
174
|
-
const context = t[
|
|
169
|
+
const context = t['context']
|
|
175
170
|
let buildR: any = null,
|
|
176
171
|
exception = null
|
|
177
172
|
try {
|
|
@@ -186,14 +181,14 @@ describe("Response build", () => {
|
|
|
186
181
|
j_expect(context.fail).not.toBeCalled()
|
|
187
182
|
})
|
|
188
183
|
|
|
189
|
-
test(
|
|
184
|
+
test('Failure exception to transaction context as RAW and null body', async () => {
|
|
190
185
|
const b = null
|
|
191
186
|
const r = new Response<any>(400, b, {
|
|
192
187
|
rawBody: true,
|
|
193
188
|
throwOnErrors: true,
|
|
194
189
|
})
|
|
195
190
|
const t = observableTransaction()
|
|
196
|
-
const context = t[
|
|
191
|
+
const context = t['context']
|
|
197
192
|
let buildR: any = null,
|
|
198
193
|
exception = null
|
|
199
194
|
try {
|
|
@@ -208,14 +203,14 @@ describe("Response build", () => {
|
|
|
208
203
|
j_expect(context.fail).not.toBeCalled()
|
|
209
204
|
})
|
|
210
205
|
|
|
211
|
-
test(
|
|
212
|
-
const b = { message:
|
|
206
|
+
test('Failure exception to transaction context as RAW and custom ero', async () => {
|
|
207
|
+
const b = { message: 'abc', error: 'error 123' }
|
|
213
208
|
const r = new Response<any>(400, b, {
|
|
214
209
|
rawBody: true,
|
|
215
210
|
throwOnErrors: true,
|
|
216
211
|
})
|
|
217
212
|
const t = observableTransaction()
|
|
218
|
-
const context = t[
|
|
213
|
+
const context = t['context']
|
|
219
214
|
let buildR: any = null,
|
|
220
215
|
exception = null
|
|
221
216
|
try {
|
|
@@ -231,9 +226,9 @@ describe("Response build", () => {
|
|
|
231
226
|
})
|
|
232
227
|
})
|
|
233
228
|
|
|
234
|
-
describe(
|
|
235
|
-
test(
|
|
236
|
-
const pName =
|
|
229
|
+
describe('Response shortcuts', () => {
|
|
230
|
+
test('MissingParamResponse', async () => {
|
|
231
|
+
const pName = 'abc'
|
|
237
232
|
const r = Response.MissingParamResponse(pName)
|
|
238
233
|
await testResponse(r, {
|
|
239
234
|
err: `Invalid request. Path parameter ${pName} is missing.`,
|
|
@@ -241,8 +236,8 @@ describe("Response shortcuts", () => {
|
|
|
241
236
|
})
|
|
242
237
|
})
|
|
243
238
|
|
|
244
|
-
test(
|
|
245
|
-
const pName =
|
|
239
|
+
test('MissingQueryResponse', async () => {
|
|
240
|
+
const pName = 'abc'
|
|
246
241
|
const r = Response.MissingQueryResponse(pName)
|
|
247
242
|
await testResponse(r, {
|
|
248
243
|
err: `Invalid request. Query parameter ${pName} is missing.`,
|
|
@@ -250,9 +245,9 @@ describe("Response shortcuts", () => {
|
|
|
250
245
|
})
|
|
251
246
|
})
|
|
252
247
|
|
|
253
|
-
test(
|
|
254
|
-
const message =
|
|
255
|
-
const code =
|
|
248
|
+
test('BadRequestResponse', async () => {
|
|
249
|
+
const message = 'abc'
|
|
250
|
+
const code = 'CODE'
|
|
256
251
|
const optBody = { additional: 123 }
|
|
257
252
|
const r = Response.BadRequestResponse(message, code, optBody)
|
|
258
253
|
await testResponse(r, {
|
|
@@ -262,17 +257,17 @@ describe("Response shortcuts", () => {
|
|
|
262
257
|
})
|
|
263
258
|
})
|
|
264
259
|
|
|
265
|
-
test(
|
|
266
|
-
const message =
|
|
260
|
+
test('BadRequestResponse no opts', async () => {
|
|
261
|
+
const message = 'abc'
|
|
267
262
|
const r = Response.BadRequestResponse(message)
|
|
268
263
|
await testResponse(r, {
|
|
269
264
|
err: message,
|
|
270
265
|
})
|
|
271
266
|
})
|
|
272
267
|
|
|
273
|
-
test(
|
|
274
|
-
const message =
|
|
275
|
-
const code =
|
|
268
|
+
test('BadRequestResponseWithRollback', async () => {
|
|
269
|
+
const message = 'abc'
|
|
270
|
+
const code = 'CODE'
|
|
276
271
|
const optBody = { additional: 123 }
|
|
277
272
|
const r = Response.BadRequestResponseWithRollback(message, code, optBody)
|
|
278
273
|
await testResponse(r, {
|
|
@@ -283,8 +278,8 @@ describe("Response shortcuts", () => {
|
|
|
283
278
|
})
|
|
284
279
|
})
|
|
285
280
|
|
|
286
|
-
test(
|
|
287
|
-
const message =
|
|
281
|
+
test('BadRequestResponseWithRollback no opts', async () => {
|
|
282
|
+
const message = 'abc'
|
|
288
283
|
const r = Response.BadRequestResponseWithRollback(message)
|
|
289
284
|
await testResponse(r, {
|
|
290
285
|
err: message,
|
|
@@ -292,9 +287,9 @@ describe("Response shortcuts", () => {
|
|
|
292
287
|
})
|
|
293
288
|
})
|
|
294
289
|
|
|
295
|
-
test(
|
|
296
|
-
const message =
|
|
297
|
-
const code =
|
|
290
|
+
test('UnauthorizedResponse', async () => {
|
|
291
|
+
const message = 'abc'
|
|
292
|
+
const code = 'CODE'
|
|
298
293
|
const r = Response.UnauthorizedResponse(message, code)
|
|
299
294
|
await testResponse(
|
|
300
295
|
r,
|
|
@@ -302,53 +297,53 @@ describe("Response shortcuts", () => {
|
|
|
302
297
|
err: message,
|
|
303
298
|
errCode: code,
|
|
304
299
|
},
|
|
305
|
-
401
|
|
300
|
+
401
|
|
306
301
|
)
|
|
307
302
|
})
|
|
308
303
|
|
|
309
|
-
test(
|
|
310
|
-
const message =
|
|
304
|
+
test('UnauthorizedResponse no code', async () => {
|
|
305
|
+
const message = 'abc'
|
|
311
306
|
const r = Response.UnauthorizedResponse(message)
|
|
312
307
|
await testResponse(r, { err: message }, 401)
|
|
313
308
|
})
|
|
314
309
|
|
|
315
|
-
test(
|
|
310
|
+
test('SuccessResponse', async () => {
|
|
316
311
|
const body = { additional: 123 }
|
|
317
312
|
const r = Response.SuccessResponse(body)
|
|
318
313
|
await testResponse(r, body, 200)
|
|
319
314
|
})
|
|
320
315
|
|
|
321
|
-
test(
|
|
316
|
+
test('SuccessResponse null body', async () => {
|
|
322
317
|
const r = Response.SuccessResponse(null)
|
|
323
318
|
await testResponse(r, {}, 200)
|
|
324
319
|
})
|
|
325
320
|
|
|
326
|
-
test(
|
|
321
|
+
test('SuccessNoContentResponse', async () => {
|
|
327
322
|
const r = Response.SuccessNoContentResponse()
|
|
328
323
|
await testResponse(r, null, 204)
|
|
329
324
|
})
|
|
330
325
|
|
|
331
|
-
test(
|
|
326
|
+
test('SimpleResponse', async () => {
|
|
332
327
|
const r = Response.SimpleResponse(null)
|
|
333
328
|
await testResponse(r, null, 200)
|
|
334
329
|
})
|
|
335
330
|
|
|
336
|
-
test(
|
|
331
|
+
test('SimpleResponse override statuscode', async () => {
|
|
337
332
|
const r = Response.SimpleResponse(null, 203)
|
|
338
333
|
await testResponse(r, null, 203)
|
|
339
334
|
})
|
|
340
335
|
|
|
341
|
-
test(
|
|
342
|
-
const url =
|
|
336
|
+
test('RedirectResponse', async () => {
|
|
337
|
+
const url = 'https://google.com'
|
|
343
338
|
const r = Response.RedirectResponse(url)
|
|
344
339
|
await testResponse(r, null, 302, { Location: url })
|
|
345
340
|
})
|
|
346
341
|
|
|
347
|
-
test(
|
|
348
|
-
const b = { name:
|
|
349
|
-
const r = Response.SuccessStreamResponse(b,
|
|
342
|
+
test('SuccessStreamResponse', async () => {
|
|
343
|
+
const b = { name: 'abc' }
|
|
344
|
+
const r = Response.SuccessStreamResponse(b, 'test-stream')
|
|
350
345
|
const t = observableTransaction()
|
|
351
|
-
const context = t[
|
|
346
|
+
const context = t['context']
|
|
352
347
|
const buildR = await r.build(context, t, false)
|
|
353
348
|
await r.build(context, t, false) //pipe twice, should not affect
|
|
354
349
|
expect(buildR).to.be.undefined
|
|
@@ -359,8 +354,8 @@ describe("Response shortcuts", () => {
|
|
|
359
354
|
statusCode: 200,
|
|
360
355
|
headers: {
|
|
361
356
|
...defaultHeaders,
|
|
362
|
-
|
|
363
|
-
Connection:
|
|
357
|
+
'Content-Type': 'test-stream',
|
|
358
|
+
Connection: 'keep-alive',
|
|
364
359
|
},
|
|
365
360
|
body: {
|
|
366
361
|
...b,
|
package/tests/API/Utils.test.ts
CHANGED
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
import { expect } from
|
|
1
|
+
import { expect } from 'chai'
|
|
2
2
|
|
|
3
|
-
import Utils from
|
|
3
|
+
import Utils from '../../src/API/Utils'
|
|
4
4
|
|
|
5
|
-
describe(
|
|
6
|
-
test(
|
|
5
|
+
describe('isHybridlessContainer', () => {
|
|
6
|
+
test('Simple isHybridlessContainer test', () => {
|
|
7
7
|
const v = Utils.isHybridlessContainer()
|
|
8
|
-
expect(v).to.be.a(
|
|
8
|
+
expect(v).to.be.a('boolean')
|
|
9
9
|
// this is done, so tests are reliable based on the testing .env
|
|
10
|
-
expect(v).to.be[process.env.HYBRIDLESS_RUNTIME ?
|
|
10
|
+
expect(v).to.be[process.env.HYBRIDLESS_RUNTIME ? 'true' : 'false']
|
|
11
11
|
})
|
|
12
12
|
})
|
|
13
13
|
|
|
14
|
-
describe(
|
|
15
|
-
test(
|
|
16
|
-
const v = Utils.isValidString(
|
|
17
|
-
expect(v).to.be.a(
|
|
14
|
+
describe('isValidString', () => {
|
|
15
|
+
test('Empty string is not valid', () => {
|
|
16
|
+
const v = Utils.isValidString('')
|
|
17
|
+
expect(v).to.be.a('boolean')
|
|
18
18
|
expect(v).to.be.false
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
-
test(
|
|
21
|
+
test('Number is not a valid string', () => {
|
|
22
22
|
// @ts-ignore
|
|
23
23
|
const v = Utils.isValidString(12)
|
|
24
|
-
expect(v).to.be.a(
|
|
24
|
+
expect(v).to.be.a('boolean')
|
|
25
25
|
expect(v).to.be.false
|
|
26
26
|
})
|
|
27
27
|
|
|
28
|
-
test(
|
|
29
|
-
const v = Utils.isValidString(
|
|
30
|
-
expect(v).to.be.a(
|
|
28
|
+
test('Simple string is valid', () => {
|
|
29
|
+
const v = Utils.isValidString('abc')
|
|
30
|
+
expect(v).to.be.a('boolean')
|
|
31
31
|
expect(v).to.be.true
|
|
32
32
|
})
|
|
33
33
|
})
|
|
34
34
|
|
|
35
|
-
describe(
|
|
36
|
-
test(
|
|
37
|
-
const v = Utils.parseIntNullIfNaN(
|
|
35
|
+
describe('parseIntNullIfNaN', () => {
|
|
36
|
+
test('Null if only chars', () => {
|
|
37
|
+
const v = Utils.parseIntNullIfNaN('abc')
|
|
38
38
|
expect(v).to.be.null
|
|
39
39
|
})
|
|
40
40
|
|
|
41
|
-
test(
|
|
42
|
-
const v = Utils.parseIntNullIfNaN(
|
|
41
|
+
test('Null if chars with numbers', () => {
|
|
42
|
+
const v = Utils.parseIntNullIfNaN('abc123')
|
|
43
43
|
expect(v).to.be.null
|
|
44
44
|
})
|
|
45
45
|
|
|
46
|
-
test(
|
|
46
|
+
test('Null if null', () => {
|
|
47
47
|
// @ts-ignore
|
|
48
48
|
const v = Utils.parseIntNullIfNaN(null)
|
|
49
49
|
expect(v).to.be.null
|
|
50
50
|
})
|
|
51
51
|
|
|
52
|
-
test(
|
|
53
|
-
const v = Utils.parseIntNullIfNaN(
|
|
54
|
-
expect(v).to.be.a(
|
|
52
|
+
test('Not null if starts with numbers (js behavior)', () => {
|
|
53
|
+
const v = Utils.parseIntNullIfNaN('123abc')
|
|
54
|
+
expect(v).to.be.a('number')
|
|
55
55
|
expect(v).to.be.equals(123)
|
|
56
56
|
})
|
|
57
57
|
|
|
58
|
-
test(
|
|
59
|
-
const v = Utils.parseIntNullIfNaN(
|
|
60
|
-
expect(v).to.be.a(
|
|
58
|
+
test('Not null if numbers', () => {
|
|
59
|
+
const v = Utils.parseIntNullIfNaN('123')
|
|
60
|
+
expect(v).to.be.a('number')
|
|
61
61
|
expect(v).to.be.equals(123)
|
|
62
62
|
})
|
|
63
63
|
|
|
64
|
-
test(
|
|
64
|
+
test('Not null if number type', () => {
|
|
65
65
|
// @ts-ignore
|
|
66
66
|
const v = Utils.parseIntNullIfNaN(123)
|
|
67
|
-
expect(v).to.be.a(
|
|
67
|
+
expect(v).to.be.a('number')
|
|
68
68
|
expect(v).to.be.equals(123)
|
|
69
69
|
})
|
|
70
70
|
})
|
|
71
71
|
|
|
72
|
-
describe(
|
|
73
|
-
test(
|
|
74
|
-
const v = Utils.parseObjectNullIfEmpty(
|
|
72
|
+
describe('parseObjectNullIfEmpty', () => {
|
|
73
|
+
test('Null if only chars', () => {
|
|
74
|
+
const v = Utils.parseObjectNullIfEmpty('abc')
|
|
75
75
|
expect(v).to.be.null
|
|
76
76
|
})
|
|
77
77
|
|
|
78
|
-
test(
|
|
79
|
-
const v = Utils.parseObjectNullIfEmpty(
|
|
78
|
+
test('Null if chars with numbers', () => {
|
|
79
|
+
const v = Utils.parseObjectNullIfEmpty('abc123')
|
|
80
80
|
expect(v).to.be.null
|
|
81
81
|
})
|
|
82
82
|
|
|
83
|
-
test(
|
|
83
|
+
test('Null if null', () => {
|
|
84
84
|
// @ts-ignore
|
|
85
85
|
const v = Utils.parseObjectNullIfEmpty(null)
|
|
86
86
|
expect(v).to.be.null
|
|
87
87
|
})
|
|
88
88
|
|
|
89
|
-
test(
|
|
90
|
-
const v = Utils.parseObjectNullIfEmpty(
|
|
89
|
+
test('Null if malformed object', () => {
|
|
90
|
+
const v = Utils.parseObjectNullIfEmpty('{d: 123}')
|
|
91
91
|
expect(v).to.be.null
|
|
92
92
|
})
|
|
93
93
|
|
|
94
|
-
test(
|
|
95
|
-
const v = Utils.parseObjectNullIfEmpty(
|
|
94
|
+
test('Null if empty object', () => {
|
|
95
|
+
const v = Utils.parseObjectNullIfEmpty('{}')
|
|
96
96
|
expect(v).to.be.null
|
|
97
97
|
})
|
|
98
98
|
|
|
99
|
-
test(
|
|
99
|
+
test('Not null if object with a key', () => {
|
|
100
100
|
const v = Utils.parseObjectNullIfEmpty('{"d": 123}')
|
|
101
|
-
expect(v).to.be.a(
|
|
102
|
-
expect(v?.[
|
|
101
|
+
expect(v).to.be.a('object')
|
|
102
|
+
expect(v?.['d']).to.be.equals(123)
|
|
103
103
|
})
|
|
104
104
|
})
|
|
105
105
|
|
|
106
|
-
describe(
|
|
107
|
-
test(
|
|
108
|
-
const v = Utils.isValidNumber(
|
|
106
|
+
describe('isValidNumber', () => {
|
|
107
|
+
test('Not a valid number if only chars', () => {
|
|
108
|
+
const v = Utils.isValidNumber('abc')
|
|
109
109
|
expect(v).to.be.false
|
|
110
110
|
})
|
|
111
111
|
|
|
112
|
-
test(
|
|
113
|
-
const v = Utils.isValidNumber(
|
|
112
|
+
test('Not a valid number if only chars and numbers', () => {
|
|
113
|
+
const v = Utils.isValidNumber('abc123')
|
|
114
114
|
expect(v).to.be.false
|
|
115
115
|
})
|
|
116
116
|
|
|
117
|
-
test(
|
|
118
|
-
const v = Utils.isValidNumber(
|
|
117
|
+
test('Valid number if numbers and chars after', () => {
|
|
118
|
+
const v = Utils.isValidNumber('123abc')
|
|
119
119
|
expect(v).to.be.true
|
|
120
120
|
})
|
|
121
121
|
|
|
122
|
-
test(
|
|
123
|
-
const v = Utils.isValidNumber(
|
|
122
|
+
test('Valid number if numbers', () => {
|
|
123
|
+
const v = Utils.isValidNumber('123')
|
|
124
124
|
expect(v).to.be.true
|
|
125
125
|
})
|
|
126
126
|
})
|
|
127
127
|
|
|
128
|
-
describe(
|
|
129
|
-
test(
|
|
130
|
-
const v = Utils.caseInsensitiveObjectForKey({ abc: 123 },
|
|
131
|
-
expect(v).to.be.a(
|
|
128
|
+
describe('caseInsensitiveObjectForKey', () => {
|
|
129
|
+
test('Get a key that is lowercase, asking uppercase', () => {
|
|
130
|
+
const v = Utils.caseInsensitiveObjectForKey({ abc: 123 }, 'ABC')
|
|
131
|
+
expect(v).to.be.a('number')
|
|
132
132
|
expect(v).to.be.equals(123)
|
|
133
133
|
})
|
|
134
134
|
|
|
135
|
-
test(
|
|
136
|
-
const v = Utils.caseInsensitiveObjectForKey({ ABC: 123 },
|
|
137
|
-
expect(v).to.be.a(
|
|
135
|
+
test('Get a key that is uppercase, asking lowercase', () => {
|
|
136
|
+
const v = Utils.caseInsensitiveObjectForKey({ ABC: 123 }, 'abc')
|
|
137
|
+
expect(v).to.be.a('number')
|
|
138
138
|
expect(v).to.be.equals(123)
|
|
139
139
|
})
|
|
140
140
|
|
|
141
|
-
test(
|
|
142
|
-
const v = Utils.caseInsensitiveObjectForKey({ ABC: 123 },
|
|
141
|
+
test('Fails gracefully if key is not found', () => {
|
|
142
|
+
const v = Utils.caseInsensitiveObjectForKey({ ABC: 123 }, 'asd')
|
|
143
143
|
expect(v).to.be.null
|
|
144
144
|
})
|
|
145
145
|
|
|
146
|
-
test(
|
|
147
|
-
const v = Utils.caseInsensitiveObjectForKey(null,
|
|
146
|
+
test('Fails gracefully for null object', () => {
|
|
147
|
+
const v = Utils.caseInsensitiveObjectForKey(null, 'abc')
|
|
148
148
|
expect(v).to.be.null
|
|
149
149
|
})
|
|
150
150
|
|
|
151
|
-
test(
|
|
152
|
-
const v = Utils.caseInsensitiveObjectForKey(
|
|
151
|
+
test('Fails gracefully for invalid object', () => {
|
|
152
|
+
const v = Utils.caseInsensitiveObjectForKey('', 'abc')
|
|
153
153
|
expect(v).to.be.null
|
|
154
154
|
})
|
|
155
155
|
})
|