@communecter/cocolight-api-client 1.0.51 → 1.0.55
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/401.cocolight-api-client.browser.js +1 -0
- package/dist/401.cocolight-api-client.cjs +1 -0
- package/dist/401.cocolight-api-client.mjs.js +1 -0
- package/dist/588.cocolight-api-client.browser.js +1 -0
- package/dist/588.cocolight-api-client.cjs +1 -0
- package/dist/588.cocolight-api-client.mjs.js +1 -0
- package/dist/593.cocolight-api-client.browser.js +1 -0
- package/dist/593.cocolight-api-client.cjs +1 -0
- package/dist/593.cocolight-api-client.mjs.js +1 -0
- package/dist/839.cocolight-api-client.browser.js +1 -0
- package/dist/839.cocolight-api-client.cjs +1 -0
- package/dist/839.cocolight-api-client.mjs.js +1 -0
- 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/dist/cocolight-api-client.vite.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js.map +1 -1
- package/package.json +28 -10
- package/src/{Api.js → Api.ts} +99 -91
- package/src/{ApiClient.js → ApiClient.ts} +444 -240
- package/src/EJSONType.ts +103 -0
- package/src/api/{Badge.js → Badge.ts} +56 -45
- package/src/api/BaseEntity.ts +3897 -0
- package/src/api/Comment.ts +200 -0
- package/src/api/{EndpointApi.js → EndpointApi.ts} +365 -299
- package/src/api/{EndpointApi.types.d.ts → EndpointApi.types.ts} +166 -9
- package/src/api/EntityRegistry.ts +208 -0
- package/src/api/Event.ts +332 -0
- package/src/api/News.ts +331 -0
- package/src/api/{Organization.js → Organization.ts} +155 -119
- package/src/api/{Poi.js → Poi.ts} +68 -60
- package/src/api/{Project.js → Project.ts} +150 -127
- package/src/api/{User.js → User.ts} +321 -256
- package/src/api/UserApi.ts +148 -0
- package/src/api/serverDataType/Comment.ts +88 -0
- package/src/api/serverDataType/Event.ts +80 -0
- package/src/api/serverDataType/News.ts +138 -0
- package/src/api/serverDataType/Organization.ts +80 -0
- package/src/api/serverDataType/Project.ts +71 -0
- package/src/api/serverDataType/User.ts +103 -0
- package/src/api/serverDataType/common.ts +80 -0
- package/src/endpoints.module.ts +2621 -0
- package/src/error.ts +86 -0
- package/src/index.ts +86 -0
- package/src/mixin/UserMixin.ts +4 -0
- package/src/types/api-responses.ts +217 -0
- package/src/types/entities.ts +22 -0
- package/src/types/error-guards.ts +230 -0
- package/src/types/index.ts +39 -0
- package/src/types/payloads.ts +21 -0
- package/src/types/transforms.ts +110 -0
- package/src/utils/{FileOfflineStorageStrategy.node.js → FileOfflineStorageStrategy.node.ts} +15 -12
- package/src/utils/{FileStorageStrategy.node.js → FileStorageStrategy.node.ts} +17 -14
- package/src/utils/MultiServerFileStorageStrategy.node.ts +67 -0
- package/src/utils/MultiServerTokenStorageStrategy.ts +139 -0
- package/src/utils/{OfflineClientManager.js → OfflineClientManager.ts} +82 -86
- package/src/utils/OfflineQueueStorageStrategy.ts +47 -0
- package/src/utils/TokenStorage.ts +77 -0
- package/src/utils/compat.ts +12 -0
- package/src/utils/createDefaultMultiServerTokenStorageStrategy.ts +35 -0
- package/src/utils/{createDefaultOfflineStrategy.js → createDefaultOfflineStrategy.ts} +8 -3
- package/src/utils/createDefaultTokenStorageStrategy.ts +33 -0
- package/src/utils/{reactive.js → reactive.ts} +49 -40
- package/src/utils/stream-utils.node.ts +12 -0
- package/types/Api.d.ts +87 -0
- package/types/Api.d.ts.map +1 -0
- package/types/ApiClient.d.ts +437 -0
- package/types/ApiClient.d.ts.map +1 -0
- package/types/EJSONType.d.ts +53 -0
- package/types/EJSONType.d.ts.map +1 -0
- package/types/api/Badge.d.ts +24 -0
- package/types/api/Badge.d.ts.map +1 -0
- package/types/api/BaseEntity.d.ts +1322 -0
- package/types/api/BaseEntity.d.ts.map +1 -0
- package/types/api/Comment.d.ts +36 -0
- package/types/api/EndpointApi.d.ts +985 -0
- package/types/api/EndpointApi.d.ts.map +1 -0
- package/types/api/EndpointApi.types.d.ts +4233 -0
- package/types/api/EntityRegistry.d.ts +24 -0
- package/types/api/EntityRegistry.d.ts.map +1 -0
- package/types/api/Event.d.ts +122 -0
- package/types/api/Event.d.ts.map +1 -0
- package/types/api/News.d.ts +77 -0
- package/types/api/News.d.ts.map +1 -0
- package/types/api/Organization.d.ts +203 -0
- package/types/api/Organization.d.ts.map +1 -0
- package/types/api/Poi.d.ts +54 -0
- package/types/api/Poi.d.ts.map +1 -0
- package/types/api/Project.d.ts +180 -0
- package/types/api/Project.d.ts.map +1 -0
- package/types/api/User.d.ts +332 -0
- package/types/api/User.d.ts.map +1 -0
- package/types/api/UserApi.d.ts +64 -0
- package/types/api/UserApi.d.ts.map +1 -0
- package/types/api/serverDataType/Comment.d.ts +83 -0
- package/types/api/serverDataType/Event.d.ts +67 -0
- package/types/api/serverDataType/News.d.ts +130 -0
- package/types/api/serverDataType/Organization.d.ts +65 -0
- package/types/api/serverDataType/Organization.d.ts.map +1 -0
- package/types/api/serverDataType/Project.d.ts +58 -0
- package/types/api/serverDataType/Project.d.ts.map +1 -0
- package/types/api/serverDataType/User.d.ts +86 -0
- package/types/api/serverDataType/User.d.ts.map +1 -0
- package/types/api/serverDataType/common.d.ts +71 -0
- package/types/api/serverDataType/common.d.ts.map +1 -0
- package/types/endpoints.module.d.ts +20559 -0
- package/types/endpoints.module.d.ts.map +1 -0
- package/types/error.d.ts +54 -0
- package/types/error.d.ts.map +1 -0
- package/types/index.d.ts +59 -0
- package/types/index.d.ts.map +1 -0
- package/types/mixin/UserMixin.d.ts +1 -0
- package/types/mixin/UserMixin.d.ts.map +1 -0
- package/types/types/api-responses.d.ts +190 -0
- package/types/types/api-responses.d.ts.map +1 -0
- package/types/types/entities.d.ts +17 -0
- package/types/types/entities.d.ts.map +1 -0
- package/types/types/error-guards.d.ts +99 -0
- package/types/types/error-guards.d.ts.map +1 -0
- package/types/types/index.d.ts +7 -0
- package/types/types/payloads.d.ts +17 -0
- package/types/types/payloads.d.ts.map +1 -0
- package/types/types/transforms.d.ts +79 -0
- package/types/types/transforms.d.ts.map +1 -0
- package/types/utils/FileOfflineStorageStrategy.node.d.ts +11 -0
- package/types/utils/FileOfflineStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/FileStorageStrategy.node.d.ts +14 -0
- package/types/utils/FileStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/MultiServerFileStorageStrategy.node.d.ts +17 -0
- package/types/utils/MultiServerFileStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/MultiServerTokenStorageStrategy.d.ts +44 -0
- package/types/utils/MultiServerTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/OfflineClientManager.d.ts +58 -0
- package/types/utils/OfflineClientManager.d.ts.map +1 -0
- package/types/utils/OfflineQueueStorageStrategy.d.ts +16 -0
- package/types/utils/OfflineQueueStorageStrategy.d.ts.map +1 -0
- package/types/utils/TokenStorage.d.ts +26 -0
- package/types/utils/TokenStorage.d.ts.map +1 -0
- package/types/utils/compat.d.ts +4 -0
- package/types/utils/compat.d.ts.map +1 -0
- package/types/utils/createDefaultMultiServerTokenStorageStrategy.d.ts +2 -0
- package/types/utils/createDefaultMultiServerTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/createDefaultOfflineStrategy.d.ts +2 -0
- package/types/utils/createDefaultOfflineStrategy.d.ts.map +1 -0
- package/types/utils/createDefaultTokenStorageStrategy.d.ts +2 -0
- package/types/utils/createDefaultTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/reactive.d.ts +54 -0
- package/types/utils/reactive.d.ts.map +1 -0
- package/types/utils/stream-utils.node.d.ts +3 -0
- package/types/utils/stream-utils.node.d.ts.map +1 -0
- package/dist/123.cocolight-api-client.browser.js +0 -1
- package/dist/123.cocolight-api-client.cjs +0 -1
- package/dist/22.cocolight-api-client.mjs.js +0 -1
- package/dist/339.cocolight-api-client.mjs.js +0 -1
- package/dist/394.cocolight-api-client.browser.js +0 -1
- package/dist/394.cocolight-api-client.cjs +0 -1
- package/dist/405.cocolight-api-client.browser.js +0 -1
- package/dist/405.cocolight-api-client.cjs +0 -1
- package/dist/774.cocolight-api-client.mjs.js +0 -1
- package/dist/790.cocolight-api-client.mjs.js +0 -1
- package/dist/931.cocolight-api-client.browser.js +0 -1
- package/dist/931.cocolight-api-client.cjs +0 -1
- package/src/EJSONType.js +0 -53
- package/src/api/BaseEntity.js +0 -2828
- package/src/api/EntityRegistry.js +0 -152
- package/src/api/Event.js +0 -226
- package/src/api/News.js +0 -244
- package/src/api/UserApi.js +0 -81
- package/src/endpoints.module.js +0 -5
- package/src/error.js +0 -68
- package/src/index.js +0 -34
- package/src/mixin/UserMixin.js +0 -8
- package/src/utils/MultiServerFileStorageStrategy.node.js +0 -65
- package/src/utils/MultiServerTokenStorageStrategy.js +0 -131
- package/src/utils/OfflineQueueStorageStrategy.js +0 -51
- package/src/utils/TokenStorage.js +0 -93
- package/src/utils/createDefaultMultiServerTokenStorageStrategy.js +0 -45
- package/src/utils/createDefaultTokenStorageStrategy.js +0 -43
- package/src/utils/stream-utils.node.js +0 -10
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.55",
|
|
4
4
|
"description": "Client Axios simplifié pour l'API cocolight",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"module": "./dist/cocolight-api-client.vite.mjs.js",
|
|
12
12
|
"browser": "./dist/cocolight-api-client.browser.js",
|
|
13
13
|
"exports": {
|
|
14
|
+
"types": "./types/index.d.ts",
|
|
14
15
|
"import": {
|
|
15
16
|
"vite": "./dist/cocolight-api-client.vite.mjs.js",
|
|
16
17
|
"browser": "./dist/cocolight-api-client.vite.mjs.js",
|
|
@@ -20,27 +21,31 @@
|
|
|
20
21
|
"default": "./dist/cocolight-api-client.mjs.js"
|
|
21
22
|
},
|
|
22
23
|
"type": "module",
|
|
24
|
+
"types": "./types/index.d.ts",
|
|
23
25
|
"scripts": {
|
|
24
|
-
"test": "
|
|
26
|
+
"test": "vitest run",
|
|
27
|
+
"test:watch": "vitest watch",
|
|
28
|
+
"test:ui": "vitest --ui",
|
|
29
|
+
"test:old": "node --experimental-vm-modules ./node_modules/.bin/jest --config jest.config.js",
|
|
25
30
|
"exemple": "node exemple/exemple.js",
|
|
26
|
-
"exemple-api": "
|
|
27
|
-
"lint": "eslint ./src ./
|
|
28
|
-
"lint:fix": "eslint ./src ./scripts --ext .js --fix",
|
|
31
|
+
"exemple-api": "tsx exemple/exemple-api.ts",
|
|
32
|
+
"lint": "eslint ./src ./scripts --ext .js,.ts",
|
|
33
|
+
"lint:fix": "eslint ./src ./scripts --ext .js,.ts --fix",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
29
35
|
"build:browser": "webpack --config webpack.config.standalone.js",
|
|
30
36
|
"build:node": "webpack --config webpack.config.node.js",
|
|
31
37
|
"build:esm": "webpack --config webpack.config.esm.mjs",
|
|
32
38
|
"build:esm:rollup": "rollup -c rollup.config.mjs",
|
|
33
39
|
"verif:esm": "webpack --config webpack.config.esm.mjs --profile --json > stats.json",
|
|
34
|
-
"build": "
|
|
40
|
+
"build:ts": "tsc --project tsconfig.json",
|
|
41
|
+
"build:types": "npm run generate:methodeapi && tsc --project tsconfig.types.json",
|
|
42
|
+
"build": "npm run generate:module:publish && npm run generate:methodeapi && npm run generate:doc && npm run lint:fix && npm run build:browser && npm run build:node && npm run build:esm && npm run build:esm:rollup && npm run build:types",
|
|
35
43
|
"build:publish": "npm run build && npm publish --access public",
|
|
36
44
|
"generate:module": "node ./scripts/transform-json-module.js",
|
|
37
45
|
"generate:module:publish": "node ./scripts/transform-json-module.publish.js",
|
|
38
46
|
"generate:doc": "node ./scripts/generate-doc.js",
|
|
39
|
-
"generate:test": "node ./scripts/generate-tests.js",
|
|
40
|
-
"generate:testdata": "node ./scripts/generate-test-data.js",
|
|
41
47
|
"generate:reponses": "node ./scripts/generate-constant-response-200.js",
|
|
42
|
-
"generate:methodeapi": "node ./scripts/generate-methode-api.js"
|
|
43
|
-
"generate:entities": "node scripts/generate-entities.js"
|
|
48
|
+
"generate:methodeapi": "node ./scripts/generate-methode-api.js"
|
|
44
49
|
},
|
|
45
50
|
"keywords": [
|
|
46
51
|
"communecter",
|
|
@@ -54,6 +59,7 @@
|
|
|
54
59
|
"files": [
|
|
55
60
|
"dist/",
|
|
56
61
|
"src/",
|
|
62
|
+
"types/",
|
|
57
63
|
"package.json",
|
|
58
64
|
"README.md"
|
|
59
65
|
],
|
|
@@ -74,13 +80,19 @@
|
|
|
74
80
|
"devDependencies": {
|
|
75
81
|
"@babel/core": "^7.26.10",
|
|
76
82
|
"@babel/preset-env": "^7.26.9",
|
|
83
|
+
"@babel/preset-typescript": "^7.27.1",
|
|
77
84
|
"@eslint/js": "^9.23.0",
|
|
78
85
|
"@rollup/plugin-babel": "^6.0.4",
|
|
79
86
|
"@rollup/plugin-commonjs": "^28.0.3",
|
|
80
87
|
"@rollup/plugin-json": "^6.1.0",
|
|
81
88
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
82
89
|
"@rollup/plugin-terser": "^0.4.4",
|
|
90
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
83
91
|
"@segment/ajv-human-errors": "^2.15.0",
|
|
92
|
+
"@types/ejson": "^2.2.2",
|
|
93
|
+
"@typescript-eslint/eslint-plugin": "^8.46.2",
|
|
94
|
+
"@typescript-eslint/parser": "^8.46.2",
|
|
95
|
+
"@vitest/ui": "^4.0.1",
|
|
84
96
|
"ajv": "^8.17.1",
|
|
85
97
|
"ajv-formats": "^3.0.1",
|
|
86
98
|
"axios": "^1.4.0",
|
|
@@ -88,6 +100,7 @@
|
|
|
88
100
|
"babel-jest": "^29.7.0",
|
|
89
101
|
"babel-loader": "^10.0.0",
|
|
90
102
|
"bson-objectid": "^2.0.4",
|
|
103
|
+
"dotenv": "^17.2.3",
|
|
91
104
|
"ejson": "^2.2.3",
|
|
92
105
|
"eslint": "^9.23.0",
|
|
93
106
|
"eslint-plugin-import": "^2.31.0",
|
|
@@ -102,6 +115,11 @@
|
|
|
102
115
|
"pino-pretty": "^13.0.0",
|
|
103
116
|
"rollup": "^4.40.0",
|
|
104
117
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
118
|
+
"ts-loader": "^9.5.4",
|
|
119
|
+
"tslib": "^2.8.1",
|
|
120
|
+
"tsx": "^4.20.6",
|
|
121
|
+
"typescript": "^5.9.2",
|
|
122
|
+
"vitest": "^4.0.1",
|
|
105
123
|
"webpack": "^5.98.0",
|
|
106
124
|
"webpack-cli": "^6.0.1"
|
|
107
125
|
}
|
package/src/{Api.js → Api.ts}
RENAMED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
// Api.
|
|
1
|
+
// Api.ts
|
|
2
2
|
import { Badge } from "./api/Badge.js";
|
|
3
|
+
import { Comment } from "./api/Comment.js";
|
|
3
4
|
import EndpointApi from "./api/EndpointApi.js";
|
|
4
5
|
import { createFromCollection, registerEntity } from "./api/EntityRegistry.js";
|
|
5
6
|
import { Event } from "./api/Event.js";
|
|
@@ -11,6 +12,10 @@ import { User } from "./api/User.js";
|
|
|
11
12
|
import { UserApi } from "./api/UserApi.js";
|
|
12
13
|
import { ApiAuthenticationError, ApiClientError, ApiError, ApiResponseError } from "./error.js";
|
|
13
14
|
|
|
15
|
+
import type ApiClient from "./ApiClient.js";
|
|
16
|
+
import type { ApiClientOptions } from "./ApiClient.js";
|
|
17
|
+
import type { GetElementsKeyResponse } from "./types/api-responses.js";
|
|
18
|
+
|
|
14
19
|
registerEntity("User", User);
|
|
15
20
|
registerEntity("Organization", Organization);
|
|
16
21
|
registerEntity("Project", Project);
|
|
@@ -18,59 +23,68 @@ registerEntity("Event", Event);
|
|
|
18
23
|
registerEntity("Poi", Poi);
|
|
19
24
|
registerEntity("Badge", Badge);
|
|
20
25
|
registerEntity("EndpointApi", EndpointApi);
|
|
21
|
-
registerEntity("UserApi", UserApi);
|
|
26
|
+
// registerEntity("UserApi", UserApi);
|
|
22
27
|
registerEntity("News", News);
|
|
28
|
+
registerEntity("Comment", Comment);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Union type for all possible entity types
|
|
32
|
+
*/
|
|
33
|
+
type EntityTypes = User | Organization | Project | Event | Poi | Badge | News | Comment;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Type pour récupérer une entité existante via l'API publique.
|
|
37
|
+
* Nécessite soit un id, soit un slug.
|
|
38
|
+
* Les propriétés additionnelles sont autorisées mais ignorées lors de la récupération.
|
|
39
|
+
*/
|
|
40
|
+
type EntityData =
|
|
41
|
+
| { id: string; slug?: string; [key: string]: any }
|
|
42
|
+
| { slug: string; id?: string; [key: string]: any };
|
|
23
43
|
|
|
24
44
|
export default class Api {
|
|
45
|
+
private _loggedUser: User | null;
|
|
46
|
+
private _client: ApiClient;
|
|
47
|
+
|
|
25
48
|
/**
|
|
26
49
|
* Authentifie l'utilisateur et retourne une instance d'Api.
|
|
27
|
-
*
|
|
28
|
-
* @param {string} email - L'adresse email.
|
|
29
|
-
* @param {string} password - Le mot de passe.
|
|
30
|
-
* @param {function|object} options - Options pour l'ApiClient (baseURL, debug, etc.) ou une instance d'ApiClient.
|
|
31
|
-
* @returns {Promise<Api>}
|
|
32
50
|
*/
|
|
33
|
-
static async userLogin(
|
|
51
|
+
static async userLogin(
|
|
52
|
+
email: string,
|
|
53
|
+
password: string,
|
|
54
|
+
options: ApiClientOptions | ApiClient
|
|
55
|
+
): Promise<Api> {
|
|
34
56
|
const userApi = Api.userApi(options);
|
|
35
57
|
return await Api.userApiLogin(userApi, email, password);
|
|
36
58
|
}
|
|
37
59
|
|
|
38
60
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* @param @param {function|object} options - Options for the ApiClient (baseURL, debug, etc.) or an instance of ApiClient.
|
|
42
|
-
* @returns {UserApi} An instance of the UserApi class.
|
|
43
|
-
* @throws {Error} Throws an error if the UserApi instance cannot be created.
|
|
61
|
+
* Crée une instance de UserApi avec les options fournies.
|
|
44
62
|
*/
|
|
45
|
-
static userApi(options) {
|
|
63
|
+
static userApi(options: ApiClientOptions | ApiClient): UserApi {
|
|
46
64
|
try {
|
|
47
65
|
const userApi = new UserApi(options);
|
|
48
66
|
return userApi;
|
|
49
67
|
} catch (error) {
|
|
50
|
-
console.error("[Api.userApi] Erreur lors de la création d'un objet utilisateur :", error.message);
|
|
68
|
+
console.error("[Api.userApi] Erreur lors de la création d'un objet utilisateur :", (error as Error).message);
|
|
51
69
|
throw error;
|
|
52
70
|
}
|
|
53
71
|
}
|
|
54
72
|
|
|
55
73
|
/**
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
* @throws {ApiAuthenticationError} If the login fails due to authentication issues.
|
|
64
|
-
* @throws {Error} If an unexpected error occurs during the login process.
|
|
65
|
-
*/
|
|
66
|
-
static async userApiLogin(userApi, email, password) {
|
|
74
|
+
* Connecte un utilisateur avec un UserApi existant.
|
|
75
|
+
*/
|
|
76
|
+
static async userApiLogin(
|
|
77
|
+
userApi: UserApi,
|
|
78
|
+
email: string,
|
|
79
|
+
password: string
|
|
80
|
+
): Promise<Api> {
|
|
67
81
|
try {
|
|
68
82
|
|
|
69
83
|
if (!userApi) {
|
|
70
|
-
throw new ApiError("userApi is not defined");
|
|
84
|
+
throw new ApiError("userApi is not defined", 500);
|
|
71
85
|
}
|
|
72
86
|
if(!userApi.client) {
|
|
73
|
-
throw new ApiError("userApi.client is not defined");
|
|
87
|
+
throw new ApiError("userApi.client is not defined", 500);
|
|
74
88
|
}
|
|
75
89
|
|
|
76
90
|
if(userApi.client.isConnected) {
|
|
@@ -80,7 +94,7 @@ export default class Api {
|
|
|
80
94
|
return new Api(loggedUser, userApi.client);
|
|
81
95
|
} else {
|
|
82
96
|
if (!email || !password) {
|
|
83
|
-
throw new ApiError("email and password are required");
|
|
97
|
+
throw new ApiError("email and password are required", 400);
|
|
84
98
|
}
|
|
85
99
|
const loggedUser = await userApi.login(email, password);
|
|
86
100
|
return new Api(loggedUser, userApi.client);
|
|
@@ -98,47 +112,37 @@ export default class Api {
|
|
|
98
112
|
|
|
99
113
|
/**
|
|
100
114
|
* Construit une instance d'Api.
|
|
101
|
-
*
|
|
102
|
-
* @param {User|null} loggedUser - L'utilisateur connecté ou null.
|
|
103
|
-
* @param {ApiClient} client - L'instance d'ApiClient.
|
|
104
115
|
*/
|
|
105
|
-
constructor(loggedUser, client) {
|
|
116
|
+
constructor(loggedUser: User | null, client: ApiClient) {
|
|
106
117
|
this._loggedUser = loggedUser;
|
|
107
118
|
this._client = client;
|
|
108
119
|
}
|
|
109
120
|
|
|
110
121
|
/**
|
|
111
122
|
* Retourne l'utilisateur connecté.
|
|
112
|
-
*
|
|
113
|
-
* @returns {Promise<User>} L'utilisateur connecté.
|
|
114
|
-
* @throws {ApiAuthenticationError} Si l'utilisateur n'est pas authentifié.
|
|
115
123
|
*/
|
|
116
|
-
async me() {
|
|
124
|
+
async me(): Promise<User> {
|
|
117
125
|
if (!this._loggedUser) {
|
|
118
|
-
throw new ApiAuthenticationError("Accès refusé : utilisateur non authentifié.");
|
|
126
|
+
throw new ApiAuthenticationError("Accès refusé : utilisateur non authentifié.", 401);
|
|
119
127
|
}
|
|
120
128
|
await this._loggedUser.get();
|
|
121
129
|
return this._loggedUser;
|
|
122
130
|
}
|
|
123
131
|
|
|
124
132
|
/**
|
|
125
|
-
* Crée une instance User
|
|
126
|
-
*
|
|
127
|
-
* @param {Object} userData - Les données de l'utilisateur public.
|
|
128
|
-
* @returns {Promise<User>} Une promesse qui résout l'instance User.
|
|
129
|
-
* @throws {Error} Si une erreur se produit lors de la création de l'utilisateur.
|
|
133
|
+
* Crée une instance User (autre que le connecté).
|
|
130
134
|
*/
|
|
131
|
-
async user(userData) {
|
|
135
|
+
async user(userData: EntityData): Promise<User> {
|
|
132
136
|
try {
|
|
133
137
|
if (!userData.id && !userData.slug) {
|
|
134
|
-
return new User(this._client, userData, { EndpointApi, Organization, Project, Event, Poi, Badge, News });
|
|
138
|
+
return new User(this._client, userData, { EndpointApi, Organization, Project, Event, Poi, Badge, News, Comment });
|
|
135
139
|
} else {
|
|
136
|
-
const user = new User(this._client, userData, { EndpointApi, Organization, Project, Event, Poi, Badge, News });
|
|
140
|
+
const user = new User(this._client, userData, { EndpointApi, Organization, Project, Event, Poi, Badge, News, Comment });
|
|
137
141
|
await user.get();
|
|
138
142
|
return user;
|
|
139
143
|
}
|
|
140
144
|
} catch (error) {
|
|
141
|
-
console.error("[Api.user] Erreur lors de la création d'un objet utilisateur public :", error.message);
|
|
145
|
+
console.error("[Api.user] Erreur lors de la création d'un objet utilisateur public :", (error as Error).message);
|
|
142
146
|
throw error;
|
|
143
147
|
}
|
|
144
148
|
}
|
|
@@ -146,105 +150,109 @@ export default class Api {
|
|
|
146
150
|
|
|
147
151
|
/**
|
|
148
152
|
* Creates an Organization object and optionally retrieves its profile.
|
|
149
|
-
*
|
|
150
|
-
* @param {Object} organizationData - The data required to initialize the Organization object.
|
|
151
|
-
* @returns {Promise<Organization>} A promise that resolves to the created Organization object.
|
|
152
|
-
* @throws {Error} Throws an error if the organization creation or profile retrieval fails.
|
|
153
153
|
*/
|
|
154
|
-
async organization(organizationData) {
|
|
154
|
+
async organization(organizationData: EntityData): Promise<Organization> {
|
|
155
155
|
try {
|
|
156
|
-
const organization = new Organization(this._client, organizationData, { EndpointApi, User, Project, Event, Poi, Badge, News });
|
|
156
|
+
const organization = new Organization(this._client, organizationData, { EndpointApi, User, Project, Event, Poi, Badge, News, Comment });
|
|
157
157
|
if (!organizationData.id && !organizationData.slug) {
|
|
158
158
|
throw new Error("Vous devez fournir un id ou un slug pour créer une instance Organization.");
|
|
159
159
|
}
|
|
160
160
|
await organization.get();
|
|
161
161
|
return organization;
|
|
162
162
|
} catch (error) {
|
|
163
|
-
console.error("[Api.organization] Erreur lors de la création d'un objet organisation :", error.message);
|
|
163
|
+
console.error("[Api.organization] Erreur lors de la création d'un objet organisation :", (error as Error).message);
|
|
164
164
|
throw error;
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
/**
|
|
169
169
|
* Creates a new Project instance and optionally retrieves its profile.
|
|
170
|
-
*
|
|
171
|
-
* @param {Object} projectData - The data used to initialize the Project instance.
|
|
172
|
-
* @returns {Promise<Project>} A promise that resolves to the created Project instance.
|
|
173
|
-
* @throws {Error} If an error occurs during project creation or profile retrieval.
|
|
174
170
|
*/
|
|
175
|
-
async project(projectData) {
|
|
171
|
+
async project(projectData: EntityData): Promise<Project> {
|
|
176
172
|
try {
|
|
177
|
-
const project = new Project(this._client, projectData, { EndpointApi, User, Organization, Event, Poi, Badge, News });
|
|
173
|
+
const project = new Project(this._client, projectData, { EndpointApi, User, Organization, Event, Poi, Badge, News, Comment });
|
|
178
174
|
if (!projectData.id && !projectData.slug) {
|
|
179
175
|
throw new Error("Vous devez fournir un id ou un slug pour créer une instance Project.");
|
|
180
176
|
}
|
|
181
177
|
await project.get();
|
|
182
178
|
return project;
|
|
183
179
|
} catch (error) {
|
|
184
|
-
console.error("[Api.project] Erreur lors de la création d'un objet projet :", error.message);
|
|
180
|
+
console.error("[Api.project] Erreur lors de la création d'un objet projet :", (error as Error).message);
|
|
185
181
|
throw error;
|
|
186
182
|
}
|
|
187
183
|
}
|
|
188
184
|
|
|
189
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Creates a new Event instance and optionally retrieves its profile.
|
|
187
|
+
*/
|
|
188
|
+
async event(eventData: EntityData): Promise<Event> {
|
|
190
189
|
try {
|
|
191
|
-
const event = new Event(this._client, eventData, { EndpointApi, User, Organization, Project, Poi, Badge, News });
|
|
190
|
+
const event = new Event(this._client, eventData, { EndpointApi, User, Organization, Project, Poi, Badge, News, Comment });
|
|
192
191
|
if (!eventData.id && !eventData.slug) {
|
|
193
192
|
throw new Error("Vous devez fournir un id ou un slug pour créer une instance Event.");
|
|
194
193
|
}
|
|
195
194
|
await event.get();
|
|
196
195
|
return event;
|
|
197
196
|
} catch (error) {
|
|
198
|
-
console.error("[Api.event] Erreur lors de la création d'un objet événement :", error.message);
|
|
197
|
+
console.error("[Api.event] Erreur lors de la création d'un objet événement :", (error as Error).message);
|
|
199
198
|
throw error;
|
|
200
199
|
}
|
|
201
200
|
}
|
|
202
201
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
return entity;
|
|
215
|
-
} else {
|
|
216
|
-
throw new ApiResponseError(`Le slug ${slug} n'est pas valide`, 200, data);
|
|
202
|
+
/**
|
|
203
|
+
* Retourne une entité à partir d'un slug.
|
|
204
|
+
*/
|
|
205
|
+
async entitySlug(slug: string): Promise<EntityTypes> {
|
|
206
|
+
if (!slug) {
|
|
207
|
+
throw new ApiError("slug requis", 400);
|
|
208
|
+
}
|
|
209
|
+
try {
|
|
210
|
+
const data = await this.endpointApi.getElementsKey({
|
|
211
|
+
pathParams:{
|
|
212
|
+
slug: slug
|
|
217
213
|
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
214
|
+
}) as GetElementsKeyResponse;
|
|
215
|
+
|
|
216
|
+
if(data.contextId && data.contextType) {
|
|
217
|
+
const entity = createFromCollection(data.contextType, this._client, { id: data.contextId });
|
|
218
|
+
if (!entity) {
|
|
219
|
+
throw new ApiResponseError(`Le type d'entité pour le slug ${slug} n'est pas reconnu`, 200, data as object);
|
|
223
220
|
}
|
|
221
|
+
await entity.get();
|
|
222
|
+
// On sait que c'est l'un des types concrets → on caste
|
|
223
|
+
return entity as EntityTypes;
|
|
224
|
+
} else {
|
|
225
|
+
throw new ApiResponseError(`Le slug ${slug} n'est pas valide`, 200, data as object);
|
|
226
|
+
}
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if(error instanceof ApiResponseError) {
|
|
229
|
+
throw new ApiResponseError(`Impossible de récupérer l'identifiant pour le slug ${slug}`, error.status, error.responseData);
|
|
230
|
+
} else {
|
|
231
|
+
throw error;
|
|
224
232
|
}
|
|
225
233
|
}
|
|
234
|
+
|
|
226
235
|
}
|
|
227
236
|
|
|
228
237
|
/**
|
|
229
238
|
* Retourne l'instance d'ApiClient.
|
|
230
|
-
*
|
|
231
|
-
* @returns {ApiClient}
|
|
232
239
|
*/
|
|
233
|
-
get client() {
|
|
240
|
+
get client(): ApiClient {
|
|
234
241
|
return this._client;
|
|
235
242
|
}
|
|
236
243
|
|
|
237
244
|
/**
|
|
238
245
|
* Retourne l'instance d'EndpointApi.
|
|
239
|
-
*
|
|
240
|
-
* @returns {EndpointApi}
|
|
241
246
|
*/
|
|
242
|
-
get endpointApi() {
|
|
247
|
+
get endpointApi(): EndpointApi {
|
|
243
248
|
return new EndpointApi(this._client);
|
|
244
249
|
}
|
|
245
250
|
|
|
246
|
-
|
|
247
|
-
|
|
251
|
+
/**
|
|
252
|
+
* Déconnecte l'utilisateur et réinitialise la session.
|
|
253
|
+
*/
|
|
254
|
+
logout(): void {
|
|
255
|
+
this._loggedUser = null;
|
|
248
256
|
this._client.resetSession();
|
|
249
257
|
this._client._logger.info("UserApi: User logged out");
|
|
250
258
|
}
|