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,854 @@
|
|
|
1
|
+
import { AuthUser } from '../auth/types';
|
|
2
|
+
import { Scope, TranslateOptions } from 'i18n-js';
|
|
3
|
+
import 'reflect-metadata';
|
|
4
|
+
import { ClassConstructor } from '../types/index';
|
|
5
|
+
import { InterpolateOptions } from '../utils/index';
|
|
6
|
+
import { Field, ResourceAction, ResourceActionName, ResourceActions, ResourceBase, ResourceContext, ResourceDataService, ResourceDefaultEvent, ResourceManyCriteria, ResourceName, ResourcePaginatedResult, ResourcePrimaryKey, ResourceQueryOptions } from './types';
|
|
7
|
+
export * from './decorators';
|
|
8
|
+
export * from './fields';
|
|
9
|
+
export * from './filters';
|
|
10
|
+
export * from './ResourcePaginationHelper';
|
|
11
|
+
export * from './types';
|
|
12
|
+
export declare abstract class Resource<Name extends ResourceName = ResourceName, DataType = unknown, TPrimaryKey extends ResourcePrimaryKey = ResourcePrimaryKey, EventType = ResourceDefaultEvent<Name>> {
|
|
13
|
+
/**
|
|
14
|
+
* The internal name of the resource.
|
|
15
|
+
*
|
|
16
|
+
* This name is used within the system for referencing the resource programmatically.
|
|
17
|
+
* It is often a short, unique identifier for the resource.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const userResource: ResourceBase = { name: "user" };
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
protected abstract name: Name;
|
|
25
|
+
private _onDictionaryChangedListener?;
|
|
26
|
+
private _onLocaleChangeListener?;
|
|
27
|
+
constructor();
|
|
28
|
+
actions?: Partial<ResourceActions<Name>>;
|
|
29
|
+
getMetaData(): ResourceBase<Name>;
|
|
30
|
+
static events: {
|
|
31
|
+
_____isObservable?: boolean;
|
|
32
|
+
on: (event: string, fn: import("../observable").ObservableCallback) => {
|
|
33
|
+
remove: () => any;
|
|
34
|
+
};
|
|
35
|
+
finally: (event: string, fn: import("../observable").ObservableCallback) => /*elided*/ any;
|
|
36
|
+
off: (event: string, fn: import("../observable").ObservableCallback) => /*elided*/ any;
|
|
37
|
+
trigger: (event: string, ...args: any[]) => /*elided*/ any;
|
|
38
|
+
offAll: () => /*elided*/ any;
|
|
39
|
+
once: (event: string, fn: import("../observable").ObservableCallback) => {
|
|
40
|
+
remove: () => any;
|
|
41
|
+
};
|
|
42
|
+
getEventCallBacks: () => Partial<Record<string, import("../observable").ObservableCallback[]>>;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* A user-friendly label for the resource.
|
|
46
|
+
*
|
|
47
|
+
* This is typically a shorter name intended for display in UI elements, such as dropdowns or buttons.
|
|
48
|
+
* It helps users identify the resource within the user interface.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const productResource: ResourceBase = { label: "Product" };
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
label?: string;
|
|
56
|
+
/**
|
|
57
|
+
* A short text that appears when the user hovers over the resource.
|
|
58
|
+
* The tooltip provides additional context or information about the resource.
|
|
59
|
+
*
|
|
60
|
+
* Typically used in user interfaces to clarify what a particular resource represents or to give instructions.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const userResource: ResourceBase = { title : "This resource manages user information." };
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
title?: string;
|
|
68
|
+
/**
|
|
69
|
+
* A type that represents a map of field names to their corresponding Field instances.
|
|
70
|
+
@description this is the list of fields that are part of the resource.It's a map where each key represents a field name, and the value contains field metadata.
|
|
71
|
+
Fields are created using the @FieldMeta decorator when resources are defined.
|
|
72
|
+
*/
|
|
73
|
+
fields?: Record<string, Field>;
|
|
74
|
+
/**
|
|
75
|
+
* Resolves the translations for the resource when the i18n dictionary or locale changes.
|
|
76
|
+
* This method is called when the "translations-changed" or "locale-changed" events are triggered.
|
|
77
|
+
*/
|
|
78
|
+
onI18nChange(): void;
|
|
79
|
+
/**
|
|
80
|
+
* Resolves the translations for the resource when the i18n dictionary or locale changes.
|
|
81
|
+
* This method is called when the "translations-changed" or "locale-changed" events are triggered.
|
|
82
|
+
*/
|
|
83
|
+
resolveTranslations(): void;
|
|
84
|
+
/**
|
|
85
|
+
* Removes the event listeners for the "translations-changed" and "locale-changed" events.
|
|
86
|
+
* This method is called when the resource is being destroyed to clean up the event listeners.
|
|
87
|
+
*/
|
|
88
|
+
destroy(): void;
|
|
89
|
+
/**
|
|
90
|
+
* Creates a resource context object containing the resource's name, label, and any additional parameters.
|
|
91
|
+
*
|
|
92
|
+
* This context is primarily used for internationalization (i18n) and event handling,
|
|
93
|
+
* providing essential resource information to translation functions and event listeners.
|
|
94
|
+
*
|
|
95
|
+
* @param additionalParams - Optional additional parameters to include in the context object.
|
|
96
|
+
* @returns An ResourceContext object containing resource information and additional parameters.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const context = resource.getResourceContext({ userId: 123 });
|
|
101
|
+
* // Returns: { resourceName: "user", resourceLabel: "User", userId: 123 }
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
getResourceContext(additionalParams?: Record<string, any>): ResourceContext;
|
|
105
|
+
/**
|
|
106
|
+
*
|
|
107
|
+
* @returns {string} the message to display when the DataProvider for the resource is invalid
|
|
108
|
+
*/
|
|
109
|
+
get INVALID_DATA_PROVIDER_ERROR(): string;
|
|
110
|
+
hasDataService(): boolean;
|
|
111
|
+
/**
|
|
112
|
+
* get the data provider for the resource.
|
|
113
|
+
* @returns {ResourceDataService<DataType>} The data provider for the resource.
|
|
114
|
+
*/
|
|
115
|
+
abstract getDataService(): ResourceDataService<DataType>;
|
|
116
|
+
/***
|
|
117
|
+
* trigger the event
|
|
118
|
+
* @param event - The event to trigger.
|
|
119
|
+
* When the event is triggered, the events observable is also triggered.
|
|
120
|
+
* @param args - The arguments to pass to the event.
|
|
121
|
+
*/
|
|
122
|
+
trigger(event: EventType | ResourceDefaultEvent<Name>, ...args: any[]): void;
|
|
123
|
+
/**
|
|
124
|
+
* Authorizes the user to perform a specific action on this resource.
|
|
125
|
+
*
|
|
126
|
+
* This method validates both that the resource has a data service available
|
|
127
|
+
* and that the user has permission to perform the specified action.
|
|
128
|
+
*
|
|
129
|
+
* @param action - The action name to authorize (e.g., 'read', 'create', 'update', 'delete').
|
|
130
|
+
* @returns A promise that resolves if authorization succeeds, or rejects with a translated error if authorization fails.
|
|
131
|
+
* @throws Error with translated message if data service is unavailable or user lacks permission.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* await resource.authorizeAction('read');
|
|
136
|
+
* await resource.authorizeAction('create');
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
authorizeAction(action: ResourceActionName<Name>): Promise<void>;
|
|
140
|
+
/***
|
|
141
|
+
* Fetches all records from the resource.
|
|
142
|
+
* @param {ResourceQueryOptions<DataType>} options - Optional options for fetching resources.
|
|
143
|
+
* @returns {Promise<ResourcePaginatedResult<DataType>>} A promise that resolves to the result of the list operation.
|
|
144
|
+
*/
|
|
145
|
+
find(options?: ResourceQueryOptions<DataType>): Promise<DataType[]>;
|
|
146
|
+
/***
|
|
147
|
+
* fetches a single record from the resource.
|
|
148
|
+
* @param {TPrimaryKey | ResourceQueryOptions<DataType>} options - The primary key or query options of the resource to retrieve.
|
|
149
|
+
* @returns {Promise<IResourceOperationResult<DataType>>} A promise that resolves to the result of the list operation.
|
|
150
|
+
*/
|
|
151
|
+
findOne(options: TPrimaryKey | ResourceQueryOptions<DataType>): Promise<DataType | null>;
|
|
152
|
+
/**
|
|
153
|
+
* Builds a translation path for the resource by combining the resource prefix with the provided key.
|
|
154
|
+
*
|
|
155
|
+
* This method constructs hierarchical translation paths used by the i18n system. It creates paths
|
|
156
|
+
* like "resources.user.notFoundError" or "resources.user.forbiddenError" that correspond to
|
|
157
|
+
* nested translation keys in the internationalization files.
|
|
158
|
+
*
|
|
159
|
+
* @param {string} key - The specific translation key to append to the resource prefix.
|
|
160
|
+
* Leading dots are automatically trimmed to avoid double dots in the path.
|
|
161
|
+
* @returns {string} The complete translation path. If no key is provided, returns just the resource prefix.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* // For a resource named "user"
|
|
166
|
+
* resource.buildTranslationPath("notFoundError"); // "resources.user.notFoundError"
|
|
167
|
+
* resource.buildTranslationPath("forbiddenError"); // "resources.user.forbiddenError"
|
|
168
|
+
* resource.buildTranslationPath(); // "resources.user."
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
buildTranslationPath(key?: string): string;
|
|
172
|
+
/***
|
|
173
|
+
* fetches a single record from the resource.
|
|
174
|
+
* If the record is not found, it throws an error.
|
|
175
|
+
* @param {TPrimaryKey | ResourceQueryOptions<DataType>} options - The primary key or query options of the resource to retrieve.
|
|
176
|
+
*/
|
|
177
|
+
findOneOrFail(options: TPrimaryKey | ResourceQueryOptions<DataType>): Promise<Awaited<DataType> extends infer T ? T extends Awaited<DataType> ? T extends object | Record<any, any> ? T : T extends string | boolean | any[] | null | undefined ? never : any : never : never>;
|
|
178
|
+
/**
|
|
179
|
+
* trigger called before the create operation.
|
|
180
|
+
* @param record {Partial<DataType>} The record to be created.
|
|
181
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
182
|
+
*/
|
|
183
|
+
protected beforeCreate(record: Partial<DataType>): Promise<void>;
|
|
184
|
+
/***
|
|
185
|
+
* trigger called after the create operation.
|
|
186
|
+
* @param {DataType} record - The created record.
|
|
187
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
188
|
+
*/
|
|
189
|
+
protected afterCreate(record: DataType): Promise<void>;
|
|
190
|
+
/**
|
|
191
|
+
* Trigger called before the update operation.
|
|
192
|
+
* @param primaryKey {TPrimaryKey}, the primary key of the record to be updated.
|
|
193
|
+
* @param dataToUpdate {Partial<DataType>} - The updated data for the record.
|
|
194
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
195
|
+
*/
|
|
196
|
+
protected beforeUpdate(primaryKey: TPrimaryKey, dataToUpdate: Partial<DataType>): Promise<void>;
|
|
197
|
+
/**
|
|
198
|
+
* Triggers called after the update operation.
|
|
199
|
+
* @param {DataType} updatedData - The updated record.
|
|
200
|
+
* @param {TPrimaryKey} primaryKey - The primary key of the updated record.
|
|
201
|
+
* @param {Partial<DataType>} dataToUpdate - The data that was used to update the record.
|
|
202
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
203
|
+
*/
|
|
204
|
+
protected afterUpdate(updatedData: DataType, primaryKey: TPrimaryKey, dataToUpdate: Partial<DataType>): Promise<void>;
|
|
205
|
+
/**
|
|
206
|
+
* Trigger called before the delete operation.
|
|
207
|
+
* @param primaryKey {TPrimaryKey} - The primary key of the record to be deleted.
|
|
208
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
209
|
+
*/
|
|
210
|
+
protected beforeDelete(primaryKey: TPrimaryKey): Promise<void>;
|
|
211
|
+
/***
|
|
212
|
+
* Triggers called after the delete operation.
|
|
213
|
+
* @param {boolean} result - The result of the delete operation.
|
|
214
|
+
* @param {TPrimaryKey} primaryKey - The primary key of the deleted record.
|
|
215
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
216
|
+
*/
|
|
217
|
+
protected afterDelete(result: boolean, primaryKey: TPrimaryKey): Promise<void>;
|
|
218
|
+
/***
|
|
219
|
+
* trigger called before the createMany operation.
|
|
220
|
+
* @param {Partial<DataType>[]} records - The records to be created.
|
|
221
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
222
|
+
*/
|
|
223
|
+
protected beforeCreateMany(records: Partial<DataType>[]): Promise<void>;
|
|
224
|
+
/***
|
|
225
|
+
* trigger called after the createMany operation.
|
|
226
|
+
* @param {DataType[]} records - The created records.
|
|
227
|
+
* @param {Partial<DataType>[]} data - The data used to create the records.
|
|
228
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
229
|
+
*/
|
|
230
|
+
protected afterCreateMany(records: DataType[], data: Partial<DataType>[]): Promise<void>;
|
|
231
|
+
/***
|
|
232
|
+
* Trigger called before the updateMany operation.
|
|
233
|
+
* @param {ResourceManyCriteria} criteria - The criteria for the update operation.
|
|
234
|
+
* @param {Partial<DataType>} data - The data for the update operation.
|
|
235
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
236
|
+
*/
|
|
237
|
+
protected beforeUpdateMany(criteria: ResourceManyCriteria<DataType, TPrimaryKey>, data: Partial<DataType>): Promise<void>;
|
|
238
|
+
/**
|
|
239
|
+
* Triggers called after the updateMany operation.
|
|
240
|
+
* @param affectedRows {number} The number of records updated
|
|
241
|
+
* @param criteria {ResourceManyCriteria} The criteria used for the update operation.
|
|
242
|
+
* @param {Partial<DataType>[]} records The records updated
|
|
243
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
244
|
+
*/
|
|
245
|
+
protected afterUpdateMany(affectedRows: number, criteria: ResourceManyCriteria<DataType, TPrimaryKey>, records: Partial<DataType>): Promise<void>;
|
|
246
|
+
/***
|
|
247
|
+
* Trigger called before the deleteMany operation.
|
|
248
|
+
* @param {ResourceManyCriteria} criteria - The criteria for the delete operation.
|
|
249
|
+
* @return {Promise<void>} A promise that resolves when the operation is complete.
|
|
250
|
+
*/
|
|
251
|
+
protected beforeDeleteMany(criteria: ResourceManyCriteria<DataType, TPrimaryKey>): Promise<void>;
|
|
252
|
+
/**
|
|
253
|
+
* Trigger called after the deleteMany operation.
|
|
254
|
+
* @param {number} affectedRows The number of affected rows
|
|
255
|
+
* @param {ResourceManyCriteria<DataType,TPrimaryKey>} criteria The criteria for the delete operation.
|
|
256
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
257
|
+
*/
|
|
258
|
+
protected afterDeleteMany(affectedRows: number, criteria: ResourceManyCriteria<DataType, TPrimaryKey>): Promise<void>;
|
|
259
|
+
/***
|
|
260
|
+
* creates a new record in the resource.
|
|
261
|
+
* This method allows you to create a new record in the resource.
|
|
262
|
+
* It first authorizes the action, then calls the `beforeCreate` hook, performs the creation,
|
|
263
|
+
* and finally calls the `afterCreate` hook before triggering the "create" event.
|
|
264
|
+
* @template T - The type of the data being created.
|
|
265
|
+
* @param {DataType} record - The data for the new record.
|
|
266
|
+
* @returns {Promise<IResourceOperationResult<DataType>>} A promise that resolves to the result of the create operation.
|
|
267
|
+
*/
|
|
268
|
+
create<T extends DataType>(record: T): Promise<DataType>;
|
|
269
|
+
/**
|
|
270
|
+
* updates a record in the resource.
|
|
271
|
+
* This method allows you to update an existing record in the resource.
|
|
272
|
+
* It first authorizes the action, then calls the `beforeUpdate` hook, performs the update,
|
|
273
|
+
* and finally calls the `afterUpdate` hook before triggering the "update" event.
|
|
274
|
+
* @template T - The type of the data being updated.
|
|
275
|
+
* @param key {TPrimaryKey} The primary key of the resource to update.
|
|
276
|
+
* @param dataToUpdate
|
|
277
|
+
* @returns
|
|
278
|
+
*/
|
|
279
|
+
update<T extends Partial<DataType>>(primaryKey: TPrimaryKey, dataToUpdate: T): Promise<DataType>;
|
|
280
|
+
/***
|
|
281
|
+
* deletes a record from the resource.
|
|
282
|
+
* This method allows you to delete an existing record from the resource.
|
|
283
|
+
* It first authorizes the action, then calls the `beforeDelete` hook, performs the deletion,
|
|
284
|
+
* and finally calls the `afterDelete` hook before triggering the "delete" event.
|
|
285
|
+
*
|
|
286
|
+
* @template TPrimaryKey - The type of the primary key of the resource.
|
|
287
|
+
* @param primaryKey {TPrimaryKey} The primary key of the resource to delete.
|
|
288
|
+
* @returns Promise<number> A promise that resolves to the result of the delete operation.
|
|
289
|
+
*/
|
|
290
|
+
delete(primaryKey: TPrimaryKey): Promise<boolean>;
|
|
291
|
+
/**
|
|
292
|
+
* Fetches a list of records from the resource and returns the total count.
|
|
293
|
+
* This method allows you to retrieve a list of records from the resource,
|
|
294
|
+
* along with the total count of records that match the query options.
|
|
295
|
+
* It first authorizes the action, then calls the `findAndCount` method on the data service,
|
|
296
|
+
* @param options - Optional query options to filter, sort, and paginate the results.
|
|
297
|
+
* @returns A promise that resolves to an object containing the list of records and the total count.
|
|
298
|
+
*/
|
|
299
|
+
findAndCount(options?: ResourceQueryOptions<DataType>): Promise<[DataType[], number]>;
|
|
300
|
+
/**
|
|
301
|
+
* Fetches a paginated list of records from the resource.
|
|
302
|
+
* This method allows you to retrieve a paginated list of records from the resource,
|
|
303
|
+
* along with the total count of records that match the query options.
|
|
304
|
+
* It first authorizes the action, then calls the `findAndPaginate` method on the data service,
|
|
305
|
+
* and finally triggers the "findAndPaginate" event.
|
|
306
|
+
* @param options - Optional query options to filter, sort, and paginate the results.
|
|
307
|
+
* @returns A promise that resolves to an object containing the paginated list of records and the total count.
|
|
308
|
+
*/
|
|
309
|
+
findAndPaginate(options?: ResourceQueryOptions<DataType> | undefined): Promise<ResourcePaginatedResult<DataType>>;
|
|
310
|
+
/**
|
|
311
|
+
* Creates multiple records in the resource.
|
|
312
|
+
* This method allows you to create multiple records in the resource in a single operation.
|
|
313
|
+
* It first authorizes the action, then calls the `beforeCreateMany` hook, performs the creation,
|
|
314
|
+
* and finally calls the `afterCreateMany` hook before triggering the "createMany" event.
|
|
315
|
+
* @template T - The type of the data being created.
|
|
316
|
+
* @param data - An array of partial data objects to create.
|
|
317
|
+
* @returns A promise that resolves to the result of the create operation.
|
|
318
|
+
*/
|
|
319
|
+
createMany<T extends DataType>(data: T[]): Promise<DataType[]>;
|
|
320
|
+
/**
|
|
321
|
+
* Updates multiple records in the resource.
|
|
322
|
+
* This method allows you to update multiple records in the resource in a single operation.
|
|
323
|
+
* It first authorizes the action, then calls the `beforeUpdateMany` hook, performs the update,
|
|
324
|
+
* and finally calls the `afterUpdateMany` hook before triggering the "updateMany" event.
|
|
325
|
+
* @template T - The type of the data being updated.
|
|
326
|
+
* @param criteria - The query options to filter the records to be updated.
|
|
327
|
+
* @param data - An array of partial data objects to update.
|
|
328
|
+
* @returns A promise that resolves to the result of the update operation.
|
|
329
|
+
*/
|
|
330
|
+
updateMany<T extends Partial<DataType>>(criteria: ResourceManyCriteria<DataType, TPrimaryKey>, data: T): Promise<number>;
|
|
331
|
+
/**
|
|
332
|
+
* Deletes multiple records from the resource based on the provided criteria.
|
|
333
|
+
* This method allows you to delete multiple records in the resource in a single operation.
|
|
334
|
+
* It first authorizes the action, then calls the `beforeDeleteMany` hook, performs the deletion,
|
|
335
|
+
* and finally calls the `afterDeleteMany` hook before triggering the "deleteMany" event.
|
|
336
|
+
* @param criteria - The query options to filter the records to be deleted.
|
|
337
|
+
* @returns A promise that resolves to the result of the delete operation.
|
|
338
|
+
*/
|
|
339
|
+
deleteMany(criteria: ResourceManyCriteria<DataType, TPrimaryKey>): Promise<number>;
|
|
340
|
+
/**
|
|
341
|
+
* Counts the number of records in the resource.
|
|
342
|
+
* This method allows you to count the total number of records in the resource.
|
|
343
|
+
* It first authorizes the action, then calls the `count` method on the data service,
|
|
344
|
+
* and finally triggers the "read" event with the count result.
|
|
345
|
+
*
|
|
346
|
+
* @template DataType - The type of the data being counted.
|
|
347
|
+
* @param options - Optional query options to filter the results.
|
|
348
|
+
* @returns {Promise<number>} A promise that resolves to the result of the count operation.
|
|
349
|
+
*/
|
|
350
|
+
count(options?: ResourceQueryOptions<DataType>): Promise<number>;
|
|
351
|
+
/***
|
|
352
|
+
* checks if the resource has the record
|
|
353
|
+
* This method allows you to check if a record exists in the resource.
|
|
354
|
+
* It first authorizes the action, then calls the `exists` method on the data service,
|
|
355
|
+
* and finally triggers the "exists" event with the result.
|
|
356
|
+
* @param {TPrimaryKey} primaryKey - The primary key of the record to check.
|
|
357
|
+
* @returns {Promise<boolean>} A promise that resolves to the result of the exists operation.
|
|
358
|
+
*/
|
|
359
|
+
exists(primaryKey: TPrimaryKey): Promise<boolean>;
|
|
360
|
+
updateMetadata(options: ResourceBase<Name>): ResourceBase<Name>;
|
|
361
|
+
/**
|
|
362
|
+
* Initializes the resource with the provided metaData.
|
|
363
|
+
*
|
|
364
|
+
* @param metaData - An object implementing the ResourceBase interface, containing the data to initialize the resource with.
|
|
365
|
+
*
|
|
366
|
+
* This method assigns the provided metaData to the resource, ensuring that any empty properties
|
|
367
|
+
* on the resource are filled with the corresponding values from the metaData object. It skips
|
|
368
|
+
* properties that are functions. After assigning the metaData, it calls the `getFields` method
|
|
369
|
+
* to further process the resource.
|
|
370
|
+
*/
|
|
371
|
+
init(): void;
|
|
372
|
+
/**
|
|
373
|
+
* Retrieves the i18n translations for the resource.
|
|
374
|
+
*
|
|
375
|
+
* @param {string} [locale] - The locale to use for the translations. If not provided, the default locale from the i18n instance will be used.
|
|
376
|
+
* @returns {Record<string,any>} - An object containing the translations for the resource, keyed by the property names.
|
|
377
|
+
* @example
|
|
378
|
+
* // Register translations for the "en" locale.
|
|
379
|
+
* i18n.registerTranslations({
|
|
380
|
+
* en: {
|
|
381
|
+
* resources: {
|
|
382
|
+
* user: { // The resource name
|
|
383
|
+
* label: "User", // The label property
|
|
384
|
+
* title: "User Information", // The title property
|
|
385
|
+
* }
|
|
386
|
+
* }
|
|
387
|
+
* }
|
|
388
|
+
* });
|
|
389
|
+
*
|
|
390
|
+
* // Retrieve the translations for the "user" resource.
|
|
391
|
+
* const userResource = ResourcesManager.getResource("user");
|
|
392
|
+
* const userTranslations = userResource.getTranslations();
|
|
393
|
+
* console.log(userTranslations);
|
|
394
|
+
* // Output:
|
|
395
|
+
* // {
|
|
396
|
+
* // label: "User",
|
|
397
|
+
* // title: "User Information",
|
|
398
|
+
* // }
|
|
399
|
+
*/
|
|
400
|
+
getTranslations(locale?: string): Record<string, any>;
|
|
401
|
+
/**
|
|
402
|
+
*Translates the given scope using the i18n default instance, ensuring that the resource name is prefixed correctly.
|
|
403
|
+
*
|
|
404
|
+
* @param {Scope} scope - The scope to use for the translation. This can be a string or an array of strings.
|
|
405
|
+
* @param {TranslateOptions} [options] - Optional options to pass to the translation function.
|
|
406
|
+
* @returns {string | T} - The translated string, or the translated value of type T if the scope returns a non-string value.
|
|
407
|
+
* @example
|
|
408
|
+
* // Register translations for the "en" locale.
|
|
409
|
+
* i18n.registerTranslations({
|
|
410
|
+
* en: {
|
|
411
|
+
* resources: {
|
|
412
|
+
* user: { // The resource name
|
|
413
|
+
* label: "User", // The label property
|
|
414
|
+
* title: "User Information", // The title property
|
|
415
|
+
* create: {
|
|
416
|
+
* label: "Create User",
|
|
417
|
+
* title: "Create a new user",
|
|
418
|
+
* },
|
|
419
|
+
* read: {
|
|
420
|
+
* label: "View User",
|
|
421
|
+
* title: "View a specific user",
|
|
422
|
+
* },
|
|
423
|
+
* }
|
|
424
|
+
* }
|
|
425
|
+
* }
|
|
426
|
+
* });
|
|
427
|
+
* // Translate the "label" property of the "user" resource.
|
|
428
|
+
* const userResource = ResourcesManager.getResource("user");
|
|
429
|
+
* const label = userResource.translate("label"); // "User"
|
|
430
|
+
*
|
|
431
|
+
* // Translate the "title" property of the "user" resource.
|
|
432
|
+
* const title = userResource.translate("title"); // "Manage user data"
|
|
433
|
+
*/
|
|
434
|
+
translate<T = string>(scope: Scope, options?: TranslateOptions): string | T;
|
|
435
|
+
/**
|
|
436
|
+
* Retrieves the name of the resource.
|
|
437
|
+
* @returns {Name} The name of the resource, cast to the Name type.
|
|
438
|
+
*/
|
|
439
|
+
getName(): Name;
|
|
440
|
+
/**
|
|
441
|
+
* Retrieves the actions associated with the resource.
|
|
442
|
+
* If the actions are not already defined or not an object,
|
|
443
|
+
* it initializes them as an empty object of type `ResourceActions`.
|
|
444
|
+
*
|
|
445
|
+
* @returns The map of resource actions.
|
|
446
|
+
*/
|
|
447
|
+
getActions(): Partial<ResourceActions<Name>>;
|
|
448
|
+
/**
|
|
449
|
+
* checks if the resource has the action
|
|
450
|
+
* @param action - The action to check
|
|
451
|
+
* @returns true if the action exists, false otherwise
|
|
452
|
+
*/
|
|
453
|
+
hasAction(action: string): action is ResourceActionName<Name>;
|
|
454
|
+
/**
|
|
455
|
+
* Determines if the given permission is allowed for the specified user.
|
|
456
|
+
*
|
|
457
|
+
* @param action - The action to check. It can be a string or an array of strings representing the action name and the resource name.
|
|
458
|
+
|
|
459
|
+
* @param user - The user for whom the permission is being checked. It can be an object implementing the AuthUser interface.The user object for whom the permission.If not provided, the function will attempt
|
|
460
|
+
* to retrieve the signed user from the session.
|
|
461
|
+
* @returns A boolean indicating whether the permission is allowed for the user.
|
|
462
|
+
*
|
|
463
|
+
* The method performs the following steps:
|
|
464
|
+
* 1. Constructs a prefix using the resource name.
|
|
465
|
+
* 2. If the permission is a string, it trims and processes it to ensure it has the correct prefix.
|
|
466
|
+
* 3. Checks if the permission string has the correct prefix.
|
|
467
|
+
* 4. Extracts the action part of the permission and checks if it is a valid action.
|
|
468
|
+
* 5. If the action is "all" or matches any of the resource's actions, it returns true.
|
|
469
|
+
* 6. Otherwise, it delegates the permission check to the Auth.isAllowed method.
|
|
470
|
+
*/
|
|
471
|
+
isAllowed(action?: ResourceActionName<Name> | ResourceActionName<Name>[], user?: AuthUser): boolean; /**
|
|
472
|
+
* Determines if the specified user has read access.
|
|
473
|
+
*
|
|
474
|
+
* @param user - The user whose read access is being checked. If no user is provided, the method will use default permissions.
|
|
475
|
+
* @returns A boolean indicating whether the user has read access.
|
|
476
|
+
*/
|
|
477
|
+
canUserRead(user?: AuthUser): boolean;
|
|
478
|
+
/**
|
|
479
|
+
* Determines if the user has permission to create a resource.
|
|
480
|
+
*
|
|
481
|
+
* @param user - The user whose permissions are being checked. If not provided, the method will use the default user.
|
|
482
|
+
* @returns A boolean indicating whether the user is allowed to create the resource.
|
|
483
|
+
*/
|
|
484
|
+
canUserCreate(user?: AuthUser): boolean;
|
|
485
|
+
/**
|
|
486
|
+
* Determines if the specified user has permission to update the resource.
|
|
487
|
+
*
|
|
488
|
+
* @param user - The user whose update permissions are being checked. If no user is provided, the method will use default permissions.
|
|
489
|
+
* @returns A boolean indicating whether the user has permission to update the resource.
|
|
490
|
+
*/
|
|
491
|
+
canUserUpdate(user?: AuthUser): boolean;
|
|
492
|
+
/**
|
|
493
|
+
* Determines if the user has permission to delete.
|
|
494
|
+
*
|
|
495
|
+
* @param user - The authenticated user whose permissions are being checked. Optional.
|
|
496
|
+
* @returns A boolean indicating whether the user is allowed to delete.
|
|
497
|
+
*/
|
|
498
|
+
canUserDelete(user?: AuthUser): boolean;
|
|
499
|
+
/**
|
|
500
|
+
* Retrieves the translated value of the specified property, using the resource's translations.
|
|
501
|
+
* If the property is not found in the translations, it returns the fallback value or the property name.
|
|
502
|
+
*
|
|
503
|
+
* @param propertyName - The name of the property to translate.
|
|
504
|
+
* @param fallbackValue - The fallback value to use if the property is not found in the translations.
|
|
505
|
+
* @param options - Additional options to pass to the translation function.
|
|
506
|
+
* @returns The translated value of the property.
|
|
507
|
+
*/
|
|
508
|
+
translateProperty(propertyName: string, fallbackValue?: string, options?: TranslateOptions): string;
|
|
509
|
+
/**
|
|
510
|
+
* Retrieves the label of the resource.
|
|
511
|
+
*
|
|
512
|
+
* If the label is not defined, it returns a default empty string.
|
|
513
|
+
*
|
|
514
|
+
* @returns {string} The label of the resource.
|
|
515
|
+
*/
|
|
516
|
+
getLabel(): string;
|
|
517
|
+
/**
|
|
518
|
+
* Retrieves the title of the resource.
|
|
519
|
+
*
|
|
520
|
+
* If the title is not defined, it returns a default empty string.
|
|
521
|
+
*
|
|
522
|
+
* @returns {string} The title of the resource.
|
|
523
|
+
*/
|
|
524
|
+
getTitle(): string;
|
|
525
|
+
/**
|
|
526
|
+
* Retrieves the fields associated with the resource.
|
|
527
|
+
*
|
|
528
|
+
* This method populates the `fields` property by invoking an external `getFields` function,
|
|
529
|
+
* which dynamically retrieves and returns all the fields related to the resource.
|
|
530
|
+
*
|
|
531
|
+
* @returns {Record<string, Field>} A record containing all the fields of the resource.
|
|
532
|
+
*/
|
|
533
|
+
getFields(): Record<string, Field>;
|
|
534
|
+
/**
|
|
535
|
+
* Interpolates placeholders in a string with values from a parameters object, automatically including resource context.
|
|
536
|
+
*
|
|
537
|
+
* This method provides a convenient wrapper around the global `interpolate` function, automatically
|
|
538
|
+
* merging the provided parameters with the resource's context information (resource name, label, etc.).
|
|
539
|
+
* This is particularly useful for internationalization and dynamic string generation within resource operations.
|
|
540
|
+
*
|
|
541
|
+
* **Placeholder Format:**
|
|
542
|
+
* - Uses the default `{key}` format for placeholders
|
|
543
|
+
* - Keys can contain dots (e.g., `{user.name}`) but are treated as flat object keys
|
|
544
|
+
* - Custom regex patterns can be specified via options for different placeholder formats
|
|
545
|
+
*
|
|
546
|
+
* **Resource Context:**
|
|
547
|
+
* - Automatically includes `resourceName`, `resourceLabel`, and any additional context from `getResourceContext()`
|
|
548
|
+
* - Provided parameters take precedence over resource context values
|
|
549
|
+
*
|
|
550
|
+
* **Value Handling:**
|
|
551
|
+
* - Missing keys or undefined/null values result in empty strings
|
|
552
|
+
* - Values are automatically formatted using the default formatter that handles all JavaScript types
|
|
553
|
+
* - Custom formatters can be provided for specialized formatting requirements
|
|
554
|
+
*
|
|
555
|
+
* @param {string} [text] - The template string containing placeholders to be replaced.
|
|
556
|
+
* If null, undefined, or empty, returns an empty string.
|
|
557
|
+
* @param {Record<string, any>} [params] - An object containing key-value pairs for interpolation.
|
|
558
|
+
* These parameters will be merged with the resource context.
|
|
559
|
+
* If null, undefined, or empty object, only resource context is used.
|
|
560
|
+
* @param {InterpolateOptions} [options] - Optional configuration object for advanced interpolation behavior.
|
|
561
|
+
* Note: The `tagRegex` option will override the default `{key}` pattern.
|
|
562
|
+
* @returns {string} The interpolated string with all placeholders replaced by their corresponding values.
|
|
563
|
+
* Placeholders without matching keys are replaced with empty strings.
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* // Basic interpolation with resource context
|
|
567
|
+
* const message = resource.interpolate("Welcome to {resourceLabel}!", { user: "Alice" });
|
|
568
|
+
* // Result: "Welcome to User!" (assuming resource label is "User")
|
|
569
|
+
*
|
|
570
|
+
* @example
|
|
571
|
+
* // Using resource context values
|
|
572
|
+
* const title = resource.interpolate("{resourceName}: {action}", { action: "created" });
|
|
573
|
+
* // Result: "user: created" (assuming resource name is "user")
|
|
574
|
+
*
|
|
575
|
+
* @example
|
|
576
|
+
* // Parameters override resource context
|
|
577
|
+
* const custom = resource.interpolate("{resourceLabel}", { resourceLabel: "Custom Label" });
|
|
578
|
+
* // Result: "Custom Label" (parameter takes precedence)
|
|
579
|
+
*
|
|
580
|
+
* @example
|
|
581
|
+
* // Complex objects are JSON stringified
|
|
582
|
+
* const dataMsg = resource.interpolate("Data: {info}", { info: { count: 5 } });
|
|
583
|
+
* // Result: "Data: {"count":5}"
|
|
584
|
+
*
|
|
585
|
+
* @example
|
|
586
|
+
* // Custom formatter for specialized formatting
|
|
587
|
+
* const formatted = resource.interpolate("Price: {amount}", { amount: 99.99 }, {
|
|
588
|
+
* valueFormatter: (value, tagName) => {
|
|
589
|
+
* if (tagName === 'amount' && typeof value === 'number') {
|
|
590
|
+
* return `$${value.toFixed(2)}`;
|
|
591
|
+
* }
|
|
592
|
+
* return String(value);
|
|
593
|
+
* }
|
|
594
|
+
* });
|
|
595
|
+
* // Result: "Price: $99.99"
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* // Custom regex for different placeholder syntax
|
|
599
|
+
* const customSyntax = resource.interpolate("Hello [[name]]!", { name: "World" }, {
|
|
600
|
+
* tagRegex: /\[\[([^\]]+)\]\]/g
|
|
601
|
+
* });
|
|
602
|
+
* // Result: "Hello World!"
|
|
603
|
+
*/
|
|
604
|
+
interpolate(text?: string, params?: Record<string, any>, options?: InterpolateOptions): string;
|
|
605
|
+
/**
|
|
606
|
+
* Retrieves the label for a specified action, optionally formatting it with provided parameters.
|
|
607
|
+
*
|
|
608
|
+
* @param actionName - The name of the action for which to get the label.
|
|
609
|
+
* @param params - Optional parameters to format the label.
|
|
610
|
+
* @returns The formatted action label.
|
|
611
|
+
*/
|
|
612
|
+
getActionLabel(actionName: ResourceActionName<Name>, params?: Record<string, any>): string;
|
|
613
|
+
/**
|
|
614
|
+
* Retrieves the title of a specified action, optionally formatting it with provided parameters.
|
|
615
|
+
*
|
|
616
|
+
* @param actionName - The name of the action for which the title is to be retrieved.
|
|
617
|
+
* @param params - An optional record of parameters to format the title.
|
|
618
|
+
* @returns The formatted title of the specified action.
|
|
619
|
+
*/
|
|
620
|
+
getActionTitle(actionName: ResourceActionName<Name>, params?: Record<string, any>): string;
|
|
621
|
+
/**
|
|
622
|
+
* Retrieves a specific action by its name.
|
|
623
|
+
*
|
|
624
|
+
* @param {ResourceActionName<Name>} actionName - The name of the action to retrieve.
|
|
625
|
+
* @returns {ResourceAction} The action object if found, otherwise an empty object.
|
|
626
|
+
*/
|
|
627
|
+
getAction(actionName: ResourceActionName<Name>): ResourceAction;
|
|
628
|
+
/**
|
|
629
|
+
* Retrieves the primary key fields from the current object's fields.
|
|
630
|
+
*
|
|
631
|
+
* @returns {Field[]} An array of fields that are marked as primary keys.
|
|
632
|
+
*/
|
|
633
|
+
getPrimaryKeys(): Field[];
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Manages a collection of resources within the application.
|
|
637
|
+
*
|
|
638
|
+
* The `ResourcesManager` class provides static methods to store, retrieve, and manage resource instances.
|
|
639
|
+
* It maintains a global record of all instantiated resources, allowing for easy access and management.
|
|
640
|
+
* Each resource is identified by a unique name, which is derived from the `Name` type.
|
|
641
|
+
*
|
|
642
|
+
* @example
|
|
643
|
+
* // Instantiate and add resources to the manager
|
|
644
|
+
* const userResource = new UserResource();
|
|
645
|
+
* ResourcesManager.addResource('userResource', userResource);
|
|
646
|
+
*
|
|
647
|
+
* // Retrieve the names of all resources
|
|
648
|
+
* const resourceNames = ResourcesManager.getAllNames();
|
|
649
|
+
* console.log(resourceNames); // Output: ['userResource']
|
|
650
|
+
*
|
|
651
|
+
* // Retrieve a specific resource
|
|
652
|
+
* const retrievedResource = ResourcesManager.getResource<UserResource>('userResource');
|
|
653
|
+
* if (retrievedResource) {
|
|
654
|
+
* console.log(retrievedResource.getLabel()); // Output: The label of the user resource
|
|
655
|
+
* }
|
|
656
|
+
*/
|
|
657
|
+
export declare class ResourcesManager {
|
|
658
|
+
static resourceMetaData: symbol;
|
|
659
|
+
/**
|
|
660
|
+
* A global constant storing a record of all instantiated resources.
|
|
661
|
+
*
|
|
662
|
+
* This represents a record of all resources, where the keys are derived from `ResourceName`
|
|
663
|
+
* and the values are instances of `Resource`.
|
|
664
|
+
*
|
|
665
|
+
* @example
|
|
666
|
+
* const allResources: IAllResource = {
|
|
667
|
+
* userResource: new UserResource()
|
|
668
|
+
* };
|
|
669
|
+
*/
|
|
670
|
+
private static resources;
|
|
671
|
+
/**
|
|
672
|
+
* Retrieves the global record of all resource metaData managed by the `ResourcesManager`.
|
|
673
|
+
*
|
|
674
|
+
* This method returns a copy of the internal record of resource metaData, which can be used to access
|
|
675
|
+
* the configuration and settings for each registered resource.
|
|
676
|
+
*
|
|
677
|
+
* @returns {Record<ResourceName, ResourceBase>} A copy of the resource metaData record.
|
|
678
|
+
*/
|
|
679
|
+
static getAllMetaData(): Record<ResourceName, ResourceBase>;
|
|
680
|
+
/**
|
|
681
|
+
* Adds resource metaData to the global record managed by the `ResourcesManager`.
|
|
682
|
+
*
|
|
683
|
+
* This method updates the internal record of resource metaData with the provided `metaData` for the given `resourceName`.
|
|
684
|
+
* The updated record is then stored as metadata on the `ResourcesManager` class.
|
|
685
|
+
*
|
|
686
|
+
* @param {ResourceName} resourceName - The unique name of the resource.
|
|
687
|
+
* @param {ResourceBase} metaData - The resource metaData to be associated with the given `resourceName`.
|
|
688
|
+
*/
|
|
689
|
+
static addMetaData(resourceName: ResourceName, metaData: ResourceBase): void;
|
|
690
|
+
/**
|
|
691
|
+
* Retrieves the global record of resource class names managed by the `ResourcesManager`.
|
|
692
|
+
*
|
|
693
|
+
* This method returns a copy of the internal record of resource class names, which can be used to access
|
|
694
|
+
* the class name associated with each registered resource.
|
|
695
|
+
*
|
|
696
|
+
* @returns {Record<string,ResourceName>} A copy of the resource class names record.
|
|
697
|
+
*/
|
|
698
|
+
static getAllClassNames(): Record<string, ResourceName>;
|
|
699
|
+
/**
|
|
700
|
+
* Retrieves the class name associated with the specified resource name.
|
|
701
|
+
*
|
|
702
|
+
* This method looks up the class name for the given `resourceName` in the global record of resource class names
|
|
703
|
+
* managed by the `ResourcesManager`. If the resource name is not found, or is not a valid non-null string, this
|
|
704
|
+
* method will return `undefined`.
|
|
705
|
+
*
|
|
706
|
+
* @param {ResourceName} resourceName - The unique name of the resource to retrieve the class name for.
|
|
707
|
+
* @returns {string | undefined} The class name associated with the specified resource name, or `undefined` if not found.
|
|
708
|
+
*/
|
|
709
|
+
static getNameFromClassName(className: string): ResourceName | undefined;
|
|
710
|
+
/**
|
|
711
|
+
* Retrieves the resource metaData for the specified resource name.
|
|
712
|
+
*
|
|
713
|
+
* This method retrieves the resource metaData associated with the given `resourceName` from the global
|
|
714
|
+
* record of resource metaData managed by the `ResourcesManager`. If the resource name is not a valid
|
|
715
|
+
* non-null string, or if the resource metaData are not found, this method will return `undefined`.
|
|
716
|
+
*
|
|
717
|
+
* @param {ResourceName} resourceName - The unique name of the resource to retrieve the metaData for.
|
|
718
|
+
* @returns {ResourceBase | undefined} The resource metaData for the specified resource name, or `undefined` if not found.
|
|
719
|
+
*/
|
|
720
|
+
static getMetaDataFromName(resourceName: ResourceName): ResourceBase | undefined;
|
|
721
|
+
/**
|
|
722
|
+
* Retrieves the resource metadata associated with the given target class.
|
|
723
|
+
*
|
|
724
|
+
* This function uses reflection to access the metadata stored on the target class using the `@ResourceMeta` decorator.
|
|
725
|
+
* It returns a new object that is a copy of the metadata, which includes properties like `name`, `label`, `title`, and `tooltip`.
|
|
726
|
+
*
|
|
727
|
+
* @param {any} target - The target class or instance from which to retrieve the metadata.
|
|
728
|
+
* @returns {Resource} An object containing the resource metadata for the given target.
|
|
729
|
+
*/
|
|
730
|
+
static getMetaDataFromTarget(target: ClassConstructor): ResourceBase | undefined;
|
|
731
|
+
/**
|
|
732
|
+
* Retrieves the resource metaData for the specified resource class name.
|
|
733
|
+
*
|
|
734
|
+
* This method first looks up the resource name associated with the given class name using the `getNameFromClassName` method.
|
|
735
|
+
* If the resource name is found, it then retrieves the resource metaData for that resource name using the `getMetaData` method.
|
|
736
|
+
* If the resource name is not found, or if the resource metaData are not found, this method will return `undefined`.
|
|
737
|
+
*
|
|
738
|
+
* @param {string} className - The class name of the resource to retrieve the metaData for.
|
|
739
|
+
* @returns {ResourceBase<any, any> | undefined} The resource mata data for the specified resource class name, or `undefined` if not found.
|
|
740
|
+
*/
|
|
741
|
+
static getMetaDataByClassName(className: string): ResourceBase | undefined;
|
|
742
|
+
/**
|
|
743
|
+
* Retrieves the names of all registered resources.
|
|
744
|
+
*
|
|
745
|
+
* This method returns an array of resource names that are currently managed by the `ResourcesManager`.
|
|
746
|
+
*
|
|
747
|
+
* @returns {string[]} An array of resource names.
|
|
748
|
+
*
|
|
749
|
+
* @example
|
|
750
|
+
* const names = ResourcesManager.getAllNames();
|
|
751
|
+
* console.log(names); // Output: ['userResource', 'productResource']
|
|
752
|
+
*/
|
|
753
|
+
static getAllNames(): string[];
|
|
754
|
+
/**
|
|
755
|
+
* Retrieves a resource instance by its name from the `resources` record.
|
|
756
|
+
*
|
|
757
|
+
* @template ResourceInstanceType The type extending `Resource` for the resource being returned.
|
|
758
|
+
* @param {ResourceName} name - The name of the resource to retrieve, as defined in `ResourceName`.
|
|
759
|
+
* @returns {(ResourceInstanceType | null)} The resource instance if it exists, or `null` if the resource is not found.
|
|
760
|
+
*
|
|
761
|
+
* @example
|
|
762
|
+
* const userResource = ResourcesManager.getResource<UserResource>('userResource');
|
|
763
|
+
* if (userResource) {
|
|
764
|
+
* console.log(userResource.getLabel()); // Output: The label of the user resource
|
|
765
|
+
* }
|
|
766
|
+
*/
|
|
767
|
+
static getResource<ResourceInstanceType extends Resource = Resource>(name: ResourceName): ResourceInstanceType | null;
|
|
768
|
+
/**
|
|
769
|
+
* Checks if a resource with the given name exists in the `ResourcesManager`.
|
|
770
|
+
*
|
|
771
|
+
* @param {ResourceName} name - The name of the resource to check.
|
|
772
|
+
* @returns {boolean} `true` if the resource exists, `false` otherwise.
|
|
773
|
+
*/
|
|
774
|
+
static hasResource(name: ResourceName): boolean;
|
|
775
|
+
/**
|
|
776
|
+
* Adds a new resource instance to the manager.
|
|
777
|
+
*
|
|
778
|
+
* @param {ResourceName} name - The unique name of the resource to add.
|
|
779
|
+
* @param {Resource<Name,DataType>} resource - The resource instance to be added.
|
|
780
|
+
* @template DataType The type of data associated with the resource.
|
|
781
|
+
*
|
|
782
|
+
* @example
|
|
783
|
+
* const productResource = new ProductResource();
|
|
784
|
+
* ResourcesManager.addResource('productResource', productResource);
|
|
785
|
+
* console.log(ResourcesManager.getAllNames()); // Output: ['userResource', 'productResource']
|
|
786
|
+
*/
|
|
787
|
+
static addResource<Name extends ResourceName, DataType = unknown>(name: Name, resource: Resource<Name, DataType>): void;
|
|
788
|
+
/**
|
|
789
|
+
* Removes a resource instance from the manager by its name.
|
|
790
|
+
*
|
|
791
|
+
* This method deletes the specified resource from the `resources` record.
|
|
792
|
+
* If the resource exists, it will be removed, and the updated list of resources will be returned.
|
|
793
|
+
*
|
|
794
|
+
* @param {ResourceName} name - The name of the resource to be removed from the manager.
|
|
795
|
+
*
|
|
796
|
+
* @returns {Record<ResourceName, Resource>} The updated record of all remaining resources after the removal.
|
|
797
|
+
*
|
|
798
|
+
* @example
|
|
799
|
+
* // Assuming a resource named 'userResource' has been previously added
|
|
800
|
+
* console.log(ResourcesManager.getAllNames()); // Output: ['userResource', 'productResource']
|
|
801
|
+
*
|
|
802
|
+
* // Remove the user resource
|
|
803
|
+
* ResourcesManager.removeResource('userResource');
|
|
804
|
+
*
|
|
805
|
+
* // Check the remaining resources
|
|
806
|
+
* console.log(ResourcesManager.getAllNames()); // Output: ['productResource']
|
|
807
|
+
*/
|
|
808
|
+
static removeResource(name: ResourceName): Record<ResourceName, Resource>;
|
|
809
|
+
/**
|
|
810
|
+
* Retrieves all resource instances managed by the manager.
|
|
811
|
+
*
|
|
812
|
+
* This method returns a record of all resources currently stored in the `ResourcesManager`.
|
|
813
|
+
* The keys are derived from `ResourceName`, and the values are instances of `Resource`.
|
|
814
|
+
* This allows for easy access to all registered resources.
|
|
815
|
+
*
|
|
816
|
+
* @returns {Record<ResourceName, Resource>} A record containing all resource instances, where each key is a resource name.
|
|
817
|
+
*
|
|
818
|
+
* @example
|
|
819
|
+
* // Retrieve all registered resources
|
|
820
|
+
* const allResources = ResourcesManager.getResources();
|
|
821
|
+
* console.log(allResources);
|
|
822
|
+
* // Output:
|
|
823
|
+
* // {
|
|
824
|
+
* // userResource: UserResourceInstance,
|
|
825
|
+
* // productResource: ProductResourceInstance
|
|
826
|
+
* // }
|
|
827
|
+
*/
|
|
828
|
+
static getResources(): Record<ResourceName, Resource>;
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* A decorator function that adds resource metadata to a class that implements `Resource`
|
|
832
|
+
*
|
|
833
|
+
* This decorator stores the resource properties (`name`, `label`, `title`) using Reflect metadata.
|
|
834
|
+
*
|
|
835
|
+
* @typeParam Datatype - An optional type representing the data that this resource holds. Defaults to `any`.
|
|
836
|
+
* @param metaData - The properties to be set as metadata on the class.
|
|
837
|
+
*
|
|
838
|
+
* @example
|
|
839
|
+
* ```typescript
|
|
840
|
+
* @ResourceMeta({
|
|
841
|
+
* name: "user",
|
|
842
|
+
* label: "User",
|
|
843
|
+
* title: "User Management",
|
|
844
|
+
* })
|
|
845
|
+
* class User {}
|
|
846
|
+
*
|
|
847
|
+
* ```
|
|
848
|
+
*/
|
|
849
|
+
export declare function ResourceMeta<Name extends ResourceName = ResourceName, DataType = unknown, TPrimaryKey extends ResourcePrimaryKey = ResourcePrimaryKey>(metaData?: ResourceBase<Name> & {
|
|
850
|
+
/***
|
|
851
|
+
* whether the resource should be instanciated or not
|
|
852
|
+
*/
|
|
853
|
+
instanciate?: boolean;
|
|
854
|
+
}): (target: typeof Resource<Name, DataType, TPrimaryKey>) => void;
|