@goralys/core 1.1.0 → 1.1.2

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 (3) hide show
  1. package/dist/index.d.ts +47 -28
  2. package/dist/index.js +114 -101
  3. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -1,3 +1,49 @@
1
+ interface GoralysClientConfig {
2
+ apiDomain: string;
3
+ }
4
+ declare function configureGoralysClient(cfg: GoralysClientConfig): void;
5
+ declare function getGoralysClientConfig(): GoralysClientConfig;
6
+
7
+ interface StorageAdapter {
8
+ getItem(key: string): string | null | Promise<string | null>;
9
+ setItem(key: string, value: string): void | Promise<void>;
10
+ removeItem(key: string): void | Promise<void>;
11
+ }
12
+ declare function configureStorage(customAdapter: StorageAdapter): void;
13
+ declare function storageGet(key: string): Promise<string | null>;
14
+ declare function storageSet(key: string, value: string): Promise<void>;
15
+ declare function storageRemove(key: string): Promise<void>;
16
+
17
+ interface PHPDateTime {
18
+ date: string;
19
+ timezone_type: number;
20
+ timezone: string;
21
+ }
22
+ type CookieValue = string | boolean | number | null | undefined;
23
+
24
+ interface CookiesAdapter {
25
+ onChange(callback: () => void): () => void;
26
+ getCookies(key: string): string | null;
27
+ setCookies(key: string, value: CookieValue, path: string, maxAge: number): void;
28
+ removeCookies(key: string): void;
29
+ }
30
+
31
+ type CheckAuthImpl = () => boolean;
32
+ declare function configureCheckAuth(impl: CheckAuthImpl): void;
33
+ declare function isAuthenticated(): boolean;
34
+
35
+ interface GoralysCoreConfig {
36
+ client: GoralysClientConfig;
37
+ storage: StorageAdapter;
38
+ cookies: CookiesAdapter;
39
+ auth: CheckAuthImpl;
40
+ cache: {
41
+ cache: () => Promise<void>;
42
+ empty: () => void;
43
+ };
44
+ }
45
+ declare function configGoralysCore(cfg: GoralysCoreConfig): void;
46
+
1
47
  type UserRole = {
2
48
  role: "admin" | "teacher" | "student" | "none";
3
49
  };
@@ -72,13 +118,6 @@ declare function useSubjects(role: UserRole["role"], showToast: ToastFn): {
72
118
  syncKey: string;
73
119
  };
74
120
 
