rest-client-sdk 7.1.1 → 7.2.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 (36) hide show
  1. package/dist/{index.js → index.cjs} +52 -2
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/{index.es.js → index.mjs} +52 -2
  4. package/dist/index.mjs.map +1 -0
  5. package/dist/types/utils/levenshtein.d.ts +1 -0
  6. package/package.json +10 -14
  7. package/dist/index.es.js.map +0 -1
  8. package/dist/index.js.map +0 -1
  9. package/src/AsyncStorageInterface.ts +0 -7
  10. package/src/ErrorFactory.ts +0 -258
  11. package/src/Mapping/Attribute.ts +0 -33
  12. package/src/Mapping/ClassMetadata.ts +0 -125
  13. package/src/Mapping/Relation.ts +0 -68
  14. package/src/Mapping.ts +0 -120
  15. package/src/RestClientSdk.ts +0 -120
  16. package/src/RestClientSdkInterface.ts +0 -27
  17. package/src/TokenGenerator/AbstractTokenGenerator.ts +0 -86
  18. package/src/TokenGenerator/AuthorizationCodeFlowTokenGenerator.ts +0 -146
  19. package/src/TokenGenerator/ClientCredentialsGenerator.ts +0 -97
  20. package/src/TokenGenerator/PasswordGenerator.ts +0 -140
  21. package/src/TokenGenerator/ProvidedTokenGenerator.ts +0 -44
  22. package/src/TokenGenerator/TokenGeneratorInterface.ts +0 -27
  23. package/src/TokenGenerator/types.ts +0 -33
  24. package/src/TokenStorage.ts +0 -256
  25. package/src/TokenStorageInterface.ts +0 -14
  26. package/src/UnitOfWork.ts +0 -354
  27. package/src/client/AbstractClient.ts +0 -545
  28. package/src/client/headerUtils.ts +0 -53
  29. package/src/decorator.ts +0 -53
  30. package/src/index.ts +0 -86
  31. package/src/isImmutable.ts +0 -52
  32. package/src/serializer/JsSerializer.ts +0 -30
  33. package/src/serializer/Serializer.ts +0 -138
  34. package/src/serializer/SerializerInterface.ts +0 -89
  35. package/src/utils/logging.ts +0 -96
  36. package/src/utils/repositoryGenerator.ts +0 -16
