@aerogel/core 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e → 0.0.0-next.d34923f3b144e8f6720e6a9cdadb2cd4fb4ab289

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.
Files changed (114) hide show
  1. package/dist/aerogel-core.cjs.js +1 -1
  2. package/dist/aerogel-core.cjs.js.map +1 -1
  3. package/dist/aerogel-core.d.ts +1568 -252
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/histoire.config.ts +7 -0
  7. package/noeldemartin.config.js +4 -1
  8. package/package.json +13 -4
  9. package/postcss.config.js +6 -0
  10. package/src/assets/histoire.css +3 -0
  11. package/src/bootstrap/bootstrap.test.ts +4 -3
  12. package/src/bootstrap/index.ts +25 -5
  13. package/src/bootstrap/options.ts +3 -0
  14. package/src/components/AGAppLayout.vue +7 -2
  15. package/src/components/AGAppModals.vue +15 -0
  16. package/src/components/AGAppOverlays.vue +10 -8
  17. package/src/components/AGAppSnackbars.vue +13 -0
  18. package/src/components/constants.ts +8 -0
  19. package/src/components/forms/AGButton.vue +25 -15
  20. package/src/components/forms/AGCheckbox.vue +7 -1
  21. package/src/components/forms/AGForm.vue +9 -10
  22. package/src/components/forms/AGInput.vue +10 -6
  23. package/src/components/forms/AGSelect.story.vue +46 -0
  24. package/src/components/forms/AGSelect.vue +60 -0
  25. package/src/components/forms/index.ts +5 -6
  26. package/src/components/headless/forms/AGHeadlessButton.vue +18 -12
  27. package/src/components/headless/forms/AGHeadlessInput.ts +29 -4
  28. package/src/components/headless/forms/AGHeadlessInput.vue +17 -7
  29. package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
  30. package/src/components/headless/forms/AGHeadlessInputInput.vue +43 -5
  31. package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
  32. package/src/components/headless/forms/AGHeadlessInputTextArea.vue +42 -0
  33. package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
  34. package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
  35. package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
  36. package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
  37. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
  38. package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
  39. package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
  40. package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
  41. package/src/components/headless/forms/composition.ts +10 -0
  42. package/src/components/headless/forms/index.ts +12 -1
  43. package/src/components/headless/index.ts +1 -0
  44. package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
  45. package/src/components/headless/modals/AGHeadlessModal.vue +3 -5
  46. package/src/components/headless/modals/index.ts +4 -6
  47. package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
  48. package/src/components/headless/snackbars/index.ts +40 -0
  49. package/src/components/index.ts +4 -1
  50. package/src/components/interfaces.ts +9 -0
  51. package/src/components/lib/AGErrorMessage.vue +16 -0
  52. package/src/components/lib/AGLink.vue +9 -0
  53. package/src/components/{basic → lib}/AGMarkdown.vue +13 -7
  54. package/src/components/lib/AGMeasured.vue +15 -0
  55. package/src/components/lib/AGStartupCrash.vue +31 -0
  56. package/src/components/lib/index.ts +5 -0
  57. package/src/components/modals/AGAlertModal.ts +15 -0
  58. package/src/components/modals/AGAlertModal.vue +4 -16
  59. package/src/components/modals/AGConfirmModal.ts +33 -0
  60. package/src/components/modals/AGConfirmModal.vue +9 -13
  61. package/src/components/modals/AGErrorReportModal.ts +46 -0
  62. package/src/components/modals/AGErrorReportModal.vue +54 -0
  63. package/src/components/modals/AGErrorReportModalButtons.vue +111 -0
  64. package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
  65. package/src/components/modals/AGLoadingModal.ts +23 -0
  66. package/src/components/modals/AGLoadingModal.vue +4 -8
  67. package/src/components/modals/AGModal.ts +1 -1
  68. package/src/components/modals/AGModal.vue +16 -13
  69. package/src/components/modals/AGModalTitle.vue +9 -0
  70. package/src/components/modals/AGPromptModal.ts +36 -0
  71. package/src/components/modals/AGPromptModal.vue +34 -0
  72. package/src/components/modals/index.ts +16 -7
  73. package/src/components/snackbars/AGSnackbar.vue +36 -0
  74. package/src/components/snackbars/index.ts +3 -0
  75. package/src/components/utils.ts +10 -0
  76. package/src/directives/index.ts +20 -3
  77. package/src/directives/measure.ts +21 -0
  78. package/src/errors/Errors.ts +65 -12
  79. package/src/errors/index.ts +26 -1
  80. package/src/errors/utils.ts +19 -0
  81. package/src/forms/Form.ts +57 -9
  82. package/src/forms/index.ts +1 -0
  83. package/src/forms/utils.ts +15 -0
  84. package/src/jobs/Job.ts +5 -0
  85. package/src/jobs/index.ts +7 -0
  86. package/src/lang/Lang.ts +15 -23
  87. package/src/main.histoire.ts +1 -0
  88. package/src/main.ts +3 -2
  89. package/src/plugins/Plugin.ts +1 -0
  90. package/src/plugins/index.ts +19 -0
  91. package/src/services/App.state.ts +22 -4
  92. package/src/services/App.ts +38 -5
  93. package/src/services/Cache.ts +43 -0
  94. package/src/services/Events.test.ts +39 -0
  95. package/src/services/Events.ts +100 -30
  96. package/src/services/Service.ts +55 -16
  97. package/src/services/index.ts +11 -5
  98. package/src/services/store.ts +8 -5
  99. package/src/testing/index.ts +25 -0
  100. package/src/ui/UI.state.ts +10 -1
  101. package/src/ui/UI.ts +168 -26
  102. package/src/ui/index.ts +12 -3
  103. package/src/utils/composition/events.ts +1 -0
  104. package/src/utils/index.ts +1 -0
  105. package/src/utils/markdown.ts +11 -2
  106. package/src/utils/tailwindcss.test.ts +26 -0
  107. package/src/utils/tailwindcss.ts +7 -0
  108. package/src/utils/vue.ts +15 -4
  109. package/tailwind.config.js +4 -0
  110. package/tsconfig.json +1 -0
  111. package/vite.config.ts +2 -1
  112. package/.eslintrc.js +0 -3
  113. package/src/components/basic/index.ts +0 -3
  114. package/src/globals.ts +0 -6
