@stancer/stancer 1.0.2 → 1.1.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.
Files changed (184) hide show
  1. package/CHANGELOG.md +12 -1
  2. package/dist/api-endpoint/http/fetch-http-client.d.ts.map +1 -1
  3. package/dist/api-endpoint/http/fetch-http-client.js +7 -1
  4. package/dist/api-endpoint/http/fetch-http-client.js.map +1 -1
  5. package/dist/core/common/handler/metadata-handler.d.ts +18 -0
  6. package/dist/core/common/handler/metadata-handler.d.ts.map +1 -0
  7. package/dist/core/common/handler/metadata-handler.js +19 -0
  8. package/dist/core/common/handler/metadata-handler.js.map +1 -0
  9. package/dist/event/model.d.ts +20 -0
  10. package/dist/event/model.d.ts.map +1 -0
  11. package/dist/event/model.js +30 -0
  12. package/dist/event/model.js.map +1 -0
  13. package/dist/index.d.ts +60 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +57 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/payment-intent/api-endpoint.d.ts +16 -1
  18. package/dist/payment-intent/api-endpoint.d.ts.map +1 -1
  19. package/dist/payment-intent/api-endpoint.js +2 -1
  20. package/dist/payment-intent/api-endpoint.js.map +1 -1
  21. package/dist/payment-intent/model.d.ts +3 -2
  22. package/dist/payment-intent/model.d.ts.map +1 -1
  23. package/dist/payment-intent/model.js +6 -0
  24. package/dist/payment-intent/model.js.map +1 -1
  25. package/dist/payment-intent/repository.d.ts +1 -1
  26. package/dist/payment-intent/repository.d.ts.map +1 -1
  27. package/dist/webhook/api-endpoint.d.ts +16 -0
  28. package/dist/webhook/api-endpoint.d.ts.map +1 -0
  29. package/dist/webhook/api-endpoint.js +33 -0
  30. package/dist/webhook/api-endpoint.js.map +1 -0
  31. package/dist/webhook/model.d.ts +35 -0
  32. package/dist/webhook/model.d.ts.map +1 -0
  33. package/dist/webhook/model.js +110 -0
  34. package/dist/webhook/model.js.map +1 -0
  35. package/dist/webhook/repository.d.ts +7 -0
  36. package/dist/webhook/repository.d.ts.map +1 -0
  37. package/dist/webhook/repository.js +10 -0
  38. package/dist/webhook/repository.js.map +1 -0
  39. package/dist/webhook/validator.d.ts +4 -0
  40. package/dist/webhook/validator.d.ts.map +1 -0
  41. package/dist/webhook/validator.js +16 -0
  42. package/dist/webhook/validator.js.map +1 -0
  43. package/package.json +33 -22
  44. package/dist/main.d.ts +0 -2
  45. package/dist/main.d.ts.map +0 -1
  46. package/dist/main.js +0 -2
  47. package/dist/main.js.map +0 -1
  48. package/reports/base.css +0 -362
  49. package/reports/block-navigation.js +0 -75
  50. package/reports/cobertura-coverage.xml +0 -18
  51. package/reports/favicon.png +0 -0
  52. package/reports/index.html +0 -177
  53. package/reports/junit.xml +0 -25
  54. package/reports/prettify.css +0 -101
  55. package/reports/prettify.js +0 -955
  56. package/reports/sort-arrow-sprite.png +0 -0
  57. package/reports/sorter.js +0 -205
  58. package/src/address/api-endpoint.ts +0 -30
  59. package/src/address/model.ts +0 -79
  60. package/src/address/repository.ts +0 -18
  61. package/src/address/validator.ts +0 -34
  62. package/src/api-endpoint/abstract-api-endpoint.ts +0 -310
  63. package/src/api-endpoint/configuration.ts +0 -125
  64. package/src/api-endpoint/http/fetch-http-client.ts +0 -29
  65. package/src/api-endpoint/http/http.ts +0 -75
  66. package/src/api-endpoint/http/index.ts +0 -2
  67. package/src/api-endpoint/version.ts +0 -4
  68. package/src/card/api-endpoint.ts +0 -39
  69. package/src/card/enum/funding.ts +0 -11
  70. package/src/card/enum/nature.ts +0 -7
  71. package/src/card/enum/network.ts +0 -16
  72. package/src/card/model.ts +0 -139
  73. package/src/card/repository.ts +0 -11
  74. package/src/card/validator.ts +0 -79
  75. package/src/core/abstract-model.ts +0 -126
  76. package/src/core/abstract-repository.ts +0 -201
  77. package/src/core/common/branded-type.ts +0 -15
  78. package/src/core/common/handler/date-handler.ts +0 -31
  79. package/src/core/common/handler/enum-handler.ts +0 -18
  80. package/src/core/common/handler/handler.ts +0 -64
  81. package/src/core/common/handler/id-handler.ts +0 -27
  82. package/src/core/common/id-validator.ts +0 -23
  83. package/src/core/common/mutation-tracked-object.ts +0 -49
  84. package/src/core/endpoint-interface.ts +0 -43
  85. package/src/core/error/base-error.ts +0 -1
  86. package/src/core/error/endpoint/api/api-conflict-error.ts +0 -7
  87. package/src/core/error/endpoint/api/api-error.ts +0 -10
  88. package/src/core/error/endpoint/api/api-not-found-error.ts +0 -7
  89. package/src/core/error/endpoint/api/api-unauthorized-error.ts +0 -7
  90. package/src/core/error/endpoint/api/api-unknown-error.ts +0 -7
  91. package/src/core/error/endpoint/api/api-validation-error.ts +0 -13
  92. package/src/core/error/endpoint/api/index.ts +0 -6
  93. package/src/core/error/endpoint/route-inaccessible-error.ts +0 -8
  94. package/src/core/error/unpopulated-error.ts +0 -3
  95. package/src/core/error/unsendable-error.ts +0 -3
  96. package/src/core/error/validation-error.ts +0 -3
  97. package/src/core/validator.ts +0 -129
  98. package/src/customer/api-endpoint.ts +0 -29
  99. package/src/customer/model.ts +0 -113
  100. package/src/customer/repository.ts +0 -14
  101. package/src/customer/validator.ts +0 -26
  102. package/src/payment/api-endpoint.ts +0 -118
  103. package/src/payment/enum/currency.ts +0 -14
  104. package/src/payment/enum/method.ts +0 -2
  105. package/src/payment/enum/status.ts +0 -21
  106. package/src/payment/model.ts +0 -378
  107. package/src/payment/repository.ts +0 -73
  108. package/src/payment/validator.ts +0 -96
  109. package/src/payment-intent/api-endpoint.ts +0 -123
  110. package/src/payment-intent/enum/status.ts +0 -21
  111. package/src/payment-intent/enum/threeds.ts +0 -2
  112. package/src/payment-intent/model.ts +0 -266
  113. package/src/payment-intent/repository.ts +0 -85
  114. package/src/payment-intent/validator.ts +0 -45
  115. package/src/payout/model.ts +0 -8
  116. package/src/refund/api-endpoint.ts +0 -33
  117. package/src/refund/enum/refund-status.ts +0 -11
  118. package/src/refund/model.ts +0 -90
  119. package/src/refund/repository.ts +0 -14
  120. package/src/refund/validator.ts +0 -44
  121. package/src/sepa/api-endpoint.ts +0 -26
  122. package/src/sepa/model.ts +0 -80
  123. package/src/sepa/repository.ts +0 -11
  124. package/src/sepa/validator.ts +0 -56
  125. package/test/fixtures/endpoint.stub.ts +0 -22
  126. package/test/fixtures/list-endpoint.stub.ts +0 -33
  127. package/test/fixtures/list-model.stub.ts +0 -24
  128. package/test/fixtures/model.stub.ts +0 -12
  129. package/test/functional/address.test.ts +0 -126
  130. package/test/functional/card.test.ts +0 -175
  131. package/test/functional/customer.test.ts +0 -174
  132. package/test/functional/payment-intent.test.ts +0 -215
  133. package/test/functional/payment.test.ts +0 -206
  134. package/test/functional/refund.test.ts +0 -112
  135. package/test/functional/sepa.test.ts +0 -136
  136. package/test/integration/stub-integration.test.ts +0 -88
  137. package/test/stub/stub-endpoint.ts +0 -42
  138. package/test/stub/stub-model.ts +0 -82
  139. package/test/stub/stub-repository.ts +0 -11
  140. package/test/test-helper.ts +0 -133
  141. package/test/unit/address/api-endpoint.test.ts +0 -134
  142. package/test/unit/address/fixtures.ts +0 -60
  143. package/test/unit/address/model.test.ts +0 -75
  144. package/test/unit/address/repository.test.ts +0 -148
  145. package/test/unit/address/validator.test.ts +0 -49
  146. package/test/unit/api-endpoint/http/fetch-http-client.test.ts +0 -44
  147. package/test/unit/api-endpoint/http/http.test.ts +0 -152
  148. package/test/unit/card/api-endpoint.test.ts +0 -170
  149. package/test/unit/card/card-helper.ts +0 -116
  150. package/test/unit/card/fixtures.ts +0 -87
  151. package/test/unit/card/model.test.ts +0 -167
  152. package/test/unit/card/repository.test.ts +0 -146
  153. package/test/unit/card/validator.test.ts +0 -96
  154. package/test/unit/core/common/mutation-tracked-object.test.ts +0 -24
  155. package/test/unit/customer/api-endpoint.test.ts +0 -143
  156. package/test/unit/customer/fixtures.ts +0 -73
  157. package/test/unit/customer/model.test.ts +0 -90
  158. package/test/unit/customer/repository.test.ts +0 -148
  159. package/test/unit/customer/validator.test.ts +0 -62
  160. package/test/unit/payment/api-endpoint.test.ts +0 -188
  161. package/test/unit/payment/fixtures.ts +0 -116
  162. package/test/unit/payment/model.test.ts +0 -240
  163. package/test/unit/payment/repository.test.ts +0 -270
  164. package/test/unit/payment/validator.test.ts +0 -137
  165. package/test/unit/payment-intent/api-endpoint.test.ts +0 -142
  166. package/test/unit/payment-intent/fixtures.ts +0 -94
  167. package/test/unit/payment-intent/model.test.ts +0 -157
  168. package/test/unit/payment-intent/repository.test.ts +0 -263
  169. package/test/unit/payment-intent/validator.test.ts +0 -136
  170. package/test/unit/refund/api-endpoint.test.ts +0 -129
  171. package/test/unit/refund/fixtures.ts +0 -58
  172. package/test/unit/refund/model.test.ts +0 -89
  173. package/test/unit/refund/repository.test.ts +0 -157
  174. package/test/unit/refund/validator.test.ts +0 -89
  175. package/test/unit/sepa/api-endpoint.test.ts +0 -170
  176. package/test/unit/sepa/fixture.ts +0 -62
  177. package/test/unit/sepa/model.test.ts +0 -86
  178. package/test/unit/sepa/repository.test.ts +0 -149
  179. package/test/unit/sepa/sepa-helper.ts +0 -71
  180. package/test/unit/sepa/validator.test.ts +0 -58
  181. package/test/unit/stub/stub-endpoint.test.ts +0 -236
  182. package/test/unit/stub/stub-list-endpoint.test.ts +0 -86
  183. package/test/unit/stub/stub-model.test.ts +0 -111
  184. package/test/unit/stub/stub-repository.test.ts +0 -144
