@communecter/cocolight-api-client 1.0.51 → 1.0.54

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 (51) hide show
  1. package/dist/123.cocolight-api-client.browser.js +1 -1
  2. package/dist/123.cocolight-api-client.cjs +1 -1
  3. package/dist/774.cocolight-api-client.mjs.js +1 -1
  4. package/dist/cocolight-api-client.browser.js +1 -1
  5. package/dist/cocolight-api-client.cjs +1 -1
  6. package/dist/cocolight-api-client.mjs.js +1 -1
  7. package/dist/cocolight-api-client.vite.mjs.js +1 -1
  8. package/dist/cocolight-api-client.vite.mjs.js.map +1 -1
  9. package/package.json +15 -9
  10. package/src/Api.js +41 -23
  11. package/src/ApiClient.js +33 -18
  12. package/src/api/EndpointApi.js +95 -95
  13. package/src/error.js +63 -10
  14. package/src/index.js +65 -2
  15. package/src/utils/FileStorageStrategy.node.js +28 -2
  16. package/src/utils/MultiServerFileStorageStrategy.node.js +24 -2
  17. package/src/utils/MultiServerTokenStorageStrategy.js +84 -27
  18. package/src/utils/TokenStorage.js +79 -19
  19. package/src/utils/createDefaultMultiServerTokenStorageStrategy.js +33 -27
  20. package/src/utils/createDefaultTokenStorageStrategy.js +29 -23
  21. package/types/Api.d.ts +131 -0
  22. package/types/ApiClient.d.ts +377 -0
  23. package/types/EJSONType.d.ts +27 -0
  24. package/types/api/Badge.d.ts +24 -0
  25. package/types/api/BaseEntity.d.ts +1017 -0
  26. package/types/api/EndpointApi.d.ts +933 -0
  27. package/types/api/EntityRegistry.d.ts +22 -0
  28. package/types/api/Event.d.ts +38 -0
  29. package/types/api/News.d.ts +45 -0
  30. package/types/api/Organization.d.ts +87 -0
  31. package/types/api/Poi.d.ts +25 -0
  32. package/types/api/Project.d.ts +81 -0
  33. package/types/api/User.d.ts +203 -0
  34. package/types/api/UserApi.d.ts +13 -0
  35. package/types/endpoints.module.d.ts +14852 -0
  36. package/types/error.d.ts +80 -0
  37. package/types/index.d.ts +52 -0
  38. package/types/mixin/UserMixin.d.ts +1 -0
  39. package/types/utils/FileOfflineStorageStrategy.node.d.ts +10 -0
  40. package/types/utils/FileStorageStrategy.node.d.ts +25 -0
  41. package/types/utils/MultiServerFileStorageStrategy.node.d.ts +22 -0
  42. package/types/utils/MultiServerTokenStorageStrategy.d.ts +65 -0
  43. package/types/utils/OfflineClientManager.d.ts +94 -0
  44. package/types/utils/OfflineQueueStorageStrategy.d.ts +13 -0
  45. package/types/utils/TokenStorage.d.ts +76 -0
  46. package/types/utils/createDefaultMultiServerTokenStorageStrategy.d.ts +11 -0
  47. package/types/utils/createDefaultOfflineStrategy.d.ts +3 -0
  48. package/types/utils/createDefaultTokenStorageStrategy.d.ts +12 -0
  49. package/types/utils/reactive.d.ts +60 -0
  50. package/types/utils/stream-utils.node.d.ts +2 -0
  51. /package/{src → types}/api/EndpointApi.types.d.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@communecter/cocolight-api-client",
3
- "version": "1.0.51",
3
+ "version": "1.0.54",
4
4
  "description": "Client Axios simplifié pour l'API cocolight",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,15 +11,17 @@
11
11
  "module": "./dist/cocolight-api-client.vite.mjs.js",
12
12
  "browser": "./dist/cocolight-api-client.browser.js",
13
13
  "exports": {
14
- "import": {
15
- "vite": "./dist/cocolight-api-client.vite.mjs.js",
16
- "browser": "./dist/cocolight-api-client.vite.mjs.js",
17
- "default": "./dist/cocolight-api-client.vite.mjs.js"
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"
18
22
  },
19
- "require": "./dist/cocolight-api-client.cjs",
20
- "default": "./dist/cocolight-api-client.mjs.js"
21
- },
22
23
  "type": "module",
