@creator.co/wapi 1.2.11 → 1.3.1
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/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/package.json +2 -1
- package/dist/src/API/Request.d.ts +26 -18
- package/dist/src/API/Request.js +27 -14
- package/dist/src/API/Request.js.map +1 -1
- package/dist/src/API/Response.d.ts +1 -1
- package/dist/src/API/Response.js.map +1 -1
- package/dist/src/BaseEvent/Transaction.d.ts +9 -3
- package/dist/src/BaseEvent/Transaction.js.map +1 -1
- package/dist/src/Database/Database.d.ts +1 -1
- package/dist/src/Database/index.d.ts +7 -0
- package/dist/src/Database/index.js +10 -0
- package/dist/src/Database/index.js.map +1 -0
- package/dist/src/Database/integrations/knex/KnexDatabase.d.ts +1 -0
- package/dist/src/Database/integrations/knex/KnexDatabase.js +9 -4
- package/dist/src/Database/integrations/knex/KnexDatabase.js.map +1 -1
- package/dist/src/Database/types.d.ts +47 -0
- package/dist/src/Database/types.js +3 -0
- package/dist/src/Database/types.js.map +1 -0
- package/dist/src/Server/Router.d.ts +18 -8
- package/dist/src/Server/Router.js.map +1 -1
- package/dist/src/Server/lib/Server.js +15 -1
- package/dist/src/Server/lib/Server.js.map +1 -1
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +1 -3
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -1
- package/index.ts +2 -0
- package/package.json +2 -1
- package/src/API/Request.ts +38 -21
- package/src/API/Response.ts +1 -1
- package/src/BaseEvent/Transaction.ts +16 -4
- package/src/Database/Database.ts +1 -1
- package/src/Database/index.ts +15 -0
- package/src/Database/integrations/knex/KnexDatabase.ts +10 -2
- package/src/Database/{types.d.ts → types.ts} +7 -2
- package/src/Server/Router.ts +29 -8
- package/src/Server/lib/Server.ts +18 -1
- package/src/Server/lib/container/GenericHandlerEvent.ts +1 -3
- package/tests/API/Request.test.ts +8 -2
- package/tests/API/Utils.test.ts +10 -0
- package/tests/Database/integrations/knex/KnexDatabase.test.ts +23 -0
- package/tests/Database/integrations/knex/KnexTransaction.test.ts +1 -1
- package/tests/Server/lib/ContainerServer.test.ts +219 -1
|
@@ -15,6 +15,17 @@ export const ViewSchema = z.object({
|
|
|
15
15
|
updatedAt: z.string(),
|
|
16
16
|
})
|
|
17
17
|
|
|
18
|
+
export const QuerySchema = z.object({
|
|
19
|
+
id: z.string(),
|
|
20
|
+
order: z.string().refine(
|
|
21
|
+
v => {
|
|
22
|
+
const n = Number(v)
|
|
23
|
+
return !isNaN(n) && v?.length > 0
|
|
24
|
+
},
|
|
25
|
+
{ message: 'Invalid number' }
|
|
26
|
+
),
|
|
27
|
+
})
|
|
28
|
+
|
|
18
29
|
describe('Container server routing', () => {
|
|
19
30
|
// @ts-ignore
|
|
20
31
|
let mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {})
|
|
@@ -155,7 +166,7 @@ describe('Container server basics', () => {
|
|
|
155
166
|
}, 10000)
|
|
156
167
|
})
|
|
157
168
|
|
|
158
|
-
describe('Container server validation', () => {
|
|
169
|
+
describe('Container server validation (body)', () => {
|
|
159
170
|
// @ts-ignore
|
|
160
171
|
let mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {})
|
|
161
172
|
beforeAll(() => {
|
|
@@ -324,4 +335,211 @@ describe('Container server validation', () => {
|
|
|
324
335
|
await server.stop()
|
|
325
336
|
})
|
|
326
337
|
})
|
|
338
|
+
|
|
339
|
+
describe('Container server validation (query)', () => {
|
|
340
|
+
// @ts-ignore
|
|
341
|
+
let mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {})
|
|
342
|
+
beforeAll(() => {
|
|
343
|
+
// @ts-ignore
|
|
344
|
+
mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {})
|
|
345
|
+
})
|
|
346
|
+
afterAll(() => {
|
|
347
|
+
mockExit.mockRestore()
|
|
348
|
+
})
|
|
349
|
+
beforeEach(() => {
|
|
350
|
+
mockExit.mockReset()
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
function validateValidationFailure(res: any, failureCount?: number) {
|
|
354
|
+
c_expect(res.body['err']).to.be.equals(Globals.ErrorResponseValidationFail)
|
|
355
|
+
c_expect(res.body['errCode']).to.be.equals(Globals.ErrorCode_InvalidInput)
|
|
356
|
+
c_expect(res.body['transactionID']).to.not.be.null
|
|
357
|
+
c_expect(res.body['transactionID']).to.be.an('string')
|
|
358
|
+
c_expect(res.body['validationFailure']?.length).to.be.equals(failureCount || 2)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
test('Validates empty query', async () => {
|
|
362
|
+
const server = new ContainerServer({
|
|
363
|
+
routes: [
|
|
364
|
+
{
|
|
365
|
+
path: '/abc',
|
|
366
|
+
method: HttpMethod.POST,
|
|
367
|
+
querySchema: QuerySchema,
|
|
368
|
+
handler: async () => {
|
|
369
|
+
return Response.SimpleResponse({ name: 'abc' })
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
],
|
|
373
|
+
})
|
|
374
|
+
await server.start()
|
|
375
|
+
// Validation fails, empty body
|
|
376
|
+
const resG = await request(defaultUrl)
|
|
377
|
+
.post(`/abc`)
|
|
378
|
+
.expect('Content-Type', 'application/json; charset=utf-8')
|
|
379
|
+
.expect(400)
|
|
380
|
+
validateValidationFailure(resG, 1)
|
|
381
|
+
await server.stop()
|
|
382
|
+
})
|
|
383
|
+
|
|
384
|
+
test('Validates empty query differently', async () => {
|
|
385
|
+
const server = new ContainerServer({
|
|
386
|
+
routes: [
|
|
387
|
+
{
|
|
388
|
+
path: '/abc',
|
|
389
|
+
method: HttpMethod.POST,
|
|
390
|
+
querySchema: QuerySchema,
|
|
391
|
+
handler: async () => {
|
|
392
|
+
return Response.SimpleResponse({ name: 'abc' })
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
],
|
|
396
|
+
})
|
|
397
|
+
await server.start()
|
|
398
|
+
// Validation fails, empty body
|
|
399
|
+
const resG = await request(defaultUrl)
|
|
400
|
+
.post(`/abc?`)
|
|
401
|
+
.expect('Content-Type', 'application/json; charset=utf-8')
|
|
402
|
+
.expect(400)
|
|
403
|
+
validateValidationFailure(resG, 1)
|
|
404
|
+
await server.stop()
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
test('Validates missing props body', async () => {
|
|
408
|
+
const server = new ContainerServer({
|
|
409
|
+
routes: [
|
|
410
|
+
{
|
|
411
|
+
path: '/abc',
|
|
412
|
+
method: HttpMethod.POST,
|
|
413
|
+
querySchema: QuerySchema,
|
|
414
|
+
handler: async () => {
|
|
415
|
+
return Response.SimpleResponse({ name: 'abc' })
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
],
|
|
419
|
+
})
|
|
420
|
+
await server.start()
|
|
421
|
+
// Validation fails, empty body
|
|
422
|
+
const resG = await request(defaultUrl)
|
|
423
|
+
.post(`/abc?id=myname`)
|
|
424
|
+
.expect('Content-Type', 'application/json; charset=utf-8')
|
|
425
|
+
.expect(400)
|
|
426
|
+
validateValidationFailure(resG, 1)
|
|
427
|
+
await server.stop()
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
test('Validates wrong type props body', async () => {
|
|
431
|
+
const server = new ContainerServer({
|
|
432
|
+
routes: [
|
|
433
|
+
{
|
|
434
|
+
path: '/abc',
|
|
435
|
+
method: HttpMethod.POST,
|
|
436
|
+
querySchema: QuerySchema,
|
|
437
|
+
handler: async () => {
|
|
438
|
+
return Response.SimpleResponse({ name: 'abc' })
|
|
439
|
+
},
|
|
440
|
+
},
|
|
441
|
+
],
|
|
442
|
+
})
|
|
443
|
+
await server.start()
|
|
444
|
+
// Validation fails, empty body
|
|
445
|
+
const resG = await request(defaultUrl)
|
|
446
|
+
.post(`/abc?id=name&order=myname`)
|
|
447
|
+
.expect('Content-Type', 'application/json; charset=utf-8')
|
|
448
|
+
.expect(400)
|
|
449
|
+
validateValidationFailure(resG, 1)
|
|
450
|
+
await server.stop()
|
|
451
|
+
})
|
|
452
|
+
|
|
453
|
+
test('Validates successfully2', async () => {
|
|
454
|
+
const server = new ContainerServer({
|
|
455
|
+
routes: [
|
|
456
|
+
{
|
|
457
|
+
path: '/abc',
|
|
458
|
+
method: HttpMethod.POST,
|
|
459
|
+
querySchema: QuerySchema,
|
|
460
|
+
handler: async () => {
|
|
461
|
+
return Response.SimpleResponse({ name: 'abc' })
|
|
462
|
+
},
|
|
463
|
+
},
|
|
464
|
+
],
|
|
465
|
+
})
|
|
466
|
+
await server.start()
|
|
467
|
+
// Validation fails, empty body
|
|
468
|
+
await request(defaultUrl)
|
|
469
|
+
.post(`/abc`)
|
|
470
|
+
.query({ id: 'name', order: 123 })
|
|
471
|
+
.expect('Content-Type', 'application/json; charset=utf-8')
|
|
472
|
+
.expect(200)
|
|
473
|
+
await server.stop()
|
|
474
|
+
})
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
describe('Container server validation (path)', () => {
|
|
478
|
+
// @ts-ignore
|
|
479
|
+
let mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {})
|
|
480
|
+
beforeAll(() => {
|
|
481
|
+
// @ts-ignore
|
|
482
|
+
mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {})
|
|
483
|
+
})
|
|
484
|
+
afterAll(() => {
|
|
485
|
+
mockExit.mockRestore()
|
|
486
|
+
})
|
|
487
|
+
beforeEach(() => {
|
|
488
|
+
mockExit.mockReset()
|
|
489
|
+
})
|
|
490
|
+
|
|
491
|
+
function validateValidationFailure(res: any, failureCount?: number) {
|
|
492
|
+
c_expect(res.body['err']).to.be.equals(Globals.ErrorResponseValidationFail)
|
|
493
|
+
c_expect(res.body['errCode']).to.be.equals(Globals.ErrorCode_InvalidInput)
|
|
494
|
+
c_expect(res.body['transactionID']).to.not.be.null
|
|
495
|
+
c_expect(res.body['transactionID']).to.be.an('string')
|
|
496
|
+
c_expect(res.body['validationFailure']?.length).to.be.equals(failureCount || 2)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
test('Validates wrong type props body', async () => {
|
|
500
|
+
const server = new ContainerServer({
|
|
501
|
+
routes: [
|
|
502
|
+
{
|
|
503
|
+
path: '/abc/:id/:order',
|
|
504
|
+
method: HttpMethod.POST,
|
|
505
|
+
pathSchema: QuerySchema,
|
|
506
|
+
handler: async () => {
|
|
507
|
+
return Response.SimpleResponse({ name: 'abc' })
|
|
508
|
+
},
|
|
509
|
+
},
|
|
510
|
+
],
|
|
511
|
+
})
|
|
512
|
+
await server.start()
|
|
513
|
+
// Validation fails, empty body
|
|
514
|
+
const resG = await request(defaultUrl)
|
|
515
|
+
.post(`/abc/name/myname`)
|
|
516
|
+
.expect('Content-Type', 'application/json; charset=utf-8')
|
|
517
|
+
.expect(400)
|
|
518
|
+
validateValidationFailure(resG, 1)
|
|
519
|
+
await server.stop()
|
|
520
|
+
})
|
|
521
|
+
|
|
522
|
+
test('Validates successfully4', async () => {
|
|
523
|
+
const server = new ContainerServer({
|
|
524
|
+
routes: [
|
|
525
|
+
{
|
|
526
|
+
path: '/abc/:id/:order',
|
|
527
|
+
method: HttpMethod.POST,
|
|
528
|
+
pathSchema: QuerySchema,
|
|
529
|
+
handler: async () => {
|
|
530
|
+
return Response.SimpleResponse({ name: 'abc' })
|
|
531
|
+
},
|
|
532
|
+
},
|
|
533
|
+
],
|
|
534
|
+
})
|
|
535
|
+
await server.start()
|
|
536
|
+
// Validation fails, empty body
|
|
537
|
+
await request(defaultUrl)
|
|
538
|
+
.post(`/abc/name/123`)
|
|
539
|
+
.expect('Content-Type', 'application/json; charset=utf-8')
|
|
540
|
+
.expect(200)
|
|
541
|
+
await server.stop()
|
|
542
|
+
})
|
|
543
|
+
})
|
|
544
|
+
|
|
327
545
|
export {}
|