@@ -1,112 +0,0 @@
1
- import { Temporal } from 'temporal-polyfill';
2
- import { describe, expect, it } from 'vitest';
3
-
4
- import { Configuration } from '#api/configuration.js';
5
- import { idCard } from '#card/model.js';
6
- import { PaymentRepository } from '#payment/repository.js';
7
- import { RefundModel, idRefund, refundAmount } from '#refund/model.js';
8
- import { RefundRepository } from '#refund/repository.js';
9
- import { randomNumber, randomString } from '#test/test-helper.js';
10
-
11
- const config = Configuration;
12
- const amount = refundAmount(randomNumber(1, 50));
13
- const paymentRepository = new PaymentRepository();
14
-
15
- describe('refund functional', async () => {
16
- config.setKey({
17
- privateLiveKey: null,
18
- privateTestKey: process.env['API_KEY'] ?? null,
19
- });
20
- config._host = process.env['API_HOST'] ?? 'api.stancer.com';
21
-
22
- const repository = new RefundRepository();
23
- describe('refund post', async () => {
24
- const payment = await paymentRepository.create();
25
- payment.setCard(idCard('card_Y2QKuArL3x7A18OjLZdSizoI'));
26
- payment.setAmount(1000);
27
- payment.setCurrency('eur');
28
- await paymentRepository.send(payment);
29
- it('create a refund with id', { concurrent: false }, async () => {
30
- const refund = await repository.create({ amount, payment: payment.id });
31
- await repository.send(refund);
32
- expect(refund.id).toBeDefined();
33
- expect(refund.id).toContain('refd_');
34
- expect(refund.amount).toBe(amount);
35
- expect(refund.currency).toBeDefined();
36
- expect(refund.status).toBeDefined();
37
- await paymentRepository.populateRefunds(payment);
38
- expect(payment.refundedAmount).toBe(refund.amount);
39
- });
40
- it('create a refund with paymentModel', { concurrent: false }, async () => {
41
- const refund = await repository.create({ amount, payment: payment.id });
42
- await repository.send(refund);
43
- expect(refund.id).toBeDefined();
44
- expect(refund.id).toContain('refd_');
45
- expect(refund.amount).toBe(amount);
46
- expect(refund.currency).toBeDefined();
47
- expect(refund.status).toBeDefined();
48
- await paymentRepository.populateRefunds(payment);
49
- expect(payment.refundedAmount).toBe(amount * 2);
50
- });
51
- it('need a payment', async () => {
52
- const amount = refundAmount(randomNumber(1, 50));
53
- const refund = await repository.create({ amount });
54
- await expect(() => repository.send(refund)).rejects.toThrow();
55
- });
56
- it('need an amount', async () => {
57
- const refund = await repository.create({ payment: payment.id });
58
- await expect(() => repository.send(refund)).rejects.toThrow();
59
- });
60
- });
61
- describe('refund other verbs', async () => {
62
- const payment = await paymentRepository.create();
63
- payment.setCard(idCard('card_Y2QKuArL3x7A18OjLZdSizoI'));
64
- payment.setAmount(1000);
65
- payment.setCurrency('eur');
66
- await paymentRepository.send(payment);
67
- const repository = new RefundRepository();
68
- it('cannot patch', async () => {
69
- const amount = refundAmount(randomNumber(1, 50));
70
- const refund = await repository.create({ amount, payment: payment.id });
71
- await repository.send(refund);
72
- refund.setAmount(refundAmount(200));
73
-
74
- await expect(async () => await repository.send(refund)).rejects.toThrow();
75
- });
76
- it('cannot delete', async () => {
77
- const amount = refundAmount(randomNumber(1, 50));
78
- const refund = await repository.create({ amount, payment: payment.id });
79
- await repository.send(refund);
80
- await expect(async () => await repository.delete(refund)).rejects.toThrow();
81
- });
82
- it('list', async () => {
83
- const expectedCount = 3;
84
- const refunds = repository.list({ limit: expectedCount });
85
- let length = 0;
86
- for await (const item of refunds) {
87
- expect(item).toBeInstanceOf(RefundModel);
88
- expect(item.amount).toBeDefined();
89
- expect(item.currency).toEqual('eur');
90
- length++;
91
- if (length > expectedCount) {
92
- return;
93
- }
94
- }
95
- expect(length).toBe(expectedCount);
96
- });
97
- it('get', async () => {
98
- const id = idRefund('refd_07vi5SIqsierzO8nuaOFBX7v');
99
- const refund = await repository.create(id);
100
- expect(refund.id).toBe(id);
101
- expect(refund.amount).toBe(44);
102
- expect(refund.currency).toBe('eur');
103
- expect(refund.createdAt).toStrictEqual(Temporal.Instant.fromEpochMilliseconds(1788350401 * 1000));
104
- expect(refund.payment).toBe('paym_tGY2hPlbr33GDEnfC0ZyQA92');
105
- expect(refund.status).toBe('to_refund');
106
- });
107
- it('get a 404', async () => {
108
- const id = `refd_${randomString(24)}`;
109
- await expect(() => repository.create(id)).rejects.toThrow();
110
- });
111
- });
112
- });
@@ -1,136 +0,0 @@
1
- import { Temporal } from 'temporal-polyfill';
2
- import 'temporal-polyfill/global';
3
- import { describe, expect, it } from 'vitest';
4
-
5
- import { Configuration } from '#api/configuration.js';
6
- import { IdSepa } from '#sepa/model.js';
7
- import { SepaRepository } from '#sepa/repository.js';
8
- import { randomMonth, randomNumber, randomString } from '#test/test-helper.js';
9
- import { bicDataProvider, validIbanDataProvider } from '#test/unit/sepa/sepa-helper.js';
10
-
11
- import { idCustomer } from '../../src/customer/model';
12
-
13
- const config = Configuration;
14
-
15
- config.setKey({
16
- privateLiveKey: null,
17
- privateTestKey: process.env['API_KEY'] ?? null,
18
- });
19
-
20
- config._host = process.env['API_HOST'] ?? 'api.stancer.com';
21
-
22
- describe('sepa functional', () => {
23
- describe('sepa creation', () => {
24
- const iban = validIbanDataProvider(true);
25
- const name = randomString(3, 64);
26
-
27
- it('create an sepa with minimum information', async () => {
28
- const repository = new SepaRepository();
29
-
30
- const sepa = await repository.create({ iban, name });
31
- await repository.send(sepa);
32
- expect(sepa.id).toBeTruthy();
33
- expect(sepa.id).toContain('sepa_');
34
- expect(sepa.name).toBe(name);
35
- expect(sepa.last4).toBe(iban.slice(iban.length - 4));
36
- });
37
-
38
- it('create a complete flat sepa', async () => {
39
- const repository = new SepaRepository();
40
- const mandate = randomString(1, 35);
41
- const name = randomString(3, 64);
42
- const dateMandate = Temporal.PlainDate.from(
43
- `${String(randomNumber(1971, 2020))}-${String(randomMonth()).padStart(2, '0')}-${String(randomNumber(1, 28)).padStart(2, '0')}`,
44
- );
45
- const iban = validIbanDataProvider(true) as string;
46
- const bic = bicDataProvider(true) as string;
47
- const customer = idCustomer('cust_SsySBRtGUq3pG9zbSOhJSKCa');
48
- const last4 = iban.slice(iban.length - 4);
49
-
50
- const sepa = await repository.create({
51
- mandate,
52
- name,
53
- dateMandate,
54
- iban,
55
- bic,
56
- customer,
57
- });
58
-
59
- await repository.send(sepa);
60
-
61
- expect(sepa.id).toBeTruthy();
62
-
63
- expect(sepa._data.mandate).toBe(sepa.mandate);
64
- expect(sepa._data.dateMandate).toBe(sepa.dateMandate);
65
- expect(sepa._data.last4).toBe(last4);
66
- expect(sepa._data.name).toBe(sepa.name);
67
- expect(sepa._data.bic).toBe(sepa.bic);
68
- });
69
- it('need required name', async () => {
70
- const iban = validIbanDataProvider(true) as string;
71
-
72
- const repository = new SepaRepository();
73
-
74
- const sepaWithoutRequiredValue = await repository.create({ iban });
75
-
76
- await expect(async () => await repository.send(sepaWithoutRequiredValue)).rejects.toThrow(
77
- 'name is required to send SepaModel',
78
- );
79
- });
80
- it('need required iban', async () => {
81
- const name = randomString(3, 64);
82
-
83
- const repository = new SepaRepository();
84
-
85
- const sepaWithoutRequiredValue = await repository.create({ name });
86
-
87
- await expect(async () => await repository.send(sepaWithoutRequiredValue)).rejects.toThrow(
88
- 'iban is required to send SepaModel',
89
- );
90
- });
91
-
92
- describe('patch', () => {
93
- it('is available on sepa', async () => {
94
- const repository = new SepaRepository();
95
- const name = randomString(3, 64);
96
- const iban = validIbanDataProvider(true) as string;
97
-
98
- const sepa = await repository.create({ name, iban });
99
-
100
- await repository.send(sepa);
101
- const id = sepa.id;
102
- expect(id).toBeTruthy();
103
- expect(sepa.last4).toBe(iban.slice(iban.length - 4));
104
- expect(sepa.name).toBe(name);
105
-
106
- const newName = randomString(3, 64);
107
- sepa.setName(newName);
108
- await repository.send(sepa);
109
- expect(id).toBe(sepa.id);
110
- expect(sepa.name).toBe(newName);
111
- expect(sepa.last4).toBe(iban.slice(iban.length - 4));
112
- });
113
- });
114
-
115
- describe('get', () => {
116
- it('get the correct object', async () => {
117
- const repository = new SepaRepository();
118
- const id = 'sepa_XbEkAt8hAKTbh6Yel7xjEk7O' as IdSepa;
119
- const sepa = await repository.create(id);
120
- expect(sepa.id).toBe(id);
121
- expect(sepa.bic).toBe('TESTFRPP');
122
- expect(sepa.last4).toBe('2606');
123
- expect(sepa.dateMandate).toEqual(Temporal.PlainDate.from('2024-12-25'));
124
- expect(sepa.mandate).toBe('libraries_mandate_id');
125
- expect(sepa.name).toBe('Patrice Dinde');
126
- expect(sepa.createdAt.epochMilliseconds).toBe(1758551637000);
127
- expect(sepa.deleted).toBe(false);
128
- });
129
- it('get a 404 exception if unknown', async () => {
130
- const id = 'sepa_' + randomString(24);
131
- const repository = new SepaRepository();
132
- await expect(() => repository.create(id)).rejects.toThrow();
133
- });
134
- });
135
- });
136
- });
@@ -1,88 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { StancerError } from '#core/error/base-error.js';
4
- import { stubSerialized } from '#test/fixtures/endpoint.stub.js';
5
- import { StubEndpoint } from '#test/stub/stub-endpoint.js';
6
- import { StubRepository } from '#test/stub/stub-repository.js';
7
- import { randomString } from '#test/test-helper.js';
8
-
9
- vi.mock('#api/http/fetch-http-client.js', () => {
10
- return {
11
- httpClient: {
12
- makeRequest: vi.fn(() => {
13
- return Promise.resolve({
14
- status: 200,
15
- json: () => {
16
- return {
17
- ...stubSerialized,
18
- } as Record<string, unknown>;
19
- },
20
- });
21
- }),
22
- },
23
- };
24
- });
25
- describe('stub', () => {
26
- describe('send', () => {
27
- it("doesn't resend previously sent object construction", async () => {
28
- const endpoint = new StubEndpoint();
29
- const spyonPatch = vi.spyOn(endpoint, 'patch');
30
- const spyonPost = vi.spyOn(endpoint, 'post');
31
- const defaultValue = randomString(12);
32
- const repository = new StubRepository(endpoint);
33
- const model = await repository.create({ default: defaultValue, allAuthorized: true });
34
- expect(model.mutations).toStrictEqual({ default: defaultValue, allAuthorized: true });
35
-
36
- await repository.send(model);
37
- expect(spyonPatch).toHaveBeenCalledTimes(0);
38
- expect(spyonPost).toHaveBeenCalledTimes(1);
39
-
40
- expect(model.mutations).toStrictEqual({});
41
- await expect(() => repository.send(model)).rejects.toThrow(StancerError);
42
-
43
- expect(spyonPatch).toHaveBeenCalledTimes(0);
44
- expect(spyonPost).toHaveBeenCalledTimes(1);
45
- });
46
- it("doesn't resend previously sent object setting", async () => {
47
- const endpoint = new StubEndpoint();
48
- const spyonPatch = vi.spyOn(endpoint, 'patch');
49
- const spyonPost = vi.spyOn(endpoint, 'post');
50
- const defaultValue = randomString(12);
51
- const repository = new StubRepository(endpoint);
52
- const model = await repository.create();
53
- model.setDefault(defaultValue);
54
- expect(model.mutations).toStrictEqual({ default: defaultValue });
55
-
56
- await repository.send(model);
57
- expect(spyonPatch).toHaveBeenCalledTimes(0);
58
- expect(spyonPost).toHaveBeenCalledTimes(1);
59
- expect(model.mutations).toStrictEqual({});
60
-
61
- await expect(() => repository.send(model)).rejects.toThrow(StancerError);
62
- expect(spyonPatch).toHaveBeenCalledTimes(0);
63
- expect(spyonPost).toHaveBeenCalledTimes(1);
64
- });
65
- });
66
- it("doesn't repatch", async () => {
67
- const endpoint = new StubEndpoint();
68
- const spyonPatch = vi.spyOn(endpoint, 'patch');
69
- const spyonPost = vi.spyOn(endpoint, 'post');
70
- const id = 'stub_' + randomString(24);
71
- const repository = new StubRepository(endpoint);
72
- const model = await repository.create(id);
73
- model.setAllAuthorized(true);
74
- expect(model.mutations).toStrictEqual({});
75
-
76
- model.setAllAuthorized(false);
77
- expect(model.mutations).toStrictEqual({ allAuthorized: false });
78
-
79
- await repository.send(model);
80
- expect(spyonPatch).toHaveBeenCalledOnce();
81
- expect(spyonPost).toHaveBeenCalledTimes(0);
82
-
83
- expect(model.mutations).toStrictEqual({});
84
- await expect(() => repository.send(model)).rejects.toThrow(StancerError);
85
- expect(spyonPatch).toHaveBeenCalledOnce();
86
- expect(spyonPost).toHaveBeenCalledTimes(0);
87
- });
88
- });
@@ -1,42 +0,0 @@
1
- import { ApiEndpoint, Restriction, baseHandler } from '#api/abstract-api-endpoint.js';
2
- import { ModelType } from '#core/abstract-model.js';
3
- import { IdValue } from '#core/common/branded-type.js';
4
- import { defaultHandler } from '#core/common/handler/handler.js';
5
- import { IdHandler } from '#core/common/handler/id-handler.js';
6
-
7
- export interface StubData extends ModelType {
8
- id: IdValue;
9
- default: string;
10
- defaultNumber: number;
11
- untouchable: boolean;
12
- allAuthorized: boolean;
13
- ungettable: boolean;
14
- unmodifiable: boolean;
15
- }
16
- const StubModel = { regex: /^stub_[a-zA-Z0-9]{24}$/ };
17
- export class StubEndpoint extends ApiEndpoint<StubData> {
18
- protected _handler = {
19
- id: IdHandler(StubModel.regex),
20
- default: defaultHandler<StubData['default']>('string'),
21
- defaultNumber: defaultHandler<StubData['defaultNumber']>('number'),
22
- untouchable: defaultHandler<StubData['untouchable']>('boolean', {
23
- gettable: false,
24
- postable: false,
25
- patchable: false,
26
- }),
27
- allAuthorized: defaultHandler<StubData['allAuthorized']>('boolean'),
28
- ungettable: defaultHandler<StubData['ungettable']>('boolean', { gettable: false }),
29
- unmodifiable: defaultHandler<StubData['unmodifiable']>('boolean', { postable: false, patchable: false }),
30
- ...baseHandler('created'),
31
- };
32
- constructor(restriction?: Restriction) {
33
- super({
34
- name: 'stubs',
35
- ...restriction, // We should not create a new object with variable restriction but it is more practical for testing.
36
- });
37
- }
38
-
39
- endpointUrl(id?: IdValue) {
40
- return this._getResourceUri(id);
41
- }
42
- }
@@ -1,82 +0,0 @@
1
- import { AbstractModel, type ModelData } from '#core/abstract-model.js';
2
- import { IdValue } from '#core/common/branded-type.js';
3
- import { isValidID } from '#core/common/id-validator.js';
4
- import { Validator, createdAtValidator } from '#core/validator.js';
5
-
6
- //#region Data interface
7
-
8
- export type IdStub = IdValue<`stub_${string}`>;
9
- export const STUB_DEFAULT_MAX_LENGTH = 64;
10
- export const STUB_DEFAULT_NUMBER_MAX_LENGTH = 264;
11
- export interface StubData extends ModelData {
12
- id: IdValue;
13
- default: string;
14
- defaultNumber: number;
15
- untouchable: boolean;
16
- allAuthorized: boolean;
17
- ungettable: boolean;
18
- unmodifiable: boolean;
19
- }
20
-
21
- //#endregion
22
- //#region validator
23
- export const StubValidator: Validator<StubData> = {
24
- id: {
25
- validate: (arg0) => isValidID<IdStub>(arg0, StubModel.regex),
26
- errorMessage: (value: string) => `The id (${value}) doesn't verify the regex ${StubModel.regex}`,
27
- },
28
- default: {
29
- validate: (arg0) => arg0.length < STUB_DEFAULT_MAX_LENGTH,
30
- errorMessage: (value: string) => `The length of value (${value.length}) is too long`,
31
- },
32
- defaultNumber: {
33
- validate: (arg0) => arg0 < STUB_DEFAULT_NUMBER_MAX_LENGTH,
34
- errorMessage: (value: string) => `DefaultNumber is too big of a number (${value})`,
35
- },
36
- ungettable: true,
37
- unmodifiable: true,
38
- untouchable: true,
39
- allAuthorized: true,
40
- createdAt: createdAtValidator,
41
- deleted: true,
42
- };
43
-
44
- //#endregion
45
-
46
- export class StubModel extends AbstractModel<StubData> {
47
- //#region properties
48
- static regex = /^stub_[a-zA-Z0-9]{24}$/;
49
-
50
- //#endregion
51
- //#region setter & getter
52
-
53
- get allAuthorized() {
54
- return this._data.allAuthorized;
55
- }
56
-
57
- setAllAuthorized(value: boolean) {
58
- this._data.allAuthorized = value;
59
- }
60
-
61
- get default() {
62
- return this._data.default;
63
- }
64
-
65
- setDefault(value: string) {
66
- this._data.default = value;
67
- }
68
-
69
- get defaultNumber() {
70
- return this._data.defaultNumber;
71
- }
72
- setDefaultNumber(value: number) {
73
- this._data.defaultNumber = value;
74
- }
75
-
76
- setUngettable(value: boolean) {
77
- this._data.ungettable = value;
78
- }
79
- get unmodifiable() {
80
- return this._data.unmodifiable;
81
- }
82
- }
@@ -1,11 +0,0 @@
1
- import { AbstractRepository, type Repository } from '#core/abstract-repository.js';
2
- import { EndpointInterface } from '#core/endpoint-interface.js';
3
-
4
- import { type StubData, StubEndpoint } from './stub-endpoint.js';
5
- import { StubModel, StubValidator } from './stub-model.js';
6
-
7
- export class StubRepository extends AbstractRepository<StubData, StubModel> implements Repository<StubData, StubModel> {
8
- constructor(endpoint?: EndpointInterface<StubData>) {
9
- super({ endpoint: endpoint ?? new StubEndpoint(), validator: StubValidator, model: StubModel });
10
- }
11
- }
@@ -1,133 +0,0 @@
1
- import { Temporal } from 'temporal-polyfill';
2
- import { expect, vi } from 'vitest';
3
-
4
- import { Http } from '#api/http/index.js';
5
- import { ID_SUFFIX_LENGTH } from '#core/common/id-validator.js';
6
-
7
- type Boundaries = { min?: number; max?: number };
8
-
9
- export const checkType = (
10
- setter: CallableFunction,
11
- type: ('string' | 'object' | 'number' | 'boolean')[],
12
- validValue: unknown,
13
- ) => {
14
- if (!type.includes('object')) {
15
- expect(() => setter({})).toThrow();
16
- }
17
- if (!type.includes('string')) {
18
- expect(() => setter('a')).toThrow();
19
- }
20
- if (!type.includes('number')) {
21
- expect(() => setter(0)).toThrow();
22
- }
23
- if (!type.includes('boolean')) {
24
- expect(() => setter(true)).toThrow();
25
- }
26
- expect(() => setter(validValue)).not.toThrow();
27
- };
28
-
29
- export const goldilocks = (validation: CallableFunction, type: 'number' | 'string', boundaries: Boundaries) => {
30
- const randomFunction = type === 'number' ? randomNumber : randomString;
31
-
32
- const min = boundaries?.min !== undefined && boundaries.min > 0 ? boundaries.min : 0;
33
- const max = boundaries?.max;
34
- if (max !== undefined) {
35
- expect(() => validation(randomFunction(max + 1, 100000))).toThrow();
36
- }
37
- if (min) {
38
- expect(() => validation(randomFunction(0, min - 1))).toThrow();
39
- }
40
- expect(() => validation(randomFunction(min ?? 0, max ?? 100))).not.toThrow();
41
- };
42
-
43
- export const goldilocksId = (setter: CallableFunction, prefix: string) => {
44
- const IDLength = prefix.length + ID_SUFFIX_LENGTH;
45
- expect.soft(() => setter(randomString(1, IDLength - 1))).toThrow();
46
- expect.soft(() => setter(randomString(IDLength + 1, 50))).toThrow();
47
- expect.soft(() => setter(randomString(IDLength))).toThrow();
48
- expect.soft(() => setter(prefix + randomString(ID_SUFFIX_LENGTH))).not.toThrow();
49
- };
50
-
51
- /**
52
- *
53
- * @param resolver
54
- * @returns cleanup function that must be called after using mockfetch to reinitialise the global stub
55
- */
56
- export const mockFetch = (resolver: <T = unknown>(arg0: string, arg1: Http.Request) => T): (() => void) => {
57
- vi.stubGlobal(
58
- 'fetch',
59
- vi.fn().mockImplementation((url: string, req: Http.Request) => {
60
- return Promise.resolve({
61
- ok: true,
62
- json: () => Promise.resolve(resolver(url, req)),
63
- status: 200,
64
- statusText: 'OK',
65
- clone: function () {
66
- return { ...this };
67
- },
68
- });
69
- }),
70
- );
71
- return () => vi.unstubAllGlobals();
72
- };
73
-
74
- export const randomCVC = (): string => {
75
- return randomString(3, 3, '0123456789');
76
- };
77
-
78
- export const randomDate = (): Temporal.PlainDate => {
79
- return new Temporal.PlainDate(randomNumber(2000, 2099), randomMonth(), randomNumber(1, 28));
80
- };
81
-
82
- export const randomExpYear = (): number => {
83
- const date = new Date();
84
- const min = date.getFullYear() + 1;
85
- return randomNumber(min, 2099);
86
- };
87
-
88
- export const randomMonth = (): number => {
89
- return randomNumber(1, 12);
90
- };
91
-
92
- export const randomNumber = (min: number, max?: number): number => {
93
- if (!max) {
94
- max = min;
95
- }
96
- return Math.floor(Math.random() * (max - min + 1)) + min;
97
- };
98
-
99
- export const randomPastDate = (): Temporal.PlainDate => {
100
- const date = new Date();
101
- const max = date.getFullYear() - 1;
102
- const pastYear = randomNumber(1900, max);
103
- return new Temporal.PlainDate(pastYear, randomMonth(), randomNumber(1, 28));
104
- };
105
-
106
- export const randomString = (
107
- min: number,
108
- max?: number,
109
- allowedString = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
110
- ): string => {
111
- const stringLength = randomNumber(min, max);
112
- let string = '';
113
- for (let i = 0; i < stringLength; i++) {
114
- string = string.concat(allowedString.charAt(randomNumber(0, allowedString.length - 1)));
115
- }
116
- return string;
117
- };
118
-
119
- export const SetterGetter = (
120
- callable: CallableFunction,
121
- getter: CallableFunction,
122
- entryValue: unknown,
123
- exitValue?: unknown,
124
- ) => {
125
- expect(getter()).toBeNullable();
126
- callable(entryValue);
127
- expect(getter()).toStrictEqual(exitValue ?? entryValue);
128
- };
129
-
130
- export const SetterGetterId = (Instanciation: CallableFunction, value: unknown = undefined) => {
131
- const object = Instanciation(value);
132
- expect(object.id).toStrictEqual(value);
133
- };