24
+ "types": "./types/index.d.ts",
23
25
  "scripts": {
24
26
  "test": "node --experimental-vm-modules ./node_modules/.bin/jest --config jest.config.js",
25
27
  "exemple": "node exemple/exemple.js",
@@ -31,7 +33,8 @@
31
33
  "build:esm": "webpack --config webpack.config.esm.mjs",
32
34
  "build:esm:rollup": "rollup -c rollup.config.mjs",
33
35
  "verif:esm": "webpack --config webpack.config.esm.mjs --profile --json > stats.json",
34
- "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",
36
+ "build:types": "tsc --project tsconfig.types.json",
37
+ "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
38
  "build:publish": "npm run build && npm publish --access public",
36
39
  "generate:module": "node ./scripts/transform-json-module.js",
37
40
  "generate:module:publish": "node ./scripts/transform-json-module.publish.js",
@@ -54,6 +57,7 @@
54
57
  "files": [
55
58
  "dist/",
56
59
  "src/",
60
+ "types/",
57
61
  "package.json",
58
62
  "README.md"
59
63
  ],
@@ -81,6 +85,7 @@
81
85
  "@rollup/plugin-node-resolve": "^16.0.1",
82
86
  "@rollup/plugin-terser": "^0.4.4",
83
87
  "@segment/ajv-human-errors": "^2.15.0",
88
+ "@types/ejson": "^2.2.2",
84
89
  "ajv": "^8.17.1",
85
90
  "ajv-formats": "^3.0.1",
86
91
  "axios": "^1.4.0",
@@ -102,6 +107,7 @@
102
107
  "pino-pretty": "^13.0.0",
103
108
  "rollup": "^4.40.0",
104
109
  "rollup-plugin-peer-deps-external": "^2.2.4",
110
+ "typescript": "^5.9.2",
105
111
  "webpack": "^5.98.0",
106
112
  "webpack-cli": "^6.0.1"
107
113
  }
package/src/Api.js CHANGED
@@ -21,14 +21,18 @@ registerEntity("EndpointApi", EndpointApi);
21
21
  registerEntity("UserApi", UserApi);
22
22
  registerEntity("News", News);
23
23
 
