@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.
Files changed (179) hide show
  1. package/dist/401.cocolight-api-client.browser.js +1 -0
  2. package/dist/401.cocolight-api-client.cjs +1 -0
  3. package/dist/401.cocolight-api-client.mjs.js +1 -0
  4. package/dist/588.cocolight-api-client.browser.js +1 -0
  5. package/dist/588.cocolight-api-client.cjs +1 -0
  6. package/dist/588.cocolight-api-client.mjs.js +1 -0
  7. package/dist/593.cocolight-api-client.browser.js +1 -0
  8. package/dist/593.cocolight-api-client.cjs +1 -0
  9. package/dist/593.cocolight-api-client.mjs.js +1 -0
  10. package/dist/839.cocolight-api-client.browser.js +1 -0
  11. package/dist/839.cocolight-api-client.cjs +1 -0
  12. package/dist/839.cocolight-api-client.mjs.js +1 -0
  13. package/dist/cocolight-api-client.browser.js +3 -3
  14. package/dist/cocolight-api-client.cjs +1 -1
  15. package/dist/cocolight-api-client.mjs.js +1 -1
  16. package/dist/cocolight-api-client.vite.mjs.js +1 -1
  17. package/dist/cocolight-api-client.vite.mjs.js.map +1 -1
  18. package/package.json +29 -17
  19. package/src/{Api.js → Api.ts} +85 -95
  20. package/src/{ApiClient.js → ApiClient.ts} +436 -247
  21. package/src/EJSONType.ts +103 -0
  22. package/src/api/{Badge.js → Badge.ts} +56 -45
  23. package/src/api/BaseEntity.ts +3890 -0
  24. package/src/api/Comment.ts +200 -0
  25. package/src/api/{EndpointApi.js → EndpointApi.ts} +363 -297
  26. package/src/api/EndpointApi.types.ts +4609 -0
  27. package/src/api/EntityRegistry.ts +203 -0
  28. package/src/api/Event.ts +332 -0
  29. package/src/api/News.ts +331 -0
  30. package/src/api/{Organization.js → Organization.ts} +155 -119
  31. package/src/api/{Poi.js → Poi.ts} +68 -60
  32. package/src/api/{Project.js → Project.ts} +150 -127
  33. package/src/api/{User.js → User.ts} +321 -256
  34. package/src/api/UserApi.ts +148 -0
  35. package/src/api/serverDataType/Comment.ts +88 -0
  36. package/src/api/serverDataType/Event.ts +80 -0
  37. package/src/api/serverDataType/News.ts +138 -0
  38. package/src/api/serverDataType/Organization.ts +80 -0
  39. package/src/api/serverDataType/Project.ts +71 -0
  40. package/src/api/serverDataType/User.ts +103 -0
  41. package/src/api/serverDataType/common.ts +80 -0
  42. package/src/endpoints.module.ts +2621 -0
  43. package/src/error.ts +86 -0
  44. package/src/index.ts +86 -0
  45. package/src/mixin/UserMixin.ts +4 -0
  46. package/src/types/api-responses.ts +217 -0
  47. package/src/types/entities.ts +22 -0
  48. package/src/types/error-guards.ts +230 -0
  49. package/src/types/index.ts +39 -0
  50. package/src/types/payloads.ts +21 -0
  51. package/src/types/transforms.ts +110 -0
  52. package/src/utils/{FileOfflineStorageStrategy.node.js → FileOfflineStorageStrategy.node.ts} +15 -12
  53. package/src/utils/{FileStorageStrategy.node.js → FileStorageStrategy.node.ts} +16 -39
  54. package/src/utils/MultiServerFileStorageStrategy.node.ts +67 -0
  55. package/src/utils/MultiServerTokenStorageStrategy.ts +139 -0
  56. package/src/utils/{OfflineClientManager.js → OfflineClientManager.ts} +82 -86
  57. package/src/utils/OfflineQueueStorageStrategy.ts +47 -0
  58. package/src/utils/TokenStorage.ts +77 -0
  59. package/src/utils/compat.ts +12 -0
  60. package/src/utils/createDefaultMultiServerTokenStorageStrategy.ts +35 -0
  61. package/src/utils/{createDefaultOfflineStrategy.js → createDefaultOfflineStrategy.ts} +8 -3
  62. package/src/utils/createDefaultTokenStorageStrategy.ts +33 -0
  63. package/src/utils/{reactive.js → reactive.ts} +49 -40
  64. package/src/utils/stream-utils.node.ts +12 -0
  65. package/types/Api.d.ts +38 -82
  66. package/types/Api.d.ts.map +1 -0
  67. package/types/ApiClient.d.ts +244 -184
  68. package/types/ApiClient.d.ts.map +1 -0
  69. package/types/EJSONType.d.ts +48 -22
  70. package/types/EJSONType.d.ts.map +1 -0
  71. package/types/api/Badge.d.ts +20 -20
  72. package/types/api/Badge.d.ts.map +1 -0
  73. package/types/api/BaseEntity.d.ts +751 -446
  74. package/types/api/BaseEntity.d.ts.map +1 -0
  75. package/types/api/Comment.d.ts +36 -0
  76. package/types/api/EndpointApi.d.ts +347 -295
  77. package/types/api/EndpointApi.d.ts.map +1 -0
  78. package/types/api/EndpointApi.types.d.ts +3914 -4133
  79. package/types/api/EntityRegistry.d.ts +18 -16
  80. package/types/api/EntityRegistry.d.ts.map +1 -0
  81. package/types/api/Event.d.ts +119 -35
  82. package/types/api/Event.d.ts.map +1 -0
  83. package/types/api/News.d.ts +52 -20
  84. package/types/api/News.d.ts.map +1 -0
  85. package/types/api/Organization.d.ts +165 -49
  86. package/types/api/Organization.d.ts.map +1 -0
  87. package/types/api/Poi.d.ts +51 -22
  88. package/types/api/Poi.d.ts.map +1 -0
  89. package/types/api/Project.d.ts +151 -52
  90. package/types/api/Project.d.ts.map +1 -0
  91. package/types/api/User.d.ts +222 -93
  92. package/types/api/User.d.ts.map +1 -0
  93. package/types/api/UserApi.d.ts +60 -9
  94. package/types/api/UserApi.d.ts.map +1 -0
  95. package/types/api/serverDataType/Comment.d.ts +83 -0
  96. package/types/api/serverDataType/Event.d.ts +67 -0
  97. package/types/api/serverDataType/News.d.ts +130 -0
  98. package/types/api/serverDataType/Organization.d.ts +65 -0
  99. package/types/api/serverDataType/Organization.d.ts.map +1 -0
  100. package/types/api/serverDataType/Project.d.ts +58 -0
  101. package/types/api/serverDataType/Project.d.ts.map +1 -0
  102. package/types/api/serverDataType/User.d.ts +86 -0
  103. package/types/api/serverDataType/User.d.ts.map +1 -0
  104. package/types/api/serverDataType/common.d.ts +71 -0
  105. package/types/api/serverDataType/common.d.ts.map +1 -0
  106. package/types/endpoints.module.d.ts +6922 -1215
  107. package/types/endpoints.module.d.ts.map +1 -0
  108. package/types/error.d.ts +25 -51
  109. package/types/error.d.ts.map +1 -0
  110. package/types/index.d.ts +55 -48
  111. package/types/index.d.ts.map +1 -0
  112. package/types/mixin/UserMixin.d.ts +1 -1
  113. package/types/mixin/UserMixin.d.ts.map +1 -0
  114. package/types/types/api-responses.d.ts +190 -0
  115. package/types/types/api-responses.d.ts.map +1 -0
  116. package/types/types/entities.d.ts +17 -0
  117. package/types/types/entities.d.ts.map +1 -0
  118. package/types/types/error-guards.d.ts +99 -0
  119. package/types/types/error-guards.d.ts.map +1 -0
  120. package/types/types/index.d.ts +7 -0
  121. package/types/types/payloads.d.ts +17 -0
  122. package/types/types/payloads.d.ts.map +1 -0
  123. package/types/types/transforms.d.ts +79 -0
  124. package/types/types/transforms.d.ts.map +1 -0
  125. package/types/utils/FileOfflineStorageStrategy.node.d.ts +10 -9
  126. package/types/utils/FileOfflineStorageStrategy.node.d.ts.map +1 -0
  127. package/types/utils/FileStorageStrategy.node.d.ts +9 -20
  128. package/types/utils/FileStorageStrategy.node.d.ts.map +1 -0
  129. package/types/utils/MultiServerFileStorageStrategy.node.d.ts +13 -18
  130. package/types/utils/MultiServerFileStorageStrategy.node.d.ts.map +1 -0
  131. package/types/utils/MultiServerTokenStorageStrategy.d.ts +30 -51
  132. package/types/utils/MultiServerTokenStorageStrategy.d.ts.map +1 -0
  133. package/types/utils/OfflineClientManager.d.ts +52 -88
  134. package/types/utils/OfflineClientManager.d.ts.map +1 -0
  135. package/types/utils/OfflineQueueStorageStrategy.d.ts +12 -9
  136. package/types/utils/OfflineQueueStorageStrategy.d.ts.map +1 -0
  137. package/types/utils/TokenStorage.d.ts +20 -70
  138. package/types/utils/TokenStorage.d.ts.map +1 -0
  139. package/types/utils/compat.d.ts +4 -0
  140. package/types/utils/compat.d.ts.map +1 -0
  141. package/types/utils/createDefaultMultiServerTokenStorageStrategy.d.ts +2 -11
  142. package/types/utils/createDefaultMultiServerTokenStorageStrategy.d.ts.map +1 -0
  143. package/types/utils/createDefaultOfflineStrategy.d.ts +2 -3
  144. package/types/utils/createDefaultOfflineStrategy.d.ts.map +1 -0
  145. package/types/utils/createDefaultTokenStorageStrategy.d.ts +2 -12
  146. package/types/utils/createDefaultTokenStorageStrategy.d.ts.map +1 -0
  147. package/types/utils/reactive.d.ts +10 -16
  148. package/types/utils/reactive.d.ts.map +1 -0
  149. package/types/utils/stream-utils.node.d.ts +3 -2
  150. package/types/utils/stream-utils.node.d.ts.map +1 -0
  151. package/dist/123.cocolight-api-client.browser.js +0 -1
  152. package/dist/123.cocolight-api-client.cjs +0 -1
  153. package/dist/22.cocolight-api-client.mjs.js +0 -1
  154. package/dist/339.cocolight-api-client.mjs.js +0 -1
  155. package/dist/394.cocolight-api-client.browser.js +0 -1
  156. package/dist/394.cocolight-api-client.cjs +0 -1
  157. package/dist/405.cocolight-api-client.browser.js +0 -1
  158. package/dist/405.cocolight-api-client.cjs +0 -1
  159. package/dist/774.cocolight-api-client.mjs.js +0 -1
  160. package/dist/790.cocolight-api-client.mjs.js +0 -1
  161. package/dist/931.cocolight-api-client.browser.js +0 -1
  162. package/dist/931.cocolight-api-client.cjs +0 -1
  163. package/src/EJSONType.js +0 -53
  164. package/src/api/BaseEntity.js +0 -2828
  165. package/src/api/EntityRegistry.js +0 -152
  166. package/src/api/Event.js +0 -226
  167. package/src/api/News.js +0 -244
  168. package/src/api/UserApi.js +0 -81
  169. package/src/endpoints.module.js +0 -5
  170. package/src/error.js +0 -121
  171. package/src/index.js +0 -97
  172. package/src/mixin/UserMixin.js +0 -8
  173. package/src/utils/MultiServerFileStorageStrategy.node.js +0 -87
  174. package/src/utils/MultiServerTokenStorageStrategy.js +0 -188
  175. package/src/utils/OfflineQueueStorageStrategy.js +0 -51
  176. package/src/utils/TokenStorage.js +0 -153
  177. package/src/utils/createDefaultMultiServerTokenStorageStrategy.js +0 -51
  178. package/src/utils/createDefaultTokenStorageStrategy.js +0 -49
  179. 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.54",
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
- "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"
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": "node --experimental-vm-modules ./node_modules/.bin/jest --config jest.config.js",
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": "node exemple/exemple-api.js",
29
- "lint": "eslint ./src ./exemple ./scripts --ext .js",
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:types": "tsc --project tsconfig.types.json",
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
  }
@@ -1,5 +1,6 @@
1
- // Api.js
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
- /** @typedef {import("./ApiClient.js").default} ApiClient */
25
- /** @typedef {import("./ApiClient.js").ApiClientOptions} ApiClientOptions */
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(email, password, options) {
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(userApi, email, password) {
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
- try {
222
- const data = await this.endpointApi.getElementsKey({
223
- pathParams:{
224
- slug: slug
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
- } catch (error) {
235
- if(error instanceof ApiResponseError) {
236
- throw new ApiResponseError(`Impossible de récupérer l'identifiant pour le slug ${slug}`, error.status, error.responseData);
237
- } else {
238
- throw error;
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.loggedUser = null;
254
+ logout(): void {
255
+ this._loggedUser = null;
266
256
  this._client.resetSession();
267
257
  this._client._logger.info("UserApi: User logged out");
268
258
  }