@communecter/cocolight-api-client 1.0.8 → 1.0.10
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/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 +3 -3
- package/dist/cocolight-api-client.cjs +1 -2
- package/dist/cocolight-api-client.mjs.js +1 -2
- package/package.json +6 -3
- package/src/Api.js +36 -19
- package/src/ApiClient.js +56 -11
- package/src/api/EndpointApi.js +1534 -0
- package/src/api/News.js +327 -0
- package/src/api/Organization.js +268 -84
- package/src/api/Project.js +287 -93
- package/src/api/User.js +397 -87
- package/src/api/UserApi.js +5 -1
- package/src/endpoints.module.js +2 -2
- package/src/mixin/DraftStateMixin.js +176 -0
- package/src/mixin/EntityMixin.js +109 -0
- package/src/mixin/MutualEntityMixin.js +48 -0
- package/src/mixin/NewsMixin.js +42 -0
- package/src/mixin/UserMixin.js +8 -0
- package/src/mixin/UtilMixin.js +294 -0
- package/src/utils/stream-utils.node.js +10 -0
- package/dist/cocolight-api-client.cjs.LICENSE.txt +0 -1
- package/dist/cocolight-api-client.mjs.js.LICENSE.txt +0 -1
- package/src/api/EntityMixin.js +0 -249
- package/src/api/NewsMixin.js +0 -168
- package/src/api/UserMixin.js +0 -59
- package/src/api/UtilMixin.js +0 -82
package/src/api/Organization.js
CHANGED
|
@@ -1,131 +1,315 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { ApiError } from "../error.js";
|
|
2
|
+
import { DraftStateMixin } from "../mixin/DraftStateMixin.js";
|
|
3
|
+
import { EntityMixin } from "../mixin/EntityMixin.js";
|
|
4
|
+
import { MutualEntityMixin } from "../mixin/MutualEntityMixin.js";
|
|
5
|
+
import { NewsMixin } from "../mixin/NewsMixin.js";
|
|
6
|
+
import { UtilMixin } from "../mixin/UtilMixin.js";
|
|
4
7
|
|
|
5
8
|
// Organization.js
|
|
6
9
|
export class Organization {
|
|
7
|
-
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
|
|
10
|
+
#draftData = {};
|
|
11
|
+
#initialDraftData = {};
|
|
12
|
+
#serverData = null;
|
|
13
|
+
|
|
14
|
+
static entityType = "organizations";
|
|
15
|
+
|
|
16
|
+
static SCHEMA_CONSTANTS = [
|
|
17
|
+
"ADD_ORGANIZATION",
|
|
18
|
+
"UPDATE_BLOCK_DESCRIPTION",
|
|
19
|
+
"UPDATE_BLOCK_INFO",
|
|
20
|
+
"UPDATE_BLOCK_SOCIAL",
|
|
21
|
+
"UPDATE_BLOCK_LOCALITY",
|
|
22
|
+
"UPDATE_BLOCK_SLUG",
|
|
23
|
+
"PROFIL_IMAGE"
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
static ADD_BLOCKS = new Map([
|
|
27
|
+
["ADD_ORGANIZATION", "addOrganization"],
|
|
28
|
+
["PROFIL_IMAGE", ""]
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
static UPDATE_BLOCKS = new Map([
|
|
32
|
+
["UPDATE_BLOCK_DESCRIPTION", "updateDescription"],
|
|
33
|
+
["UPDATE_BLOCK_SOCIAL", "updateSocial"],
|
|
34
|
+
["UPDATE_BLOCK_LOCALITY", "updateLocality"],
|
|
35
|
+
["UPDATE_BLOCK_INFO", "updateInfo"],
|
|
36
|
+
["UPDATE_BLOCK_SLUG", "updateSlug"],
|
|
37
|
+
["PROFIL_IMAGE", "updateImageProfil"]
|
|
38
|
+
]);
|
|
11
39
|
|
|
12
40
|
/**
|
|
13
41
|
* Crée une instance de Organization.
|
|
14
|
-
*
|
|
15
|
-
* @param {
|
|
42
|
+
*
|
|
43
|
+
* @param {ApiClient|User} parent - Instance de ApiClient ou User.
|
|
44
|
+
* @param {Object} [data={}] - Données de l'organisation.
|
|
45
|
+
* @param {Object} [deps={}] - Dépendances injectées.
|
|
46
|
+
* @param {function|object} deps.EndpointApi - Classe ou instance de EndpointApi pour les appels API.
|
|
47
|
+
* @param {function} deps.User - Classe User pour la gestion des utilisateurs.
|
|
48
|
+
* @param {function} deps.Project - Classe Project pour la gestion des projets.
|
|
49
|
+
* @param {function} deps.News - Classe News pour la gestion des actualités.
|
|
50
|
+
*
|
|
51
|
+
* @throws {ApiError} Si le parent n'est pas valide ou si les dépendances ne sont pas injectées correctement.
|
|
16
52
|
*/
|
|
17
53
|
|
|
18
|
-
constructor(
|
|
19
|
-
|
|
20
|
-
|
|
54
|
+
constructor(parent, data = {}, deps = {}) {
|
|
55
|
+
this.__entityTag = "Organization";
|
|
56
|
+
|
|
57
|
+
if (!deps.EndpointApi) throw new ApiError("EndpointApi class must be injected to avoid circular dependency.");
|
|
58
|
+
if (!deps.User) throw new ApiError("User class must be injected.");
|
|
59
|
+
if (!deps.Project) throw new ApiError("Project class must be injected.");
|
|
60
|
+
if (!deps.News) throw new ApiError("News class must be injected.");
|
|
61
|
+
|
|
62
|
+
if (!parent) throw new ApiError("Parent is required.");
|
|
63
|
+
|
|
64
|
+
this.deps = deps;
|
|
65
|
+
|
|
66
|
+
this.EndpointApiClass = deps.EndpointApi;
|
|
67
|
+
|
|
68
|
+
if (parent?.__entityTag === "ApiClient") {
|
|
69
|
+
this.apiClient = parent;
|
|
70
|
+
this.parent = null;
|
|
71
|
+
} else if (parent?.__entityTag === "User") {
|
|
72
|
+
this.apiClient = parent.apiClient;
|
|
73
|
+
this.parent = parent;
|
|
74
|
+
} else {
|
|
75
|
+
throw new ApiError("Invalid parent for Organization.");
|
|
21
76
|
}
|
|
22
|
-
|
|
23
|
-
this
|
|
24
|
-
|
|
77
|
+
|
|
78
|
+
this.endpointApi = typeof deps.EndpointApi === "function"
|
|
79
|
+
? new deps.EndpointApi(this.apiClient)
|
|
80
|
+
: (typeof deps.EndpointApi === "object"
|
|
81
|
+
? deps.EndpointApi
|
|
82
|
+
: (() => { throw new ApiError("deps.EndpointApi must be a class or instance."); })());
|
|
83
|
+
|
|
84
|
+
this.#serverData = null;
|
|
85
|
+
|
|
86
|
+
const { draft, proxy } = this.buildDraftAndProxy({
|
|
87
|
+
data: { ...data, ...this.defaultFields },
|
|
88
|
+
serverData: this.#serverData,
|
|
89
|
+
constant: Organization.SCHEMA_CONSTANTS,
|
|
90
|
+
apiClient: this.apiClient,
|
|
91
|
+
transforms: this.transforms,
|
|
92
|
+
removeFields: this.removeFields
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
this.#initialDraftData = JSON.parse(JSON.stringify(draft)); // snapshot propre
|
|
96
|
+
this.#draftData = draft;
|
|
97
|
+
this.data = proxy;
|
|
25
98
|
}
|
|
26
99
|
|
|
27
100
|
// Getters en lecture seule pour chaque propriété
|
|
28
101
|
get id() {
|
|
29
|
-
return this.#id;
|
|
102
|
+
return this.#draftData.id || null;
|
|
30
103
|
}
|
|
31
104
|
|
|
32
105
|
_id(newId) {
|
|
33
|
-
this.#id = newId;
|
|
106
|
+
this.#draftData.id = newId;
|
|
34
107
|
}
|
|
35
108
|
|
|
36
109
|
get slug() {
|
|
37
|
-
return this.#slug;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
_slug(newSlug) {
|
|
41
|
-
this.#slug = newSlug;
|
|
110
|
+
return this.#draftData.slug || null;
|
|
42
111
|
}
|
|
43
112
|
|
|
44
113
|
get isConnected() {
|
|
45
114
|
return this.apiClient.isConnected;
|
|
46
115
|
}
|
|
47
116
|
|
|
48
|
-
get data() {
|
|
49
|
-
return this.#data;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
117
|
_setData(newData) {
|
|
53
|
-
this.#
|
|
118
|
+
this.#serverData = { ...newData };
|
|
119
|
+
|
|
120
|
+
const { draft, proxy } = this.buildDraftAndProxy({
|
|
121
|
+
data: { ...newData, ...this.defaultFields },
|
|
122
|
+
serverData: this.#serverData,
|
|
123
|
+
constant: Organization.SCHEMA_CONSTANTS,
|
|
124
|
+
apiClient: this.apiClient,
|
|
125
|
+
transforms: this.transforms,
|
|
126
|
+
removeFields: this.removeFields
|
|
127
|
+
});
|
|
128
|
+
this.#initialDraftData = JSON.parse(JSON.stringify(draft));
|
|
129
|
+
this.#draftData = draft;
|
|
130
|
+
this.data = proxy;
|
|
54
131
|
}
|
|
55
132
|
|
|
56
133
|
get userId() {
|
|
57
134
|
return this.apiClient.userId;
|
|
58
135
|
}
|
|
136
|
+
|
|
137
|
+
get draftData() {
|
|
138
|
+
return this.#draftData;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get initialDraftData() {
|
|
142
|
+
return this.#initialDraftData;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
get serverData() {
|
|
146
|
+
return this.#serverData;
|
|
147
|
+
}
|
|
59
148
|
|
|
60
149
|
get isMe() {
|
|
61
|
-
return this.isConnected && this.userId === this.id;
|
|
150
|
+
return this.isConnected && this.userId === this.parent?.id;
|
|
62
151
|
}
|
|
63
152
|
|
|
64
153
|
getEntityType() {
|
|
65
|
-
return
|
|
154
|
+
return Organization.entityType;
|
|
66
155
|
}
|
|
67
156
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
157
|
+
static fromServerData(data, parent, deps) {
|
|
158
|
+
const instance = new Organization(parent, {}, deps);
|
|
159
|
+
instance.#serverData = { ...data };
|
|
160
|
+
|
|
161
|
+
const { draft, proxy } = instance.buildDraftAndProxy({
|
|
162
|
+
data: { ...data, ...instance.defaultFields },
|
|
163
|
+
serverData: instance.#serverData,
|
|
164
|
+
constant: Organization.SCHEMA_CONSTANTS,
|
|
165
|
+
apiClient: instance.apiClient,
|
|
166
|
+
transforms: instance.transforms,
|
|
167
|
+
removeFields: instance.removeFields
|
|
78
168
|
});
|
|
79
|
-
|
|
169
|
+
|
|
170
|
+
instance.#draftData = draft;
|
|
171
|
+
instance.data = proxy;
|
|
80
172
|
|
|
81
|
-
|
|
82
|
-
* Récupérer les actualités : Récupère la liste d’actualités selon plusieurs critères.
|
|
83
|
-
* Constant : GET_NEWS
|
|
84
|
-
* @param {Object} data - Les données à envoyer.
|
|
85
|
-
* @param {number} data.dateLimit - Limite de date timestamp ou 0 (default: 0)
|
|
86
|
-
* @param {object} data.search - data.search
|
|
87
|
-
* @param {string} data.search.name - Nom ou terme recherché (default: "")
|
|
88
|
-
* @param {number} data.indexStep - Nombre de résultats par page (default: 12)
|
|
89
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
90
|
-
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
91
|
-
* @throws {Error} - En cas d'erreur inattendue.
|
|
92
|
-
*/
|
|
93
|
-
async getNews(data = {}) {
|
|
94
|
-
data.pathParams = { type: this.getEntityType(), id: this.id };
|
|
95
|
-
return this._getNews(data);
|
|
173
|
+
return instance;
|
|
96
174
|
}
|
|
97
175
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
* @param {Object} data - Les données à envoyer.
|
|
102
|
-
* @param {string} data.text - Contenu de l’actualité
|
|
103
|
-
* @param {string} data.scope - Portée de l'actualité (ex: public, privé...) (default: "public")
|
|
104
|
-
* @param {boolean} data.markdownActive - Markdown activé (true/false) (default: true)
|
|
105
|
-
* @param {string} data.type - Type de l'objet, toujours 'news'. (default: "news")
|
|
106
|
-
* @param {boolean} data.json - Indique que la réponse est au format JSON. (default: true)
|
|
107
|
-
* @param {array | string} data.tags - Tags : "" pour effacer tous les tags, ou tableau de mots-clés.
|
|
108
|
-
* @param {object} data.mediaImg - Optionnel. Informations sur les images associées à la news.
|
|
109
|
-
* @param {number} data.mediaImg.countImages - Nombre d'images.
|
|
110
|
-
* @param {Array<string>} data.mediaImg.images - Liste des identifiants ou chemins d'images.
|
|
111
|
-
* @param {object} data.mediaFile - Optionnel. Informations sur les fichiers associés à la news.
|
|
112
|
-
* @param {number} data.mediaFile.countFiles - Nombre de fichiers.
|
|
113
|
-
* @param {Array<string>} data.mediaFile.files - Liste des identifiants ou chemins de fichiers.
|
|
114
|
-
* @param {object} data.mentions - Liste des mentions sous forme d'objet avec des clés dynamiques représentant l'indice de la mention.
|
|
115
|
-
* @param {Object.<string, object>} data.mentions - Objet dont les clés keys matching ^[0-9]+$
|
|
116
|
-
* @returns {Promise<Object>} - Les données de réponse.
|
|
117
|
-
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
118
|
-
* @throws {ApiAuthenticationError} - En cas d'erreur d'authentification.
|
|
119
|
-
* @throws {Error} - En cas d'erreur inattendue.
|
|
120
|
-
*/
|
|
121
|
-
async addNews(data = {}) {
|
|
122
|
-
data.parentId = this.id;
|
|
123
|
-
data.parentType = this.getEntityType();
|
|
124
|
-
return this.callIsConnected(() => this._addNews(data));
|
|
176
|
+
async refresh() {
|
|
177
|
+
if (!this.id) throw new ApiError("Impossible de rafraîchir sans ID.");
|
|
178
|
+
return this.get();
|
|
125
179
|
}
|
|
180
|
+
|
|
181
|
+
async save() {
|
|
182
|
+
|
|
183
|
+
if(!this.isConnected){
|
|
184
|
+
throw new ApiError("Impossible de sauvegarder sans être connecté.");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const payload = { ...this.#draftData };
|
|
188
|
+
|
|
189
|
+
if (!this.id) {
|
|
190
|
+
await this._add(payload);
|
|
191
|
+
// this._updateInitialDraftSnapshot();
|
|
192
|
+
await this.refresh();
|
|
193
|
+
return this.#serverData;
|
|
194
|
+
} else {
|
|
195
|
+
const hasChanged = await this._update(payload);
|
|
196
|
+
if (hasChanged) {
|
|
197
|
+
// this._updateInitialDraftSnapshot();
|
|
198
|
+
await this.refresh();
|
|
199
|
+
}
|
|
200
|
+
return this.#serverData;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async _add(payload){
|
|
206
|
+
// si pas d'id on le crée
|
|
207
|
+
payload.id = this._newId();
|
|
208
|
+
// on enlève le slug mais il ne devrait pas être là
|
|
209
|
+
if(payload.slug){
|
|
210
|
+
delete payload.slug;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
for (const [constant, methodName] of Organization.ADD_BLOCKS) {
|
|
214
|
+
const blockData = this.extractChangedFieldsFromSchema(
|
|
215
|
+
this.apiClient,
|
|
216
|
+
constant,
|
|
217
|
+
{ ...payload, ...this.defaultFields},
|
|
218
|
+
() => {}
|
|
219
|
+
);
|
|
220
|
+
if (blockData && Object.keys(blockData).length > 0) {
|
|
221
|
+
const data = await this[methodName](blockData);
|
|
222
|
+
|
|
223
|
+
if (!this.id && data?.map?.id) {
|
|
224
|
+
this.#draftData.id = data.map.id;
|
|
225
|
+
this.#draftData.slug = data.map.slug;
|
|
226
|
+
// on met à jour le slug dans le draftData
|
|
126
227
|
|
|
228
|
+
// faire ça ou alors resfresh() pour re-synchroniser
|
|
229
|
+
// this.#serverData = data.map;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async _update(payload){
|
|
236
|
+
// on enlève l'id car il existe déjà dans les appels API je laisse slug car il peut être changer en update ici
|
|
237
|
+
// if(payload.slug){
|
|
238
|
+
// delete payload.slug;
|
|
239
|
+
// }
|
|
240
|
+
if(payload.id){
|
|
241
|
+
delete payload.id;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
let hasChanged = false;
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
// Sinon, on fait les updates en blocs
|
|
248
|
+
for (const [constant, methodName] of Organization.UPDATE_BLOCKS) {
|
|
249
|
+
const blockData = this.extractChangedFieldsFromSchema(
|
|
250
|
+
this.apiClient,
|
|
251
|
+
constant,
|
|
252
|
+
{ ...payload, ...this.defaultFields},
|
|
253
|
+
() => this.initialDraftData,
|
|
254
|
+
this.removeFields
|
|
255
|
+
);
|
|
256
|
+
if (blockData && Object.keys(blockData).length > 0) {
|
|
257
|
+
await this[methodName](blockData);
|
|
258
|
+
hasChanged = true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return hasChanged;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
addOrganization(data = {}) {
|
|
266
|
+
return this.callIsConnected(() => this.endpointApi.addOrganization(data));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
_updateInitialDraftSnapshot() {
|
|
270
|
+
this.#initialDraftData = JSON.parse(JSON.stringify(this.#draftData));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
hasChanges() {
|
|
274
|
+
return JSON.stringify(this.#draftData) !== JSON.stringify(this.#initialDraftData);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
defaultFields = {
|
|
278
|
+
typeElement: this.getEntityType()
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
removeFields = [
|
|
282
|
+
"typeElement"
|
|
283
|
+
];
|
|
284
|
+
|
|
285
|
+
// role = admin c'est bizarre car en faite c'est gérer dans links sur user et l'orga
|
|
286
|
+
// links.members.${this.userId}.isAdmin === true
|
|
287
|
+
transforms = {
|
|
288
|
+
github: (val, full) => full?.socialNetwork?.github,
|
|
289
|
+
gitlab: (val, full) => full?.socialNetwork?.gitlab,
|
|
290
|
+
facebook: (val, full) => full?.socialNetwork?.facebook,
|
|
291
|
+
twitter: (val, full) => full?.socialNetwork?.twitter,
|
|
292
|
+
instagram: (val, full) => full?.socialNetwork?.instagram,
|
|
293
|
+
diaspora: (val, full) => full?.socialNetwork?.diaspora,
|
|
294
|
+
mastodon: (val, full) => full?.socialNetwork?.mastodon,
|
|
295
|
+
telegram: (val, full) => full?.socialNetwork?.telegram,
|
|
296
|
+
signal: (val, full) => full?.socialNetwork?.signal
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
async project(projectData = {}, ) {
|
|
300
|
+
try {
|
|
301
|
+
const project = new this.deps.Project(this, projectData, { User: this.deps.User, News: this.deps.News, EndpointApi : this.deps.EndpointApi });
|
|
302
|
+
if (projectData.id || projectData.slug) {
|
|
303
|
+
await project.get();
|
|
304
|
+
}
|
|
305
|
+
return project;
|
|
306
|
+
} catch (error) {
|
|
307
|
+
this.apiClient._logger.error(`[Api.${this.__entityTag}.project] Erreur lors de la création d'une instance project :`, error.message);
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
127
312
|
}
|
|
128
313
|
|
|
129
314
|
// Incorporation du mixin dans Organization
|
|
130
|
-
Object.assign(Organization.prototype, EntityMixin,
|
|
131
|
-
|
|
315
|
+
Object.assign(Organization.prototype, UtilMixin, MutualEntityMixin, EntityMixin, NewsMixin, DraftStateMixin);
|