@communecter/cocolight-api-client 1.0.43 → 1.0.45

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.43",
3
+ "version": "1.0.45",
4
4
  "description": "Client Axios simplifié pour l'API cocolight",
5
5
  "repository": {
6
6
  "type": "git",
package/src/Api.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Api.js
2
2
  import { Badge } from "./api/Badge.js";
3
3
  import EndpointApi from "./api/EndpointApi.js";
4
- import { registerEntity } from "./api/EntityRegistry.js";
4
+ import { createFromCollection, registerEntity } from "./api/EntityRegistry.js";
5
5
  import { Event } from "./api/Event.js";
6
6
  import { News } from "./api/News.js";
7
7
  import { Organization } from "./api/Organization.js";
@@ -9,7 +9,7 @@ import { Poi } from "./api/Poi.js";
9
9
  import { Project } from "./api/Project.js";
10
10
  import { User } from "./api/User.js";
11
11
  import { UserApi } from "./api/UserApi.js";
12
- import { ApiAuthenticationError, ApiClientError, ApiError } from "./error.js";
12
+ import { ApiAuthenticationError, ApiClientError, ApiError, ApiResponseError } from "./error.js";
13
13
 
14
14
  registerEntity("User", User);
15
15
  registerEntity("Organization", Organization);
@@ -200,6 +200,31 @@ export default class Api {
200
200
  }
201
201
  }
202
202
 
