@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/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 +4 -2
- package/src/Api.js +8 -3
- package/src/ApiClient.js +46 -10
- package/src/api/EndpointApi.js +1534 -0
- package/src/api/News.js +286 -0
- package/src/api/Organization.js +135 -33
- package/src/api/Project.js +136 -33
- package/src/api/User.js +52 -48
- package/src/api/UserApi.js +2 -1
- package/src/endpoints.module.js +2 -2
- 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/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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@communecter/cocolight-api-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "Client Axios simplifié pour l'API cocolight",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -60,7 +60,8 @@
|
|
|
60
60
|
"jwt-decode": "^4.0.0",
|
|
61
61
|
"pino": "^9.6.0",
|
|
62
62
|
"pino-pretty": "^13.0.0",
|
|
63
|
-
"file-type": "^20.4.1"
|
|
63
|
+
"file-type": "^20.4.1",
|
|
64
|
+
"bson-objectid": "^2.0.4"
|
|
64
65
|
},
|
|
65
66
|
"devDependencies": {
|
|
66
67
|
"@babel/core": "^7.26.10",
|
|
@@ -72,6 +73,7 @@
|
|
|
72
73
|
"axios": "^1.4.0",
|
|
73
74
|
"axios-retry": "^4.5.0",
|
|
74
75
|
"file-type": "^20.4.1",
|
|
76
|
+
"bson-objectid": "^2.0.4",
|
|
75
77
|
"babel-jest": "^29.7.0",
|
|
76
78
|
"babel-loader": "^10.0.0",
|
|
77
79
|
"ejson": "^2.2.3",
|
package/src/Api.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// Api.js
|
|
2
|
+
import EndpointApi from "./api/EndpointApi.js";
|
|
2
3
|
import { Organization } from "./api/Organization.js";
|
|
3
4
|
import { Project } from "./api/Project.js";
|
|
4
5
|
import { User } from "./api/User.js";
|
|
@@ -92,7 +93,7 @@ export default class Api {
|
|
|
92
93
|
*/
|
|
93
94
|
user(userData) {
|
|
94
95
|
try {
|
|
95
|
-
return new User(this._client, userData);
|
|
96
|
+
return new User(this._client, userData, {}, { EndpointApi });
|
|
96
97
|
} catch (error) {
|
|
97
98
|
console.error("[Api.user] Erreur lors de la création d'un objet utilisateur public :", error.message);
|
|
98
99
|
throw error;
|
|
@@ -111,7 +112,7 @@ export default class Api {
|
|
|
111
112
|
*/
|
|
112
113
|
async organization(organizationData, options = { getProfile: true }) {
|
|
113
114
|
try {
|
|
114
|
-
const oraganization = new Organization(this._client, organizationData);
|
|
115
|
+
const oraganization = new Organization(this._client, organizationData, { EndpointApi });
|
|
115
116
|
if (options.getProfile) {
|
|
116
117
|
await oraganization.getProfil();
|
|
117
118
|
}
|
|
@@ -133,7 +134,7 @@ export default class Api {
|
|
|
133
134
|
*/
|
|
134
135
|
async project(projectData, options = { getProfile: true }) {
|
|
135
136
|
try {
|
|
136
|
-
const project = new Project(this._client, projectData);
|
|
137
|
+
const project = new Project(this._client, projectData, { EndpointApi });
|
|
137
138
|
if (options.getProfile) {
|
|
138
139
|
await project.getProfil();
|
|
139
140
|
}
|
|
@@ -154,4 +155,8 @@ export default class Api {
|
|
|
154
155
|
return this._client;
|
|
155
156
|
}
|
|
156
157
|
|
|
158
|
+
get endpointApi() {
|
|
159
|
+
return new EndpointApi(this._client);
|
|
160
|
+
}
|
|
161
|
+
|
|
157
162
|
}
|
package/src/ApiClient.js
CHANGED
|
@@ -78,6 +78,13 @@ export default class ApiClient extends EventEmitter {
|
|
|
78
78
|
this._ajv = new Ajv({ strict: false, useDefaults: true, allErrors: true, verbose: true });
|
|
79
79
|
addFormats(this._ajv);
|
|
80
80
|
|
|
81
|
+
this._ajv.addKeyword({
|
|
82
|
+
keyword: "startBeforeEnd",
|
|
83
|
+
type: "object",
|
|
84
|
+
errors: true,
|
|
85
|
+
validate: this._startBeforeEndValidate
|
|
86
|
+
});
|
|
87
|
+
|
|
81
88
|
// Pino logger
|
|
82
89
|
// (Ici en mode pretty-print sur la console, tu peux configurer comme tu veux)
|
|
83
90
|
this._logger = pino({
|
|
@@ -582,9 +589,9 @@ export default class ApiClient extends EventEmitter {
|
|
|
582
589
|
const validateRequest = this._ajv.compile(schemaToCompile);
|
|
583
590
|
const valid = validateRequest(cleanedData);
|
|
584
591
|
if (!valid) {
|
|
585
|
-
const errorMessages = this._ajvErrorHuman(validateRequest.errors);
|
|
592
|
+
const errorMessages = validateRequest.errors ? this._ajvErrorHuman(validateRequest.errors) : [];
|
|
586
593
|
this.emit("validationError", { stage: "request", errors: validateRequest.errors });
|
|
587
|
-
throw new ApiValidationError("Request validation failed.", 400, errorMessages, validateRequest.errors);
|
|
594
|
+
throw new ApiValidationError("Request validation failed.", 400, errorMessages , validateRequest.errors);
|
|
588
595
|
}
|
|
589
596
|
|
|
590
597
|
data = this._resolveSpecialValuesInPlace(cleanedData, resolvedParams);
|
|
@@ -679,11 +686,16 @@ export default class ApiClient extends EventEmitter {
|
|
|
679
686
|
} catch (error) {
|
|
680
687
|
this._updateCircuitBreakerError();
|
|
681
688
|
this._logger.error(`[ApiClient] Erreur lors de l'appel de ${constant}: ${error.message}`);
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
689
|
+
if(error instanceof ApiValidationError) {
|
|
690
|
+
throw error;
|
|
691
|
+
} else {
|
|
692
|
+
throw new ApiClientError(
|
|
693
|
+
`Erreur lors de l'appel de l'API : ${error.message}`,
|
|
694
|
+
error.response ? error.response.status : 500,
|
|
695
|
+
error.response ? error.response.data : null
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
|
|
687
699
|
}
|
|
688
700
|
}
|
|
689
701
|
|
|
@@ -694,9 +706,14 @@ export default class ApiClient extends EventEmitter {
|
|
|
694
706
|
* @returns {Array<string>} An array of human-readable error messages extracted from the AJV errors.
|
|
695
707
|
*/
|
|
696
708
|
_ajvErrorHuman(errors){
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
709
|
+
try {
|
|
710
|
+
const errorsMessages = new AggregateAjvError(errors);
|
|
711
|
+
const messages = errorsMessages.errors.map(({ message }) => message);
|
|
712
|
+
return messages;
|
|
713
|
+
} catch (e) {
|
|
714
|
+
this._logger.error("[ApiClient] _ajvErrorHuman", e);
|
|
715
|
+
return errors.map(({ message }) => message);
|
|
716
|
+
}
|
|
700
717
|
}
|
|
701
718
|
|
|
702
719
|
/**
|
|
@@ -1343,5 +1360,24 @@ export default class ApiClient extends EventEmitter {
|
|
|
1343
1360
|
return data;
|
|
1344
1361
|
}
|
|
1345
1362
|
|
|
1363
|
+
_startBeforeEndValidate = (schema, data) => {
|
|
1364
|
+
if (!data.startDate || !data.endDate) return true;
|
|
1365
|
+
|
|
1366
|
+
const isValid = new Date(data.startDate) < new Date(data.endDate);
|
|
1367
|
+
if (!isValid) {
|
|
1368
|
+
this._startBeforeEndValidate.errors = [
|
|
1369
|
+
{
|
|
1370
|
+
instancePath: "", // ou "." si tu veux le chemin actuel
|
|
1371
|
+
schemaPath: "#/startBeforeEnd",
|
|
1372
|
+
keyword: "startBeforeEnd",
|
|
1373
|
+
params: {},
|
|
1374
|
+
message: "startDate must be before endDate"
|
|
1375
|
+
}
|
|
1376
|
+
];
|
|
1377
|
+
}
|
|
1378
|
+
return isValid;
|
|
1379
|
+
};
|
|
1380
|
+
|
|
1381
|
+
|
|
1346
1382
|
|
|
1347
1383
|
}
|