@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
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { fileTypeFromBuffer } from "file-type";
|
|
2
|
+
|
|
3
|
+
import { ApiAuthenticationError, ApiValidationError } from "../error.js";
|
|
4
|
+
|
|
5
|
+
// UtilMixin.js
|
|
6
|
+
export const UtilMixin = {
|
|
7
|
+
|
|
8
|
+
async call(constant, data = {}) {
|
|
9
|
+
return this.apiClient.safeCall(async () => {
|
|
10
|
+
const response = await this.apiClient.callEndpoint(constant, data);
|
|
11
|
+
this.apiClient.checkAndThrowApiResponseError(response);
|
|
12
|
+
return response.data;
|
|
13
|
+
});
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
async callNoConnected(constant, data = {}) {
|
|
17
|
+
if(this.isConnected) {
|
|
18
|
+
throw new ApiAuthenticationError("Vous devez ne devez pas être connecté pour faire cette action.");
|
|
19
|
+
}
|
|
20
|
+
return this.call(constant, data);
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
async callIsConnected(param, data = {}) {
|
|
24
|
+
if(!this.isConnected) {
|
|
25
|
+
throw new ApiAuthenticationError("Vous devez être connecté pour faire cette action.");
|
|
26
|
+
}
|
|
27
|
+
// Si le premier paramètre est une fonction, on l'exécute en tant que callback
|
|
28
|
+
if (typeof param === "function") {
|
|
29
|
+
return await param();
|
|
30
|
+
}
|
|
31
|
+
return this.call(param, data);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
async callIsMe(param, data = {}) {
|
|
35
|
+
if (!this.isMe) {
|
|
36
|
+
throw new ApiAuthenticationError("Vous devez être vous-même pour faire cette action.");
|
|
37
|
+
}
|
|
38
|
+
// Si le premier paramètre est une fonction, on l'exécute en tant que callback
|
|
39
|
+
if (typeof param === "function") {
|
|
40
|
+
return await param();
|
|
41
|
+
}
|
|
42
|
+
// Sinon, on considère qu'il s'agit d'un constant et on appelle la méthode par défaut
|
|
43
|
+
return await this.callIsConnected(param, data);
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
async validateImage(imageInput){
|
|
47
|
+
const image = await this._validateUploadInput(imageInput, {
|
|
48
|
+
allowedMimeTypes: ["image/jpeg", "image/png", "image/jpg"],
|
|
49
|
+
expectedType: "image"
|
|
50
|
+
});
|
|
51
|
+
return image;
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
async validateFile(fileInput){
|
|
55
|
+
const file = await this._validateUploadInput(fileInput, {
|
|
56
|
+
allowedMimeTypes: ["application/pdf", "text/plain", "text/csv"],
|
|
57
|
+
expectedType: "file"
|
|
58
|
+
});
|
|
59
|
+
return file;
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
async _validateUploadInput(input, { allowedMimeTypes = [], expectedType = "any" }) {
|
|
63
|
+
if (!input) {
|
|
64
|
+
throw new ApiValidationError("Le fichier est requis.");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const isNode = typeof window === "undefined" && typeof process !== "undefined";
|
|
68
|
+
let mimeType = "";
|
|
69
|
+
let output = input;
|
|
70
|
+
|
|
71
|
+
// Navigateur : File
|
|
72
|
+
if (typeof File !== "undefined" && input instanceof File) {
|
|
73
|
+
mimeType = input.type;
|
|
74
|
+
if (!allowedMimeTypes.includes(mimeType)) {
|
|
75
|
+
throw new ApiValidationError("Le type du fichier est invalide.");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Navigateur : Blob
|
|
80
|
+
else if (typeof Blob !== "undefined" && input instanceof Blob) {
|
|
81
|
+
mimeType = input.type;
|
|
82
|
+
if (!allowedMimeTypes.includes(mimeType)) {
|
|
83
|
+
throw new ApiValidationError("Le type du fichier est invalide.");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const ext = mimeType.split("/")[1] || "bin";
|
|
87
|
+
const fileName = `${Date.now()}.${ext}`;
|
|
88
|
+
output = new File([input], fileName, { type: mimeType });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Node.js : Buffer
|
|
92
|
+
else if (isNode && Buffer.isBuffer(input)) {
|
|
93
|
+
const fileTypeResult = await fileTypeFromBuffer(input);
|
|
94
|
+
mimeType = fileTypeResult?.mime;
|
|
95
|
+
|
|
96
|
+
if (!fileTypeResult || !allowedMimeTypes.includes(mimeType)) {
|
|
97
|
+
throw new ApiValidationError("Le type du fichier est invalide.");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Pour un fichier image, on transforme en stream
|
|
101
|
+
if (expectedType === "image") {
|
|
102
|
+
const ext = fileTypeResult.ext;
|
|
103
|
+
const filename = `${Date.now()}.${ext}`;
|
|
104
|
+
output = await this._createReadStreamFromBuffer(input, filename, mimeType);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Node.js : ReadableStream
|
|
109
|
+
else if (isNode && input?.readable && typeof input._read === "function") {
|
|
110
|
+
const previewChunks = [];
|
|
111
|
+
const tee = await this._passThrough();
|
|
112
|
+
const resultStream = await this._passThrough();
|
|
113
|
+
|
|
114
|
+
const MAX_BYTES = 4100;
|
|
115
|
+
let bytesRead = 0;
|
|
116
|
+
|
|
117
|
+
input.on("data", (chunk) => {
|
|
118
|
+
if (bytesRead < MAX_BYTES) {
|
|
119
|
+
previewChunks.push(chunk);
|
|
120
|
+
bytesRead += chunk.length;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
input.pipe(tee).pipe(resultStream);
|
|
125
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
126
|
+
|
|
127
|
+
const previewBuffer = Buffer.concat(previewChunks);
|
|
128
|
+
const fileTypeResult = await fileTypeFromBuffer(previewBuffer);
|
|
129
|
+
mimeType = fileTypeResult?.mime;
|
|
130
|
+
|
|
131
|
+
if (!fileTypeResult || !allowedMimeTypes.includes(mimeType)) {
|
|
132
|
+
throw new ApiValidationError("Le type du fichier est invalide.");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
resultStream.path = `${Date.now()}.${fileTypeResult.ext}`;
|
|
136
|
+
resultStream.mimeType = mimeType;
|
|
137
|
+
output = resultStream;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
else {
|
|
141
|
+
throw new ApiValidationError("Type de fichier non reconnu.");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return output;
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Transforme un Buffer en ReadableStream équivalent à fs.createReadStream
|
|
149
|
+
* @param {Buffer} buffer - Le buffer contenant les données binaires
|
|
150
|
+
* @param {string} filename - Nom de fichier (utilisé dans FormData)
|
|
151
|
+
* @param {string} mimeType - Type MIME (utilisé dans FormData)
|
|
152
|
+
* @returns {Object} - { stream, filename, mimeType }
|
|
153
|
+
*/
|
|
154
|
+
async _createReadStreamFromBuffer(buffer, filename = "file.bin", mimeType = "application/octet-stream") {
|
|
155
|
+
const stream = await this._bufferToReadable(buffer);
|
|
156
|
+
stream.path = filename; // 👈 hack pour simuler un vrai fichier ReadStream
|
|
157
|
+
stream.mimeType = mimeType;
|
|
158
|
+
return stream;
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
async _bufferToReadable(buffer) {
|
|
162
|
+
if (typeof window === "undefined") {
|
|
163
|
+
const { bufferToReadable } = await import("../utils/stream-utils.node.js");
|
|
164
|
+
return bufferToReadable(buffer);
|
|
165
|
+
} else {
|
|
166
|
+
throw new Error("bufferToReadable ne doit pas être appelé dans le navigateur");
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
async _passThrough() {
|
|
171
|
+
if (typeof window === "undefined") {
|
|
172
|
+
const { createPassThrough } = await import("../utils/stream-utils.node.js");
|
|
173
|
+
return createPassThrough();
|
|
174
|
+
} else {
|
|
175
|
+
throw new Error("passThrough ne doit pas être appelé dans le navigateur");
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
// async _bufferToReadable(buffer) {
|
|
180
|
+
// if (typeof window === "undefined") {
|
|
181
|
+
// const { Readable } = await import("stream");
|
|
182
|
+
// return Readable.from(buffer);
|
|
183
|
+
// } else {
|
|
184
|
+
// throw new Error("bufferToReadable ne doit pas être appelé dans le navigateur");
|
|
185
|
+
// }
|
|
186
|
+
// },
|
|
187
|
+
|
|
188
|
+
// async _passThrough() {
|
|
189
|
+
// if (typeof window === "undefined") {
|
|
190
|
+
// const { PassThrough } = await import("stream");
|
|
191
|
+
// return new PassThrough();
|
|
192
|
+
// } else {
|
|
193
|
+
// throw new Error("passThrough ne doit pas être appelé dans le navigateur");
|
|
194
|
+
// }
|
|
195
|
+
// },
|
|
196
|
+
|
|
197
|
+
_omitProps(obj, propsToRemove) {
|
|
198
|
+
if (!obj || typeof obj !== "object") return {};
|
|
199
|
+
|
|
200
|
+
const result = { ...obj };
|
|
201
|
+
for (const prop of propsToRemove) {
|
|
202
|
+
delete result[prop];
|
|
203
|
+
}
|
|
204
|
+
return result;
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
_pickProps(obj, keys, transforms = {}) {
|
|
208
|
+
if (!obj || typeof obj !== "object") return {};
|
|
209
|
+
|
|
210
|
+
const result = {};
|
|
211
|
+
|
|
212
|
+
for (const key of keys) {
|
|
213
|
+
if (key in obj) {
|
|
214
|
+
const value = obj[key];
|
|
215
|
+
if (typeof transforms[key] === "function") {
|
|
216
|
+
result[key] = transforms[key](value, obj); // (valeur, objet source)
|
|
217
|
+
} else {
|
|
218
|
+
result[key] = value;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return result;
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
_createFilteredProxy(list) {
|
|
227
|
+
return new Proxy(list, {
|
|
228
|
+
get(target, prop, receiver) {
|
|
229
|
+
if (typeof prop === "string" && !isNaN(prop)) {
|
|
230
|
+
const active = target.filter(n => !n._isDeleted);
|
|
231
|
+
return active[prop];
|
|
232
|
+
}
|
|
233
|
+
if (prop === "length") {
|
|
234
|
+
return target.filter(n => !n._isDeleted).length;
|
|
235
|
+
}
|
|
236
|
+
if (typeof target[prop] === "function") {
|
|
237
|
+
return (...args) => target.filter(n => !n._isDeleted)[prop](...args);
|
|
238
|
+
}
|
|
239
|
+
return Reflect.get(target, prop, receiver);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
};
|
|
246
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// utils/stream-utils.node.js
|
|
2
|
+
export async function bufferToReadable(buffer) {
|
|
3
|
+
const { Readable } = await import("stream");
|
|
4
|
+
return Readable.from(buffer);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export async function createPassThrough() {
|
|
8
|
+
const { PassThrough } = await import("stream");
|
|
9
|
+
return new PassThrough();
|
|
10
|
+
}
|
package/src/api/EntityMixin.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { ApiResponseError } from "../error.js";
|
|
2
|
-
|
|
3
|
-
// EntityMixin.js
|
|
4
|
-
export const EntityMixin = {
|
|
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
|
-
return this.apiClient.safeCall(async () => {
|
|
12
|
-
if (!this.id && this.slug) {
|
|
13
|
-
const response = await this.apiClient.callEndpoint("GET_ELEMENTS_KEY", {
|
|
14
|
-
pathParams:{
|
|
15
|
-
slug: this.slug
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
if(response?.data?.result === true) {
|
|
19
|
-
if(response?.data?.contextId && response?.data?.contextType === type) {
|
|
20
|
-
this.id = response.data.contextId;
|
|
21
|
-
} else {
|
|
22
|
-
throw new ApiResponseError(`Le slug ${this.slug} ne correspond pas à un ${type}`, response.status, response.data);
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
throw new ApiResponseError(`Impossible de récupérer l'identifiant pour le slug ${this.slug}`, response.status, response.data);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return this.id;
|
|
29
|
-
});
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Récupère le profil public de l'entité.
|
|
34
|
-
* @returns {Promise<Object>} Les données du profil public.
|
|
35
|
-
*/
|
|
36
|
-
async getPublicProfile() {
|
|
37
|
-
return this.apiClient.safeCall(async () => {
|
|
38
|
-
await this.resolveId(this.getEntityType());
|
|
39
|
-
return this.apiClient.callEndpoint("GET_ELEMENTS_ABOUT", { pathParams: { id: this.id } });
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|