@stancer/stancer 0.0.0-stage → 1.0.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/.editorconfig +14 -0
- package/.gitlab-ci.yml +159 -0
- package/.prettierignore +3 -0
- package/CHANGELOG.md +14 -0
- package/LICENSE +19 -0
- package/README.md +166 -2
- package/eslint.config.js +14 -0
- package/package.json +58 -3
- package/pnpm-workspace.yaml +19 -0
- package/prettier.config.js +3 -0
- package/src/address/api-endpoint.ts +30 -0
- package/src/address/model.ts +79 -0
- package/src/address/repository.ts +18 -0
- package/src/address/validator.ts +34 -0
- package/src/api-endpoint/abstract-api-endpoint.ts +310 -0
- package/src/api-endpoint/configuration.ts +125 -0
- package/src/api-endpoint/http/fetch-http-client.ts +29 -0
- package/src/api-endpoint/http/http.ts +75 -0
- package/src/api-endpoint/http/index.ts +2 -0
- package/src/api-endpoint/version.ts +4 -0
- package/src/card/api-endpoint.ts +39 -0
- package/src/card/enum/funding.ts +11 -0
- package/src/card/enum/nature.ts +7 -0
- package/src/card/enum/network.ts +16 -0
- package/src/card/model.ts +139 -0
- package/src/card/repository.ts +11 -0
- package/src/card/validator.ts +79 -0
- package/src/core/abstract-model.ts +126 -0
- package/src/core/abstract-repository.ts +201 -0
- package/src/core/common/branded-type.ts +15 -0
- package/src/core/common/handler/date-handler.ts +31 -0
- package/src/core/common/handler/enum-handler.ts +18 -0
- package/src/core/common/handler/handler.ts +64 -0
- package/src/core/common/handler/id-handler.ts +27 -0
- package/src/core/common/id-validator.ts +23 -0
- package/src/core/common/mutation-tracked-object.ts +49 -0
- package/src/core/endpoint-interface.ts +43 -0
- package/src/core/error/base-error.ts +1 -0
- package/src/core/error/endpoint/api/api-conflict-error.ts +7 -0
- package/src/core/error/endpoint/api/api-error.ts +10 -0
- package/src/core/error/endpoint/api/api-not-found-error.ts +7 -0
- package/src/core/error/endpoint/api/api-unauthorized-error.ts +7 -0
- package/src/core/error/endpoint/api/api-unknown-error.ts +7 -0
- package/src/core/error/endpoint/api/api-validation-error.ts +13 -0
- package/src/core/error/endpoint/api/index.ts +6 -0
- package/src/core/error/endpoint/route-inaccessible-error.ts +8 -0
- package/src/core/error/unpopulated-error.ts +3 -0
- package/src/core/error/unsendable-error.ts +3 -0
- package/src/core/error/validation-error.ts +3 -0
- package/src/core/validator.ts +129 -0
- package/src/customer/api-endpoint.ts +29 -0
- package/src/customer/model.ts +113 -0
- package/src/customer/repository.ts +14 -0
- package/src/customer/validator.ts +26 -0
- package/src/payment/api-endpoint.ts +118 -0
- package/src/payment/enum/currency.ts +14 -0
- package/src/payment/enum/method.ts +2 -0
- package/src/payment/enum/status.ts +21 -0
- package/src/payment/model.ts +378 -0
- package/src/payment/repository.ts +73 -0
- package/src/payment/validator.ts +96 -0
- package/src/payment-intent/api-endpoint.ts +123 -0
- package/src/payment-intent/enum/status.ts +21 -0
- package/src/payment-intent/enum/threeds.ts +2 -0
- package/src/payment-intent/model.ts +266 -0
- package/src/payment-intent/repository.ts +85 -0
- package/src/payment-intent/validator.ts +45 -0
- package/src/payout/model.ts +8 -0
- package/src/refund/api-endpoint.ts +33 -0
- package/src/refund/enum/refund-status.ts +11 -0
- package/src/refund/model.ts +90 -0
- package/src/refund/repository.ts +14 -0
- package/src/refund/validator.ts +44 -0
- package/src/sepa/api-endpoint.ts +26 -0
- package/src/sepa/model.ts +80 -0
- package/src/sepa/repository.ts +11 -0
- package/src/sepa/validator.ts +56 -0
- package/test/fixtures/endpoint.stub.ts +22 -0
- package/test/fixtures/list-endpoint.stub.ts +33 -0
- package/test/fixtures/list-model.stub.ts +24 -0
- package/test/fixtures/model.stub.ts +12 -0
- package/test/functional/address.test.ts +126 -0
- package/test/functional/card.test.ts +175 -0
- package/test/functional/customer.test.ts +174 -0
- package/test/functional/payment-intent.test.ts +215 -0
- package/test/functional/payment.test.ts +206 -0
- package/test/functional/refund.test.ts +112 -0
- package/test/functional/sepa.test.ts +136 -0
- package/test/integration/stub-integration.test.ts +88 -0
- package/test/stub/stub-endpoint.ts +42 -0
- package/test/stub/stub-model.ts +82 -0
- package/test/stub/stub-repository.ts +11 -0
- package/test/test-helper.ts +133 -0
- package/test/unit/address/api-endpoint.test.ts +134 -0
- package/test/unit/address/fixtures.ts +60 -0
- package/test/unit/address/model.test.ts +75 -0
- package/test/unit/address/repository.test.ts +148 -0
- package/test/unit/address/validator.test.ts +49 -0
- package/test/unit/api-endpoint/http/fetch-http-client.test.ts +44 -0
- package/test/unit/api-endpoint/http/http.test.ts +152 -0
- package/test/unit/card/api-endpoint.test.ts +170 -0
- package/test/unit/card/card-helper.ts +116 -0
- package/test/unit/card/fixtures.ts +87 -0
- package/test/unit/card/model.test.ts +167 -0
- package/test/unit/card/repository.test.ts +146 -0
- package/test/unit/card/validator.test.ts +96 -0
- package/test/unit/core/common/mutation-tracked-object.test.ts +24 -0
- package/test/unit/customer/api-endpoint.test.ts +143 -0
- package/test/unit/customer/fixtures.ts +73 -0
- package/test/unit/customer/model.test.ts +90 -0
- package/test/unit/customer/repository.test.ts +148 -0
- package/test/unit/customer/validator.test.ts +62 -0
- package/test/unit/payment/api-endpoint.test.ts +188 -0
- package/test/unit/payment/fixtures.ts +116 -0
- package/test/unit/payment/model.test.ts +240 -0
- package/test/unit/payment/repository.test.ts +270 -0
- package/test/unit/payment/validator.test.ts +137 -0
- package/test/unit/payment-intent/api-endpoint.test.ts +142 -0
- package/test/unit/payment-intent/fixtures.ts +94 -0
- package/test/unit/payment-intent/model.test.ts +157 -0
- package/test/unit/payment-intent/repository.test.ts +263 -0
- package/test/unit/payment-intent/validator.test.ts +136 -0
- package/test/unit/refund/api-endpoint.test.ts +129 -0
- package/test/unit/refund/fixtures.ts +58 -0
- package/test/unit/refund/model.test.ts +89 -0
- package/test/unit/refund/repository.test.ts +157 -0
- package/test/unit/refund/validator.test.ts +89 -0
- package/test/unit/sepa/api-endpoint.test.ts +170 -0
- package/test/unit/sepa/fixture.ts +62 -0
- package/test/unit/sepa/model.test.ts +86 -0
- package/test/unit/sepa/repository.test.ts +149 -0
- package/test/unit/sepa/sepa-helper.ts +71 -0
- package/test/unit/sepa/validator.test.ts +58 -0
- package/test/unit/stub/stub-endpoint.test.ts +236 -0
- package/test/unit/stub/stub-list-endpoint.test.ts +86 -0
- package/test/unit/stub/stub-model.test.ts +111 -0
- package/test/unit/stub/stub-repository.test.ts +144 -0
- package/tsconfig.json +50 -0
- package/vitest.config.ts +20 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { AbstractModel, type ModelData } from '#core/abstract-model.js';
|
|
2
|
+
import type { IdValue } from '#core/common/branded-type.js';
|
|
3
|
+
import { validateID } from '#core/common/id-validator.js';
|
|
4
|
+
|
|
5
|
+
import type { IdCustomer } from '../customer/model.js';
|
|
6
|
+
import { type FundingCard } from './enum/funding.js';
|
|
7
|
+
import type { NatureCard } from './enum/nature.js';
|
|
8
|
+
import type { NetworkCard, PreferredNetworkCard } from './enum/network.js';
|
|
9
|
+
|
|
10
|
+
export type IdCard = IdValue<`card_${string}`>;
|
|
11
|
+
export const idCard = (id: string) => validateID<IdCard>(id, CardModel.regex);
|
|
12
|
+
export interface CardData extends ModelData {
|
|
13
|
+
brand: string;
|
|
14
|
+
country: string;
|
|
15
|
+
customer: IdCustomer;
|
|
16
|
+
cvc: string;
|
|
17
|
+
deleted: boolean;
|
|
18
|
+
expMonth: number;
|
|
19
|
+
expYear: number;
|
|
20
|
+
externalId: string;
|
|
21
|
+
funding: FundingCard;
|
|
22
|
+
id: IdCard;
|
|
23
|
+
last4: string;
|
|
24
|
+
name: string;
|
|
25
|
+
nature: NatureCard;
|
|
26
|
+
network: NetworkCard;
|
|
27
|
+
number: string;
|
|
28
|
+
preferredNetwork: PreferredNetworkCard;
|
|
29
|
+
tokenize: boolean;
|
|
30
|
+
zipCode: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class CardModel extends AbstractModel<CardData> {
|
|
34
|
+
static regex = /^card_[a-zA-Z0-9]{24}$/;
|
|
35
|
+
|
|
36
|
+
get customer() {
|
|
37
|
+
return this._data.customer;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get brand() {
|
|
41
|
+
return this._data.brand;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get country() {
|
|
45
|
+
return this._data.country;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get expMonth() {
|
|
49
|
+
return this._data.expMonth;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get expYear() {
|
|
53
|
+
return this._data.expYear;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get externalId() {
|
|
57
|
+
return this._data.externalId;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get funding() {
|
|
61
|
+
return this._data.funding;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get last4() {
|
|
65
|
+
if (!this._data.last4 && this._data.number) {
|
|
66
|
+
return this._data.number.slice(-4);
|
|
67
|
+
}
|
|
68
|
+
return this._data.last4;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
get name() {
|
|
72
|
+
return this._data.name;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get nature() {
|
|
76
|
+
return this._data.nature;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get preferredNetwork() {
|
|
80
|
+
return this._data.preferredNetwork;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
get network() {
|
|
84
|
+
return this._data.network;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get zipCode() {
|
|
88
|
+
return this._data.zipCode;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
setCustomer(value: IdCustomer) {
|
|
92
|
+
this._data.customer = value;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
setCvc(value: string) {
|
|
97
|
+
this._data.cvc = value;
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setExpYear(value: number) {
|
|
102
|
+
this._data.expYear = value;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
setExpMonth(value: number) {
|
|
107
|
+
this._data.expMonth = value;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
setExternalId(value: string) {
|
|
111
|
+
this._data.externalId = value;
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
setName(value: string) {
|
|
116
|
+
this._data.name = value;
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
setNumber(value: string) {
|
|
121
|
+
this._data.number = value;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
setPreferredNetwork(value: PreferredNetworkCard) {
|
|
126
|
+
this._data.preferredNetwork = value;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
setTokenize(value: boolean) {
|
|
131
|
+
this._data.tokenize = value;
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setZipCode(value: string) {
|
|
136
|
+
this._data.zipCode = value;
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CardApiEndpoint } from '#card/api-endpoint.js';
|
|
2
|
+
import { type CardData, CardModel } from '#card/model.js';
|
|
3
|
+
import { cardValidator } from '#card/validator.js';
|
|
4
|
+
import { AbstractRepository, type Repository } from '#core/abstract-repository.js';
|
|
5
|
+
import type { EndpointInterface } from '#core/endpoint-interface.js';
|
|
6
|
+
|
|
7
|
+
export class CardRepository extends AbstractRepository<CardData, CardModel> implements Repository<CardData, CardModel> {
|
|
8
|
+
constructor(endpoint?: EndpointInterface<CardData>) {
|
|
9
|
+
super({ endpoint: endpoint ?? new CardApiEndpoint(), validator: cardValidator, model: CardModel });
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { IdValidator } from '#core/common/id-validator.js';
|
|
2
|
+
import { type Validator, createdAtValidator, enumValidator, goldilocksValidator } from '#core/validator.js';
|
|
3
|
+
|
|
4
|
+
import { CustomerModel, type IdCustomer } from '../customer/model.js';
|
|
5
|
+
import { FundingCardEnum } from './enum/funding.js';
|
|
6
|
+
import { NatureCardEnum } from './enum/nature.js';
|
|
7
|
+
import { NetworkCardEnum, PreferredNetworkCardEnum } from './enum/network.js';
|
|
8
|
+
import { type CardData, CardModel, type IdCard } from './model.js';
|
|
9
|
+
|
|
10
|
+
const expMonthValidator = {
|
|
11
|
+
validate: (arg0: number) => typeof arg0 === 'number' && arg0 > 0 && arg0 <= 12,
|
|
12
|
+
errorMessage: (value: string) => `${value} is not a month (between 1 and 12)`,
|
|
13
|
+
};
|
|
14
|
+
const expYearValidator = {
|
|
15
|
+
validate: (arg0: number) => typeof arg0 === 'number' && String(arg0).length === 4,
|
|
16
|
+
errorMessage: (value: string) => `${value} is not a correct year (must be 4 digits)`,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const cardNumberValidator = {
|
|
20
|
+
validate: (arg0: string) => {
|
|
21
|
+
if (typeof arg0 !== 'string') {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const numb = arg0.replaceAll(/\s/g, '').replaceAll(/\D/g, '');
|
|
26
|
+
|
|
27
|
+
if (!numb) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const reversed: string[] = numb.split('').reverse();
|
|
32
|
+
const calc = (digit: number) => {
|
|
33
|
+
const number = digit * 2;
|
|
34
|
+
if (number < 10) {
|
|
35
|
+
return number;
|
|
36
|
+
}
|
|
37
|
+
const splited = String(number).split('');
|
|
38
|
+
return Number(splited[0]) + Number(splited[1]);
|
|
39
|
+
};
|
|
40
|
+
const sum = reversed.reduce((n, sum, index) => {
|
|
41
|
+
if (index % 2) {
|
|
42
|
+
return String(Number(n) + calc(Number(sum)));
|
|
43
|
+
}
|
|
44
|
+
return String(Number(n) + Number(sum));
|
|
45
|
+
}, '0');
|
|
46
|
+
|
|
47
|
+
if (Number(sum) % 10) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return true;
|
|
52
|
+
},
|
|
53
|
+
errorMessage: (value: string) => `${value} is not a valid credit card number`,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const cardValidator: Validator<CardData> = {
|
|
57
|
+
id: IdValidator<IdCard>(CardModel.regex),
|
|
58
|
+
brand: true,
|
|
59
|
+
country: goldilocksValidator('country', { size: 2 }),
|
|
60
|
+
customer: IdValidator<IdCustomer>(CustomerModel.regex),
|
|
61
|
+
cvc: {
|
|
62
|
+
validate: (arg0: string) => /^\d{3}$/.test(arg0),
|
|
63
|
+
errorMessage: (value: string) => `${value} is not a valid CVC (must be 3 digits)`,
|
|
64
|
+
},
|
|
65
|
+
createdAt: createdAtValidator,
|
|
66
|
+
deleted: true,
|
|
67
|
+
expMonth: expMonthValidator,
|
|
68
|
+
expYear: expYearValidator,
|
|
69
|
+
externalId: goldilocksValidator('externalID', { min: 1, max: 36 }),
|
|
70
|
+
funding: enumValidator(FundingCardEnum, false),
|
|
71
|
+
last4: true,
|
|
72
|
+
name: goldilocksValidator('name', { min: 1, max: 64 }),
|
|
73
|
+
nature: enumValidator(NatureCardEnum, false),
|
|
74
|
+
network: enumValidator(NetworkCardEnum, false),
|
|
75
|
+
number: cardNumberValidator,
|
|
76
|
+
preferredNetwork: enumValidator(PreferredNetworkCardEnum),
|
|
77
|
+
tokenize: true,
|
|
78
|
+
zipCode: goldilocksValidator('zipCode', { min: 0, max: 8 }),
|
|
79
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { Temporal } from 'temporal-polyfill';
|
|
2
|
+
|
|
3
|
+
import { type ValidationFunction, type Validator, processValidator } from '#core/validator.js';
|
|
4
|
+
|
|
5
|
+
import { IdValue } from './common/branded-type.js';
|
|
6
|
+
import { type MutationTrackedObject, trackObjectMutations } from './common/mutation-tracked-object.js';
|
|
7
|
+
|
|
8
|
+
//#region Types & Interfaces
|
|
9
|
+
|
|
10
|
+
export interface ModelData {
|
|
11
|
+
createdAt: Temporal.Instant;
|
|
12
|
+
deleted: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ModelType extends ModelData {
|
|
16
|
+
id: IdValue;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* All setters and getters of AbstractModel and their children will be inconsistent.
|
|
23
|
+
* This is intentional to help users use only the values accessible to them.
|
|
24
|
+
*/
|
|
25
|
+
export abstract class AbstractModel<Data extends ModelType> {
|
|
26
|
+
//#region Properties
|
|
27
|
+
|
|
28
|
+
protected _data: MutationTrackedObject<Partial<Data>>;
|
|
29
|
+
protected _id: Data['id'] | undefined;
|
|
30
|
+
protected validator: ValidationFunction<Data>;
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
|
|
34
|
+
//#region Constructor
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Construct a model object.
|
|
38
|
+
* Prefer using repository.create() instead of constructing it directly.
|
|
39
|
+
* @param validator - The validator function for this model
|
|
40
|
+
* @param id - Optional ID string or partial data object
|
|
41
|
+
*/
|
|
42
|
+
constructor(validator: Validator<Data>, id?: string | Partial<Data>) {
|
|
43
|
+
this.validator = processValidator(validator);
|
|
44
|
+
this._data = trackObjectMutations<Data>({}, this.validator);
|
|
45
|
+
|
|
46
|
+
if (typeof id === 'object' && id !== null) {
|
|
47
|
+
const data = id;
|
|
48
|
+
const keys = Object.keys(data) as (keyof Partial<Data>)[];
|
|
49
|
+
keys.forEach((key) => {
|
|
50
|
+
if (data[key] === undefined) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this.validator(key, data[key]);
|
|
54
|
+
(this._data[key] as Partial<Data>[keyof Data]) = data[key];
|
|
55
|
+
});
|
|
56
|
+
this._id = data.id ?? undefined;
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (id) {
|
|
61
|
+
const stringID = IdValue(id);
|
|
62
|
+
this.validator('id', stringID);
|
|
63
|
+
this._id = stringID;
|
|
64
|
+
this._data['id'] = this._id;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
|
|
70
|
+
//#region Public Methods
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Recreate a DataTrackObject with no mutations.
|
|
74
|
+
* Call when data has been synced and is no longer considered mutated.
|
|
75
|
+
* @param data - The data to be tracked
|
|
76
|
+
*/
|
|
77
|
+
public dataTrackMutations(data: Partial<Data>): void {
|
|
78
|
+
const keys = Object.keys(data) as (keyof Partial<Data>)[];
|
|
79
|
+
keys.forEach((key) => {
|
|
80
|
+
if (data[key] === undefined) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.validator(key, data[key]);
|
|
84
|
+
});
|
|
85
|
+
this._id = data.id;
|
|
86
|
+
this._data = trackObjectMutations<Data>(data, this.validator);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
//#endregion
|
|
90
|
+
|
|
91
|
+
//#region Getters
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Date of creation of this model in our API.
|
|
95
|
+
* Alias: createdAt
|
|
96
|
+
*/
|
|
97
|
+
get created(): Temporal.Instant | undefined {
|
|
98
|
+
return this._data.createdAt;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Date of creation of this model in our API.
|
|
103
|
+
* Alias: created
|
|
104
|
+
*/
|
|
105
|
+
get createdAt(): Temporal.Instant | undefined {
|
|
106
|
+
return this._data.createdAt;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
get deleted(): boolean | undefined {
|
|
110
|
+
return this._data.deleted;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get id(): Data['id'] | undefined {
|
|
114
|
+
return this._id;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Get mutated data.
|
|
119
|
+
* This is what will be sent to our endpoint.
|
|
120
|
+
*/
|
|
121
|
+
get mutations() {
|
|
122
|
+
return this._data.getMutations();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { Url } from 'url';
|
|
2
|
+
|
|
3
|
+
import type { AbstractModel, ModelType } from '#core/abstract-model.js';
|
|
4
|
+
import type { EndpointGenerator, EndpointInterface, ListParams } from '#core/endpoint-interface.js';
|
|
5
|
+
import type { Validator } from '#core/validator.js';
|
|
6
|
+
import { StancerError } from '#error/base-error.js';
|
|
7
|
+
import { UnsendableError } from '#error/unsendable-error.js';
|
|
8
|
+
|
|
9
|
+
import { IdValue } from './common/branded-type.js';
|
|
10
|
+
|
|
11
|
+
//#region type & interfaces
|
|
12
|
+
interface CustomIteratorMapperInterface {
|
|
13
|
+
map: <Data extends ModelType, CurrentModel extends AbstractModel<Data>>(
|
|
14
|
+
func: (id?: Partial<Data>) => CurrentModel,
|
|
15
|
+
gen: EndpointGenerator<Data>,
|
|
16
|
+
) => ModelGenerator<Data, CurrentModel>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type ModelGenerator<
|
|
20
|
+
Data extends ModelType,
|
|
21
|
+
CurrentModel extends AbstractModel<Data>,
|
|
22
|
+
ReturnType = void,
|
|
23
|
+
> = AsyncGenerator<CurrentModel, ReturnType, CurrentModel>;
|
|
24
|
+
|
|
25
|
+
export interface Repository<Data extends ModelType, CurrentModel extends AbstractModel<Data>> {
|
|
26
|
+
/**
|
|
27
|
+
* Create a model or get it from the API if Id is given.
|
|
28
|
+
* @param id either the Id of the object or data to construct our model
|
|
29
|
+
* @returns the created model.
|
|
30
|
+
*/
|
|
31
|
+
create: (id?: Data['id'] | Partial<Data>) => Promise<CurrentModel>;
|
|
32
|
+
/**
|
|
33
|
+
* Get a model from the API
|
|
34
|
+
* @param id The id of the model
|
|
35
|
+
* @returns the model from the API whose id is the given id
|
|
36
|
+
*/
|
|
37
|
+
get: (id: IdValue) => Promise<CurrentModel>;
|
|
38
|
+
/**
|
|
39
|
+
* Delete a model in API
|
|
40
|
+
* @param model the model to be deleted
|
|
41
|
+
* @returns the deleted model
|
|
42
|
+
*/
|
|
43
|
+
delete: (model: CurrentModel) => Promise<CurrentModel>;
|
|
44
|
+
/**
|
|
45
|
+
* Send a model in the API
|
|
46
|
+
* if The model has Id it will try to patch otherwise it will try to send.
|
|
47
|
+
* @param model the model to be sent
|
|
48
|
+
* @returns the sent model with data populated.
|
|
49
|
+
*/
|
|
50
|
+
send: (model: CurrentModel) => Promise<CurrentModel>;
|
|
51
|
+
/**
|
|
52
|
+
* List models.
|
|
53
|
+
* @param params listing parameters (start,limit,end...)
|
|
54
|
+
* @returns async generator of models
|
|
55
|
+
*/
|
|
56
|
+
list: (params?: ListParams, endpointUrl?: Url) => ModelGenerator<Data, CurrentModel>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Static model type definition
|
|
61
|
+
* Omit all the non static Method
|
|
62
|
+
*/
|
|
63
|
+
export interface StaticModel<Data extends ModelType> extends Omit<
|
|
64
|
+
AbstractModel<Data>,
|
|
65
|
+
| 'id'
|
|
66
|
+
| 'delete'
|
|
67
|
+
| 'populate'
|
|
68
|
+
| 'send'
|
|
69
|
+
| 'mutations'
|
|
70
|
+
| 'dataTrackMutations'
|
|
71
|
+
| 'data'
|
|
72
|
+
| 'created'
|
|
73
|
+
| 'createdAt'
|
|
74
|
+
| 'deleted'
|
|
75
|
+
> {
|
|
76
|
+
new (validator: Validator<Data>, id: string | Partial<Data> | undefined): AbstractModel<Data>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface RepositoryParameters<Data extends ModelType, Endpoint extends EndpointInterface<Data>> {
|
|
80
|
+
endpoint: Endpoint;
|
|
81
|
+
model: StaticModel<Data>;
|
|
82
|
+
validator: Validator<Data>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const CustomIteratorMapper: CustomIteratorMapperInterface = {
|
|
86
|
+
map: <Data extends ModelType, CurrentModel extends AbstractModel<Data>>(
|
|
87
|
+
func: (id?: Partial<Data>) => CurrentModel,
|
|
88
|
+
gen: EndpointGenerator<Data>,
|
|
89
|
+
): ModelGenerator<Data, CurrentModel> =>
|
|
90
|
+
(async function* () {
|
|
91
|
+
let curr;
|
|
92
|
+
while (!(curr = await gen.next()).done) {
|
|
93
|
+
const populated = func(curr.value);
|
|
94
|
+
yield populated;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return;
|
|
98
|
+
})(),
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
|
|
103
|
+
//#region Repository class
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @remarks Type assertions (`as CurrentModel`) are used throughout this class
|
|
107
|
+
* due to TypeScript's limitation with abstract class constructors in generic
|
|
108
|
+
* contexts. The staticModel constructor returns AbstractModel<Data>, but we
|
|
109
|
+
* guarantee it's CurrentModel at runtime through proper inheritance.
|
|
110
|
+
*/
|
|
111
|
+
export abstract class AbstractRepository<
|
|
112
|
+
Data extends ModelType,
|
|
113
|
+
CurrentModel extends AbstractModel<Data>,
|
|
114
|
+
Endpoint extends EndpointInterface<Data> = EndpointInterface<Data>,
|
|
115
|
+
> implements Repository<Data, CurrentModel> {
|
|
116
|
+
protected endpoint: Endpoint;
|
|
117
|
+
protected staticModel: StaticModel<Data>;
|
|
118
|
+
protected validator: Validator<Data>;
|
|
119
|
+
|
|
120
|
+
constructor({ endpoint, model, validator }: RepositoryParameters<Data, Endpoint>) {
|
|
121
|
+
this.endpoint = endpoint;
|
|
122
|
+
this.staticModel = model;
|
|
123
|
+
this.validator = validator;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public async create(id?: Data['id'] | Partial<Data>): Promise<CurrentModel> {
|
|
127
|
+
if (typeof id === 'string') {
|
|
128
|
+
return this.get(IdValue(id));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return this.createNew(id);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Create a new model
|
|
136
|
+
* @param data initial data that will populate the model
|
|
137
|
+
* @returns a new model
|
|
138
|
+
*/
|
|
139
|
+
public createNew(data?: Partial<Data>): CurrentModel {
|
|
140
|
+
return new this.staticModel(this.validator, data) as CurrentModel;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public async delete(model: CurrentModel) {
|
|
144
|
+
if (!model.id) {
|
|
145
|
+
throw new StancerError('could not delete the model, missing id');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const deletedData = await this.endpoint.delete(model.id);
|
|
149
|
+
if (deletedData) {
|
|
150
|
+
model.dataTrackMutations(deletedData);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return model;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Get a model from the API
|
|
158
|
+
* @param id the Id of the model
|
|
159
|
+
* @returns the model got from the API
|
|
160
|
+
*/
|
|
161
|
+
public async get(id: IdValue): Promise<CurrentModel> {
|
|
162
|
+
const model = new this.staticModel(this.validator, id) as CurrentModel;
|
|
163
|
+
const data = await this.endpoint.get(id);
|
|
164
|
+
if (data) {
|
|
165
|
+
model.dataTrackMutations(data);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return model;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async *list(params?: ListParams, endpointUrl?: Url): ModelGenerator<Data, CurrentModel> {
|
|
172
|
+
const generator = this.endpoint.list(params ?? {}, endpointUrl);
|
|
173
|
+
yield* CustomIteratorMapper.map((item) => this.createNew(item), generator);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Send a model to the API only mutated fields will be sent.
|
|
179
|
+
*
|
|
180
|
+
* @param model the model whose data will be sent to the api
|
|
181
|
+
* @returns the model populated With data from the API
|
|
182
|
+
*/
|
|
183
|
+
public async send(model: CurrentModel) {
|
|
184
|
+
try {
|
|
185
|
+
const newData = await this.endpoint.send(model.id, model.mutations);
|
|
186
|
+
if (!newData) {
|
|
187
|
+
return model;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
model.dataTrackMutations(newData);
|
|
191
|
+
return model;
|
|
192
|
+
} catch (err) {
|
|
193
|
+
if (err instanceof UnsendableError) {
|
|
194
|
+
throw new UnsendableError(`${err.message} is required to send ${this.staticModel.name}`, { cause: err });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
throw err;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StancerValidationError } from '#error/validation-error.js';
|
|
2
|
+
|
|
3
|
+
export type Branded<T, Tag> = T & { readonly __tag: Tag };
|
|
4
|
+
|
|
5
|
+
export type IdValue<T = string> = Branded<T, 'ID'>;
|
|
6
|
+
|
|
7
|
+
const isId = (id: string): id is IdValue => Boolean(id);
|
|
8
|
+
|
|
9
|
+
export const IdValue = (id: string): IdValue => {
|
|
10
|
+
if (isId(id)) {
|
|
11
|
+
return id;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
throw new StancerValidationError('not an ID');
|
|
15
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Temporal } from 'temporal-polyfill';
|
|
2
|
+
|
|
3
|
+
import { type AccessRight, type Handler, setAccess } from './handler.js';
|
|
4
|
+
|
|
5
|
+
const isValidRfc9557String = (arg0: unknown): arg0 is string =>
|
|
6
|
+
typeof arg0 === 'string' && RegExp(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/).test(arg0);
|
|
7
|
+
|
|
8
|
+
export const RFC9557Handler = (access: Partial<AccessRight>): Handler<Temporal.PlainDate> => {
|
|
9
|
+
return {
|
|
10
|
+
serialize: (arg0: Temporal.PlainDate) => arg0.toString(),
|
|
11
|
+
deserialize: (arg0: unknown) => (isValidRfc9557String(arg0) ? Temporal.PlainDate.from(arg0) : undefined),
|
|
12
|
+
...setAccess(access),
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const timestampHandler = (
|
|
17
|
+
access: Partial<AccessRight>,
|
|
18
|
+
name?: string,
|
|
19
|
+
): Handler<Temporal.Instant | undefined> => {
|
|
20
|
+
const payloadName = name ? { payloadName: name } : {};
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
serialize: (arg0: Temporal.Instant | undefined) => (arg0 ? arg0.epochMilliseconds / 1000 : undefined),
|
|
24
|
+
deserialize: (arg0: unknown) => {
|
|
25
|
+
const timestamp = Number(arg0);
|
|
26
|
+
return isNaN(timestamp) ? undefined : Temporal.Instant.fromEpochMilliseconds(timestamp * 1000);
|
|
27
|
+
},
|
|
28
|
+
...setAccess(access),
|
|
29
|
+
...payloadName,
|
|
30
|
+
};
|
|
31
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type AccessRight, type Handler, setAccess } from './handler.js';
|
|
2
|
+
|
|
3
|
+
export const enumHandler = <Enum extends readonly string[]>(
|
|
4
|
+
enumDeclaration: Enum,
|
|
5
|
+
access?: Partial<AccessRight>,
|
|
6
|
+
): Handler<(typeof enumDeclaration)[number]> => {
|
|
7
|
+
return {
|
|
8
|
+
deserialize: (arg0: unknown) => {
|
|
9
|
+
if (typeof arg0 === 'string' && enumDeclaration.includes(arg0)) {
|
|
10
|
+
return arg0 as (typeof enumDeclaration)[number];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return undefined;
|
|
14
|
+
},
|
|
15
|
+
serialize: (arg0: (typeof enumDeclaration)[number]) => arg0,
|
|
16
|
+
...setAccess(access ?? {}),
|
|
17
|
+
};
|
|
18
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export type AccessRight = {
|
|
2
|
+
gettable: boolean;
|
|
3
|
+
postable: boolean;
|
|
4
|
+
patchable: boolean;
|
|
5
|
+
postRequired: 'partial' | boolean;
|
|
6
|
+
patchRequired: 'partial' | boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type Serializer<SDKType> = {
|
|
10
|
+
deserialize: (arg0: unknown) => SDKType | undefined;
|
|
11
|
+
serialize: (arg0: SDKType) => unknown;
|
|
12
|
+
payloadName?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type Handler<SDKType> = AccessRight & Serializer<SDKType>;
|
|
16
|
+
|
|
17
|
+
export const defaultSerialize = <T>(value: T) => value;
|
|
18
|
+
|
|
19
|
+
export const defaultDeserialize = <T>(value: unknown, type: BasicType): T | undefined => {
|
|
20
|
+
if (value === undefined || value === null) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const typeNarrowing = (value: unknown): value is T =>
|
|
25
|
+
type === 'array' ? Array.isArray(value) : typeof value === type;
|
|
26
|
+
|
|
27
|
+
if (typeNarrowing(value)) {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return undefined;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const useDefaultSerializer = <SDKType>(type: BasicType) => {
|
|
35
|
+
return {
|
|
36
|
+
serialize: defaultSerialize<SDKType>,
|
|
37
|
+
deserialize: (value: unknown) => defaultDeserialize<SDKType>(value, type),
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type BasicType = 'boolean' | 'string' | 'number' | 'array' | 'object';
|
|
42
|
+
|
|
43
|
+
export const defaultHandler = <SDKType>(type: BasicType, access?: Partial<AccessRight>): Handler<SDKType> => {
|
|
44
|
+
return {
|
|
45
|
+
...useDefaultSerializer<SDKType>(type),
|
|
46
|
+
...setAccess(access ?? {}),
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const setAccess = ({
|
|
51
|
+
gettable = true,
|
|
52
|
+
postable = true,
|
|
53
|
+
patchable = true,
|
|
54
|
+
postRequired = false,
|
|
55
|
+
patchRequired = false,
|
|
56
|
+
}: Partial<AccessRight>): AccessRight => {
|
|
57
|
+
return {
|
|
58
|
+
gettable,
|
|
59
|
+
postable,
|
|
60
|
+
patchable,
|
|
61
|
+
postRequired,
|
|
62
|
+
patchRequired,
|
|
63
|
+
};
|
|
64
|
+
};
|