@schorts/shared-kernel 4.2.0 → 4.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 (175) hide show
  1. package/dist/cjs/value-objects/array-value.js +2 -0
  2. package/dist/cjs/value-objects/array-value.js.map +1 -1
  3. package/dist/cjs/value-objects/index.js +3 -1
  4. package/dist/cjs/value-objects/index.js.map +1 -1
  5. package/dist/cjs/value-objects/object-value.js +2 -0
  6. package/dist/cjs/value-objects/object-value.js.map +1 -1
  7. package/dist/cjs/value-objects/url-value.js +23 -0
  8. package/dist/cjs/value-objects/url-value.js.map +1 -0
  9. package/dist/esm/value-objects/array-value.js +2 -0
  10. package/dist/esm/value-objects/array-value.js.map +1 -1
  11. package/dist/esm/value-objects/index.js +3 -1
  12. package/dist/esm/value-objects/index.js.map +1 -1
  13. package/dist/esm/value-objects/object-value.js +2 -0
  14. package/dist/esm/value-objects/object-value.js.map +1 -1
  15. package/dist/esm/value-objects/url-value.js +23 -0
  16. package/dist/esm/value-objects/url-value.js.map +1 -0
  17. package/dist/types/pub-sub/subscription.d.ts +0 -1
  18. package/dist/types/pub-sub/subscription.d.ts.map +1 -1
  19. package/dist/types/telemetry/telemetry.d.ts +0 -1
  20. package/dist/types/telemetry/telemetry.d.ts.map +1 -1
  21. package/dist/types/value-objects/array-value.d.ts +2 -0
  22. package/dist/types/value-objects/array-value.d.ts.map +1 -1
  23. package/dist/types/value-objects/index.d.ts +1 -0
  24. package/dist/types/value-objects/index.d.ts.map +1 -1
  25. package/dist/types/value-objects/object-value.d.ts +2 -0
  26. package/dist/types/value-objects/object-value.d.ts.map +1 -1
  27. package/dist/types/value-objects/url-value.d.ts +11 -0
  28. package/dist/types/value-objects/url-value.d.ts.map +1 -0
  29. package/package.json +11 -3
  30. package/.idx/airules.md +0 -186
  31. package/.idx/dev.nix +0 -54
  32. package/.vscode/settings.json +0 -7
  33. package/CHANGELOG +0 -357
  34. package/__tests__/auth/auth-provider.test.ts +0 -50
  35. package/__tests__/auth/require-auth.decorator.test.ts +0 -88
  36. package/__tests__/criteria/criteria.test.ts +0 -159
  37. package/__tests__/criteria/direction.test.ts +0 -11
  38. package/__tests__/criteria/filter-criterion.test.ts +0 -15
  39. package/__tests__/criteria/operator.test.ts +0 -22
  40. package/__tests__/criteria/order.test.ts +0 -14
  41. package/__tests__/dao/dao.test.ts +0 -126
  42. package/__tests__/domain-events/domain-event-primitives.test.ts +0 -34
  43. package/__tests__/domain-events/domain-event.test.ts +0 -50
  44. package/__tests__/entities/entity-registry.test.ts +0 -71
  45. package/__tests__/entities/entity.test.ts +0 -114
  46. package/__tests__/entities/register-entity.decorator.test.ts +0 -54
  47. package/__tests__/formatters/pascal-camel-to-snake.test.ts +0 -19
  48. package/__tests__/http/fetch-http-provider.test.ts +0 -155
  49. package/__tests__/http/http-provider.test.ts +0 -55
  50. package/__tests__/json-api/json-api-connector.test.ts +0 -78
  51. package/__tests__/json-api/json-api-list.test.ts +0 -24
  52. package/__tests__/json-api/json-api-single.test.ts +0 -24
  53. package/__tests__/json-api/url-criteria-builder.test.ts +0 -74
  54. package/__tests__/messages/message.test.ts +0 -16
  55. package/__tests__/models/base-model.test.ts +0 -10
  56. package/__tests__/state-manager/state-manager.test.ts +0 -101
  57. package/__tests__/unit-of-work/unit-of-work.test.ts +0 -47
  58. package/__tests__/utils/url/url-with-params-builder.test.ts +0 -39
  59. package/__tests__/value-objects/coordinates-value.test.ts +0 -68
  60. package/__tests__/value-objects/email-value.test.ts +0 -43
  61. package/__tests__/value-objects/enum-value.test.ts +0 -51
  62. package/__tests__/value-objects/integer-value.test.ts +0 -115
  63. package/__tests__/value-objects/phone-value.test.ts +0 -82
  64. package/__tests__/value-objects/slug-value.test.ts +0 -43
  65. package/__tests__/value-objects/string-value.test.ts +0 -121
  66. package/__tests__/value-objects/uuid-value.test.ts +0 -67
  67. package/__tests__/value-objects/value-object.test.ts +0 -25
  68. package/jest.config.js +0 -25
  69. package/src/abac/index.ts +0 -1
  70. package/src/abac/predicate.ts +0 -3
  71. package/src/auth/auth-provider.ts +0 -11
  72. package/src/auth/exceptions/index.ts +0 -2
  73. package/src/auth/exceptions/not-authenticated.ts +0 -11
  74. package/src/auth/exceptions/require-auth-not-implemented-correctly.ts +0 -11
  75. package/src/auth/index.ts +0 -3
  76. package/src/auth/require-auth.decorator.ts +0 -42
  77. package/src/converters/index.ts +0 -1
  78. package/src/converters/remote-file-to-base64.ts +0 -19
  79. package/src/criteria/criteria.ts +0 -46
  80. package/src/criteria/direction.ts +0 -1
  81. package/src/criteria/exceptions/index.ts +0 -2
  82. package/src/criteria/exceptions/limit-not-valid.ts +0 -11
  83. package/src/criteria/exceptions/offset-not-valid.ts +0 -11
  84. package/src/criteria/index.ts +0 -5
  85. package/src/criteria/operator.ts +0 -12
  86. package/src/criteria/order.ts +0 -4
  87. package/src/dao/dao.ts +0 -16
  88. package/src/dao/index.ts +0 -1
  89. package/src/domain-events/domain-event-primitives.ts +0 -10
  90. package/src/domain-events/domain-event-registry.ts +0 -40
  91. package/src/domain-events/domain-event.ts +0 -29
  92. package/src/domain-events/exceptions/domain-event-not-registered.ts +0 -11
  93. package/src/domain-events/exceptions/index.ts +0 -1
  94. package/src/domain-events/index.ts +0 -4
  95. package/src/entities/entity-registry.ts +0 -30
  96. package/src/entities/entity.ts +0 -25
  97. package/src/entities/exceptions/entity-not-registered.ts +0 -11
  98. package/src/entities/exceptions/index.ts +0 -1
  99. package/src/entities/index.ts +0 -4
  100. package/src/entities/register-entity.decorator.ts +0 -16
  101. package/src/event-bus/event-store.ts +0 -9
  102. package/src/event-bus/event-subscriber.ts +0 -5
  103. package/src/event-bus/in-memory/async-in-memory-event-bus.ts +0 -67
  104. package/src/event-bus/in-memory/in-memory-event-store.ts +0 -28
  105. package/src/event-bus/in-memory/index.ts +0 -2
  106. package/src/event-bus/index.ts +0 -3
  107. package/src/formatters/index.ts +0 -1
  108. package/src/formatters/pascal-camel-to-snake.ts +0 -8
  109. package/src/http/exceptions/http-exception.ts +0 -8
  110. package/src/http/exceptions/index.ts +0 -1
  111. package/src/http/fetch-http-provider.ts +0 -151
  112. package/src/http/http-interceptor.ts +0 -3
  113. package/src/http/http-provider.ts +0 -7
  114. package/src/http/index.ts +0 -5
  115. package/src/i18n/index.ts +0 -2
  116. package/src/i18n/shared-kernel-translation-keys.ts +0 -19
  117. package/src/i18n/translation-resolver.ts +0 -3
  118. package/src/index.ts +0 -21
  119. package/src/json-api/exceptions/index.ts +0 -1
  120. package/src/json-api/exceptions/json-api-errors.ts +0 -17
  121. package/src/json-api/index.ts +0 -5
  122. package/src/json-api/json-api-connector.ts +0 -97
  123. package/src/json-api/json-api-list.ts +0 -13
  124. package/src/json-api/json-api-single.ts +0 -13
  125. package/src/json-api/url-criteria-builder.ts +0 -49
  126. package/src/messages/index.ts +0 -1
  127. package/src/messages/message.ts +0 -3
  128. package/src/models/base-model.ts +0 -3
  129. package/src/models/index.ts +0 -1
  130. package/src/pub-sub/index.ts +0 -2
  131. package/src/pub-sub/publisher.ts +0 -3
  132. package/src/pub-sub/subscription.ts +0 -7
  133. package/src/rbac/base-resource.ts +0 -3
  134. package/src/rbac/exceptions/index.ts +0 -1
  135. package/src/rbac/exceptions/not-authorized.ts +0 -1
  136. package/src/rbac/index.ts +0 -5
  137. package/src/rbac/permission.ts +0 -4
  138. package/src/rbac/rbac-policy.ts +0 -41
  139. package/src/result/index.ts +0 -1
  140. package/src/result/result.ts +0 -35
  141. package/src/state-manager/in-memory-state-manager.ts +0 -22
  142. package/src/state-manager/index.ts +0 -3
  143. package/src/state-manager/session-storage-state-manager.ts +0 -47
  144. package/src/state-manager/state-manager.ts +0 -28
  145. package/src/telemetry/console-telemetry.ts +0 -27
  146. package/src/telemetry/index.ts +0 -2
  147. package/src/telemetry/telemetry.ts +0 -6
  148. package/src/unit-of-work/index.ts +0 -1
  149. package/src/unit-of-work/unit-of-work.ts +0 -5
  150. package/src/utils/index.ts +0 -2
  151. package/src/utils/url/index.ts +0 -1
  152. package/src/utils/url/url-with-params-builder.ts +0 -19
  153. package/src/utils/value-objects/index.ts +0 -1
  154. package/src/utils/value-objects/rule.ts +0 -3
  155. package/src/value-objects/array-value.ts +0 -92
  156. package/src/value-objects/boolean-value.ts +0 -23
  157. package/src/value-objects/coordinates-value.ts +0 -50
  158. package/src/value-objects/date-value.ts +0 -36
  159. package/src/value-objects/email-value.ts +0 -25
  160. package/src/value-objects/enum-value.ts +0 -27
  161. package/src/value-objects/exceptions/index.ts +0 -2
  162. package/src/value-objects/exceptions/value-not-valid.ts +0 -3
  163. package/src/value-objects/exceptions/values-not-valid.ts +0 -11
  164. package/src/value-objects/float-valuet.ts +0 -39
  165. package/src/value-objects/index.ts +0 -17
  166. package/src/value-objects/integer-value.ts +0 -29
  167. package/src/value-objects/object-value.ts +0 -84
  168. package/src/value-objects/phone-value.ts +0 -53
  169. package/src/value-objects/slug-value.ts +0 -25
  170. package/src/value-objects/string-value.ts +0 -27
  171. package/src/value-objects/uuid-value.ts +0 -34
  172. package/src/value-objects/value-object.ts +0 -7
  173. package/tsconfig.cjs.json +0 -13
  174. package/tsconfig.esm.json +0 -12
  175. package/tsconfig.json +0 -47
