@communecter/cocolight-api-client 1.0.9 → 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/cocolight-api-client.browser.js +3 -3
- package/dist/cocolight-api-client.cjs +1 -1
- package/dist/cocolight-api-client.mjs.js +1 -1
- package/package.json +3 -2
- package/src/Api.js +31 -19
- package/src/ApiClient.js +10 -1
- package/src/api/News.js +129 -88
- package/src/api/Organization.js +248 -166
- package/src/api/Project.js +263 -172
- package/src/api/User.js +355 -49
- package/src/api/UserApi.js +4 -1
- package/src/endpoints.module.js +1 -1
- package/src/mixin/DraftStateMixin.js +176 -0
- package/src/mixin/EntityMixin.js +96 -35
- package/src/mixin/MutualEntityMixin.js +48 -0
- package/src/mixin/NewsMixin.js +35 -1
- package/src/mixin/UtilMixin.js +49 -1
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// DraftStateMixin.js
|
|
2
|
+
import { ApiError, ApiValidationError } from "../error.js";
|
|
3
|
+
|
|
4
|
+
export const DraftStateMixin = {
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Crée un proxy combinant draft + serveur, avec transformations facultatives.
|
|
8
|
+
* @param {Object} server
|
|
9
|
+
* @param {Object} draft
|
|
10
|
+
* @param {Array} allowedFields - champs autorisés dans le draft
|
|
11
|
+
* @param {Object} [transforms={}] - transformateurs de lecture
|
|
12
|
+
* @param {Object} [options={}] - options
|
|
13
|
+
* @returns {Proxy}
|
|
14
|
+
*/
|
|
15
|
+
createDraftProxy(apiClient, server = {}, draft = {}, allowedFields = [], transforms = {}, options = {}) {
|
|
16
|
+
return new Proxy({}, {
|
|
17
|
+
get: (_, prop) => {
|
|
18
|
+
const val = prop in draft ? draft[prop] : server[prop];
|
|
19
|
+
const transformer = transforms[prop];
|
|
20
|
+
return typeof transformer === "function" ? transformer(val) : val;
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
set: (_, prop, value) => {
|
|
24
|
+
if (!allowedFields.includes(prop)) {
|
|
25
|
+
const message = `[DraftProxy] Le champ "${prop}" n'est pas autorisé.`;
|
|
26
|
+
if (options.throwOnError) {
|
|
27
|
+
throw new ApiValidationError(message, 400, null, {
|
|
28
|
+
field: prop,
|
|
29
|
+
value,
|
|
30
|
+
allowedFields
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
apiClient._logger.warn(message);
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
draft[prop] = value;
|
|
37
|
+
return true;
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
deleteProperty: (_, prop) => {
|
|
41
|
+
if (!allowedFields.includes(prop)) return false;
|
|
42
|
+
delete draft[prop];
|
|
43
|
+
return true;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
has: (_, prop) => prop in draft || prop in server,
|
|
47
|
+
ownKeys: () => [...new Set([...Object.keys(server), ...Object.keys(draft)])],
|
|
48
|
+
getOwnPropertyDescriptor: () => ({ enumerable: true, configurable: true })
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
_extractWritableFields(schema = {}, data = {}, ctx = { defs: {}, visited: new Set() }) {
|
|
53
|
+
if (!schema || typeof schema !== "object") return [];
|
|
54
|
+
if (schema.$id && ctx.visited.has(schema.$id)) return [];
|
|
55
|
+
if (schema.$id) ctx.visited.add(schema.$id);
|
|
56
|
+
ctx.defs = schema.$defs || schema.definitions || ctx.defs;
|
|
57
|
+
|
|
58
|
+
const fields = [];
|
|
59
|
+
|
|
60
|
+
if (schema.$ref) {
|
|
61
|
+
const refKey = schema.$ref.replace(/^#\/?(\$defs|definitions)\//, "");
|
|
62
|
+
const resolved = ctx.defs?.[refKey];
|
|
63
|
+
if (resolved) fields.push(...this._extractWritableFields(resolved, data, ctx));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (schema.allOf) {
|
|
67
|
+
schema.allOf.forEach(s => fields.push(...this._extractWritableFields(s, data, ctx)));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (schema.if && schema.then) {
|
|
71
|
+
const condition = schema.if?.properties;
|
|
72
|
+
let matches = true;
|
|
73
|
+
if (condition) {
|
|
74
|
+
for (const key in condition) {
|
|
75
|
+
const expected = condition[key]?.const;
|
|
76
|
+
if (data[key] !== expected) {
|
|
77
|
+
matches = false;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (matches && schema.then) {
|
|
83
|
+
fields.push(...this._extractWritableFields(schema.then, data, ctx));
|
|
84
|
+
} else if (!matches && schema.else) {
|
|
85
|
+
fields.push(...this._extractWritableFields(schema.else, data, ctx));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (schema.properties) {
|
|
90
|
+
fields.push(...Object.entries(schema.properties)
|
|
91
|
+
// eslint-disable-next-line no-unused-vars
|
|
92
|
+
.filter(([_, def]) => def.readOnly !== true && def.const === undefined)
|
|
93
|
+
.map(([key]) => key));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return [...new Set(fields)];
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
buildDraftAndProxy({ data = {}, serverData = null, constant, apiClient, transforms = {}, throwOnError = true, removeFields = [] }) {
|
|
100
|
+
const constants = Array.isArray(constant) ? constant : [constant];
|
|
101
|
+
const combinedSchema = {
|
|
102
|
+
allOf: [],
|
|
103
|
+
$defs: {}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
for (const key of constants) {
|
|
107
|
+
const sch = apiClient.getRequestSchema(key);
|
|
108
|
+
if (!sch) throw new ApiError(`Unable to find schema for ${key}.`);
|
|
109
|
+
|
|
110
|
+
// Extraire et fusionner les $defs
|
|
111
|
+
if (sch.$defs) {
|
|
112
|
+
for (const [defKey, defVal] of Object.entries(sch.$defs)) {
|
|
113
|
+
if (combinedSchema.$defs[defKey]) {
|
|
114
|
+
apiClient._logger.warn(`Duplicate $defs key '${defKey}' from schema '${key}'`);
|
|
115
|
+
} else {
|
|
116
|
+
combinedSchema.$defs[defKey] = defVal;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
combinedSchema.allOf.push(sch);
|
|
122
|
+
}
|
|
123
|
+
const draft = {};
|
|
124
|
+
let allowed = this._extractWritableFields(combinedSchema, data);
|
|
125
|
+
|
|
126
|
+
if (data.id && allowed.indexOf("id") === -1) {
|
|
127
|
+
allowed.push("id");
|
|
128
|
+
}
|
|
129
|
+
if (data.slug && allowed.indexOf("slug") === -1) {
|
|
130
|
+
allowed.push("slug");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
allowed = allowed.filter(k => !removeFields.includes(k));
|
|
134
|
+
|
|
135
|
+
for (const key of allowed) {
|
|
136
|
+
const raw = data[key];
|
|
137
|
+
const transformed = typeof transforms[key] === "function" ? transforms[key](raw, data) : raw;
|
|
138
|
+
if (transformed !== undefined) draft[key] = transformed;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const proxy = this.createDraftProxy(apiClient, serverData, draft, allowed, transforms, { throwOnError });
|
|
142
|
+
|
|
143
|
+
return { draft, proxy };
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
extractChangedFieldsFromSchema(apiClient, constant, data = {}, getInitialDraft, removeFields = []) {
|
|
147
|
+
const schema = apiClient.getRequestSchema(constant);
|
|
148
|
+
let allowed = this._extractWritableFields(schema, data);
|
|
149
|
+
const changed = {};
|
|
150
|
+
const initialDraft = getInitialDraft?.() || {};
|
|
151
|
+
|
|
152
|
+
// on enlève les champs qui ne sont pas dans le draft
|
|
153
|
+
// ou qui sont dans removeFields
|
|
154
|
+
allowed = allowed.filter(k => !removeFields.includes(k));
|
|
155
|
+
|
|
156
|
+
for (const key of allowed) {
|
|
157
|
+
// on verifie que le champ existe dans le draft
|
|
158
|
+
// sinon on ne le prend pas en compte
|
|
159
|
+
|
|
160
|
+
if (data[key] === undefined) continue;
|
|
161
|
+
|
|
162
|
+
const current = data[key];
|
|
163
|
+
const initial = initialDraft[key];
|
|
164
|
+
|
|
165
|
+
const changedValue =
|
|
166
|
+
JSON.stringify(current) !== JSON.stringify(initial);
|
|
167
|
+
|
|
168
|
+
if (changedValue) {
|
|
169
|
+
changed[key] = current;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return Object.keys(changed).length > 0 ? changed : null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
};
|
package/src/mixin/EntityMixin.js
CHANGED
|
@@ -3,46 +3,107 @@ import { ApiResponseError } from "../error.js";
|
|
|
3
3
|
// EntityMixin.js
|
|
4
4
|
export const EntityMixin = {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @returns {Promise<
|
|
6
|
+
* Récupère le profil complet de l'organisation.
|
|
7
|
+
*
|
|
8
|
+
* @returns {Promise<Object>} Le profil complet.
|
|
9
9
|
*/
|
|
10
|
-
async
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return this.id;
|
|
10
|
+
async get() {
|
|
11
|
+
return this.apiClient.safeCall(async () => {
|
|
12
|
+
const data = await this.getPublicProfile();
|
|
13
|
+
this._setData(data);
|
|
14
|
+
return data;
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Mettre à jour les paramètres d'un élément : Mise à jour des paramètres spécifiques d'un élément.
|
|
20
|
+
* Constant : UPDATE_SETTINGS
|
|
21
|
+
* @param {Object} data - Les données à envoyer.
|
|
22
|
+
* @param {string} data.type - data.type
|
|
23
|
+
* @param {undefined} data.value - data.value
|
|
24
|
+
* @returns {Promise<Object>} - Les données de réponse.
|
|
25
|
+
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
26
|
+
* @throws {ApiAuthenticationError} - En cas d'erreur d'authentification.
|
|
27
|
+
* @throws {Error} - En cas d'erreur inattendue.
|
|
28
|
+
*/
|
|
29
|
+
async updateSettings(data = {}) {
|
|
30
|
+
data.idEntity = this.id;
|
|
31
|
+
data.typeEntity = this.getEntityType();
|
|
32
|
+
return this.callIsConnected(() => this.endpointApi.updateSettings(data));
|
|
36
33
|
},
|
|
37
34
|
|
|
38
35
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
* Mettre à jour la description d'un élément : Permet de mettre à jour la description courte et complète d'un élément.
|
|
37
|
+
* Constant : UPDATE_BLOCK_DESCRIPTION
|
|
38
|
+
* @param {Object} data - Les données à envoyer.
|
|
39
|
+
* @param {string} data.descMentions - Mentions dans la description (default: "")
|
|
40
|
+
* @param {string} data.shortDescription - Courte description
|
|
41
|
+
* @param {string} data.description - Description complète
|
|
42
|
+
* @returns {Promise<Object>} - Les données de réponse.
|
|
43
|
+
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
44
|
+
* @throws {ApiAuthenticationError} - En cas d'erreur d'authentification.
|
|
45
|
+
* @throws {Error} - En cas d'erreur inattendue.
|
|
46
|
+
*/
|
|
47
|
+
async updateDescription(data = {}) {
|
|
48
|
+
data.typeElement = this.getEntityType();
|
|
49
|
+
data.id = this.id;
|
|
50
|
+
return this.callIsConnected(() => this.endpointApi.updateBlockDescription(data));
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Mettre à jour les informations d'un élément : Permet de mettre à jour les informations générales d'un élément (nom, contacts, etc.).
|
|
55
|
+
* Constant : UPDATE_BLOCK_INFO
|
|
41
56
|
*/
|
|
42
|
-
async
|
|
43
|
-
|
|
44
|
-
|
|
57
|
+
async updateInfo(data = {}) {
|
|
58
|
+
data.typeElement = this.getEntityType();
|
|
59
|
+
data.id = this.id;
|
|
60
|
+
return this.callIsConnected(() => this.endpointApi.updateBlockInfo(data));
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 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.
|
|
65
|
+
* Constant : UPDATE_BLOCK_SOCIAL
|
|
66
|
+
*/
|
|
67
|
+
async updateSocial(data = {}) {
|
|
68
|
+
data.typeElement = this.getEntityType();
|
|
69
|
+
data.id = this.id;
|
|
70
|
+
return this.callIsConnected(() => this.endpointApi.updateBlockSocial(data));
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Mettre à jour les localités d'un élément : Permet de mettre à jour l'adresse et les informations géographiques d'un élément.
|
|
75
|
+
* Constant : UPDATE_BLOCK_LOCALITY
|
|
76
|
+
*/
|
|
77
|
+
async updateLocality(data = {}) {
|
|
78
|
+
data.typeElement = this.getEntityType();
|
|
79
|
+
data.id = this.id;
|
|
80
|
+
return this.callIsConnected(() => this.endpointApi.updateBlockLocality(data));
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Mettre à jour le slug d'un élément : Permet de mettre à jour le slug pour une URL simplifiée.
|
|
85
|
+
* Constant : UPDATE_BLOCK_SLUG
|
|
86
|
+
*/
|
|
87
|
+
async updateSlug({ slug }) {
|
|
88
|
+
try {
|
|
89
|
+
await this.endpointApi.check({ type: this.getEntityType(), id: this.id, slug });
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if(error instanceof ApiResponseError) {
|
|
92
|
+
throw new ApiResponseError("Erreur lors de la vérification du slug.", error.status, error.data);
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
return this.callIsConnected(() => this.endpointApi.updateBlockSlug({ typeElement: this.getEntityType(), id: this.id, slug }));
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Mettre à jour l'image de profil : Permet de mettre à jour l'image de profil d'un utilisateur ou d'une entité.
|
|
101
|
+
* Constant : PROFIL_IMAGE
|
|
102
|
+
*/
|
|
103
|
+
async updateImageProfil({ profil_avatar: image }) {
|
|
104
|
+
image = await this.validateImage(image);
|
|
105
|
+
const data = { pathParams: { folder: this.getEntityType(), ownerId: this.id }, profil_avatar: image };
|
|
106
|
+
return this.callIsConnected(() => this.endpointApi.profilImage(data));
|
|
45
107
|
}
|
|
46
|
-
|
|
47
108
|
};
|
|
48
109
|
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ApiResponseError } from "../error.js";
|
|
2
|
+
|
|
3
|
+
// MutualEntityMixin.js
|
|
4
|
+
export const MutualEntityMixin = {
|
|
5
|
+
/**
|
|
6
|
+
* Résout l'identifiant de l'entité si seul le slug est fourni.
|
|
7
|
+
* @param {string} type - Le type d'entité (ex : "citoyens", "organizations", "projects").
|
|
8
|
+
* @returns {Promise<string>} L'identifiant résolu.
|
|
9
|
+
*/
|
|
10
|
+
async resolveId(type) {
|
|
11
|
+
if (!this.id && this.slug) {
|
|
12
|
+
try {
|
|
13
|
+
const data = await this.endpointApi.getElementsKey({
|
|
14
|
+
pathParams:{
|
|
15
|
+
slug: this.slug
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
if(data?.contextId && data?.contextType === type) {
|
|
19
|
+
this._id(data.contextId);
|
|
20
|
+
} else {
|
|
21
|
+
throw new ApiResponseError(`Le slug ${this.slug} ne correspond pas à un ${type}`, 200, data);
|
|
22
|
+
}
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if(error instanceof ApiResponseError) {
|
|
25
|
+
if(error?.responseData?.contextType !== type) {
|
|
26
|
+
throw error;
|
|
27
|
+
} else {
|
|
28
|
+
throw new ApiResponseError(`Impossible de récupérer l'identifiant pour le slug ${this.slug}`, error.status, error.responseData);
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return this.id;
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Récupère le profil public de l'entité.
|
|
40
|
+
* @returns {Promise<Object>} Les données du profil public.
|
|
41
|
+
*/
|
|
42
|
+
async getPublicProfile() {
|
|
43
|
+
await this.resolveId(this.getEntityType());
|
|
44
|
+
return this.endpointApi.getElementsAbout({ pathParams: { id: this.id, type: this.getEntityType() } });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
};
|
|
48
|
+
|
package/src/mixin/NewsMixin.js
CHANGED
|
@@ -1,8 +1,42 @@
|
|
|
1
|
-
// eslint-disable-next-line no-unused-vars
|
|
2
1
|
import { ApiResponseError } from "../error.js";
|
|
3
2
|
|
|
4
3
|
// NewsMixin.js
|
|
5
4
|
export const NewsMixin = {
|
|
5
|
+
/**
|
|
6
|
+
* Récupérer les actualités : Récupère la liste d’actualités selon plusieurs critères.
|
|
7
|
+
* Constant : GET_NEWS
|
|
8
|
+
* @param {Object} data - Les données à envoyer.
|
|
9
|
+
* @param {number} data.dateLimit - Limite de date timestamp ou 0 (default: 0)
|
|
10
|
+
* @param {object} data.search - data.search
|
|
11
|
+
* @param {string} data.search.name - Nom ou terme recherché (default: "")
|
|
12
|
+
* @param {number} data.indexStep - Nombre de résultats par page (default: 12)
|
|
13
|
+
* @returns {Promise<Object>} - Les données de réponse.
|
|
14
|
+
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
15
|
+
* @throws {Error} - En cas d'erreur inattendue.
|
|
16
|
+
*/
|
|
17
|
+
async getNews(data = {}) {
|
|
18
|
+
data.pathParams = { type: this.getEntityType(), id: this.id };
|
|
19
|
+
const arrayObjetNews = await this.endpointApi.getNews(data);
|
|
20
|
+
if(!Array.isArray(arrayObjetNews)){
|
|
21
|
+
throw new ApiResponseError("Erreur lors de la récupération des actualités.", 500, arrayObjetNews);
|
|
22
|
+
}
|
|
23
|
+
const rawNewsList = arrayObjetNews.map((newsData) =>
|
|
24
|
+
this.deps.News.fromServerData(newsData, this, { User : this.deps.User, EndpointApi: this.deps.EndpointApi })
|
|
25
|
+
);
|
|
26
|
+
return this._createFilteredProxy(rawNewsList);
|
|
27
|
+
},
|
|
6
28
|
|
|
29
|
+
async news(newsData) {
|
|
30
|
+
try {
|
|
31
|
+
const news = new this.deps.News(this, newsData, { User: this.deps.User, EndpointApi : this.deps.EndpointApi });
|
|
32
|
+
if (newsData.id) {
|
|
33
|
+
await news.get();
|
|
34
|
+
}
|
|
35
|
+
return news;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
this.apiClient._logger.error(`[Api.${this.getEntityType()}.news] Erreur lors de la création d'une instance news :`, error.message);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
7
41
|
};
|
|
8
42
|
|
package/src/mixin/UtilMixin.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import ObjectID from "bson-objectid";
|
|
1
2
|
import { fileTypeFromBuffer } from "file-type";
|
|
2
3
|
|
|
3
4
|
import { ApiAuthenticationError, ApiValidationError } from "../error.js";
|
|
@@ -5,6 +6,16 @@ import { ApiAuthenticationError, ApiValidationError } from "../error.js";
|
|
|
5
6
|
// UtilMixin.js
|
|
6
7
|
export const UtilMixin = {
|
|
7
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Appelle une méthode de l'API.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} constant - Le nom de la méthode à appeler.
|
|
13
|
+
* @param {object} data - Les données à passer à la méthode.
|
|
14
|
+
* @returns {Promise<any>} - La promesse de la méthode appelée.
|
|
15
|
+
* @throws {ApiValidationError} - Si une erreur se produit lors de l'appel de l'API.
|
|
16
|
+
* @throws {ApiResponseError} - Si une erreur se produit lors de l'appel de l'API.
|
|
17
|
+
* @throws {ApiClientError} - Si l'utilisateur n'est pas authentifié.
|
|
18
|
+
*/
|
|
8
19
|
async call(constant, data = {}) {
|
|
9
20
|
return this.apiClient.safeCall(async () => {
|
|
10
21
|
const response = await this.apiClient.callEndpoint(constant, data);
|
|
@@ -13,6 +24,17 @@ export const UtilMixin = {
|
|
|
13
24
|
});
|
|
14
25
|
},
|
|
15
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Appelle une méthode de l'API si l'utilisateur n'est pas connecté.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} constant - Le nom de la méthode à appeler.
|
|
31
|
+
* @param {object} data - Les données à passer à la méthode.
|
|
32
|
+
* @returns {Promise<any>} - La promesse de la méthode appelée.
|
|
33
|
+
* @throws {ApiAuthenticationError} - Si l'utilisateur est connecté.
|
|
34
|
+
* @throws {ApiValidationError} - Si une erreur se produit lors de l'appel de l'API.
|
|
35
|
+
* @throws {ApiResponseError} - Si une erreur se produit lors de l'appel de l'API.
|
|
36
|
+
* @throws {ApiClientError} - Si une erreur se produit lors de l'appel de l'API.
|
|
37
|
+
*/
|
|
16
38
|
async callNoConnected(constant, data = {}) {
|
|
17
39
|
if(this.isConnected) {
|
|
18
40
|
throw new ApiAuthenticationError("Vous devez ne devez pas être connecté pour faire cette action.");
|
|
@@ -20,6 +42,17 @@ export const UtilMixin = {
|
|
|
20
42
|
return this.call(constant, data);
|
|
21
43
|
},
|
|
22
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Appelle une méthode de l'API si l'utilisateur est connecté.
|
|
47
|
+
*
|
|
48
|
+
* @param {string|function} param - Le nom de la méthode à appeler ou une fonction de rappel.
|
|
49
|
+
* @param {object} data - Les données à passer à la méthode.
|
|
50
|
+
* @returns {Promise<any>} - La promesse de la méthode appelée.
|
|
51
|
+
* @throws {ApiAuthenticationError} - Si l'utilisateur n'est pas connecté.
|
|
52
|
+
* @throws {ApiValidationError} - Si une erreur se produit lors de l'appel de l'API.
|
|
53
|
+
* @throws {ApiResponseError} - Si une erreur se produit lors de l'appel de l'API.
|
|
54
|
+
* @throws {ApiClientError} - Si une erreur se produit lors de l'appel de l'API.
|
|
55
|
+
*/
|
|
23
56
|
async callIsConnected(param, data = {}) {
|
|
24
57
|
if(!this.isConnected) {
|
|
25
58
|
throw new ApiAuthenticationError("Vous devez être connecté pour faire cette action.");
|
|
@@ -31,6 +64,17 @@ export const UtilMixin = {
|
|
|
31
64
|
return this.call(param, data);
|
|
32
65
|
},
|
|
33
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Appelle une méthode de l'API si l'utilisateur est lui-même.
|
|
69
|
+
*
|
|
70
|
+
* @param {string|function} param - Le nom de la méthode à appeler ou une fonction de rappel.
|
|
71
|
+
* @param {object} data - Les données à passer à la méthode.
|
|
72
|
+
* @returns {Promise<any>} - La promesse de la méthode appelée.
|
|
73
|
+
* @throws {ApiAuthenticationError} - Si l'utilisateur n'est pas lui-même.
|
|
74
|
+
* @throws {ApiValidationError} - Si une erreur se produit lors de l'appel de l'API.
|
|
75
|
+
* @throws {ApiResponseError} - Si une erreur se produit lors de l'appel de l'API.
|
|
76
|
+
* @throws {ApiClientError} - Si une erreur se produit lors de l'appel de l'API.
|
|
77
|
+
*/
|
|
34
78
|
async callIsMe(param, data = {}) {
|
|
35
79
|
if (!this.isMe) {
|
|
36
80
|
throw new ApiAuthenticationError("Vous devez être vous-même pour faire cette action.");
|
|
@@ -239,8 +283,12 @@ export const UtilMixin = {
|
|
|
239
283
|
return Reflect.get(target, prop, receiver);
|
|
240
284
|
}
|
|
241
285
|
});
|
|
242
|
-
}
|
|
286
|
+
},
|
|
243
287
|
|
|
288
|
+
_newId() {
|
|
289
|
+
const newId = new ObjectID();
|
|
290
|
+
return newId.toString();
|
|
291
|
+
}
|
|
244
292
|
|
|
245
293
|
};
|
|
246
294
|
|