@@ -1,12 +1,16 @@
1
- import { JSError, facade, isObject } from '@noeldemartin/utils';
1
+ import { JSError, facade, isObject, objectWithoutEmpty, toString } from '@noeldemartin/utils';
2
2
 
3
3
  import App from '@/services/App';
4
4
  import ServiceBootError from '@/errors/ServiceBootError';
5
- import UI from '@/ui/UI';
6
- import { translate } from '@/lang/utils';
5
+ import UI, { UIComponents } from '@/ui/UI';
6
+ import { translateWithDefault } from '@/lang/utils';
7
7
 
8
8
  import Service from './Errors.state';
9
+ import { Colors } from '@/components/constants';
10
+ import { Events } from '@/services';
11
+ import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
9
12
  import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
13
+ import type { ModalComponent } from '@/ui/UI.state';
10
14
 
11
15
  export class ErrorsService extends Service {
12
16
 
@@ -22,14 +26,27 @@ export class ErrorsService extends Service {
22
26
  }
23
27
 
24
28
  public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
25
- const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
29
+ const reports = Array.isArray(error) ? (error as ErrorReport[]) : [await this.createErrorReport(error)];
26
30
 
27
- // TODO open errors modal
28
- reports;
31
+ if (reports.length === 0) {
32
+ UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
33
+
34
+ return;
35
+ }
36
+
37
+ UI.openModal<ModalComponent<AGErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
38
+ reports,
39
+ });
29
40
  }
30
41
 
