@communecter/cocolight-api-client 1.0.7 → 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/README.md +4 -4
- package/dist/22.cocolight-api-client.mjs.js +1 -0
- package/dist/320.cocolight-api-client.browser.js +1 -0
- package/dist/931.cocolight-api-client.browser.js +1 -0
- package/dist/931.cocolight-api-client.cjs +1 -0
- package/dist/cocolight-api-client.browser.js +7 -6
- package/dist/cocolight-api-client.browser.js.LICENSE.txt +1 -0
- package/dist/cocolight-api-client.cjs +1 -1
- package/dist/cocolight-api-client.mjs.js +1 -1
- package/package.json +13 -4
- package/src/Api.js +12 -8
- package/src/ApiClient.js +118 -10
- package/src/api/EndpointApi.js +1534 -0
- package/src/api/News.js +286 -0
- package/src/api/Organization.js +162 -8
- package/src/api/Project.js +163 -8
- package/src/api/User.js +150 -9
- package/src/api/UserApi.js +2 -1
- package/src/endpoints.module.js +2 -2
- package/src/error.js +11 -0
- package/src/index.js +2 -1
- package/src/mixin/EntityMixin.js +48 -0
- package/src/mixin/NewsMixin.js +8 -0
- package/src/mixin/UserMixin.js +8 -0
- package/src/mixin/UtilMixin.js +246 -0
- package/src/utils/stream-utils.node.js +10 -0
- package/src/api/EntityMixin.js +0 -43
package/src/api/User.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ApiResponseError } from "../error.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";
|
|
2
7
|
|
|
3
8
|
// User.js
|
|
4
9
|
export class User {
|
|
@@ -12,13 +17,29 @@ export class User {
|
|
|
12
17
|
* @param {ApiClient} apiClient - L'instance d'ApiClient.
|
|
13
18
|
* @param {Object} identifier - Objet contenant { id } ou { slug }.
|
|
14
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.
|
|
15
22
|
*/
|
|
16
23
|
|
|
17
|
-
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
|
+
}
|
|
18
28
|
if (!id && !slug) {
|
|
19
29
|
throw new Error("Vous devez fournir un id ou un slug pour créer un User.");
|
|
20
30
|
}
|
|
21
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
|
+
|
|
22
43
|
this.#id = id || null;
|
|
23
44
|
this.#slug = slug || null;
|
|
24
45
|
this.#data = data;
|
|
@@ -28,6 +49,10 @@ export class User {
|
|
|
28
49
|
get id() {
|
|
29
50
|
return this.#id;
|
|
30
51
|
}
|
|
52
|
+
|
|
53
|
+
_id(newId) {
|
|
54
|
+
this.#id = newId;
|
|
55
|
+
}
|
|
31
56
|
|
|
32
57
|
get slug() {
|
|
33
58
|
return this.#slug;
|
|
@@ -70,20 +95,136 @@ export class User {
|
|
|
70
95
|
*
|
|
71
96
|
* @returns {Promise<Object>} Le profil complet.
|
|
72
97
|
*/
|
|
73
|
-
async
|
|
98
|
+
async getProfil() {
|
|
74
99
|
return this.apiClient.safeCall(async () => {
|
|
75
100
|
if (this.isMe) {
|
|
76
|
-
const
|
|
77
|
-
return
|
|
101
|
+
const data = await this.endpointApi.meInfoUrl();
|
|
102
|
+
return data;
|
|
78
103
|
} else {
|
|
79
|
-
const
|
|
80
|
-
return
|
|
104
|
+
const data = await this.getPublicProfile();
|
|
105
|
+
return data;
|
|
81
106
|
}
|
|
82
107
|
});
|
|
83
108
|
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Changer le mot de passe : Permet de changer le mot de passe d'un utilisateur.
|
|
112
|
+
* Constant : CHANGE_PASSWORD
|
|
113
|
+
*/
|
|
114
|
+
async changePassword(data = {}) {
|
|
115
|
+
return this.callIsMe(() => this.endpointApi.changePassword(data));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Supprimer un compte : Permet de supprimer un compte utilisateur.
|
|
120
|
+
* Constant : DELETE_ACCOUNT
|
|
121
|
+
*/
|
|
122
|
+
async delete(data = {}) {
|
|
123
|
+
return this.callIsMe(() => this.endpointApi.deleteAccount(data));
|
|
124
|
+
}
|
|
84
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Mettre à jour les paramètres utilisateur : Mise à jour des paramètres spécifiques d'un utilisateur.
|
|
128
|
+
* Constant : UPDATE_SETTINGS
|
|
129
|
+
*/
|
|
130
|
+
async updateSettings(data = {}) {
|
|
131
|
+
return this.callIsMe(() => this.endpointApi.updateSettings(data));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Mettre à jour la description d'un élément : Permet de mettre à jour la description courte et complète d'un élément.
|
|
136
|
+
* Constant : UPDATE_BLOCK_DESCRIPTION
|
|
137
|
+
*/
|
|
138
|
+
async updateDescription(data = {}) {
|
|
139
|
+
return this.callIsMe(() => this.endpointApi.updateBlockDescription(data));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Mettre à jour les informations d'un élément : Permet de mettre à jour les informations générales d'un élément (nom, contacts, etc.).
|
|
144
|
+
* Constant : UPDATE_BLOCK_INFO
|
|
145
|
+
*/
|
|
146
|
+
async updateInfo(data = {}) {
|
|
147
|
+
return this.callIsMe(() => this.endpointApi.updateBlockInfo(data));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Mettre à jour les réseaux sociaux d'un élément : Permet de mettre à jour les liens vers les réseaux sociaux d'un élément.
|
|
152
|
+
* Constant : UPDATE_BLOCK_SOCIAL
|
|
153
|
+
*/
|
|
154
|
+
async updateSocial(data = {}) {
|
|
155
|
+
return this.callIsMe(() => this.endpointApi.updateBlockSocial(data));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Mettre à jour les localités d'un élément : Permet de mettre à jour l'adresse et les informations géographiques d'un élément.
|
|
160
|
+
* Constant : UPDATE_BLOCK_LOCALITY
|
|
161
|
+
*/
|
|
162
|
+
async updateLocality(data = {}) {
|
|
163
|
+
return this.callIsMe(() => this.endpointApi.updateBlockLocality(data));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Mettre à jour le slug d'un élément : Permet de mettre à jour le slug pour une URL simplifiée.
|
|
168
|
+
* Constant : UPDATE_BLOCK_SLUG
|
|
169
|
+
*/
|
|
170
|
+
async updateSlug(slug) {
|
|
171
|
+
try {
|
|
172
|
+
await this.endpointApi.check({ slug });
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if(error instanceof ApiResponseError) {
|
|
175
|
+
throw new ApiResponseError("Erreur lors de la vérification du slug.", error.status, error.data);
|
|
176
|
+
}
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
return this.callIsMe(() => this.endpointApi.updateBlockSlug({ slug }));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Mettre à jour l'image de profil : Permet de mettre à jour l'image de profil d'un utilisateur ou d'une entité.
|
|
184
|
+
* Constant : PROFIL_IMAGE
|
|
185
|
+
*/
|
|
186
|
+
async updateImageProfil(image) {
|
|
187
|
+
image = await this.validateImage(image);
|
|
188
|
+
return this.callIsMe(() => this.endpointApi.profilImage({ profil_avatar: image }));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Récupérer les actualités : Récupère la liste d’actualités selon plusieurs critères.
|
|
193
|
+
* Constant : GET_NEWS
|
|
194
|
+
*/
|
|
195
|
+
async getNews(data = {}) {
|
|
196
|
+
if (data.pathParams) {
|
|
197
|
+
delete data.pathParams;
|
|
198
|
+
}
|
|
199
|
+
if (!this.isMe){
|
|
200
|
+
// is not me add id
|
|
201
|
+
data.pathParams = { id: this.id };
|
|
202
|
+
}
|
|
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);
|
|
212
|
+
}
|
|
213
|
+
|
|
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;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
85
226
|
}
|
|
86
227
|
|
|
87
|
-
// Incorporation
|
|
88
|
-
Object.assign(User.prototype, EntityMixin);
|
|
228
|
+
// Incorporation des mixins dans User
|
|
229
|
+
Object.assign(User.prototype, EntityMixin, UtilMixin, UserMixin, NewsMixin);
|
|
89
230
|
|
package/src/api/UserApi.js
CHANGED
|
@@ -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
|
}
|