@punica/form 1.0.3 → 1.1.1

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.
@@ -1,30 +1,33 @@
1
1
  import { BaseListener } from '@punica/common';
2
- import { Form, FormEvents, FormItem, IReader, IServiceCommand, IServiceInitialize, IServiceControl } from '..';
2
+ import { Form, FormEvents, FormItem, IServiceCommand, IServiceInitialize, IServiceControl } from '..';
3
3
  /**
4
4
  * Controller for managing a form and its associated services.
5
5
  */
6
6
  export declare class FormController<E, F extends FormItem<E>> extends BaseListener<FormEvents> {
7
7
  #private;
8
8
  /**
9
- * Constructor for initializing the FormController with form data.
10
- * @param formData - The initial form data
9
+ * Private constructor to prevent direct instantiation.
11
10
  */
12
- constructor(formData: Form<E, F>);
11
+ private constructor();
13
12
  /**
14
- * Constructor for initializing the FormController with an entity and reader.
15
- * @param entity - The entity for the form
16
- * @param reader - The reader for initializing the form
13
+ * Static method to initialize the FormController with form data.
14
+ * @param form The initial form data
17
15
  */
18
- constructor(entity: E, reader: IReader<E, F>);
16
+ static fromForm<E, F extends FormItem<E>>(form: Form<E, F>): Promise<FormController<E, F>>;
17
+ /**
18
+ * Static method to initialize the FormController with an entity.
19
+ * @param entity The entity for the form
20
+ */
21
+ static fromEntity<E, F extends FormItem<E>>(entity: E): Promise<FormController<E, F>>;
19
22
  /**
20
23
  * Trigger a form event.
21
- * @param eventType - The type of the event
22
- * @param data - Data associated with the event
24
+ * @param eventType The type of the event
25
+ * @param data Data associated with the event
23
26
  */
24
27
  private fireEvent;
25
28
  /**
26
29
  * Create a command item for a form item.
27
- * @param item - The form item
30
+ * @param item The form item
28
31
  * @returns A promise that resolves to a CommandItem
29
32
  */
30
33
  private createCommandItem;
@@ -35,25 +38,49 @@ export declare class FormController<E, F extends FormItem<E>> extends BaseListen
35
38
  private createCommandService;
36
39
  /**
37
40
  * Get a form item by its property key.
38
- * @param property - The property key of the form item
41
+ * @param property The property key of the form item
39
42
  * @returns The form item corresponding to the property key
40
43
  */
41
44
  private getItem;
42
45
  /**
43
46
  * Write an array of form items to the form data.
44
- * @param items - An array of form items
47
+ * @param items An array of form items
45
48
  */
46
49
  private writeItems;
47
50
  /**
48
- * Get services by name(s).
49
- * @param serviceName - The name of the service
50
- * @param additionalServiceNames - Additional names of services
51
+ * Get services by their names.
52
+ * @param serviceName The name of the service
53
+ * @param additionalServiceNames Additional names of services
51
54
  * @returns The requested services
52
55
  */
53
56
  getServices<T = IServiceInitialize<E, F> & IServiceControl & IServiceCommand, R = T & Array<T>>(serviceName: string, ...additionalServiceNames: string[]): R;
54
57
  /**
55
- * Start the form controller.
56
- * @returns A promise that resolves to the form data
58
+ * Registers form items by triggering the REGISTER_ITEMS event.
59
+ */
60
+ private registerFormItems;
61
+ /**
62
+ * Maps form items to their properties.
63
+ */
64
+ private mapFormItems;
65
+ /**
66
+ * Initializes the services defined in the form data.
67
+ */
68
+ private initializeServices;
69
+ /**
70
+ * Executes the readers defined in the form data.
71
+ */
72
+ private runReaders;
73
+ /**
74
+ * Executes the starters defined in the form data.
75
+ */
76
+ private runStarters;
77
+ /**
78
+ * Creates a deep copy of the form data for the initial state.
79
+ */
80
+ private deepCopyForm;
81
+ /**
82
+ * Starts the form controller by executing all necessary steps to initialize the form.
83
+ * @returns A promise that resolves when the form is fully initialized.
57
84
  */
58
85
  start(): Promise<Form<E, F>>;
59
86
  }
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _FormController_serviceMap, _FormController_itemsMap, _FormController_formData, _FormController_reader, _FormController_initialFormData, _FormController_initialEntity;
13
+ var _FormController_serviceMap, _FormController_itemsMap, _FormController_form, _FormController_initialForm, _FormController_initialEntity;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.FormController = void 0;
16
16
  const common_1 = require("@punica/common");
