@communecter/cocolight-api-client 1.0.42 → 1.0.44

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.42",
3
+ "version": "1.0.44",
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
@@ -102,6 +102,8 @@ export default class ApiClient extends EventEmitter {
102
102
  this._ajv = new Ajv({ strict: false, useDefaults: true, allErrors: true, verbose: true });
103
103
  addFormats(this._ajv);
104
104
 
105
+ this._ajv.addFormat("textarea", true);
106
+
105
107
  this._ajv.addKeyword({
106
108
  keyword: "startBeforeEnd",
107
109
  type: "object",
@@ -1512,7 +1514,7 @@ export default class ApiClient extends EventEmitter {
1512
1514
  if (!isValid) {
1513
1515
  this._startBeforeEndValidate.errors = [
1514
1516
  {
1515
- instancePath: "", // ou "." si tu veux le chemin actuel
1517
+ instancePath: "/startDate", // ou "." si tu veux le chemin actuel
1516
1518
  schemaPath: "#/startBeforeEnd",
1517
1519
  keyword: "startBeforeEnd",
1518
1520
  params: {},
@@ -1373,6 +1373,37 @@ export class BaseEntity {
1373
1373
  }
1374
1374
  }
1375
1375
 
1376
+ /**
1377
+ * Récupère et lie une entité à partir de son slug.
1378
+ * @param {string} slug - Le slug de l'entité.
1379
+ * @return {Promise<Object|null>} L'entité liée ou null.
1380
+ */
1381
+ async entityBySlug(slug) {
1382
+ if (!slug) {
1383
+ throw new ApiError("Le slug est requis.");
1384
+ }
1385
+ try {
1386
+ const data = await this.endpointApi.getElementsKey({
1387
+ pathParams:{
1388
+ slug: slug
1389
+ }
1390
+ });
1391
+ if(data?.contextId && data?.contextType) {
1392
+ const entity = await this.entity(data.contextType, { id: data.contextId });
1393
+ return entity;
1394
+ } else {
1395
+ throw new ApiResponseError(`Le slug ${slug} n'est pas valide`, 200, data);
1396
+ }
1397
+ } catch (error) {
1398
+ if(error instanceof ApiResponseError) {
1399
+ throw new ApiResponseError(`Impossible de récupérer l'identifiant pour le slug ${slug}`, error.status, error.responseData);
1400
+ } else {
1401
+ throw error;
1402
+ }
1403
+ }
1404
+ }
1405
+
1406
+
1376
1407
  /**
1377
1408
  * Construit dynamiquement des filtres sur un lien entre entités
1378
1409
  *
@@ -3025,7 +3025,7 @@ export interface AddEventData {
3025
3025
  };
3026
3026
  };
3027
3027
  /**
3028
- * Nom du poi
3028
+ * Nom de l'événement
3029
3029
  */
3030
3030
  name: string;
3031
3031
  /**
@@ -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
  }