@stancer/stancer 0.0.0-stage → 1.0.0

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 (139) hide show
  1. package/.editorconfig +14 -0
  2. package/.gitlab-ci.yml +159 -0
  3. package/.prettierignore +3 -0
  4. package/CHANGELOG.md +10 -0
  5. package/LICENSE +19 -0
  6. package/README.md +166 -2
  7. package/eslint.config.js +14 -0
  8. package/package.json +52 -3
  9. package/pnpm-workspace.yaml +19 -0
  10. package/prettier.config.js +3 -0
  11. package/src/address/api-endpoint.ts +30 -0
  12. package/src/address/model.ts +79 -0
  13. package/src/address/repository.ts +18 -0
  14. package/src/address/validator.ts +34 -0
  15. package/src/api-endpoint/abstract-api-endpoint.ts +310 -0
  16. package/src/api-endpoint/configuration.ts +125 -0
  17. package/src/api-endpoint/http/fetch-http-client.ts +29 -0
  18. package/src/api-endpoint/http/http.ts +75 -0
  19. package/src/api-endpoint/http/index.ts +2 -0
  20. package/src/api-endpoint/version.ts +4 -0
  21. package/src/card/api-endpoint.ts +39 -0
  22. package/src/card/enum/funding.ts +11 -0
  23. package/src/card/enum/nature.ts +7 -0
  24. package/src/card/enum/network.ts +16 -0
  25. package/src/card/model.ts +139 -0
  26. package/src/card/repository.ts +11 -0
  27. package/src/card/validator.ts +79 -0
  28. package/src/core/abstract-model.ts +126 -0
  29. package/src/core/abstract-repository.ts +201 -0
  30. package/src/core/common/branded-type.ts +15 -0
  31. package/src/core/common/handler/date-handler.ts +31 -0
  32. package/src/core/common/handler/enum-handler.ts +18 -0
  33. package/src/core/common/handler/handler.ts +64 -0
  34. package/src/core/common/handler/id-handler.ts +27 -0
  35. package/src/core/common/id-validator.ts +23 -0
  36. package/src/core/common/mutation-tracked-object.ts +49 -0
  37. package/src/core/endpoint-interface.ts +43 -0
  38. package/src/core/error/base-error.ts +1 -0
  39. package/src/core/error/endpoint/api/api-conflict-error.ts +7 -0
  40. package/src/core/error/endpoint/api/api-error.ts +10 -0
  41. package/src/core/error/endpoint/api/api-not-found-error.ts +7 -0
  42. package/src/core/error/endpoint/api/api-unauthorized-error.ts +7 -0
  43. package/src/core/error/endpoint/api/api-unknown-error.ts +7 -0
  44. package/src/core/error/endpoint/api/api-validation-error.ts +13 -0
  45. package/src/core/error/endpoint/api/index.ts +6 -0
  46. package/src/core/error/endpoint/route-inaccessible-error.ts +8 -0
  47. package/src/core/error/unpopulated-error.ts +3 -0
  48. package/src/core/error/unsendable-error.ts +3 -0
  49. package/src/core/error/validation-error.ts +3 -0
  50. package/src/core/validator.ts +129 -0
  51. package/src/customer/api-endpoint.ts +29 -0
  52. package/src/customer/model.ts +113 -0
  53. package/src/customer/repository.ts +14 -0
  54. package/src/customer/validator.ts +26 -0
  55. package/src/payment/api-endpoint.ts +118 -0
  56. package/src/payment/enum/currency.ts +14 -0
  57. package/src/payment/enum/method.ts +2 -0
  58. package/src/payment/enum/status.ts +21 -0
  59. package/src/payment/model.ts +378 -0
  60. package/src/payment/repository.ts +73 -0
  61. package/src/payment/validator.ts +96 -0
  62. package/src/payment-intent/api-endpoint.ts +123 -0
  63. package/src/payment-intent/enum/status.ts +21 -0
  64. package/src/payment-intent/enum/threeds.ts +2 -0
  65. package/src/payment-intent/model.ts +266 -0
  66. package/src/payment-intent/repository.ts +85 -0
  67. package/src/payment-intent/validator.ts +45 -0
  68. package/src/payout/model.ts +8 -0
  69. package/src/refund/api-endpoint.ts +33 -0
  70. package/src/refund/enum/refund-status.ts +11 -0
  71. package/src/refund/model.ts +90 -0
  72. package/src/refund/repository.ts +14 -0
  73. package/src/refund/validator.ts +44 -0
  74. package/src/sepa/api-endpoint.ts +26 -0
  75. package/src/sepa/model.ts +80 -0
  76. package/src/sepa/repository.ts +11 -0
  77. package/src/sepa/validator.ts +56 -0
  78. package/test/fixtures/endpoint.stub.ts +22 -0
  79. package/test/fixtures/list-endpoint.stub.ts +33 -0
  80. package/test/fixtures/list-model.stub.ts +24 -0
  81. package/test/fixtures/model.stub.ts +12 -0
  82. package/test/functional/address.test.ts +126 -0
  83. package/test/functional/card.test.ts +175 -0
  84. package/test/functional/customer.test.ts +174 -0
  85. package/test/functional/payment-intent.test.ts +215 -0
  86. package/test/functional/payment.test.ts +206 -0
  87. package/test/functional/refund.test.ts +112 -0
  88. package/test/functional/sepa.test.ts +136 -0
  89. package/test/integration/stub-integration.test.ts +88 -0
  90. package/test/stub/stub-endpoint.ts +42 -0
  91. package/test/stub/stub-model.ts +82 -0
  92. package/test/stub/stub-repository.ts +11 -0
  93. package/test/test-helper.ts +133 -0
  94. package/test/unit/address/api-endpoint.test.ts +134 -0
  95. package/test/unit/address/fixtures.ts +60 -0
  96. package/test/unit/address/model.test.ts +75 -0
  97. package/test/unit/address/repository.test.ts +148 -0
  98. package/test/unit/address/validator.test.ts +49 -0
  99. package/test/unit/api-endpoint/http/fetch-http-client.test.ts +44 -0
  100. package/test/unit/api-endpoint/http/http.test.ts +152 -0
  101. package/test/unit/card/api-endpoint.test.ts +170 -0
  102. package/test/unit/card/card-helper.ts +116 -0
  103. package/test/unit/card/fixtures.ts +87 -0
  104. package/test/unit/card/model.test.ts +167 -0
  105. package/test/unit/card/repository.test.ts +146 -0
  106. package/test/unit/card/validator.test.ts +96 -0
  107. package/test/unit/core/common/mutation-tracked-object.test.ts +24 -0
  108. package/test/unit/customer/api-endpoint.test.ts +143 -0
  109. package/test/unit/customer/fixtures.ts +73 -0
  110. package/test/unit/customer/model.test.ts +90 -0
  111. package/test/unit/customer/repository.test.ts +148 -0
  112. package/test/unit/customer/validator.test.ts +62 -0
  113. package/test/unit/payment/api-endpoint.test.ts +188 -0
  114. package/test/unit/payment/fixtures.ts +116 -0
  115. package/test/unit/payment/model.test.ts +240 -0
  116. package/test/unit/payment/repository.test.ts +270 -0
  117. package/test/unit/payment/validator.test.ts +137 -0
  118. package/test/unit/payment-intent/api-endpoint.test.ts +142 -0
  119. package/test/unit/payment-intent/fixtures.ts +94 -0
  120. package/test/unit/payment-intent/model.test.ts +157 -0
  121. package/test/unit/payment-intent/repository.test.ts +263 -0
  122. package/test/unit/payment-intent/validator.test.ts +136 -0
  123. package/test/unit/refund/api-endpoint.test.ts +129 -0
  124. package/test/unit/refund/fixtures.ts +58 -0
  125. package/test/unit/refund/model.test.ts +89 -0
  126. package/test/unit/refund/repository.test.ts +157 -0
  127. package/test/unit/refund/validator.test.ts +89 -0
  128. package/test/unit/sepa/api-endpoint.test.ts +170 -0
  129. package/test/unit/sepa/fixture.ts +62 -0
  130. package/test/unit/sepa/model.test.ts +86 -0
  131. package/test/unit/sepa/repository.test.ts +149 -0
  132. package/test/unit/sepa/sepa-helper.ts +71 -0
  133. package/test/unit/sepa/validator.test.ts +58 -0
  134. package/test/unit/stub/stub-endpoint.test.ts +236 -0
  135. package/test/unit/stub/stub-list-endpoint.test.ts +86 -0
  136. package/test/unit/stub/stub-model.test.ts +111 -0
  137. package/test/unit/stub/stub-repository.test.ts +144 -0
  138. package/tsconfig.json +50 -0
  139. package/vitest.config.ts +20 -0
