@frenchbaas/js 0.1.0
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/README.md +98 -0
- package/dist/index.cjs +541 -0
- package/dist/index.d.mts +355 -0
- package/dist/index.d.ts +355 -0
- package/dist/index.js +506 -0
- package/package.json +37 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
interface FrenchBaasConfig {
|
|
2
|
+
/** URL de base de l'API FrenchBaas (ex: https://api.frenchbaas.fr) */
|
|
3
|
+
url: string;
|
|
4
|
+
/** Clé API du projet (X-Api-Key) */
|
|
5
|
+
apiKey: string;
|
|
6
|
+
/**
|
|
7
|
+
* Stratégie de stockage des tokens.
|
|
8
|
+
* - 'memory' (défaut) : tokens en mémoire JS — plus sûr (pas de XSS via storage)
|
|
9
|
+
* - 'localStorage' : tokens persistés — pratique mais expose au XSS
|
|
10
|
+
*/
|
|
11
|
+
storage?: 'memory' | 'localStorage';
|
|
12
|
+
}
|
|
13
|
+
interface User {
|
|
14
|
+
id: string;
|
|
15
|
+
email: string;
|
|
16
|
+
}
|
|
17
|
+
interface AuthResult {
|
|
18
|
+
user: User;
|
|
19
|
+
access_token: string;
|
|
20
|
+
refresh_token: string;
|
|
21
|
+
}
|
|
22
|
+
interface RefreshResult {
|
|
23
|
+
access_token: string;
|
|
24
|
+
refresh_token: string;
|
|
25
|
+
}
|
|
26
|
+
interface Document<T = Record<string, unknown>> {
|
|
27
|
+
id: string;
|
|
28
|
+
project_id: string;
|
|
29
|
+
collection_id: string;
|
|
30
|
+
data: T;
|
|
31
|
+
created_by: string | null;
|
|
32
|
+
created_at: string;
|
|
33
|
+
updated_at: string;
|
|
34
|
+
}
|
|
35
|
+
interface PaginationMeta {
|
|
36
|
+
total: number;
|
|
37
|
+
page: number;
|
|
38
|
+
per_page: number;
|
|
39
|
+
pages: number;
|
|
40
|
+
}
|
|
41
|
+
interface DocumentsPage<T = Record<string, unknown>> {
|
|
42
|
+
data: Document<T>[];
|
|
43
|
+
meta: PaginationMeta;
|
|
44
|
+
}
|
|
45
|
+
interface GetOptions {
|
|
46
|
+
/** Numéro de page (défaut: 1) */
|
|
47
|
+
page?: number;
|
|
48
|
+
/** Documents par page — max 100 (défaut: 50) */
|
|
49
|
+
perPage?: number;
|
|
50
|
+
}
|
|
51
|
+
type FieldType = 'string' | 'integer' | 'number' | 'boolean' | 'array' | 'object' | 'datetime';
|
|
52
|
+
interface SchemaField {
|
|
53
|
+
name: string;
|
|
54
|
+
type: FieldType;
|
|
55
|
+
required?: boolean;
|
|
56
|
+
default?: unknown;
|
|
57
|
+
}
|
|
58
|
+
interface CollectionSchema {
|
|
59
|
+
collection_id: string;
|
|
60
|
+
name: string;
|
|
61
|
+
visibility: 'public' | 'authenticated' | 'private';
|
|
62
|
+
schema: SchemaField[];
|
|
63
|
+
}
|
|
64
|
+
interface OpenApiSpec {
|
|
65
|
+
openapi: string;
|
|
66
|
+
info: Record<string, unknown>;
|
|
67
|
+
paths: Record<string, unknown>;
|
|
68
|
+
[key: string]: unknown;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type StorageStrategy = 'memory' | 'localStorage';
|
|
72
|
+
/**
|
|
73
|
+
* Gère le stockage des tokens JWT et des infos utilisateur.
|
|
74
|
+
*
|
|
75
|
+
* Stratégie 'memory' : tout en mémoire JS (disparaît au reload — plus sûr contre XSS)
|
|
76
|
+
* Stratégie 'localStorage' : persisté entre les reloads (pratique mais exposé au XSS)
|
|
77
|
+
*/
|
|
78
|
+
declare class TokenStore {
|
|
79
|
+
private _state;
|
|
80
|
+
private readonly _strategy;
|
|
81
|
+
constructor(strategy?: StorageStrategy);
|
|
82
|
+
get accessToken(): string | null;
|
|
83
|
+
get refreshToken(): string | null;
|
|
84
|
+
get user(): User | null;
|
|
85
|
+
/** Stocke access token, refresh token et optionnellement l'utilisateur. */
|
|
86
|
+
set(tokens: {
|
|
87
|
+
accessToken: string;
|
|
88
|
+
refreshToken: string;
|
|
89
|
+
user?: User | null;
|
|
90
|
+
}): void;
|
|
91
|
+
/** Met à jour uniquement les tokens (appelé après un refresh — sans user). */
|
|
92
|
+
updateTokens(accessToken: string, refreshToken: string): void;
|
|
93
|
+
/** Efface tous les tokens et l'utilisateur (appelé lors du logout). */
|
|
94
|
+
clear(): void;
|
|
95
|
+
/**
|
|
96
|
+
* Vérifie si l'access token est expiré ou expire dans moins de 30 secondes.
|
|
97
|
+
* Retourne true si absent ou invalide.
|
|
98
|
+
*/
|
|
99
|
+
isAccessTokenExpired(): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Vérifie si le refresh token est expiré.
|
|
102
|
+
* Retourne true si absent ou invalide.
|
|
103
|
+
*/
|
|
104
|
+
isRefreshTokenExpired(): boolean;
|
|
105
|
+
private _isExpired;
|
|
106
|
+
private _persist;
|
|
107
|
+
private _loadFromStorage;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
type RefreshFn = () => Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Client HTTP interne.
|
|
113
|
+
* Gère automatiquement :
|
|
114
|
+
* - Les headers X-Api-Key et Authorization sur chaque requête
|
|
115
|
+
* - Le pre-emptive refresh (si le token expire dans < 30s)
|
|
116
|
+
* - Le retry unique sur 401 (refresh puis relance la requête)
|
|
117
|
+
* - Le décodage des erreurs API en exceptions typées
|
|
118
|
+
*/
|
|
119
|
+
declare class HttpClient {
|
|
120
|
+
private readonly _baseUrl;
|
|
121
|
+
private readonly _apiKey;
|
|
122
|
+
private readonly _tokenStore;
|
|
123
|
+
private _refreshFn;
|
|
124
|
+
private _refreshing;
|
|
125
|
+
constructor(_baseUrl: string, _apiKey: string, _tokenStore: TokenStore);
|
|
126
|
+
/** Enregistre la fonction de refresh (injectée par AuthModule). */
|
|
127
|
+
setRefreshFn(fn: RefreshFn): void;
|
|
128
|
+
get<T>(path: string, params?: Record<string, string | number | undefined>): Promise<T>;
|
|
129
|
+
post<T>(path: string, body?: unknown): Promise<T>;
|
|
130
|
+
patch<T>(path: string, body?: unknown): Promise<T>;
|
|
131
|
+
delete<T>(path: string): Promise<T>;
|
|
132
|
+
private _request;
|
|
133
|
+
private _fetch;
|
|
134
|
+
/**
|
|
135
|
+
* Déclenche le refresh en s'assurant qu'un seul refresh tourne à la fois
|
|
136
|
+
* même si plusieurs requêtes l'appellent simultanément.
|
|
137
|
+
*/
|
|
138
|
+
private _triggerRefresh;
|
|
139
|
+
private _parseResponse;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Module d'authentification SDK.
|
|
144
|
+
*
|
|
145
|
+
* Gère : signUp, login, logout, getUser, isLoggedIn.
|
|
146
|
+
* Le refresh token est appelé automatiquement par HttpClient
|
|
147
|
+
* via la fonction enregistrée dans setRefreshFn.
|
|
148
|
+
*
|
|
149
|
+
* Important : le refresh utilise un fetch direct (pas HttpClient)
|
|
150
|
+
* pour éviter toute récursion infinie.
|
|
151
|
+
*/
|
|
152
|
+
declare class AuthModule {
|
|
153
|
+
private readonly _http;
|
|
154
|
+
private readonly _tokenStore;
|
|
155
|
+
private readonly _baseUrl;
|
|
156
|
+
private readonly _apiKey;
|
|
157
|
+
constructor(_http: HttpClient, _tokenStore: TokenStore, _baseUrl: string, _apiKey: string);
|
|
158
|
+
/**
|
|
159
|
+
* Inscrit un nouvel utilisateur final.
|
|
160
|
+
* Stocke automatiquement les tokens après inscription réussie.
|
|
161
|
+
*/
|
|
162
|
+
signUp(params: {
|
|
163
|
+
email: string;
|
|
164
|
+
password: string;
|
|
165
|
+
}): Promise<AuthResult>;
|
|
166
|
+
/**
|
|
167
|
+
* Connecte un utilisateur existant.
|
|
168
|
+
* Stocke automatiquement les tokens après connexion réussie.
|
|
169
|
+
*/
|
|
170
|
+
login(params: {
|
|
171
|
+
email: string;
|
|
172
|
+
password: string;
|
|
173
|
+
}): Promise<AuthResult>;
|
|
174
|
+
/**
|
|
175
|
+
* Déconnecte l'utilisateur et efface tous les tokens stockés.
|
|
176
|
+
* L'appel API est best-effort (stateless côté serveur).
|
|
177
|
+
*/
|
|
178
|
+
logout(): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Retourne l'utilisateur courant depuis la mémoire (synchrone).
|
|
181
|
+
* Retourne null si non connecté ou si la page a été rechargée
|
|
182
|
+
* avec la stratégie 'memory'.
|
|
183
|
+
*/
|
|
184
|
+
getUser(): User | null;
|
|
185
|
+
/**
|
|
186
|
+
* Indique si l'utilisateur a une session active (refresh token valide).
|
|
187
|
+
*/
|
|
188
|
+
isLoggedIn(): boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Refresh interne — appelé automatiquement par HttpClient.
|
|
191
|
+
* Utilise un fetch direct pour éviter la récursion infinie.
|
|
192
|
+
* Le refresh token expiré → logout propre + AuthError.
|
|
193
|
+
*/
|
|
194
|
+
_refresh(): Promise<void>;
|
|
195
|
+
private _storeAndReturn;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Client pour une collection FrenchBaas.
|
|
200
|
+
* Obtenu via `client.collection('uuid-collection-id')`.
|
|
201
|
+
*
|
|
202
|
+
* Le type générique T représente la structure du champ `data` de vos documents.
|
|
203
|
+
* Exemple : client.collection<{ title: string; published: boolean }>('uuid')
|
|
204
|
+
*/
|
|
205
|
+
declare class CollectionClient<T = Record<string, unknown>> {
|
|
206
|
+
private readonly _collectionId;
|
|
207
|
+
private readonly _http;
|
|
208
|
+
constructor(_collectionId: string, _http: HttpClient);
|
|
209
|
+
/**
|
|
210
|
+
* Liste les documents de la collection avec pagination.
|
|
211
|
+
* La visibilité (public/authenticated/private) est gérée côté serveur.
|
|
212
|
+
*
|
|
213
|
+
* @param options.page Numéro de page (défaut: 1)
|
|
214
|
+
* @param options.perPage Documents par page, max 100 (défaut: 50)
|
|
215
|
+
*/
|
|
216
|
+
get(options?: GetOptions): Promise<DocumentsPage<T>>;
|
|
217
|
+
/**
|
|
218
|
+
* Crée un nouveau document dans la collection.
|
|
219
|
+
* Les champs sont validés contre le schéma côté serveur.
|
|
220
|
+
*
|
|
221
|
+
* @param data Données du document (doit respecter le schéma de la collection)
|
|
222
|
+
*/
|
|
223
|
+
create(data: Partial<T>): Promise<Document<T>>;
|
|
224
|
+
/**
|
|
225
|
+
* Met à jour partiellement un document (merge).
|
|
226
|
+
* Seuls les champs fournis sont mis à jour, les autres restent inchangés.
|
|
227
|
+
*
|
|
228
|
+
* - Collection private : seul le créateur peut modifier.
|
|
229
|
+
* - Collection authenticated/public : tout utilisateur authentifié peut modifier.
|
|
230
|
+
*
|
|
231
|
+
* @param documentId UUID du document à modifier
|
|
232
|
+
* @param data Champs à mettre à jour
|
|
233
|
+
*/
|
|
234
|
+
update(documentId: string, data: Partial<T>): Promise<Document<T>>;
|
|
235
|
+
/**
|
|
236
|
+
* Supprime un document.
|
|
237
|
+
*
|
|
238
|
+
* - Collection private : seul le créateur peut supprimer.
|
|
239
|
+
* - Collection authenticated/public : tout utilisateur authentifié peut supprimer.
|
|
240
|
+
*
|
|
241
|
+
* @param documentId UUID du document à supprimer
|
|
242
|
+
*/
|
|
243
|
+
delete(documentId: string): Promise<void>;
|
|
244
|
+
/**
|
|
245
|
+
* Retourne le schéma de la collection (nom des champs, types, visibilité).
|
|
246
|
+
* Utile pour construire des formulaires dynamiques.
|
|
247
|
+
*/
|
|
248
|
+
schema(): Promise<CollectionSchema>;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Client principal FrenchBaas.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* const client = new FrenchBaas({
|
|
257
|
+
* url: 'https://api.frenchbaas.fr',
|
|
258
|
+
* apiKey: 'votre-api-key',
|
|
259
|
+
* })
|
|
260
|
+
*
|
|
261
|
+
* await client.auth.login({ email, password })
|
|
262
|
+
*
|
|
263
|
+
* const posts = client.collection('uuid-collection')
|
|
264
|
+
* const { data } = await posts.get()
|
|
265
|
+
* await posts.create({ title: 'Hello' })
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
declare class FrenchBaasClient {
|
|
269
|
+
/** Module d'authentification — signUp, login, logout, getUser */
|
|
270
|
+
readonly auth: AuthModule;
|
|
271
|
+
private readonly _http;
|
|
272
|
+
private readonly _tokenStore;
|
|
273
|
+
constructor(config: FrenchBaasConfig);
|
|
274
|
+
/**
|
|
275
|
+
* Retourne un client pour une collection spécifique.
|
|
276
|
+
*
|
|
277
|
+
* Le type générique T permet de typer les données des documents :
|
|
278
|
+
* ```ts
|
|
279
|
+
* const posts = client.collection<{ title: string; published: boolean }>('uuid')
|
|
280
|
+
* const doc = await posts.create({ title: 'Hello', published: true })
|
|
281
|
+
* // doc.data.title est typé string ✓
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @param collectionId UUID de la collection (visible dans le Dashboard)
|
|
285
|
+
*/
|
|
286
|
+
collection<T = Record<string, unknown>>(collectionId: string): CollectionClient<T>;
|
|
287
|
+
/**
|
|
288
|
+
* Retourne la spécification OpenAPI du projet.
|
|
289
|
+
* Contient toutes les collections, leurs schémas et les endpoints disponibles.
|
|
290
|
+
* Utile pour de la génération de code ou de la documentation dynamique.
|
|
291
|
+
*/
|
|
292
|
+
getOpenApiSpec(): Promise<OpenApiSpec>;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Classe de base pour toutes les erreurs FrenchBaas.
|
|
297
|
+
* Permet de faire `err instanceof FrenchBaasError` pour distinguer
|
|
298
|
+
* les erreurs SDK des erreurs JS natives.
|
|
299
|
+
*/
|
|
300
|
+
declare class FrenchBaasError extends Error {
|
|
301
|
+
readonly status?: number | undefined;
|
|
302
|
+
constructor(message: string, status?: number | undefined);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Erreur d'authentification.
|
|
306
|
+
* Levée sur : 401 (token invalide/expiré), 403 (accès refusé non-quota).
|
|
307
|
+
*/
|
|
308
|
+
declare class AuthError extends FrenchBaasError {
|
|
309
|
+
constructor(message: string, status?: number);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Erreur réseau — impossible de joindre le serveur.
|
|
313
|
+
* Levée sur : fetch rejeté (hors ligne, DNS, timeout).
|
|
314
|
+
*/
|
|
315
|
+
declare class NetworkError extends FrenchBaasError {
|
|
316
|
+
constructor(message?: string);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Erreur de validation.
|
|
320
|
+
* Levée sur : 400 (paramètres manquants), 413 (document trop grand), 422 (schéma invalide).
|
|
321
|
+
*/
|
|
322
|
+
declare class ValidationError extends FrenchBaasError {
|
|
323
|
+
readonly errors: string[];
|
|
324
|
+
constructor(message: string, errors?: string[]);
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Erreur ressource introuvable.
|
|
328
|
+
* Levée sur : 404.
|
|
329
|
+
*/
|
|
330
|
+
declare class NotFoundError extends FrenchBaasError {
|
|
331
|
+
constructor(message?: string);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Erreur quota dépassé.
|
|
335
|
+
* Levée sur : 403 avec info quota dans la réponse (documents, SDK users, projets).
|
|
336
|
+
*/
|
|
337
|
+
declare class QuotaError extends FrenchBaasError {
|
|
338
|
+
constructor(message: string);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Erreur rate limiting.
|
|
342
|
+
* Levée sur : 429 (trop de requêtes).
|
|
343
|
+
*/
|
|
344
|
+
declare class RateLimitError extends FrenchBaasError {
|
|
345
|
+
constructor(message?: string);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Erreur serveur interne.
|
|
349
|
+
* Levée sur : 5xx.
|
|
350
|
+
*/
|
|
351
|
+
declare class ServerError extends FrenchBaasError {
|
|
352
|
+
constructor(message?: string, status?: number);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export { AuthError, type AuthResult, type CollectionSchema, type Document, type DocumentsPage, type FieldType, FrenchBaasClient as FrenchBaas, type FrenchBaasConfig, FrenchBaasError, type GetOptions, NetworkError, NotFoundError, type OpenApiSpec, type PaginationMeta, QuotaError, RateLimitError, type RefreshResult, type SchemaField, ServerError, type User, ValidationError };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
interface FrenchBaasConfig {
|
|
2
|
+
/** URL de base de l'API FrenchBaas (ex: https://api.frenchbaas.fr) */
|
|
3
|
+
url: string;
|
|
4
|
+
/** Clé API du projet (X-Api-Key) */
|
|
5
|
+
apiKey: string;
|
|
6
|
+
/**
|
|
7
|
+
* Stratégie de stockage des tokens.
|
|
8
|
+
* - 'memory' (défaut) : tokens en mémoire JS — plus sûr (pas de XSS via storage)
|
|
9
|
+
* - 'localStorage' : tokens persistés — pratique mais expose au XSS
|
|
10
|
+
*/
|
|
11
|
+
storage?: 'memory' | 'localStorage';
|
|
12
|
+
}
|
|
13
|
+
interface User {
|
|
14
|
+
id: string;
|
|
15
|
+
email: string;
|
|
16
|
+
}
|
|
17
|
+
interface AuthResult {
|
|
18
|
+
user: User;
|
|
19
|
+
access_token: string;
|
|
20
|
+
refresh_token: string;
|
|
21
|
+
}
|
|
22
|
+
interface RefreshResult {
|
|
23
|
+
access_token: string;
|
|
24
|
+
refresh_token: string;
|
|
25
|
+
}
|
|
26
|
+
interface Document<T = Record<string, unknown>> {
|
|
27
|
+
id: string;
|
|
28
|
+
project_id: string;
|
|
29
|
+
collection_id: string;
|
|
30
|
+
data: T;
|
|
31
|
+
created_by: string | null;
|
|
32
|
+
created_at: string;
|
|
33
|
+
updated_at: string;
|
|
34
|
+
}
|
|
35
|
+
interface PaginationMeta {
|
|
36
|
+
total: number;
|
|
37
|
+
page: number;
|
|
38
|
+
per_page: number;
|
|
39
|
+
pages: number;
|
|
40
|
+
}
|
|
41
|
+
interface DocumentsPage<T = Record<string, unknown>> {
|
|
42
|
+
data: Document<T>[];
|
|
43
|
+
meta: PaginationMeta;
|
|
44
|
+
}
|
|
45
|
+
interface GetOptions {
|
|
46
|
+
/** Numéro de page (défaut: 1) */
|
|
47
|
+
page?: number;
|
|
48
|
+
/** Documents par page — max 100 (défaut: 50) */
|
|
49
|
+
perPage?: number;
|
|
50
|
+
}
|
|
51
|
+
type FieldType = 'string' | 'integer' | 'number' | 'boolean' | 'array' | 'object' | 'datetime';
|
|
52
|
+
interface SchemaField {
|
|
53
|
+
name: string;
|
|
54
|
+
type: FieldType;
|
|
55
|
+
required?: boolean;
|
|
56
|
+
default?: unknown;
|
|
57
|
+
}
|
|
58
|
+
interface CollectionSchema {
|
|
59
|
+
collection_id: string;
|
|
60
|
+
name: string;
|
|
61
|
+
visibility: 'public' | 'authenticated' | 'private';
|
|
62
|
+
schema: SchemaField[];
|
|
63
|
+
}
|
|
64
|
+
interface OpenApiSpec {
|
|
65
|
+
openapi: string;
|
|
66
|
+
info: Record<string, unknown>;
|
|
67
|
+
paths: Record<string, unknown>;
|
|
68
|
+
[key: string]: unknown;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type StorageStrategy = 'memory' | 'localStorage';
|
|
72
|
+
/**
|
|
73
|
+
* Gère le stockage des tokens JWT et des infos utilisateur.
|
|
74
|
+
*
|
|
75
|
+
* Stratégie 'memory' : tout en mémoire JS (disparaît au reload — plus sûr contre XSS)
|
|
76
|
+
* Stratégie 'localStorage' : persisté entre les reloads (pratique mais exposé au XSS)
|
|
77
|
+
*/
|
|
78
|
+
declare class TokenStore {
|
|
79
|
+
private _state;
|
|
80
|
+
private readonly _strategy;
|
|
81
|
+
constructor(strategy?: StorageStrategy);
|
|
82
|
+
get accessToken(): string | null;
|
|
83
|
+
get refreshToken(): string | null;
|
|
84
|
+
get user(): User | null;
|
|
85
|
+
/** Stocke access token, refresh token et optionnellement l'utilisateur. */
|
|
86
|
+
set(tokens: {
|
|
87
|
+
accessToken: string;
|
|
88
|
+
refreshToken: string;
|
|
89
|
+
user?: User | null;
|
|
90
|
+
}): void;
|
|
91
|
+
/** Met à jour uniquement les tokens (appelé après un refresh — sans user). */
|
|
92
|
+
updateTokens(accessToken: string, refreshToken: string): void;
|
|
93
|
+
/** Efface tous les tokens et l'utilisateur (appelé lors du logout). */
|
|
94
|
+
clear(): void;
|
|
95
|
+
/**
|
|
96
|
+
* Vérifie si l'access token est expiré ou expire dans moins de 30 secondes.
|
|
97
|
+
* Retourne true si absent ou invalide.
|
|
98
|
+
*/
|
|
99
|
+
isAccessTokenExpired(): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Vérifie si le refresh token est expiré.
|
|
102
|
+
* Retourne true si absent ou invalide.
|
|
103
|
+
*/
|
|
104
|
+
isRefreshTokenExpired(): boolean;
|
|
105
|
+
private _isExpired;
|
|
106
|
+
private _persist;
|
|
107
|
+
private _loadFromStorage;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
type RefreshFn = () => Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Client HTTP interne.
|
|
113
|
+
* Gère automatiquement :
|
|
114
|
+
* - Les headers X-Api-Key et Authorization sur chaque requête
|
|
115
|
+
* - Le pre-emptive refresh (si le token expire dans < 30s)
|
|
116
|
+
* - Le retry unique sur 401 (refresh puis relance la requête)
|
|
117
|
+
* - Le décodage des erreurs API en exceptions typées
|
|
118
|
+
*/
|
|
119
|
+
declare class HttpClient {
|
|
120
|
+
private readonly _baseUrl;
|
|
121
|
+
private readonly _apiKey;
|
|
122
|
+
private readonly _tokenStore;
|
|
123
|
+
private _refreshFn;
|
|
124
|
+
private _refreshing;
|
|
125
|
+
constructor(_baseUrl: string, _apiKey: string, _tokenStore: TokenStore);
|
|
126
|
+
/** Enregistre la fonction de refresh (injectée par AuthModule). */
|
|
127
|
+
setRefreshFn(fn: RefreshFn): void;
|
|
128
|
+
get<T>(path: string, params?: Record<string, string | number | undefined>): Promise<T>;
|
|
129
|
+
post<T>(path: string, body?: unknown): Promise<T>;
|
|
130
|
+
patch<T>(path: string, body?: unknown): Promise<T>;
|
|
131
|
+
delete<T>(path: string): Promise<T>;
|
|
132
|
+
private _request;
|
|
133
|
+
private _fetch;
|
|
134
|
+
/**
|
|
135
|
+
* Déclenche le refresh en s'assurant qu'un seul refresh tourne à la fois
|
|
136
|
+
* même si plusieurs requêtes l'appellent simultanément.
|
|
137
|
+
*/
|
|
138
|
+
private _triggerRefresh;
|
|
139
|
+
private _parseResponse;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Module d'authentification SDK.
|
|
144
|
+
*
|
|
145
|
+
* Gère : signUp, login, logout, getUser, isLoggedIn.
|
|
146
|
+
* Le refresh token est appelé automatiquement par HttpClient
|
|
147
|
+
* via la fonction enregistrée dans setRefreshFn.
|
|
148
|
+
*
|
|
149
|
+
* Important : le refresh utilise un fetch direct (pas HttpClient)
|
|
150
|
+
* pour éviter toute récursion infinie.
|
|
151
|
+
*/
|
|
152
|
+
declare class AuthModule {
|
|
153
|
+
private readonly _http;
|
|
154
|
+
private readonly _tokenStore;
|
|
155
|
+
private readonly _baseUrl;
|
|
156
|
+
private readonly _apiKey;
|
|
157
|
+
constructor(_http: HttpClient, _tokenStore: TokenStore, _baseUrl: string, _apiKey: string);
|
|
158
|
+
/**
|
|
159
|
+
* Inscrit un nouvel utilisateur final.
|
|
160
|
+
* Stocke automatiquement les tokens après inscription réussie.
|
|
161
|
+
*/
|
|
162
|
+
signUp(params: {
|
|
163
|
+
email: string;
|
|
164
|
+
password: string;
|
|
165
|
+
}): Promise<AuthResult>;
|
|
166
|
+
/**
|
|
167
|
+
* Connecte un utilisateur existant.
|
|
168
|
+
* Stocke automatiquement les tokens après connexion réussie.
|
|
169
|
+
*/
|
|
170
|
+
login(params: {
|
|
171
|
+
email: string;
|
|
172
|
+
password: string;
|
|
173
|
+
}): Promise<AuthResult>;
|
|
174
|
+
/**
|
|
175
|
+
* Déconnecte l'utilisateur et efface tous les tokens stockés.
|
|
176
|
+
* L'appel API est best-effort (stateless côté serveur).
|
|
177
|
+
*/
|
|
178
|
+
logout(): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Retourne l'utilisateur courant depuis la mémoire (synchrone).
|
|
181
|
+
* Retourne null si non connecté ou si la page a été rechargée
|
|
182
|
+
* avec la stratégie 'memory'.
|
|
183
|
+
*/
|
|
184
|
+
getUser(): User | null;
|
|
185
|
+
/**
|
|
186
|
+
* Indique si l'utilisateur a une session active (refresh token valide).
|
|
187
|
+
*/
|
|
188
|
+
isLoggedIn(): boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Refresh interne — appelé automatiquement par HttpClient.
|
|
191
|
+
* Utilise un fetch direct pour éviter la récursion infinie.
|
|
192
|
+
* Le refresh token expiré → logout propre + AuthError.
|
|
193
|
+
*/
|
|
194
|
+
_refresh(): Promise<void>;
|
|
195
|
+
private _storeAndReturn;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Client pour une collection FrenchBaas.
|
|
200
|
+
* Obtenu via `client.collection('uuid-collection-id')`.
|
|
201
|
+
*
|
|
202
|
+
* Le type générique T représente la structure du champ `data` de vos documents.
|
|
203
|
+
* Exemple : client.collection<{ title: string; published: boolean }>('uuid')
|
|
204
|
+
*/
|
|
205
|
+
declare class CollectionClient<T = Record<string, unknown>> {
|
|
206
|
+
private readonly _collectionId;
|
|
207
|
+
private readonly _http;
|
|
208
|
+
constructor(_collectionId: string, _http: HttpClient);
|
|
209
|
+
/**
|
|
210
|
+
* Liste les documents de la collection avec pagination.
|
|
211
|
+
* La visibilité (public/authenticated/private) est gérée côté serveur.
|
|
212
|
+
*
|
|
213
|
+
* @param options.page Numéro de page (défaut: 1)
|
|
214
|
+
* @param options.perPage Documents par page, max 100 (défaut: 50)
|
|
215
|
+
*/
|
|
216
|
+
get(options?: GetOptions): Promise<DocumentsPage<T>>;
|
|
217
|
+
/**
|
|
218
|
+
* Crée un nouveau document dans la collection.
|
|
219
|
+
* Les champs sont validés contre le schéma côté serveur.
|
|
220
|
+
*
|
|
221
|
+
* @param data Données du document (doit respecter le schéma de la collection)
|
|
222
|
+
*/
|
|
223
|
+
create(data: Partial<T>): Promise<Document<T>>;
|
|
224
|
+
/**
|
|
225
|
+
* Met à jour partiellement un document (merge).
|
|
226
|
+
* Seuls les champs fournis sont mis à jour, les autres restent inchangés.
|
|
227
|
+
*
|
|
228
|
+
* - Collection private : seul le créateur peut modifier.
|
|
229
|
+
* - Collection authenticated/public : tout utilisateur authentifié peut modifier.
|
|
230
|
+
*
|
|
231
|
+
* @param documentId UUID du document à modifier
|
|
232
|
+
* @param data Champs à mettre à jour
|
|
233
|
+
*/
|
|
234
|
+
update(documentId: string, data: Partial<T>): Promise<Document<T>>;
|
|
235
|
+
/**
|
|
236
|
+
* Supprime un document.
|
|
237
|
+
*
|
|
238
|
+
* - Collection private : seul le créateur peut supprimer.
|
|
239
|
+
* - Collection authenticated/public : tout utilisateur authentifié peut supprimer.
|
|
240
|
+
*
|
|
241
|
+
* @param documentId UUID du document à supprimer
|
|
242
|
+
*/
|
|
243
|
+
delete(documentId: string): Promise<void>;
|
|
244
|
+
/**
|
|
245
|
+
* Retourne le schéma de la collection (nom des champs, types, visibilité).
|
|
246
|
+
* Utile pour construire des formulaires dynamiques.
|
|
247
|
+
*/
|
|
248
|
+
schema(): Promise<CollectionSchema>;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Client principal FrenchBaas.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* const client = new FrenchBaas({
|
|
257
|
+
* url: 'https://api.frenchbaas.fr',
|
|
258
|
+
* apiKey: 'votre-api-key',
|
|
259
|
+
* })
|
|
260
|
+
*
|
|
261
|
+
* await client.auth.login({ email, password })
|
|
262
|
+
*
|
|
263
|
+
* const posts = client.collection('uuid-collection')
|
|
264
|
+
* const { data } = await posts.get()
|
|
265
|
+
* await posts.create({ title: 'Hello' })
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
declare class FrenchBaasClient {
|
|
269
|
+
/** Module d'authentification — signUp, login, logout, getUser */
|
|
270
|
+
readonly auth: AuthModule;
|
|
271
|
+
private readonly _http;
|
|
272
|
+
private readonly _tokenStore;
|
|
273
|
+
constructor(config: FrenchBaasConfig);
|
|
274
|
+
/**
|
|
275
|
+
* Retourne un client pour une collection spécifique.
|
|
276
|
+
*
|
|
277
|
+
* Le type générique T permet de typer les données des documents :
|
|
278
|
+
* ```ts
|
|
279
|
+
* const posts = client.collection<{ title: string; published: boolean }>('uuid')
|
|
280
|
+
* const doc = await posts.create({ title: 'Hello', published: true })
|
|
281
|
+
* // doc.data.title est typé string ✓
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @param collectionId UUID de la collection (visible dans le Dashboard)
|
|
285
|
+
*/
|
|
286
|
+
collection<T = Record<string, unknown>>(collectionId: string): CollectionClient<T>;
|
|
287
|
+
/**
|
|
288
|
+
* Retourne la spécification OpenAPI du projet.
|
|
289
|
+
* Contient toutes les collections, leurs schémas et les endpoints disponibles.
|
|
290
|
+
* Utile pour de la génération de code ou de la documentation dynamique.
|
|
291
|
+
*/
|
|
292
|
+
getOpenApiSpec(): Promise<OpenApiSpec>;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Classe de base pour toutes les erreurs FrenchBaas.
|
|
297
|
+
* Permet de faire `err instanceof FrenchBaasError` pour distinguer
|
|
298
|
+
* les erreurs SDK des erreurs JS natives.
|
|
299
|
+
*/
|
|
300
|
+
declare class FrenchBaasError extends Error {
|
|
301
|
+
readonly status?: number | undefined;
|
|
302
|
+
constructor(message: string, status?: number | undefined);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Erreur d'authentification.
|
|
306
|
+
* Levée sur : 401 (token invalide/expiré), 403 (accès refusé non-quota).
|
|
307
|
+
*/
|
|
308
|
+
declare class AuthError extends FrenchBaasError {
|
|
309
|
+
constructor(message: string, status?: number);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Erreur réseau — impossible de joindre le serveur.
|
|
313
|
+
* Levée sur : fetch rejeté (hors ligne, DNS, timeout).
|
|
314
|
+
*/
|
|
315
|
+
declare class NetworkError extends FrenchBaasError {
|
|
316
|
+
constructor(message?: string);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Erreur de validation.
|
|
320
|
+
* Levée sur : 400 (paramètres manquants), 413 (document trop grand), 422 (schéma invalide).
|
|
321
|
+
*/
|
|
322
|
+
declare class ValidationError extends FrenchBaasError {
|
|
323
|
+
readonly errors: string[];
|
|
324
|
+
constructor(message: string, errors?: string[]);
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Erreur ressource introuvable.
|
|
328
|
+
* Levée sur : 404.
|
|
329
|
+
*/
|
|
330
|
+
declare class NotFoundError extends FrenchBaasError {
|
|
331
|
+
constructor(message?: string);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Erreur quota dépassé.
|
|
335
|
+
* Levée sur : 403 avec info quota dans la réponse (documents, SDK users, projets).
|
|
336
|
+
*/
|
|
337
|
+
declare class QuotaError extends FrenchBaasError {
|
|
338
|
+
constructor(message: string);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Erreur rate limiting.
|
|
342
|
+
* Levée sur : 429 (trop de requêtes).
|
|
343
|
+
*/
|
|
344
|
+
declare class RateLimitError extends FrenchBaasError {
|
|
345
|
+
constructor(message?: string);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Erreur serveur interne.
|
|
349
|
+
* Levée sur : 5xx.
|
|
350
|
+
*/
|
|
351
|
+
declare class ServerError extends FrenchBaasError {
|
|
352
|
+
constructor(message?: string, status?: number);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export { AuthError, type AuthResult, type CollectionSchema, type Document, type DocumentsPage, type FieldType, FrenchBaasClient as FrenchBaas, type FrenchBaasConfig, FrenchBaasError, type GetOptions, NetworkError, NotFoundError, type OpenApiSpec, type PaginationMeta, QuotaError, RateLimitError, type RefreshResult, type SchemaField, ServerError, type User, ValidationError };
|