@communecter/cocolight-api-client 1.0.4 → 1.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@communecter/cocolight-api-client",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Client Axios simplifié pour l'API cocolight",
5
5
  "repository": {
6
6
  "type": "git",
package/src/ApiClient.js CHANGED
@@ -274,25 +274,69 @@ export default class ApiClient extends EventEmitter {
274
274
  return obj;
275
275
  }
276
276
 
277
- _resolveSpecialValuesInPlace(obj) {
277
+ _resolveSpecialValuesInPlace(obj, pathParams = {}) {
278
278
  const aliasMap = {
279
279
  userId: () => this.userId,
280
280
  accessToken: () => this._accessToken,
281
281
  refreshToken: () => this._refreshToken,
282
- baseURL: () => this._baseURL
282
+ baseURL: () => this._baseURL,
283
+ pathParams: (subPath) => {
284
+ // Si la valeur existe dans pathParams, on la retourne
285
+ const value = this._getValueByPath(pathParams, subPath);
286
+ return value !== undefined && value !== null ? value : undefined;
287
+ }
283
288
  };
284
289
 
290
+ // Expression régulière qui capture les alias sous forme délimitée @{...} ou simple @...
291
+ const regex = /@(?:\{([^}]+)\}|([\w.]+))/g;
292
+
285
293
  const resolveString = (str) => {
286
294
  if (typeof str !== "string") return str;
287
- return str.replace(/@(\w+)/g, (_, key) => {
288
- const fn = aliasMap[key];
289
- const value = typeof fn === "function" ? fn() : undefined;
290
- if (value === undefined || value === null) {
291
- return `@${key}`; // on laisse tel quel si la valeur n'est pas définie
295
+ return str.replace(regex, (_, delimitedAlias, plainAlias) => {
296
+ // Si alias délimité, on traite toute la chaîne à l'intérieur des accolades.
297
+ if (delimitedAlias) {
298
+ // On s'attend à une forme comme "pathParams.id.autre"
299
+ if (delimitedAlias.startsWith("pathParams.")) {
300
+ const subPath = delimitedAlias.substring("pathParams.".length);
301
+ const value = aliasMap.pathParams(subPath);
302
+ return value !== undefined && value !== null ? value : `@{${delimitedAlias}}`;
303
+ } else {
304
+ // Pour d'autres alias délimités, on cherche directement dans aliasMap
305
+ const fn = aliasMap[delimitedAlias];
306
+ if (typeof fn === "function") {
307
+ const value = fn();
308
+ return value !== undefined && value !== null ? value : `@{${delimitedAlias}}`;
309
+ }
310
+ return `@{${delimitedAlias}}`;
311
+ }
312
+ } else if (plainAlias) {
313
+ // Traitement de l'alias simple, potentiellement avec des segments
314
+ // Vérifier s'il commence par "pathParams."
315
+ if (plainAlias.startsWith("pathParams.")) {
316
+ // Extrait la partie après "pathParams." et sépare le premier segment et le reste.
317
+ const match = plainAlias.match(/^pathParams\.([^.]+)(.*)$/);
318
+ if (match) {
319
+ const subKey = match[1]; // ex: "id"
320
+ const remainder = match[2]; // ex: "" ou ".autrechose"
321
+ // Si le reste est vide, ou s'il ne doit pas être remplacé (on remplace toujours le premier segment uniquement)
322
+ const value = aliasMap.pathParams(subKey);
323
+ return value !== undefined && value !== null ? value + remainder : `@${plainAlias}`;
324
+ }
325
+ return `@${plainAlias}`;
326
+ } else {
327
+ // Pour tout autre alias simple sans point ou autre
328
+ const fn = aliasMap[plainAlias];
329
+ if (typeof fn === "function") {
330
+ const value = fn();
331
+ return value !== undefined && value !== null ? value : `@${plainAlias}`;
332
+ }
333
+ return `@${plainAlias}`;
334
+ }
292
335
  }
293
- return value;
336
+ return str;
294
337
  });
295
338
  };
339
+
296
340
 
297
341
  // Vérifie si l'objet est un binaire (par exemple un flux ou un Buffer)
298
342
  const isBinary = (input) => {
@@ -432,6 +476,7 @@ export default class ApiClient extends EventEmitter {
432
476
 
433
477
  // === 1. PathParams ===
434
478
  let resolvedPath = path;
479
+ let resolvedParams = {};
435
480
  if (pathSchema) {
436
481
 
437
482
  // Si auth est "none" et que userId n'est pas défini, on nettoie le schéma
@@ -449,7 +494,7 @@ export default class ApiClient extends EventEmitter {
449
494
  throw new ApiClientError("Path parameter validation failed.", 400, validatePathParams.errors);
450
495
  }
451
496
 
452
- const resolvedParams = this._resolveSpecialValuesInPlace(pathParams);
497
+ resolvedParams = this._resolveSpecialValuesInPlace(pathParams);
453
498
 
454
499
  resolvedPath = resolvedPath.replace(/\{(\w+)\}/g, (_, key) => {
455
500
  const val = resolvedParams[key];
@@ -477,7 +522,7 @@ export default class ApiClient extends EventEmitter {
477
522
  throw new ApiClientError("Request validation failed.", 400, validateRequest.errors);
478
523
  }
479
524
 
480
- data = this._resolveSpecialValuesInPlace(cleanedData);
525
+ data = this._resolveSpecialValuesInPlace(cleanedData, resolvedParams);
481
526
  }
482
527
 
483
528
  // === 3. Payload ===
@@ -910,28 +955,28 @@ export default class ApiClient extends EventEmitter {
910
955
  * @param {Object} obj - L’objet à normaliser.
911
956
  * @returns {Object} L’objet normalisé.
912
957
  */
913
- _normalizeDatesRecursively(obj) {
914
- if (obj === null || typeof obj !== "object") return obj;
915
-
916
- // Si c'est un tableau, on traite chaque élément récursivement.
917
- if (Array.isArray(obj)) {
918
- return obj.map(item => this._normalizeDatesRecursively(item));
919
- }
920
-
921
- Object.keys(obj).forEach((key) => {
922
- if (this._dateFields.includes(key)) {
923
- if (obj[key] && typeof obj[key] === "object" && obj[key].sec && typeof obj[key].sec === "number") {
924
- obj[key] = { $date: obj[key].sec * 1000 };
925
- } else if (typeof obj[key] === "number") {
926
- obj[key] = { $date: obj[key] * 1000 };
927
- }
928
- }
929
- if (obj[key] && typeof obj[key] === "object") {
930
- this._normalizeDatesRecursively(obj[key]);
931
- }
932
- });
933
- return obj;
934
- }
958
+ // _normalizeDatesRecursively(obj) {
959
+ // if (obj === null || typeof obj !== "object") return obj;
960
+
961
+ // // Si c'est un tableau, on traite chaque élément récursivement.
962
+ // if (Array.isArray(obj)) {
963
+ // return obj.map(item => this._normalizeDatesRecursively(item));
964
+ // }
965
+
966
+ // Object.keys(obj).forEach((key) => {
967
+ // if (this._dateFields.includes(key)) {
968
+ // if (obj[key] && typeof obj[key] === "object" && obj[key].sec && typeof obj[key].sec === "number") {
969
+ // obj[key] = { $date: obj[key].sec * 1000 };
970
+ // } else if (typeof obj[key] === "number") {
971
+ // obj[key] = { $date: obj[key] * 1000 };
972
+ // }
973
+ // }
974
+ // if (obj[key] && typeof obj[key] === "object") {
975
+ // this._normalizeDatesRecursively(obj[key]);
976
+ // }
977
+ // });
978
+ // return obj;
979
+ // }
935
980
 
936
981
  /**
937
982
  * Parcourt récursivement un objet pour normaliser les chemins d'images.
@@ -942,30 +987,30 @@ export default class ApiClient extends EventEmitter {
942
987
  * @param {Object} obj - L’objet à normaliser.
943
988
  * @returns {Object} L’objet normalisé.
944
989
  */
945
- _normalizeImagesRecursively(obj) {
946
- if (obj === null || typeof obj !== "object") return obj;
947
-
948
- // Si c'est un tableau, on traite chaque élément récursivement.
949
- if (Array.isArray(obj)) {
950
- return obj.map(item => this._normalizeImagesRecursively(item));
951
- }
952
-
953
- Object.keys(obj).forEach((key) => {
954
- if (this._imageFields.includes(key) && typeof obj[key] === "string" && obj[key].trim() !== "") {
955
- obj[key] = this._ensureFullURL(obj[key]);
956
- }
957
-
958
- // Cas particulier pour content.image
959
- if (key === "content" && obj[key]?.image) {
960
- obj[key].image = this._ensureFullURL(obj[key].image);
961
- }
962
-
963
- if (obj[key] && typeof obj[key] === "object") {
964
- this._normalizeImagesRecursively(obj[key]);
965
- }
966
- });
967
- return obj;
968
- }
990
+ // _normalizeImagesRecursively(obj) {
991
+ // if (obj === null || typeof obj !== "object") return obj;
992
+
993
+ // // Si c'est un tableau, on traite chaque élément récursivement.
994
+ // if (Array.isArray(obj)) {
995
+ // return obj.map(item => this._normalizeImagesRecursively(item));
996
+ // }
997
+
998
+ // Object.keys(obj).forEach((key) => {
999
+ // if (this._imageFields.includes(key) && typeof obj[key] === "string" && obj[key].trim() !== "") {
1000
+ // obj[key] = this._ensureFullURL(obj[key]);
1001
+ // }
1002
+
1003
+ // // Cas particulier pour content.image
1004
+ // if (key === "content" && obj[key]?.image) {
1005
+ // obj[key].image = this._ensureFullURL(obj[key].image);
1006
+ // }
1007
+
1008
+ // if (obj[key] && typeof obj[key] === "object") {
1009
+ // this._normalizeImagesRecursively(obj[key]);
1010
+ // }
1011
+ // });
1012
+ // return obj;
1013
+ // }
969
1014
 
970
1015
  /**
971
1016
  * Parcourt récursivement un objet ou un tableau pour normaliser les identifiants (ID).
@@ -975,33 +1020,33 @@ export default class ApiClient extends EventEmitter {
975
1020
  * @param {Object|Array} obj - L'objet ou le tableau à normaliser.
976
1021
  * @returns {Object|Array} L'objet ou le tableau normalisé.
977
1022
  */
978
- _normalizeIdRecursively(obj) {
979
- if (obj === null || typeof obj !== "object") return obj;
980
-
981
- // Si c'est un tableau, on traite chaque élément récursivement.
982
- if (Array.isArray(obj)) {
983
- return obj.map(item => this._normalizeIdRecursively(item));
984
- }
985
-
986
- // Vérifier et normaliser l'ID dans l'objet
987
- if (obj._id && obj._id.$id && /^[0-9a-fA-F]{24}$/.test(obj._id.$id)) {
988
- obj.id = obj._id.$id;
989
- obj._id = { $type: "oid", $value: obj._id.$id };
990
- }
991
- if (obj.id && obj.id.$id && /^[0-9a-fA-F]{24}$/.test(obj.id.$id)) {
992
- obj._id = { $type: "oid", $value: obj.id.$id };
993
- obj.id = obj.id.$id;
994
- }
995
-
996
- // Appel récursif pour chaque propriété de l'objet
997
- Object.keys(obj).forEach(key => {
998
- if (obj[key] && typeof obj[key] === "object") {
999
- obj[key] = this._normalizeIdRecursively(obj[key]);
1000
- }
1001
- });
1002
-
1003
- return obj;
1004
- }
1023
+ // _normalizeIdRecursively(obj) {
1024
+ // if (obj === null || typeof obj !== "object") return obj;
1025
+
1026
+ // // Si c'est un tableau, on traite chaque élément récursivement.
1027
+ // if (Array.isArray(obj)) {
1028
+ // return obj.map(item => this._normalizeIdRecursively(item));
1029
+ // }
1030
+
1031
+ // // Vérifier et normaliser l'ID dans l'objet
1032
+ // if (obj._id && obj._id.$id && /^[0-9a-fA-F]{24}$/.test(obj._id.$id)) {
1033
+ // obj.id = obj._id.$id;
1034
+ // obj._id = { $type: "oid", $value: obj._id.$id };
1035
+ // }
1036
+ // if (obj.id && obj.id.$id && /^[0-9a-fA-F]{24}$/.test(obj.id.$id)) {
1037
+ // obj._id = { $type: "oid", $value: obj.id.$id };
1038
+ // obj.id = obj.id.$id;
1039
+ // }
1040
+
1041
+ // // Appel récursif pour chaque propriété de l'objet
1042
+ // Object.keys(obj).forEach(key => {
1043
+ // if (obj[key] && typeof obj[key] === "object") {
1044
+ // obj[key] = this._normalizeIdRecursively(obj[key]);
1045
+ // }
1046
+ // });
1047
+
1048
+ // return obj;
1049
+ // }
1005
1050
 
1006
1051
  /**
1007
1052
  * Normalise récursivement les valeurs booléennes.
@@ -1010,22 +1055,22 @@ export default class ApiClient extends EventEmitter {
1010
1055
  * @param {any} data - L'objet, le tableau ou la valeur à normaliser.
1011
1056
  * @returns {any} La donnée normalisée.
1012
1057
  */
1013
- _normalizeBooleansRecursively(data) {
1014
- if (typeof data === "string") {
1015
- if (data === "true") return true;
1016
- if (data === "false") return false;
1017
- return data;
1018
- }
1019
- if (Array.isArray(data)) {
1020
- return data.map(item => this._normalizeBooleansRecursively(item));
1021
- }
1022
- if (data !== null && typeof data === "object") {
1023
- Object.keys(data).forEach(key => {
1024
- data[key] = this._normalizeBooleansRecursively(data[key]);
1025
- });
1026
- }
1027
- return data;
1028
- }
1058
+ // _normalizeBooleansRecursively(data) {
1059
+ // if (typeof data === "string") {
1060
+ // if (data === "true") return true;
1061
+ // if (data === "false") return false;
1062
+ // return data;
1063
+ // }
1064
+ // if (Array.isArray(data)) {
1065
+ // return data.map(item => this._normalizeBooleansRecursively(item));
1066
+ // }
1067
+ // if (data !== null && typeof data === "object") {
1068
+ // Object.keys(data).forEach(key => {
1069
+ // data[key] = this._normalizeBooleansRecursively(data[key]);
1070
+ // });
1071
+ // }
1072
+ // return data;
1073
+ // }
1029
1074
 
1030
1075
  /**
1031
1076
  * Transforme une chaîne "true" ou "false" en booléen.