@aerogel/core 0.0.0-next.f1f5a990033d966dc0bb12d251110fbc9350dcc7 → 0.0.0-next.f7d6218c6788b7d93b1f58d848fc11cd0a351798
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 +2344 -1394
- package/dist/aerogel-core.js +3712 -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 +27 -14
- package/src/bootstrap/options.ts +4 -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/HeadlessInputDescription.vue +27 -0
- 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/HeadlessInputTextArea.vue +40 -0
- 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/ui/Form.vue +24 -0
- 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/{lib/AGStartupCrash.vue → ui/StartupCrash.vue} +8 -8
- 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 +11 -5
- package/src/directives/measure.ts +41 -7
- package/src/errors/Errors.state.ts +1 -1
- package/src/errors/Errors.ts +41 -42
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +16 -18
- package/src/errors/settings/Debug.vue +32 -0
- package/src/errors/settings/index.ts +10 -0
- package/src/errors/utils.ts +35 -0
- package/src/forms/FormController.test.ts +113 -0
- package/src/forms/FormController.ts +255 -0
- package/src/forms/index.ts +3 -2
- package/src/forms/utils.ts +76 -20
- package/src/forms/validation.ts +50 -0
- package/src/index.css +76 -0
- package/src/{main.ts → index.ts} +3 -0
- package/src/jobs/Job.ts +147 -0
- package/src/jobs/index.ts +10 -0
- 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 +44 -29
- 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 +37 -5
- package/src/services/App.ts +40 -6
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +110 -36
- package/src/services/Service.ts +150 -49
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +19 -7
- package/src/services/store.ts +8 -5
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +30 -0
- package/src/testing/setup.ts +11 -0
- package/src/ui/UI.state.ts +14 -12
- package/src/ui/UI.ts +282 -110
- package/src/ui/index.ts +28 -26
- package/src/ui/utils.ts +16 -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 +5 -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 -35
- package/src/components/forms/AGForm.vue +0 -26
- package/src/components/forms/AGInput.vue +0 -36
- package/src/components/forms/AGSelect.story.vue +0 -28
- package/src/components/forms/AGSelect.vue +0 -53
- package/src/components/forms/index.ts +0 -5
- package/src/components/headless/forms/AGHeadlessButton.vue +0 -51
- package/src/components/headless/forms/AGHeadlessInput.ts +0 -28
- package/src/components/headless/forms/AGHeadlessInput.vue +0 -57
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -45
- package/src/components/headless/forms/AGHeadlessSelect.ts +0 -39
- package/src/components/headless/forms/AGHeadlessSelect.vue +0 -76
- 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/index.ts +0 -14
- 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 -36
- package/src/components/lib/AGMeasured.vue +0 -15
- 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/index.ts +0 -26
- 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/Form.ts +0 -184
- 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/errors/Errors.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import { JSError, facade, isObject, objectWithoutEmpty, toString } from '@noeldemartin/utils';
|
|
1
|
+
import { JSError, facade, isDevelopment, isObject, isTesting, objectWithoutEmpty, toString } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
-
import App from '
|
|
4
|
-
import ServiceBootError from '
|
|
5
|
-
import UI
|
|
6
|
-
import { translateWithDefault } from '
|
|
3
|
+
import App from '@aerogel/core/services/App';
|
|
4
|
+
import ServiceBootError from '@aerogel/core/errors/ServiceBootError';
|
|
5
|
+
import UI from '@aerogel/core/ui/UI';
|
|
6
|
+
import { translateWithDefault } from '@aerogel/core/lang/utils';
|
|
7
|
+
import { Events } from '@aerogel/core/services';
|
|
7
8
|
|
|
8
9
|
import Service from './Errors.state';
|
|
9
|
-
import { Colors } from '@/components/constants';
|
|
10
|
-
import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
|
|
11
10
|
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
12
|
-
import type { ModalComponent } from '@/ui/UI.state';
|
|
13
11
|
|
|
14
12
|
export class ErrorsService extends Service {
|
|
15
13
|
|
|
@@ -24,22 +22,33 @@ export class ErrorsService extends Service {
|
|
|
24
22
|
this.enabled = false;
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
public async inspect(error: ErrorSource | ErrorReport[]): Promise<void
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
25
|
+
public async inspect(error: ErrorSource | ErrorReport, reports?: ErrorReport[]): Promise<void>;
|
|
26
|
+
public async inspect(reports: ErrorReport[]): Promise<void>;
|
|
27
|
+
public async inspect(errorOrReports: ErrorSource | ErrorReport[], _reports?: ErrorReport[]): Promise<void> {
|
|
28
|
+
if (Array.isArray(errorOrReports) && errorOrReports.length === 0) {
|
|
31
29
|
UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
|
|
32
30
|
|
|
33
31
|
return;
|
|
34
32
|
}
|
|
35
33
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
const report = Array.isArray(errorOrReports)
|
|
35
|
+
? (errorOrReports[0] as ErrorReport)
|
|
36
|
+
: this.isErrorReport(errorOrReports)
|
|
37
|
+
? errorOrReports
|
|
38
|
+
: await this.createErrorReport(errorOrReports);
|
|
39
|
+
const reports = Array.isArray(errorOrReports) ? (errorOrReports as ErrorReport[]) : (_reports ?? [report]);
|
|
40
|
+
|
|
41
|
+
UI.modal(UI.requireComponent('error-report-modal'), { report, reports });
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
42
|
-
|
|
45
|
+
await Events.emit('error', { error, message });
|
|
46
|
+
|
|
47
|
+
if (isTesting('unit')) {
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (isDevelopment()) {
|
|
43
52
|
this.logError(error);
|
|
44
53
|
}
|
|
45
54
|
|
|
@@ -47,7 +56,7 @@ export class ErrorsService extends Service {
|
|
|
47
56
|
throw error;
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
if (!App.isMounted) {
|
|
59
|
+
if (!App.isMounted()) {
|
|
51
60
|
const startupError = await this.createStartupErrorReport(error);
|
|
52
61
|
|
|
53
62
|
if (startupError) {
|
|
@@ -64,20 +73,16 @@ export class ErrorsService extends Service {
|
|
|
64
73
|
date: new Date(),
|
|
65
74
|
};
|
|
66
75
|
|
|
67
|
-
UI.
|
|
76
|
+
UI.toast(
|
|
68
77
|
message ??
|
|
69
78
|
translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
|
|
70
79
|
{
|
|
71
|
-
|
|
80
|
+
variant: 'danger',
|
|
72
81
|
actions: [
|
|
73
82
|
{
|
|
74
|
-
|
|
83
|
+
label: translateWithDefault('errors.viewDetails', 'View details'),
|
|
75
84
|
dismiss: true,
|
|
76
|
-
|
|
77
|
-
UI.openModal<ModalComponent<AGErrorReportModalProps>>(
|
|
78
|
-
UI.requireComponent(UIComponents.ErrorReportModal),
|
|
79
|
-
{ reports: [report] },
|
|
80
|
-
),
|
|
85
|
+
click: () => UI.modal(UI.requireComponent('error-report-modal'), { report, reports: [report] }),
|
|
81
86
|
},
|
|
82
87
|
],
|
|
83
88
|
},
|
|
@@ -110,22 +115,6 @@ export class ErrorsService extends Service {
|
|
|
110
115
|
});
|
|
111
116
|
}
|
|
112
117
|
|
|
113
|
-
public getErrorMessage(error: ErrorSource): string {
|
|
114
|
-
if (typeof error === 'string') {
|
|
115
|
-
return error;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (error instanceof Error || error instanceof JSError) {
|
|
119
|
-
return error.message;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (isObject(error)) {
|
|
123
|
-
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
127
|
-
}
|
|
128
|
-
|
|
129
118
|
private logError(error: unknown): void {
|
|
130
119
|
// eslint-disable-next-line no-console
|
|
131
120
|
console.error(error);
|
|
@@ -135,6 +124,10 @@ export class ErrorsService extends Service {
|
|
|
135
124
|
}
|
|
136
125
|
}
|
|
137
126
|
|
|
127
|
+
private isErrorReport(error: unknown): error is ErrorReport {
|
|
128
|
+
return isObject(error) && 'title' in error;
|
|
129
|
+
}
|
|
130
|
+
|
|
138
131
|
private async createErrorReport(error: ErrorSource): Promise<ErrorReport> {
|
|
139
132
|
if (typeof error === 'string') {
|
|
140
133
|
return { title: error };
|
|
@@ -185,4 +178,10 @@ export class ErrorsService extends Service {
|
|
|
185
178
|
|
|
186
179
|
}
|
|
187
180
|
|
|
188
|
-
export default facade(
|
|
181
|
+
export default facade(ErrorsService);
|
|
182
|
+
|
|
183
|
+
declare module '@aerogel/core/services/Events' {
|
|
184
|
+
export interface EventsPayload {
|
|
185
|
+
error: { error: ErrorSource; message?: string };
|
|
186
|
+
}
|
|
187
|
+
}
|
package/src/errors/index.ts
CHANGED
|
@@ -1,31 +1,27 @@
|
|
|
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
|
-
export
|
|
11
|
+
export * from './utils';
|
|
12
|
+
export { Errors };
|
|
13
|
+
export { default as JobCancelledError } from './JobCancelledError';
|
|
14
|
+
export { default as ServiceBootError } from './ServiceBootError';
|
|
15
|
+
export type { ErrorSource, ErrorReport, ErrorReportLog };
|
|
10
16
|
|
|
11
17
|
const services = { $errors: Errors };
|
|
12
18
|
const frameworkHandler: ErrorHandler = (error) => {
|
|
13
|
-
if (!Errors.instance) {
|
|
14
|
-
// eslint-disable-next-line no-console
|
|
15
|
-
console.warn('Errors service hasn\'t been initialized properly!');
|
|
16
|
-
|
|
17
|
-
// eslint-disable-next-line no-console
|
|
18
|
-
console.error(error);
|
|
19
|
-
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
19
|
Errors.report(error);
|
|
24
20
|
|
|
25
21
|
return true;
|
|
26
22
|
};
|
|
27
23
|
|
|
28
|
-
function setUpErrorHandler(app:
|
|
24
|
+
function setUpErrorHandler(app: AppInstance, baseHandler: ErrorHandler = () => false): void {
|
|
29
25
|
const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
|
|
30
26
|
|
|
31
27
|
app.config.errorHandler = errorHandler;
|
|
@@ -40,16 +36,18 @@ export default definePlugin({
|
|
|
40
36
|
async install(app, options) {
|
|
41
37
|
setUpErrorHandler(app, options.handleError);
|
|
42
38
|
|
|
39
|
+
settings.forEach((setting) => App.addSetting(setting));
|
|
40
|
+
|
|
43
41
|
await bootServices(app, services);
|
|
44
42
|
},
|
|
45
43
|
});
|
|
46
44
|
|
|
47
|
-
declare module '
|
|
45
|
+
declare module '@aerogel/core/bootstrap/options' {
|
|
48
46
|
export interface AerogelOptions {
|
|
49
47
|
handleError?(error: ErrorSource): boolean;
|
|
50
48
|
}
|
|
51
49
|
}
|
|
52
50
|
|
|
53
|
-
declare module '
|
|
51
|
+
declare module '@aerogel/core/services' {
|
|
54
52
|
export interface Services extends ErrorsServices {}
|
|
55
53
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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="enabled" aria-labelledby="debug-setting" />
|
|
8
|
+
</Setting>
|
|
9
|
+
</template>
|
|
10
|
+
|
|
11
|
+
<script setup lang="ts">
|
|
12
|
+
import { ref, watchEffect } from 'vue';
|
|
13
|
+
import type Eruda from 'eruda';
|
|
14
|
+
|
|
15
|
+
import Setting from '@aerogel/core/components/ui/Setting.vue';
|
|
16
|
+
import Switch from '@aerogel/core/components/ui/Switch.vue';
|
|
17
|
+
|
|
18
|
+
let eruda: typeof Eruda | null = null;
|
|
19
|
+
const enabled = ref(false);
|
|
20
|
+
|
|
21
|
+
watchEffect(async () => {
|
|
22
|
+
if (!enabled.value) {
|
|
23
|
+
eruda?.destroy();
|
|
24
|
+
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
eruda ??= (await import('eruda')).default;
|
|
29
|
+
|
|
30
|
+
eruda.init();
|
|
31
|
+
});
|
|
32
|
+
</script>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { JSError, isObject, toString } from '@noeldemartin/utils';
|
|
2
|
+
import { translateWithDefault } from '@aerogel/core/lang/utils';
|
|
3
|
+
import type { ErrorSource } from './Errors.state';
|
|
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
|
+
|
|
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
|
+
|
|
22
|
+
if (typeof error === 'string') {
|
|
23
|
+
return error;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (error instanceof Error || error instanceof JSError) {
|
|
27
|
+
return error.message;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (isObject(error)) {
|
|
31
|
+
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
35
|
+
}
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
2
|
+
import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
|
|
3
|
+
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
4
|
+
|
|
5
|
+
import { validate, validateType } from './validation';
|
|
6
|
+
|
|
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
|
+
> {
|
|
14
|
+
type: TType;
|
|
15
|
+
trim?: boolean;
|
|
16
|
+
default?: GetFormFieldValue<TType>;
|
|
17
|
+
rules?: TRules;
|
|
18
|
+
values?: readonly TValueType[];
|
|
19
|
+
[__valueType]?: TValueType;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type FormFieldType = 'string' | 'enum' | 'number' | 'boolean' | 'object' | 'date';
|
|
23
|
+
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
24
|
+
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
25
|
+
|
|
26
|
+
export type FormData<T> = {
|
|
27
|
+
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules, infer TValueType>
|
|
28
|
+
? TRules extends 'required'
|
|
29
|
+
? GetFormFieldValue<TType, TValueType>
|
|
30
|
+
: GetFormFieldValue<TType, TValueType> | null
|
|
31
|
+
: never;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type FormErrors<T> = {
|
|
35
|
+
[k in keyof T]: string[] | null;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type GetFormFieldValue<TType, TValueType = unknown> = TType extends 'string'
|
|
39
|
+
? string
|
|
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();
|
|
55
|
+
|
|
56
|
+
export type SubmitFormListener = () => unknown;
|
|
57
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
58
|
+
|
|
59
|
+
export default class FormController<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
60
|
+
|
|
61
|
+
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
62
|
+
|
|
63
|
+
private _fields: Fields;
|
|
64
|
+
private _data: FormData<Fields>;
|
|
65
|
+
private _submitted: Ref<boolean>;
|
|
66
|
+
private _errors: FormErrors<Fields>;
|
|
67
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
68
|
+
|
|
69
|
+
constructor(fields: Fields) {
|
|
70
|
+
super();
|
|
71
|
+
|
|
72
|
+
this._fields = fields;
|
|
73
|
+
this._submitted = ref(false);
|
|
74
|
+
this._data = this.getInitialData(fields);
|
|
75
|
+
this._errors = this.getInitialErrors(fields);
|
|
76
|
+
|
|
77
|
+
validForms.set(
|
|
78
|
+
this,
|
|
79
|
+
computed(() => !Object.values(this._errors).some((error) => error !== null)),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
this.errors = readonly(this._errors);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public get valid(): boolean {
|
|
86
|
+
return !!validForms.get(this)?.value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public get submitted(): boolean {
|
|
90
|
+
return this._submitted.value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
|
|
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;
|
|
101
|
+
|
|
102
|
+
if (this._submitted.value) {
|
|
103
|
+
this.validate();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getFieldValue<T extends keyof Fields>(field: T): GetFormFieldValue<Fields[T]['type']> {
|
|
108
|
+
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
109
|
+
}
|
|
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
|
+
|
|
119
|
+
public data(): FormData<Fields> {
|
|
120
|
+
return { ...this._data };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public validate(): boolean {
|
|
124
|
+
const errors = Object.entries(this._fields).reduce(
|
|
125
|
+
(formErrors, [name, definition]) => {
|
|
126
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
127
|
+
|
|
128
|
+
return formErrors;
|
|
129
|
+
},
|
|
130
|
+
{} as Record<string, string[] | null>,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
this.resetErrors(errors);
|
|
134
|
+
|
|
135
|
+
return this.valid;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public reset(options: { keepData?: boolean; keepErrors?: boolean } = {}): void {
|
|
139
|
+
this._submitted.value = false;
|
|
140
|
+
|
|
141
|
+
options.keepData || this.resetData();
|
|
142
|
+
options.keepErrors || this.resetErrors();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public submit(): boolean {
|
|
146
|
+
this._submitted.value = true;
|
|
147
|
+
|
|
148
|
+
const valid = this.validate();
|
|
149
|
+
|
|
150
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
151
|
+
|
|
152
|
+
return valid;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public on(event: 'focus', listener: FocusFormListener): () => void;
|
|
156
|
+
public on(event: 'submit', listener: SubmitFormListener): () => void;
|
|
157
|
+
public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
|
|
158
|
+
this._listeners[event] ??= [];
|
|
159
|
+
|
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
+
this._listeners[event]?.push(listener as any);
|
|
162
|
+
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
164
|
+
return () => this.off(event as any, listener);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public off(event: 'focus', listener: FocusFormListener): void;
|
|
168
|
+
public off(event: 'submit', listener: SubmitFormListener): void;
|
|
169
|
+
public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
|
|
170
|
+
arrayRemove(this._listeners[event] ?? [], listener);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
public async focus(input: string): Promise<void> {
|
|
174
|
+
await nextTick();
|
|
175
|
+
|
|
176
|
+
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
protected override __get(property: string): unknown {
|
|
180
|
+
if (!(property in this._fields)) {
|
|
181
|
+
return super.__get(property);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return this.getFieldValue(property);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
protected override __set(property: string, value: unknown): void {
|
|
188
|
+
if (!(property in this._fields)) {
|
|
189
|
+
super.__set(property, value);
|
|
190
|
+
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this.setFieldValue(property, value as FormData<Fields>[string]);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
|
|
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
|
+
}
|
|
208
|
+
|
|
209
|
+
errors.push(...validate(value, rule));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return errors.length > 0 ? errors : null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private getInitialData(fields: Fields): FormData<Fields> {
|
|
216
|
+
if (this.static().isConjuring()) {
|
|
217
|
+
return {} as FormData<Fields>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const data = Object.entries(fields).reduce((initialData, [name, definition]) => {
|
|
221
|
+
initialData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
222
|
+
|
|
223
|
+
return initialData;
|
|
224
|
+
}, {} as FormData<Fields>);
|
|
225
|
+
|
|
226
|
+
return reactive(data) as FormData<Fields>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private getInitialErrors(fields: Fields): FormErrors<Fields> {
|
|
230
|
+
if (this.static().isConjuring()) {
|
|
231
|
+
return {} as FormErrors<Fields>;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const errors = Object.keys(fields).reduce((formErrors, name) => {
|
|
235
|
+
formErrors[name as keyof Fields] = null;
|
|
236
|
+
|
|
237
|
+
return formErrors;
|
|
238
|
+
}, {} as FormErrors<Fields>);
|
|
239
|
+
|
|
240
|
+
return reactive(errors) as FormErrors<Fields>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private resetData(): void {
|
|
244
|
+
for (const [name, field] of Object.entries(this._fields)) {
|
|
245
|
+
this._data[name as keyof Fields] = (field.default ?? null) as FormData<Fields>[keyof Fields];
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private resetErrors(errors?: Record<string, string[] | null>): void {
|
|
250
|
+
Object.keys(this._errors).forEach((key) => delete this._errors[key as keyof Fields]);
|
|
251
|
+
|
|
252
|
+
errors && Object.assign(this._errors, errors);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
}
|
package/src/forms/index.ts
CHANGED