31
42
  public async report(error: ErrorSource, message?: string): Promise<void> {
32
- if (App.isDevelopment || App.isTesting) {
43
+ await Events.emit('error', { error, message });
44
+
45
+ if (App.testing) {
46
+ throw error;
47
+ }
48
+
49
+ if (App.development) {
33
50
  this.logError(error);
34
51
  }
35
52
 
@@ -37,7 +54,7 @@ export class ErrorsService extends Service {
37
54
  throw error;
38
55
  }
39
56
 
40
- if (!App.isMounted) {
57
+ if (!App.isMounted()) {
41
58
  const startupError = await this.createStartupErrorReport(error);
42
59
 
43
60
  if (startupError) {
@@ -54,8 +71,24 @@ export class ErrorsService extends Service {
54
71
  date: new Date(),
55
72
  };
56
73
 
57
- // TODO open error snackbar
58
- UI.alert(message ?? 'Something went wrong, but it\'s not your fault! (look at the console for details)');
74
+ UI.showSnackbar(
75
+ message ??
76
+ translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
77
+ {
78
+ color: Colors.Danger,
79
+ actions: [
80
+ {
81
+ text: translateWithDefault('errors.viewDetails', 'View details'),
82
+ dismiss: true,
83
+ handler: () =>
84
+ UI.openModal<ModalComponent<AGErrorReportModalProps>>(
85
+ UI.requireComponent(UIComponents.ErrorReportModal),
86
+ { reports: [report] },
87
+ ),
88
+ },
89
+ ],
90
+ },
91
+ );
59
92
 
60
93
  this.setState({ logs: [log].concat(this.logs) });
61
94
  }
@@ -102,8 +135,22 @@ export class ErrorsService extends Service {
102
135
  return this.createErrorReportFromError(error);
103
136
  }
104
137
 
138
+ if (isObject(error)) {
139
+ return objectWithoutEmpty({
140
+ title: toString(
141
+ error['name'] ?? error['title'] ?? translateWithDefault('errors.unknown', 'Unknown Error'),
142
+ ),
143
+ description: toString(
144
+ error['message'] ??
145
+ error['description'] ??
146
+ translateWithDefault('errors.unknownDescription', 'Unknown error object'),
147
+ ),
148
+ error,
149
+ });
150
+ }
151
+
105
152
  return {
106
- title: translate('errors.unknown'),
153
+ title: translateWithDefault('errors.unknown', 'Unknown Error'),
107
154
  error,
108
155
  };
109
156
  }
@@ -129,4 +176,10 @@ export class ErrorsService extends Service {
129
176
 
130
177
  }
131
178
 
132
- export default facade(new ErrorsService());
179
+ export default facade(ErrorsService);
180
+
181
+ declare module '@/services/Events' {
182
+ export interface EventsPayload {
183
+ error: { error: ErrorSource; message?: string };
184
+ }
185
+ }
@@ -1,21 +1,46 @@
1
+ import type { App } from 'vue';
2
+
1
3
  import { bootServices } from '@/services';
2
4
  import { definePlugin } from '@/plugins';
3
5
 
4
6
  import Errors from './Errors';
5
7
  import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
6
8
 
9
+ export * from './utils';
7
10
  export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
8
11
 
9
12
  const services = { $errors: Errors };
13
+ const frameworkHandler: ErrorHandler = (error) => {
14
+ Errors.report(error);
15
+
16
+ return true;
17
+ };
18
+
19
+ function setUpErrorHandler(app: App, baseHandler: ErrorHandler = () => false): void {
20
+ const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
21
+
22
+ app.config.errorHandler = errorHandler;
23
+ globalThis.onerror = (event, _, __, ___, error) => errorHandler(error ?? event);
24
+ globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
25
+ }
10
26
 
27
+ export type ErrorHandler = (error: ErrorSource) => boolean;
11
28
  export type ErrorsServices = typeof services;
12
29
 
13
30
  export default definePlugin({
14
- async install(app) {
31
+ async install(app, options) {
32
+ setUpErrorHandler(app, options.handleError);
33
+
15
34
  await bootServices(app, services);
16
35
  },
17
36
  });
18
37
 
38
+ declare module '@/bootstrap/options' {
39
+ export interface AerogelOptions {
40
+ handleError?(error: ErrorSource): boolean;
41
+ }
42
+ }
43
+
19
44
  declare module '@/services' {
20
45
  export interface Services extends ErrorsServices {}
21
46
  }
@@ -0,0 +1,19 @@
1
+ import { JSError, isObject, toString } from '@noeldemartin/utils';
2
+ import { translateWithDefault } from '@/lang/utils';
3
+ import type { ErrorSource } from './Errors.state';
4
+
5
+ export function getErrorMessage(error: ErrorSource): string {
6
+ if (typeof error === 'string') {
7
+ return error;
8
+ }
9
+
10
+ if (error instanceof Error || error instanceof JSError) {
11
+ return error.message;
12
+ }
13
+
14
+ if (isObject(error)) {
15
+ return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
16
+ }
17
+
18
+ return translateWithDefault('errors.unknown', 'Unknown Error');
19
+ }
package/src/forms/Form.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { MagicObject } from '@noeldemartin/utils';
2
- import { computed, reactive, readonly, ref } from 'vue';
1
+ import { MagicObject, arrayRemove } from '@noeldemartin/utils';
2
+ import { computed, nextTick, reactive, readonly, ref } from 'vue';
3
3
  import type { ObjectValues } from '@noeldemartin/utils';
4
4
  import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
5
5
 
@@ -7,6 +7,8 @@ export const FormFieldTypes = {
7
7
  String: 'string',
8
8
  Number: 'number',
9
9
  Boolean: 'boolean',
10
+ Object: 'object',
11
+ Date: 'date',
10
12
  } as const;
11
13
 
12
14
  export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
@@ -17,6 +19,7 @@ export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType
17
19
 
18
20
  export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
19
21
  export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
22
+ export type FormFieldValue = GetFormFieldValue<FormFieldType>;
20
23
 
21
24
  export type FormData<T> = {
22
25
  -readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
@@ -36,17 +39,26 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
36
39
  ? number
37
40
  : TType extends typeof FormFieldTypes.Boolean
38
41
  ? boolean
42
+ : TType extends typeof FormFieldTypes.Object
43
+ ? object
44
+ : TType extends typeof FormFieldTypes.Date
45
+ ? Date
39
46
  : never;
40
47
 
48
+ const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
49
+
50
+ export type SubmitFormListener = () => unknown;
51
+ export type FocusFormListener = (input: string) => unknown;
52
+
41
53
  export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
42
54
 
43
55
  public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
44
56
 
45
57
  private _fields: Fields;
46
58
  private _data: FormData<Fields>;
47
- private _valid: ComputedRef<boolean>;
48
59
  private _submitted: Ref<boolean>;
49
60
  private _errors: FormErrors<Fields>;
61
+ private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
50
62
 
51
63
  constructor(fields: Fields) {
52
64
  super();
@@ -55,13 +67,17 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
55
67
  this._submitted = ref(false);
56
68
  this._data = this.getInitialData(fields);
57
69
  this._errors = this.getInitialErrors(fields);
58
- this._valid = computed(() => !Object.values(this._errors).some((error) => error !== null));
70
+
71
+ validForms.set(
72
+ this,
73
+ computed(() => !Object.values(this._errors).some((error) => error !== null)),
74
+ );
59
75
 
60
76
  this.errors = readonly(this._errors);
61
77
  }
62
78
 
63
79
  public get valid(): boolean {
64
- return this._valid.value;
80
+ return !!validForms.get(this)?.value;
65
81
  }
66
82
 
67
83
  public get submitted(): boolean {
@@ -80,6 +96,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
80
96
  return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
81
97
  }
82
98
 
99
+ public data(): FormData<Fields> {
100
+ return { ...this._data };
101
+ }
102
+
83
103
  public validate(): boolean {
84
104
  const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
85
105
  formErrors[name] = this.getFieldErrors(name, definition);
@@ -92,17 +112,45 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
92
112
  return this.valid;
93
113
  }
94
114
 
95
- public reset(): void {
115
+ public reset(options: { keepData?: boolean; keepErrors?: boolean } = {}): void {
96
116
  this._submitted.value = false;
97
117
 
98
- this.resetData();
99
- this.resetErrors();
118
+ options.keepData || this.resetData();
119
+ options.keepErrors || this.resetErrors();
100
120
  }
101
121
 
102
122
  public submit(): boolean {
103
123
  this._submitted.value = true;
104
124
 
105
- return this.validate();
125
+ const valid = this.validate();
126
+
127
+ valid && this._listeners['submit']?.forEach((listener) => listener());
128
+
129
+ return valid;
130
+ }
131
+
132
+ public on(event: 'focus', listener: FocusFormListener): () => void;
133
+ public on(event: 'submit', listener: SubmitFormListener): () => void;
134
+ public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
135
+ this._listeners[event] ??= [];
136
+
137
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
138
+ this._listeners[event]?.push(listener as any);
139
+
140
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
141
+ return () => this.off(event as any, listener);
142
+ }
143
+
144
+ public off(event: 'focus', listener: FocusFormListener): void;
145
+ public off(event: 'submit', listener: SubmitFormListener): void;
146
+ public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
147
+ arrayRemove(this._listeners[event] ?? [], listener);
148
+ }
149
+
150
+ public async focus(input: string): Promise<void> {
151
+ await nextTick();
152
+
153
+ this._listeners['focus']?.forEach((listener) => listener(input));
106
154
  }
107
155
 
108
156
  protected __get(property: string): unknown {
@@ -1,3 +1,4 @@
1
1
  export * from './Form';
2
2
  export * from './composition';
3
3
  export * from './utils';
4
+ export { default as Form } from './Form';
@@ -8,6 +8,13 @@ export function booleanInput(defaultValue?: boolean): FormFieldDefinition<typeof
8
8
  };
9
9
  }
10
10
 
11
+ export function dateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
12
+ return {
13
+ default: defaultValue,
14
+ type: FormFieldTypes.Date,
15
+ };
16
+ }
17
+
11
18
  export function requiredBooleanInput(
12
19
  defaultValue?: boolean,
13
20
  ): FormFieldDefinition<typeof FormFieldTypes.Boolean, 'required'> {
@@ -18,6 +25,14 @@ export function requiredBooleanInput(
18
25
  };
19
26
  }
20
27
 
28
+ export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
29
+ return {
30
+ default: defaultValue,
31
+ type: FormFieldTypes.Date,
32
+ rules: 'required',
33
+ };
34
+ }
35
+
21
36
  export function requiredNumberInput(
22
37
  defaultValue?: number,
23
38
  ): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
@@ -0,0 +1,5 @@
1
+ export default abstract class Job {
2
+
3
+ public abstract run(): Promise<void>;
4
+
5
+ }
@@ -0,0 +1,7 @@
1
+ import Job from './Job';
2
+
3
+ export { Job };
4
+
5
+ export async function dispatch(job: Job): Promise<void> {
6
+ await job.run();
7
+ }
package/src/lang/Lang.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { facade, toString } from '@noeldemartin/utils';
1
+ import { facade } from '@noeldemartin/utils';
2
2
 
3
3
  import App from '@/services/App';
4
4
  import Service from '@/services/Service';
5
5
 
6
6
  export interface LangProvider {
7
- translate(key: string, parameters?: Record<string, unknown>): string;
7
+ translate(key: string, parameters?: Record<string, unknown> | number): string;
8
+ translateWithDefault(key: string, defaultMessage: string, parameters?: Record<string, unknown> | number): string;
8
9
  }
9
10
 
10
11
  export class LangService extends Service {
@@ -17,10 +18,16 @@ export class LangService extends Service {
17
18
  this.provider = {
18
19
  translate: (key) => {
19
20
  // eslint-disable-next-line no-console
20
- App.isDevelopment && console.warn('Lang provider is missing');
21
+ App.development && console.warn('Lang provider is missing');
21
22
 
22
23
  return key;
23
24
  },
25
+ translateWithDefault: (_, defaultMessage) => {
26
+ // eslint-disable-next-line no-console
27
+ App.development && console.warn('Lang provider is missing');
28
+
29
+ return defaultMessage;
30
+ },
24
31
  };
25
32
  }
26
33
 
@@ -28,33 +35,18 @@ export class LangService extends Service {
28
35
  this.provider = provider;
29
36
  }
30
37
 
31
- public translate(key: string, parameters?: Record<string, unknown>): string {
38
+ public translate(key: string, parameters?: Record<string, unknown> | number): string {
32
39
  return this.provider.translate(key, parameters) ?? key;
33
40
  }
34
41
 
35
- public translateWithDefault(key: string, defaultMessage: string): string;
36
- public translateWithDefault(key: string, parameters: Record<string, unknown>, defaultMessage: string): string;
37
42
  public translateWithDefault(
38
43
  key: string,
39
- defaultMessageOrParameters?: string | Record<string, unknown>,
40
- defaultMessage?: string,
44
+ defaultMessage: string,
45
+ parameters: Record<string, unknown> | number = {},
41
46
  ): string {
42
- defaultMessage ??= defaultMessageOrParameters as string;
43
-
44
- const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters ?? {};
45
- const message = this.provider.translate(key, parameters) ?? key;
46
-
47
- if (message === key) {
48
- return Object.entries(parameters).reduce(
49
- (renderedMessage, [name, value]) =>
50
- renderedMessage.replace(new RegExp(`\\{\\s*${name}\\s*\\}`, 'g'), toString(value)),
51
- defaultMessage,
52
- );
53
- }
54
-
55
- return message;
47
+ return this.provider.translateWithDefault(key, defaultMessage, parameters);
56
48
  }
57
49
 
58
50
  }
59
51
 
60
- export default facade(new LangService());
52
+ export default facade(LangService);
@@ -0,0 +1 @@
1
+ import './assets/histoire.css';
package/src/main.ts CHANGED
@@ -1,11 +1,12 @@
1
- import './globals';
2
-
3
1
  export * from './bootstrap';
4
2
  export * from './components';
3
+ export * from './directives';
5
4
  export * from './errors';
6
5
  export * from './forms';
6
+ export * from './jobs';
7
7
  export * from './lang';
8
8
  export * from './plugins';
9
9
  export * from './services';
10
+ export * from './testing';
10
11
  export * from './ui';
11
12
  export * from './utils';
@@ -3,5 +3,6 @@ import type { App } from 'vue';
3
3
  import type { AerogelOptions } from '@/bootstrap/options';
4
4
 
5
5
  export interface Plugin {
6
+ name?: string;
6
7
  install(app: App, options: AerogelOptions): void | Promise<void>;
7
8
  }
@@ -1,3 +1,7 @@
1
+ import type { GetClosureArgs } from '@noeldemartin/utils';
2
+
3
+ import App from '@/services/App';
4
+
1
5
  import type { Plugin } from './Plugin';
2
6
 
3
7
  export * from './Plugin';
@@ -5,3 +9,18 @@ export * from './Plugin';
5
9
  export function definePlugin<T extends Plugin>(plugin: T): T {
6
10
  return plugin;
7
11
  }
12
+
13
+ export async function installPlugins(plugins: Plugin[], ...args: GetClosureArgs<Plugin['install']>): Promise<void> {
14
+ App.setState(
15
+ 'plugins',
16
+ plugins.reduce((pluginsMap, plugin) => {
17
+ if (plugin.name) {
18
+ pluginsMap[plugin.name] = plugin;
19
+ }
20
+
21
+ return pluginsMap;
22
+ }, {} as Record<string, Plugin>),
23
+ );
24
+
25
+ await Promise.all(plugins.map((plugin) => plugin.install(...args)) ?? []);
26
+ }
@@ -1,13 +1,31 @@
1
+ import Aerogel from 'virtual:aerogel';
2
+
1
3
  import { defineServiceState } from '@/services/Service';
4
+ import type { Plugin } from '@/plugins/Plugin';
2
5
 
3
6
  export default defineServiceState({
4
7
  name: 'app',
5
8
  initialState: {
6
- environment: __AG_ENV,
7
- isMounted: false,
9
+ plugins: {} as Record<string, Plugin>,
10
+ environment: Aerogel.environment,
11
+ version: Aerogel.version,
12
+ sourceUrl: Aerogel.sourceUrl,
8
13
  },
9
14
  computed: {
10
- isDevelopment: (state) => state.environment === 'development',
11
- isTesting: (state) => state.environment === 'testing',
15
+ development: (state) => state.environment === 'development',
16
+ testing: (state) => state.environment === 'test' || state.environment === 'testing',
17
+ versionName(state): string {
18
+ if (this.development) {
19
+ return 'dev.' + Aerogel.sourceHash.toString().substring(0, 7);
20
+ }
21
+
22
+ return `v${state.version}`;
23
+ },
24
+ versionUrl(state): string {
25
+ return (
26
+ state.sourceUrl +
27
+ (this.development ? `/tree/${Aerogel.sourceHash}` : `/releases/tag/${this.versionName}`)
28
+ );
29
+ },
12
30
  },
13
31
  });
@@ -1,17 +1,50 @@
1
- import { facade } from '@noeldemartin/utils';
1
+ import Aerogel from 'virtual:aerogel';
2
+
3
+ import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
2
4
 
3
5
  import Events from '@/services/Events';
6
+ import type { Plugin } from '@/plugins';
4
7
 
5
8
  import Service from './App.state';
6
9
 
7
10
  export class AppService extends Service {
8
11
 
9
- protected async boot(): Promise<void> {
10
- await super.boot();
12
+ public readonly name = Aerogel.name;
13
+ public readonly ready = new PromisedValue<void>();
14
+ public readonly mounted = new PromisedValue<void>();
15
+
16
+ public isReady(): boolean {
17
+ return this.ready.isResolved();
18
+ }
19
+
20
+ public isMounted(): boolean {
21
+ return this.mounted.isResolved();
22
+ }
23
+
24
+ public async whenReady<T>(callback: () => T): Promise<T> {
25
+ const result = await this.ready.then(callback);
26
+
27
+ return result;
28
+ }
29
+
30
+ public async reload(queryParameters?: Record<string, string | undefined>): Promise<void> {
31
+ queryParameters && updateLocationQueryParameters(queryParameters);
32
+
33
+ location.reload();
34
+
35
+ // Stall until the reload happens
36
+ await forever();
37
+ }
38
+
39
+ public plugin<T extends Plugin = Plugin>(name: string): T | null {
40
+ return (this.plugins[name] as T) ?? null;
41
+ }
11
42
 
12
- Events.once('application-mounted', () => this.setState({ isMounted: true }));
43
+ protected async boot(): Promise<void> {
44
+ Events.once('application-ready', () => this.ready.resolve());
45
+ Events.once('application-mounted', () => this.mounted.resolve());
13
46
  }
14
47
 
15
48
  }
16
49
 
17
- export default facade(new AppService());
50
+ export default facade(AppService);
@@ -0,0 +1,43 @@
1
+ import { PromisedValue, facade, tap } from '@noeldemartin/utils';
2
+
3
+ import Service from '@/services/Service';
4
+
5
+ export class CacheService extends Service {
6
+
7
+ private cache?: PromisedValue<Cache> = undefined;
8
+
9
+ public async get(url: string): Promise<Response | null> {
10
+ const cache = await this.open();
11
+ const response = await cache.match(url);
12
+
13
+ return response ?? null;
14
+ }
15
+
16
+ public async store(url: string, response: Response): Promise<void> {
17
+ const cache = await this.open();
18
+
19
+ await cache.put(url, response);
20
+ }
21
+
22
+ public async replace(url: string, response: Response): Promise<void> {
23
+ const cache = await this.open();
24
+ const keys = await cache.keys(url);
25
+
26
+ if (keys.length === 0) {
27
+ return;
28
+ }
29
+
30
+ await cache.put(url, response);
31
+ }
32
+
33
+ protected async open(): Promise<Cache> {
34
+ return (this.cache =
35
+ this.cache ??
36
+ tap(new PromisedValue<Cache>(), (cache) => {
37
+ caches.open('app').then((instance) => cache.resolve(instance));
38
+ }));
39
+ }
40
+
41
+ }
42
+
43
+ export default facade(CacheService);
@@ -0,0 +1,39 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
+
3
+ import Events, { EventListenerPriorities } from './Events';
4
+
5
+ describe('Events', () => {
6
+
7
+ beforeEach(() => void Events.reset());
8
+
9
+ it('registers listeners', async () => {
10
+ // Arrange
11
+ let counter = 0;
12
+
13
+ Events.on('trigger', () => counter++);
14
+
15
+ // Act
16
+ await Events.emit('trigger');
17
+ await Events.emit('trigger');
18
+ await Events.emit('trigger');
19
+
20
+ // Assert
21
+ expect(counter).toEqual(3);
22
+ });
23
+
24
+ it('triggers listeners by priority', async () => {
25
+ // Arrange
26
+ const storage: string[] = [];
27
+
28
+ Events.on('trigger', () => storage.push('second'));
29
+ Events.on('trigger', { priority: EventListenerPriorities.Low }, () => storage.push('third'));
30
+ Events.on('trigger', { priority: EventListenerPriorities.High }, () => storage.push('first'));
31
+
32
+ // Act
33
+ await Events.emit('trigger');
34
+
35
+ // Assert
36
+ expect(storage).toEqual(['first', 'second', 'third']);
37
+ });
38
+
39
+ });