@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/src/error.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type générique pour les détails d'erreur bruts
|
|
3
|
+
*/
|
|
4
|
+
export type GenericErrorDetails = Record<string, any>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Classe de base pour toutes les erreurs de l'API.
|
|
8
|
+
*/
|
|
9
|
+
export class ApiError extends Error {
|
|
10
|
+
public override readonly name: string = "ApiError";
|
|
11
|
+
public readonly status: number;
|
|
12
|
+
public readonly details?: GenericErrorDetails;
|
|
13
|
+
|
|
14
|
+
constructor(message: string, status: number, details?: GenericErrorDetails) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.status = status;
|
|
17
|
+
this.details = details;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Erreur levée par le client API lorsqu'un appel échoue (réseau, config, etc.).
|
|
23
|
+
*/
|
|
24
|
+
export class ApiClientError extends ApiError {
|
|
25
|
+
public override readonly name: string = "ApiClientError";
|
|
26
|
+
|
|
27
|
+
constructor(message: string, status: number, details?: GenericErrorDetails) {
|
|
28
|
+
super(message, status, details);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Erreur de validation AJV (schemas de requête/réponse).
|
|
34
|
+
*/
|
|
35
|
+
export class ApiValidationError extends Error {
|
|
36
|
+
public override readonly name: string = "ApiValidationError";
|
|
37
|
+
public readonly status: number;
|
|
38
|
+
public readonly messages: string[];
|
|
39
|
+
public readonly details: any;
|
|
40
|
+
|
|
41
|
+
constructor(message: string, status: number, messages: string[], details: any = null) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.status = status;
|
|
44
|
+
this.messages = messages;
|
|
45
|
+
this.details = details;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Levée lorsqu'on bloque l'accès à l'API (circuit breaker).
|
|
51
|
+
*/
|
|
52
|
+
export class CircuitBreakerError extends ApiError {
|
|
53
|
+
public override readonly name: string = "CircuitBreakerError";
|
|
54
|
+
|
|
55
|
+
constructor(message: string) {
|
|
56
|
+
super(message, 503);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Erreur "métier" retour de l'API (ex: result=false) même si HTTP=200.
|
|
62
|
+
*/
|
|
63
|
+
export class ApiResponseError extends Error {
|
|
64
|
+
public override readonly name: string = "ApiResponseError";
|
|
65
|
+
public readonly status: number;
|
|
66
|
+
public readonly responseData: object;
|
|
67
|
+
public readonly details: any;
|
|
68
|
+
|
|
69
|
+
constructor(message: string, status: number, responseData: object, details: any = null) {
|
|
70
|
+
super(message);
|
|
71
|
+
this.status = status;
|
|
72
|
+
this.responseData = responseData;
|
|
73
|
+
this.details = details;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Erreur d'authentification (ex: 401/403).
|
|
79
|
+
*/
|
|
80
|
+
export class ApiAuthenticationError extends ApiError {
|
|
81
|
+
public override readonly name: string = "ApiAuthenticationError";
|
|
82
|
+
|
|
83
|
+
constructor(message: string, status: number, details?: GenericErrorDetails) {
|
|
84
|
+
super(message, status, details);
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { fromEntityJSON } from "./api/EntityRegistry.js";
|
|
2
|
+
import Api from "./Api.js";
|
|
3
|
+
import ApiClient from "./ApiClient.js";
|
|
4
|
+
import * as error from "./error.js";
|
|
5
|
+
import { createDefaultMultiServerTokenStorageStrategy } from "./utils/createDefaultMultiServerTokenStorageStrategy.js";
|
|
6
|
+
import { createDefaultTokenStorageStrategy } from "./utils/createDefaultTokenStorageStrategy.js";
|
|
7
|
+
import { MultiServerTokenStorageStrategy } from "./utils/MultiServerTokenStorageStrategy.js";
|
|
8
|
+
import OfflineClientManager from "./utils/OfflineClientManager.js";
|
|
9
|
+
import * as reactive from "./utils/reactive.js";
|
|
10
|
+
import { TokenStorageStrategy } from "./utils/TokenStorage.js";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const cocolightApiClient = {
|
|
14
|
+
ApiClient,
|
|
15
|
+
Api,
|
|
16
|
+
error,
|
|
17
|
+
reactive,
|
|
18
|
+
effect: reactive.effect,
|
|
19
|
+
batch: reactive.batch,
|
|
20
|
+
isSignal: reactive.isSignal,
|
|
21
|
+
reactiveObject: reactive.reactive,
|
|
22
|
+
isReactive: reactive.isReactive,
|
|
23
|
+
computed: reactive.computed,
|
|
24
|
+
watch: reactive.watch,
|
|
25
|
+
getSignals: reactive.getSignals,
|
|
26
|
+
subscribeTo: reactive.subscribeTo,
|
|
27
|
+
tokenStorageStrategy: {
|
|
28
|
+
createDefaultTokenStorageStrategy,
|
|
29
|
+
TokenStorageStrategy,
|
|
30
|
+
createDefaultMultiServerTokenStorageStrategy,
|
|
31
|
+
MultiServerTokenStorageStrategy
|
|
32
|
+
},
|
|
33
|
+
helper: {
|
|
34
|
+
fromEntityJSON
|
|
35
|
+
},
|
|
36
|
+
OfflineClientManager
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default cocolightApiClient;
|
|
40
|
+
|
|
41
|
+
// Export des types pour faciliter l'utilisation
|
|
42
|
+
|
|
43
|
+
// Classes principales
|
|
44
|
+
export type { default as Api } from "./Api.js";
|
|
45
|
+
export type { default as ApiClient } from "./ApiClient.js";
|
|
46
|
+
export type { default as EndpointApi } from "./api/EndpointApi.js";
|
|
47
|
+
export type { UserApi } from "./api/UserApi.js";
|
|
48
|
+
|
|
49
|
+
// Types ApiClient (configuration et résultats d'appels)
|
|
50
|
+
export type {
|
|
51
|
+
ApiClientOptions,
|
|
52
|
+
CallEndpointData,
|
|
53
|
+
CallEndpointResult,
|
|
54
|
+
CallEndpointResponse,
|
|
55
|
+
ApiClientOfflineEnqueueResult
|
|
56
|
+
} from "./ApiClient.js";
|
|
57
|
+
|
|
58
|
+
// Stratégies de stockage
|
|
59
|
+
export type { TokenStorageStrategy } from "./utils/TokenStorage.js";
|
|
60
|
+
export type { MultiServerTokenStorageStrategy } from "./utils/MultiServerTokenStorageStrategy.js";
|
|
61
|
+
export type { default as OfflineClientManager } from "./utils/OfflineClientManager.js";
|
|
62
|
+
export type { OfflineAction } from "./utils/OfflineClientManager.js";
|
|
63
|
+
|
|
64
|
+
// Classes d'entités
|
|
65
|
+
export type { User } from "./api/User.js";
|
|
66
|
+
export type { Organization } from "./api/Organization.js";
|
|
67
|
+
export type { Project } from "./api/Project.js";
|
|
68
|
+
export type { Event } from "./api/Event.js";
|
|
69
|
+
export type { Poi } from "./api/Poi.js";
|
|
70
|
+
export type { Badge } from "./api/Badge.js";
|
|
71
|
+
export type { News } from "./api/News.js";
|
|
72
|
+
|
|
73
|
+
// Types ServerData (données normalisées reçues du serveur)
|
|
74
|
+
export type * from "./api/serverDataType/User.js";
|
|
75
|
+
export type * from "./api/serverDataType/Organization.js";
|
|
76
|
+
export type * from "./api/serverDataType/Project.js";
|
|
77
|
+
export type * from "./api/serverDataType/common.js";
|
|
78
|
+
|
|
79
|
+
// Types utilitaires
|
|
80
|
+
export type { PaginatorPage } from "./api/BaseEntity.js";
|
|
81
|
+
|
|
82
|
+
// Types de réponses, payloads, entités, et transforms
|
|
83
|
+
export type * from "./types/index.js";
|
|
84
|
+
|
|
85
|
+
// Export de tous les types générés depuis EndpointApi.types.ts
|
|
86
|
+
export type * from "./api/EndpointApi.types.js";
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// types/api-responses.ts - Types pour les réponses API et amélioration du type safety
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-empty-object-type */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
|
+
|
|
5
|
+
import type { CollectionKey } from "../api/EntityRegistry.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Interface de base pour toutes les réponses API
|
|
9
|
+
*/
|
|
10
|
+
export interface BaseApiResponse {
|
|
11
|
+
success?: boolean;
|
|
12
|
+
message?: string;
|
|
13
|
+
timestamp?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Réponse avec données
|
|
18
|
+
*/
|
|
19
|
+
export interface ApiDataResponse<T = any> extends BaseApiResponse {
|
|
20
|
+
data: T;
|
|
21
|
+
map?: {
|
|
22
|
+
id?: string;
|
|
23
|
+
slug?: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Réponse d'erreur API
|
|
29
|
+
*/
|
|
30
|
+
export interface ApiErrorResponse extends BaseApiResponse {
|
|
31
|
+
error: {
|
|
32
|
+
code: number;
|
|
33
|
+
message: string;
|
|
34
|
+
details?: any;
|
|
35
|
+
stack?: string;
|
|
36
|
+
};
|
|
37
|
+
success: false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Réponse paginée
|
|
42
|
+
*/
|
|
43
|
+
export interface PaginatedApiResponse<T = any> extends BaseApiResponse {
|
|
44
|
+
data: {
|
|
45
|
+
count: {
|
|
46
|
+
total: number;
|
|
47
|
+
};
|
|
48
|
+
results: T[];
|
|
49
|
+
pageIndex: number;
|
|
50
|
+
pageNumber: number;
|
|
51
|
+
hasNext: boolean;
|
|
52
|
+
hasPrev: boolean;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Réponse pour les opérations de connexion/déconnexion
|
|
58
|
+
*/
|
|
59
|
+
export interface ConnectionResponse extends BaseApiResponse {
|
|
60
|
+
connection: {
|
|
61
|
+
established: boolean;
|
|
62
|
+
type: string;
|
|
63
|
+
childId: string;
|
|
64
|
+
parentId: string;
|
|
65
|
+
date: number;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Réponse pour l'authentification
|
|
71
|
+
*/
|
|
72
|
+
export interface AuthResponse extends BaseApiResponse {
|
|
73
|
+
user?: {
|
|
74
|
+
id: string;
|
|
75
|
+
username: string;
|
|
76
|
+
email: string;
|
|
77
|
+
roles: any;
|
|
78
|
+
};
|
|
79
|
+
accessToken?: string;
|
|
80
|
+
refreshToken?: string;
|
|
81
|
+
expiresIn?: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Réponse pour l'upload de fichiers
|
|
86
|
+
*/
|
|
87
|
+
export interface UploadResponse extends BaseApiResponse {
|
|
88
|
+
file: {
|
|
89
|
+
id: string;
|
|
90
|
+
filename: string;
|
|
91
|
+
originalName: string;
|
|
92
|
+
mimetype: string;
|
|
93
|
+
size: number;
|
|
94
|
+
url: string;
|
|
95
|
+
thumbnailUrl?: string;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Réponse pour les opérations CRUD simples
|
|
101
|
+
*/
|
|
102
|
+
export interface CrudResponse extends BaseApiResponse {
|
|
103
|
+
operation: "create" | "read" | "update" | "delete";
|
|
104
|
+
entityType: string;
|
|
105
|
+
entityId: string;
|
|
106
|
+
affected: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Types pour les endpoints spécifiques
|
|
111
|
+
*/
|
|
112
|
+
export interface GetElementsKeyResponse extends BaseApiResponse {
|
|
113
|
+
contextId?: string;
|
|
114
|
+
contextType?: CollectionKey;
|
|
115
|
+
[key: string]: unknown;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Union de tous les types de réponse
|
|
120
|
+
*/
|
|
121
|
+
export type ApiResponse =
|
|
122
|
+
| BaseApiResponse
|
|
123
|
+
| ApiDataResponse
|
|
124
|
+
| ApiErrorResponse
|
|
125
|
+
| PaginatedApiResponse
|
|
126
|
+
| ConnectionResponse
|
|
127
|
+
| AuthResponse
|
|
128
|
+
| UploadResponse
|
|
129
|
+
| CrudResponse
|
|
130
|
+
| GetElementsKeyResponse;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Types pour les statuts HTTP
|
|
134
|
+
*/
|
|
135
|
+
export type HttpStatusCode =
|
|
136
|
+
| 200 | 201 | 202 | 204 // Success
|
|
137
|
+
| 400 | 401 | 403 | 404 | 409 | 422 // Client errors
|
|
138
|
+
| 500 | 501 | 502 | 503 | 504; // Server errors
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Interface pour les détails d'erreur API
|
|
142
|
+
*/
|
|
143
|
+
export interface ApiErrorDetails {
|
|
144
|
+
code: HttpStatusCode;
|
|
145
|
+
message: string;
|
|
146
|
+
details?: any;
|
|
147
|
+
validationErrors?: Array<{
|
|
148
|
+
field: string;
|
|
149
|
+
message: string;
|
|
150
|
+
value?: any;
|
|
151
|
+
}>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Types pour les réponses d'endpoint spécifiques
|
|
156
|
+
*/
|
|
157
|
+
export namespace EndpointResponses {
|
|
158
|
+
export interface AddUser extends ApiDataResponse<{
|
|
159
|
+
id: string;
|
|
160
|
+
username: string;
|
|
161
|
+
email: string;
|
|
162
|
+
created: number;
|
|
163
|
+
}> {}
|
|
164
|
+
|
|
165
|
+
export interface AddOrganization extends ApiDataResponse<{
|
|
166
|
+
id: string;
|
|
167
|
+
name: string;
|
|
168
|
+
created: number;
|
|
169
|
+
}> {}
|
|
170
|
+
|
|
171
|
+
export interface AddProject extends ApiDataResponse<{
|
|
172
|
+
id: string;
|
|
173
|
+
name: string;
|
|
174
|
+
created: number;
|
|
175
|
+
}> {}
|
|
176
|
+
|
|
177
|
+
export interface AddEvent extends ApiDataResponse<{
|
|
178
|
+
id: string;
|
|
179
|
+
name: string;
|
|
180
|
+
startDate?: number;
|
|
181
|
+
created: number;
|
|
182
|
+
}> {}
|
|
183
|
+
|
|
184
|
+
export interface AddPoi extends ApiDataResponse<{
|
|
185
|
+
id: string;
|
|
186
|
+
name: string;
|
|
187
|
+
created: number;
|
|
188
|
+
}> {}
|
|
189
|
+
|
|
190
|
+
export interface AddNews extends ApiDataResponse<{
|
|
191
|
+
id: string;
|
|
192
|
+
title: string;
|
|
193
|
+
publishDate?: number;
|
|
194
|
+
created: number;
|
|
195
|
+
}> {}
|
|
196
|
+
|
|
197
|
+
export interface AddBadges extends ApiDataResponse<{
|
|
198
|
+
id: string;
|
|
199
|
+
name: string;
|
|
200
|
+
created: number;
|
|
201
|
+
}> {}
|
|
202
|
+
|
|
203
|
+
export interface Connect extends ConnectionResponse {}
|
|
204
|
+
export interface Disconnect extends ConnectionResponse {}
|
|
205
|
+
export interface Follow extends ConnectionResponse {}
|
|
206
|
+
export interface LinkValidate extends ConnectionResponse {}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Type helper pour extraire le type de données d'une réponse API
|
|
211
|
+
*/
|
|
212
|
+
export type ExtractApiData<T> = T extends ApiDataResponse<infer U> ? U : never;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Type helper pour les réponses qui peuvent être soit des données soit des erreurs
|
|
216
|
+
*/
|
|
217
|
+
export type ApiResult<T> = ApiDataResponse<T> | ApiErrorResponse;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// types/entities.ts - Types partagés pour éviter les dépendances circulaires
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Types de collection possibles
|
|
5
|
+
*/
|
|
6
|
+
export type CollectionType = "citoyens" | "organizations" | "projects" | "events" | "poi" | "news" | "badges" | "comments";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Types pour les références entre entités
|
|
10
|
+
*/
|
|
11
|
+
export interface EntityRef {
|
|
12
|
+
type: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type EntityRefsMap = Record<string, EntityRef>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Types pour les transformations de données
|
|
20
|
+
*/
|
|
21
|
+
export type TransformFunction<TInput = any, TOutput = any> = (value: TInput, fullObject?: any) => TOutput;
|
|
22
|
+
export type TransformsMap = Record<string, TransformFunction | undefined>;
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// types/error-guards.ts - Type guards et utilitaires pour la gestion d'erreurs
|
|
2
|
+
|
|
3
|
+
import { ApiError, ApiClientError, ApiResponseError, ApiValidationError, ApiAuthenticationError, CircuitBreakerError } from "../error.js";
|
|
4
|
+
|
|
5
|
+
import type { ApiErrorDetails, HttpStatusCode } from "./api-responses.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Type guard pour vérifier si une valeur est une Error
|
|
9
|
+
*/
|
|
10
|
+
export function isError(value: unknown): value is Error {
|
|
11
|
+
return value instanceof Error;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type guard pour vérifier si une valeur est une ApiError
|
|
16
|
+
*/
|
|
17
|
+
export function isApiError(value: unknown): value is ApiError {
|
|
18
|
+
return value instanceof ApiError;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Type guard pour vérifier si une valeur est une ApiClientError
|
|
23
|
+
*/
|
|
24
|
+
export function isApiClientError(value: unknown): value is ApiClientError {
|
|
25
|
+
return value instanceof ApiClientError;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Type guard pour vérifier si une valeur est une ApiResponseError
|
|
30
|
+
*/
|
|
31
|
+
export function isApiResponseError(value: unknown): value is ApiResponseError {
|
|
32
|
+
return value instanceof ApiResponseError;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Type guard pour vérifier si une valeur est une ApiValidationError
|
|
37
|
+
*/
|
|
38
|
+
export function isApiValidationError(value: unknown): value is ApiValidationError {
|
|
39
|
+
return value instanceof ApiValidationError;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Type guard pour vérifier si une valeur est une ApiAuthenticationError
|
|
44
|
+
*/
|
|
45
|
+
export function isApiAuthenticationError(value: unknown): value is ApiAuthenticationError {
|
|
46
|
+
return value instanceof ApiAuthenticationError;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Type guard pour vérifier si une valeur est une CircuitBreakerError
|
|
51
|
+
*/
|
|
52
|
+
export function isCircuitBreakerError(value: unknown): value is CircuitBreakerError {
|
|
53
|
+
return value instanceof CircuitBreakerError;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Type guard pour vérifier si une erreur a une propriété response (erreurs axios)
|
|
58
|
+
*/
|
|
59
|
+
export function hasResponse(error: unknown): error is Error & { response: { status: number; data: any } } {
|
|
60
|
+
return isError(error) && "response" in error &&
|
|
61
|
+
typeof (error as any).response === "object" &&
|
|
62
|
+
typeof (error as any).response.status === "number";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Type guard pour vérifier si une erreur a un code de statut HTTP
|
|
67
|
+
*/
|
|
68
|
+
export function hasStatusCode(error: unknown): error is Error & { status: HttpStatusCode } {
|
|
69
|
+
return isError(error) && "status" in error &&
|
|
70
|
+
typeof (error as any).status === "number";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Utilitaire pour extraire le message d'erreur de façon type-safe
|
|
75
|
+
*/
|
|
76
|
+
export function getErrorMessage(error: unknown): string {
|
|
77
|
+
if (isError(error)) {
|
|
78
|
+
return error.message;
|
|
79
|
+
}
|
|
80
|
+
if (typeof error === "string") {
|
|
81
|
+
return error;
|
|
82
|
+
}
|
|
83
|
+
if (error && typeof error === "object" && "message" in error) {
|
|
84
|
+
return String((error as any).message);
|
|
85
|
+
}
|
|
86
|
+
return "Unknown error";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Utilitaire pour extraire le code de statut d'une erreur
|
|
91
|
+
*/
|
|
92
|
+
export function getErrorStatusCode(error: unknown): HttpStatusCode | undefined {
|
|
93
|
+
if (hasStatusCode(error)) {
|
|
94
|
+
return error.status as HttpStatusCode;
|
|
95
|
+
}
|
|
96
|
+
if (hasResponse(error)) {
|
|
97
|
+
return error.response.status as HttpStatusCode;
|
|
98
|
+
}
|
|
99
|
+
if (isApiError(error)) {
|
|
100
|
+
return error.status as HttpStatusCode;
|
|
101
|
+
}
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Utilitaire pour extraire les détails d'erreur de façon type-safe
|
|
107
|
+
*/
|
|
108
|
+
export function getErrorDetails(error: unknown): ApiErrorDetails | undefined {
|
|
109
|
+
if (isApiError(error)) {
|
|
110
|
+
return {
|
|
111
|
+
code: error.status as HttpStatusCode,
|
|
112
|
+
message: error.message,
|
|
113
|
+
details: (error as any).responseData
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (hasResponse(error)) {
|
|
118
|
+
return {
|
|
119
|
+
code: error.response.status as HttpStatusCode,
|
|
120
|
+
message: getErrorMessage(error),
|
|
121
|
+
details: error.response.data
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const status = getErrorStatusCode(error);
|
|
126
|
+
if (status) {
|
|
127
|
+
return {
|
|
128
|
+
code: status,
|
|
129
|
+
message: getErrorMessage(error)
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Utilitaire pour créer une erreur typée
|
|
138
|
+
*/
|
|
139
|
+
export function createTypedError(message: string, status: HttpStatusCode = 500, details?: any): ApiError {
|
|
140
|
+
return new ApiError(message, status, details);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Wrapper pour les fonctions qui peuvent lever des erreurs
|
|
145
|
+
*/
|
|
146
|
+
export async function safeCall<T>(
|
|
147
|
+
fn: () => Promise<T>,
|
|
148
|
+
fallback?: T
|
|
149
|
+
): Promise<{ data: T; error: null } | { data: T | undefined; error: ApiErrorDetails }> {
|
|
150
|
+
try {
|
|
151
|
+
const data = await fn();
|
|
152
|
+
return { data, error: null };
|
|
153
|
+
} catch (error) {
|
|
154
|
+
const errorDetails = getErrorDetails(error);
|
|
155
|
+
return {
|
|
156
|
+
data: fallback,
|
|
157
|
+
error: errorDetails || {
|
|
158
|
+
code: 500,
|
|
159
|
+
message: getErrorMessage(error)
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Type pour les résultats d'opérations sécurisées
|
|
167
|
+
*/
|
|
168
|
+
export type SafeResult<T> =
|
|
169
|
+
| { success: true; data: T; error: null }
|
|
170
|
+
| { success: false; data: undefined; error: ApiErrorDetails };
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Version améliorée de safeCall avec indicateur de succès
|
|
174
|
+
*/
|
|
175
|
+
export async function trySafe<T>(fn: () => Promise<T>): Promise<SafeResult<T>> {
|
|
176
|
+
try {
|
|
177
|
+
const data = await fn();
|
|
178
|
+
return { success: true, data, error: null };
|
|
179
|
+
} catch (error) {
|
|
180
|
+
const errorDetails = getErrorDetails(error);
|
|
181
|
+
return {
|
|
182
|
+
success: false,
|
|
183
|
+
data: undefined,
|
|
184
|
+
error: errorDetails || {
|
|
185
|
+
code: 500,
|
|
186
|
+
message: getErrorMessage(error)
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Utilitaire pour valider et normaliser les erreurs
|
|
194
|
+
*/
|
|
195
|
+
export function normalizeError(error: unknown): ApiError {
|
|
196
|
+
if (isApiError(error)) {
|
|
197
|
+
return error;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const message = getErrorMessage(error);
|
|
201
|
+
const status = getErrorStatusCode(error) || 500;
|
|
202
|
+
const details = hasResponse(error) ? error.response.data : undefined;
|
|
203
|
+
|
|
204
|
+
return new ApiError(message, status, details);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Type helper pour les fonctions qui peuvent lever des erreurs typées
|
|
209
|
+
*/
|
|
210
|
+
export type ErrorProne<T> = () => Promise<T> | T;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Décorateur de méthode pour capturer et normaliser les erreurs
|
|
214
|
+
*/
|
|
215
|
+
export function catchErrors<T extends any[], R>(
|
|
216
|
+
target: any,
|
|
217
|
+
propertyName: string,
|
|
218
|
+
descriptor: TypedPropertyDescriptor<(...args: T) => Promise<R>>
|
|
219
|
+
) {
|
|
220
|
+
const method = descriptor.value;
|
|
221
|
+
if (!method) return;
|
|
222
|
+
|
|
223
|
+
descriptor.value = async function (...args: T): Promise<R> {
|
|
224
|
+
try {
|
|
225
|
+
return await method.apply(this, args);
|
|
226
|
+
} catch (error) {
|
|
227
|
+
throw normalizeError(error);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// types/index.ts - Point d'entrée central pour tous les types partagés
|
|
2
|
+
|
|
3
|
+
// Entity types
|
|
4
|
+
export type {
|
|
5
|
+
CollectionKey,
|
|
6
|
+
EntityTag
|
|
7
|
+
} from "../api/EntityRegistry.js";
|
|
8
|
+
|
|
9
|
+
export type {
|
|
10
|
+
TransformsMap,
|
|
11
|
+
TransformFunction,
|
|
12
|
+
} from "./entities.js";
|
|
13
|
+
|
|
14
|
+
// Payload types
|
|
15
|
+
export type {
|
|
16
|
+
SocialNetworkPayload,
|
|
17
|
+
} from "./payloads.js";
|
|
18
|
+
|
|
19
|
+
// Transform types
|
|
20
|
+
export type {
|
|
21
|
+
UserTransforms,
|
|
22
|
+
OrganizationTransforms,
|
|
23
|
+
ProjectTransforms,
|
|
24
|
+
EventTransforms
|
|
25
|
+
} from "./transforms.js";
|
|
26
|
+
|
|
27
|
+
// API Response types
|
|
28
|
+
export type {
|
|
29
|
+
BaseApiResponse,
|
|
30
|
+
ApiDataResponse,
|
|
31
|
+
ApiErrorResponse,
|
|
32
|
+
PaginatedApiResponse,
|
|
33
|
+
GetElementsKeyResponse,
|
|
34
|
+
ApiResponse
|
|
35
|
+
} from "./api-responses.js";
|
|
36
|
+
|
|
37
|
+
// API Error types
|
|
38
|
+
export type { ApiErrorDetails } from "./api-responses.js";
|
|
39
|
+
export type { GenericErrorDetails } from "../error.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// types/payloads.ts - Types pour les payloads des méthodes _add et _update
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Types pour les réseaux sociaux
|
|
6
|
+
*/
|
|
7
|
+
export interface SocialNetworkPayload {
|
|
8
|
+
github?: string;
|
|
9
|
+
gitlab?: string;
|
|
10
|
+
facebook?: string;
|
|
11
|
+
twitter?: string;
|
|
12
|
+
instagram?: string;
|
|
13
|
+
linkedIn?: string;
|
|
14
|
+
youtube?: string;
|
|
15
|
+
website?: string;
|
|
16
|
+
diaspora?: string;
|
|
17
|
+
mastodon?: string;
|
|
18
|
+
telegram?: string;
|
|
19
|
+
signal?: string;
|
|
20
|
+
}
|
|
21
|
+
|