@communecter/cocolight-api-client 1.0.54 → 1.0.56
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 +29 -17
- package/src/{Api.js → Api.ts} +85 -95
- package/src/{ApiClient.js → ApiClient.ts} +436 -247
- package/src/EJSONType.ts +103 -0
- package/src/api/{Badge.js → Badge.ts} +56 -45
- package/src/api/BaseEntity.ts +3890 -0
- package/src/api/Comment.ts +200 -0
- package/src/api/{EndpointApi.js → EndpointApi.ts} +363 -297
- package/src/api/EndpointApi.types.ts +4609 -0
- package/src/api/EntityRegistry.ts +203 -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} +16 -39
- 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 +38 -82
- package/types/Api.d.ts.map +1 -0
- package/types/ApiClient.d.ts +244 -184
- package/types/ApiClient.d.ts.map +1 -0
- package/types/EJSONType.d.ts +48 -22
- package/types/EJSONType.d.ts.map +1 -0
- package/types/api/Badge.d.ts +20 -20
- package/types/api/Badge.d.ts.map +1 -0
- package/types/api/BaseEntity.d.ts +751 -446
- package/types/api/BaseEntity.d.ts.map +1 -0
- package/types/api/Comment.d.ts +36 -0
- package/types/api/EndpointApi.d.ts +347 -295
- package/types/api/EndpointApi.d.ts.map +1 -0
- package/types/api/EndpointApi.types.d.ts +3914 -4133
- package/types/api/EntityRegistry.d.ts +18 -16
- package/types/api/EntityRegistry.d.ts.map +1 -0
- package/types/api/Event.d.ts +119 -35
- package/types/api/Event.d.ts.map +1 -0
- package/types/api/News.d.ts +52 -20
- package/types/api/News.d.ts.map +1 -0
- package/types/api/Organization.d.ts +165 -49
- package/types/api/Organization.d.ts.map +1 -0
- package/types/api/Poi.d.ts +51 -22
- package/types/api/Poi.d.ts.map +1 -0
- package/types/api/Project.d.ts +151 -52
- package/types/api/Project.d.ts.map +1 -0
- package/types/api/User.d.ts +222 -93
- package/types/api/User.d.ts.map +1 -0
- package/types/api/UserApi.d.ts +60 -9
- 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 +6922 -1215
- package/types/endpoints.module.d.ts.map +1 -0
- package/types/error.d.ts +25 -51
- package/types/error.d.ts.map +1 -0
- package/types/index.d.ts +55 -48
- package/types/index.d.ts.map +1 -0
- package/types/mixin/UserMixin.d.ts +1 -1
- 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 +10 -9
- package/types/utils/FileOfflineStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/FileStorageStrategy.node.d.ts +9 -20
- package/types/utils/FileStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/MultiServerFileStorageStrategy.node.d.ts +13 -18
- package/types/utils/MultiServerFileStorageStrategy.node.d.ts.map +1 -0
- package/types/utils/MultiServerTokenStorageStrategy.d.ts +30 -51
- package/types/utils/MultiServerTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/OfflineClientManager.d.ts +52 -88
- package/types/utils/OfflineClientManager.d.ts.map +1 -0
- package/types/utils/OfflineQueueStorageStrategy.d.ts +12 -9
- package/types/utils/OfflineQueueStorageStrategy.d.ts.map +1 -0
- package/types/utils/TokenStorage.d.ts +20 -70
- 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 -11
- package/types/utils/createDefaultMultiServerTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/createDefaultOfflineStrategy.d.ts +2 -3
- package/types/utils/createDefaultOfflineStrategy.d.ts.map +1 -0
- package/types/utils/createDefaultTokenStorageStrategy.d.ts +2 -12
- package/types/utils/createDefaultTokenStorageStrategy.d.ts.map +1 -0
- package/types/utils/reactive.d.ts +10 -16
- package/types/utils/reactive.d.ts.map +1 -0
- package/types/utils/stream-utils.node.d.ts +3 -2
- 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 -121
- package/src/index.js +0 -97
- package/src/mixin/UserMixin.js +0 -8
- package/src/utils/MultiServerFileStorageStrategy.node.js +0 -87
- package/src/utils/MultiServerTokenStorageStrategy.js +0 -188
- package/src/utils/OfflineQueueStorageStrategy.js +0 -51
- package/src/utils/TokenStorage.js +0 -153
- package/src/utils/createDefaultMultiServerTokenStorageStrategy.js +0 -51
- package/src/utils/createDefaultTokenStorageStrategy.js +0 -49
- 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.56",
|
|
4
4
|
"description": "Client Axios simplifié pour l'API cocolight",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,38 +12,40 @@
|
|
|
12
12
|
"browser": "./dist/cocolight-api-client.browser.js",
|
|
13
13
|
"exports": {
|
|
14
14
|
"types": "./types/index.d.ts",
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
},
|
|
20
|
-
"require": "./dist/cocolight-api-client.cjs",
|
|
21
|
-
"default": "./dist/cocolight-api-client.mjs.js"
|
|
15
|
+
"import": {
|
|
16
|
+
"vite": "./dist/cocolight-api-client.vite.mjs.js",
|
|
17
|
+
"browser": "./dist/cocolight-api-client.vite.mjs.js",
|
|
18
|
+
"default": "./dist/cocolight-api-client.vite.mjs.js"
|
|
22
19
|
},
|
|
20
|
+
"require": "./dist/cocolight-api-client.cjs",
|
|
21
|
+
"default": "./dist/cocolight-api-client.mjs.js"
|
|
22
|
+
},
|
|
23
23
|
"type": "module",
|
|
24
24
|
"types": "./types/index.d.ts",
|
|
25
25
|
"scripts": {
|
|
26
|
-
"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",
|
|
27
30
|
"exemple": "node exemple/exemple.js",
|
|
28
|
-
"exemple-api": "
|
|
29
|
-
"lint": "eslint ./src ./
|
|
30
|
-
"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",
|
|
31
35
|
"build:browser": "webpack --config webpack.config.standalone.js",
|
|
32
36
|
"build:node": "webpack --config webpack.config.node.js",
|
|
33
37
|
"build:esm": "webpack --config webpack.config.esm.mjs",
|
|
34
38
|
"build:esm:rollup": "rollup -c rollup.config.mjs",
|
|
35
39
|
"verif:esm": "webpack --config webpack.config.esm.mjs --profile --json > stats.json",
|
|
36
|
-
"build:
|
|
40
|
+
"build:ts": "tsc --project tsconfig.json",
|
|
41
|
+
"build:types": "npm run generate:methodeapi && tsc --project tsconfig.types.json",
|
|
37
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",
|
|
38
43
|
"build:publish": "npm run build && npm publish --access public",
|
|
39
44
|
"generate:module": "node ./scripts/transform-json-module.js",
|
|
40
45
|
"generate:module:publish": "node ./scripts/transform-json-module.publish.js",
|
|
41
46
|
"generate:doc": "node ./scripts/generate-doc.js",
|
|
42
|
-
"generate:test": "node ./scripts/generate-tests.js",
|
|
43
|
-
"generate:testdata": "node ./scripts/generate-test-data.js",
|
|
44
47
|
"generate:reponses": "node ./scripts/generate-constant-response-200.js",
|
|
45
|
-
"generate:methodeapi": "node ./scripts/generate-methode-api.js"
|
|
46
|
-
"generate:entities": "node scripts/generate-entities.js"
|
|
48
|
+
"generate:methodeapi": "node ./scripts/generate-methode-api.js"
|
|
47
49
|
},
|
|
48
50
|
"keywords": [
|
|
49
51
|
"communecter",
|
|
@@ -78,14 +80,19 @@
|
|
|
78
80
|
"devDependencies": {
|
|
79
81
|
"@babel/core": "^7.26.10",
|
|
80
82
|
"@babel/preset-env": "^7.26.9",
|
|
83
|
+
"@babel/preset-typescript": "^7.27.1",
|
|
81
84
|
"@eslint/js": "^9.23.0",
|
|
82
85
|
"@rollup/plugin-babel": "^6.0.4",
|
|
83
86
|
"@rollup/plugin-commonjs": "^28.0.3",
|
|
84
87
|
"@rollup/plugin-json": "^6.1.0",
|
|
85
88
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
86
89
|
"@rollup/plugin-terser": "^0.4.4",
|
|
90
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
87
91
|
"@segment/ajv-human-errors": "^2.15.0",
|
|
88
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",
|
|
89
96
|
"ajv": "^8.17.1",
|
|
90
97
|
"ajv-formats": "^3.0.1",
|
|
91
98
|
"axios": "^1.4.0",
|
|
@@ -93,6 +100,7 @@
|
|
|
93
100
|
"babel-jest": "^29.7.0",
|
|
94
101
|
"babel-loader": "^10.0.0",
|
|
95
102
|
"bson-objectid": "^2.0.4",
|
|
103
|
+
"dotenv": "^17.2.3",
|
|
96
104
|
"ejson": "^2.2.3",
|
|
97
105
|
"eslint": "^9.23.0",
|
|
98
106
|
"eslint-plugin-import": "^2.31.0",
|
|
@@ -107,7 +115,11 @@
|
|
|
107
115
|
"pino-pretty": "^13.0.0",
|
|
108
116
|
"rollup": "^4.40.0",
|
|
109
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",
|
|
110
121
|
"typescript": "^5.9.2",
|
|
122
|
+
"vitest": "^4.0.1",
|
|
111
123
|
"webpack": "^5.98.0",
|
|
112
124
|
"webpack-cli": "^6.0.1"
|
|
113
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,61 +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;
|
|
23
34
|
|
|
24
|
-
/**
|
|
25
|
-
|
|
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 };
|
|
26
43
|
|
|
27
44
|
export default class Api {
|
|
45
|
+
private _loggedUser: User | null;
|
|
46
|
+
private _client: ApiClient;
|
|
47
|
+
|
|
28
48
|
/**
|
|
29
49
|
* Authentifie l'utilisateur et retourne une instance d'Api.
|
|
30
|
-
*
|
|
31
|
-
* @param {string} email - L'adresse email.
|
|
32
|
-
* @param {string} password - Le mot de passe.
|
|
33
|
-
* @param {ApiClientOptions|ApiClient} options - Options pour l'ApiClient ou instance existante.
|
|
34
|
-
* @returns {Promise<Api>}
|
|
35
|
-
* @throws {ApiAuthenticationError|ApiClientError|ApiError}
|
|
36
50
|
*/
|
|
37
|
-
static async userLogin(
|
|
51
|
+
static async userLogin(
|
|
52
|
+
email: string,
|
|
53
|
+
password: string,
|
|
54
|
+
options: ApiClientOptions | ApiClient
|
|
55
|
+
): Promise<Api> {
|
|
38
56
|
const userApi = Api.userApi(options);
|
|
39
57
|
return await Api.userApiLogin(userApi, email, password);
|
|
40
58
|
}
|
|
41
59
|
|
|
42
60
|
/**
|
|
43
61
|
* Crée une instance de UserApi avec les options fournies.
|
|
44
|
-
*
|
|
45
|
-
* @param {ApiClientOptions|ApiClient} options - Options pour ApiClient ou instance d'ApiClient.
|
|
46
|
-
* @returns {UserApi}
|
|
47
|
-
* @throws {Error}
|
|
48
62
|
*/
|
|
49
|
-
static userApi(options) {
|
|
63
|
+
static userApi(options: ApiClientOptions | ApiClient): UserApi {
|
|
50
64
|
try {
|
|
51
65
|
const userApi = new UserApi(options);
|
|
52
66
|
return userApi;
|
|
53
67
|
} catch (error) {
|
|
54
|
-
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);
|
|
55
69
|
throw error;
|
|
56
70
|
}
|
|
57
71
|
}
|
|
58
72
|
|
|
59
73
|
/**
|
|
60
74
|
* Connecte un utilisateur avec un UserApi existant.
|
|
61
|
-
*
|
|
62
|
-
* @param {UserApi} userApi - Instance UserApi utilisée pour l'authentification.
|
|
63
|
-
* @param {string} email - Email de l'utilisateur.
|
|
64
|
-
* @param {string} password - Mot de passe de l'utilisateur.
|
|
65
|
-
* @returns {Promise<Api>}
|
|
66
|
-
* @throws {ApiAuthenticationError|ApiClientError|ApiError}
|
|
67
75
|
*/
|
|
68
|
-
static async userApiLogin(
|
|
76
|
+
static async userApiLogin(
|
|
77
|
+
userApi: UserApi,
|
|
78
|
+
email: string,
|
|
79
|
+
password: string
|
|
80
|
+
): Promise<Api> {
|
|
69
81
|
try {
|
|
70
82
|
|
|
71
83
|
if (!userApi) {
|
|
72
|
-
throw new ApiError("userApi is not defined");
|
|
84
|
+
throw new ApiError("userApi is not defined", 500);
|
|
73
85
|
}
|
|
74
86
|
if(!userApi.client) {
|
|
75
|
-
throw new ApiError("userApi.client is not defined");
|
|
87
|
+
throw new ApiError("userApi.client is not defined", 500);
|
|
76
88
|
}
|
|
77
89
|
|
|
78
90
|
if(userApi.client.isConnected) {
|
|
@@ -82,7 +94,7 @@ export default class Api {
|
|
|
82
94
|
return new Api(loggedUser, userApi.client);
|
|
83
95
|
} else {
|
|
84
96
|
if (!email || !password) {
|
|
85
|
-
throw new ApiError("email and password are required");
|
|
97
|
+
throw new ApiError("email and password are required", 400);
|
|
86
98
|
}
|
|
87
99
|
const loggedUser = await userApi.login(email, password);
|
|
88
100
|
return new Api(loggedUser, userApi.client);
|
|
@@ -100,24 +112,18 @@ export default class Api {
|
|
|
100
112
|
|
|
101
113
|
/**
|
|
102
114
|
* Construit une instance d'Api.
|
|
103
|
-
*
|
|
104
|
-
* @param {User|null} loggedUser - L'utilisateur connecté ou null.
|
|
105
|
-
* @param {ApiClient} client - L'instance d'ApiClient.
|
|
106
115
|
*/
|
|
107
|
-
constructor(loggedUser, client) {
|
|
116
|
+
constructor(loggedUser: User | null, client: ApiClient) {
|
|
108
117
|
this._loggedUser = loggedUser;
|
|
109
118
|
this._client = client;
|
|
110
119
|
}
|
|
111
120
|
|
|
112
121
|
/**
|
|
113
122
|
* Retourne l'utilisateur connecté.
|
|
114
|
-
*
|
|
115
|
-
* @returns {Promise<User>} L'utilisateur connecté.
|
|
116
|
-
* @throws {ApiAuthenticationError} Si l'utilisateur n'est pas authentifié.
|
|
117
123
|
*/
|
|
118
|
-
async me() {
|
|
124
|
+
async me(): Promise<User> {
|
|
119
125
|
if (!this._loggedUser) {
|
|
120
|
-
throw new ApiAuthenticationError("Accès refusé : utilisateur non authentifié.");
|
|
126
|
+
throw new ApiAuthenticationError("Accès refusé : utilisateur non authentifié.", 401);
|
|
121
127
|
}
|
|
122
128
|
await this._loggedUser.get();
|
|
123
129
|
return this._loggedUser;
|
|
@@ -125,22 +131,18 @@ export default class Api {
|
|
|
125
131
|
|
|
126
132
|
/**
|
|
127
133
|
* Crée une instance User (autre que le connecté).
|
|
128
|
-
*
|
|
129
|
-
* @param {{id?: string, slug?: string, [k: string]: any}} userData
|
|
130
|
-
* @returns {Promise<User>}
|
|
131
|
-
* @throws {Error}
|
|
132
134
|
*/
|
|
133
|
-
async user(userData) {
|
|
135
|
+
async user(userData: EntityData): Promise<User> {
|
|
134
136
|
try {
|
|
135
137
|
if (!userData.id && !userData.slug) {
|
|
136
|
-
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 });
|
|
137
139
|
} else {
|
|
138
|
-
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 });
|
|
139
141
|
await user.get();
|
|
140
142
|
return user;
|
|
141
143
|
}
|
|
142
144
|
} catch (error) {
|
|
143
|
-
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);
|
|
144
146
|
throw error;
|
|
145
147
|
}
|
|
146
148
|
}
|
|
@@ -148,121 +150,109 @@ export default class Api {
|
|
|
148
150
|
|
|
149
151
|
/**
|
|
150
152
|
* Creates an Organization object and optionally retrieves its profile.
|
|
151
|
-
*
|
|
152
|
-
* @param {{id?: string, slug?: string, [k: string]: any}} organizationData - The data required to initialize the Organization object.
|
|
153
|
-
* @returns {Promise<Organization>} A promise that resolves to the created Organization object.
|
|
154
|
-
* @throws {Error} Throws an error if the organization creation or profile retrieval fails.
|
|
155
153
|
*/
|
|
156
|
-
async organization(organizationData) {
|
|
154
|
+
async organization(organizationData: EntityData): Promise<Organization> {
|
|
157
155
|
try {
|
|
158
|
-
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 });
|
|
159
157
|
if (!organizationData.id && !organizationData.slug) {
|
|
160
158
|
throw new Error("Vous devez fournir un id ou un slug pour créer une instance Organization.");
|
|
161
159
|
}
|
|
162
160
|
await organization.get();
|
|
163
161
|
return organization;
|
|
164
162
|
} catch (error) {
|
|
165
|
-
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);
|
|
166
164
|
throw error;
|
|
167
165
|
}
|
|
168
166
|
}
|
|
169
167
|
|
|
170
168
|
/**
|
|
171
169
|
* Creates a new Project instance and optionally retrieves its profile.
|
|
172
|
-
*
|
|
173
|
-
* @param {{id?: string, slug?: string, [k: string]: any}} projectData - The data used to initialize the Project instance.
|
|
174
|
-
* @returns {Promise<Project>} A promise that resolves to the created Project instance.
|
|
175
|
-
* @throws {Error} If an error occurs during project creation or profile retrieval.
|
|
176
170
|
*/
|
|
177
|
-
async project(projectData) {
|
|
171
|
+
async project(projectData: EntityData): Promise<Project> {
|
|
178
172
|
try {
|
|
179
|
-
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 });
|
|
180
174
|
if (!projectData.id && !projectData.slug) {
|
|
181
175
|
throw new Error("Vous devez fournir un id ou un slug pour créer une instance Project.");
|
|
182
176
|
}
|
|
183
177
|
await project.get();
|
|
184
178
|
return project;
|
|
185
179
|
} catch (error) {
|
|
186
|
-
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);
|
|
187
181
|
throw error;
|
|
188
182
|
}
|
|
189
183
|
}
|
|
190
184
|
|
|
191
185
|
/**
|
|
192
186
|
* Creates a new Event instance and optionally retrieves its profile.
|
|
193
|
-
*
|
|
194
|
-
* @param {{id?: string, slug?: string, [k: string]: any}} eventData
|
|
195
|
-
* @returns {Promise<Event>}
|
|
196
|
-
* @throws {Error}
|
|
197
187
|
*/
|
|
198
|
-
async event(eventData) {
|
|
188
|
+
async event(eventData: EntityData): Promise<Event> {
|
|
199
189
|
try {
|
|
200
|
-
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 });
|
|
201
191
|
if (!eventData.id && !eventData.slug) {
|
|
202
192
|
throw new Error("Vous devez fournir un id ou un slug pour créer une instance Event.");
|
|
203
193
|
}
|
|
204
194
|
await event.get();
|
|
205
195
|
return event;
|
|
206
196
|
} catch (error) {
|
|
207
|
-
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);
|
|
208
198
|
throw error;
|
|
209
199
|
}
|
|
210
200
|
}
|
|
211
201
|
|
|
212
202
|
/**
|
|
213
203
|
* Retourne une entité à partir d'un slug.
|
|
214
|
-
*
|
|
215
|
-
* @param {string} slug
|
|
216
|
-
* @returns {Promise<User|Organization|Project|Event|Poi|Badge|News>}
|
|
217
|
-
* @throws {ApiResponseError|ApiClientError|ApiError}
|
|
218
204
|
*/
|
|
219
|
-
async entitySlug(slug) {
|
|
220
|
-
if (slug) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if(data?.contextId && data?.contextType) {
|
|
228
|
-
const entity = createFromCollection(data?.contextType, this._client, { id: data?.contextId });
|
|
229
|
-
await entity.get();
|
|
230
|
-
return entity;
|
|
231
|
-
} else {
|
|
232
|
-
throw new ApiResponseError(`Le slug ${slug} n'est pas valide`, 200, data);
|
|
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
|
|
233
213
|
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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);
|
|
239
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;
|
|
240
232
|
}
|
|
241
233
|
}
|
|
234
|
+
|
|
242
235
|
}
|
|
243
236
|
|
|
244
237
|
/**
|
|
245
238
|
* Retourne l'instance d'ApiClient.
|
|
246
|
-
* @returns {ApiClient}
|
|
247
239
|
*/
|
|
248
|
-
get client() {
|
|
240
|
+
get client(): ApiClient {
|
|
249
241
|
return this._client;
|
|
250
242
|
}
|
|
251
243
|
|
|
252
244
|
/**
|
|
253
245
|
* Retourne l'instance d'EndpointApi.
|
|
254
|
-
* @returns {EndpointApi}
|
|
255
246
|
*/
|
|
256
|
-
get endpointApi() {
|
|
247
|
+
get endpointApi(): EndpointApi {
|
|
257
248
|
return new EndpointApi(this._client);
|
|
258
249
|
}
|
|
259
250
|
|
|
260
251
|
/**
|
|
261
252
|
* Déconnecte l'utilisateur et réinitialise la session.
|
|
262
|
-
* @returns {void}
|
|
263
253
|
*/
|
|
264
|
-
logout
|
|
265
|
-
this.
|
|
254
|
+
logout(): void {
|
|
255
|
+
this._loggedUser = null;
|
|
266
256
|
this._client.resetSession();
|
|
267
257
|
this._client._logger.info("UserApi: User logged out");
|
|
268
258
|
}
|