@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.
- package/CHANGELOG.md +12 -1
- package/dist/api-endpoint/http/fetch-http-client.d.ts.map +1 -1
- package/dist/api-endpoint/http/fetch-http-client.js +7 -1
- package/dist/api-endpoint/http/fetch-http-client.js.map +1 -1
- package/dist/core/common/handler/metadata-handler.d.ts +18 -0
- package/dist/core/common/handler/metadata-handler.d.ts.map +1 -0
- package/dist/core/common/handler/metadata-handler.js +19 -0
- package/dist/core/common/handler/metadata-handler.js.map +1 -0
- package/dist/event/model.d.ts +20 -0
- package/dist/event/model.d.ts.map +1 -0
- package/dist/event/model.js +30 -0
- package/dist/event/model.js.map +1 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -0
- package/dist/payment-intent/api-endpoint.d.ts +16 -1
- package/dist/payment-intent/api-endpoint.d.ts.map +1 -1
- package/dist/payment-intent/api-endpoint.js +2 -1
- package/dist/payment-intent/api-endpoint.js.map +1 -1
- package/dist/payment-intent/model.d.ts +3 -2
- package/dist/payment-intent/model.d.ts.map +1 -1
- package/dist/payment-intent/model.js +6 -0
- package/dist/payment-intent/model.js.map +1 -1
- package/dist/payment-intent/repository.d.ts +1 -1
- package/dist/payment-intent/repository.d.ts.map +1 -1
- package/dist/webhook/api-endpoint.d.ts +16 -0
- package/dist/webhook/api-endpoint.d.ts.map +1 -0
- package/dist/webhook/api-endpoint.js +33 -0
- package/dist/webhook/api-endpoint.js.map +1 -0
- package/dist/webhook/model.d.ts +35 -0
- package/dist/webhook/model.d.ts.map +1 -0
- package/dist/webhook/model.js +110 -0
- package/dist/webhook/model.js.map +1 -0
- package/dist/webhook/repository.d.ts +7 -0
- package/dist/webhook/repository.d.ts.map +1 -0
- package/dist/webhook/repository.js +10 -0
- package/dist/webhook/repository.js.map +1 -0
- package/dist/webhook/validator.d.ts +4 -0
- package/dist/webhook/validator.d.ts.map +1 -0
- package/dist/webhook/validator.js +16 -0
- package/dist/webhook/validator.js.map +1 -0
- package/package.json +33 -22
- package/dist/main.d.ts +0 -2
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -2
- package/dist/main.js.map +0 -1
- package/reports/base.css +0 -362
- package/reports/block-navigation.js +0 -75
- package/reports/cobertura-coverage.xml +0 -18
- package/reports/favicon.png +0 -0
- package/reports/index.html +0 -177
- package/reports/junit.xml +0 -25
- package/reports/prettify.css +0 -101
- package/reports/prettify.js +0 -955
- package/reports/sort-arrow-sprite.png +0 -0
- package/reports/sorter.js +0 -205
- package/src/address/api-endpoint.ts +0 -30
- package/src/address/model.ts +0 -79
- package/src/address/repository.ts +0 -18
- package/src/address/validator.ts +0 -34
- package/src/api-endpoint/abstract-api-endpoint.ts +0 -310
- package/src/api-endpoint/configuration.ts +0 -125
- package/src/api-endpoint/http/fetch-http-client.ts +0 -29
- package/src/api-endpoint/http/http.ts +0 -75
- package/src/api-endpoint/http/index.ts +0 -2
- package/src/api-endpoint/version.ts +0 -4
- package/src/card/api-endpoint.ts +0 -39
- package/src/card/enum/funding.ts +0 -11
- package/src/card/enum/nature.ts +0 -7
- package/src/card/enum/network.ts +0 -16
- package/src/card/model.ts +0 -139
- package/src/card/repository.ts +0 -11
- package/src/card/validator.ts +0 -79
- package/src/core/abstract-model.ts +0 -126
- package/src/core/abstract-repository.ts +0 -201
- package/src/core/common/branded-type.ts +0 -15
- package/src/core/common/handler/date-handler.ts +0 -31
- package/src/core/common/handler/enum-handler.ts +0 -18
- package/src/core/common/handler/handler.ts +0 -64
- package/src/core/common/handler/id-handler.ts +0 -27
- package/src/core/common/id-validator.ts +0 -23
- package/src/core/common/mutation-tracked-object.ts +0 -49
- package/src/core/endpoint-interface.ts +0 -43
- package/src/core/error/base-error.ts +0 -1
- package/src/core/error/endpoint/api/api-conflict-error.ts +0 -7
- package/src/core/error/endpoint/api/api-error.ts +0 -10
- package/src/core/error/endpoint/api/api-not-found-error.ts +0 -7
- package/src/core/error/endpoint/api/api-unauthorized-error.ts +0 -7
- package/src/core/error/endpoint/api/api-unknown-error.ts +0 -7
- package/src/core/error/endpoint/api/api-validation-error.ts +0 -13
- package/src/core/error/endpoint/api/index.ts +0 -6
- package/src/core/error/endpoint/route-inaccessible-error.ts +0 -8
- package/src/core/error/unpopulated-error.ts +0 -3
- package/src/core/error/unsendable-error.ts +0 -3
- package/src/core/error/validation-error.ts +0 -3
- package/src/core/validator.ts +0 -129
- package/src/customer/api-endpoint.ts +0 -29
- package/src/customer/model.ts +0 -113
- package/src/customer/repository.ts +0 -14
- package/src/customer/validator.ts +0 -26
- package/src/payment/api-endpoint.ts +0 -118
- package/src/payment/enum/currency.ts +0 -14
- package/src/payment/enum/method.ts +0 -2
- package/src/payment/enum/status.ts +0 -21
- package/src/payment/model.ts +0 -378
- package/src/payment/repository.ts +0 -73
- package/src/payment/validator.ts +0 -96
- package/src/payment-intent/api-endpoint.ts +0 -123
- package/src/payment-intent/enum/status.ts +0 -21
- package/src/payment-intent/enum/threeds.ts +0 -2
- package/src/payment-intent/model.ts +0 -266
- package/src/payment-intent/repository.ts +0 -85
- package/src/payment-intent/validator.ts +0 -45
- package/src/payout/model.ts +0 -8
- package/src/refund/api-endpoint.ts +0 -33
- package/src/refund/enum/refund-status.ts +0 -11
- package/src/refund/model.ts +0 -90
- package/src/refund/repository.ts +0 -14
- package/src/refund/validator.ts +0 -44
- package/src/sepa/api-endpoint.ts +0 -26
- package/src/sepa/model.ts +0 -80
- package/src/sepa/repository.ts +0 -11
- package/src/sepa/validator.ts +0 -56
- package/test/fixtures/endpoint.stub.ts +0 -22
- package/test/fixtures/list-endpoint.stub.ts +0 -33
- package/test/fixtures/list-model.stub.ts +0 -24
- package/test/fixtures/model.stub.ts +0 -12
- package/test/functional/address.test.ts +0 -126
- package/test/functional/card.test.ts +0 -175
- package/test/functional/customer.test.ts +0 -174
- package/test/functional/payment-intent.test.ts +0 -215
- package/test/functional/payment.test.ts +0 -206
- package/test/functional/refund.test.ts +0 -112
- package/test/functional/sepa.test.ts +0 -136
- package/test/integration/stub-integration.test.ts +0 -88
- package/test/stub/stub-endpoint.ts +0 -42
- package/test/stub/stub-model.ts +0 -82
- package/test/stub/stub-repository.ts +0 -11
- package/test/test-helper.ts +0 -133
- package/test/unit/address/api-endpoint.test.ts +0 -134
- package/test/unit/address/fixtures.ts +0 -60
- package/test/unit/address/model.test.ts +0 -75
- package/test/unit/address/repository.test.ts +0 -148
- package/test/unit/address/validator.test.ts +0 -49
- package/test/unit/api-endpoint/http/fetch-http-client.test.ts +0 -44
- package/test/unit/api-endpoint/http/http.test.ts +0 -152
- package/test/unit/card/api-endpoint.test.ts +0 -170
- package/test/unit/card/card-helper.ts +0 -116
- package/test/unit/card/fixtures.ts +0 -87
- package/test/unit/card/model.test.ts +0 -167
- package/test/unit/card/repository.test.ts +0 -146
- package/test/unit/card/validator.test.ts +0 -96
- package/test/unit/core/common/mutation-tracked-object.test.ts +0 -24
- package/test/unit/customer/api-endpoint.test.ts +0 -143
- package/test/unit/customer/fixtures.ts +0 -73
- package/test/unit/customer/model.test.ts +0 -90
- package/test/unit/customer/repository.test.ts +0 -148
- package/test/unit/customer/validator.test.ts +0 -62
- package/test/unit/payment/api-endpoint.test.ts +0 -188
- package/test/unit/payment/fixtures.ts +0 -116
- package/test/unit/payment/model.test.ts +0 -240
- package/test/unit/payment/repository.test.ts +0 -270
- package/test/unit/payment/validator.test.ts +0 -137
- package/test/unit/payment-intent/api-endpoint.test.ts +0 -142
- package/test/unit/payment-intent/fixtures.ts +0 -94
- package/test/unit/payment-intent/model.test.ts +0 -157
- package/test/unit/payment-intent/repository.test.ts +0 -263
- package/test/unit/payment-intent/validator.test.ts +0 -136
- package/test/unit/refund/api-endpoint.test.ts +0 -129
- package/test/unit/refund/fixtures.ts +0 -58
- package/test/unit/refund/model.test.ts +0 -89
- package/test/unit/refund/repository.test.ts +0 -157
- package/test/unit/refund/validator.test.ts +0 -89
- package/test/unit/sepa/api-endpoint.test.ts +0 -170
- package/test/unit/sepa/fixture.ts +0 -62
- package/test/unit/sepa/model.test.ts +0 -86
- package/test/unit/sepa/repository.test.ts +0 -149
- package/test/unit/sepa/sepa-helper.ts +0 -71
- package/test/unit/sepa/validator.test.ts +0 -58
- package/test/unit/stub/stub-endpoint.test.ts +0 -236
- package/test/unit/stub/stub-list-endpoint.test.ts +0 -86
- package/test/unit/stub/stub-model.test.ts +0 -111
- package/test/unit/stub/stub-repository.test.ts +0 -144
|
@@ -1,201 +0,0 @@
|
|
|
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
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,64 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { IdValue } from '../branded-type.js';
|
|
2
|
-
import { isValidID } from '../id-validator.js';
|
|
3
|
-
import { type AccessRight, type Handler, defaultSerialize, setAccess } from './handler.js';
|
|
4
|
-
|
|
5
|
-
export const IdHandler = <ID extends IdValue>(regex: RegExp, access?: Partial<AccessRight>): Handler<ID> => {
|
|
6
|
-
return {
|
|
7
|
-
deserialize: (id: unknown) => {
|
|
8
|
-
if (typeof id === 'string' && isValidID<ID>(id, regex)) {
|
|
9
|
-
return id;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (typeof id === 'object' && id !== null && Object.hasOwn(id, 'id')) {
|
|
13
|
-
const data = id as { id: string };
|
|
14
|
-
|
|
15
|
-
if (isValidID<ID>(data.id, regex)) {
|
|
16
|
-
return data.id;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return undefined;
|
|
21
|
-
},
|
|
22
|
-
serialize: defaultSerialize,
|
|
23
|
-
...setAccess(
|
|
24
|
-
access ?? { gettable: true, postable: false, patchable: false, postRequired: false, patchRequired: false },
|
|
25
|
-
),
|
|
26
|
-
};
|
|
27
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { StancerValidationError } from '#core/error/validation-error.js';
|
|
2
|
-
|
|
3
|
-
import type { IdValue } from './branded-type.js';
|
|
4
|
-
|
|
5
|
-
export const ID_SUFFIX_LENGTH = 24;
|
|
6
|
-
|
|
7
|
-
export const isValidID = <ID extends IdValue>(id: string, regex: RegExp): id is ID => regex.test(id);
|
|
8
|
-
export const IdErrorMessage = (value: string, regex: RegExp) => `The id (${value}) doesn't match the pattern ${regex}`;
|
|
9
|
-
|
|
10
|
-
export const IdValidator = <Id extends IdValue>(regex: RegExp) => {
|
|
11
|
-
return {
|
|
12
|
-
validate: (arg0: string): arg0 is Id => isValidID<Id>(arg0, regex),
|
|
13
|
-
errorMessage: (value: string) => IdErrorMessage(value, regex),
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export const validateID = <ID extends IdValue>(id: string, regex: RegExp) => {
|
|
18
|
-
if (isValidID<ID>(id, regex)) {
|
|
19
|
-
return id;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
throw new StancerValidationError(IdErrorMessage(id, regex));
|
|
23
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type { ModelType } from '#core/abstract-model.js';
|
|
2
|
-
import type { ValidationFunction } from '#core/validator.js';
|
|
3
|
-
|
|
4
|
-
const inMutation = <T extends object>(mutations: T, key: string | number | symbol): key is keyof T => {
|
|
5
|
-
return Object.hasOwn(mutations, key);
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export type MutationTrackedObject<T extends Partial<ModelType>> = T & {
|
|
9
|
-
getMutations(): Partial<T>;
|
|
10
|
-
toRaw(): T;
|
|
11
|
-
};
|
|
12
|
-
export const trackObjectMutations = <T extends ModelType>(
|
|
13
|
-
data: Partial<T>,
|
|
14
|
-
validator: ValidationFunction<T>,
|
|
15
|
-
): MutationTrackedObject<T> => {
|
|
16
|
-
const mutations: Partial<T> = {};
|
|
17
|
-
const handler: ProxyHandler<T> = {
|
|
18
|
-
deleteProperty(target, key: string | symbol) {
|
|
19
|
-
if (inMutation(mutations, key)) {
|
|
20
|
-
mutations[key] = undefined;
|
|
21
|
-
}
|
|
22
|
-
return Reflect.deleteProperty(target, key);
|
|
23
|
-
},
|
|
24
|
-
get(target, key, receiver) {
|
|
25
|
-
if (key === 'getMutations') {
|
|
26
|
-
return () => ({ ...mutations });
|
|
27
|
-
}
|
|
28
|
-
if (key === 'toRaw') {
|
|
29
|
-
return () => target;
|
|
30
|
-
}
|
|
31
|
-
return Reflect.get(target, key, receiver);
|
|
32
|
-
},
|
|
33
|
-
set(target, key: string | symbol, value, receiver) {
|
|
34
|
-
if (typeof key === 'symbol') {
|
|
35
|
-
return Reflect.set(target, key, value, receiver);
|
|
36
|
-
}
|
|
37
|
-
const oldValue = Reflect.get(target, key, receiver);
|
|
38
|
-
if (oldValue === undefined || oldValue !== value) {
|
|
39
|
-
// validator throw error on fail
|
|
40
|
-
validator(key as keyof T, value);
|
|
41
|
-
mutations[key as keyof T] = value;
|
|
42
|
-
}
|
|
43
|
-
return Reflect.set(target, key, value, receiver);
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// We redefine getMutations & toRaw in handler get. Proxy has getMutations & toRaw
|
|
48
|
-
return new Proxy(data, handler) as MutationTrackedObject<T>;
|
|
49
|
-
};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { Url } from 'url';
|
|
2
|
-
|
|
3
|
-
import type { IdValue } from './common/branded-type.js';
|
|
4
|
-
|
|
5
|
-
export interface ListParams {
|
|
6
|
-
filters: Record<string, unknown>;
|
|
7
|
-
limit: number;
|
|
8
|
-
start: number;
|
|
9
|
-
end: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type EndpointGenerator<Data> = AsyncIterableIterator<Partial<Data>, void, Partial<Data>>;
|
|
13
|
-
export interface EndpointInterface<Data> {
|
|
14
|
-
/**
|
|
15
|
-
* Delete the object with specific id in the API
|
|
16
|
-
* @param id The Id of the object we want to delete
|
|
17
|
-
* @returns The response of the API
|
|
18
|
-
*/
|
|
19
|
-
delete: (id: IdValue) => Promise<Partial<Data>>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Get an object from the API
|
|
23
|
-
* @param id The Id of the object we want to get
|
|
24
|
-
* @returns The data of an object
|
|
25
|
-
*/
|
|
26
|
-
get: (id: IdValue) => Promise<Partial<Data>>;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Get a list of object from the API
|
|
30
|
-
* @param params The params of the listing
|
|
31
|
-
* @param endpoint A custom url for specific subObject
|
|
32
|
-
* @returns A generator of object Data
|
|
33
|
-
*/
|
|
34
|
-
list: (params: Partial<ListParams>, endpoint?: Url) => EndpointGenerator<Data>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Sent an object to the API
|
|
38
|
-
* @param id The ID of the object
|
|
39
|
-
* @param mutations The data to be sent to our API
|
|
40
|
-
* @returns The new data of the object.
|
|
41
|
-
*/
|
|
42
|
-
send: (id: IdValue | undefined, mutations: Partial<Data>) => Promise<Partial<Data>>;
|
|
43
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export class StancerError extends Error {}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { StancerApiError } from './api-error.js';
|
|
2
|
-
|
|
3
|
-
export interface StancerApiValidationErrorDetails {
|
|
4
|
-
loc: (string | number)[];
|
|
5
|
-
msg: string;
|
|
6
|
-
type: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class StancerApiValidationError extends StancerApiError {
|
|
10
|
-
constructor(public details?: StancerApiValidationErrorDetails[]) {
|
|
11
|
-
super(422, details ? `Validation failed: ${JSON.stringify(details)}` : 'Payload cannot be validated');
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { StancerError } from '../base-error.js';
|
|
2
|
-
|
|
3
|
-
export class StancerRouteInaccessibleError extends StancerError {
|
|
4
|
-
constructor(verb: 'POST' | 'PATCH' | 'LIST' | 'DELETE', resource: string, error?: ErrorOptions) {
|
|
5
|
-
const message = `${verb} is not allowed for ${resource} resource`;
|
|
6
|
-
super(message, error);
|
|
7
|
-
}
|
|
8
|
-
}
|