@zoobzio/nuxt 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,88 @@
1
+ import type { AppError } from "../types/error";
2
+ import type { RequestOptions } from "../types/request";
3
+ import type { MaybeRefOrGetter } from "vue";
4
+
5
+ import { toValue, useAsyncData, useLazyAsyncData, watch } from "#imports";
6
+ import { status } from "ltrl-http";
7
+ import { useLogger } from "./log";
8
+ import { useAppError } from "./error";
9
+
10
+ /**
11
+ * Builds a request composable over one of Nuxt's async-data composables.
12
+ * `blocking` sets `server`: a blocking request runs during SSR and a
13
+ * failure there is fatal by default; a lazy one runs on the client only
14
+ * and a failure toasts by default.
15
+ */
16
+ const make = (
17
+ fn: typeof useAsyncData | typeof useLazyAsyncData,
18
+ blocking: boolean,
19
+ ) => {
20
+ return <ResT, DataT = ResT>(
21
+ key: MaybeRefOrGetter<string>,
22
+ handler: () => Promise<ResT>,
23
+ opts: RequestOptions<ResT, DataT> = {},
24
+ ) => {
25
+ const id = crypto.randomUUID();
26
+ const log = useLogger(toValue(key));
27
+
28
+ log.debug("Request started", {
29
+ id,
30
+ start: new Date().toISOString(),
31
+ });
32
+
33
+ const { error: errOpts, ...asyncOpts } = opts;
34
+
35
+ const results = fn<ResT, AppError, DataT>(key, handler, {
36
+ ...asyncOpts,
37
+ server: blocking,
38
+ });
39
+
40
+ watch(
41
+ results.status,
42
+ (current) => {
43
+ if (current === "idle" || current === "pending") return;
44
+ if (current === "error") {
45
+ log.error("Request failed", {
46
+ id,
47
+ error: results.error.value,
48
+ });
49
+ throw useAppError(
50
+ errOpts?.status ?? status.INTERNAL_SERVER_ERROR,
51
+ errOpts?.message ?? "Request failed",
52
+ {
53
+ notification: errOpts?.notification ?? {
54
+ title: "An unknown error occurred",
55
+ variant: "error",
56
+ description: results.error.value?.message,
57
+ },
58
+ cause: errOpts?.cause ?? results.error.value,
59
+ fatal: errOpts?.fatal ?? blocking,
60
+ },
61
+ );
62
+ }
63
+ log.debug("Request finished", {
64
+ id,
65
+ end: new Date().toISOString(),
66
+ status: current,
67
+ results: results.data.value,
68
+ });
69
+ },
70
+ { immediate: true },
71
+ );
72
+
73
+ return results;
74
+ };
75
+ };
76
+
77
+ /**
78
+ * A request that blocks navigation: `useAsyncData` with `server: true`.
79
+ * A failure throws a fatal app error by default.
80
+ */
81
+ export const useRequest = make(useAsyncData, true);
82
+
83
+ /**
84
+ * A request that does not block navigation: `useLazyAsyncData` with
85
+ * `server: false`, so it runs on the client. A failure throws a non-fatal
86
+ * app error by default, which the shell toasts.
87
+ */
88
+ export const useLazyRequest = make(useLazyAsyncData, false);
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Status code the error page shows when the error carries none.
3
+ */
4
+ export const ERROR_DEFAULT_STATUS_CODE = 500;
5
+
6
+ /**
7
+ * Status code the error page reads as "not found".
8
+ */
9
+ export const ERROR_NOT_FOUND_STATUS_CODE = 404;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The name of the root logger, and the default of `useLogger`.
3
+ */
4
+ export const LOG_ROOT_NAME = "app";
5
+
6
+ /**
7
+ * The log levels by severity. A line is kept when its level is at or above
8
+ * the current level.
9
+ */
10
+ export const LOG_LEVELS = {
11
+ trace: 0,
12
+ debug: 1,
13
+ info: 2,
14
+ warn: 3,
15
+ error: 4,
16
+ } as const;
package/app/error.vue ADDED
@@ -0,0 +1,35 @@
1
+ <script lang="ts">
2
+ import type { NuxtError } from "#app";
3
+
4
+ import { clearError, computed } from "#imports";
5
+ import {
6
+ ERROR_DEFAULT_STATUS_CODE,
7
+ ERROR_NOT_FOUND_STATUS_CODE,
8
+ } from "./constants/error";
9
+ </script>
10
+
11
+ <script setup lang="ts">
12
+ const props = defineProps<{
13
+ error: NuxtError;
14
+ }>();
15
+
16
+ const statusCode = computed(
17
+ () => props.error.statusCode ?? ERROR_DEFAULT_STATUS_CODE,
18
+ );
19
+
20
+ const message = computed(() => {
21
+ if (statusCode.value === ERROR_NOT_FOUND_STATUS_CODE)
22
+ return "The page you're looking for doesn't exist.";
23
+ return props.error.message || "Something went wrong.";
24
+ });
25
+
26
+ const handleError = () => clearError({ redirect: "/" });
27
+ </script>
28
+
29
+ <template>
30
+ <div class="f-error-page">
31
+ <span class="f-error-code">{{ statusCode }}</span>
32
+ <span class="f-error-message">{{ message }}</span>
33
+ <button class="f-error-action" @click="handleError">Go home</button>
34
+ </div>
35
+ </template>
@@ -0,0 +1,55 @@
1
+ import type { BrowserOptions, UseBrowser } from "../types/browser";
2
+ import type { Actions, Config, Service } from "@zoobzio/browser";
3
+
4
+ import { useNuxtApp } from "#imports";
5
+ import { BrowserService } from "@zoobzio/browser";
6
+ import { accessBrowser } from "../stores/browser";
7
+ import { split } from "../utils/actions";
8
+
9
+ /**
10
+ * Builds the id-scoped composable of a browser. Call it at module scope:
11
+ * the options split once into the headless config (the descriptors) and
12
+ * the wiring (the fetch and the handlers), and `id` keys everything that
13
+ * follows. Each call of the composable, in setup, builds a service over
14
+ * the id's shared store and relays its domain events onto the app logger and the Nuxt hook
15
+ * bus as `browser:updated` and `browser:navigated`. The machine itself is
16
+ * `@zoobzio/browser`; the Vue browser renders it through its `service`
17
+ * prop.
18
+ */
19
+ export const createBrowser = <T>(
20
+ id: string,
21
+ options: BrowserOptions<T>,
22
+ ): UseBrowser<T> => {
23
+ const { fetch, actions, folderActions, bulkActions, initial, ...rest } =
24
+ options;
25
+ const files = split(actions);
26
+ const folders = split(folderActions);
27
+ const bulk = split(bulkActions);
28
+ const config: Config<T> = {
29
+ ...rest,
30
+ actions: files.descriptors,
31
+ folderActions: folders.descriptors,
32
+ bulkActions: bulk.descriptors,
33
+ };
34
+ const wiring: Actions<T> = {
35
+ fetch,
36
+ actions: files.handlers,
37
+ folderActions: folders.handlers,
38
+ bulkActions: bulk.handlers,
39
+ };
40
+ return (): Service<T> => {
41
+ const nuxt = useNuxtApp();
42
+ const log = nuxt.$logger(id);
43
+ const state = accessBrowser<T>(id, initial);
44
+ const service = new BrowserService(id, config, state, wiring);
45
+ service.on("updated", (event) => {
46
+ log.debug("Browser updated", event);
47
+ nuxt.callHook("browser:updated", event);
48
+ });
49
+ service.on("navigated", (event) => {
50
+ log.debug("Browser navigated", event);
51
+ nuxt.callHook("browser:navigated", event);
52
+ });
53
+ return service;
54
+ };
55
+ };
@@ -0,0 +1,54 @@
1
+ import type { FormOptions, UseForm } from "../types/form";
2
+ import type { Actions, Service } from "@zoobzio/form";
3
+
4
+ import { useNuxtApp } from "#imports";
5
+ import { FormService } from "@zoobzio/form";
6
+ import { accessForm } from "../stores/form";
7
+
8
+ /**
9
+ * Builds the id-scoped composable of a form. Call it at module scope: the
10
+ * options split once into the headless config and the wiring (the
11
+ * lifecycle actions and the middleware), and `id` keys everything that
12
+ * follows. Each call of the composable, in setup, builds a service over
13
+ * the id's shared store and relays its domain events onto the app logger and the Nuxt hook
14
+ * bus under the `form:` prefix. The machine itself is `@zoobzio/form`; the
15
+ * Vue form renders it through its `service` prop.
16
+ */
17
+ export const createForm = <T>(
18
+ id: string,
19
+ options: FormOptions<T>,
20
+ ): UseForm<T> => {
21
+ const { init, submit, middleware, initial, ...config } = options;
22
+ const wiring: Actions<T> = { init, submit, middleware };
23
+ return (): Service<T> => {
24
+ const nuxt = useNuxtApp();
25
+ const log = nuxt.$logger(id);
26
+ const state = accessForm(id, config, initial);
27
+ const service = new FormService(id, config, state, wiring);
28
+ service.on("initialized", (event) => {
29
+ log.debug("Form initialized", event);
30
+ nuxt.callHook("form:initialized", event);
31
+ });
32
+ service.on("submitted", (event) => {
33
+ log.debug("Form submitted", event);
34
+ nuxt.callHook("form:submitted", event);
35
+ });
36
+ service.on("rejected", (event) => {
37
+ log.debug("Form rejected", event);
38
+ nuxt.callHook("form:rejected", event);
39
+ });
40
+ service.on("restored", (event) => {
41
+ log.debug("Form restored", event);
42
+ nuxt.callHook("form:restored", event);
43
+ });
44
+ service.on("reset", (event) => {
45
+ log.debug("Form reset", event);
46
+ nuxt.callHook("form:reset", event);
47
+ });
48
+ service.on("failed", (event) => {
49
+ log.debug("Form failed", event);
50
+ nuxt.callHook("form:failed", event);
51
+ });
52
+ return service;
53
+ };
54
+ };
@@ -0,0 +1,50 @@
1
+ import type { TableOptions, UseTable } from "../types/table";
2
+ import type { Actions, Config, Service } from "@zoobzio/table";
3
+
4
+ import { useNuxtApp } from "#imports";
5
+ import { TableService } from "@zoobzio/table";
6
+ import { accessTable } from "../stores/table";
7
+ import { split } from "../utils/actions";
8
+
9
+ /**
10
+ * Builds the id-scoped composable of a table. Call it at module scope: the
11
+ * options split once into the headless config (the descriptors) and the
12
+ * wiring (the fetch and the handlers), and `id` keys everything that
13
+ * follows. Each call of the composable, in setup, builds a service over
14
+ * the id's shared store and relays its domain events onto the app logger and the Nuxt hook
15
+ * bus as `table:updated` and `table:filtered`. The machine itself is
16
+ * `@zoobzio/table`; the Vue table renders it through its `service` prop.
17
+ */
18
+ export const createTable = <T>(
19
+ id: string,
20
+ options: TableOptions<T>,
21
+ ): UseTable<T> => {
22
+ const { fetch, actions, bulkActions, initial, ...rest } = options;
23
+ const rows = split(actions);
24
+ const bulk = split(bulkActions);
25
+ const config: Config<T> = {
26
+ ...rest,
27
+ actions: rows.descriptors,
28
+ bulkActions: bulk.descriptors,
29
+ };
30
+ const wiring: Actions<T> = {
31
+ fetch,
32
+ actions: rows.handlers,
33
+ bulkActions: bulk.handlers,
34
+ };
35
+ return (): Service<T> => {
36
+ const nuxt = useNuxtApp();
37
+ const log = nuxt.$logger(id);
38
+ const state = accessTable(id, config, initial);
39
+ const service = new TableService(id, config, state, wiring);
40
+ service.on("updated", (event) => {
41
+ log.debug("Table updated", event);
42
+ nuxt.callHook("table:updated", event);
43
+ });
44
+ service.on("filtered", (event) => {
45
+ log.debug("Table filtered", event);
46
+ nuxt.callHook("table:filtered", event);
47
+ });
48
+ return service;
49
+ };
50
+ };
@@ -0,0 +1,17 @@
1
+ import { defineNuxtPlugin } from "#imports";
2
+ import { useLogger } from "../composables/log";
3
+ import { LOG_ROOT_NAME } from "../constants/log";
4
+
5
+ /**
6
+ * Provides the root logger as `nuxt.$log` and the logger constructor as
7
+ * `nuxt.$logger`, so code that holds the app instance (the factories in
8
+ * particular) can log without resolving the composable from setup scope.
9
+ */
10
+ export default defineNuxtPlugin(() => {
11
+ return {
12
+ provide: {
13
+ log: useLogger(LOG_ROOT_NAME),
14
+ logger: useLogger,
15
+ },
16
+ };
17
+ });
@@ -0,0 +1,23 @@
1
+ import type { BrowserInitial } from "../types/browser";
2
+ import type { State } from "@zoobzio/browser";
3
+
4
+ import { useState } from "#imports";
5
+ import { createBrowserState } from "@zoobzio/browser";
6
+ import { seed } from "../utils/state";
7
+
8
+ /**
9
+ * The Nuxt backing for a browser's state container: one `useState` record
10
+ * keyed `browser-<id>`. Its value is a reactive proxy, so the service's
11
+ * field writes are tracked; instances sharing an id share the container
12
+ * across the app, and it survives SSR hydration. No config parameter: the
13
+ * trail starts at the root and sorting starts unset, unless `initial` says
14
+ * otherwise. It shapes the record once, when it is first created; later
15
+ * calls under the same id get the record as it stands.
16
+ */
17
+ export const accessBrowser = <T>(
18
+ id: string,
19
+ initial?: BrowserInitial<T>,
20
+ ): State<T> =>
21
+ useState<State<T>>(`browser-${id}`, () =>
22
+ seed(createBrowserState<T>(), initial),
23
+ ).value;
@@ -0,0 +1,22 @@
1
+ import type { FormInitial } from "../types/form";
2
+ import type { Config, State } from "@zoobzio/form";
3
+
4
+ import { useState } from "#imports";
5
+ import { createFormState } from "@zoobzio/form";
6
+ import { seed } from "../utils/state";
7
+
8
+ /**
9
+ * The Nuxt backing for a form's state container: one `useState` record
10
+ * keyed `form-<id>`. Its value is a reactive proxy, so the service's field
11
+ * writes are tracked; instances sharing an id share the container across
12
+ * the app, and it survives SSR hydration. `config` (its defaults) and
13
+ * `initial` shape the record once, when it is first created; later calls
14
+ * under the same id get the record as it stands.
15
+ */
16
+ export const accessForm = <T>(
17
+ id: string,
18
+ config: Config<T>,
19
+ initial?: FormInitial<T>,
20
+ ): State<T> =>
21
+ useState<State<T>>(`form-${id}`, () => seed(createFormState(config), initial))
22
+ .value;
@@ -0,0 +1,14 @@
1
+ import type { LogLevel, LogLine } from "../types/log";
2
+
3
+ import { useState } from "#imports";
4
+
5
+ /**
6
+ * The Nuxt backing for the app's logs: the current level under `log:level`
7
+ * (default `info`) and the kept lines under `log:backlog`, each a `useState`
8
+ * record shared by every logger.
9
+ */
10
+ export const accessLogs = () => {
11
+ const level = useState<LogLevel>("log:level", () => "info");
12
+ const backlog = useState<LogLine[]>("log:backlog", () => []);
13
+ return { level, backlog };
14
+ };
@@ -0,0 +1,13 @@
1
+ import type { Notification } from "../types/notification";
2
+
3
+ import { useState } from "#imports";
4
+
5
+ /**
6
+ * The Nuxt backing for the app's notifications: one `useState` record,
7
+ * keyed `app:notifications`, shared by every caller and carried across SSR
8
+ * hydration.
9
+ */
10
+ export const accessNotifications = () => {
11
+ const notifications = useState<Notification[]>("app:notifications", () => []);
12
+ return { notifications };
13
+ };
@@ -0,0 +1,23 @@
1
+ import type { TableInitial } from "../types/table";
2
+ import type { Config, State } from "@zoobzio/table";
3
+
4
+ import { useState } from "#imports";
5
+ import { createTableState } from "@zoobzio/table";
6
+ import { seed } from "../utils/state";
7
+
8
+ /**
9
+ * The Nuxt backing for a table's state container: one `useState` record
10
+ * keyed `table-<id>`. Its value is a reactive proxy, so the service's field
11
+ * writes are tracked; instances sharing an id share the container across
12
+ * the app, and it survives SSR hydration. `config` and `initial` shape the
13
+ * record once, when it is first created; later calls under the same id get
14
+ * the record as it stands.
15
+ */
16
+ export const accessTable = <T>(
17
+ id: string,
18
+ config: Config<T>,
19
+ initial?: TableInitial<T>,
20
+ ): State<T> =>
21
+ useState<State<T>>(`table-${id}`, () =>
22
+ seed(createTableState(config), initial),
23
+ ).value;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * An action as the layer takes it: the headless descriptor (how it renders)
3
+ * with its handler (what it does) under one key, so the two halves the
4
+ * services keep apart cannot drift. `split` takes them apart again.
5
+ */
6
+ export type Bound<D extends object, F> = D & { action: F };
@@ -0,0 +1,71 @@
1
+ import type { Bound } from "./actions";
2
+ import type { Hooks } from "./hooks";
3
+ import type {
4
+ ActionDescriptor,
5
+ Actions,
6
+ BulkAction,
7
+ Config,
8
+ Events,
9
+ FileAction,
10
+ FolderAction,
11
+ Service,
12
+ State,
13
+ } from "@zoobzio/browser";
14
+
15
+ /**
16
+ * A file action: its descriptor and its handler, under the config key.
17
+ */
18
+ export type BrowserAction<T> = Bound<ActionDescriptor, FileAction<T>["action"]>;
19
+
20
+ /**
21
+ * A folder action: its descriptor and its handler, under the config key.
22
+ */
23
+ export type BrowserFolderAction = Bound<
24
+ ActionDescriptor,
25
+ FolderAction["action"]
26
+ >;
27
+
28
+ /**
29
+ * A bulk action over the selected files: its descriptor and its handler,
30
+ * under the config key.
31
+ */
32
+ export type BrowserBulkAction = Bound<ActionDescriptor, BulkAction["action"]>;
33
+
34
+ /**
35
+ * The slice of the state container a browser may start from: the trail,
36
+ * the file sort, and the selection the consumer chooses, never what a fetch
37
+ * fills in. It seeds the `useState` record, so it must survive Nuxt's
38
+ * payload (devalue carries the `Set`).
39
+ */
40
+ export type BrowserInitial<T> = Partial<
41
+ Pick<State<T>, "path" | "sortField" | "sortDirection" | "selected">
42
+ >;
43
+
44
+ /**
45
+ * What `createBrowser` takes: the headless config with each action's
46
+ * handler joined to its descriptor, the fetch mechanism, and the initial
47
+ * state.
48
+ */
49
+ export type BrowserOptions<T> = Omit<
50
+ Config<T>,
51
+ "actions" | "folderActions" | "bulkActions"
52
+ > & {
53
+ fetch: Actions<T>["fetch"];
54
+ actions?: Record<string, BrowserAction<T>>;
55
+ folderActions?: Record<string, BrowserFolderAction>;
56
+ bulkActions?: Record<string, BrowserBulkAction>;
57
+ initial?: BrowserInitial<T>;
58
+ };
59
+
60
+ /**
61
+ * The Nuxt hooks a browser relays: `browser:updated` and
62
+ * `browser:navigated`, each carrying the service's payload with its
63
+ * scoping `id`.
64
+ */
65
+ export type BrowserHooks = Hooks<"browser", Events<unknown>>;
66
+
67
+ /**
68
+ * The id-scoped composable `createBrowser` returns. Call it in setup; every
69
+ * call yields a service over the same shared container.
70
+ */
71
+ export type UseBrowser<T> = () => Service<T>;
@@ -0,0 +1,27 @@
1
+ import type { NuxtError } from "#app";
2
+ import type { Notification } from "./notification";
3
+
4
+ /**
5
+ * The `data` an app error carries: when it was built and, optionally, the
6
+ * notification the shell's error boundary toasts in place of the message.
7
+ */
8
+ export type ErrorData = {
9
+ submitted: string;
10
+ notification?: Omit<Notification, "id">;
11
+ };
12
+
13
+ /**
14
+ * What `useAppError` takes beyond the code and the message: the notification
15
+ * to toast, the underlying cause, and whether the error is fatal (renders
16
+ * the error page rather than toasting).
17
+ */
18
+ export type ErrorOptions = {
19
+ notification?: Omit<Notification, "id">;
20
+ cause?: unknown;
21
+ fatal?: boolean;
22
+ };
23
+
24
+ /**
25
+ * A Nuxt error carrying {@link ErrorData}.
26
+ */
27
+ export type AppError = NuxtError<ErrorData>;
@@ -0,0 +1,34 @@
1
+ import type { Hooks } from "./hooks";
2
+ import type { Actions, Config, Events, Service, State } from "@zoobzio/form";
3
+
4
+ /**
5
+ * The slice of the state container a form may start from: the payload and
6
+ * the touched keys. The payload here is the first render's, distinct from
7
+ * `defaults`, which is what `reset` returns to. It seeds the `useState`
8
+ * record, so it must survive Nuxt's payload (devalue carries the `Set` and
9
+ * any `Date`).
10
+ */
11
+ export type FormInitial<T> = Partial<Pick<State<T>, "payload" | "touched">>;
12
+
13
+ /**
14
+ * What `createForm` takes: the headless config, the lifecycle actions and
15
+ * middleware, and the initial state. Config and actions share no key, so
16
+ * they sit side by side.
17
+ */
18
+ export type FormOptions<T> = Config<T> &
19
+ Actions<T> & {
20
+ initial?: FormInitial<T>;
21
+ };
22
+
23
+ /**
24
+ * The Nuxt hooks a form relays: `form:initialized`, `form:submitted`,
25
+ * `form:rejected`, `form:restored`, `form:reset`, and `form:failed`, each
26
+ * carrying the service's payload with its scoping `id`.
27
+ */
28
+ export type FormHooks = Hooks<"form", Events<unknown>>;
29
+
30
+ /**
31
+ * The id-scoped composable `createForm` returns. Call it in setup; every
32
+ * call yields a service over the same shared container.
33
+ */
34
+ export type UseForm<T> = () => Service<T>;
@@ -0,0 +1,49 @@
1
+ import type { RuntimeNuxtHooks } from "#app";
2
+
3
+ /**
4
+ * An event payload scoped to a service instance by its id. Every event the
5
+ * layer relays carries one.
6
+ */
7
+ export interface ScopedEvent {
8
+ id: string;
9
+ }
10
+
11
+ /**
12
+ * The hook names whose payload carries a scoping id: the hooks `useHooks`
13
+ * can filter by instance. The payload is checked whole, not member by
14
+ * member, so a hook with an optional payload stays out.
15
+ */
16
+ export type ScopedHookKey = {
17
+ [K in keyof RuntimeNuxtHooks]: RuntimeNuxtHooks[K] extends (
18
+ event: infer E,
19
+ ) => unknown
20
+ ? [E] extends [ScopedEvent]
21
+ ? K
22
+ : never
23
+ : never;
24
+ }[keyof RuntimeNuxtHooks];
25
+
26
+ /**
27
+ * A listener for one scoped hook, with the hook's own payload.
28
+ */
29
+ export type HookListener<K extends ScopedHookKey> = {
30
+ fn(event: Parameters<RuntimeNuxtHooks[K]>[0]): void;
31
+ }["fn"];
32
+
33
+ /**
34
+ * A listener as the runner sees it, over the erased payload. Method syntax
35
+ * on purpose: it is bivariant, so a listener typed on its hook's payload
36
+ * assigns in. Sound by construction, since the runner only calls it with
37
+ * events of the hook it was registered under.
38
+ */
39
+ export type ErasedHookListener = {
40
+ fn(event: ScopedEvent): void;
41
+ }["fn"];
42
+
43
+ /**
44
+ * A listener map: scoped hook name to callback. Each callback receives that
45
+ * hook's payload.
46
+ */
47
+ export type HookListeners = {
48
+ [K in ScopedHookKey]?: HookListener<K>;
49
+ };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * A service's domain events as Nuxt runtime hooks: each event name under
3
+ * the feature's prefix, each listener taking the event's arguments. The
4
+ * `change` event (every field write of the container) stays off the bus.
5
+ */
6
+ export type Hooks<P extends string, E extends Record<string, unknown[]>> = {
7
+ [K in keyof E as K extends "change" ? never : `${P}:${K & string}`]: (
8
+ ...args: E[K]
9
+ ) => void;
10
+ };
@@ -0,0 +1,38 @@
1
+ import type { LOG_LEVELS } from "../constants/log";
2
+ import type { Ref } from "vue";
3
+
4
+ /**
5
+ * A log level, by name.
6
+ */
7
+ export type LogLevel = keyof typeof LOG_LEVELS;
8
+
9
+ /**
10
+ * One kept line: the logger's name, the level, the message, its data, and
11
+ * when it was submitted as an ISO string.
12
+ */
13
+ export type LogLine = {
14
+ name: string;
15
+ message: string;
16
+ level: LogLevel;
17
+ data: Record<string, unknown>;
18
+ submitted: string;
19
+ };
20
+
21
+ /**
22
+ * One level's emit: a message and optional structured data.
23
+ */
24
+ export type LogEmit = (message: string, data?: Record<string, unknown>) => void;
25
+
26
+ /**
27
+ * A named logger over the shared level and backlog, with one emit per
28
+ * level.
29
+ */
30
+ export type Logger = {
31
+ level: Ref<LogLevel>;
32
+ backlog: Ref<LogLine[]>;
33
+ trace: LogEmit;
34
+ debug: LogEmit;
35
+ info: LogEmit;
36
+ warn: LogEmit;
37
+ error: LogEmit;
38
+ };