@@ -1,14 +0,0 @@
1
- import { expectTypeOf } from "expect-type";
2
-
3
- import { Order } from "../../src/criteria";
4
-
5
- type ExpectedOrder = {
6
- field: string;
7
- direction: "ASC" | "DESC" | "NONE";
8
- }
9
-
10
- describe('Order', () => {
11
- it('should match the expected type', () => {
12
- expectTypeOf<Order>().toEqualTypeOf<ExpectedOrder>();
13
- });
14
- });
@@ -1,126 +0,0 @@
1
- import { expectTypeOf } from "expect-type";
2
-
3
- import { DAO } from "../../src/dao";
4
- import { Entity } from "../../src/entities";
5
- import { Criteria } from "../../src/criteria";
6
- import { UUIDValue } from "../../src/value-objects";
7
- import { UnitOfWork } from "../../src/unit-of-work";
8
-
9
- type Model = {
10
- id: string;
11
- name: string;
12
- };
13
-
14
- class IDValue extends UUIDValue {
15
- attributeName = "ID";
16
- }
17
-
18
- class ModelEntity extends Entity<IDValue, Model> {
19
- constructor(id: IDValue) {
20
- super(id);
21
- }
22
-
23
- toPrimitives(): Model {
24
- throw new Error("Method not implemented.");
25
- }
26
- }
27
-
28
- describe("DAO", () => {
29
- describe('#getAll', () => {
30
- it('should declare the method', () => {
31
- expectTypeOf<DAO<Model, ModelEntity>>().toHaveProperty("getAll");
32
- });
33
-
34
- it('should no require any arguments', () => {
35
- expectTypeOf<DAO<Model, ModelEntity>['getAll']>().parameters.toEqualTypeOf<[]>();
36
- });
37
-
38
- it('should return a promise with the Entity', () => {
39
- expectTypeOf<DAO<Model, ModelEntity>['getAll']>().returns.toEqualTypeOf<Promise<ModelEntity[]>>();
40
- });
41
- });
42
-
43
- describe('#findByID', () => {
44
- it('should declare the method', () => {
45
- expectTypeOf<DAO<Model, ModelEntity>>().toHaveProperty("findByID");
46
- });
47
-
48
- it('should require the ID argument', () => {
49
- expectTypeOf<DAO<Model, ModelEntity>['findByID']>().parameters.toEqualTypeOf<[ModelEntity["id"]["value"]]>();
50
- });
51
-
52
- it('should return a promise with the Entity or null', () => {
53
- expectTypeOf<DAO<Model, ModelEntity>['findByID']>().returns.toEqualTypeOf<Promise<ModelEntity | null>>();
54
- });
55
- });
56
-
57
- describe('#findOneBy', () => {
58
- it('should declare the method', () => {
59
- expectTypeOf<DAO<Model, ModelEntity>>().toHaveProperty("findOneBy");
60
- });
61
-
62
- it('should receive the criteria', () => {
63
- expectTypeOf<DAO<Model, ModelEntity>['findOneBy']>().parameters.toEqualTypeOf<[Criteria]>();
64
- });
65
-
66
- it('should return a promise with the Entity or null', () => {
67
- expectTypeOf<DAO<Model, ModelEntity>['findOneBy']>().returns.toEqualTypeOf<Promise<ModelEntity | null>>();
68
- });
69
- });
70
-
71
- describe('#search', () => {
72
- it('should declare the method', () => {
73
- expectTypeOf<DAO<Model, ModelEntity>>().toHaveProperty("search");
74
- });
75
-
76
- it('should receive the criteria argument', () => {
77
- expectTypeOf<DAO<Model, ModelEntity>['search']>().parameters.toEqualTypeOf<[Criteria]>();
78
- });
79
-
80
- it('should return a promise with an array of entities', () => {
81
- expectTypeOf<DAO<Model, ModelEntity>['search']>().returns.toEqualTypeOf<Promise<ModelEntity[]>>();
82
- });
83
- });
84
-
85
- describe('#create', () => {
86
- it('should declare the method', () => {
87
- expectTypeOf<DAO<Model, ModelEntity>>().toHaveProperty("create");
88
- });
89
-
90
- it('should receive the entity and can receive a UnitOfWork', () => {
91
- expectTypeOf<DAO<Model, ModelEntity>['create']>().parameters.toEqualTypeOf<[ModelEntity, UnitOfWork?]>();
92
- });
93
-
94
- it('should return a promise with the created entity', () => {
95
- expectTypeOf<DAO<Model, ModelEntity>['create']>().returns.toEqualTypeOf<Promise<ModelEntity>>();
96
- });
97
- });
98
-
99
- describe('#update', () => {
100
- it('should declare the method', () => {
101
- expectTypeOf<DAO<Model, ModelEntity>>().toHaveProperty("update");
102
- });
103
-
104
- it('should receive the entity and can receive a UnitOfWork', () => {
105
- expectTypeOf<DAO<Model, ModelEntity>['update']>().parameters.toEqualTypeOf<[ModelEntity, UnitOfWork?]>();
106
- });
107
-
108
- it('should return a promise with the updated entity', () => {
109
- expectTypeOf<DAO<Model, ModelEntity>['update']>().returns.toEqualTypeOf<Promise<ModelEntity>>();
110
- });
111
- });
112
-
113
- describe('#delete', () => {
114
- it('should declare the method', () => {
115
- expectTypeOf<DAO<Model, ModelEntity>>().toHaveProperty("delete");
116
- });
117
-
118
- it('should receive the entity and can receive a UnitOfWork', () => {
119
- expectTypeOf<DAO<Model, ModelEntity>['delete']>().parameters.toEqualTypeOf<[ModelEntity, UnitOfWork?]>();
120
- });
121
-
122
- it('should return a promise with the deleted entity', () => {
123
- expectTypeOf<DAO<Model, ModelEntity>['delete']>().returns.toEqualTypeOf<Promise<ModelEntity>>();
124
- });
125
- });
126
- });
@@ -1,34 +0,0 @@
1
- import { expectTypeOf } from "expect-type";
2
-
3
- import { DomainEventPrimitives } from "../../src/domain-events";
4
-
5
- describe("DomainEventPrimitives", () => {
6
- it('should have a "id" property of type string', () => {
7
- expectTypeOf<DomainEventPrimitives>().toHaveProperty('id');
8
- expectTypeOf<DomainEventPrimitives['id']>().toBeString();
9
- });
10
-
11
- it('should have a "occurred_at" property of type string', () => {
12
- expectTypeOf<DomainEventPrimitives>().toHaveProperty('occurred_at');
13
- expectTypeOf<DomainEventPrimitives['occurred_at']>().toBeString();
14
- });
15
-
16
- it('should have a "type" property of type string', () => {
17
- expectTypeOf<DomainEventPrimitives>().toHaveProperty('type');
18
- expectTypeOf<DomainEventPrimitives['type']>().toBeString();
19
- });
20
-
21
- it('should have a "version" property of type number', () => {
22
- expectTypeOf<DomainEventPrimitives>().toHaveProperty('version');
23
- expectTypeOf<DomainEventPrimitives['version']>().toBeNumber();
24
- });
25
-
26
- it('should have a "payload" property of type number', () => {
27
- type PayloadSchema = {
28
- name: string;
29
- };
30
-
31
- expectTypeOf<DomainEventPrimitives<PayloadSchema>>().toHaveProperty('payload');
32
- expectTypeOf<DomainEventPrimitives<PayloadSchema>['payload']>().toEqualTypeOf<PayloadSchema>();
33
- });
34
- });
@@ -1,50 +0,0 @@
1
- import { expectTypeOf } from "expect-type";
2
-
3
- import { DomainEvent, DomainEventPrimitives } from "../../src/domain-events";
4
-
5
- describe('DomainEvent', () => {
6
- it('should have a "id" property of type string', () => {
7
- expectTypeOf<DomainEvent>().toHaveProperty("id");
8
- expectTypeOf<DomainEvent['id']>().toBeString();
9
- });
10
-
11
- it('should have a "occurredAt" property of type Date', () => {
12
- expectTypeOf<DomainEvent>().toHaveProperty("occurredAt");
13
- expectTypeOf<DomainEvent['occurredAt']>().toEqualTypeOf<Date>();
14
- });
15
-
16
- it('should have a "type" property of type string', () => {
17
- expectTypeOf<DomainEvent>().toHaveProperty("type");
18
- expectTypeOf<DomainEvent['type']>().toBeString();
19
- });
20
-
21
- it('should have a "version" property of type number', () => {
22
- expectTypeOf<DomainEvent>().toHaveProperty("version");
23
- expectTypeOf<DomainEvent['version']>().toBeNumber();
24
- });
25
-
26
- it('should have a "payload" property of customt type', () => {
27
- type PayloadSchema = {
28
- name: string;
29
- };
30
-
31
- expectTypeOf<DomainEvent<PayloadSchema>>().toHaveProperty("payload");
32
- expectTypeOf<DomainEvent<PayloadSchema>['payload']>().toEqualTypeOf<PayloadSchema>();
33
- });
34
-
35
- it('should have a "getEventName" method that returns an string', () => {
36
- expectTypeOf<DomainEvent>().toHaveProperty('getEventName');
37
- expectTypeOf<DomainEvent['getEventName']>().toBeFunction();
38
- expectTypeOf<DomainEvent['getEventName']>().returns.toBeString();
39
- });
40
-
41
- it('should declare a "toPrimitives" method', () => {
42
- type PayloadSchema = {
43
- name: string;
44
- };
45
-
46
- expectTypeOf<DomainEvent>().toHaveProperty('toPrimitives');
47
- expectTypeOf<DomainEvent['toPrimitives']>().toBeFunction();
48
- expectTypeOf<DomainEvent<PayloadSchema>['toPrimitives']>().returns.toEqualTypeOf<DomainEventPrimitives<PayloadSchema>>();
49
- });
50
- });
@@ -1,71 +0,0 @@
1
- import { EntityRegistry, Entity, EntityNotRegistered } from "../../src/entities";
2
- import { UUIDValue } from "../../src/value-objects";
3
-
4
- type MockModel = {
5
- id: string;
6
- };
7
-
8
- class IDValue extends UUIDValue {
9
- attributeName = 'ID';
10
- }
11
-
12
- class MockEntity extends Entity<IDValue, MockModel> {
13
- constructor(id: IDValue) {
14
- super(id);
15
- }
16
-
17
- toPrimitives(): MockModel {
18
- return {
19
- id: this.id.value,
20
- };
21
- }
22
-
23
- static fromPrimitives<MockModel>(model: MockModel): MockEntity {
24
- return new MockEntity(
25
- new IDValue(model["id"]),
26
- );
27
- }
28
- }
29
-
30
- describe("EntityRegistry", () => {
31
- const type = "mock";
32
-
33
- beforeEach(() => {
34
- (EntityRegistry as any).registry.clear();
35
- });
36
-
37
- it("registers an entity constructor", () => {
38
- EntityRegistry.register(type, MockEntity);
39
-
40
- const resolved = EntityRegistry.resolve(type);
41
-
42
- expect(resolved).toBe(MockEntity);
43
- });
44
-
45
- it("resolves a registered entity constructor", () => {
46
- EntityRegistry.register(type, MockEntity);
47
-
48
- const resolved = EntityRegistry.resolve(type);
49
-
50
- expect(resolved?.fromPrimitives).toBeDefined();
51
- });
52
-
53
- it("returns null for unregistered types", () => {
54
- const resolved = EntityRegistry.resolve("unknown");
55
-
56
- expect(resolved).toBeNull();
57
- });
58
-
59
- it("creates an entity from a registered constructor", () => {
60
- EntityRegistry.register(type, MockEntity);
61
-
62
- const entity = EntityRegistry.create(type, { id: '123', name: 'Jorge' });
63
-
64
- expect(entity).toBeInstanceOf(MockEntity);
65
- expect(entity.id.value).toEqual('123');
66
- });
67
-
68
- it("throws EntityNotRegistered for unknown type", () => {
69
- expect(() => EntityRegistry.create("unknown", { id: '123' })).toThrow(EntityNotRegistered);
70
- });
71
- });
@@ -1,114 +0,0 @@
1
- import { expectTypeOf } from "expect-type";
2
-
3
- import { Entity } from "../../src/entities";
4
- import { ValueObject, UUIDValue } from "../../src/value-objects";
5
- import { DomainEvent, DomainEventPrimitives } from "../../src/domain-events";
6
-
7
- type Model = {
8
- id: string;
9
- name: string;
10
- };
11
-
12
- class TestDomainEvent extends DomainEvent {
13
- static eventName = "shared.v1.tests.test_domain";
14
-
15
- toPrimitives(): DomainEventPrimitives<{}> {
16
- return {
17
- id: this.id,
18
- occurred_at: this.occurredAt.toString(),
19
- payload: this.payload,
20
- type: this.type,
21
- version: this.version,
22
- };
23
- }
24
-
25
- getEventName() {
26
- return TestDomainEvent.eventName;
27
- }
28
- }
29
-
30
- class IDValue extends UUIDValue {
31
- readonly attributeName = "ID";
32
- }
33
-
34
- class TestEntity extends Entity<IDValue, Model> {
35
- toPrimitives(): Model {
36
- return {
37
- id: this.id.toString(),
38
- name: "test",
39
- };
40
- }
41
- }
42
-
43
- describe('Entity', () => {
44
- it('should have a "id" property of type string', () => {
45
- expectTypeOf<Entity<IDValue, Model>>().toHaveProperty("id");
46
- expectTypeOf<Entity<IDValue, Model>["id"]>().toEqualTypeOf<IDValue>();
47
- });
48
-
49
- it('should have a private "domainEvents" property of type Array<DomainEvent>', () => {
50
- expectTypeOf<Entity<IDValue, Model>["domainEvents"]>().toEqualTypeOf<DomainEvent[]>();
51
- });
52
-
53
- describe('#pullDomainEvents', () => {
54
- let testEntity: TestEntity;
55
-
56
- beforeEach(() => {
57
- testEntity = new TestEntity(new IDValue(""))
58
- });
59
-
60
- it('should return an array of DomainEvent', () => {
61
- const domainEvents = testEntity.pullDomainEvents();
62
-
63
- expectTypeOf<typeof domainEvents>().toEqualTypeOf<DomainEvent[]>();
64
- });
65
-
66
- it('should return the record domain events', () => {
67
- const testDomainEvent = new TestDomainEvent(
68
- "",
69
- new Date(),
70
- "Test",
71
- 1,
72
- {},
73
- );
74
-
75
- testEntity.recordDomainEvent(testDomainEvent);
76
-
77
- const domainEvents = testEntity.pullDomainEvents();
78
- const expectedDomainEvents = [testDomainEvent];
79
-
80
- expect(domainEvents).toEqual(expectedDomainEvents);
81
- });
82
- });
83
-
84
- describe('#recordDomainEvent', () => {
85
- let testEntity: TestEntity;
86
-
87
- beforeEach(() => {
88
- testEntity = new TestEntity(new IDValue(""))
89
- });
90
-
91
- it('should record a domain events', () => {
92
- const testDomainEvent = new TestDomainEvent(
93
- "",
94
- new Date(),
95
- "Test",
96
- 1,
97
- {},
98
- );
99
-
100
- testEntity.recordDomainEvent(testDomainEvent);
101
-
102
- const domainEvents = testEntity.pullDomainEvents();
103
- const expectedDomainEvents = [testDomainEvent];
104
-
105
- expect(domainEvents).toEqual(expectedDomainEvents);
106
- });
107
- });
108
-
109
- it('should declare a "toPrimitives" method', () => {
110
- expectTypeOf<Entity<IDValue, Model>>().toHaveProperty('toPrimitives');
111
- expectTypeOf<Entity<IDValue, Model>['toPrimitives']>().toBeFunction();
112
- expectTypeOf<Entity<IDValue, Model>['toPrimitives']>().returns.toEqualTypeOf<Model>();
113
- });
114
- });
@@ -1,54 +0,0 @@
1
- import { RegisterEntity, EntityRegistry, Entity, EntityNotRegistered } from "../../src/entities";
2
- import { UUIDValue } from "../../src/value-objects";
3
-
4
- type MockModel = {
5
- id: string;
6
- };
7
-
8
- class ID extends UUIDValue {
9
- attributeName = "id";
10
- }
11
-
12
- class MockEntity extends Entity<ID, MockModel> {
13
- constructor(id: ID) {
14
- super(id);
15
- }
16
-
17
- toPrimitives(): MockModel {
18
- return {
19
- id: this.id.value,
20
- };
21
- }
22
-
23
- static fromPrimitives<MockModel>(model: MockModel): MockEntity {
24
- return new MockEntity(new ID(model["id"]));
25
- }
26
- }
27
-
28
- RegisterEntity("mock")(MockEntity);
29
-
30
- describe("RegisterEntity", () => {
31
- beforeEach(() => {
32
- (EntityRegistry as any).registry.clear();
33
- RegisterEntity("mock")(MockEntity);
34
- });
35
-
36
- it("registers the entity constructor under the given type", () => {
37
- const resolved = EntityRegistry.resolve<MockModel>("mock");
38
- expect(resolved).toBe(MockEntity);
39
- });
40
-
41
- it("creates an entity instance from registered type", () => {
42
- const model = { id: 'abc-123' };
43
- const entity = EntityRegistry.create("mock", model);
44
-
45
- expect(entity).toBeInstanceOf(MockEntity);
46
- expect(entity.id.value).toBe("abc-123");
47
- });
48
-
49
- it("throws if trying to create an unregistered entity", () => {
50
- const model = { id: '123' };
51
-
52
- expect(() => EntityRegistry.create("unknown", model)).toThrow(EntityNotRegistered);
53
- });
54
- });
@@ -1,19 +0,0 @@
1
- import { PascalCamelToSnake } from "../../src/formatters";
2
-
3
- describe('PascalCamelToSnake', () => {
4
- describe('.format', () => {
5
- it('should return a pascal case to snake case', () => {
6
- const resultID = PascalCamelToSnake.format("ID");
7
- const resultExampleDAO = PascalCamelToSnake.format("ExampleDAO");
8
-
9
- expect(resultID).toEqual("id");
10
- expect(resultExampleDAO).toEqual("example_dao");
11
- });
12
-
13
- it('should return a camel case to snake case', () => {
14
- const resultAlgoClass = PascalCamelToSnake.format("algoClass");
15
-
16
- expect(resultAlgoClass).toEqual("algo_class");
17
- });
18
- });
19
- });
@@ -1,155 +0,0 @@
1
- import { FetchHTTPProvider, HTTPException } from "../../src/http";
2
-
3
- const url = new URL("https://api.example.com/resource");
4
-
5
- function createMockResponse({
6
- ok = true,
7
- status = 200,
8
- contentType = "application/json",
9
- body = {},
10
- }: {
11
- ok?: boolean;
12
- status?: number;
13
- contentType?: string;
14
- body?: any;
15
- }): Promise<Response> {
16
- return Promise.resolve({
17
- ok,
18
- status,
19
- headers: {
20
- get: (key: string) =>
21
- key.toLowerCase() === "content-type" ? contentType : null,
22
- },
23
- json: async () => body,
24
- text: async () => (typeof body === "string" ? body : JSON.stringify(body)),
25
- blob: async () =>
26
- new Blob([typeof body === "string" ? body : JSON.stringify(body)], {
27
- type: contentType,
28
- }),
29
- } as unknown as Response);
30
- }
31
-
32
- describe("FetchHTTPProvider", () => {
33
- let provider: FetchHTTPProvider;
34
-
35
- beforeEach(() => {
36
- provider = new FetchHTTPProvider();
37
- global.fetch = jest.fn();
38
- });
39
-
40
- afterEach(() => {
41
- jest.resetAllMocks();
42
- });
43
-
44
- it("parses JSON response", async () => {
45
- const mockJson = { message: "success" };
46
-
47
- (global.fetch as jest.Mock).mockImplementation(() =>
48
- createMockResponse({ body: mockJson })
49
- );
50
-
51
- const result = await provider.get<typeof mockJson>(url);
52
-
53
- expect(result).toEqual(mockJson);
54
- });
55
-
56
- it("parses text response", async () => {
57
- const mockText = "plain text";
58
-
59
- (global.fetch as jest.Mock).mockImplementation(() =>
60
- createMockResponse({ contentType: "text/plain", body: mockText })
61
- );
62
-
63
- const result = await provider.get<string>(url);
64
-
65
- expect(result).toEqual(mockText);
66
- });
67
-
68
- it("parses blob response", async () => {
69
- const mockBinary = "image data";
70
-
71
- (global.fetch as jest.Mock).mockImplementation(() =>
72
- createMockResponse({ contentType: "image/png", body: mockBinary })
73
- );
74
-
75
- const result = await provider.get<Blob>(url);
76
-
77
- expect(result).toBeInstanceOf(Blob);
78
- });
79
-
80
- it("returns undefined for 204 No Content", async () => {
81
- (global.fetch as jest.Mock).mockImplementation(() =>
82
- createMockResponse({ status: 204, body: undefined })
83
- );
84
-
85
- const result = await provider.get<undefined>(url);
86
-
87
- expect(result).toBeUndefined();
88
- });
89
-
90
- it("throws HTTPException on failed JSON response", async () => {
91
- const errorJson = { title: "Unauthorized", code: 401 };
92
-
93
- (global.fetch as jest.Mock).mockImplementation(() =>
94
- createMockResponse({ ok: false, status: 401, body: errorJson })
95
- );
96
-
97
- await expect(provider.get(url)).rejects.toThrow(HTTPException);
98
- });
99
-
100
- it("throws HTTPException on failed text response", async () => {
101
- const errorText = "Forbidden";
102
-
103
- (global.fetch as jest.Mock).mockImplementation(() =>
104
- createMockResponse({
105
- ok: false,
106
- status: 403,
107
- contentType: "text/plain",
108
- body: errorText,
109
- })
110
- );
111
-
112
- await expect(provider.get(url)).rejects.toThrow(HTTPException);
113
- });
114
-
115
- it("deduplicates ongoing requests", async () => {
116
- const mockJson = { message: "deduplicated" };
117
-
118
- (global.fetch as jest.Mock).mockImplementation(() =>
119
- createMockResponse({ body: mockJson })
120
- );
121
-
122
- const promise1 = provider.get<typeof mockJson>(url);
123
- const promise2 = provider.get<typeof mockJson>(url);
124
-
125
- expect(promise1).toStrictEqual(promise2);
126
-
127
- const result = await promise1;
128
-
129
- expect(result).toEqual(mockJson);
130
- expect(global.fetch).toHaveBeenCalledTimes(1);
131
- });
132
-
133
- it("cleans up ongoingRequests after completion", async () => {
134
- const mockJson = { message: "done" };
135
-
136
- (global.fetch as jest.Mock).mockImplementation(() =>
137
- createMockResponse({ body: mockJson })
138
- );
139
-
140
- await provider.get<typeof mockJson>(url);
141
-
142
- const key = `GET:${url.href}:`;
143
- const internalMap = (provider as any).ongoingRequests as Map<string, Promise<any>>;
144
-
145
- expect(internalMap.has(key)).toBe(false);
146
- });
147
-
148
- it("throws if fetch returns undefined", async () => {
149
- (global.fetch as jest.Mock).mockImplementation(() =>
150
- Promise.resolve(undefined as any)
151
- );
152
-
153
- await expect(provider.get(url)).rejects.toThrow(HTTPException);
154
- });
155
- });