@goralys/core 1.0.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.
@@ -0,0 +1,259 @@
1
+ type UserRole = {
2
+ role: "admin" | "teacher" | "student" | "none";
3
+ };
4
+ declare const USER_ROLES: readonly ["admin", "teacher", "student", "none"];
5
+ type UserData = {
6
+ username: string;
7
+ full_name: string;
8
+ role: UserRole["role"];
9
+ public_id: string;
10
+ email: string | null | undefined;
11
+ };
12
+ type User = {
13
+ username: string;
14
+ publicId: string;
15
+ fullName: string;
16
+ role: UserRole["role"];
17
+ };
18
+ type AuthEvent = "unauthenticated" | "expired";
19
+ type UserEvent = "login" | "logout" | "register";
20
+ type ConfirmOptions = {
21
+ title: string;
22
+ message: string;
23
+ };
24
+
25
+ type Toast = {
26
+ type: "error" | "warning" | "info" | "success";
27
+ title: string;
28
+ message: string;
29
+ expires?: number;
30
+ };
31
+ type ToastFn = (toast: Toast, duration?: number) => void;
32
+
33
+ type SubjectStatus = "not_submitted" | "submitted" | "rejected" | "approved";
34
+ declare const SubjectStatusConfig: Record<SubjectStatus, {
35
+ label: string;
36
+ helper: Record<UserRole["role"], string>;
37
+ }>;
38
+ type Subject = {
39
+ comment: string;
40
+ hasDraft: boolean;
41
+ lastRejected?: string;
42
+ status: SubjectStatus;
43
+ student: string;
44
+ studentToken: string;
45
+ subject: string;
46
+ teacher: string;
47
+ teacherToken: string;
48
+ topic: string;
49
+ interdisciplinary: boolean;
50
+ };
51
+ declare const searchFields: {
52
+ readonly all: "Tout";
53
+ readonly student: "Élèves";
54
+ readonly teacher: "Professeur";
55
+ readonly topic: "Matière";
56
+ };
57
+ type SubjectsSearchField = keyof typeof searchFields;
58
+ type DraftModalResult = {
59
+ type: "withDraft";
60
+ file: File | null;
61
+ } | {
62
+ type: "withoutDraft";
63
+ } | {
64
+ type: "closed";
65
+ };
66
+ declare const getStatusLabel: (status: SubjectStatus) => string;
67
+ declare const getStatusHelper: (status: SubjectStatus, role: UserRole["role"]) => string;
68
+
69
+ declare function useSubjects(role: UserRole["role"], showToast: ToastFn): {
70
+ subjects: Subject[] | null;
71
+ refetch: () => Promise<undefined | void>;
72
+ syncKey: string;
73
+ };
74
+
75
+ interface PHPDateTime {
76
+ date: string;
77
+ timezone_type: number;
78
+ timezone: string;
79
+ }
80
+ type CookieValue = string | boolean | number | null | undefined;
81
+
82
+ declare const reasonsConfig: {
83
+ readonly "password-forgot": {
84
+ readonly label: "Mot de passe oublié";
85
+ };
86
+ readonly "subject-error": {
87
+ readonly label: "Question envoyée/validée/rejetée par erreur";
88
+ };
89
+ readonly "personal-info-error": {
90
+ readonly label: "Informations personnelles erronées (ex: Nom/Prénom)";
91
+ };
92
+ readonly other: {
93
+ readonly label: "Autre (précisez)";
94
+ };
95
+ };
96
+ type SupportReasons = keyof typeof reasonsConfig;
97
+ type SupportTicket = {
98
+ id: number;
99
+ reason: SupportReasons;
100
+ opener: string;
101
+ openerToken: string;
102
+ message: string;
103
+ createdAt: PHPDateTime;
104
+ };
105
+
106
+ declare function useSupportTickets(showToast: ToastFn): {
107
+ supportTickets: SupportTicket[] | null;
108
+ refetch: () => Promise<undefined | void>;
109
+ syncKey: string;
110
+ };
111
+
112
+ interface UserFetchResult {
113
+ users: User[] | null;
114
+ refetch: () => Promise<void | undefined>;
115
+ syncKey: string;
116
+ }
117
+ declare function useUsers(showToast: ToastFn): UserFetchResult;
118
+ declare function useVirtualUsers(showToast: ToastFn): UserFetchResult;
119
+ declare function useAdmins(showToast: ToastFn): UserFetchResult;
120
+ declare function useVirtualAdmins(showToast: ToastFn): UserFetchResult;
121
+
122
+ declare const LOGO_PATH: {
123
+ main: string;
124
+ eye: string;
125
+ };
126
+
127
+ declare const PERSISTANT_COOKIES: string[];
128
+ declare const PERSISTANT_LOCALS: string[];
129
+ declare const USERNAME_KEY = "username";
130
+ declare const ROLE_KEY = "user-role";
131
+ declare const FULL_NAME_KEY = "full-name";
132
+ declare const EMAIL_KEY = "email";
133
+ declare const PUB_ID_KEY = "public-id";
134
+ declare const SUBJECT_SYNC_RADIX = "subjects-synced-";
135
+ declare const SUBJECT_CACHE_RADIX = "subjects-cache-";
136
+ declare const USER_CACHE_RADIX = "users-cache-";
137
+ declare const USER_SYNC_RADIX = "users-synced-";
138
+ declare const ADMIN_CACHE_RADIX = "admins-cache-";
139
+ declare const ADMIN_SYNC_RADIX = "admins-synced-";
140
+ declare const SUPPORT_TICKET_CACHE = "support-tickets-cache";
141
+ declare const SUPPORT_TICKET_SYNC = "support-tickets-synced";
142
+ declare const SUBJECT_SYNCS: Record<UserRole["role"], string>;
143
+ declare const SUBJECT_CACHES: Record<UserRole["role"], string>;
144
+ type UserType = {
145
+ type: "users-virtual" | "users-real" | "admins-virtual" | "admins-real";
146
+ };
147
+ declare const USER_SYNCS: Record<UserType["type"], string>;
148
+ declare const USER_CACHES: Record<UserType["type"], string>;
149
+ type Item = {
150
+ q: string;
151
+ a: string;
152
+ };
153
+ type Detail = {
154
+ title: string;
155
+ description: string;
156
+ format: string;
157
+ };
158
+ type Subsection = {
159
+ id: string;
160
+ title: string;
161
+ items: Item[];
162
+ details?: Detail[];
163
+ };
164
+ type Section = {
165
+ id: string;
166
+ title: string;
167
+ subsections: Subsection[];
168
+ };
169
+ declare const helpContent: Section[];
170
+
171
+ declare function setCookiesExpire(duration: number): void;
172
+
173
+ declare function isFoolsDay(): boolean;
174
+
175
+ declare function buildArray<T>(...items: (T | false | null | undefined)[]): T[];
176
+ declare const parsePhpDateTime: (phpDate: PHPDateTime) => string;
177
+
178
+ declare function emitAuthEvent(event: AuthEvent): void;
179
+ declare function onAuthEvent(callback: (event: AuthEvent) => void): () => void;
180
+
181
+ type CheckAuthImpl = () => boolean;
182
+ declare function configureCheckAuth(impl: CheckAuthImpl): void;
183
+ declare function isAuthenticated(): boolean;
184
+
185
+ declare function emitUserEvent(event: UserEvent): void;
186
+ declare function onUserEvent(callback: (event: UserEvent) => void): () => void;
187
+
188
+ interface GoralysClientConfig {
189
+ apiDomain: string;
190
+ }
191
+ declare function configureGoralysClient(cfg: GoralysClientConfig): void;
192
+ declare function getGoralysClientConfig(): GoralysClientConfig;
193
+
194
+ /**
195
+ * This filed is a slightly modified version of Next.js own http method declaration.
196
+ */
197
+ declare const HTTP_METHODS: readonly ["GET", "HEAD", "OPTIONS", "POST", "PUT", "DELETE", "PATCH", "BREW"];
198
+ type HttpMethod = (typeof HTTP_METHODS)[number];
199
+ /**
200
+ * Checks to see if the passed string is an HTTP method. Note that this is case-sensitive.
201
+ *
202
+ * @param maybeMethod the string that may be an HTTP method.
203
+ * @returns true if the string is an HTTP method.
204
+ */
205
+ declare function isHTTPMethod(maybeMethod: string): maybeMethod is HttpMethod;
206
+
207
+ declare function goralysFetchClient(method: HttpMethod, input: string | URL | Request, payload?: Record<string, string | number | boolean | null> | FormData, requestOptions?: RequestInit): Promise<Response>;
208
+ declare function fetchCsrfClient(formId: string): Promise<string | null>;
209
+ declare function buildApiUrl(endpoint: string, params: Record<string, string | null>, domain?: boolean): string;
210
+ declare function handleToastRequest(r: Response, showToast: (payload: Toast, duration?: number) => void, redirect?: boolean, duration?: number): Promise<boolean>;
211
+
212
+ declare class GoralysActionHandler {
213
+ handle: (r: Response) => Promise<void>;
214
+ private onLogin;
215
+ }
216
+
217
+ type NavigationEvent = {
218
+ type: "teapot";
219
+ toastType: string;
220
+ toastTitle: string;
221
+ toastMessage: string;
222
+ } | {
223
+ type: "redirect";
224
+ url: string;
225
+ };
226
+ type Listener = (event: NavigationEvent) => void;
227
+ declare function emitNavigationEvent(event: NavigationEvent): void;
228
+ declare function onNavigationEvent(listener: Listener): () => void;
229
+
230
+ interface StorageAdapter {
231
+ getItem(key: string): string | null | Promise<string | null>;
232
+ setItem(key: string, value: string): void | Promise<void>;
233
+ removeItem(key: string): void | Promise<void>;
234
+ }
235
+ declare function configureStorage(customAdapter: StorageAdapter): void;
236
+ declare function storageGet(key: string): Promise<string | null>;
237
+ declare function storageSet(key: string, value: string): Promise<void>;
238
+ declare function storageRemove(key: string): Promise<void>;
239
+
240
+ /**
241
+ * Returns the full name of a subject based on its short name or an alias.
242
+ * It searches for the first subject in {@link SUBJECTS} whose short name starts with the given string.
243
+ *
244
+ * @param short The short name, alias, or prefix to search for.
245
+ * @returns The full subject name if a match is found; otherwise, returns the input string itself.
246
+ */
247
+ declare function getLongFromShort(short: string): string;
248
+
249
+ declare function fetchSubjectsForRoleClient(role: UserRole): Promise<Response | null>;
250
+
251
+ declare function configUserCacheCallbacks(cache: () => Promise<void>, empty: () => void): void;
252
+ declare function cacheUserDataClient(): Promise<void>;
253
+ declare function emptyUserCacheClient(): void;
254
+ declare function fetchUsersClient(): Promise<Response>;
255
+ declare function fetchVirtualUsersClient(): Promise<Response>;
256
+ declare function fetchAdminsClient(): Promise<Response>;
257
+ declare function fetchVirtualAdminsClient(): Promise<Response>;
258
+
259
+ export { ADMIN_CACHE_RADIX, ADMIN_SYNC_RADIX, type AuthEvent, 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, configUserCacheCallbacks, configureCheckAuth, configureGoralysClient, configureStorage, 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, setCookiesExpire, storageGet, storageRemove, storageSet, useAdmins, useSubjects, useSupportTickets, useUsers, useVirtualAdmins, useVirtualUsers };