203
+ async entitySlug(slug) {
204
+ if (slug) {
205
+ try {
206
+ const data = await this.endpointApi.getElementsKey({
207
+ pathParams:{
208
+ slug: slug
209
+ }
210
+ });
211
+ if(data?.contextId && data?.contextType) {
212
+ const entity = createFromCollection(data?.contextType, this._client, { id: data?.contextId });
213
+ await entity.get();
214
+ return entity;
215
+ } else {
216
+ throw new ApiResponseError(`Le slug ${slug} n'est pas valide`, 200, data);
217
+ }
218
+ } catch (error) {
219
+ if(error instanceof ApiResponseError) {
220
+ throw new ApiResponseError(`Impossible de récupérer l'identifiant pour le slug ${slug}`, error.status, error.responseData);
221
+ } else {
222
+ throw error;
223
+ }
224
+ }
225
+ }
226
+ }
227
+
203
228
  /**
204
229
  * Retourne l'instance d'ApiClient.
205
230
  *
package/src/ApiClient.js CHANGED
@@ -1514,7 +1514,7 @@ export default class ApiClient extends EventEmitter {
1514
1514
  if (!isValid) {
1515
1515
  this._startBeforeEndValidate.errors = [
1516
1516
  {
1517
- instancePath: "", // ou "." si tu veux le chemin actuel
1517
+ instancePath: "/startDate", // ou "." si tu veux le chemin actuel
1518
1518
  schemaPath: "#/startBeforeEnd",
1519
1519
  keyword: "startBeforeEnd",
1520
1520
  params: {},
@@ -295,12 +295,17 @@ export class BaseEntity {
295
295
 
296
296
 
297
297
  _toRawDeep(obj) {
298
+
299
+ if (!this || typeof this._toRawDeep !== "function") {
300
+ throw new Error("`this._toRawDeep` is not bound correctly. Use a lambda to preserve context.");
301
+ }
302
+
298
303
  if (isSignal(obj)) {
299
304
  return this._toRawDeep(obj.value);
300
305
  }
301
306
 
302
307
  if (Array.isArray(obj)) {
303
- return obj.map(this._toRawDeep);
308
+ return obj.map((item) => this._toRawDeep(item));
304
309
  }
305
310
 
306
311
  if (typeof obj === "object" && obj !== null) {
@@ -1373,6 +1378,37 @@ export class BaseEntity {
1373
1378
  }
1374
1379
  }
1375
1380
 
1381
+ /**
1382
+ * Récupère et lie une entité à partir de son slug.
1383
+ * @param {string} slug - Le slug de l'entité.
1384
+ * @return {Promise<Object|null>} L'entité liée ou null.
1385
+ */
1386
+ async entityBySlug(slug) {
1387
+ if (!slug) {
1388
+ throw new ApiError("Le slug est requis.");
1389
+ }
1390
+ try {
1391
+ const data = await this.endpointApi.getElementsKey({
1392
+ pathParams:{
1393
+ slug: slug
1394
+ }
1395
+ });
1396
+ if(data?.contextId && data?.contextType) {
1397
+ const entity = await this.entity(data.contextType, { id: data.contextId });
1398
+ return entity;
1399
+ } else {
1400
+ throw new ApiResponseError(`Le slug ${slug} n'est pas valide`, 200, data);
1401
+ }
1402
+ } catch (error) {
1403
+ if(error instanceof ApiResponseError) {
1404
+ throw new ApiResponseError(`Impossible de récupérer l'identifiant pour le slug ${slug}`, error.status, error.responseData);
1405
+ } else {
1406
+ throw error;
1407
+ }
1408
+ }
1409
+ }
1410
+
1411
+
1376
1412
  /**
1377
1413
  * Construit dynamiquement des filtres sur un lien entre entités
1378
1414
  *
@@ -63,4 +63,90 @@ function _getEntityMeta(entityType, __entityTag) {
63
63
  };
64
64
 
65
65
  return map[entityType] || null;
66
- }
66
+ }
67
+
68
+ /**
69
+ * Crée une instance d'entité basée sur le seul nom de la collection.
70
+ * @param {string} collection - ex: "citoyens", "projects", ...
71
+ * @param {Object} parent - ApiClient ou entité parente
72
+ * @returns {BaseEntity|null}
73
+ */
74
+ export function createFromCollection(collection, parent = null, data = {}) {
75
+ const _collectionMap = {
76
+ citoyens: {
77
+ entityTag: "User",
78
+ meta: tag => _buildMeta(tag, { remove: [] })
79
+ },
80
+ organizations: {
81
+ entityTag: "Organization",
82
+ meta: tag => _buildMeta(tag, { remove: [] })
83
+ },
84
+ projects: {
85
+ entityTag: "Project",
86
+ meta: tag => _buildMeta(tag, { remove: [] })
87
+ },
88
+ events: {
89
+ entityTag: "Event",
90
+ meta: tag => _buildMeta(tag, { remove: ["Badge"] })
91
+ },
92
+ poi: {
93
+ entityTag: "Poi",
94
+ meta: tag => _buildMeta(tag, { remove: ["Badge","News"] })
95
+ },
96
+ news: {
97
+ entityTag: "News",
98
+ meta: tag => _buildMeta(tag, { remove: [] })
99
+ },
100
+ badges: {
101
+ entityTag: "Badge",
102
+ meta: tag => _buildCustomMeta(tag)
103
+ }
104
+ };
105
+ const entry = _collectionMap[collection];
106
+ if (!entry) {
107
+ console.warn(`Collection inconnue : '${collection}'`);
108
+ return null;
109
+ }
110
+ const { entityTag, meta } = entry;
111
+ const { entityClass, deps } = meta(entityTag);
112
+ return new entityClass(parent, data, deps);
113
+ }
114
+
115
+ /**
116
+ * Construit entityClass et deps pour une collection standard.
117
+ * @param {string} tag
118
+ * @param {{remove: string[]}} options
119
+ */
120
+ function _buildMeta(tag, options) {
121
+ const EntityClass = EntityRegistry.get(tag);
122
+ const allDeps = {
123
+ EndpointApi: EntityRegistry.get("EndpointApi"),
124
+ User: EntityRegistry.get("User"),
125
+ Organization: EntityRegistry.get("Organization"),
126
+ Project: EntityRegistry.get("Project"),
127
+ Event: EntityRegistry.get("Event"),
128
+ Poi: EntityRegistry.get("Poi"),
129
+ Badge: EntityRegistry.get("Badge"),
130
+ News: EntityRegistry.get("News")
131
+ };
132
+ // inject self
133
+ allDeps[tag] = EntityClass;
134
+ // remove undesired
135
+ options.remove.forEach(dep => delete allDeps[dep]);
136
+ return { entityClass: EntityClass, deps: allDeps };
137
+ }
138
+
139
+ /**
140
+ * Cas spécifique des badges : seules certaines dépendances.
141
+ */
142
+ function _buildCustomMeta(tag) {
143
+ const EntityClass = EntityRegistry.get(tag);
144
+ const deps = {
145
+ EndpointApi: EntityRegistry.get("EndpointApi"),
146
+ User: EntityRegistry.get("User"),
147
+ Organization: EntityRegistry.get("Organization"),
148
+ Project: EntityRegistry.get("Project")
149
+ };
150
+ deps[tag] = EntityClass;
151
+ return { entityClass: EntityClass, deps };
152
+ }
@@ -237,7 +237,7 @@ export class Organization extends BaseEntity {
237
237
  */
238
238
  async poi(poiData = {}) {
239
239
  if(!this.isAdmin()){
240
- throw new ApiError("Vous n'avez pas les droits pour créer un projet dans cette organisation", 403);
240
+ throw new ApiError("Vous n'avez pas les droits pour créer un poi dans cette organisation", 403);
241
241
  }
242
242
  return super.poi(poiData);
243
243
  }
@@ -251,7 +251,7 @@ export class Organization extends BaseEntity {
251
251
  */
252
252
  async event(eventData = {}) {
253
253
  if(!this.isAdmin()){
254
- throw new ApiError("Vous n'avez pas les droits pour créer un projet dans cette organisation", 403);
254
+ throw new ApiError("Vous n'avez pas les droits pour créer un event dans cette organisation", 403);
255
255
  }
256
256
  return super.event(eventData);
257
257
  }
@@ -265,7 +265,7 @@ export class Organization extends BaseEntity {
265
265
  */
266
266
  async badge(badgeData = {}) {
267
267
  if(!this.isAdmin()){
268
- throw new ApiError("Vous n'avez pas les droits pour créer un projet dans cette organisation", 403);
268
+ throw new ApiError("Vous n'avez pas les droits pour créer un badge dans cette organisation", 403);
269
269
  }
270
270
  return super.badge(badgeData);
271
271
  }