@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.
- package/LICENSE +21 -0
- package/README.md +231 -2
- package/app/app.d.ts +14 -0
- package/app/app.vue +70 -0
- package/app/assets/css/reset.css +146 -0
- package/app/composables/error.ts +34 -0
- package/app/composables/hook.ts +32 -0
- package/app/composables/log.ts +42 -0
- package/app/composables/notification.ts +24 -0
- package/app/composables/request.ts +88 -0
- package/app/constants/error.ts +9 -0
- package/app/constants/log.ts +16 -0
- package/app/error.vue +35 -0
- package/app/factories/browser.ts +55 -0
- package/app/factories/form.ts +54 -0
- package/app/factories/table.ts +50 -0
- package/app/plugins/log.ts +17 -0
- package/app/stores/browser.ts +23 -0
- package/app/stores/form.ts +22 -0
- package/app/stores/log.ts +14 -0
- package/app/stores/notification.ts +13 -0
- package/app/stores/table.ts +23 -0
- package/app/types/actions.ts +6 -0
- package/app/types/browser.ts +71 -0
- package/app/types/error.ts +27 -0
- package/app/types/form.ts +34 -0
- package/app/types/hook.ts +49 -0
- package/app/types/hooks.ts +10 -0
- package/app/types/log.ts +38 -0
- package/app/types/notification.ts +12 -0
- package/app/types/request.ts +18 -0
- package/app/types/table.ts +65 -0
- package/app/utils/actions.ts +21 -0
- package/app/utils/state.ts +19 -0
- package/modules/imports/constant.ts +146 -0
- package/modules/imports/index.ts +52 -0
- package/modules/imports/util.ts +2 -0
- package/nuxt.config.ts +23 -0
- package/package.json +61 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Alexander Thorwaldson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,232 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zoobzio/nuxt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Nuxt layer for the foundation design system. It carries the Nuxt-only
|
|
4
|
+
wiring over [`@zoobzio/vue`](../vue/) and the headless services in
|
|
5
|
+
`packages/`: `useState`-backed stores, hook-bus factories, and the app shell.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
Extend the layer from your app's `nuxt.config`:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
export default defineNuxtConfig({
|
|
13
|
+
extends: ["@zoobzio/nuxt"],
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Nothing is injected implicitly: no directory is scanned for components
|
|
18
|
+
or composables, and bare component tags do not resolve. The registries
|
|
19
|
+
behind Nuxt's two virtual modules stay live, filled only by
|
|
20
|
+
`modules/imports/`, so a consumer imports from them explicitly:
|
|
21
|
+
|
|
22
|
+
- `#components` carries the components of `@zoobzio/vue`.
|
|
23
|
+
- `#imports` carries the `define*` constructors of `@zoobzio/vue` and the
|
|
24
|
+
layer's own surface: the composables, factories, stores, and constants
|
|
25
|
+
as values, and everything under `types/` as types.
|
|
26
|
+
|
|
27
|
+
A consumer never depends on `@zoobzio/vue` itself. The package's
|
|
28
|
+
composables are not registered; they build the components and are not a
|
|
29
|
+
consumer surface. Nor are the layer's `utils/`, which are the factories'
|
|
30
|
+
plumbing.
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { Autocomplete, Select, Table } from "#components";
|
|
34
|
+
import { defineAutocomplete, defineSelect } from "#imports";
|
|
35
|
+
import { createTable, useHooks, useLogger } from "#imports";
|
|
36
|
+
import type { TableOptions } from "#imports";
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The same files also ship through subpath exports, the same way
|
|
40
|
+
`@zoobzio/vue` ships `src/`, for code outside the Nuxt build:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createTable } from "@zoobzio/nuxt/factories/table";
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Stores and factories
|
|
47
|
+
|
|
48
|
+
The data components of `@zoobzio/vue` (`Table`, `Form`, `Browser`) render a
|
|
49
|
+
headless service through their `service` prop and own no state of their
|
|
50
|
+
own. The layer carries that state for Nuxt, split in two tiers:
|
|
51
|
+
|
|
52
|
+
- **Stores** (`stores/`, `access<Name>`) back one service's state container
|
|
53
|
+
with a `useState` record keyed by id (`table-<id>`). The record's value is
|
|
54
|
+
a reactive proxy, so the service's writes are tracked; every instance
|
|
55
|
+
under the same id shares it, and it survives SSR hydration.
|
|
56
|
+
- **Factories** (`factories/`, `create<Name>`) take an id and the options
|
|
57
|
+
and return an id-scoped composable. Call the factory at module scope and
|
|
58
|
+
the composable in setup: each call builds a service over the id's store
|
|
59
|
+
and relays the service's domain events onto the Nuxt hook bus.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
// composables/users.ts
|
|
63
|
+
import { createTable } from "@zoobzio/nuxt/factories/table";
|
|
64
|
+
|
|
65
|
+
export const useUsers = createTable<User>("users", {
|
|
66
|
+
columns: [
|
|
67
|
+
{ key: "id", label: "ID", type: "number" },
|
|
68
|
+
{ key: "name", label: "Name", type: "text", sortable: true },
|
|
69
|
+
],
|
|
70
|
+
rowKey: "id",
|
|
71
|
+
fetch: (params) => $fetch("/api/users", { query: params }),
|
|
72
|
+
actions: {
|
|
73
|
+
edit: { label: "Edit", action: (user) => navigateTo(`/users/${user.id}`) },
|
|
74
|
+
},
|
|
75
|
+
initial: { pageSize: 50, sortField: "name" },
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```vue
|
|
80
|
+
<script setup lang="ts">
|
|
81
|
+
import { Table } from "#components";
|
|
82
|
+
import { useUsers } from "~/composables/users";
|
|
83
|
+
|
|
84
|
+
const users = useUsers();
|
|
85
|
+
await users.init();
|
|
86
|
+
</script>
|
|
87
|
+
|
|
88
|
+
<template>
|
|
89
|
+
<Table :service="users" />
|
|
90
|
+
</template>
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The options are the headless config with the behavior joined on. An action
|
|
94
|
+
is its descriptor and its handler under one key (`{ label, action }`); the
|
|
95
|
+
factory splits them back into the config and the wiring the service takes.
|
|
96
|
+
`initial` is the slice of the state container the consumer chooses (for a
|
|
97
|
+
table: paging, sort, selection, column order, committed query), never what
|
|
98
|
+
a fetch fills in. It seeds the `useState` record on first access, so it
|
|
99
|
+
must be something Nuxt's payload can carry: plain data, with `Set` and
|
|
100
|
+
`Date` allowed (devalue serializes them). A second id with the same
|
|
101
|
+
options is a second, independent state.
|
|
102
|
+
|
|
103
|
+
| Factory | Store | Hooks relayed |
|
|
104
|
+
| --------------- | --------------- | --------------------------------------------------------------------------------------------------- |
|
|
105
|
+
| `createTable` | `accessTable` | `table:updated`, `table:filtered` |
|
|
106
|
+
| `createForm` | `accessForm` | `form:initialized`, `form:submitted`, `form:rejected`, `form:restored`, `form:reset`, `form:failed` |
|
|
107
|
+
| `createBrowser` | `accessBrowser` | `browser:updated`, `browser:navigated` |
|
|
108
|
+
|
|
109
|
+
Every relayed payload carries the service's `id`. `useHooks` subscribes
|
|
110
|
+
for one instance: each listener fires for events with that id only, and
|
|
111
|
+
the subscriptions leave with the calling scope, so call it in setup.
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { useHooks } from "@zoobzio/nuxt/composables/hook";
|
|
115
|
+
|
|
116
|
+
useHooks("users", {
|
|
117
|
+
"table:updated": ({ total }) => console.log(total),
|
|
118
|
+
"table:filtered": ({ query }) => console.log(query),
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Each relay is also logged at `debug` under the service's id (see
|
|
123
|
+
[Logging](#logging)).
|
|
124
|
+
|
|
125
|
+
The hook names and payloads are declared on `RuntimeNuxtHooks` in
|
|
126
|
+
`app/app.d.ts`; the option, initial-state, and hook types live in
|
|
127
|
+
`types/<name>`.
|
|
128
|
+
|
|
129
|
+
## Logging
|
|
130
|
+
|
|
131
|
+
The layer keeps a log in state: the current level under `log:level`
|
|
132
|
+
(default `info`) and the kept lines under `log:backlog`, behind
|
|
133
|
+
`accessLogs`. `useLogger(name?)` is a named logger over them with one emit
|
|
134
|
+
per level (`trace`, `debug`, `info`, `warn`, `error`); a line is kept when
|
|
135
|
+
its level is at or above the current level and dropped otherwise. The level
|
|
136
|
+
and the backlog are the shared refs, so any logger may read or change them.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { useLogger } from "@zoobzio/nuxt/composables/log";
|
|
140
|
+
|
|
141
|
+
const log = useLogger("checkout");
|
|
142
|
+
log.level.value = "debug";
|
|
143
|
+
log.info("Order placed", { id: order.id });
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
`plugins/log` provides the root logger as `nuxt.$log` and the constructor
|
|
147
|
+
as `nuxt.$logger`, so code holding the app instance logs without setup
|
|
148
|
+
scope. The factories use it: every relayed event is logged at `debug` under
|
|
149
|
+
the service's id.
|
|
150
|
+
|
|
151
|
+
## Requests and errors
|
|
152
|
+
|
|
153
|
+
`useAppError(code, message, options?)` builds an app error: a Nuxt error with
|
|
154
|
+
the code's status text, stamped with when it was built, and carrying the
|
|
155
|
+
notification the shell toasts for it. `options` takes the `notification`,
|
|
156
|
+
the `cause`, and `fatal`; a fatal error renders the error page instead.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { useAppError } from "@zoobzio/nuxt/composables/error";
|
|
160
|
+
|
|
161
|
+
throw useAppError(404, "No such order", {
|
|
162
|
+
notification: { variant: "warning", title: "Order not found" },
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`useRequest` and `useLazyRequest` wrap `useAsyncData` and
|
|
167
|
+
`useLazyAsyncData`: the same arguments, with the request logged under its
|
|
168
|
+
key and a failure thrown as an app error rather than left on `error`. The
|
|
169
|
+
blocking one runs during SSR and fails fatal by default; the lazy one runs
|
|
170
|
+
on the client and fails non-fatal, so the shell toasts it. The `error`
|
|
171
|
+
option sets the status, message, and notification of the error thrown.
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { useLazyRequest } from "@zoobzio/nuxt/composables/request";
|
|
175
|
+
|
|
176
|
+
const { data } = useLazyRequest("orders", () => $fetch("/api/orders"), {
|
|
177
|
+
error: { status: 502, message: "Orders are unavailable" },
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## App shell
|
|
182
|
+
|
|
183
|
+
`app.vue` is the layer's root: reka's `ConfigProvider` (ids from Nuxt's
|
|
184
|
+
`useId`, so they match across hydration) and `TooltipProvider` around the
|
|
185
|
+
loading indicator, the page inside its layout, and the `Toaster`. An
|
|
186
|
+
`error.vue` renders the status code and a message, with a control that
|
|
187
|
+
clears the error home. An app that extends the layer gets both unless it
|
|
188
|
+
provides its own.
|
|
189
|
+
|
|
190
|
+
Notifications are the one piece of app state the layer owns: a `useState`
|
|
191
|
+
list behind `accessNotifications`, and `useNotifications` over it with
|
|
192
|
+
`push` (the id is assigned) and `remove`. The shell renders the list as
|
|
193
|
+
toasts and dismisses each on close.
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
import { useNotifications } from "@zoobzio/nuxt/composables/notification";
|
|
197
|
+
|
|
198
|
+
const { push } = useNotifications();
|
|
199
|
+
push({ variant: "success", title: "Saved" });
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
The layer loads one stylesheet, `app/assets/css/reset.css`: a
|
|
203
|
+
Preflight-style reset inside `@layer reset`, so any unlayered stylesheet
|
|
204
|
+
(the system's own semantic classes, the app's CSS) wins every
|
|
205
|
+
equal-specificity conflict. It uses no tokens, so it applies before and
|
|
206
|
+
independently of any theme.
|
|
207
|
+
|
|
208
|
+
The shell also wraps the page in a `NuxtErrorBoundary`. An error thrown
|
|
209
|
+
under it is toasted rather than replacing the page: when the error's `data`
|
|
210
|
+
carries a `notification` (`AppError` in `types/error`), that notification
|
|
211
|
+
is pushed as is; otherwise the error's message is pushed under the `error`
|
|
212
|
+
variant.
|
|
213
|
+
|
|
214
|
+
## Layout
|
|
215
|
+
|
|
216
|
+
```
|
|
217
|
+
app/
|
|
218
|
+
app.vue — the root: reka providers, loading indicator, error boundary, toaster
|
|
219
|
+
error.vue — the error page
|
|
220
|
+
app.d.ts — RuntimeNuxtHooks augmentation for the relayed events
|
|
221
|
+
assets/css/ — reset.css, loaded by the layer config
|
|
222
|
+
composables/ — useAppError, useHooks, useLogger, useNotifications, useRequest, useLazyRequest
|
|
223
|
+
constants/ — error page status codes, log levels
|
|
224
|
+
factories/ — create<Name>(id, options) → use<Name>(): Service
|
|
225
|
+
plugins/ — log: provides $log and $logger
|
|
226
|
+
stores/ — access<Name>(id, …) → the id's shared state container; accessLogs, accessNotifications
|
|
227
|
+
types/ — per-feature options, initial state, and hook maps; hook, log, notification, error, request
|
|
228
|
+
utils/ — split (actions → descriptors + handlers), seed (state)
|
|
229
|
+
modules/ — local Nuxt modules: imports/ fills #components and #imports (the @zoobzio/vue surface and the layer's own)
|
|
230
|
+
tests/ — vitest + happy-dom, mirroring app/ path-for-path
|
|
231
|
+
nuxt.config.ts — layer config (no implicit imports, no component scanning or tag resolution; loads the reset)
|
|
232
|
+
```
|
package/app/app.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { BrowserHooks } from "./types/browser";
|
|
2
|
+
import type { FormHooks } from "./types/form";
|
|
3
|
+
import type { Logger } from "./types/log";
|
|
4
|
+
import type { TableHooks } from "./types/table";
|
|
5
|
+
|
|
6
|
+
declare module "#app" {
|
|
7
|
+
interface NuxtApp {
|
|
8
|
+
$log: Logger;
|
|
9
|
+
$logger: (name?: string) => Logger;
|
|
10
|
+
}
|
|
11
|
+
interface RuntimeNuxtHooks extends BrowserHooks, FormHooks, TableHooks {}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export {};
|
package/app/app.vue
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ErrorData } from "./types/error";
|
|
3
|
+
|
|
4
|
+
import Toast from "@zoobzio/vue/components/toast.vue";
|
|
5
|
+
import Toaster from "@zoobzio/vue/components/toaster.vue";
|
|
6
|
+
import {
|
|
7
|
+
NuxtErrorBoundary,
|
|
8
|
+
NuxtLayout,
|
|
9
|
+
NuxtLoadingIndicator,
|
|
10
|
+
NuxtPage,
|
|
11
|
+
} from "#components";
|
|
12
|
+
|
|
13
|
+
import { ConfigProvider, TooltipProvider } from "reka-ui";
|
|
14
|
+
import { useHead, useId } from "#imports";
|
|
15
|
+
import { useNotifications } from "./composables/notification";
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<script setup lang="ts">
|
|
19
|
+
const useIdFunction = () => useId();
|
|
20
|
+
|
|
21
|
+
useHead({
|
|
22
|
+
htmlAttrs: {
|
|
23
|
+
lang: "en",
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const { notifications, push, remove } = useNotifications();
|
|
28
|
+
|
|
29
|
+
const onError = (err: unknown) => {
|
|
30
|
+
const data =
|
|
31
|
+
err instanceof Error && "data" in err
|
|
32
|
+
? (err.data as ErrorData | undefined)
|
|
33
|
+
: undefined;
|
|
34
|
+
|
|
35
|
+
if (data?.notification) {
|
|
36
|
+
push(data.notification);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
push({
|
|
41
|
+
title: err instanceof Error ? err.message : "An unexpected error occurred",
|
|
42
|
+
variant: "error",
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<template>
|
|
48
|
+
<ConfigProvider :use-id="useIdFunction">
|
|
49
|
+
<TooltipProvider>
|
|
50
|
+
<NuxtLoadingIndicator color="var(--sys-primary)" :threshold="0" />
|
|
51
|
+
<NuxtErrorBoundary @error="onError">
|
|
52
|
+
<NuxtLayout>
|
|
53
|
+
<NuxtPage />
|
|
54
|
+
</NuxtLayout>
|
|
55
|
+
</NuxtErrorBoundary>
|
|
56
|
+
<Toaster>
|
|
57
|
+
<template #toasts>
|
|
58
|
+
<Toast
|
|
59
|
+
v-for="notification in notifications"
|
|
60
|
+
:key="notification.id"
|
|
61
|
+
:title="notification.title"
|
|
62
|
+
:description="notification.description"
|
|
63
|
+
:variant="notification.variant"
|
|
64
|
+
@close="remove(notification.id)"
|
|
65
|
+
/>
|
|
66
|
+
</template>
|
|
67
|
+
</Toaster>
|
|
68
|
+
</TooltipProvider>
|
|
69
|
+
</ConfigProvider>
|
|
70
|
+
</template>
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tailwind-Preflight-style reset, scoped to `@layer reset` so any unlayered
|
|
3
|
+
* stylesheet (foundation's semantic classes, consuming-app CSS) wins every
|
|
4
|
+
* equal-specificity conflict. Token-free by design — the reset must apply
|
|
5
|
+
* before and independently of the untheme cascade.
|
|
6
|
+
*/
|
|
7
|
+
@layer reset {
|
|
8
|
+
*,
|
|
9
|
+
::before,
|
|
10
|
+
::after,
|
|
11
|
+
::backdrop {
|
|
12
|
+
box-sizing: border-box;
|
|
13
|
+
margin: 0;
|
|
14
|
+
padding: 0;
|
|
15
|
+
border: 0 solid;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
html {
|
|
19
|
+
line-height: 1.5;
|
|
20
|
+
-webkit-text-size-adjust: 100%;
|
|
21
|
+
tab-size: 4;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
hr {
|
|
25
|
+
height: 0;
|
|
26
|
+
color: inherit;
|
|
27
|
+
border-top-width: 1px;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
abbr[title] {
|
|
31
|
+
text-decoration: underline dotted;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
h1,
|
|
35
|
+
h2,
|
|
36
|
+
h3,
|
|
37
|
+
h4,
|
|
38
|
+
h5,
|
|
39
|
+
h6 {
|
|
40
|
+
font-size: inherit;
|
|
41
|
+
font-weight: inherit;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
a {
|
|
45
|
+
color: inherit;
|
|
46
|
+
text-decoration: inherit;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
b,
|
|
50
|
+
strong {
|
|
51
|
+
font-weight: bolder;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
code,
|
|
55
|
+
kbd,
|
|
56
|
+
samp,
|
|
57
|
+
pre {
|
|
58
|
+
font-family:
|
|
59
|
+
ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
|
|
60
|
+
monospace;
|
|
61
|
+
font-size: 1em;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
small {
|
|
65
|
+
font-size: 80%;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
sub,
|
|
69
|
+
sup {
|
|
70
|
+
font-size: 75%;
|
|
71
|
+
line-height: 0;
|
|
72
|
+
position: relative;
|
|
73
|
+
vertical-align: baseline;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
sub {
|
|
77
|
+
bottom: -0.25em;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
sup {
|
|
81
|
+
top: -0.5em;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
table {
|
|
85
|
+
text-indent: 0;
|
|
86
|
+
border-color: inherit;
|
|
87
|
+
border-collapse: collapse;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
button,
|
|
91
|
+
input,
|
|
92
|
+
optgroup,
|
|
93
|
+
select,
|
|
94
|
+
textarea {
|
|
95
|
+
font: inherit;
|
|
96
|
+
letter-spacing: inherit;
|
|
97
|
+
color: inherit;
|
|
98
|
+
background-color: transparent;
|
|
99
|
+
border-radius: 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
button,
|
|
103
|
+
input:where([type="button"], [type="reset"], [type="submit"]) {
|
|
104
|
+
appearance: button;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
::placeholder {
|
|
108
|
+
opacity: 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
summary {
|
|
112
|
+
display: list-item;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
ol,
|
|
116
|
+
ul,
|
|
117
|
+
menu {
|
|
118
|
+
list-style: none;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
img,
|
|
122
|
+
svg,
|
|
123
|
+
video,
|
|
124
|
+
canvas,
|
|
125
|
+
audio,
|
|
126
|
+
iframe,
|
|
127
|
+
embed,
|
|
128
|
+
object {
|
|
129
|
+
display: block;
|
|
130
|
+
vertical-align: middle;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
img,
|
|
134
|
+
video {
|
|
135
|
+
max-width: 100%;
|
|
136
|
+
height: auto;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
:disabled {
|
|
140
|
+
cursor: default;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
[hidden]:where(:not([hidden="until-found"])) {
|
|
144
|
+
display: none !important;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AppError, ErrorData, ErrorOptions } from "../types/error";
|
|
2
|
+
import type { HTTPCode } from "ltrl-http";
|
|
3
|
+
|
|
4
|
+
import { createError } from "#imports";
|
|
5
|
+
import { useHTTPCode } from "ltrl-http";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Builds an app error: a Nuxt error with the code's status text, stamped
|
|
9
|
+
* with when it was built and carrying the notification the shell toasts
|
|
10
|
+
* for it. Throw it, or pass it to `showError`.
|
|
11
|
+
*
|
|
12
|
+
* @param code - The HTTP status code.
|
|
13
|
+
* @param message - The error's message.
|
|
14
|
+
* @param options - The notification, the cause, and whether it is fatal.
|
|
15
|
+
*/
|
|
16
|
+
export const useAppError = (
|
|
17
|
+
code: HTTPCode,
|
|
18
|
+
message: string,
|
|
19
|
+
options: ErrorOptions = {},
|
|
20
|
+
): AppError => {
|
|
21
|
+
const status = useHTTPCode(code);
|
|
22
|
+
const { cause, fatal, notification } = options;
|
|
23
|
+
return createError<ErrorData>({
|
|
24
|
+
statusCode: code,
|
|
25
|
+
statusText: status.label,
|
|
26
|
+
message,
|
|
27
|
+
cause,
|
|
28
|
+
fatal,
|
|
29
|
+
data: {
|
|
30
|
+
submitted: new Date().toISOString(),
|
|
31
|
+
notification,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ErasedHookListener,
|
|
3
|
+
HookListeners,
|
|
4
|
+
ScopedEvent,
|
|
5
|
+
} from "../types/hook";
|
|
6
|
+
|
|
7
|
+
import { onScopeDispose, useNuxtApp } from "#imports";
|
|
8
|
+
import { entries } from "objectively";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Subscribes to the Nuxt hook bus for one service instance. Each listener
|
|
12
|
+
* fires only for events whose `id` matches, and every subscription is
|
|
13
|
+
* removed when the calling scope is disposed, so call this in setup.
|
|
14
|
+
*
|
|
15
|
+
* @param id - The service id the listeners are scoped to.
|
|
16
|
+
* @param listeners - Scoped hook name to callback.
|
|
17
|
+
*/
|
|
18
|
+
export const useHooks = (id: string, listeners: HookListeners): void => {
|
|
19
|
+
const nuxt = useNuxtApp();
|
|
20
|
+
|
|
21
|
+
const scoped: Record<string, (event: ScopedEvent) => void> = {};
|
|
22
|
+
for (const [key, listener] of entries(listeners)) {
|
|
23
|
+
if (!listener) continue;
|
|
24
|
+
const fn: ErasedHookListener = listener;
|
|
25
|
+
scoped[key] = (event) => {
|
|
26
|
+
if (event.id !== id) return;
|
|
27
|
+
fn(event);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
onScopeDispose(nuxt.hooks.addHooks(scoped));
|
|
32
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Logger, LogLevel } from "../types/log";
|
|
2
|
+
|
|
3
|
+
import { accessLogs } from "../stores/log";
|
|
4
|
+
import { LOG_LEVELS, LOG_ROOT_NAME } from "../constants/log";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A named logger over the shared store. Each emit keeps a line when its
|
|
8
|
+
* level is at or above the current level and drops it otherwise. The level
|
|
9
|
+
* and the backlog are the shared refs, so any logger may read or change
|
|
10
|
+
* them.
|
|
11
|
+
*
|
|
12
|
+
* @param name - The logger's name, kept on each line. Defaults to the root.
|
|
13
|
+
*/
|
|
14
|
+
export const useLogger = (name: string = LOG_ROOT_NAME): Logger => {
|
|
15
|
+
const { level, backlog } = accessLogs();
|
|
16
|
+
|
|
17
|
+
const make = (lvl: LogLevel) => {
|
|
18
|
+
return (message: string, data: Record<string, unknown> = {}) => {
|
|
19
|
+
if (LOG_LEVELS[lvl] < LOG_LEVELS[level.value]) return;
|
|
20
|
+
backlog.value = [
|
|
21
|
+
...backlog.value,
|
|
22
|
+
{
|
|
23
|
+
name,
|
|
24
|
+
level: lvl,
|
|
25
|
+
message,
|
|
26
|
+
data,
|
|
27
|
+
submitted: new Date().toISOString(),
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
level,
|
|
35
|
+
backlog,
|
|
36
|
+
trace: make("trace"),
|
|
37
|
+
debug: make("debug"),
|
|
38
|
+
info: make("info"),
|
|
39
|
+
warn: make("warn"),
|
|
40
|
+
error: make("error"),
|
|
41
|
+
};
|
|
42
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Notification } from "../types/notification";
|
|
2
|
+
|
|
3
|
+
import { accessNotifications } from "../stores/notification";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The app's notification queue: the shared list the shell renders as
|
|
7
|
+
* toasts, with `push` to add one (the id is assigned here) and `remove` to
|
|
8
|
+
* dismiss one by id.
|
|
9
|
+
*/
|
|
10
|
+
export const useNotifications = () => {
|
|
11
|
+
const { notifications } = accessNotifications();
|
|
12
|
+
|
|
13
|
+
const push = (notification: Omit<Notification, "id">): Notification => {
|
|
14
|
+
const entry = { ...notification, id: crypto.randomUUID() };
|
|
15
|
+
notifications.value = [...notifications.value, entry];
|
|
16
|
+
return entry;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const remove = (id: Notification["id"]) => {
|
|
20
|
+
notifications.value = notifications.value.filter((n) => n.id !== id);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return { notifications, push, remove };
|
|
24
|
+
};
|