@martel/calyx 1.12.0 → 1.13.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/CHANGELOG.md +15 -0
- package/README.md +5 -3
- package/benchmarks/graphql-benchmark.ts +4 -1
- package/benchmarks/serialization-benchmark.ts +46 -6
- package/benchmarks/techniques-benchmark.ts +12 -0
- package/benchmarks/validation-benchmark.ts +8 -1
- package/docs/controllers.md +3 -3
- package/docs/dependency-injection.md +3 -3
- package/docs/lifecycle.md +3 -3
- package/package.json +1 -1
- package/src/cli/index.ts +7 -1
- package/src/config/config.module.ts +16 -2
- package/src/config/config.service.ts +20 -6
- package/src/cookies/cookies.ts +45 -8
- package/src/core/container.ts +340 -154
- package/src/core/testing-module.ts +4 -0
- package/src/cqrs/cqrs.ts +93 -4
- package/src/database/sequelize.module.ts +239 -0
- package/src/event-emitter/decorators.ts +2 -2
- package/src/event-emitter/event-emitter.ts +3 -0
- package/src/graphql/graphql.module.ts +2 -4
- package/src/http/application.ts +140 -10
- package/src/http/decorators.ts +21 -1
- package/src/http/exceptions.ts +97 -0
- package/src/http/factory.ts +3 -0
- package/src/http/router.ts +27 -4
- package/src/index.ts +1 -0
- package/src/microservices/exceptions.ts +10 -0
- package/src/microservices/index.ts +1 -0
- package/src/queue/queue.module.ts +73 -5
- package/src/terminus/terminus.ts +75 -2
- package/src/validation/compiler.ts +137 -17
- package/src/validation/decorators.ts +164 -2
- package/src/websockets/exceptions.ts +10 -0
- package/src/websockets/index.ts +1 -0
- package/tests/circular-di.test.ts +151 -0
- package/tests/di.test.ts +10 -2
- package/tests/nestjs-parity.test.ts +255 -0
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { expect, test, describe } from 'bun:test';
|
|
2
|
+
import { filter, map } from 'rxjs/operators';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
2
4
|
import {
|
|
3
5
|
Test,
|
|
4
6
|
LazyModuleLoader,
|
|
@@ -30,6 +32,28 @@ import {
|
|
|
30
32
|
ICommandHandler,
|
|
31
33
|
IQueryHandler,
|
|
32
34
|
IEventHandler,
|
|
35
|
+
ConflictException,
|
|
36
|
+
ServiceUnavailableException,
|
|
37
|
+
UnprocessableEntityException,
|
|
38
|
+
WsException,
|
|
39
|
+
RpcException,
|
|
40
|
+
IsUUID,
|
|
41
|
+
Min,
|
|
42
|
+
Max,
|
|
43
|
+
IsBoolean,
|
|
44
|
+
Length,
|
|
45
|
+
ValidationCompiler,
|
|
46
|
+
registerAs,
|
|
47
|
+
ConfigModule,
|
|
48
|
+
SequelizeModule,
|
|
49
|
+
SequelizeModel,
|
|
50
|
+
BullModule,
|
|
51
|
+
UnhandledExceptionBus,
|
|
52
|
+
EventPublisher,
|
|
53
|
+
AggregateRoot,
|
|
54
|
+
Saga,
|
|
55
|
+
MemoryHealthIndicator,
|
|
56
|
+
TypeOrmHealthIndicator,
|
|
33
57
|
} from '../src/index.ts';
|
|
34
58
|
|
|
35
59
|
@Injectable()
|
|
@@ -269,4 +293,235 @@ describe('NestJS Parity Extensions', () => {
|
|
|
269
293
|
|
|
270
294
|
await moduleRef.close();
|
|
271
295
|
});
|
|
296
|
+
|
|
297
|
+
test('HTTP Exceptions: check new status codes', () => {
|
|
298
|
+
const exc1 = new ConflictException();
|
|
299
|
+
expect(exc1.getStatus()).toBe(409);
|
|
300
|
+
|
|
301
|
+
const exc2 = new ServiceUnavailableException();
|
|
302
|
+
expect(exc2.getStatus()).toBe(503);
|
|
303
|
+
|
|
304
|
+
const exc3 = new UnprocessableEntityException();
|
|
305
|
+
expect(exc3.getStatus()).toBe(422);
|
|
306
|
+
|
|
307
|
+
const wsExc = new WsException('error');
|
|
308
|
+
expect(wsExc.getError()).toBe('error');
|
|
309
|
+
|
|
310
|
+
const rpcExc = new RpcException('error');
|
|
311
|
+
expect(rpcExc.getError()).toBe('error');
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test('JIT Validation: check advanced decorators', () => {
|
|
315
|
+
class DTO {
|
|
316
|
+
@IsUUID()
|
|
317
|
+
uuid!: string;
|
|
318
|
+
|
|
319
|
+
@Min(10)
|
|
320
|
+
minVal!: number;
|
|
321
|
+
|
|
322
|
+
@Max(50)
|
|
323
|
+
maxVal!: number;
|
|
324
|
+
|
|
325
|
+
@IsBoolean()
|
|
326
|
+
boolVal!: boolean;
|
|
327
|
+
|
|
328
|
+
@Length(3, 8)
|
|
329
|
+
strLen!: string;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const validate = ValidationCompiler.compile(DTO);
|
|
333
|
+
|
|
334
|
+
const errs1 = validate({
|
|
335
|
+
uuid: 'abc',
|
|
336
|
+
minVal: 5,
|
|
337
|
+
maxVal: 60,
|
|
338
|
+
boolVal: 'not-bool' as any,
|
|
339
|
+
strLen: 'hi',
|
|
340
|
+
});
|
|
341
|
+
expect(errs1).not.toBeNull();
|
|
342
|
+
expect(errs1!.length).toBe(5);
|
|
343
|
+
|
|
344
|
+
const errs2 = validate({
|
|
345
|
+
uuid: '123e4567-e89b-12d3-a456-426614174000',
|
|
346
|
+
minVal: 15,
|
|
347
|
+
maxVal: 40,
|
|
348
|
+
boolVal: true,
|
|
349
|
+
strLen: 'hello',
|
|
350
|
+
});
|
|
351
|
+
expect(errs2).toBeNull();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test('ConfigNamespaces: registerAs and dot-notation', () => {
|
|
355
|
+
const dbConfig = registerAs('database', () => ({
|
|
356
|
+
host: 'my-host',
|
|
357
|
+
port: 5432,
|
|
358
|
+
}));
|
|
359
|
+
|
|
360
|
+
const module = ConfigModule.forRoot({
|
|
361
|
+
load: [dbConfig],
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
const service = (module.providers[0] as any).useValue;
|
|
365
|
+
expect(service.get('database.host')).toBe('my-host');
|
|
366
|
+
expect(service.get('database.port')).toBe(5432);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
test('SequelizeModule: check database fallback model', async () => {
|
|
370
|
+
class User extends SequelizeModel {
|
|
371
|
+
name!: string;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const moduleRef = await Test.createTestingModule({
|
|
375
|
+
imports: [
|
|
376
|
+
SequelizeModule.forRoot({ storage: ':memory:' }),
|
|
377
|
+
SequelizeModule.forFeature([User]),
|
|
378
|
+
],
|
|
379
|
+
}).compile();
|
|
380
|
+
|
|
381
|
+
const injectedModel = moduleRef.get('Sequelize_Model_User') as any;
|
|
382
|
+
expect(injectedModel).toBeDefined();
|
|
383
|
+
|
|
384
|
+
const user = await User.create({ name: 'Alice' });
|
|
385
|
+
expect(user.id).toBeDefined();
|
|
386
|
+
expect(user.name).toBe('Alice');
|
|
387
|
+
|
|
388
|
+
const found = await User.findByPk(user.id);
|
|
389
|
+
expect(found).not.toBeNull();
|
|
390
|
+
expect(found.name).toBe('Alice');
|
|
391
|
+
|
|
392
|
+
await user.update({ name: 'Bob' });
|
|
393
|
+
const found2 = await User.findByPk(user.id);
|
|
394
|
+
expect(found2.name).toBe('Bob');
|
|
395
|
+
|
|
396
|
+
await user.destroy();
|
|
397
|
+
const found3 = await User.findByPk(user.id);
|
|
398
|
+
expect(found3).toBeNull();
|
|
399
|
+
|
|
400
|
+
await moduleRef.close();
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test('BullModule: check root registry and compatibility methods', async () => {
|
|
404
|
+
const moduleRef = await Test.createTestingModule({
|
|
405
|
+
imports: [
|
|
406
|
+
BullModule.forRoot({}),
|
|
407
|
+
BullModule.registerQueue({ name: 'audio' }),
|
|
408
|
+
],
|
|
409
|
+
}).compile();
|
|
410
|
+
|
|
411
|
+
const queue = moduleRef.get('Queue_audio') as any;
|
|
412
|
+
expect(queue).toBeDefined();
|
|
413
|
+
expect(queue.pause).toBeDefined();
|
|
414
|
+
expect(queue.resume).toBeDefined();
|
|
415
|
+
|
|
416
|
+
await queue.pause();
|
|
417
|
+
expect(await queue.isPaused()).toBe(true);
|
|
418
|
+
await queue.resume();
|
|
419
|
+
expect(await queue.isPaused()).toBe(false);
|
|
420
|
+
|
|
421
|
+
const job = await queue.add('test-job', { foo: 'bar' });
|
|
422
|
+
expect(job.id).toBeDefined();
|
|
423
|
+
expect(await queue.count()).toBe(1);
|
|
424
|
+
|
|
425
|
+
await queue.drain();
|
|
426
|
+
expect(await queue.count()).toBe(0);
|
|
427
|
+
|
|
428
|
+
await moduleRef.close();
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
test('CQRS Saga & AggregateRoot & ExceptionBus', async () => {
|
|
432
|
+
class MyCommand implements ICommand {
|
|
433
|
+
constructor(public readonly val: string) {}
|
|
434
|
+
}
|
|
435
|
+
class MyEvent implements IEvent {
|
|
436
|
+
constructor(public readonly val: string) {}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
let commandVal = '';
|
|
440
|
+
|
|
441
|
+
@CommandHandler(MyCommand)
|
|
442
|
+
class MyCommandHandler implements ICommandHandler<MyCommand> {
|
|
443
|
+
async execute(command: MyCommand) {
|
|
444
|
+
commandVal = command.val;
|
|
445
|
+
if (command.val === 'throw') {
|
|
446
|
+
throw new Error('command-failed');
|
|
447
|
+
}
|
|
448
|
+
return 'ok';
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
class OrderAggregate extends AggregateRoot {
|
|
453
|
+
createOrder(val: string) {
|
|
454
|
+
this.apply(new MyEvent(val));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
@Injectable()
|
|
459
|
+
class OrderSagas {
|
|
460
|
+
@Saga()
|
|
461
|
+
orderCreated = (events$: Observable<any>): Observable<ICommand> => {
|
|
462
|
+
return events$.pipe(
|
|
463
|
+
filter((e: any) => e instanceof MyEvent),
|
|
464
|
+
map((e: any) => new MyCommand(e.val))
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const moduleRef = await Test.createTestingModule({
|
|
470
|
+
imports: [CqrsModule],
|
|
471
|
+
providers: [MyCommandHandler, OrderSagas],
|
|
472
|
+
}).compile();
|
|
473
|
+
|
|
474
|
+
const eventBus = moduleRef.get(EventBus);
|
|
475
|
+
const exceptionBus = moduleRef.get(UnhandledExceptionBus);
|
|
476
|
+
const commandBus = moduleRef.get(CommandBus);
|
|
477
|
+
|
|
478
|
+
const cqrs = moduleRef.get(CqrsModule);
|
|
479
|
+
(cqrs as any).onModuleInit();
|
|
480
|
+
|
|
481
|
+
const agg = new OrderAggregate();
|
|
482
|
+
agg.createOrder('hello-saga');
|
|
483
|
+
expect(agg.getUncommittedEvents().length).toBe(1);
|
|
484
|
+
|
|
485
|
+
const publisher = moduleRef.get(EventPublisher);
|
|
486
|
+
const boundAgg = publisher.mergeObjectContext(agg);
|
|
487
|
+
boundAgg.commit();
|
|
488
|
+
expect(agg.getUncommittedEvents().length).toBe(0);
|
|
489
|
+
|
|
490
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
491
|
+
expect(commandVal).toBe('hello-saga');
|
|
492
|
+
|
|
493
|
+
let caughtErr: any = null;
|
|
494
|
+
exceptionBus.subscribe((err) => {
|
|
495
|
+
caughtErr = err;
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
try {
|
|
499
|
+
await commandBus.execute(new MyCommand('throw'));
|
|
500
|
+
} catch {
|
|
501
|
+
// expected
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
expect(caughtErr).not.toBeNull();
|
|
505
|
+
expect(caughtErr.error.message).toBe('command-failed');
|
|
506
|
+
|
|
507
|
+
await moduleRef.close();
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
test('Terminus health check indicators', async () => {
|
|
511
|
+
const moduleRef = await Test.createTestingModule({
|
|
512
|
+
imports: [TerminusModule],
|
|
513
|
+
}).compile();
|
|
514
|
+
|
|
515
|
+
const memIndicator = moduleRef.get(MemoryHealthIndicator);
|
|
516
|
+
const dbIndicator = moduleRef.get(TypeOrmHealthIndicator);
|
|
517
|
+
|
|
518
|
+
const memRes = await memIndicator.checkHeap('heap', 1000 * 1024 * 1024);
|
|
519
|
+
expect(memRes.heap.status).toBe('up');
|
|
520
|
+
|
|
521
|
+
const dbRes = await dbIndicator.pingCheck('db');
|
|
522
|
+
expect(dbRes.db.status).toBe('up');
|
|
523
|
+
|
|
524
|
+
await moduleRef.close();
|
|
525
|
+
});
|
|
272
526
|
});
|
|
527
|
+
|