@@ -19,48 +19,70 @@ const __1 = require("..");
19
19
  * Controller for managing a form and its associated services.
20
20
  */
21
21
  class FormController extends common_1.BaseListener {
22
+ //#region constructor
22
23
  /**
23
- *
24
- * @param args
24
+ * Private constructor to prevent direct instantiation.
25
25
  */
26
- constructor(...args) {
26
+ constructor() {
27
27
  super();
28
- _FormController_serviceMap.set(this, void 0);
29
- _FormController_itemsMap.set(this, void 0);
30
- _FormController_formData.set(this, void 0);
31
- _FormController_reader.set(this, void 0);
32
- _FormController_initialFormData.set(this, void 0);
33
- _FormController_initialEntity.set(this, void 0);
34
- if (args.length == 1) {
35
- const [formData] = args;
36
- __classPrivateFieldSet(this, _FormController_formData, formData, "f");
37
- }
38
- else if (args.length == 2) {
39
- const [entity, reader] = args;
40
- __classPrivateFieldSet(this, _FormController_initialEntity, entity, "f");
41
- __classPrivateFieldSet(this, _FormController_reader, reader, "f");
42
- }
28
+ _FormController_serviceMap.set(this, void 0); // Map to store services by their names
29
+ _FormController_itemsMap.set(this, void 0); // Map to store form items by their properties
30
+ _FormController_form.set(this, void 0); // Current form data
31
+ _FormController_initialForm.set(this, void 0); // Initial deep copy of the form data
32
+ _FormController_initialEntity.set(this, void 0); // Initial entity for the form
43
33
  __classPrivateFieldSet(this, _FormController_serviceMap, {}, "f");
44
34
  __classPrivateFieldSet(this, _FormController_itemsMap, {}, "f");
45
35
  }
36
+ /**
37
+ * Static method to initialize the FormController with form data.
38
+ * @param form The initial form data
39
+ */
40
+ static fromForm(form) {
41
+ return new Promise((resolve) => {
42
+ const instance = new FormController();
43
+ __classPrivateFieldSet(instance, _FormController_form, form, "f");
44
+ __classPrivateFieldSet(instance, _FormController_initialEntity, {}, "f");
45
+ resolve(instance);
46
+ });
47
+ }
48
+ /**
49
+ * Static method to initialize the FormController with an entity.
50
+ * @param entity The entity for the form
51
+ */
52
+ static fromEntity(entity) {
53
+ return new Promise(async (resolve) => {
54
+ const instance = new FormController();
55
+ const initialReader = (0, __1.createInitialReader)();
56
+ __classPrivateFieldSet(instance, _FormController_initialEntity, entity, "f");
57
+ __classPrivateFieldSet(instance, _FormController_form, {
58
+ itemsMap: null,
59
+ items: null,
60
+ services: null,
61
+ starters: null,
62
+ readers: null
63
+ }, "f");
64
+ await initialReader.read(__classPrivateFieldGet(instance, _FormController_initialEntity, "f"), __classPrivateFieldGet(instance, _FormController_form, "f"));
65
+ resolve(instance);
66
+ });
67
+ }
46
68
  //#endregion
47
69
  /**
48
70
  * Trigger a form event.
49
- * @param eventType - The type of the event
50
- * @param data - Data associated with the event
71
+ * @param eventType The type of the event
72
+ * @param data Data associated with the event
51
73
  */
52
74
  fireEvent(eventType, data) {
53
75
  this.trigger(eventType, data);
54
76
  }
55
77
  /**
56
78
  * Create a command item for a form item.
57
- * @param item - The form item
79
+ * @param item The form item
58
80
  * @returns A promise that resolves to a CommandItem
59
81
  */
60
82
  async createCommandItem(item) {
61
83
  var _a;
62
84
  let itemCustomCommand = {};
63
- for await (const service of (_a = __classPrivateFieldGet(this, _FormController_formData, "f")) === null || _a === void 0 ? void 0 : _a.services) {
85
+ for await (const service of (_a = __classPrivateFieldGet(this, _FormController_form, "f")) === null || _a === void 0 ? void 0 : _a.services) {
64
86
  const { addCustomFeaturesForCommandItem } = service;
65
87
  if (addCustomFeaturesForCommandItem) {
66
88
  const command = addCustomFeaturesForCommandItem();
@@ -81,9 +103,9 @@ class FormController extends common_1.BaseListener {
81
103
  */
82
104
  createCommandService() {
83
105
  return {
84
- initialFormData: __classPrivateFieldGet(this, _FormController_initialFormData, "f"),
106
+ initialForm: __classPrivateFieldGet(this, _FormController_initialForm, "f"),
85
107
  initialEntity: __classPrivateFieldGet(this, _FormController_initialEntity, "f"),
86
- formData: __classPrivateFieldGet(this, _FormController_formData, "f"),
108
+ form: __classPrivateFieldGet(this, _FormController_form, "f"),
87
109
  itemsMap: __classPrivateFieldGet(this, _FormController_itemsMap, "f"),
88
110
  getItem: this.getItem.bind(this),
89
111
  writeItems: this.writeItems.bind(this),
@@ -93,11 +115,11 @@ class FormController extends common_1.BaseListener {
93
115
  }
94
116
  /**
95
117
  * Get a form item by its property key.
96
- * @param property - The property key of the form item
118
+ * @param property The property key of the form item
97
119
  * @returns The form item corresponding to the property key
98
120
  */
99
121
  getItem(property) {
100
- const { items, itemsMap } = __classPrivateFieldGet(this, _FormController_formData, "f");
122
+ const { items, itemsMap } = __classPrivateFieldGet(this, _FormController_form, "f");
101
123
  const itemIndex = itemsMap[property];
102
124
  if (!Number.isInteger(itemIndex)) {
103
125
  return null;
@@ -106,20 +128,20 @@ class FormController extends common_1.BaseListener {
106
128
  }
107
129
  /**
108
130
  * Write an array of form items to the form data.
109
- * @param items - An array of form items
131
+ * @param items An array of form items
110
132
  */
111
133
  async writeItems(items) {
112
134
  for await (const item of items) {
113
- const { itemsMap, items } = __classPrivateFieldGet(this, _FormController_formData, "f");
135
+ const { itemsMap, items } = __classPrivateFieldGet(this, _FormController_form, "f");
114
136
  const { property } = item;
115
137
  const index = itemsMap[property];
116
138
  items[index] = item;
117
139
  }
118
140
  }
119
141
  /**
120
- * Get services by name(s).
121
- * @param serviceName - The name of the service
122
- * @param additionalServiceNames - Additional names of services
142
+ * Get services by their names.
143
+ * @param serviceName The name of the service
144
+ * @param additionalServiceNames Additional names of services
123
145
  * @returns The requested services
124
146
  */
125
147
  getServices(serviceName, ...additionalServiceNames) {
@@ -134,54 +156,83 @@ class FormController extends common_1.BaseListener {
134
156
  return __classPrivateFieldGet(this, _FormController_serviceMap, "f")[serviceName];
135
157
  }
136
158
  /**
137
- * Start the form controller.
138
- * @returns A promise that resolves to the form data
159
+ * Registers form items by triggering the REGISTER_ITEMS event.
139
160
  */
140
- start() {
141
- return new Promise(async (resolve) => {
142
- var _a;
143
- // Trigger the REGISTER_ITEMS event with the registered item keys
144
- this.fireEvent('REGISTER_ITEMS', __1.FormItemRegister.getInstance().getItemKeys());
145
- if (__classPrivateFieldGet(this, _FormController_formData, "f") == null) {
146
- const initialReader = (0, __1.createInitialReader)();
147
- __classPrivateFieldSet(this, _FormController_formData, await initialReader.read(__classPrivateFieldGet(this, _FormController_initialEntity, "f")), "f");
148
- if (__classPrivateFieldGet(this, _FormController_reader, "f")) {
149
- __classPrivateFieldSet(this, _FormController_formData, await __classPrivateFieldGet(this, _FormController_reader, "f").read(__classPrivateFieldGet(this, _FormController_initialEntity, "f"), __classPrivateFieldGet(this, _FormController_formData, "f")), "f");
161
+ registerFormItems() {
162
+ this.fireEvent('REGISTER_ITEMS', __1.FormItemRegister.getInstance().getItemKeys());
163
+ }
164
+ /**
165
+ * Maps form items to their properties.
166
+ */
167
+ mapFormItems() {
168
+ let index = 0;
169
+ __classPrivateFieldSet(this, _FormController_itemsMap, {}, "f");
170
+ for (const item of __classPrivateFieldGet(this, _FormController_form, "f").items) {
171
+ __classPrivateFieldGet(this, _FormController_itemsMap, "f")[item.property] = index++;
172
+ }
173
+ }
174
+ /**
175
+ * Initializes the services defined in the form data.
176
+ */
177
+ async initializeServices() {
178
+ if (__classPrivateFieldGet(this, _FormController_form, "f").services) {
179
+ for await (const service of __classPrivateFieldGet(this, _FormController_form, "f").services) {
180
+ const command = this.createCommandService();
181
+ const serviceControl = service;
182
+ __classPrivateFieldGet(this, _FormController_serviceMap, "f")[service.name] = service;
183
+ if (serviceControl.initialize) {
184
+ await serviceControl.initialize(command);
150
185
  }
151
186
  }
152
- else {
153
- __classPrivateFieldSet(this, _FormController_initialEntity, {}, "f");
154
- }
155
- // Create item map
156
- let index = 0;
157
- __classPrivateFieldSet(this, _FormController_itemsMap, {}, "f");
158
- for await (const item of __classPrivateFieldGet(this, _FormController_formData, "f").items) {
159
- __classPrivateFieldGet(this, _FormController_itemsMap, "f")[item.property] = index++;
160
- }
161
- // Initialize services
162
- // The services you want to use are started
163
- if (__classPrivateFieldGet(this, _FormController_formData, "f").services) {
164
- for await (const service of (_a = __classPrivateFieldGet(this, _FormController_formData, "f")) === null || _a === void 0 ? void 0 : _a.services) {
165
- const command = this.createCommandService();
166
- const serviceControl = service;
167
- __classPrivateFieldGet(this, _FormController_serviceMap, "f")[service.name] = service;
168
- if (serviceControl.initialize) {
169
- serviceControl.initialize(command);
170
- }
171
- }
187
+ }
188
+ }
189
+ /**
190
+ * Executes the readers defined in the form data.
191
+ */
192
+ async runReaders() {
193
+ if (__classPrivateFieldGet(this, _FormController_form, "f").readers) {
194
+ for await (const reader of __classPrivateFieldGet(this, _FormController_form, "f").readers) {
195
+ reader.read(__classPrivateFieldGet(this, _FormController_initialEntity, "f"), __classPrivateFieldGet(this, _FormController_form, "f"));
172
196
  }
173
- // Executed starter methods
174
- if (__classPrivateFieldGet(this, _FormController_formData, "f").starters) {
175
- for await (const starter of __classPrivateFieldGet(this, _FormController_formData, "f").starters) {
176
- // Execute starter run methods
177
- __classPrivateFieldSet(this, _FormController_formData, await starter.run(__classPrivateFieldGet(this, _FormController_formData, "f"), __classPrivateFieldGet(this, _FormController_initialEntity, "f")), "f");
178
- }
197
+ }
198
+ }
199
+ /**
200
+ * Executes the starters defined in the form data.
201
+ */
202
+ async runStarters() {
203
+ if (__classPrivateFieldGet(this, _FormController_form, "f").starters) {
204
+ for await (const starter of __classPrivateFieldGet(this, _FormController_form, "f").starters) {
205
+ __classPrivateFieldSet(this, _FormController_form, await starter.run(__classPrivateFieldGet(this, _FormController_form, "f"), __classPrivateFieldGet(this, _FormController_initialEntity, "f")), "f");
179
206
  }
180
- // Deep clone form data
181
- __classPrivateFieldSet(this, _FormController_initialFormData, (0, __1.deepCopy)(__classPrivateFieldGet(this, _FormController_formData, "f")), "f");
182
- resolve(__classPrivateFieldGet(this, _FormController_formData, "f"));
183
- });
207
+ }
208
+ }
209
+ /**
210
+ * Creates a deep copy of the form data for the initial state.
211
+ */
212
+ deepCopyForm() {
213
+ __classPrivateFieldSet(this, _FormController_initialForm, (0, __1.deepCopy)(__classPrivateFieldGet(this, _FormController_form, "f")), "f");
214
+ }
215
+ /**
216
+ * Starts the form controller by executing all necessary steps to initialize the form.
217
+ * @returns A promise that resolves when the form is fully initialized.
218
+ */
219
+ async start() {
220
+ try {
221
+ this.registerFormItems();
222
+ this.mapFormItems();
223
+ await this.initializeServices();
224
+ await this.runReaders();
225
+ await this.runStarters();
226
+ this.mapFormItems();
227
+ this.deepCopyForm();
228
+ // Trigger an update event after the form is fully initialized
229
+ return __classPrivateFieldGet(this, _FormController_form, "f");
230
+ }
231
+ catch (error) {
232
+ console.error('Error starting the form controller:', error);
233
+ throw error;
234
+ }
184
235
  }
185
236
  }
186
237
  exports.FormController = FormController;
187
- _FormController_serviceMap = new WeakMap(), _FormController_itemsMap = new WeakMap(), _FormController_formData = new WeakMap(), _FormController_reader = new WeakMap(), _FormController_initialFormData = new WeakMap(), _FormController_initialEntity = new WeakMap();
238
+ _FormController_serviceMap = new WeakMap(), _FormController_itemsMap = new WeakMap(), _FormController_form = new WeakMap(), _FormController_initialForm = new WeakMap(), _FormController_initialEntity = new WeakMap();
@@ -0,0 +1,9 @@
1
+ import { FormItem, IReader } from '..';
2
+ /**
3
+ * Constant for identifying form readers decorator.
4
+ */
5
+ export declare const DECORATOR_READERS = "form-readers";
6
+ /**
7
+ * Decorator for adding readers to a form class.
8
+ */
9
+ export declare const Readers: <E, F extends FormItem<E>>(data: IReader<E, F>[]) => ClassDecorator;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Readers = exports.DECORATOR_READERS = void 0;
4
+ const __1 = require("..");
5
+ /**
6
+ * Constant for identifying form readers decorator.
7
+ */
8
+ exports.DECORATOR_READERS = 'form-readers';
9
+ /**
10
+ * Decorator for adding readers to a form class.
11
+ */
12
+ const Readers = (data) => {
13
+ return (0, __1.defineFormProperty)(exports.DECORATOR_READERS)(data);
14
+ };
15
+ exports.Readers = Readers;
@@ -1,4 +1,3 @@
1
- import { IClassDecorator } from '@punica/common';
2
1
  import { IService } from '..';
3
2
  /**
4
3
  * Constant for identifying form services decorator.
@@ -7,4 +6,4 @@ export declare const DECORATOR_SERVICES = "form-services";
7
6
  /**
8
7
  * Decorator for adding services to a form class.
9
8
  */
10
- export declare const Services: IClassDecorator<Array<IService>>;
9
+ export declare const Services: (data: Array<IService>) => ClassDecorator;
@@ -9,4 +9,7 @@ exports.DECORATOR_SERVICES = 'form-services';
9
9
  /**
10
10
  * Decorator for adding services to a form class.
11
11
  */
12
- exports.Services = (0, __1.defineFormProperty)(exports.DECORATOR_SERVICES);
12
+ const Services = (data) => {
13
+ return (0, __1.defineFormProperty)(exports.DECORATOR_SERVICES)(data);
14
+ };
15
+ exports.Services = Services;
@@ -1,5 +1,4 @@
1
- import { IClassDecorator } from '@punica/common';
2
- import { IStarter } from '..';
1
+ import { FormItem, IStarter } from '..';
3
2
  /**
4
3
  * Constant for identifying form starters decorator.
5
4
  */
@@ -7,4 +6,4 @@ export declare const DECORATOR_STARTERS = "form-starters";
7
6
  /**
8
7
  * Decorator for adding starters to a form class.
9
8
  */
10
- export declare const Starters: IClassDecorator<Array<IStarter<any, any>>>;
9
+ export declare const Starters: <E, F extends FormItem<E>>(data: IStarter<E, F>[]) => ClassDecorator;
@@ -9,4 +9,7 @@ exports.DECORATOR_STARTERS = 'form-starters';
9
9
  /**
10
10
  * Decorator for adding starters to a form class.
11
11
  */
12
- exports.Starters = (0, __1.defineFormProperty)(exports.DECORATOR_STARTERS);
12
+ const Starters = (data) => {
13
+ return (0, __1.defineFormProperty)(exports.DECORATOR_STARTERS)(data);
14
+ };
15
+ exports.Starters = Starters;
@@ -1,2 +1,3 @@
1
+ export * from './Readers';
1
2
  export * from './Starters';
2
3
  export * from './Services';
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Readers"), exports);
17
18
  __exportStar(require("./Starters"), exports);
18
19
  __exportStar(require("./Services"), exports);
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  * @returns {IPropertyDecorator<T>} - Decorator function.
7
7
  */
8
8
  const defineFormItemProperty = (type) => {
9
- // Register the form item type and renderer
10
9
  return (data) => {
11
10
  return (target, propertyKey) => {
12
11
  const allMetadata = Reflect.getMetadata(type, target) || {};
@@ -1,8 +1,7 @@
1
- import { IClassDecorator } from '@punica/common';
2
1
  /**
3
2
  * Defines a form property with the specified type.
4
3
  * @param {string} type - Type of the form property.
5
- * @returns {IClassDecorator<T>} - Decorator function.
4
+ * @returns {ClassDecorator<T>} - Decorator function.
6
5
  */
7
- declare const defineFormProperty: <T>(type: string) => IClassDecorator<T>;
6
+ declare const defineFormProperty: <T>(type: string) => (data: T) => ClassDecorator;
8
7
  export default defineFormProperty;
@@ -3,13 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  /**
4
4
  * Defines a form property with the specified type.
5
5
  * @param {string} type - Type of the form property.
6
- * @returns {IClassDecorator<T>} - Decorator function.
6
+ * @returns {ClassDecorator<T>} - Decorator function.
7
7
  */
8
8
  const defineFormProperty = (type) => {
9
9
  return (data) => {
10
10
  return (target) => {
11
11
  Reflect.defineMetadata(type, data, target.prototype);
12
- return target;
13
12
  };
14
13
  };
15
14
  };
@@ -4,9 +4,9 @@ import { CommandItem } from '.';
4
4
  * Defines the structure of the CommandService.
5
5
  */
6
6
  export type CommandService<E, F extends FormItem<E>> = {
7
- initialFormData: Form<E, F>;
7
+ initialForm: Form<E, F>;
8
8
  initialEntity: E;
9
- formData: Form<E, F>;
9
+ form: Form<E, F>;
10
10
  itemsMap: Record<keyof E, number>;
11
11
  createCommandItem: (item: F) => Promise<CommandItem<E, F>>;
12
12
  fireEvent: (eventType: FormEvents, data: any) => void;
@@ -1,10 +1,11 @@
1
- import { FormItem, IService, IStarter } from '../..';
1
+ import { FormItem, IReader, IService, IStarter } from '../..';
2
2
  export type Form<E = any, F extends FormItem<E> = any> = {
3
3
  itemsMap: {
4
4
  [key in keyof E]: number;
5
5
  };
6
6
  items: Array<F>;
7
7
  services?: Array<IService>;
8
+ readers?: Array<IReader<E, F>>;
8
9
  starters?: Array<IStarter<E, F>>;
9
10
  [key: string]: any;
10
11
  };
@@ -1,5 +1,5 @@
1
1
  import { CommandItem } from '../command';
2
- export type FormItem<E = any> = {
2
+ export type FormItem<E> = {
3
3
  property: keyof E;
4
4
  path?: string;
5
5
  label: string;
@@ -1,4 +1,4 @@
1
1
  import { Form, FormItem } from '..';
2
2
  export interface IReader<E, F extends FormItem<E>> {
3
- read(entity: E, initialFormData?: Form<E, F>): Promise<Form<E, F>>;
3
+ read(entity: E, form: Form<E, F>): Promise<void>;
4
4
  }
@@ -3,7 +3,7 @@ export interface IService {
3
3
  get name(): string;
4
4
  }
5
5
  export interface IServiceInitialize<E, F extends FormItem<E>> extends IService {
6
- initialize: (command: CommandService<E, F>) => void;
6
+ initialize: (command: CommandService<E, F>) => Promise<void>;
7
7
  }
8
8
  export interface IServiceControl<T = any> extends IService {
9
9
  run: (...args: any[]) => Promise<T>;
@@ -5,9 +5,9 @@ import { Form, FormItem } from '..';
5
5
  export interface IStarter<E, F extends FormItem<E>> {
6
6
  /**
7
7
  * Function to run the starter logic.
8
- * @param formData - The current form data.
8
+ * @param form - The current form data.
9
9
  * @param entity - The entity associated with the form.
10
10
  * @returns A Promise that resolves to the updated form data after initialization.
11
11
  */
12
- run: (formData: Form<E, F>, entity: E) => Promise<Form<E, F>>;
12
+ run: (form: Form<E, F>, entity: E) => Promise<Form<E, F>>;
13
13
  }
@@ -19,9 +19,9 @@ declare abstract class BaseReader<E, F extends FormItem<E>> implements IReader<E
19
19
  /**
20
20
  * Reads form data from the entity.
21
21
  * @param {E} entity - The entity to read form data from.
22
- * @returns {Promise<Form<E, F>>} - Resolves to the form data.
22
+ * @param form
23
23
  */
24
- read(entity: E): Promise<Form<E, F>>;
24
+ read(entity: E, form: Form<E, F>): Promise<void>;
25
25
  /**
26
26
  * Gets a form item by property.
27
27
  * @param {keyof E} property - The property of the form item.
@@ -40,12 +40,12 @@ class BaseReader {
40
40
  /**
41
41
  * Reads form data from the entity.
42
42
  * @param {E} entity - The entity to read form data from.
43
- * @returns {Promise<Form<E, F>>} - Resolves to the form data.
43
+ * @param form
44
44
  */
45
- async read(entity) {
45
+ async read(entity, form) {
46
46
  __classPrivateFieldSet(this, _BaseReader_entity, entity, "f");
47
- __classPrivateFieldSet(this, _BaseReader_form, await this.reader.read(entity), "f");
48
- return __classPrivateFieldGet(this, _BaseReader_form, "f");
47
+ __classPrivateFieldSet(this, _BaseReader_form, form, "f");
48
+ await this.reader.read(entity, form);
49
49
  }
50
50
  /**
51
51
  * Gets a form item by property.
@@ -8,10 +8,10 @@ declare class Reader<E, F extends FormItem<E>> extends BaseReader<E, F> {
8
8
  */
9
9
  private createFormItemsFromEntity;
10
10
  /**
11
- * Creates the form from the entity.
12
- * @param entity - The entity to be read
13
- * @returns Promise<Form<E, F>> - Created form
11
+ * Reads form data from the entity.
12
+ * @param {E} entity - The entity to read form data from.
13
+ * @param form
14
14
  */
15
- read(entity: E): Promise<Form<E, F>>;
15
+ read(entity: E, form: Form<E, F>): Promise<void>;
16
16
  }
17
17
  export default Reader;
@@ -37,12 +37,12 @@ class Reader extends base_1.default {
37
37
  });
38
38
  }
39
39
  /**
40
- * Creates the form from the entity.
41
- * @param entity - The entity to be read
42
- * @returns Promise<Form<E, F>> - Created form
40
+ * Reads form data from the entity.
41
+ * @param {E} entity - The entity to read form data from.
42
+ * @param form
43
43
  */
44
- async read(entity) {
45
- const form = await super.read(entity);
44
+ async read(entity, form) {
45
+ await super.read(entity, form);
46
46
  // Create form items from the entity
47
47
  form.items = await this.createFormItemsFromEntity(entity);
48
48
  form.itemsMap = {};
@@ -50,7 +50,6 @@ class Reader extends base_1.default {
50
50
  form.items.forEach((item, index) => {
51
51
  form.itemsMap[item.property] = index;
52
52
  });
53
- return form;
54
53
  }
55
54
  }
56
55
  exports.default = Reader;
@@ -15,10 +15,10 @@ declare class Reader<E, F extends FormItem<E>> extends BaseReader<E, F> {
15
15
  */
16
16
  private readItemsData;
17
17
  /**
18
- * Reads the entity and updates form items with their corresponding data.
19
- * @param entity - The entity to be read.
20
- * @returns The form with updated form item values.
18
+ * Reads form data from the entity.
19
+ * @param {E} entity - The entity to read form data from.
20
+ * @param form
21
21
  */
22
- read(entity: E): Promise<Form<E, F>>;
22
+ read(entity: E, form: Form<E, F>): Promise<void>;
23
23
  }
24
24
  export default Reader;
@@ -30,14 +30,13 @@ class Reader extends base_1.default {
30
30
  }
31
31
  }
32
32
  /**
33
- * Reads the entity and updates form items with their corresponding data.
34
- * @param entity - The entity to be read.
35
- * @returns The form with updated form item values.
33
+ * Reads form data from the entity.
34
+ * @param {E} entity - The entity to read form data from.
35
+ * @param form
36
36
  */
37
- async read(entity) {
38
- const form = await super.read(entity);
37
+ async read(entity, form) {
38
+ await super.read(entity, form);
39
39
  await this.readItemsData(entity, form.items);
40
- return form;
41
40
  }
42
41
  }
43
42
  exports.default = Reader;
@@ -7,10 +7,10 @@ declare class Reader<E, F extends FormItem<E>> extends BaseReader<E, F> {
7
7
  */
8
8
  private readItemsData;
9
9
  /**
10
- * Reads an entity and updates form items with their corresponding data.
11
- * @param entity - The entity to be read.
12
- * @returns The form with updated form item values.
10
+ * Reads form data from the entity.
11
+ * @param {E} entity - The entity to read form data from.
12
+ * @param form
13
13
  */
14
- read(entity: E): Promise<Form<E, F>>;
14
+ read(entity: E, form: Form<E, F>): Promise<void>;
15
15
  }
16
16
  export default Reader;
@@ -21,15 +21,14 @@ class Reader extends base_1.default {
21
21
  }
22
22
  }
23
23
  /**
24
- * Reads an entity and updates form items with their corresponding data.
25
- * @param entity - The entity to be read.
26
- * @returns The form with updated form item values.
24
+ * Reads form data from the entity.
25
+ * @param {E} entity - The entity to read form data from.
26
+ * @param form
27
27
  */
28
- async read(entity) {
29
- const form = await super.read(entity);
28
+ async read(entity, form) {
29
+ await super.read(entity, form);
30
30
  // Read and initialize form items.
31
31
  this.readItemsData(form.items);
32
- return form;
33
32
  }
34
33
  }
35
34
  exports.default = Reader;
@@ -3,18 +3,16 @@ import { FormItem, Form, IReader } from '../..';
3
3
  * Class for reading form data.
4
4
  */
5
5
  declare class Reader<E, F extends FormItem<E>> implements IReader<E, F> {
6
- #private;
7
6
  /**
8
- * Initializes a new instance of the Reader class.
9
- * @param {Form<E, F>} initialForm - Initial form data.
7
+ *
10
8
  */
11
- constructor(initialForm?: Form<E, F>);
9
+ constructor();
12
10
  /**
13
11
  * Reads the form data from the provided entity.
14
12
  * @param {E} entity - The entity to read form data from.
15
- * @returns {Promise<Form<E, F>>} - Resolves to the form data.
13
+ * @returns {Promise<Form<E, F>>} form - Resolves to the form data.
16
14
  * @throws {Error} - If the entity is not provided.
17
15
  */
18
- read(entity: E): Promise<Form<E, F>>;
16
+ read(entity: E, form: Form<E, F>): Promise<void>;
19
17
  }
20
18
  export default Reader;
@@ -1,16 +1,4 @@
1
1
  "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _Reader_form;
14
2
  Object.defineProperty(exports, "__esModule", { value: true });
15
3
  const __1 = require("../..");
16
4
  /**
@@ -18,39 +6,25 @@ const __1 = require("../..");
18
6
  */
19
7
  class Reader {
20
8
  /**
21
- * Initializes a new instance of the Reader class.
22
- * @param {Form<E, F>} initialForm - Initial form data.
9
+ *
23
10
  */
24
- constructor(initialForm = null) {
25
- _Reader_form.set(this, void 0);
26
- // Initializes an empty form if initialForm is not provided
27
- __classPrivateFieldSet(this, _Reader_form, initialForm || {
28
- itemsMap: null,
29
- items: null,
30
- services: null,
31
- starters: null
32
- }, "f");
33
- }
11
+ constructor() { }
34
12
  /**
35
13
  * Reads the form data from the provided entity.
36
14
  * @param {E} entity - The entity to read form data from.
37
- * @returns {Promise<Form<E, F>>} - Resolves to the form data.
15
+ * @returns {Promise<Form<E, F>>} form - Resolves to the form data.
38
16
  * @throws {Error} - If the entity is not provided.
39
17
  */
40
- read(entity) {
41
- return new Promise((resolve, reject) => {
42
- if (!entity) {
43
- // Throws an error if the entity is not provided
44
- reject(new Error('Entity is required'));
45
- }
18
+ read(entity, form) {
19
+ return new Promise((resolve) => {
46
20
  // Retrieves services metadata from the entity using reflection
47
- __classPrivateFieldGet(this, _Reader_form, "f").services = Reflect.getMetadata(__1.DECORATOR_SERVICES, entity);
21
+ form.services = Reflect.getMetadata(__1.DECORATOR_SERVICES, entity);
22
+ // Retrieves starters metadata from the entity using reflection
23
+ form.starters = Reflect.getMetadata(__1.DECORATOR_STARTERS, entity);
48
24
  // Retrieves starters metadata from the entity using reflection
49
- __classPrivateFieldGet(this, _Reader_form, "f").starters = Reflect.getMetadata(__1.DECORATOR_STARTERS, entity);
50
- // Resolves the promise with the form data
51
- resolve(__classPrivateFieldGet(this, _Reader_form, "f"));
25
+ form.readers = Reflect.getMetadata(__1.DECORATOR_READERS, entity);
26
+ resolve();
52
27
  });
53
28
  }
54
29
  }
55
- _Reader_form = new WeakMap();
56
30
  exports.default = Reader;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@punica/form",
3
- "version": "1.0.3",
3
+ "version": "1.1.1",
4
4
  "description": "Form",
5
5
  "author": {
6
6
  "name": "Uryan Sezgin TEKE",
@@ -36,5 +36,5 @@
36
36
  "@punica/common": "^1.0.1",
37
37
  "@punica/util": "^1.0.0"
38
38
  },
39
- "gitHead": "5b97f5cf59f2898ff7aeb3fb4a915b9fdeddd9a8"
39
+ "gitHead": "767aa46806a76af1f985c53f0d4a06e2b852fd0a"
40
40
  }