75
- interface PHPDateTime {
76
- date: string;
77
- timezone_type: number;
78
- timezone: string;
79
- }
80
- type CookieValue = string | boolean | number | null | undefined;
81
-
82
121
  declare const reasonsConfig: {
83
122
  readonly "password-forgot": {
84
123
  readonly label: "Mot de passe oublié";
@@ -176,19 +215,9 @@ declare const parsePhpDateTime: (phpDate: PHPDateTime) => string;
176
215
  declare function emitAuthEvent(event: AuthEvent): void;
177
216
  declare function onAuthEvent(callback: (event: AuthEvent) => void): () => void;
178
217
 
179
- type CheckAuthImpl = () => boolean;
180
- declare function configureCheckAuth(impl: CheckAuthImpl): void;
181
- declare function isAuthenticated(): boolean;
182
-
183
218
  declare function emitUserEvent(event: UserEvent): void;
184
219
  declare function onUserEvent(callback: (event: UserEvent) => void): () => void;
185
220
 
186
- interface GoralysClientConfig {
187
- apiDomain: string;
188
- }
189
- declare function configureGoralysClient(cfg: GoralysClientConfig): void;
190
- declare function getGoralysClientConfig(): GoralysClientConfig;
191
-
192
221
  /**
193
222
  * This filed is a slightly modified version of Next.js own http method declaration.
194
223
  */
@@ -225,16 +254,6 @@ type Listener = (event: NavigationEvent) => void;
225
254
  declare function emitNavigationEvent(event: NavigationEvent): void;
226
255
  declare function onNavigationEvent(listener: Listener): () => void;
227
256
 
228
- interface StorageAdapter {
229
- getItem(key: string): string | null | Promise<string | null>;
230
- setItem(key: string, value: string): void | Promise<void>;
231
- removeItem(key: string): void | Promise<void>;
232
- }
233
- declare function configureStorage(customAdapter: StorageAdapter): void;
234
- declare function storageGet(key: string): Promise<string | null>;
235
- declare function storageSet(key: string, value: string): Promise<void>;
236
- declare function storageRemove(key: string): Promise<void>;
237
-
238
257
  /**
239
258
  * Returns the full name of a subject based on its short name or an alias.
240
259
  * It searches for the first subject in {@link SUBJECTS} whose short name starts with the given string.
@@ -254,4 +273,4 @@ declare function fetchVirtualUsersClient(): Promise<Response>;
254
273
  declare function fetchAdminsClient(): Promise<Response>;
255
274
  declare function fetchVirtualAdminsClient(): Promise<Response>;
256
275
 
257
- export { ADMIN_CACHE_RADIX, ADMIN_SYNC_RADIX, type AuthEvent, type CheckAuthImpl, type ConfirmOptions, type CookieValue, type DraftModalResult, EMAIL_KEY, FULL_NAME_KEY, GoralysActionHandler, type GoralysClientConfig, HTTP_METHODS, type HttpMethod, LOGO_PATH, PERSISTANT_COOKIES, PERSISTANT_LOCALS, type PHPDateTime, PUB_ID_KEY, ROLE_KEY, SUBJECT_CACHES, SUBJECT_CACHE_RADIX, SUBJECT_SYNCS, SUBJECT_SYNC_RADIX, SUPPORT_TICKET_CACHE, SUPPORT_TICKET_SYNC, type StorageAdapter, type Subject, type SubjectStatus, SubjectStatusConfig, type SubjectsSearchField, type SupportReasons, type SupportTicket, type Toast, type ToastFn, USERNAME_KEY, USER_CACHES, USER_CACHE_RADIX, USER_ROLES, USER_SYNCS, USER_SYNC_RADIX, type User, type UserData, type UserEvent, type UserRole, type UserType, buildApiUrl, buildArray, cacheUserDataClient, configureCheckAuth, configureGoralysClient, configureStorage, configureUserCacheCallbacks, emitAuthEvent, emitNavigationEvent, emitUserEvent, emptyUserCacheClient, fetchAdminsClient, fetchCsrfClient, fetchSubjectsForRoleClient, fetchUsersClient, fetchVirtualAdminsClient, fetchVirtualUsersClient, getGoralysClientConfig, getLongFromShort, getStatusHelper, getStatusLabel, goralysFetchClient, handleToastRequest, helpContent, isAuthenticated, isFoolsDay, isHTTPMethod, onAuthEvent, onNavigationEvent, onUserEvent, parsePhpDateTime, reasonsConfig, searchFields, storageGet, storageRemove, storageSet, useAdmins, useSubjects, useSupportTickets, useUsers, useVirtualAdmins, useVirtualUsers };
276
+ export { ADMIN_CACHE_RADIX, ADMIN_SYNC_RADIX, type AuthEvent, type CheckAuthImpl, type ConfirmOptions, type CookieValue, type DraftModalResult, EMAIL_KEY, FULL_NAME_KEY, GoralysActionHandler, type GoralysClientConfig, HTTP_METHODS, type HttpMethod, LOGO_PATH, PERSISTANT_COOKIES, PERSISTANT_LOCALS, type PHPDateTime, PUB_ID_KEY, ROLE_KEY, SUBJECT_CACHES, SUBJECT_CACHE_RADIX, SUBJECT_SYNCS, SUBJECT_SYNC_RADIX, SUPPORT_TICKET_CACHE, SUPPORT_TICKET_SYNC, type StorageAdapter, type Subject, type SubjectStatus, SubjectStatusConfig, type SubjectsSearchField, type SupportReasons, type SupportTicket, type Toast, type ToastFn, USERNAME_KEY, USER_CACHES, USER_CACHE_RADIX, USER_ROLES, USER_SYNCS, USER_SYNC_RADIX, type User, type UserData, type UserEvent, type UserRole, type UserType, buildApiUrl, buildArray, cacheUserDataClient, configGoralysCore, configureCheckAuth, configureGoralysClient, configureStorage, configureUserCacheCallbacks, emitAuthEvent, emitNavigationEvent, emitUserEvent, emptyUserCacheClient, fetchAdminsClient, fetchCsrfClient, fetchSubjectsForRoleClient, fetchUsersClient, fetchVirtualAdminsClient, fetchVirtualUsersClient, getGoralysClientConfig, getLongFromShort, getStatusHelper, getStatusLabel, goralysFetchClient, handleToastRequest, helpContent, isAuthenticated, isFoolsDay, isHTTPMethod, onAuthEvent, onNavigationEvent, onUserEvent, parsePhpDateTime, reasonsConfig, searchFields, storageGet, storageRemove, storageSet, useAdmins, useSubjects, useSupportTickets, useUsers, useVirtualAdmins, useVirtualUsers };
package/dist/index.js CHANGED
@@ -1,5 +1,68 @@
1
- // src/hooks/useSubjects.ts
2
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
1
+ // src/lib/fetch/config.ts
2
+ var config = null;
3
+ function configureGoralysClient(cfg) {
4
+ config = cfg;
5
+ }
6
+ function getGoralysClientConfig() {
7
+ if (!config) {
8
+ throw new Error("Goralys client not configured. Call configureGoralysClient() first.");
9
+ }
10
+ return config;
11
+ }
12
+
13
+ // src/lib/storage/storage-adapter.ts
14
+ var adapter = null;
15
+ function configureStorage(customAdapter) {
16
+ adapter = customAdapter;
17
+ }
18
+ function getAdapter() {
19
+ if (!adapter) {
20
+ throw new Error("Storage not configured. Call configureStorage() first.");
21
+ }
22
+ return adapter;
23
+ }
24
+ async function storageGet(key) {
25
+ return getAdapter().getItem(key);
26
+ }
27
+ async function storageSet(key, value) {
28
+ await getAdapter().setItem(key, value);
29
+ }
30
+ async function storageRemove(key) {
31
+ await getAdapter().removeItem(key);
32
+ }
33
+
34
+ // src/lib/storage/cookies-adapter.ts
35
+ var adapter2 = null;
36
+ function configureCookies(customAdapter) {
37
+ adapter2 = customAdapter;
38
+ }
39
+ function getAdapter2() {
40
+ if (!adapter2) {
41
+ throw new Error("Cookies not configured. Call configureCookies() first.");
42
+ }
43
+ return adapter2;
44
+ }
45
+ function cookiesGet(key) {
46
+ return getAdapter2().getCookies(key);
47
+ }
48
+ function cookiesSet(key, value, path = "/", maxAge = 1.5 * 60) {
49
+ getAdapter2().setCookies(key, value, path, maxAge);
50
+ }
51
+ function cookiesOnChange(callback) {
52
+ return getAdapter2().onChange(callback);
53
+ }
54
+
55
+ // src/lib/auth/check-auth.ts
56
+ var checkAuthImpl;
57
+ function configureCheckAuth(impl) {
58
+ checkAuthImpl = impl;
59
+ }
60
+ function isAuthenticated() {
61
+ if (!checkAuthImpl) {
62
+ throw new Error("Auth check not configured. Call configureAuthCheck() first.");
63
+ }
64
+ return checkAuthImpl();
65
+ }
3
66
 
4
67
  // src/lib/auth/auth-event.ts
5
68
  var listeners = /* @__PURE__ */ new Set();
@@ -23,42 +86,6 @@ function onNavigationEvent(listener) {
23
86
  return () => listeners2.delete(listener);
24
87
  }
25
88
 
26
- // src/lib/user/user.client.ts
27
- var cacheUserClient;
28
- var emptyCacheUserClient;
29
- function configureUserCacheCallbacks(cache, empty) {
30
- cacheUserClient = cache;
31
- emptyCacheUserClient = empty;
32
- }
33
- async function cacheUserDataClient() {
34
- if (!cacheUserClient) {
35
- throw new Error("User cache not configured. Call configUserCacheCallbacks() first.");
36
- }
37
- return await cacheUserClient();
38
- }
39
- function emptyUserCacheClient() {
40
- if (!emptyCacheUserClient) {
41
- throw new Error("User cache not configured. Call configUserCacheCallbacks() first.");
42
- }
43
- return emptyCacheUserClient();
44
- }
45
- async function fetchUsersClient() {
46
- const csrfToken = await fetchCsrfClient("get-all-users");
47
- return await goralysFetchClient("GET", buildApiUrl("users/all", { "csrf-token": csrfToken }));
48
- }
49
- async function fetchVirtualUsersClient() {
50
- const csrfToken = await fetchCsrfClient("get-virtual-users");
51
- return await goralysFetchClient("GET", buildApiUrl("users/virtual", { "csrf-token": csrfToken }));
52
- }
53
- async function fetchAdminsClient() {
54
- const csrfToken = await fetchCsrfClient("get-all-admins");
55
- return await goralysFetchClient("GET", buildApiUrl("admins/all", { "csrf-token": csrfToken }));
56
- }
57
- async function fetchVirtualAdminsClient() {
58
- const csrfToken = await fetchCsrfClient("get-virtual-admins");
59
- return await goralysFetchClient("GET", buildApiUrl("admins/virtual", { "csrf-token": csrfToken }));
60
- }
61
-
62
89
  // src/lib/auth/user-event.ts
63
90
  var listeners3 = /* @__PURE__ */ new Set();
64
91
  function emitUserEvent(event) {
@@ -93,18 +120,6 @@ var GoralysActionHandler = class {
93
120
  }
94
121
  };
95
122
 
96
- // src/lib/fetch/config.ts
97
- var config = null;
98
- function configureGoralysClient(cfg) {
99
- config = cfg;
100
- }
101
- function getGoralysClientConfig() {
102
- if (!config) {
103
- throw new Error("Goralys client not configured. Call configureGoralysClient() first.");
104
- }
105
- return config;
106
- }
107
-
108
123
  // src/lib/fetch/fetch.client.ts
109
124
  var actionHandler = new GoralysActionHandler();
110
125
  async function goralysFetchClient(method, input, payload, requestOptions) {
@@ -174,6 +189,54 @@ async function handleToastRequest(r, showToast, redirect = true, duration) {
174
189
  return false;
175
190
  }
176
191
 
192
+ // src/lib/user/user.client.ts
193
+ var cacheUserClient;
194
+ var emptyCacheUserClient;
195
+ function configureUserCacheCallbacks(cache, empty) {
196
+ cacheUserClient = cache;
197
+ emptyCacheUserClient = empty;
198
+ }
199
+ async function cacheUserDataClient() {
200
+ if (!cacheUserClient) {
201
+ throw new Error("User cache not configured. Call configUserCacheCallbacks() first.");
202
+ }
203
+ return await cacheUserClient();
204
+ }
205
+ function emptyUserCacheClient() {
206
+ if (!emptyCacheUserClient) {
207
+ throw new Error("User cache not configured. Call configUserCacheCallbacks() first.");
208
+ }
209
+ return emptyCacheUserClient();
210
+ }
211
+ async function fetchUsersClient() {
212
+ const csrfToken = await fetchCsrfClient("get-all-users");
213
+ return await goralysFetchClient("GET", buildApiUrl("users/all", { "csrf-token": csrfToken }));
214
+ }
215
+ async function fetchVirtualUsersClient() {
216
+ const csrfToken = await fetchCsrfClient("get-virtual-users");
217
+ return await goralysFetchClient("GET", buildApiUrl("users/virtual", { "csrf-token": csrfToken }));
218
+ }
219
+ async function fetchAdminsClient() {
220
+ const csrfToken = await fetchCsrfClient("get-all-admins");
221
+ return await goralysFetchClient("GET", buildApiUrl("admins/all", { "csrf-token": csrfToken }));
222
+ }
223
+ async function fetchVirtualAdminsClient() {
224
+ const csrfToken = await fetchCsrfClient("get-virtual-admins");
225
+ return await goralysFetchClient("GET", buildApiUrl("admins/virtual", { "csrf-token": csrfToken }));
226
+ }
227
+
228
+ // src/config.ts
229
+ function configGoralysCore(cfg) {
230
+ configureGoralysClient(cfg.client);
231
+ configureCookies(cfg.cookies);
232
+ configureStorage(cfg.storage);
233
+ configureCheckAuth(cfg.auth);
234
+ configureUserCacheCallbacks(cfg.cache.cache, cfg.cache.empty);
235
+ }
236
+
237
+ // src/hooks/useSubjects.ts
238
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
239
+
177
240
  // src/lib/subjects/subjects.client.ts
178
241
  async function fetchStudentSubjectsClient() {
179
242
  const csrfToken = await fetchCsrfClient("get-student-subjects");
@@ -447,57 +510,6 @@ Attention : les administrateurs ont acc\xE8s \xE0 toutes les fonctionnalit\xE9s
447
510
  }
448
511
  ];
449
512
 
450
- // src/lib/auth/check-auth.ts
451
- var checkAuthImpl;
452
- function configureCheckAuth(impl) {
453
- checkAuthImpl = impl;
454
- }
455
- function isAuthenticated() {
456
- if (!checkAuthImpl) {
457
- throw new Error("Auth check not configured. Call configureAuthCheck() first.");
458
- }
459
- return checkAuthImpl();
460
- }
461
-
462
- // src/lib/storage/storage-adapter.ts
463
- var adapter = null;
464
- function configureStorage(customAdapter) {
465
- adapter = customAdapter;
466
- }
467
- function getAdapter() {
468
- if (!adapter) {
469
- throw new Error("Storage not configured. Call configureStorage() first.");
470
- }
471
- return adapter;
472
- }
473
- async function storageGet(key) {
474
- return getAdapter().getItem(key);
475
- }
476
- async function storageSet(key, value) {
477
- await getAdapter().setItem(key, value);
478
- }
479
- async function storageRemove(key) {
480
- await getAdapter().removeItem(key);
481
- }
482
-
483
- // src/lib/storage/cookies-adapter.ts
484
- var adapter2 = null;
485
- function getAdapter2() {
486
- if (!adapter2) {
487
- throw new Error("Cookies not configured. Call configureCookies() first.");
488
- }
489
- return adapter2;
490
- }
491
- function cookiesGet(key) {
492
- return getAdapter2().getCookies(key);
493
- }
494
- function cookiesSet(key, value, path = "/", maxAge = 1.5 * 60) {
495
- getAdapter2().setCookies(key, value, path, maxAge);
496
- }
497
- function cookiesOnChange(callback) {
498
- return getAdapter2().onChange(callback);
499
- }
500
-
501
513
  // src/hooks/useSubjects.ts
502
514
  function useSubjects(role, showToast) {
503
515
  const [subjects, setSubjects] = useState(null);
@@ -887,6 +899,7 @@ export {
887
899
  buildApiUrl,
888
900
  buildArray,
889
901
  cacheUserDataClient,
902
+ configGoralysCore,
890
903
  configureCheckAuth,
891
904
  configureGoralysClient,
892
905
  configureStorage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goralys/core",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "license": "LGPL-2.1-or-later",
5
5
  "description": "Shared business logic for the Goralys web application",
6
6
  "repository": {