@communecter/cocolight-api-client 1.0.33 → 1.0.34
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/dist/cocolight-api-client.browser.js +1 -1
- package/dist/cocolight-api-client.cjs +1 -1
- package/dist/cocolight-api-client.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js.map +1 -1
- package/package.json +1 -1
- package/src/api/BaseEntity.js +45 -39
- package/src/api/EntityRegistry.js +2 -2
package/package.json
CHANGED
package/src/api/BaseEntity.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// BaseEntity.js
|
|
2
2
|
import ObjectID from "bson-objectid";
|
|
3
3
|
// import { fileTypeFromBuffer } from "file-type";
|
|
4
|
+
import EJSON from "ejson";
|
|
4
5
|
import pkg from "file-type";
|
|
5
6
|
|
|
6
7
|
import { ApiAuthenticationError, ApiError, ApiResponseError, ApiValidationError } from "../error.js";
|
|
@@ -282,75 +283,80 @@ export class BaseEntity {
|
|
|
282
283
|
if (this.parent?.id) parentMeta.id = this.parent.id;
|
|
283
284
|
if (typeof this.parent?.getEntityType === "function") parentMeta.type = this.parent.getEntityType();
|
|
284
285
|
if (this.parent?.__entityTag) parentMeta.__entityTag = this.parent.__entityTag;
|
|
286
|
+
if (this.parent?.slug) parentMeta.slug = this.parent.slug;
|
|
285
287
|
|
|
286
288
|
return {
|
|
287
289
|
__entityTag: this.__entityTag,
|
|
288
|
-
|
|
290
|
+
__isSerializedEntity: true,
|
|
291
|
+
serverData: this._serialize(this._serverData),
|
|
289
292
|
parent: Object.keys(parentMeta).length > 0 ? parentMeta : null
|
|
290
293
|
};
|
|
291
294
|
}
|
|
292
295
|
|
|
293
|
-
|
|
296
|
+
|
|
297
|
+
_serialize(obj) {
|
|
298
|
+
try {
|
|
299
|
+
return JSON.parse(EJSON.stringify(this._removeUnserializables(obj)));
|
|
300
|
+
} catch (e) {
|
|
301
|
+
this.apiClient?._logger?.error?.("Erreur de sérialisation EJSON", e);
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Supprime les propriétés non sérialisables d'un objet.
|
|
308
|
+
*
|
|
309
|
+
* @param {Object} obj - L'objet à nettoyer.
|
|
310
|
+
* @param {WeakSet} [seen] - Ensemble pour éviter les références circulaires.
|
|
311
|
+
* @returns {Object} L'objet nettoyé.
|
|
312
|
+
* @private
|
|
313
|
+
*/
|
|
314
|
+
_removeUnserializables(obj, seen = new WeakSet()) {
|
|
294
315
|
if (obj === null || typeof obj !== "object") return obj;
|
|
295
316
|
|
|
296
|
-
// Évite les boucles circulaires
|
|
297
317
|
if (seen.has(obj)) return null;
|
|
298
318
|
seen.add(obj);
|
|
299
319
|
|
|
300
|
-
//
|
|
301
|
-
if (obj instanceof Date || Object.prototype.toString.call(obj) === "[object Date]") {
|
|
302
|
-
return obj.toISOString();
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// Si c'est un Proxy réactif de ton système maison
|
|
320
|
+
// Ignore les proxys réactifs
|
|
306
321
|
if (obj.__isReactive && typeof obj.__raw === "object") {
|
|
307
|
-
return this.
|
|
322
|
+
return this._removeUnserializables(obj.__raw, seen);
|
|
308
323
|
}
|
|
309
324
|
|
|
310
|
-
// Tableaux
|
|
311
325
|
if (Array.isArray(obj)) {
|
|
312
|
-
return obj.map((
|
|
326
|
+
return obj.map((el) => this._removeUnserializables(el, seen));
|
|
313
327
|
}
|
|
314
328
|
|
|
315
|
-
|
|
316
|
-
const result = {};
|
|
329
|
+
const clean = {};
|
|
317
330
|
for (const key of Object.keys(obj)) {
|
|
318
|
-
const
|
|
331
|
+
const val = obj[key];
|
|
319
332
|
|
|
320
|
-
|
|
321
|
-
|
|
333
|
+
if (
|
|
334
|
+
typeof val === "function" ||
|
|
335
|
+
typeof val === "symbol" ||
|
|
336
|
+
typeof val === "undefined"
|
|
337
|
+
) {
|
|
322
338
|
continue;
|
|
323
339
|
}
|
|
324
340
|
|
|
325
341
|
try {
|
|
326
|
-
|
|
342
|
+
clean[key] = this._removeUnserializables(val, seen);
|
|
327
343
|
// eslint-disable-next-line no-unused-vars
|
|
328
344
|
} catch (e) {
|
|
329
|
-
|
|
345
|
+
clean[key] = null;
|
|
330
346
|
}
|
|
331
347
|
}
|
|
332
348
|
|
|
333
|
-
return
|
|
349
|
+
return clean;
|
|
334
350
|
}
|
|
335
351
|
|
|
352
|
+
/**
|
|
353
|
+
* Restaure les données sérialisées en un objet d'origine.
|
|
354
|
+
*
|
|
355
|
+
* @param {Object} obj - L'objet à restaurer.
|
|
356
|
+
* @returns {Object} L'objet restauré.
|
|
357
|
+
*/
|
|
336
358
|
static _revive(obj) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
// Détecter et restaurer les ISO string en Date
|
|
340
|
-
if (typeof obj === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(obj)) {
|
|
341
|
-
return new Date(obj);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
if (Array.isArray(obj)) {
|
|
345
|
-
return obj.map((item) => this._revive(item));
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
const result = {};
|
|
349
|
-
for (const key of Object.keys(obj)) {
|
|
350
|
-
result[key] = this._revive(obj[key]);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
return result;
|
|
359
|
+
return EJSON.fromJSONValue(obj);
|
|
354
360
|
}
|
|
355
361
|
|
|
356
362
|
/**
|
|
@@ -363,9 +369,9 @@ export class BaseEntity {
|
|
|
363
369
|
*/
|
|
364
370
|
static fromJSON(json, parent, deps) {
|
|
365
371
|
|
|
366
|
-
const {
|
|
372
|
+
const { serverData } = json;
|
|
367
373
|
|
|
368
|
-
const instance = this.fromServerData(this._revive(
|
|
374
|
+
const instance = this.fromServerData(this._revive(serverData), parent, deps);
|
|
369
375
|
|
|
370
376
|
return instance;
|
|
371
377
|
}
|
|
@@ -18,8 +18,8 @@ export function registerEntity(tag, EntityClass) {
|
|
|
18
18
|
*/
|
|
19
19
|
export function fromEntityJSON(json, parent = null) {
|
|
20
20
|
if (!json?.__entityTag) return null;
|
|
21
|
-
if (!json.
|
|
22
|
-
const meta = _getEntityMeta(json.
|
|
21
|
+
if (!json.serverData?.collection) return json;
|
|
22
|
+
const meta = _getEntityMeta(json.serverData.collection, json.__entityTag);
|
|
23
23
|
if (!meta) return json;
|
|
24
24
|
if (!meta?.entityClass?.fromJSON) throw new Error(`Classe inconnue ou fromJSON manquant pour ${json.__entityTag}`);
|
|
25
25
|
return meta.entityClass.fromJSON(json, parent, meta.deps);
|