package/src/UnitOfWork.ts DELETED
@@ -1,354 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import diff from 'deep-diff';
3
- import Mapping from './Mapping';
4
- import Attribute from './Mapping/Attribute';
5
- import ClassMetadata, {
6
- DefaultSerializedModelType,
7
- } from './Mapping/ClassMetadata';
8
- import { isImmutable } from './isImmutable';
9
-
10
- type Id = string | number;
11
- type StringKeyObject = Record<string, any>;
12
-
13
- /**
14
- * deep comparaison between objects
15
- */
16
- function objectDiffers(
17
- left: Record<string, unknown>,
18
- right: Record<string, unknown>
19
- ): boolean {
20
- const result = diff(left, right);
21
-
22
- return result ? result.length > 0 : false;
23
- }
24
-
25
- /**
26
- * get the id of an entity or return itself if string
27
- */
28
- function getEntityId(
29
- stringOrEntity: string | Record<string, unknown>,
30
- idSerializedKey: string
31
- ): Id {
32
- if (typeof stringOrEntity !== 'object') {
33
- return stringOrEntity;
34
- }
35
-
36
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
37
- // @ts-ignore
38
- return stringOrEntity[idSerializedKey] as Id;
39
- }
40
-
41
- /**
42
- * find old relation value from the new relation id
43
- * if not found, returs the default serilazed model
44
- */
45
- function findOldRelation(
46
- newRelationValue: any[],
47
- oldRelationValue: any[],
48
- classMetadata: ClassMetadata
49
- ): DefaultSerializedModelType {
50
- const idSerializedKey = classMetadata.getIdentifierAttribute().serializedKey;
51
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
52
- // @ts-ignore
53
- const relationValueId = getEntityId(newRelationValue, idSerializedKey);
54
-
55
- const foundValue =
56
- oldRelationValue &&
57
- oldRelationValue.find((innerOldRelationValue) => {
58
- const oldRelationValueId = getEntityId(
59
- innerOldRelationValue,
60
- idSerializedKey
61
- );
62
-
63
- return relationValueId === oldRelationValueId;
64
- });
65
-
66
- if (foundValue) {
67
- return foundValue;
68
- }
69
-
70
- return classMetadata.getDefaultSerializedModel();
71
- }
72
-
73
- /**
74
- * add all identifier from one to many relation list
75
- */
76
- function getIdentifierForList(
77
- newValue: any[],
78
- idSerializedKey: string
79
- ): StringKeyObject | string {
80
- return newValue.map((value) => {
81
- if (Object.keys(value).includes(idSerializedKey)) {
82
- return {
83
- [idSerializedKey]: value[idSerializedKey],
84
- };
85
- }
86
-
87
- if (typeof value === 'string') {
88
- return value;
89
- }
90
-
91
- throw new TypeError(
92
- 'new value should include a list of string or objects containing the serialized key'
93
- );
94
- });
95
- }
96
-
97
- class UnitOfWork {
98
- mapping: Mapping;
99
-
100
- #storage: { [key in Id]: Record<string, unknown> };
101
-
102
- #enabled: boolean;
103
-
104
- constructor(mapping: Mapping, enabled = false) {
105
- this.mapping = mapping;
106
-
107
- this.#enabled = enabled;
108
- this.#storage = {};
109
- }
110
-
111
- registerClean(id: Id, entity: Record<string, unknown>): void {
112
- if (!this.#enabled) {
113
- return;
114
- }
115
- if (isImmutable(entity)) {
116
- this.#storage[id] = entity;
117
- } else {
118
- this.#storage[id] = { ...entity };
119
- }
120
- }
121
-
122
- getDirtyEntity(id: Id): Record<string, unknown> {
123
- return this.#storage[id];
124
- }
125
-
126
- clear(id: Id): void {
127
- delete this.#storage[id];
128
- }
129
-
130
- getDirtyData(
131
- newSerializedModel: Record<string, unknown>,
132
- oldSerializedModel: Record<string, unknown>,
133
- classMetadata: ClassMetadata
134
- ): StringKeyObject {
135
- return this._getDirtyFields(
136
- newSerializedModel,
137
- oldSerializedModel,
138
- classMetadata
139
- );
140
- }
141
-
142
- private _getDirtyFieldsForAttribute(
143
- dirtyFieldsParam: StringKeyObject,
144
- key: string,
145
- attribute: Attribute,
146
- oldValue: Record<string, unknown>,
147
- newValue: Record<string, unknown>
148
- ): StringKeyObject {
149
- const dirtyFields = dirtyFieldsParam;
150
-
151
- if (attribute.type === 'object') {
152
- if (oldValue === undefined || objectDiffers(oldValue, newValue)) {
153
- dirtyFields[key] = newValue;
154
- }
155
- } else if (oldValue !== newValue) {
156
- dirtyFields[key] = newValue;
157
- }
158
-
159
- return dirtyFields;
160
- }
161
-
162
- private _getDirtyFieldsForManyToOne(
163
- dirtyFieldsParam: StringKeyObject,
164
- key: string,
165
- oldValue: Record<string, unknown>,
166
- newValue: Record<string, unknown>,
167
- relationMetadata: ClassMetadata,
168
- idSerializedKey: string
169
- ): StringKeyObject {
170
- const dirtyFields = dirtyFieldsParam;
171
- if (oldValue !== newValue) {
172
- if (newValue === null) {
173
- dirtyFields[key] = null;
174
-
175
- return dirtyFields;
176
- }
177
-
178
- if (typeof oldValue === 'string' || typeof newValue === 'string') {
179
- dirtyFields[key] = newValue;
180
- }
181
-
182
- const recursiveDiff = this._getDirtyFields(
183
- newValue,
184
- oldValue,
185
- relationMetadata
186
- );
187
-
188
- if (Object.keys(recursiveDiff).length > 0) {
189
- recursiveDiff[idSerializedKey] = getEntityId(newValue, idSerializedKey);
190
- dirtyFields[key] = recursiveDiff;
191
- }
192
- }
193
-
194
- return dirtyFields;
195
- }
196
-
197
- private _getDirtyFieldsForOneToMany(
198
- dirtyFieldsParam: StringKeyObject,
199
- key: string,
200
- idSerializedKey: string,
201
- relationMetadata: ClassMetadata,
202
- oldValue: any[],
203
- newValue: any[]
204
- ): StringKeyObject {
205
- const dirtyFields = dirtyFieldsParam;
206
- const newValueLength = newValue ? newValue.length : 0;
207
- const oldValueLength = oldValue ? oldValue.length : 0;
208
-
209
- // number of items changed
210
- if (newValueLength !== oldValueLength) {
211
- dirtyFields[key] = getIdentifierForList(newValue, idSerializedKey);
212
- }
213
-
214
- if (newValue && newValue.length > 0) {
215
- if (dirtyFields[key] === undefined) {
216
- dirtyFields[key] = [];
217
- }
218
-
219
- const relationList = newValue;
220
-
221
- relationList.forEach((newRelationValue, relationAttributeName) => {
222
- const oldRelationValue = findOldRelation(
223
- newRelationValue,
224
- oldValue,
225
- relationMetadata
226
- );
227
-
228
- if (newRelationValue !== oldRelationValue) {
229
- if (
230
- typeof newRelationValue === 'string' ||
231
- typeof oldRelationValue === 'string'
232
- ) {
233
- dirtyFields[key][relationAttributeName] = newRelationValue;
234
-
235
- return;
236
- }
237
-
238
- const recursiveDiff = this._getDirtyFields(
239
- newRelationValue,
240
- oldRelationValue,
241
- relationMetadata
242
- );
243
-
244
- if (Object.keys(recursiveDiff).length > 0) {
245
- const entityId = getEntityId(newRelationValue, idSerializedKey);
246
-
247
- if (entityId !== null) {
248
- recursiveDiff[idSerializedKey] = entityId;
249
- }
250
-
251
- if (dirtyFields[key][relationAttributeName]) {
252
- Object.assign(
253
- dirtyFields[key][relationAttributeName],
254
- recursiveDiff
255
- );
256
- } else {
257
- dirtyFields[key][relationAttributeName] = recursiveDiff;
258
- }
259
- }
260
- }
261
- });
262
-
263
- if (dirtyFields[key].length > 0) {
264
- // add identifier because one or more object linked has been updated
265
- // see test: 'get dirty data many to one update item'
266
- dirtyFields[key] = Object.assign(
267
- getIdentifierForList(newValue, idSerializedKey),
268
- dirtyFields[key]
269
- );
270
- } else {
271
- // no changes, no need to send the key with an empty array as value
272
- delete dirtyFields[key];
273
- }
274
- }
275
-
276
- return dirtyFields;
277
- }
278
-
279
- private _getDirtyFields(
280
- newSerializedModel: StringKeyObject,
281
- oldSerializedModel: StringKeyObject,
282
- classMetadata: ClassMetadata
283
- ): StringKeyObject {
284
- let dirtyFields = {};
285
-
286
- Object.values(classMetadata.getAttributeList()).forEach((attribute) => {
287
- const key = attribute.attributeName;
288
- const newValue = newSerializedModel[key];
289
- const oldValue = oldSerializedModel ? oldSerializedModel[key] : null;
290
-
291
- const currentRelation = classMetadata.getRelation(
292
- attribute.serializedKey
293
- );
294
-
295
- if (newValue === undefined) {
296
- return;
297
- }
298
-
299
- // not a relation
300
- if (!currentRelation) {
301
- dirtyFields = this._getDirtyFieldsForAttribute(
302
- dirtyFields,
303
- key,
304
- attribute,
305
- oldValue,
306
- newValue
307
- );
308
-
309
- return;
310
- }
311
-
312
- const relationMetadata = this.mapping.getClassMetadataByKey(
313
- currentRelation.targetMetadataKey
314
- );
315
-
316
- if (!relationMetadata) {
317
- throw new TypeError(
318
- `relation metadata is not set for relation ${classMetadata.key}.${currentRelation.targetMetadataKey}`
319
- );
320
- }
321
-
322
- const idSerializedKey = relationMetadata.getIdentifierAttribute()
323
- .serializedKey;
324
-
325
- // MANY_TO_ONE relation
326
- if (currentRelation.isRelationToOne()) {
327
- dirtyFields = this._getDirtyFieldsForManyToOne(
328
- dirtyFields,
329
- key,
330
- oldValue,
331
- newValue,
332
- relationMetadata,
333
- idSerializedKey
334
- );
335
-
336
- return;
337
- }
338
-
339
- // *_TO_MANY relation
340
- dirtyFields = this._getDirtyFieldsForOneToMany(
341
- dirtyFields,
342
- key,
343
- idSerializedKey,
344
- relationMetadata,
345
- oldValue,
346
- newValue
347
- );
348
- });
349
-
350
- return dirtyFields;
351
- }
352
- }
353
-
354
- export default UnitOfWork;