reslib 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.
- package/README.md +298 -0
- package/build/auth/index.d.ts +2034 -0
- package/build/auth/index.js +5 -0
- package/build/auth/types.d.ts +465 -0
- package/build/auth/types.js +1 -0
- package/build/countries/countries.d.ts +1454 -0
- package/build/countries/countries.js +1 -0
- package/build/countries/index.d.ts +159 -0
- package/build/countries/index.js +5 -0
- package/build/countries/types.d.ts +65 -0
- package/build/countries/types.js +1 -0
- package/build/currency/currencies.d.ts +8 -0
- package/build/currency/currencies.js +1 -0
- package/build/currency/index.d.ts +51 -0
- package/build/currency/index.js +5 -0
- package/build/currency/session.d.ts +23 -0
- package/build/currency/session.js +5 -0
- package/build/currency/types.d.ts +1039 -0
- package/build/currency/types.js +1 -0
- package/build/currency/utils.d.ts +25 -0
- package/build/currency/utils.js +1 -0
- package/build/i18n/index.d.ts +640 -0
- package/build/i18n/index.js +5 -0
- package/build/inputFormatter/index.d.ts +396 -0
- package/build/inputFormatter/index.js +5 -0
- package/build/inputFormatter/types.d.ts +544 -0
- package/build/inputFormatter/types.js +1 -0
- package/build/logger/index.d.ts +235 -0
- package/build/logger/index.js +5 -0
- package/build/observable/index.d.ts +329 -0
- package/build/observable/index.js +1 -0
- package/build/platform/index.d.ts +32 -0
- package/build/platform/index.js +1 -0
- package/build/resources/ResourcePaginationHelper.d.ts +537 -0
- package/build/resources/ResourcePaginationHelper.js +2 -0
- package/build/resources/decorators/create.decorator.d.ts +20 -0
- package/build/resources/decorators/create.decorator.js +1 -0
- package/build/resources/decorators/index.d.ts +41 -0
- package/build/resources/decorators/index.js +1 -0
- package/build/resources/fields/index.d.ts +33 -0
- package/build/resources/fields/index.js +1 -0
- package/build/resources/filters.d.ts +62 -0
- package/build/resources/filters.js +1 -0
- package/build/resources/index.d.ts +854 -0
- package/build/resources/index.js +6 -0
- package/build/resources/types/filters.d.ts +508 -0
- package/build/resources/types/filters.js +1 -0
- package/build/resources/types/index.d.ts +4138 -0
- package/build/resources/types/index.js +1 -0
- package/build/session/index.d.ts +1474 -0
- package/build/session/index.js +1 -0
- package/build/translations/auth.en.d.ts +3 -0
- package/build/translations/auth.en.js +1 -0
- package/build/translations/countries.en.d.ts +6 -0
- package/build/translations/countries.en.js +1 -0
- package/build/translations/currencies.en.d.ts +5 -0
- package/build/translations/currencies.en.js +1 -0
- package/build/translations/date.en.d.ts +19 -0
- package/build/translations/date.en.js +1 -0
- package/build/translations/index.d.ts +1583 -0
- package/build/translations/index.js +5 -0
- package/build/translations/resources.en.d.ts +6 -0
- package/build/translations/resources.en.js +1 -0
- package/build/translations/validator.en.d.ts +104 -0
- package/build/translations/validator.en.js +5 -0
- package/build/types/date.d.ts +44 -0
- package/build/types/date.js +1 -0
- package/build/types/dictionary.d.ts +29 -0
- package/build/types/dictionary.js +1 -0
- package/build/types/i18n.d.ts +121 -0
- package/build/types/i18n.js +1 -0
- package/build/types/index.d.ts +145 -0
- package/build/types/index.js +1 -0
- package/build/utils/areEquals.d.ts +19 -0
- package/build/utils/areEquals.js +1 -0
- package/build/utils/date/dateHelper.d.ts +371 -0
- package/build/utils/date/dateHelper.js +5 -0
- package/build/utils/date/index.d.ts +212 -0
- package/build/utils/date/index.js +5 -0
- package/build/utils/date/isDateObj.d.ts +14 -0
- package/build/utils/date/isDateObj.js +1 -0
- package/build/utils/debounce.d.ts +52 -0
- package/build/utils/debounce.js +1 -0
- package/build/utils/defaultArray.d.ts +18 -0
- package/build/utils/defaultArray.js +1 -0
- package/build/utils/defaultBool.d.ts +14 -0
- package/build/utils/defaultBool.js +1 -0
- package/build/utils/defaultStr.d.ts +17 -0
- package/build/utils/defaultStr.js +1 -0
- package/build/utils/defaultVal.d.ts +18 -0
- package/build/utils/defaultVal.js +1 -0
- package/build/utils/dom/index.d.ts +65 -0
- package/build/utils/dom/index.js +1 -0
- package/build/utils/dom/isDOMElement.d.ts +11 -0
- package/build/utils/dom/isDOMElement.js +1 -0
- package/build/utils/file/index.d.ts +26 -0
- package/build/utils/file/index.js +1 -0
- package/build/utils/global.d.ts +53 -0
- package/build/utils/global.js +1 -0
- package/build/utils/image.d.ts +56 -0
- package/build/utils/image.js +1 -0
- package/build/utils/index.d.ts +39 -0
- package/build/utils/index.js +6 -0
- package/build/utils/interpolate.d.ts +105 -0
- package/build/utils/interpolate.js +1 -0
- package/build/utils/isEmail.d.ts +57 -0
- package/build/utils/isEmail.js +1 -0
- package/build/utils/isEmpty.d.ts +18 -0
- package/build/utils/isEmpty.js +1 -0
- package/build/utils/isNonNullString.d.ts +17 -0
- package/build/utils/isNonNullString.js +1 -0
- package/build/utils/isNullable.d.ts +7 -0
- package/build/utils/isNullable.js +1 -0
- package/build/utils/isNumber.d.ts +36 -0
- package/build/utils/isNumber.js +1 -0
- package/build/utils/isPrimitive.d.ts +16 -0
- package/build/utils/isPrimitive.js +1 -0
- package/build/utils/isPromise.d.ts +14 -0
- package/build/utils/isPromise.js +1 -0
- package/build/utils/isRegex.d.ts +15 -0
- package/build/utils/isRegex.js +1 -0
- package/build/utils/isTime.d.ts +18 -0
- package/build/utils/isTime.js +1 -0
- package/build/utils/json.d.ts +224 -0
- package/build/utils/json.js +1 -0
- package/build/utils/numbers.d.ts +148 -0
- package/build/utils/numbers.js +5 -0
- package/build/utils/object.d.ts +567 -0
- package/build/utils/object.js +1 -0
- package/build/utils/sort.d.ts +67 -0
- package/build/utils/sort.js +1 -0
- package/build/utils/string.d.ts +165 -0
- package/build/utils/string.js +1 -0
- package/build/utils/stringify.d.ts +23 -0
- package/build/utils/stringify.js +1 -0
- package/build/utils/uniqid.d.ts +18 -0
- package/build/utils/uniqid.js +1 -0
- package/build/utils/uri/index.d.ts +333 -0
- package/build/utils/uri/index.js +2 -0
- package/build/validator/index.d.ts +4 -0
- package/build/validator/index.js +6 -0
- package/build/validator/rules/array.d.ts +848 -0
- package/build/validator/rules/array.js +5 -0
- package/build/validator/rules/boolean.d.ts +87 -0
- package/build/validator/rules/boolean.js +5 -0
- package/build/validator/rules/date.d.ts +551 -0
- package/build/validator/rules/date.js +5 -0
- package/build/validator/rules/default.d.ts +367 -0
- package/build/validator/rules/default.js +5 -0
- package/build/validator/rules/enum.d.ts +155 -0
- package/build/validator/rules/enum.js +5 -0
- package/build/validator/rules/file.d.ts +356 -0
- package/build/validator/rules/file.js +5 -0
- package/build/validator/rules/format.d.ts +2825 -0
- package/build/validator/rules/format.js +6 -0
- package/build/validator/rules/index.d.ts +16 -0
- package/build/validator/rules/index.js +6 -0
- package/build/validator/rules/multiRules.d.ts +475 -0
- package/build/validator/rules/multiRules.js +5 -0
- package/build/validator/rules/numeric.d.ts +1135 -0
- package/build/validator/rules/numeric.js +5 -0
- package/build/validator/rules/string.d.ts +504 -0
- package/build/validator/rules/string.js +5 -0
- package/build/validator/rules/target.d.ts +137 -0
- package/build/validator/rules/target.js +5 -0
- package/build/validator/rules/utils.d.ts +1 -0
- package/build/validator/rules/utils.js +1 -0
- package/build/validator/rulesMarkers.d.ts +11 -0
- package/build/validator/rulesMarkers.js +1 -0
- package/build/validator/types.d.ts +2906 -0
- package/build/validator/types.js +1 -0
- package/build/validator/validator.d.ts +3692 -0
- package/build/validator/validator.js +5 -0
- package/lib/cjs/auth.js +1 -0
- package/lib/cjs/countries.js +1 -0
- package/lib/cjs/currency.js +1 -0
- package/lib/cjs/i18n.js +1 -0
- package/lib/cjs/inputFormatter.js +1 -0
- package/lib/cjs/logger.js +1 -0
- package/lib/cjs/observable.js +1 -0
- package/lib/cjs/platform.js +1 -0
- package/lib/cjs/resources.js +1 -0
- package/lib/cjs/session.js +1 -0
- package/lib/cjs/types.js +1 -0
- package/lib/cjs/utils.js +1 -0
- package/lib/cjs/validator.js +1 -0
- package/lib/esm/auth.mjs +1 -0
- package/lib/esm/countries.mjs +1 -0
- package/lib/esm/currency.mjs +1 -0
- package/lib/esm/i18n.mjs +1 -0
- package/lib/esm/inputFormatter.mjs +1 -0
- package/lib/esm/logger.mjs +1 -0
- package/lib/esm/observable.mjs +1 -0
- package/lib/esm/platform.mjs +1 -0
- package/lib/esm/resources.mjs +1 -0
- package/lib/esm/session.mjs +1 -0
- package/lib/esm/types.mjs +1 -0
- package/lib/esm/utils.mjs +1 -0
- package/lib/esm/validator.mjs +1 -0
- package/package.json +244 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import { ResourceActionName, ResourceActionTuple, ResourceName } from '../resources/types';
|
|
2
|
+
import { Dictionary } from '../types/dictionary';
|
|
3
|
+
/**
|
|
4
|
+
* @interface AuthUser
|
|
5
|
+
* Represents an authenticated user in the application.
|
|
6
|
+
*
|
|
7
|
+
* The `AuthUser` interface defines the structure for an authenticated
|
|
8
|
+
* user object, which includes core user identification, optional session
|
|
9
|
+
* tracking, permissions mapping, authentication token, and role assignments.
|
|
10
|
+
* This interface extends `Dictionary` to allow for additional custom properties.
|
|
11
|
+
*
|
|
12
|
+
* ### Properties
|
|
13
|
+
*
|
|
14
|
+
* - `id` (string | number): A unique identifier for the user. This
|
|
15
|
+
* can be either a string or a number, depending on the implementation.
|
|
16
|
+
*
|
|
17
|
+
* - `authSessionCreatedAt` (number, optional): An optional property
|
|
18
|
+
* that stores the timestamp (in milliseconds) of when the
|
|
19
|
+
* authentication session was created. This can be useful for
|
|
20
|
+
* tracking session duration or expiration.
|
|
21
|
+
*
|
|
22
|
+
* - `perms` (AuthPerms, optional): An optional property that maps
|
|
23
|
+
* resource names to an array of actions that the user is permitted
|
|
24
|
+
* to perform on those resources. This allows for fine-grained control
|
|
25
|
+
* over user permissions within the application.
|
|
26
|
+
*
|
|
27
|
+
* - `token` (string, optional): An optional authentication token
|
|
28
|
+
* associated with the user, typically used for API authentication
|
|
29
|
+
* or session management.
|
|
30
|
+
*
|
|
31
|
+
* - `roles` (AuthRole[], optional): An optional array of roles assigned
|
|
32
|
+
* to the user, defining their authorization level and capabilities.
|
|
33
|
+
*
|
|
34
|
+
* ### Example Usage
|
|
35
|
+
*
|
|
36
|
+
* Here is an example of how the `AuthUser` interface can be used:
|
|
37
|
+
*
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const user: AuthUser = {
|
|
40
|
+
* id: "user123",
|
|
41
|
+
* authSessionCreatedAt: Date.now(),
|
|
42
|
+
* token: "jwt-token-here",
|
|
43
|
+
* perms: {
|
|
44
|
+
* documents: ["read", "create", "update"],
|
|
45
|
+
* users: ["read", "delete"]
|
|
46
|
+
* },
|
|
47
|
+
* roles: [
|
|
48
|
+
* { name: "admin", perms: { documents: ["read", "create", "update"] } }
|
|
49
|
+
* ]
|
|
50
|
+
* };
|
|
51
|
+
*
|
|
52
|
+
* // Function to check if a user has permission to perform an action
|
|
53
|
+
* function hasPermission(user: AuthUser, resource: ResourceName, action: ResourceActionName): boolean {
|
|
54
|
+
* return user.perms?.[resource]?.includes(action) ?? false;
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* // Example of checking permissions
|
|
58
|
+
* const canReadDocuments = hasPermission(user, "documents", "read"); // true
|
|
59
|
+
* const canDeleteUsers = hasPermission(user, "users", "delete"); // true
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* In this example, the `AuthUser` interface is used to define a user
|
|
63
|
+
* object with an ID, session creation timestamp, authentication token,
|
|
64
|
+
* permissions map, and roles. The `hasPermission` function checks if
|
|
65
|
+
* the user has the specified permission for a given resource.
|
|
66
|
+
*
|
|
67
|
+
* @see {@link ResourceName} for the `ResourceName` type.
|
|
68
|
+
* @see {@link ResourceActionName} for the `ResourceActionName` type.
|
|
69
|
+
* @see {@link AuthPerms} for the `AuthPerms` type.
|
|
70
|
+
* @see {@link AuthRole} for the `AuthRole` interface.
|
|
71
|
+
*/
|
|
72
|
+
export interface AuthUser extends Dictionary {
|
|
73
|
+
id: string | number;
|
|
74
|
+
authSessionCreatedAt?: number;
|
|
75
|
+
perms?: AuthPerms;
|
|
76
|
+
/**
|
|
77
|
+
* The authentication token associated with the user.
|
|
78
|
+
*/
|
|
79
|
+
token?: string;
|
|
80
|
+
roles?: AuthRole[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* @interface AuthRole
|
|
84
|
+
* Represents an authorization role with associated permissions.
|
|
85
|
+
*
|
|
86
|
+
* The `AuthRole` interface defines the structure of an authorization role,
|
|
87
|
+
* which consists of a unique name identifier and a comprehensive set of
|
|
88
|
+
* permissions that define what actions the role can perform on various resources.
|
|
89
|
+
* This interface extends `Record<string, any>` to allow for additional custom properties.
|
|
90
|
+
*
|
|
91
|
+
* ### Properties
|
|
92
|
+
*
|
|
93
|
+
* - `name` (string): The unique identifier of the authorization role.
|
|
94
|
+
* This serves as the primary key for role identification and should be
|
|
95
|
+
* descriptive and unique within the application (e.g., 'admin', 'moderator', 'user').
|
|
96
|
+
*
|
|
97
|
+
* - `perms` (AuthPerms): The permissions associated with this role.
|
|
98
|
+
* This property defines what actions the role can perform on different resources,
|
|
99
|
+
* enabling fine-grained access control based on role assignments.
|
|
100
|
+
*
|
|
101
|
+
* ### Example Usage
|
|
102
|
+
*
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const adminRole: AuthRole = {
|
|
105
|
+
* name: 'admin',
|
|
106
|
+
* perms: {
|
|
107
|
+
* documents: ["read", "create", "update", "delete"],
|
|
108
|
+
* users: ["read", "create", "update", "delete"],
|
|
109
|
+
* posts: ["read", "create", "update", "delete"],
|
|
110
|
+
* }
|
|
111
|
+
* };
|
|
112
|
+
*
|
|
113
|
+
* const moderatorRole: AuthRole = {
|
|
114
|
+
* name: 'moderator',
|
|
115
|
+
* perms: {
|
|
116
|
+
* documents: ["read", "update"],
|
|
117
|
+
* posts: ["read", "create", "update"],
|
|
118
|
+
* }
|
|
119
|
+
* };
|
|
120
|
+
*
|
|
121
|
+
* // Function to check if a role has permission for an action
|
|
122
|
+
* function roleHasPermission(role: AuthRole, resource: ResourceName, action: ResourceActionName): boolean {
|
|
123
|
+
* return role.perms?.[resource]?.includes(action) ?? false;
|
|
124
|
+
* }
|
|
125
|
+
*
|
|
126
|
+
* // Example usage
|
|
127
|
+
* const canAdminDeleteUsers = roleHasPermission(adminRole, "users", "delete"); // true
|
|
128
|
+
* const canModeratorDeleteUsers = roleHasPermission(moderatorRole, "users", "delete"); // false
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @see {@link AuthPerms} for the permissions structure.
|
|
132
|
+
* @see {@link AuthUser} for how roles are assigned to users.
|
|
133
|
+
*/
|
|
134
|
+
export interface AuthRole extends Record<string, any> {
|
|
135
|
+
/**
|
|
136
|
+
* The name of the authorization role.
|
|
137
|
+
*
|
|
138
|
+
* @description This property represents the unique identifier of the role.
|
|
139
|
+
* @example 'admin', 'moderator', 'user'
|
|
140
|
+
*/
|
|
141
|
+
name: string;
|
|
142
|
+
/**
|
|
143
|
+
* The set of permissions associated with the role.
|
|
144
|
+
*
|
|
145
|
+
* @description This property represents the permissions that are granted to the role.
|
|
146
|
+
* @see {@link AuthPerms} for the `AuthPerms` type.
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const perms: AuthPerms = {
|
|
150
|
+
* // permissions for the role
|
|
151
|
+
* "documents": ["read", "create", "update"],
|
|
152
|
+
* "users": ["read", "delete"],
|
|
153
|
+
* "posts": ["read", "create"],
|
|
154
|
+
* };
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
perms: AuthPerms;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @interface AuthSessionStorage
|
|
161
|
+
* Interface for managing authentication session storage.
|
|
162
|
+
*
|
|
163
|
+
* The `AuthSessionStorage` interface defines methods for storing and
|
|
164
|
+
* retrieving session data associated with authenticated users. This
|
|
165
|
+
* interface provides a structured way to manage session data, ensuring
|
|
166
|
+
* that it can be easily accessed and manipulated.
|
|
167
|
+
*
|
|
168
|
+
* ### Methods
|
|
169
|
+
*
|
|
170
|
+
* - `get(key?: string): any`: Retrieves the value of the session
|
|
171
|
+
* associated with the specified key. If no key is provided, it
|
|
172
|
+
* returns the entire session value.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} key - The key of the value to retrieve.
|
|
175
|
+
* @returns {any} The value associated with the specified key, or
|
|
176
|
+
* undefined if the key does not exist.
|
|
177
|
+
*
|
|
178
|
+
* - `set(key?: string | Dictionary, value?: any): any`: Persists a value
|
|
179
|
+
* in the session storage. This can either be a single key-value
|
|
180
|
+
* pair or an object containing multiple session data.
|
|
181
|
+
*
|
|
182
|
+
* @param {string | Dictionary} key - The key of the value to persist, or
|
|
183
|
+
* an object containing session data.
|
|
184
|
+
* @param {any} value - The value to persist. This can be of any type.
|
|
185
|
+
*
|
|
186
|
+
* - `getData(): Dictionary`: Retrieves all session data associated with
|
|
187
|
+
* the session name defined in the `sessionName` property.
|
|
188
|
+
*
|
|
189
|
+
* @returns {Dictionary} An object containing all session data.
|
|
190
|
+
*
|
|
191
|
+
* - `getKey(): string`: Returns the key associated with the session
|
|
192
|
+
* name defined in the `sessionName` property.
|
|
193
|
+
*
|
|
194
|
+
* @returns {string} The session key.
|
|
195
|
+
*
|
|
196
|
+
* ### Properties
|
|
197
|
+
*
|
|
198
|
+
* - `sessionName` (string, optional): The name of the session used by
|
|
199
|
+
* the session manager. This can be used to identify the session
|
|
200
|
+
* context in which the storage operates.
|
|
201
|
+
*/
|
|
202
|
+
export interface AuthSessionStorage {
|
|
203
|
+
/**
|
|
204
|
+
* Retrieves the value of the session associated with the specified key.
|
|
205
|
+
*
|
|
206
|
+
* @param {string} key - The key of the value to retrieve.
|
|
207
|
+
* @returns {any} The value associated with the specified key, or undefined if the key does not exist.
|
|
208
|
+
*/
|
|
209
|
+
get: (key?: string) => any;
|
|
210
|
+
/**
|
|
211
|
+
* Persists a value in the session storage.
|
|
212
|
+
*
|
|
213
|
+
* @param {string | Dictionary} key - The key of the value to persist, or an object containing session data.
|
|
214
|
+
* @param {any} value - The value to persist. This can be of any type.
|
|
215
|
+
* @returns {any} The result of the persistence operation.
|
|
216
|
+
*/
|
|
217
|
+
set: (key?: string | Dictionary, value?: any) => any;
|
|
218
|
+
/**
|
|
219
|
+
* Retrieves all session data associated with the session name.
|
|
220
|
+
*
|
|
221
|
+
* @returns {Dictionary} An object containing all session data.
|
|
222
|
+
*/
|
|
223
|
+
getData: () => Dictionary;
|
|
224
|
+
/**
|
|
225
|
+
* Returns the key associated with the session name.
|
|
226
|
+
*
|
|
227
|
+
* @returns {string} The session key.
|
|
228
|
+
*/
|
|
229
|
+
getKey: () => string;
|
|
230
|
+
/**
|
|
231
|
+
* The name of the session used by the session manager.
|
|
232
|
+
*/
|
|
233
|
+
sessionName?: string;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* @type AuthPerm
|
|
237
|
+
* Defines a flexible permission type for evaluating user access to resources.
|
|
238
|
+
*
|
|
239
|
+
* The `AuthPerm` type supports multiple formats for defining permissions,
|
|
240
|
+
* allowing both simple boolean checks and complex resource-action based permissions.
|
|
241
|
+
* This flexibility enables various authorization patterns from simple role-based
|
|
242
|
+
* access control to dynamic, context-aware permission evaluation.
|
|
243
|
+
*
|
|
244
|
+
* @template TResourceName - The specific resource name type. Defaults to `ResourceName`.
|
|
245
|
+
*
|
|
246
|
+
* ### Permission Formats
|
|
247
|
+
*
|
|
248
|
+
* - **Function**: `(user: AuthUser) => boolean` - Dynamic permission check based on user context
|
|
249
|
+
* - **Boolean**: `false` - Explicit denial of access
|
|
250
|
+
* - **Resource Action Tuple**: `[TResourceName, ResourceActionName<TResourceName>]` - Specific resource-action permission
|
|
251
|
+
* - **Resource Action Tuples Array**: `Array<[TResourceName, ResourceActionName<TResourceName>]>` - Multiple resource-action permissions
|
|
252
|
+
*
|
|
253
|
+
* ### Example Usage
|
|
254
|
+
*
|
|
255
|
+
* ```typescript
|
|
256
|
+
* // Dynamic permission using a function
|
|
257
|
+
* const adminOnly: AuthPerm = (user: AuthUser) => {
|
|
258
|
+
* return user.roles?.some(role => role.name === 'admin') ?? false;
|
|
259
|
+
* };
|
|
260
|
+
*
|
|
261
|
+
* // Explicit denial
|
|
262
|
+
* const noAccess: AuthPerm = false;
|
|
263
|
+
*
|
|
264
|
+
* // Single resource-action permission
|
|
265
|
+
* const readUsers: AuthPerm = ["users", "read"];
|
|
266
|
+
*
|
|
267
|
+
* // Multiple resource-action permissions
|
|
268
|
+
* const manageUsers: AuthPerm = [
|
|
269
|
+
* ["users", "read"],
|
|
270
|
+
* ["users", "create"],
|
|
271
|
+
* ["users", "update"]
|
|
272
|
+
* ];
|
|
273
|
+
*
|
|
274
|
+
* // Function to check permissions
|
|
275
|
+
* function hasPermission(user: AuthUser, perm: AuthPerm): boolean {
|
|
276
|
+
* if (typeof perm === 'function') {
|
|
277
|
+
* return perm(user);
|
|
278
|
+
* }
|
|
279
|
+
* if (perm === false) {
|
|
280
|
+
* return false;
|
|
281
|
+
* }
|
|
282
|
+
* // Handle tuple/array formats (implementation would check against user perms)
|
|
283
|
+
* return true; // Simplified for example
|
|
284
|
+
* }
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* @see {@link AuthUser} for the user context passed to function permissions.
|
|
288
|
+
* @see {@link ResourceActionTuple} for the tuple format specification.
|
|
289
|
+
* @typedef {((user: AuthUser) => boolean) | false | ResourceActionTuple<TResourceName> | ResourceActionTuple<TResourceName>[]} AuthPerm
|
|
290
|
+
*/
|
|
291
|
+
export type AuthPerm<TResourceName extends ResourceName = ResourceName> = ((user: AuthUser) => boolean) | false | ResourceActionTuple<TResourceName> | ResourceActionTuple<TResourceName>[];
|
|
292
|
+
/**
|
|
293
|
+
* Represents a mapping of authentication permissions for resources.
|
|
294
|
+
* The `AuthPerms` type defines a structure that maps resource names
|
|
295
|
+
* to an array of actions that can be performed on those resources.
|
|
296
|
+
* This type is useful for managing user permissions in an application,
|
|
297
|
+
* allowing for fine-grained control over what actions users can take
|
|
298
|
+
* on various resources.
|
|
299
|
+
*
|
|
300
|
+
*
|
|
301
|
+
* ### Structure
|
|
302
|
+
*
|
|
303
|
+
* The `AuthPerms` type is defined as a `Record` where:
|
|
304
|
+
* - The keys are of type `ResourceName`, representing the names of
|
|
305
|
+
* the resources (e.g., "documents", "users").
|
|
306
|
+
* - The values are arrays of `ResourceActionName`, representing the
|
|
307
|
+
* actions that can be performed on the corresponding resource (e.g.,
|
|
308
|
+
* ["read", "create", "update"]).
|
|
309
|
+
*
|
|
310
|
+
* ### Example Usage
|
|
311
|
+
*
|
|
312
|
+
* Here is an example of how the `AuthPerms` type can be used:
|
|
313
|
+
*
|
|
314
|
+
* ```typescript
|
|
315
|
+
* // Example of defining user permissions using AuthPerms
|
|
316
|
+
* const userPermissions: AuthPerms = {
|
|
317
|
+
* documents: ["read", "create", "update"],
|
|
318
|
+
* users: ["read", "delete"],
|
|
319
|
+
* posts: ["read", "create"]
|
|
320
|
+
* };
|
|
321
|
+
* ```
|
|
322
|
+
*
|
|
323
|
+
* In this example, the `AuthPerms` type is used to define a permissions
|
|
324
|
+
* object for a user, mapping resources to the actions they are allowed
|
|
325
|
+
* to perform.
|
|
326
|
+
* @typedef {Partial<{ [TResourceName in ResourceName]: Partial<ResourceActionName<TResourceName>[]> }>} AuthPerms
|
|
327
|
+
*/
|
|
328
|
+
export type AuthPerms = Partial<{
|
|
329
|
+
[TResourceName in ResourceName]: Partial<ResourceActionName<TResourceName>[]>;
|
|
330
|
+
}>;
|
|
331
|
+
/**
|
|
332
|
+
* @interface AuthEventMap
|
|
333
|
+
* Defines the mapping of authentication event names to their string descriptions.
|
|
334
|
+
*
|
|
335
|
+
* This interface serves as a global registry for authentication-related events
|
|
336
|
+
* and supports TypeScript module augmentation, allowing libraries and applications
|
|
337
|
+
* to extend the available authentication events. It provides type-safe event
|
|
338
|
+
* handling and ensures consistency across the authentication system.
|
|
339
|
+
*
|
|
340
|
+
* ### Built-in Events
|
|
341
|
+
*
|
|
342
|
+
* - `SIGN_IN`: Triggered when a user successfully authenticates
|
|
343
|
+
* - `SIGN_OUT`: Triggered when a user logs out
|
|
344
|
+
* - `SIGN_UP`: Triggered when a new user registers
|
|
345
|
+
*
|
|
346
|
+
* ### Module Augmentation
|
|
347
|
+
*
|
|
348
|
+
* This interface can be extended in external modules to add custom authentication events:
|
|
349
|
+
*
|
|
350
|
+
* ```typescript
|
|
351
|
+
* // In a separate module or application
|
|
352
|
+
* declare module "reslib/auth" {
|
|
353
|
+
* interface AuthEventMap {
|
|
354
|
+
* PASSWORD_RESET: string;
|
|
355
|
+
* PROFILE_UPDATE: string;
|
|
356
|
+
* ACCOUNT_LOCKED: string;
|
|
357
|
+
* }
|
|
358
|
+
* }
|
|
359
|
+
*
|
|
360
|
+
* // Now AuthEvent includes the new events
|
|
361
|
+
* const event: AuthEvent = 'PASSWORD_RESET'; // TypeScript validates this
|
|
362
|
+
* ```
|
|
363
|
+
*
|
|
364
|
+
* ### Example Usage
|
|
365
|
+
*
|
|
366
|
+
* ```typescript
|
|
367
|
+
* // Basic usage with built-in events
|
|
368
|
+
* const authEvents: AuthEventMap = {
|
|
369
|
+
* SIGN_IN: 'User has signed in successfully.',
|
|
370
|
+
* SIGN_OUT: 'User has signed out.',
|
|
371
|
+
* SIGN_UP: 'New user account created.'
|
|
372
|
+
* };
|
|
373
|
+
*
|
|
374
|
+
* // Event handling with type safety
|
|
375
|
+
* function handleAuthEvent(event: AuthEvent, data?: any) {
|
|
376
|
+
* const message = authEvents[event];
|
|
377
|
+
* console.log(`Auth event: ${message}`, data);
|
|
378
|
+
*
|
|
379
|
+
* switch (event) {
|
|
380
|
+
* case 'SIGN_IN':
|
|
381
|
+
* // Handle sign in logic
|
|
382
|
+
* break;
|
|
383
|
+
* case 'SIGN_OUT':
|
|
384
|
+
* // Handle sign out logic
|
|
385
|
+
* break;
|
|
386
|
+
* case 'SIGN_UP':
|
|
387
|
+
* // Handle sign up logic
|
|
388
|
+
* break;
|
|
389
|
+
* }
|
|
390
|
+
* }
|
|
391
|
+
*
|
|
392
|
+
* // Usage
|
|
393
|
+
* handleAuthEvent('SIGN_IN', { userId: 123 });
|
|
394
|
+
* ```
|
|
395
|
+
*
|
|
396
|
+
* @see {@link AuthEvent} for the derived event type.
|
|
397
|
+
*/
|
|
398
|
+
export interface AuthEventMap {
|
|
399
|
+
/**
|
|
400
|
+
* Event triggered when a user signs in.
|
|
401
|
+
*
|
|
402
|
+
* This event is emitted when a user successfully logs into the system.
|
|
403
|
+
* It can be used to trigger actions such as updating the user interface
|
|
404
|
+
* or logging the sign-in activity.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* // Example of handling the SIGN_IN event
|
|
408
|
+
* eventEmitter.on('SIGN_IN', () => {
|
|
409
|
+
* console.log('User signed in, updating UI...');
|
|
410
|
+
* });
|
|
411
|
+
*/
|
|
412
|
+
SIGN_IN: string;
|
|
413
|
+
/**
|
|
414
|
+
* Event triggered when a user signs out.
|
|
415
|
+
*
|
|
416
|
+
* This event is emitted when a user successfully logs out of the system.
|
|
417
|
+
* It can be used to trigger actions such as clearing user data or
|
|
418
|
+
* redirecting to the login page.
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* // Example of handling the SIGN_OUT event
|
|
422
|
+
* eventEmitter.on('SIGN_OUT', () => {
|
|
423
|
+
* console.log('User signed out, redirecting to login...');
|
|
424
|
+
* });
|
|
425
|
+
*/
|
|
426
|
+
SIGN_OUT: string;
|
|
427
|
+
/**
|
|
428
|
+
* Event triggered when a user signs up.
|
|
429
|
+
*
|
|
430
|
+
* This event is emitted when a new user successfully registers for an account.
|
|
431
|
+
* It can be used to trigger actions such as sending a welcome email or
|
|
432
|
+
* redirecting the user to a confirmation page.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* // Example of handling the SIGN_UP event
|
|
436
|
+
* eventEmitter.on('SIGN_UP', () => {
|
|
437
|
+
* console.log('New user signed up, sending welcome email...');
|
|
438
|
+
* });
|
|
439
|
+
*/
|
|
440
|
+
SIGN_UP: string;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Type representing the keys of the AuthEventMap interface.
|
|
444
|
+
*
|
|
445
|
+
* This type is a union of string literals corresponding to the event names
|
|
446
|
+
* defined in the AuthEventMap interface. It allows for type-safe handling
|
|
447
|
+
* of authentication events throughout the application.
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* // Example of using AuthEvent
|
|
451
|
+
* function handleAuthEvent(event: AuthEvent) {
|
|
452
|
+
* switch (event) {
|
|
453
|
+
* case 'SIGN_IN':
|
|
454
|
+
* console.log('Handling sign-in event...');
|
|
455
|
+
* break;
|
|
456
|
+
* case 'SIGN_OUT':
|
|
457
|
+
* console.log('Handling sign-out event...');
|
|
458
|
+
* break;
|
|
459
|
+
* case 'SIGN_UP':
|
|
460
|
+
* console.log('Handling sign-up event...');
|
|
461
|
+
* break;
|
|
462
|
+
* }
|
|
463
|
+
* }
|
|
464
|
+
*/
|
|
465
|
+
export type AuthEvent = keyof AuthEventMap;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';
|