@communecter/cocolight-api-client 1.0.8 → 1.0.9

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/src/api/User.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { ApiResponseError } from "../error.js";
2
- import { EntityMixin } from "./EntityMixin.js";
3
- import { NewsMixin } from "./NewsMixin.js";
4
- import { UserMixin } from "./UserMixin.js";
5
- import { UtilMixin } from "./UtilMixin.js";
2
+ import { News } from "./News.js";
3
+ import { EntityMixin } from "../mixin/EntityMixin.js";
4
+ import { NewsMixin } from "../mixin/NewsMixin.js";
5
+ import { UserMixin } from "../mixin/UserMixin.js";
6
+ import { UtilMixin } from "../mixin/UtilMixin.js";
6
7
 
7
8
  // User.js
8
9
  export class User {
@@ -16,13 +17,29 @@ export class User {
16
17
  * @param {ApiClient} apiClient - L'instance d'ApiClient.
17
18
  * @param {Object} identifier - Objet contenant { id } ou { slug }.
18
19
  * @param {Object} [data={}] - Données supplémentaires.
20
+ * @param {Object} [deps={}] - Dépendances injectées (ex: User).
21
+ * @param {EndpointApi} deps.EndpointApi - Classe EndpointApi pour éviter les dépendances circulaires.
19
22
  */
20
23
 
21
- constructor(apiClient, { id, slug } = {}, data = {}) {
24
+ constructor(apiClient, { id, slug } = {}, data = {}, deps = {}) {
25
+ if(!deps.EndpointApi){
26
+ throw new Error("EndpointApi class must be injected to avoid circular dependency.");
27
+ }
22
28
  if (!id && !slug) {
23
29
  throw new Error("Vous devez fournir un id ou un slug pour créer un User.");
24
30
  }
25
31
  this.apiClient = apiClient;
32
+ this.deps = deps;
33
+
34
+ // Gérer les deux cas : fonction constructeur ou instance
35
+ if (typeof deps.EndpointApi === "function") {
36
+ this.endpointApi = new deps.EndpointApi(this.apiClient);
37
+ } else if (typeof deps.EndpointApi === "object") {
38
+ this.endpointApi = deps.EndpointApi;
39
+ } else {
40
+ throw new Error("deps.EndpointApi doit être une classe ou une instance valide.");
41
+ }
42
+
26
43
  this.#id = id || null;
27
44
  this.#slug = slug || null;
28
45
  this.#data = data;
@@ -81,7 +98,7 @@ export class User {
81
98
  async getProfil() {
82
99
  return this.apiClient.safeCall(async () => {
83
100
  if (this.isMe) {
84
- const data = await this._meInfoUrl();
101
+ const data = await this.endpointApi.meInfoUrl();
85
102
  return data;
86
103
  } else {
87
104
  const data = await this.getPublicProfile();
@@ -95,7 +112,7 @@ export class User {
95
112
  * Constant : CHANGE_PASSWORD
96
113
  */
97
114
  async changePassword(data = {}) {
98
- return this.callIsMe(() => this._changePassword(data));
115
+ return this.callIsMe(() => this.endpointApi.changePassword(data));
99
116
  }
100
117
 
101
118
  /**
@@ -103,7 +120,7 @@ export class User {
103
120
  * Constant : DELETE_ACCOUNT
104
121
  */
105
122
  async delete(data = {}) {
106
- return this.callIsMe(() => this._deleteAccount(data));
123
+ return this.callIsMe(() => this.endpointApi.deleteAccount(data));
107
124
  }
108
125
 
109
126
  /**
@@ -111,7 +128,7 @@ export class User {
111
128
  * Constant : UPDATE_SETTINGS
112
129
  */
113
130
  async updateSettings(data = {}) {
114
- return this.callIsMe(() => this._updateSettings(data));
131
+ return this.callIsMe(() => this.endpointApi.updateSettings(data));
115
132
  }
116
133
 
117
134
  /**
@@ -119,7 +136,7 @@ export class User {
119
136
  * Constant : UPDATE_BLOCK_DESCRIPTION
120
137
  */
121
138
  async updateDescription(data = {}) {
122
- return this.callIsMe(() => this._updateBlockDescription(data));
139
+ return this.callIsMe(() => this.endpointApi.updateBlockDescription(data));
123
140
  }
124
141
 
125
142
  /**
@@ -127,7 +144,7 @@ export class User {
127
144
  * Constant : UPDATE_BLOCK_INFO
128
145
  */
129
146
  async updateInfo(data = {}) {
130
- return this.callIsMe(() => this._updateBlockInfo(data));
147
+ return this.callIsMe(() => this.endpointApi.updateBlockInfo(data));
131
148
  }
132
149
 
133
150
  /**
@@ -135,7 +152,7 @@ export class User {
135
152
  * Constant : UPDATE_BLOCK_SOCIAL
136
153
  */
137
154
  async updateSocial(data = {}) {
138
- return this.callIsMe(() => this._updateBlockSocial(data));
155
+ return this.callIsMe(() => this.endpointApi.updateBlockSocial(data));
139
156
  }
140
157
 
141
158
  /**
@@ -143,7 +160,7 @@ export class User {
143
160
  * Constant : UPDATE_BLOCK_LOCALITY
144
161
  */
145
162
  async updateLocality(data = {}) {
146
- return this.callIsMe(() => this._updateBlockLocality(data));
163
+ return this.callIsMe(() => this.endpointApi.updateBlockLocality(data));
147
164
  }
148
165
 
149
166
  /**
@@ -152,14 +169,14 @@ export class User {
152
169
  */
153
170
  async updateSlug(slug) {
154
171
  try {
155
- await this._check({ slug });
172
+ await this.endpointApi.check({ slug });
156
173
  } catch (error) {
157
174
  if(error instanceof ApiResponseError) {
158
175
  throw new ApiResponseError("Erreur lors de la vérification du slug.", error.status, error.data);
159
176
  }
160
177
  throw error;
161
178
  }
162
- return this.callIsMe(() => this._updateBlockSlug({ slug }));
179
+ return this.callIsMe(() => this.endpointApi.updateBlockSlug({ slug }));
163
180
  }
164
181
 
165
182
  /**
@@ -168,7 +185,7 @@ export class User {
168
185
  */
169
186
  async updateImageProfil(image) {
170
187
  image = await this.validateImage(image);
171
- return this.callIsMe(() => this._profilImage({ profil_avatar: image }));
188
+ return this.callIsMe(() => this.endpointApi.profilImage({ profil_avatar: image }));
172
189
  }
173
190
 
174
191
  /**
@@ -183,42 +200,29 @@ export class User {
183
200
  // is not me add id
184
201
  data.pathParams = { id: this.id };
185
202
  }
186
- return this._getNews(data);
203
+ const arrayObjetNews = await this.endpointApi.getNews(data);
204
+ if(!Array.isArray(arrayObjetNews)){
205
+ throw new ApiResponseError("Erreur lors de la récupération des actualités.", 500, arrayObjetNews);
206
+ }
207
+ const rawNewsList = arrayObjetNews.map((newsData) =>
208
+ News.fromServerData(newsData, this, { User, EndpointApi: this.deps.EndpointApi })
209
+ );
210
+
211
+ return this._createFilteredProxy(rawNewsList);
187
212
  }
188
213
 
189
- /**
190
- * Ajouter une actualité : Ajoute une nouvelle actualité.
191
- * Constant : ADD_NEWS
192
- * @param {Object} data - Les données à envoyer.
193
- * @param {string} data.text - Contenu de l’actualité
194
- * @param {string} data.scope - Portée de l'actualité (ex: public, privé...) (default: "public")
195
- * @param {boolean} data.markdownActive - Markdown activé (true/false) (default: true)
196
- * @param {string} data.type - Type de l'objet, toujours 'news'. (default: "news")
197
- * @param {boolean} data.json - Indique que la réponse est au format JSON. (default: true)
198
- * @param {array | string} data.tags - Tags : "" pour effacer tous les tags, ou tableau de mots-clés.
199
- * @param {object} data.mediaImg - Optionnel. Informations sur les images associées à la news.
200
- * @param {number} data.mediaImg.countImages - Nombre d'images.
201
- * @param {Array<string>} data.mediaImg.images - Liste des identifiants ou chemins d'images.
202
- * @param {object} data.mediaFile - Optionnel. Informations sur les fichiers associés à la news.
203
- * @param {number} data.mediaFile.countFiles - Nombre de fichiers.
204
- * @param {Array<string>} data.mediaFile.files - Liste des identifiants ou chemins de fichiers.
205
- * @param {object} data.mentions - Liste des mentions sous forme d'objet avec des clés dynamiques représentant l'indice de la mention.
206
- * @param {Object.<string, object>} data.mentions - Objet dont les clés keys matching ^[0-9]+$
207
- * @returns {Promise<Object>} - Les données de réponse.
208
- * @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
209
- * @throws {ApiAuthenticationError} - En cas d'erreur d'authentification.
210
- * @throws {Error} - En cas d'erreur inattendue.
211
- */
212
- async addNews(data = {}) {
213
- if (data.parentId) {
214
- delete data.parentId;
215
- }
216
- if (data.parentType) {
217
- delete data.parentType;
214
+ async news(newsData = {}, ) {
215
+ try {
216
+ const news = new News(this, newsData, { User, EndpointApi : this.deps.EndpointApi });
217
+ if (newsData.id) {
218
+ await news.get();
219
+ }
220
+ return news;
221
+ } catch (error) {
222
+ console.error("[Api.user.news] Erreur lors de la création d'une instance news :", error.message);
223
+ throw error;
218
224
  }
219
- return this.callIsMe(() => this._addNews(data));
220
225
  }
221
-
222
226
  }
223
227
 
224
228
  // Incorporation des mixins dans User
@@ -1,6 +1,7 @@
1
1
  // UserApi.js
2
2
  import ApiClient from "../ApiClient.js";
3
3
  import { ApiResponseError } from "../error.js";
4
+ import EndpointApi from "./EndpointApi.js";
4
5
  import { User } from "./User.js";
5
6
 
6
7
  export class UserApi {
@@ -15,7 +16,7 @@ export class UserApi {
15
16
  // Appel à un endpoint d'authentification
16
17
  const response = await this.client.callEndpoint("AUTHENTICATE_URL", { email, password });
17
18
  // Création d'une instance de LoggedInUser à partir des données reçues
18
- this.loggedUser = new User(this.client, { id: response.data.user.id }, response.data.user);
19
+ this.loggedUser = new User(this.client, { id: response.data.user.id }, response.data.user, { EndpointApi });
19
20
  return this.loggedUser;
20
21
  });
21
22
  }