24
+ /** @typedef {import("./ApiClient.js").default} ApiClient */
25
+ /** @typedef {import("./ApiClient.js").ApiClientOptions} ApiClientOptions */
26
+
24
27
  export default class Api {
25
28
  /**
26
29
  * Authentifie l'utilisateur et retourne une instance d'Api.
27
30
  *
28
31
  * @param {string} email - L'adresse email.
29
32
  * @param {string} password - Le mot de passe.
30
- * @param {function|object} options - Options pour l'ApiClient (baseURL, debug, etc.) ou une instance d'ApiClient.
33
+ * @param {ApiClientOptions|ApiClient} options - Options pour l'ApiClient ou instance existante.
31
34
  * @returns {Promise<Api>}
35
+ * @throws {ApiAuthenticationError|ApiClientError|ApiError}
32
36
  */
33
37
  static async userLogin(email, password, options) {
34
38
  const userApi = Api.userApi(options);
@@ -36,11 +40,11 @@ export default class Api {
36
40
  }
37
41
 
38
42
  /**
39
- * Creates an instance of the UserApi class with the provided options.
43
+ * Crée une instance de UserApi avec les options fournies.
40
44
  *
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.
45
+ * @param {ApiClientOptions|ApiClient} options - Options pour ApiClient ou instance d'ApiClient.
46
+ * @returns {UserApi}
47
+ * @throws {Error}
44
48
  */
45
49
  static userApi(options) {
46
50
  try {
@@ -53,16 +57,14 @@ export default class Api {
53
57
  }
54
58
 
55
59
  /**
56
- * Logs in a user using the provided userApi instance, email, and password.
57
- *
58
- * @param {Object} userApi - The API instance used for user authentication.
59
- * @param {string} email - The email address of the user.
60
- * @param {string} password - The password of the user.
61
- * @returns {Promise<Api>} A promise that resolves to an instance of the Api class
62
- * initialized with the logged-in user and the API client.
63
- * @throws {ApiAuthenticationError} If the login fails due to authentication issues.
64
- * @throws {Error} If an unexpected error occurs during the login process.
65
- */
60
+ * 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
+ */
66
68
  static async userApiLogin(userApi, email, password) {
67
69
  try {
68
70
 
@@ -122,11 +124,11 @@ export default class Api {
122
124
  }
123
125
 
124
126
  /**
125
- * Crée une instance User pour un utilisateur donné (autre que le connecté).
127
+ * Crée une instance User (autre que le connecté).
126
128
  *
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.
129
+ * @param {{id?: string, slug?: string, [k: string]: any}} userData
130
+ * @returns {Promise<User>}
131
+ * @throws {Error}
130
132
  */
131
133
  async user(userData) {
132
134
  try {
@@ -147,7 +149,7 @@ export default class Api {
147
149
  /**
148
150
  * Creates an Organization object and optionally retrieves its profile.
149
151
  *
150
- * @param {Object} organizationData - The data required to initialize the Organization object.
152
+ * @param {{id?: string, slug?: string, [k: string]: any}} organizationData - The data required to initialize the Organization object.
151
153
  * @returns {Promise<Organization>} A promise that resolves to the created Organization object.
152
154
  * @throws {Error} Throws an error if the organization creation or profile retrieval fails.
153
155
  */
@@ -168,7 +170,7 @@ export default class Api {
168
170
  /**
169
171
  * Creates a new Project instance and optionally retrieves its profile.
170
172
  *
171
- * @param {Object} projectData - The data used to initialize the Project instance.
173
+ * @param {{id?: string, slug?: string, [k: string]: any}} projectData - The data used to initialize the Project instance.
172
174
  * @returns {Promise<Project>} A promise that resolves to the created Project instance.
173
175
  * @throws {Error} If an error occurs during project creation or profile retrieval.
174
176
  */
@@ -186,6 +188,13 @@ export default class Api {
186
188
  }
187
189
  }
188
190
 
191
+ /**
192
+ * 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
+ */
189
198
  async event(eventData) {
190
199
  try {
191
200
  const event = new Event(this._client, eventData, { EndpointApi, User, Organization, Project, Poi, Badge, News });
@@ -200,6 +209,13 @@ export default class Api {
200
209
  }
201
210
  }
202
211
 
212
+ /**
213
+ * 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
+ */
203
219
  async entitySlug(slug) {
204
220
  if (slug) {
205
221
  try {
@@ -227,7 +243,6 @@ export default class Api {
227
243
 
228
244
  /**
229
245
  * Retourne l'instance d'ApiClient.
230
- *
231
246
  * @returns {ApiClient}
232
247
  */
233
248
  get client() {
@@ -236,13 +251,16 @@ export default class Api {
236
251
 
237
252
  /**
238
253
  * Retourne l'instance d'EndpointApi.
239
- *
240
254
  * @returns {EndpointApi}
241
255
  */
242
256
  get endpointApi() {
243
257
  return new EndpointApi(this._client);
244
258
  }
245
259
 
260
+ /**
261
+ * Déconnecte l'utilisateur et réinitialise la session.
262
+ * @returns {void}
263
+ */
246
264
  logout () {
247
265
  this.loggedUser = null;
248
266
  this._client.resetSession();
package/src/ApiClient.js CHANGED
@@ -15,6 +15,25 @@ import { ApiClientError, ApiResponseError, ApiValidationError, CircuitBreakerErr
15
15
  import { MultiServerTokenStorageStrategy } from "./utils/MultiServerTokenStorageStrategy.js";
16
16
  import { MemoryStorageStrategy } from "./utils/TokenStorage.js";
17
17
 
18
+ /** @typedef {import("./utils/TokenStorage.js").TokenStorageStrategy} TokenStorageStrategy */
19
+ /** @typedef {import("./utils/TokenStorage.js").MemoryStorageStrategy} MemoryStorageStrategy */
20
+ /** @typedef {import("./utils/MultiServerTokenStorageStrategy.js").MultiServerTokenStorageStrategy} MultiServerTokenStorageStrategy */
21
+
22
+ /**
23
+ * @typedef ApiClientOptions
24
+ * @property {string} baseURL
25
+ * @property {string} [accessToken]
26
+ * @property {string} [refreshToken]
27
+ * @property {string} [refreshUrl="/api/cocolight/refreshtoken"]
28
+ * @property {any[]} [endpoints]
29
+ * @property {number} [timeout=30000]
30
+ * @property {boolean} [debug=false]
31
+ * @property {number} [maxRetries=0]
32
+ * @property {number} [circuitBreakerThreshold=5]
33
+ * @property {number} [circuitBreakerResetTime=60000]
34
+ * @property {boolean} [fromJSONValue=true]
35
+ * @property {TokenStorageStrategy|null} [tokenStorageStrategy=null]
36
+ */
18
37
 
19
38
  EJSON.addType("oid", value => {
20
39
  return new MongoID.ObjectID(value);
@@ -39,19 +58,7 @@ EJSON.addType("oid", value => {
39
58
  */
40
59
  export default class ApiClient extends EventEmitter {
41
60
  /**
42
- * @param {Object} options
43
- * @param {string} options.baseURL
44
- * @param {string} [options.accessToken]
45
- * @param {string} [options.refreshToken]
46
- * @param {string} [options.refreshUrl=/api/cocolight/refreshtoken]
47
- * @param {Array} [options.endpoints=endpointsJson.endpoints]
48
- * @param {number} [options.timeout=30000]
49
- * @param {boolean} [options.debug=false]
50
- * @param {number} [options.maxRetries=0] - Nombre de tentatives auto (axios-retry)
51
- * @param {number} [options.circuitBreakerThreshold=5] - Nb d'erreurs avant de bloquer
52
- * @param {number} [options.circuitBreakerResetTime=60000] - Ms avant de reset le breaker
53
- * @param {boolean} [options.fromJSONValue=true] - Si true, les données sont transformées en EJSON
54
- * @param {TokenStorageStrategy} [options.tokenStorageStrategy=null] - Stratégie de stockage des tokens
61
+ * @param {ApiClientOptions} [options]
55
62
  */
56
63
  constructor({
57
64
  baseURL,
@@ -148,15 +155,19 @@ export default class ApiClient extends EventEmitter {
148
155
  this._breakerResetTime = circuitBreakerResetTime;
149
156
  this._breakerErrorCount = 0;
150
157
  this._breakerOpen = false;
158
+ /** @type {string|null} */
151
159
  this._lastBreakerOpenTime = null;
152
160
 
161
+ /** @type {string|null} */
153
162
  this._accessToken = null;
163
+ /** @type {string|null} */
154
164
  this._refreshToken = null;
155
165
 
156
166
  if (tokenStorageStrategy instanceof MultiServerTokenStorageStrategy) {
157
167
  tokenStorageStrategy.use(this._baseURL);
158
168
  }
159
169
 
170
+ /** @type {TokenStorageStrategy} */
160
171
  this._tokenStorage = tokenStorageStrategy || new MemoryStorageStrategy();
161
172
 
162
173
  if (
@@ -235,12 +246,16 @@ export default class ApiClient extends EventEmitter {
235
246
  /**
236
247
  * Sets the access token for the API client and updates the authorization header.
237
248
  *
238
- * @param {string} token - The access token to be set.
249
+ * @param {string|null} token - The access token to be set.
239
250
  */
240
251
  setToken(token) {
241
252
  this._accessToken = token;
242
253
  this._tokenStorage.setAccessToken(token);
243
- this._client.defaults.headers.common["Authorization"] = "Bearer " + token;
254
+ if (token) {
255
+ this._client.defaults.headers.common["Authorization"] = "Bearer " + token;
256
+ } else {
257
+ delete this._client.defaults.headers.common["Authorization"];
258
+ }
244
259
  // Extrait l'id depuis le token et le stocke si disponible
245
260
  const userId = this._getIdFromToken(token);
246
261
  if (userId) {
@@ -253,7 +268,7 @@ export default class ApiClient extends EventEmitter {
253
268
  /**
254
269
  * Retrieves the current access token.
255
270
  *
256
- * @returns {string} The access token.
271
+ * @returns {string|null} The access token.
257
272
  */
258
273
  getToken() {
259
274
  return this._accessToken;
@@ -262,7 +277,7 @@ export default class ApiClient extends EventEmitter {
262
277
  /**
263
278
  * Sets the refresh token for the API client.
264
279
  *
265
- * @param {string} refreshToken - The refresh token to be set.
280
+ * @param {string|null} token - The refresh token to be set.
266
281
  */
267
282
  setRefreshToken(token) {
268
283
  this._refreshToken = token;
@@ -281,7 +296,7 @@ export default class ApiClient extends EventEmitter {
281
296
  /**
282
297
  * Retrieves the current refresh token.
283
298
  *
284
- * @returns {string} The refresh token.
299
+ * @returns {string|null} The refresh token.
285
300
  */
286
301
  getRefreshToken() {
287
302
  return this._refreshToken;