@aerogel/core 0.0.0-next.fd1bd21aea7a9ab8c4eab69a5f5776db5de8bf35 → 0.1.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/dist/aerogel-core.css +1 -0
- package/dist/aerogel-core.d.ts +2263 -1698
- package/dist/aerogel-core.js +3809 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +32 -37
- package/src/bootstrap/bootstrap.test.ts +4 -8
- package/src/bootstrap/index.ts +25 -16
- package/src/bootstrap/options.ts +1 -1
- package/src/components/AppLayout.vue +14 -0
- package/src/components/{AGAppModals.vue → AppModals.vue} +3 -4
- package/src/components/AppOverlays.vue +9 -0
- package/src/components/AppToasts.vue +16 -0
- package/src/components/contracts/AlertModal.ts +19 -0
- package/src/components/contracts/Button.ts +16 -0
- package/src/components/contracts/ConfirmModal.ts +48 -0
- package/src/components/contracts/DropdownMenu.ts +25 -0
- package/src/components/contracts/ErrorReportModal.ts +33 -0
- package/src/components/contracts/Input.ts +26 -0
- package/src/components/contracts/LoadingModal.ts +26 -0
- package/src/components/contracts/Modal.ts +21 -0
- package/src/components/contracts/PromptModal.ts +34 -0
- package/src/components/contracts/Select.ts +45 -0
- package/src/components/contracts/Toast.ts +15 -0
- package/src/components/contracts/index.ts +11 -0
- package/src/components/headless/HeadlessButton.vue +51 -0
- package/src/components/headless/HeadlessInput.vue +59 -0
- package/src/components/headless/{forms/AGHeadlessInputDescription.vue → HeadlessInputDescription.vue} +7 -8
- package/src/components/headless/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
- package/src/components/headless/HeadlessInputInput.vue +86 -0
- package/src/components/headless/{forms/AGHeadlessInputLabel.vue → HeadlessInputLabel.vue} +3 -7
- package/src/components/headless/{forms/AGHeadlessInputTextArea.vue → HeadlessInputTextArea.vue} +11 -11
- package/src/components/headless/HeadlessModal.vue +57 -0
- package/src/components/headless/HeadlessModalContent.vue +30 -0
- package/src/components/headless/HeadlessModalDescription.vue +12 -0
- package/src/components/headless/HeadlessModalOverlay.vue +12 -0
- package/src/components/headless/HeadlessModalTitle.vue +12 -0
- package/src/components/headless/HeadlessSelect.vue +120 -0
- package/src/components/headless/{forms/AGHeadlessSelectError.vue → HeadlessSelectError.vue} +5 -6
- package/src/components/headless/HeadlessSelectLabel.vue +25 -0
- package/src/components/headless/HeadlessSelectOption.vue +34 -0
- package/src/components/headless/HeadlessSelectOptions.vue +42 -0
- package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
- package/src/components/headless/HeadlessSelectValue.vue +18 -0
- package/src/components/headless/HeadlessSwitch.vue +96 -0
- package/src/components/headless/HeadlessToast.vue +18 -0
- package/src/components/headless/HeadlessToastAction.vue +13 -0
- package/src/components/headless/index.ts +20 -3
- package/src/components/index.ts +6 -9
- package/src/components/ui/AdvancedOptions.vue +18 -0
- package/src/components/ui/AlertModal.vue +17 -0
- package/src/components/ui/Button.vue +115 -0
- package/src/components/ui/Checkbox.vue +56 -0
- package/src/components/ui/ConfirmModal.vue +50 -0
- package/src/components/ui/DropdownMenu.vue +32 -0
- package/src/components/ui/DropdownMenuOption.vue +22 -0
- package/src/components/ui/DropdownMenuOptions.vue +44 -0
- package/src/components/ui/EditableContent.vue +82 -0
- package/src/components/ui/ErrorLogs.vue +19 -0
- package/src/components/ui/ErrorLogsModal.vue +48 -0
- package/src/components/ui/ErrorMessage.vue +15 -0
- package/src/components/ui/ErrorReportModal.vue +73 -0
- package/src/components/{modals/AGErrorReportModalButtons.vue → ui/ErrorReportModalButtons.vue} +34 -27
- package/src/components/ui/ErrorReportModalTitle.vue +24 -0
- package/src/components/{forms/AGForm.vue → ui/Form.vue} +4 -5
- package/src/components/ui/Input.vue +56 -0
- package/src/components/ui/Link.vue +12 -0
- package/src/components/ui/LoadingModal.vue +34 -0
- package/src/components/ui/Markdown.vue +97 -0
- package/src/components/ui/Modal.vue +131 -0
- package/src/components/ui/ModalContext.vue +31 -0
- package/src/components/ui/ProgressBar.vue +51 -0
- package/src/components/ui/PromptModal.vue +38 -0
- package/src/components/ui/Select.vue +27 -0
- package/src/components/ui/SelectLabel.vue +21 -0
- package/src/components/ui/SelectOption.vue +29 -0
- package/src/components/ui/SelectOptions.vue +35 -0
- package/src/components/ui/SelectTrigger.vue +29 -0
- package/src/components/ui/Setting.vue +31 -0
- package/src/components/ui/SettingsModal.vue +15 -0
- package/src/components/ui/StartupCrash.vue +76 -0
- package/src/components/ui/Switch.vue +11 -0
- package/src/components/ui/TextArea.vue +56 -0
- package/src/components/ui/Toast.vue +46 -0
- package/src/components/ui/index.ts +35 -0
- package/src/directives/index.ts +9 -5
- package/src/directives/measure.ts +33 -8
- package/src/errors/Errors.state.ts +2 -1
- package/src/errors/Errors.ts +56 -33
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +15 -8
- package/src/errors/settings/Debug.vue +14 -0
- package/src/errors/settings/index.ts +10 -0
- package/src/errors/utils.ts +17 -1
- package/src/forms/FormController.test.ts +113 -0
- package/src/forms/{Form.ts → FormController.ts} +73 -42
- package/src/forms/index.ts +3 -3
- package/src/forms/utils.ts +65 -24
- package/src/forms/validation.ts +50 -0
- package/src/index.css +76 -0
- package/src/jobs/Job.ts +144 -2
- package/src/jobs/index.ts +4 -1
- package/src/jobs/listeners.ts +3 -0
- package/src/jobs/status.ts +4 -0
- package/src/lang/DefaultLangProvider.ts +46 -0
- package/src/lang/Lang.state.ts +11 -0
- package/src/lang/Lang.ts +48 -21
- package/src/lang/index.ts +12 -6
- package/src/lang/settings/Language.vue +48 -0
- package/src/lang/settings/index.ts +10 -0
- package/src/plugins/Plugin.ts +1 -1
- package/src/plugins/index.ts +10 -7
- package/src/services/App.state.ts +36 -3
- package/src/services/App.ts +19 -3
- package/src/services/Cache.ts +1 -1
- package/src/services/Events.test.ts +8 -8
- package/src/services/Events.ts +16 -12
- package/src/services/Service.ts +135 -59
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +16 -7
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +8 -3
- package/src/testing/setup.ts +11 -0
- package/src/ui/UI.state.ts +14 -12
- package/src/ui/UI.ts +250 -123
- package/src/ui/index.ts +28 -28
- package/src/ui/utils.ts +16 -0
- package/src/utils/app.ts +7 -0
- package/src/utils/classes.ts +41 -0
- package/src/utils/composition/events.ts +4 -6
- package/src/utils/composition/forms.ts +20 -4
- package/src/utils/composition/persistent.test.ts +33 -0
- package/src/utils/composition/persistent.ts +11 -0
- package/src/utils/composition/state.test.ts +47 -0
- package/src/utils/composition/state.ts +33 -0
- package/src/utils/index.ts +6 -1
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +53 -6
- package/src/utils/types.ts +3 -0
- package/src/utils/vue.ts +38 -132
- package/dist/aerogel-core.cjs.js +0 -2
- package/dist/aerogel-core.cjs.js.map +0 -1
- package/dist/aerogel-core.esm.js +0 -2
- package/dist/aerogel-core.esm.js.map +0 -1
- package/histoire.config.ts +0 -7
- package/noeldemartin.config.js +0 -5
- package/postcss.config.js +0 -6
- package/src/assets/histoire.css +0 -3
- package/src/components/AGAppLayout.vue +0 -16
- package/src/components/AGAppOverlays.vue +0 -41
- package/src/components/AGAppSnackbars.vue +0 -13
- package/src/components/constants.ts +0 -8
- package/src/components/forms/AGButton.vue +0 -44
- package/src/components/forms/AGCheckbox.vue +0 -41
- package/src/components/forms/AGInput.vue +0 -40
- package/src/components/forms/AGSelect.story.vue +0 -46
- package/src/components/forms/AGSelect.vue +0 -60
- package/src/components/forms/index.ts +0 -5
- package/src/components/headless/forms/AGHeadlessButton.vue +0 -56
- package/src/components/headless/forms/AGHeadlessInput.ts +0 -32
- package/src/components/headless/forms/AGHeadlessInput.vue +0 -57
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -81
- package/src/components/headless/forms/AGHeadlessSelect.ts +0 -42
- package/src/components/headless/forms/AGHeadlessSelect.vue +0 -77
- package/src/components/headless/forms/AGHeadlessSelectButton.vue +0 -24
- package/src/components/headless/forms/AGHeadlessSelectLabel.vue +0 -24
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +0 -4
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +0 -39
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +0 -3
- package/src/components/headless/forms/composition.ts +0 -10
- package/src/components/headless/forms/index.ts +0 -17
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -34
- package/src/components/headless/modals/AGHeadlessModal.vue +0 -86
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -28
- package/src/components/headless/modals/AGHeadlessModalTitle.vue +0 -13
- package/src/components/headless/modals/index.ts +0 -4
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +0 -10
- package/src/components/headless/snackbars/index.ts +0 -40
- package/src/components/lib/AGErrorMessage.vue +0 -16
- package/src/components/lib/AGLink.vue +0 -9
- package/src/components/lib/AGMarkdown.vue +0 -41
- package/src/components/lib/AGMeasured.vue +0 -15
- package/src/components/lib/AGStartupCrash.vue +0 -31
- package/src/components/lib/index.ts +0 -5
- package/src/components/modals/AGAlertModal.ts +0 -15
- package/src/components/modals/AGAlertModal.vue +0 -14
- package/src/components/modals/AGConfirmModal.ts +0 -27
- package/src/components/modals/AGConfirmModal.vue +0 -26
- package/src/components/modals/AGErrorReportModal.ts +0 -46
- package/src/components/modals/AGErrorReportModal.vue +0 -54
- package/src/components/modals/AGErrorReportModalTitle.vue +0 -25
- package/src/components/modals/AGLoadingModal.ts +0 -23
- package/src/components/modals/AGLoadingModal.vue +0 -15
- package/src/components/modals/AGModal.ts +0 -10
- package/src/components/modals/AGModal.vue +0 -39
- package/src/components/modals/AGModalContext.ts +0 -8
- package/src/components/modals/AGModalContext.vue +0 -22
- package/src/components/modals/AGModalTitle.vue +0 -9
- package/src/components/modals/AGPromptModal.ts +0 -30
- package/src/components/modals/AGPromptModal.vue +0 -34
- package/src/components/modals/index.ts +0 -17
- package/src/components/snackbars/AGSnackbar.vue +0 -36
- package/src/components/snackbars/index.ts +0 -3
- package/src/components/utils.ts +0 -10
- package/src/directives/initial-focus.ts +0 -11
- package/src/forms/Form.test.ts +0 -58
- package/src/forms/composition.ts +0 -6
- package/src/main.histoire.ts +0 -1
- package/src/utils/tailwindcss.test.ts +0 -26
- package/src/utils/tailwindcss.ts +0 -7
- package/tailwind.config.js +0 -4
- package/tsconfig.json +0 -11
- package/vite.config.ts +0 -14
- /package/src/{main.ts → index.ts} +0 -0
package/src/errors/Errors.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { JSError, facade, isObject, objectWithoutEmpty, toString } from '@noeldemartin/utils';
|
|
1
|
+
import { JSError, facade, isDevelopment, isObject, isTesting, objectWithoutEmpty, toString } from '@noeldemartin/utils';
|
|
2
|
+
import { watchEffect } from 'vue';
|
|
3
|
+
import type Eruda from 'eruda';
|
|
2
4
|
|
|
3
|
-
import App from '
|
|
4
|
-
import ServiceBootError from '
|
|
5
|
-
import UI
|
|
6
|
-
import { translateWithDefault } from '
|
|
5
|
+
import App from '@aerogel/core/services/App';
|
|
6
|
+
import ServiceBootError from '@aerogel/core/errors/ServiceBootError';
|
|
7
|
+
import UI from '@aerogel/core/ui/UI';
|
|
8
|
+
import { translateWithDefault } from '@aerogel/core/lang/utils';
|
|
9
|
+
import { Events } from '@aerogel/core/services';
|
|
7
10
|
|
|
8
11
|
import Service from './Errors.state';
|
|
9
|
-
import { Colors } from '@/components/constants';
|
|
10
|
-
import { Events } from '@/services';
|
|
11
|
-
import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
|
|
12
12
|
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
13
|
-
import type { ModalComponent } from '@/ui/UI.state';
|
|
14
13
|
|
|
15
14
|
export class ErrorsService extends Service {
|
|
16
15
|
|
|
17
16
|
public forceReporting: boolean = false;
|
|
18
17
|
private enabled: boolean = true;
|
|
18
|
+
private eruda: typeof Eruda | null = null;
|
|
19
19
|
|
|
20
20
|
public enable(): void {
|
|
21
21
|
this.enabled = true;
|
|
@@ -25,28 +25,33 @@ export class ErrorsService extends Service {
|
|
|
25
25
|
this.enabled = false;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
public async inspect(error: ErrorSource | ErrorReport[]): Promise<void
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (
|
|
28
|
+
public async inspect(error: ErrorSource | ErrorReport, reports?: ErrorReport[]): Promise<void>;
|
|
29
|
+
public async inspect(reports: ErrorReport[]): Promise<void>;
|
|
30
|
+
public async inspect(errorOrReports: ErrorSource | ErrorReport[], _reports?: ErrorReport[]): Promise<void> {
|
|
31
|
+
if (Array.isArray(errorOrReports) && errorOrReports.length === 0) {
|
|
32
32
|
UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
|
|
33
33
|
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
const report = Array.isArray(errorOrReports)
|
|
38
|
+
? (errorOrReports[0] as ErrorReport)
|
|
39
|
+
: this.isErrorReport(errorOrReports)
|
|
40
|
+
? errorOrReports
|
|
41
|
+
: await this.createErrorReport(errorOrReports);
|
|
42
|
+
const reports = Array.isArray(errorOrReports) ? (errorOrReports as ErrorReport[]) : (_reports ?? [report]);
|
|
43
|
+
|
|
44
|
+
UI.modal(UI.requireComponent('error-report-modal'), { report, reports });
|
|
40
45
|
}
|
|
41
46
|
|
|
42
47
|
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
43
48
|
await Events.emit('error', { error, message });
|
|
44
49
|
|
|
45
|
-
if (
|
|
50
|
+
if (isTesting('unit')) {
|
|
46
51
|
throw error;
|
|
47
52
|
}
|
|
48
53
|
|
|
49
|
-
if (
|
|
54
|
+
if (isDevelopment()) {
|
|
50
55
|
this.logError(error);
|
|
51
56
|
}
|
|
52
57
|
|
|
@@ -71,20 +76,16 @@ export class ErrorsService extends Service {
|
|
|
71
76
|
date: new Date(),
|
|
72
77
|
};
|
|
73
78
|
|
|
74
|
-
UI.
|
|
79
|
+
UI.toast(
|
|
75
80
|
message ??
|
|
76
81
|
translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
|
|
77
82
|
{
|
|
78
|
-
|
|
83
|
+
variant: 'danger',
|
|
79
84
|
actions: [
|
|
80
85
|
{
|
|
81
|
-
|
|
86
|
+
label: translateWithDefault('errors.viewDetails', 'View details'),
|
|
82
87
|
dismiss: true,
|
|
83
|
-
|
|
84
|
-
UI.openModal<ModalComponent<AGErrorReportModalProps>>(
|
|
85
|
-
UI.requireComponent(UIComponents.ErrorReportModal),
|
|
86
|
-
{ reports: [report] },
|
|
87
|
-
),
|
|
88
|
+
click: () => UI.modal(UI.requireComponent('error-report-modal'), { report, reports: [report] }),
|
|
88
89
|
},
|
|
89
90
|
],
|
|
90
91
|
},
|
|
@@ -93,6 +94,19 @@ export class ErrorsService extends Service {
|
|
|
93
94
|
this.setState({ logs: [log].concat(this.logs) });
|
|
94
95
|
}
|
|
95
96
|
|
|
97
|
+
public reportDevelopmentError(error: ErrorSource, message?: string): void {
|
|
98
|
+
if (!isDevelopment()) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (message) {
|
|
103
|
+
// eslint-disable-next-line no-console
|
|
104
|
+
console.warn(message);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.logError(error);
|
|
108
|
+
}
|
|
109
|
+
|
|
96
110
|
public see(report: ErrorReport): void {
|
|
97
111
|
this.setState({
|
|
98
112
|
logs: this.logs.map((log) => {
|
|
@@ -108,12 +122,17 @@ export class ErrorsService extends Service {
|
|
|
108
122
|
});
|
|
109
123
|
}
|
|
110
124
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
125
|
+
protected override async boot(): Promise<void> {
|
|
126
|
+
watchEffect(async () => {
|
|
127
|
+
if (!this.debug) {
|
|
128
|
+
this.eruda?.destroy();
|
|
129
|
+
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.eruda ??= (await import('eruda')).default;
|
|
134
|
+
|
|
135
|
+
this.eruda.init();
|
|
117
136
|
});
|
|
118
137
|
}
|
|
119
138
|
|
|
@@ -126,6 +145,10 @@ export class ErrorsService extends Service {
|
|
|
126
145
|
}
|
|
127
146
|
}
|
|
128
147
|
|
|
148
|
+
private isErrorReport(error: unknown): error is ErrorReport {
|
|
149
|
+
return isObject(error) && 'title' in error;
|
|
150
|
+
}
|
|
151
|
+
|
|
129
152
|
private async createErrorReport(error: ErrorSource): Promise<ErrorReport> {
|
|
130
153
|
if (typeof error === 'string') {
|
|
131
154
|
return { title: error };
|
|
@@ -178,7 +201,7 @@ export class ErrorsService extends Service {
|
|
|
178
201
|
|
|
179
202
|
export default facade(ErrorsService);
|
|
180
203
|
|
|
181
|
-
declare module '
|
|
204
|
+
declare module '@aerogel/core/services/Events' {
|
|
182
205
|
export interface EventsPayload {
|
|
183
206
|
error: { error: ErrorSource; message?: string };
|
|
184
207
|
}
|
package/src/errors/index.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import type { App } from 'vue';
|
|
1
|
+
import type { App as AppInstance } from 'vue';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
3
|
+
import App from '@aerogel/core/services/App';
|
|
4
|
+
import { bootServices } from '@aerogel/core/services';
|
|
5
|
+
import { definePlugin } from '@aerogel/core/plugins';
|
|
5
6
|
|
|
6
7
|
import Errors from './Errors';
|
|
7
|
-
import
|
|
8
|
+
import settings from './settings';
|
|
9
|
+
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
8
10
|
|
|
9
11
|
export * from './utils';
|
|
10
|
-
export { Errors
|
|
12
|
+
export { Errors };
|
|
13
|
+
export { default as JobCancelledError } from './JobCancelledError';
|
|
14
|
+
export { default as ServiceBootError } from './ServiceBootError';
|
|
15
|
+
export type { ErrorSource, ErrorReport, ErrorReportLog };
|
|
11
16
|
|
|
12
17
|
const services = { $errors: Errors };
|
|
13
18
|
const frameworkHandler: ErrorHandler = (error) => {
|
|
@@ -16,7 +21,7 @@ const frameworkHandler: ErrorHandler = (error) => {
|
|
|
16
21
|
return true;
|
|
17
22
|
};
|
|
18
23
|
|
|
19
|
-
function setUpErrorHandler(app:
|
|
24
|
+
function setUpErrorHandler(app: AppInstance, baseHandler: ErrorHandler = () => false): void {
|
|
20
25
|
const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
|
|
21
26
|
|
|
22
27
|
app.config.errorHandler = errorHandler;
|
|
@@ -31,16 +36,18 @@ export default definePlugin({
|
|
|
31
36
|
async install(app, options) {
|
|
32
37
|
setUpErrorHandler(app, options.handleError);
|
|
33
38
|
|
|
39
|
+
settings.forEach((setting) => App.addSetting(setting));
|
|
40
|
+
|
|
34
41
|
await bootServices(app, services);
|
|
35
42
|
},
|
|
36
43
|
});
|
|
37
44
|
|
|
38
|
-
declare module '
|
|
45
|
+
declare module '@aerogel/core/bootstrap/options' {
|
|
39
46
|
export interface AerogelOptions {
|
|
40
47
|
handleError?(error: ErrorSource): boolean;
|
|
41
48
|
}
|
|
42
49
|
}
|
|
43
50
|
|
|
44
|
-
declare module '
|
|
51
|
+
declare module '@aerogel/core/services' {
|
|
45
52
|
export interface Services extends ErrorsServices {}
|
|
46
53
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<Setting
|
|
3
|
+
title-id="debug-setting"
|
|
4
|
+
:title="$td('settings.debug', 'Debugging')"
|
|
5
|
+
:description="$td('settings.debugDescription', 'Enable debugging with [Eruda](https://eruda.liriliri.io/).')"
|
|
6
|
+
>
|
|
7
|
+
<Switch v-model="$errors.debug" aria-labelledby="debug-setting" />
|
|
8
|
+
</Setting>
|
|
9
|
+
</template>
|
|
10
|
+
|
|
11
|
+
<script setup lang="ts">
|
|
12
|
+
import Setting from '@aerogel/core/components/ui/Setting.vue';
|
|
13
|
+
import Switch from '@aerogel/core/components/ui/Switch.vue';
|
|
14
|
+
</script>
|
package/src/errors/utils.ts
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import { JSError, isObject, toString } from '@noeldemartin/utils';
|
|
2
|
-
import { translateWithDefault } from '
|
|
2
|
+
import { translateWithDefault } from '@aerogel/core/lang/utils';
|
|
3
3
|
import type { ErrorSource } from './Errors.state';
|
|
4
4
|
|
|
5
|
+
const handlers: ErrorHandler[] = [];
|
|
6
|
+
|
|
7
|
+
export type ErrorHandler = (error: ErrorSource) => string | undefined;
|
|
8
|
+
|
|
9
|
+
export function registerErrorHandler(handler: ErrorHandler): void {
|
|
10
|
+
handlers.push(handler);
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
export function getErrorMessage(error: ErrorSource): string {
|
|
14
|
+
for (const handler of handlers) {
|
|
15
|
+
const result = handler(error);
|
|
16
|
+
|
|
17
|
+
if (result) {
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
6
22
|
if (typeof error === 'string') {
|
|
7
23
|
return error;
|
|
8
24
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, expect, expectTypeOf, it } from 'vitest';
|
|
2
|
+
import { tt } from '@noeldemartin/testing';
|
|
3
|
+
import type { Equals } from '@noeldemartin/utils';
|
|
4
|
+
import type { Expect } from '@noeldemartin/testing';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
enumInput,
|
|
8
|
+
numberInput,
|
|
9
|
+
objectInput,
|
|
10
|
+
requiredObjectInput,
|
|
11
|
+
requiredStringInput,
|
|
12
|
+
stringInput,
|
|
13
|
+
} from '@aerogel/core/forms/utils';
|
|
14
|
+
import { useForm } from '@aerogel/core/utils/composition/forms';
|
|
15
|
+
|
|
16
|
+
describe('FormController', () => {
|
|
17
|
+
|
|
18
|
+
it('defines magic fields', () => {
|
|
19
|
+
const form = useForm({
|
|
20
|
+
name: requiredStringInput(),
|
|
21
|
+
age: numberInput(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
expectTypeOf(form.name).toEqualTypeOf<string>();
|
|
25
|
+
expectTypeOf(form.age).toEqualTypeOf<number | null>();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('validates required fields', () => {
|
|
29
|
+
// Arrange
|
|
30
|
+
const form = useForm({
|
|
31
|
+
name: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
rules: 'required',
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Act
|
|
38
|
+
form.submit();
|
|
39
|
+
|
|
40
|
+
// Assert
|
|
41
|
+
expect(form.valid).toBe(false);
|
|
42
|
+
expect(form.submitted).toBe(true);
|
|
43
|
+
expect(form.errors.name).toEqual(['required']);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('resets form', () => {
|
|
47
|
+
// Arrange
|
|
48
|
+
const form = useForm({
|
|
49
|
+
name: {
|
|
50
|
+
type: 'string',
|
|
51
|
+
rules: 'required',
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
form.name = 'Foo bar';
|
|
56
|
+
form.submit();
|
|
57
|
+
|
|
58
|
+
// Act
|
|
59
|
+
form.reset();
|
|
60
|
+
|
|
61
|
+
// Assert
|
|
62
|
+
expect(form.valid).toBe(true);
|
|
63
|
+
expect(form.submitted).toBe(false);
|
|
64
|
+
expect(form.name).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('trims values', () => {
|
|
68
|
+
// Arrange
|
|
69
|
+
const form = useForm({
|
|
70
|
+
trimmed: {
|
|
71
|
+
type: 'string',
|
|
72
|
+
rules: 'required',
|
|
73
|
+
},
|
|
74
|
+
untrimmed: {
|
|
75
|
+
type: 'string',
|
|
76
|
+
rules: 'required',
|
|
77
|
+
trim: false,
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Act
|
|
82
|
+
form.trimmed = ' ';
|
|
83
|
+
form.untrimmed = ' ';
|
|
84
|
+
|
|
85
|
+
form.submit();
|
|
86
|
+
|
|
87
|
+
// Assert
|
|
88
|
+
expect(form.valid).toBe(false);
|
|
89
|
+
expect(form.submitted).toBe(true);
|
|
90
|
+
expect(form.trimmed).toEqual('');
|
|
91
|
+
expect(form.untrimmed).toEqual(' ');
|
|
92
|
+
expect(form.errors).toEqual({ trimmed: ['required'], untrimmed: null });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('infers field types', () => {
|
|
96
|
+
const form = useForm({
|
|
97
|
+
one: stringInput(),
|
|
98
|
+
two: requiredStringInput(),
|
|
99
|
+
three: objectInput(),
|
|
100
|
+
four: requiredObjectInput<{ foo: string; bar?: number }>(),
|
|
101
|
+
five: enumInput(['foo', 'bar']),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
tt<
|
|
105
|
+
| Expect<Equals<typeof form.one, string | null>>
|
|
106
|
+
| Expect<Equals<typeof form.two, string>>
|
|
107
|
+
| Expect<Equals<typeof form.three, object | null>>
|
|
108
|
+
| Expect<Equals<typeof form.four, { foo: string; bar?: number }>>
|
|
109
|
+
| Expect<Equals<typeof form.five, 'foo' | 'bar' | null>>
|
|
110
|
+
>();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
});
|
|
@@ -1,31 +1,33 @@
|
|
|
1
|
-
import { MagicObject, arrayRemove } from '@noeldemartin/utils';
|
|
2
1
|
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
3
|
-
import
|
|
2
|
+
import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
|
|
4
3
|
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
String: 'string',
|
|
8
|
-
Number: 'number',
|
|
9
|
-
Boolean: 'boolean',
|
|
10
|
-
Object: 'object',
|
|
11
|
-
Date: 'date',
|
|
12
|
-
} as const;
|
|
5
|
+
import { validate, validateType } from './validation';
|
|
13
6
|
|
|
14
|
-
export
|
|
7
|
+
export const __valueType: unique symbol = Symbol();
|
|
8
|
+
|
|
9
|
+
export interface FormFieldDefinition<
|
|
10
|
+
TType extends FormFieldType = FormFieldType,
|
|
11
|
+
TRules extends string = string,
|
|
12
|
+
TValueType = unknown,
|
|
13
|
+
> {
|
|
15
14
|
type: TType;
|
|
15
|
+
trim?: boolean;
|
|
16
16
|
default?: GetFormFieldValue<TType>;
|
|
17
17
|
rules?: TRules;
|
|
18
|
+
values?: readonly TValueType[];
|
|
19
|
+
[__valueType]?: TValueType;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
export type
|
|
21
|
-
export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
|
|
22
|
+
export type FormFieldType = 'string' | 'enum' | 'number' | 'boolean' | 'object' | 'date';
|
|
22
23
|
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
24
|
+
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
23
25
|
|
|
24
26
|
export type FormData<T> = {
|
|
25
|
-
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
|
|
27
|
+
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules, infer TValueType>
|
|
26
28
|
? TRules extends 'required'
|
|
27
|
-
? GetFormFieldValue<TType>
|
|
28
|
-
: GetFormFieldValue<TType> | null
|
|
29
|
+
? GetFormFieldValue<TType, TValueType>
|
|
30
|
+
: GetFormFieldValue<TType, TValueType> | null
|
|
29
31
|
: never;
|
|
30
32
|
};
|
|
31
33
|
|
|
@@ -33,24 +35,28 @@ export type FormErrors<T> = {
|
|
|
33
35
|
[k in keyof T]: string[] | null;
|
|
34
36
|
};
|
|
35
37
|
|
|
36
|
-
export type GetFormFieldValue<TType> = TType extends
|
|
38
|
+
export type GetFormFieldValue<TType, TValueType = unknown> = TType extends 'string'
|
|
37
39
|
? string
|
|
38
|
-
: TType extends
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
: TType extends 'number'
|
|
41
|
+
? number
|
|
42
|
+
: TType extends 'boolean'
|
|
43
|
+
? boolean
|
|
44
|
+
: TType extends 'enum'
|
|
45
|
+
? TValueType
|
|
46
|
+
: TType extends 'object'
|
|
47
|
+
? TValueType extends object
|
|
48
|
+
? TValueType
|
|
49
|
+
: object
|
|
50
|
+
: TType extends 'date'
|
|
51
|
+
? Date
|
|
52
|
+
: never;
|
|
53
|
+
|
|
54
|
+
const validForms: WeakMap<FormController, ComputedRef<boolean>> = new WeakMap();
|
|
49
55
|
|
|
50
56
|
export type SubmitFormListener = () => unknown;
|
|
51
57
|
export type FocusFormListener = (input: string) => unknown;
|
|
52
58
|
|
|
53
|
-
export default class
|
|
59
|
+
export default class FormController<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
54
60
|
|
|
55
61
|
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
56
62
|
|
|
@@ -85,7 +91,13 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
85
91
|
}
|
|
86
92
|
|
|
87
93
|
public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
|
|
88
|
-
|
|
94
|
+
const definition =
|
|
95
|
+
this._fields[field] ?? fail<FormFieldDefinition>(`Trying to set undefined '${toString(field)}' field`);
|
|
96
|
+
|
|
97
|
+
this._data[field] =
|
|
98
|
+
definition.type === 'string' && (definition.trim ?? true)
|
|
99
|
+
? (toString(value).trim() as FormData<Fields>[T])
|
|
100
|
+
: value;
|
|
89
101
|
|
|
90
102
|
if (this._submitted.value) {
|
|
91
103
|
this.validate();
|
|
@@ -96,16 +108,27 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
96
108
|
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
97
109
|
}
|
|
98
110
|
|
|
111
|
+
public getFieldRules<T extends keyof Fields>(field: T): string[] {
|
|
112
|
+
return this._fields[field]?.rules?.split('|') ?? [];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public getFieldType<T extends keyof Fields>(field: T): FormFieldType | null {
|
|
116
|
+
return this._fields[field]?.type ?? null;
|
|
117
|
+
}
|
|
118
|
+
|
|
99
119
|
public data(): FormData<Fields> {
|
|
100
120
|
return { ...this._data };
|
|
101
121
|
}
|
|
102
122
|
|
|
103
123
|
public validate(): boolean {
|
|
104
|
-
const errors = Object.entries(this._fields).reduce(
|
|
105
|
-
formErrors[name
|
|
124
|
+
const errors = Object.entries(this._fields).reduce(
|
|
125
|
+
(formErrors, [name, definition]) => {
|
|
126
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
106
127
|
|
|
107
|
-
|
|
108
|
-
|
|
128
|
+
return formErrors;
|
|
129
|
+
},
|
|
130
|
+
{} as Record<string, string[] | null>,
|
|
131
|
+
);
|
|
109
132
|
|
|
110
133
|
this.resetErrors(errors);
|
|
111
134
|
|
|
@@ -153,29 +176,37 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
153
176
|
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
154
177
|
}
|
|
155
178
|
|
|
156
|
-
protected __get(property: string): unknown {
|
|
179
|
+
protected override __get(property: string): unknown {
|
|
157
180
|
if (!(property in this._fields)) {
|
|
158
181
|
return super.__get(property);
|
|
159
182
|
}
|
|
160
183
|
|
|
161
|
-
return this.
|
|
184
|
+
return this.getFieldValue(property);
|
|
162
185
|
}
|
|
163
186
|
|
|
164
|
-
protected __set(property: string, value: unknown): void {
|
|
187
|
+
protected override __set(property: string, value: unknown): void {
|
|
165
188
|
if (!(property in this._fields)) {
|
|
166
189
|
super.__set(property, value);
|
|
167
190
|
|
|
168
191
|
return;
|
|
169
192
|
}
|
|
170
193
|
|
|
171
|
-
|
|
194
|
+
this.setFieldValue(property, value as FormData<Fields>[string]);
|
|
172
195
|
}
|
|
173
196
|
|
|
174
197
|
private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
|
|
175
198
|
const errors = [];
|
|
199
|
+
const value = this._data[name];
|
|
200
|
+
const rules = definition.rules?.split('|') ?? [];
|
|
201
|
+
|
|
202
|
+
errors.push(...validateType(value, definition));
|
|
203
|
+
|
|
204
|
+
for (const rule of rules) {
|
|
205
|
+
if (rule !== 'required' && (value === null || value === undefined)) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
176
208
|
|
|
177
|
-
|
|
178
|
-
errors.push('required');
|
|
209
|
+
errors.push(...validate(value, rule));
|
|
179
210
|
}
|
|
180
211
|
|
|
181
212
|
return errors.length > 0 ? errors : null;
|
|
@@ -186,10 +217,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
186
217
|
return {} as FormData<Fields>;
|
|
187
218
|
}
|
|
188
219
|
|
|
189
|
-
const data = Object.entries(fields).reduce((
|
|
190
|
-
|
|
220
|
+
const data = Object.entries(fields).reduce((initialData, [name, definition]) => {
|
|
221
|
+
initialData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
191
222
|
|
|
192
|
-
return
|
|
223
|
+
return initialData;
|
|
193
224
|
}, {} as FormData<Fields>);
|
|
194
225
|
|
|
195
226
|
return reactive(data) as FormData<Fields>;
|
package/src/forms/index.ts
CHANGED