@@ -0,0 +1,111 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { StancerValidationError } from '#error/validation-error.js';
4
+ import { IdStub, StubData, StubModel, StubValidator } from '#test/stub/stub-model.js';
5
+ import { randomNumber, randomString } from '#test/test-helper.js';
6
+
7
+ describe('stub model', () => {
8
+ describe('constructor', () => {
9
+ it('should be created without anything', () => {
10
+ const stub = new StubModel(StubValidator);
11
+ expect(stub.id).toBeUndefined();
12
+ expect(stub._data).toEqual({});
13
+ });
14
+
15
+ it('should be created with good ID', () => {
16
+ const id = 'stub_' + randomString(24, 24);
17
+ const stub = new StubModel(StubValidator, id);
18
+ expect(stub.id).toEqual(id);
19
+ expect(stub._data).toEqual({ id: id });
20
+ expect(stub._data.getMutations()).toStrictEqual({ id });
21
+ });
22
+
23
+ it("shouldn't be created with bad ID", () => {
24
+ const id = randomString(22, 24);
25
+ expect(() => new StubModel(StubValidator, id)).toThrow();
26
+ });
27
+
28
+ it("shouldn't accept custom bad data", () => {
29
+ const data: Partial<StubData> = {
30
+ id: randomString(24, 25) as IdStub,
31
+ default: randomString(12, 25),
32
+ };
33
+ expect(() => new StubModel(StubValidator, data)).toThrow();
34
+ });
35
+
36
+ it('should accept custom good data', () => {
37
+ const id = ('stub_' + randomString(24)) as IdStub;
38
+ const defaultValue = randomString(28, 32);
39
+ const data: Partial<StubData> = {
40
+ id,
41
+ default: defaultValue,
42
+ };
43
+ const stub = new StubModel(StubValidator, data);
44
+ expect(stub._data).toEqual(data);
45
+ });
46
+ });
47
+
48
+ describe('Id setter', () => {
49
+ it('should accept good Id', () => {
50
+ const id = 'stub_' + randomString(24, 24);
51
+ const stub = new StubModel(StubValidator, id);
52
+ expect(stub._data.id).toStrictEqual(id);
53
+ expect(stub.id).toStrictEqual(id);
54
+ });
55
+
56
+ it('should refuse bad ID', () => {
57
+ const falseId = randomString(12, 24);
58
+ expect(() => new StubModel(StubValidator, falseId)).toThrow(StancerValidationError);
59
+ });
60
+ });
61
+
62
+ describe('default setter', () => {
63
+ it('should accept good string length', () => {
64
+ const stub = new StubModel(StubValidator);
65
+ const defaultValue = randomString(1, 63);
66
+ stub.setDefault(defaultValue);
67
+ expect(stub.default).toBe(defaultValue);
68
+ });
69
+
70
+ it("shouldn't accept bad value", () => {
71
+ const stub = new StubModel(StubValidator);
72
+ //We want to hard prevent bad typing
73
+ expect(() => stub.setDefault(12)).toThrow();
74
+ expect(() => stub.setDefault(randomString(65, 100))).toThrow();
75
+ });
76
+ });
77
+
78
+ describe('DefaultNumber setter', () => {
79
+ it('should accept good number', () => {
80
+ const stub = new StubModel(StubValidator);
81
+ const defaultNumber = randomNumber(1, 263);
82
+ stub.setDefaultNumber(defaultNumber);
83
+ expect(stub.defaultNumber).toBe(defaultNumber);
84
+ });
85
+
86
+ it("shouldn't accept bad value", () => {
87
+ const stub = new StubModel(StubValidator);
88
+ const defaultNumber = randomNumber(265, 30000);
89
+ expect(() => stub.setDefaultNumber(defaultNumber)).toThrow();
90
+ expect(() => stub.setDefaultNumber('string')).toThrow();
91
+ });
92
+ });
93
+
94
+ describe('boolean property', () => {
95
+ it('should accept boolean', () => {
96
+ const stub = new StubModel(StubValidator);
97
+ const boolValue = true;
98
+ stub.setUngettable(boolValue);
99
+ expect(stub._data.ungettable).toBe(boolValue);
100
+ stub.setAllAuthorized(boolValue);
101
+ expect(stub.allAuthorized).toBe(boolValue);
102
+ });
103
+
104
+ it('should have gettable property', () => {
105
+ const stub = new StubModel(StubValidator);
106
+ // direct accession to _data is not validated
107
+ stub._data.unmodifiable = true;
108
+ expect(stub.unmodifiable).toBe(true);
109
+ });
110
+ });
111
+ });
@@ -0,0 +1,144 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+
3
+ import { AbstractRepository } from '#core/abstract-repository.js';
4
+ import { IdValue } from '#core/common/branded-type.js';
5
+ import { StancerError } from '#error/base-error.js';
6
+ import { StancerRouteInaccessibleError } from '#error/endpoint/route-inaccessible-error.js';
7
+ import ListModelFixtures from '#test/fixtures/list-model.stub.js';
8
+ import ModelFixtures from '#test/fixtures/model.stub.js';
9
+ import { StubEndpoint } from '#test/stub/stub-endpoint.js';
10
+ import { IdStub, StubModel, StubValidator } from '#test/stub/stub-model.js';
11
+ import { StubRepository } from '#test/stub/stub-repository.js';
12
+ import { randomNumber, randomString } from '#test/test-helper.js';
13
+
14
+ const stancerErrorID = 'stub_unidentifiedError' + randomString(7);
15
+ const stancerRouteInaccessibleId = 'stub_StancerRouteInnacessible';
16
+ const dataHasBeenSentId = 'stub_DataHasBeenSent' + randomString(9);
17
+ const dataUnmodified = 'stub_unmodifiedData' + randomString(10);
18
+ class MockApiEndpoint {
19
+ constructor() {}
20
+ list() {
21
+ async function* listGenerator() {
22
+ yield* ListModelFixtures;
23
+ }
24
+ return listGenerator();
25
+ }
26
+ delete() {
27
+ return ModelFixtures;
28
+ }
29
+ send(id: IdValue, data: Record<string, unknown>) {
30
+ if (id) {
31
+ if (id === stancerErrorID) {
32
+ throw new StancerError();
33
+ }
34
+ if (id === stancerRouteInaccessibleId) {
35
+ throw new StancerRouteInaccessibleError('PATCH', 'stub');
36
+ }
37
+ if (id === dataUnmodified) {
38
+ return;
39
+ }
40
+ }
41
+ return { ...data, id: dataHasBeenSentId };
42
+ }
43
+ get() {
44
+ return ModelFixtures;
45
+ }
46
+ }
47
+ it('can be instanciated', () => {
48
+ const instance = new StubRepository();
49
+ expect(instance).toBeInstanceOf(StubRepository);
50
+ expect(instance).toBeInstanceOf(AbstractRepository);
51
+ expect(instance.endpoint).toBeInstanceOf(StubEndpoint);
52
+ expect(instance.validator).toBe(StubValidator);
53
+ expect(instance.staticModel).toBe(StubModel);
54
+ });
55
+
56
+ describe('repository', () => {
57
+ const mockRepo = new MockApiEndpoint() as unknown as StubEndpoint; // I lie, for convenience sake
58
+ const repository = new StubRepository(mockRepo);
59
+
60
+ describe('create', () => {
61
+ it("doesn't accept bad data", async () => {
62
+ const badID = randomString(30, 56);
63
+ await expect(async () => await repository.create(badID)).rejects.toThrow();
64
+ const badID2 = randomNumber(1, 12);
65
+ await expect(async () => await repository.create(badID2)).rejects.toThrow();
66
+ const badData = { default: randomNumber(1, 12) };
67
+ await expect(async () => await repository.create(badData)).rejects.toThrow();
68
+ });
69
+
70
+ it('accept good data', async () => {
71
+ const id = 'stub_' + randomString(24);
72
+ const model = await repository.create(id);
73
+ expect(model).toBeInstanceOf(StubModel);
74
+ });
75
+ });
76
+
77
+ it('delete', async () => {
78
+ const modelWithoutId = await repository.create(ModelFixtures);
79
+ await expect(() => repository.delete(modelWithoutId)).rejects.toThrow();
80
+
81
+ const model = await repository.create({ ...ModelFixtures, id: ('stub_' + randomString(24)) as IdStub });
82
+ await expect((await repository.delete(model))._data).toStrictEqual(ModelFixtures);
83
+ });
84
+
85
+ describe('send', () => {
86
+ it('should return model', async () => {
87
+ const model = await repository.create(ModelFixtures);
88
+ const sentModel = await repository.send(model);
89
+ expect(sentModel).toStrictEqual(model);
90
+ expect(sentModel._data.id).toEqual(dataHasBeenSentId);
91
+ });
92
+
93
+ it('should throw on inprecise error', async () => {
94
+ const model = await repository.create({ ...ModelFixtures, id: stancerErrorID as IdStub });
95
+ await expect(() => repository.send(model)).rejects.toThrow(StancerError);
96
+ });
97
+
98
+ it('should return error on StancerRouteInacessibleError', async () => {
99
+ const model = await repository.create({ ...ModelFixtures, id: stancerRouteInaccessibleId as IdStub });
100
+ await expect(async () => await repository.send(model)).rejects.toThrow(StancerRouteInaccessibleError);
101
+ });
102
+
103
+ it('should return unmodified model when no new Data', async () => {
104
+ const model = await repository.create({ ...ModelFixtures, id: dataUnmodified as IdStub });
105
+ const sentModel = await repository.send(model);
106
+ expect(model).toStrictEqual(sentModel);
107
+ expect(sentModel._data.id).toEqual(dataUnmodified);
108
+ });
109
+ });
110
+
111
+ describe('populate', () => {
112
+ it('should not modify model if no Id', async () => {
113
+ const getEndpoint = vi.spyOn(mockRepo, 'get');
114
+ const model = await repository.create(ModelFixtures);
115
+ expect(model).toBeInstanceOf(StubModel);
116
+ expect(getEndpoint).toHaveBeenCalledTimes(0);
117
+ });
118
+
119
+ it('should get model if only id', async () => {
120
+ const getEndpoint = vi.spyOn(mockRepo, 'get');
121
+ const model = await repository.create(('stub_' + randomString(24)) as IdStub);
122
+ expect(model).toBeInstanceOf(StubModel);
123
+ expect(getEndpoint).toHaveBeenCalledTimes(1);
124
+ });
125
+ });
126
+
127
+ describe('list', async () => {
128
+ it('return correct model one by one', async () => {
129
+ const listStub = repository.list();
130
+ const item1 = await listStub.next();
131
+ expect(item1.done).toBeFalsy();
132
+ expect(item1.value).toHaveProperty('id');
133
+ expect(item1.value).toBeInstanceOf(StubModel);
134
+
135
+ const item2 = await listStub.next();
136
+ expect(item2.done).toBeFalsy();
137
+ expect(item2.value).toHaveProperty('id');
138
+ expect(item2.value).toBeInstanceOf(StubModel);
139
+
140
+ const item3 = await listStub.next();
141
+ expect(item3.done).toBeTruthy();
142
+ });
143
+ });
144
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "declarationMap": true,
5
+ "esModuleInterop": true,
6
+ "exactOptionalPropertyTypes": true,
7
+ "isolatedModules": true,
8
+ "lib": [
9
+ "ESNext"
10
+ ],
11
+ "module": "NodeNext",
12
+ "moduleDetection": "force",
13
+ "noFallthroughCasesInSwitch": true,
14
+ "noImplicitOverride": true,
15
+ "noImplicitReturns": true,
16
+ "noPropertyAccessFromIndexSignature": true,
17
+ "noUncheckedIndexedAccess": true,
18
+ "noUncheckedSideEffectImports": true,
19
+ "noUnusedLocals": true,
20
+ "noUnusedParameters": true,
21
+ "outDir": "./dist",
22
+ "paths": {
23
+ "#api/*": [
24
+ "./src/api-endpoint/*"
25
+ ],
26
+ "#error/*": [
27
+ "./src/core/error/*"
28
+ ],
29
+ "#core/*": [
30
+ "./src/core/*"
31
+ ],
32
+ "#test/*": [
33
+ "./test/*"
34
+ ]
35
+ },
36
+ "resolveJsonModule": true,
37
+ "rootDir": "./src",
38
+ "skipLibCheck": true,
39
+ "sourceMap": true,
40
+ "strict": true,
41
+ "target": "ESNext",
42
+ "types": [
43
+ "node"
44
+ ],
45
+ "verbatimModuleSyntax": true
46
+ },
47
+ "include": [
48
+ "./src/"
49
+ ]
50
+ }
@@ -0,0 +1,20 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ coverage: {
6
+ enabled: true,
7
+ exclude: [
8
+ '**/dist',
9
+ '**/*.config.ts',
10
+ '**/*.ignore.ts',
11
+ '**/*.js',
12
+ ],
13
+ provider: 'v8',
14
+ reporter: ['cobertura', 'html', 'text'],
15
+ reportsDirectory: 'reports/',
16
+ },
17
+ outputFile: 'reports/junit.xml',
18
+ reporters: ['default', 'junit'],
19
+ },
20
+ });