@evobug_com/safe-ts-vue 0.3.0 → 0.4.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/README.md +1 -1
- package/dist/html.d.ts +1 -0
- package/dist/html.js +3 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/match.d.ts +17 -9
- package/dist/match.js +17 -8
- package/dist/task.d.ts +26 -7
- package/dist/task.js +19 -7
- package/dist/url.d.ts +1 -1
- package/dist/url.js +1 -1
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @evobug_com/safe-ts-vue
|
|
2
2
|
|
|
3
|
-
SAFE-TS helpers for Vue 3.5+: <Match> for enum values in templates, async tasks without races, template refs typed by where the element is, listeners and intervals tied to the component, typed props without casts, required injections, safe URLs for :href.
|
|
3
|
+
SAFE-TS helpers for Vue 3.5+: <Match> for enum values in templates, async tasks without races, template refs typed by where the element is, listeners and intervals tied to the component, typed props without casts, required injections, safe URLs for :href and typed safe HTML for v-html.
|
|
4
4
|
|
|
5
5
|
**Documentation: [safe-ts.evobug-pages.com/vue](https://safe-ts.evobug-pages.com/vue/)**
|
|
6
6
|
|
package/dist/html.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { defineSanitizer, escapeHtml, type Html } from "@evobug_com/safe-ts-std/html";
|
package/dist/html.js
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ export { Match, type MatchSlots } from "./match.js";
|
|
|
5
5
|
export { type DefaultedProp, oneOf, type OptionalProp, prop, type PropConstructorOf, type PropDefault, type PropTypeOf, type PropValidator, type RequiredProp, } from "./prop.js";
|
|
6
6
|
export { AsyncState } from "./state.js";
|
|
7
7
|
export { type Task, useTask } from "./task.js";
|
|
8
|
-
export {
|
|
8
|
+
export { defineSanitizer, escapeHtml, type Html } from "./html.js";
|
|
9
|
+
export { safeHref, type SafeUrl, safeUrl, type UnsafeUrl } from "./url.js";
|
package/dist/index.js
CHANGED
package/dist/match.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ type Tagged = {
|
|
|
3
3
|
readonly tag: string;
|
|
4
4
|
};
|
|
5
5
|
/**
|
|
6
|
-
* The slots of `<Match :
|
|
6
|
+
* The slots of `<Match :value="state">`: one per variant, given that variant (so `#Ready="{ value }"`
|
|
7
7
|
* reads its fields), and `_` for the variants without a slot of their own.
|
|
8
8
|
*/
|
|
9
9
|
export type MatchSlots<E extends Tagged> = {
|
|
@@ -17,7 +17,7 @@ export type MatchSlots<E extends Tagged> = {
|
|
|
17
17
|
* Show an enum value variant by variant, like `match(value, { ... })` in a template:
|
|
18
18
|
*
|
|
19
19
|
* ```vue
|
|
20
|
-
* <Match :
|
|
20
|
+
* <Match :value="user">
|
|
21
21
|
* <template #Loading>…</template>
|
|
22
22
|
* <template #Ready="{ value }">{{ value.name }}</template>
|
|
23
23
|
* <template #Failed="{ error }">{{ error.message }}</template>
|
|
@@ -28,23 +28,29 @@ export type MatchSlots<E extends Tagged> = {
|
|
|
28
28
|
* safe-ts checks that every variant has a slot (or that `#_` is there), and that each slot names
|
|
29
29
|
* a variant. With the build transform (`safeTs()` before `vue()` in Vite), `<Match>` is compiled
|
|
30
30
|
* into a `v-if` chain on `value.tag` and this component isn't used at all; it renders what the
|
|
31
|
-
* transform can't rewrite (`:
|
|
32
|
-
* run without it.
|
|
31
|
+
* transform can't rewrite (`:value="find(id)"`, slot props with defaults), and tests or tools
|
|
32
|
+
* that run without it.
|
|
33
33
|
*
|
|
34
34
|
* The value can't be missing: unlike `match(...)`, there is no `#undefined` slot. Put `<Match>`
|
|
35
35
|
* under `v-if="value"`, with `v-else` for the missing case.
|
|
36
36
|
*
|
|
37
37
|
* Pass the value, not a Ref: the template unwraps refs only at the top level, so a task's state
|
|
38
|
-
* is `:
|
|
38
|
+
* is `:value="task.state.value"` (or destructure it: `const { state } = useTask(...)`).
|
|
39
|
+
*
|
|
40
|
+
* `:on` is the older name of the prop, and still works the same at runtime and in the build
|
|
41
|
+
* transform. vue-tsc refuses it (TS2345): Vue's props type reads a prop named `on` as an event
|
|
42
|
+
* listener (`on${Capitalize<string>}`), so it must be a function. Write `:value`.
|
|
39
43
|
*/
|
|
40
44
|
export declare const Match: new <E extends Tagged>(props: ({
|
|
41
|
-
readonly
|
|
45
|
+
readonly value?: E; /** @deprecated Write `:value`: vue-tsc refuses `:on`. */
|
|
46
|
+
readonly on?: E;
|
|
42
47
|
} & ({
|
|
43
48
|
[x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
|
|
44
49
|
} | {
|
|
45
50
|
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
|
46
51
|
})) & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps) => import("vue").CreateComponentPublicInstanceWithMixins<{
|
|
47
|
-
readonly
|
|
52
|
+
readonly value?: E; /** @deprecated Write `:value`: vue-tsc refuses `:on`. */
|
|
53
|
+
readonly on?: E;
|
|
48
54
|
} & ({
|
|
49
55
|
[x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
|
|
50
56
|
} | {
|
|
@@ -57,11 +63,13 @@ export declare const Match: new <E extends Tagged>(props: ({
|
|
|
57
63
|
M: {};
|
|
58
64
|
Defaults: {};
|
|
59
65
|
}, ({
|
|
60
|
-
readonly
|
|
66
|
+
readonly value?: E; /** @deprecated Write `:value`: vue-tsc refuses `:on`. */
|
|
67
|
+
readonly on?: E;
|
|
61
68
|
} & {
|
|
62
69
|
[x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
|
|
63
70
|
}) | ({
|
|
64
|
-
readonly
|
|
71
|
+
readonly value?: E; /** @deprecated Write `:value`: vue-tsc refuses `:on`. */
|
|
72
|
+
readonly on?: E;
|
|
65
73
|
} & {
|
|
66
74
|
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
|
67
75
|
}), {}, {}, {}, {}, {}>;
|
package/dist/match.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// @safe-ts std: picking the slot by the value's tag needs one cast (see `Match`).
|
|
2
|
-
// `<Match :
|
|
2
|
+
// `<Match :value="state">`: an enum value in a template, one slot per variant.
|
|
3
3
|
import { defineComponent } from "vue";
|
|
4
4
|
/**
|
|
5
5
|
* Show an enum value variant by variant, like `match(value, { ... })` in a template:
|
|
6
6
|
*
|
|
7
7
|
* ```vue
|
|
8
|
-
* <Match :
|
|
8
|
+
* <Match :value="user">
|
|
9
9
|
* <template #Loading>…</template>
|
|
10
10
|
* <template #Ready="{ value }">{{ value.name }}</template>
|
|
11
11
|
* <template #Failed="{ error }">{{ error.message }}</template>
|
|
@@ -16,18 +16,27 @@ import { defineComponent } from "vue";
|
|
|
16
16
|
* safe-ts checks that every variant has a slot (or that `#_` is there), and that each slot names
|
|
17
17
|
* a variant. With the build transform (`safeTs()` before `vue()` in Vite), `<Match>` is compiled
|
|
18
18
|
* into a `v-if` chain on `value.tag` and this component isn't used at all; it renders what the
|
|
19
|
-
* transform can't rewrite (`:
|
|
20
|
-
* run without it.
|
|
19
|
+
* transform can't rewrite (`:value="find(id)"`, slot props with defaults), and tests or tools
|
|
20
|
+
* that run without it.
|
|
21
21
|
*
|
|
22
22
|
* The value can't be missing: unlike `match(...)`, there is no `#undefined` slot. Put `<Match>`
|
|
23
23
|
* under `v-if="value"`, with `v-else` for the missing case.
|
|
24
24
|
*
|
|
25
25
|
* Pass the value, not a Ref: the template unwraps refs only at the top level, so a task's state
|
|
26
|
-
* is `:
|
|
26
|
+
* is `:value="task.state.value"` (or destructure it: `const { state } = useTask(...)`).
|
|
27
|
+
*
|
|
28
|
+
* `:on` is the older name of the prop, and still works the same at runtime and in the build
|
|
29
|
+
* transform. vue-tsc refuses it (TS2345): Vue's props type reads a prop named `on` as an event
|
|
30
|
+
* listener (`on${Capitalize<string>}`), so it must be a function. Write `:value`.
|
|
27
31
|
*/
|
|
28
|
-
export const Match = defineComponent((
|
|
32
|
+
export const Match = defineComponent((
|
|
33
|
+
// Both optional, so that either name type checks: safe-ts reports a `<Match>` with neither.
|
|
34
|
+
props, { slots }) => () => {
|
|
35
|
+
const subject = props.value ?? props.on;
|
|
36
|
+
if (subject === undefined)
|
|
37
|
+
return undefined;
|
|
29
38
|
// A slot per tag: TypeScript can't tie `slots[tag]` to the variant with that tag, which
|
|
30
39
|
// is what `MatchSlots` says it is.
|
|
31
40
|
const byTag = slots;
|
|
32
|
-
return (byTag[
|
|
33
|
-
}, { name: "Match", props: ["on"] });
|
|
41
|
+
return (byTag[subject.tag] ?? slots._)?.(subject);
|
|
42
|
+
}, { name: "Match", props: ["value", "on"] });
|
package/dist/task.d.ts
CHANGED
|
@@ -1,17 +1,33 @@
|
|
|
1
|
-
import { type Result } from "@evobug_com/safe-ts-std/result";
|
|
1
|
+
import { type Option, Panic, type Result } from "@evobug_com/safe-ts-std/result";
|
|
2
2
|
import { type ShallowRef } from "vue";
|
|
3
3
|
import { AsyncState } from "./state.js";
|
|
4
4
|
/** What `useTask` returns. */
|
|
5
5
|
export type Task<A extends readonly unknown[], T, E> = {
|
|
6
6
|
/** Where the latest run stands. Read-only: only the task changes it. */
|
|
7
7
|
readonly state: Readonly<ShallowRef<AsyncState<T, E>>>;
|
|
8
|
+
/**
|
|
9
|
+
* What the latest run threw or rejected with, as a `Panic`, while `state` is back on `Idle`;
|
|
10
|
+
* `undefined` otherwise. A bug, not a failure (see `useTask`): show it in the `Idle` branch
|
|
11
|
+
* ("Something went wrong") so a crash doesn't leave a blank screen. The next `run(...)` or
|
|
12
|
+
* `reset()` clears it.
|
|
13
|
+
*/
|
|
14
|
+
readonly panic: Readonly<ShallowRef<Option<Panic>>>;
|
|
8
15
|
/**
|
|
9
16
|
* Start a run, aborting the one in progress. Resolves when this run is over (or was
|
|
10
17
|
* superseded). Rejects only when `run` itself rejected or threw: a bug, see `useTask`.
|
|
11
18
|
*/
|
|
12
19
|
readonly run: (...args: A) => Promise<void>;
|
|
13
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Abort the run in progress, if any; `state` goes back to `Idle`. Does nothing when no run
|
|
22
|
+
* is in progress: a finished run's `Ready` or `Failed` stays. To forget it, `reset()`.
|
|
23
|
+
*/
|
|
14
24
|
readonly abort: () => void;
|
|
25
|
+
/**
|
|
26
|
+
* Back to `Idle` from wherever the task is: aborts the run in progress, if any, and forgets
|
|
27
|
+
* the last outcome (`Ready`, `Failed`, a `panic`). For a "dismiss" or before showing
|
|
28
|
+
* something else, where the old answer must not stay on screen.
|
|
29
|
+
*/
|
|
30
|
+
readonly reset: () => void;
|
|
15
31
|
};
|
|
16
32
|
/**
|
|
17
33
|
* Run async work and keep its outcome in a reactive `state`, with at most one run at a time:
|
|
@@ -19,7 +35,7 @@ export type Task<A extends readonly unknown[], T, E> = {
|
|
|
19
35
|
* ```ts
|
|
20
36
|
* const user = useTask((signal, id: string) => fetchUser(id, signal));
|
|
21
37
|
* watch(() => props.id, (id) => user.run(id), { immediate: true });
|
|
22
|
-
* // template: <p v-if="user.state.value.
|
|
38
|
+
* // template: <p v-if="is(user.state.value, AsyncState.Ready)">{{ user.state.value.value.name }}</p>
|
|
23
39
|
* ```
|
|
24
40
|
*
|
|
25
41
|
* - Every `run(...)` aborts the previous run's `signal` and starts a new one. Only the latest
|
|
@@ -33,9 +49,12 @@ export type Task<A extends readonly unknown[], T, E> = {
|
|
|
33
49
|
* When `run` rejects (or throws) instead of returning an `Err`, that is a bug: the failures
|
|
34
50
|
* you expect belong in the `Result`. It is not turned into `Failed`, because `Failed` holds an
|
|
35
51
|
* `E` and the rejection reason is `unknown`, and it is not swallowed either: `state` goes back
|
|
36
|
-
* to `Idle` (no endless spinner) and the promise returned by
|
|
37
|
-
*
|
|
38
|
-
* or a `watch` callback to `app.config.errorHandler`, so
|
|
39
|
-
*
|
|
52
|
+
* to `Idle` (no endless spinner), `panic` holds the reason, and the promise returned by
|
|
53
|
+
* `task.run(...)` rejects with it. Vue reports a promise rejected from a template event
|
|
54
|
+
* handler, a lifecycle hook or a `watch` callback to `app.config.errorHandler`, so
|
|
55
|
+
* `@click="save.run()"` surfaces it. Give the `Idle` branch of the template something to show
|
|
56
|
+
* (`<template #Idle>` or `#_` in `<Match>`), and read `panic` there: a renderer with nothing for
|
|
57
|
+
* `Idle` draws a blank where the crash was. A rejection after the run was aborted (`fetch`'s
|
|
58
|
+
* `AbortError`) is expected and dropped.
|
|
40
59
|
*/
|
|
41
60
|
export declare function useTask<T, E, A extends readonly unknown[] = []>(run: (signal: AbortSignal, ...args: A) => Promise<Result<T, E>>): Task<A, T, E>;
|
package/dist/task.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Async work for a component, without the "async race": a slow earlier request can't
|
|
2
2
|
// overwrite the result of a later one.
|
|
3
|
-
import { isErr } from "@evobug_com/safe-ts-std/result";
|
|
3
|
+
import { isErr, Panic } from "@evobug_com/safe-ts-std/result";
|
|
4
4
|
import { getCurrentScope, onScopeDispose, shallowReadonly, shallowRef } from "vue";
|
|
5
5
|
import { AsyncState } from "./state.js";
|
|
6
6
|
/**
|
|
@@ -9,7 +9,7 @@ import { AsyncState } from "./state.js";
|
|
|
9
9
|
* ```ts
|
|
10
10
|
* const user = useTask((signal, id: string) => fetchUser(id, signal));
|
|
11
11
|
* watch(() => props.id, (id) => user.run(id), { immediate: true });
|
|
12
|
-
* // template: <p v-if="user.state.value.
|
|
12
|
+
* // template: <p v-if="is(user.state.value, AsyncState.Ready)">{{ user.state.value.value.name }}</p>
|
|
13
13
|
* ```
|
|
14
14
|
*
|
|
15
15
|
* - Every `run(...)` aborts the previous run's `signal` and starts a new one. Only the latest
|
|
@@ -23,13 +23,17 @@ import { AsyncState } from "./state.js";
|
|
|
23
23
|
* When `run` rejects (or throws) instead of returning an `Err`, that is a bug: the failures
|
|
24
24
|
* you expect belong in the `Result`. It is not turned into `Failed`, because `Failed` holds an
|
|
25
25
|
* `E` and the rejection reason is `unknown`, and it is not swallowed either: `state` goes back
|
|
26
|
-
* to `Idle` (no endless spinner) and the promise returned by
|
|
27
|
-
*
|
|
28
|
-
* or a `watch` callback to `app.config.errorHandler`, so
|
|
29
|
-
*
|
|
26
|
+
* to `Idle` (no endless spinner), `panic` holds the reason, and the promise returned by
|
|
27
|
+
* `task.run(...)` rejects with it. Vue reports a promise rejected from a template event
|
|
28
|
+
* handler, a lifecycle hook or a `watch` callback to `app.config.errorHandler`, so
|
|
29
|
+
* `@click="save.run()"` surfaces it. Give the `Idle` branch of the template something to show
|
|
30
|
+
* (`<template #Idle>` or `#_` in `<Match>`), and read `panic` there: a renderer with nothing for
|
|
31
|
+
* `Idle` draws a blank where the crash was. A rejection after the run was aborted (`fetch`'s
|
|
32
|
+
* `AbortError`) is expected and dropped.
|
|
30
33
|
*/
|
|
31
34
|
export function useTask(run) {
|
|
32
35
|
const state = shallowRef(AsyncState.Idle());
|
|
36
|
+
const panic = shallowRef(undefined);
|
|
33
37
|
// The run in progress: the only one allowed to write `state`.
|
|
34
38
|
let current = undefined;
|
|
35
39
|
let disposed = false;
|
|
@@ -40,12 +44,19 @@ export function useTask(run) {
|
|
|
40
44
|
current = undefined;
|
|
41
45
|
state.value = AsyncState.Idle();
|
|
42
46
|
};
|
|
47
|
+
const reset = () => {
|
|
48
|
+
current?.abort();
|
|
49
|
+
current = undefined;
|
|
50
|
+
panic.value = undefined;
|
|
51
|
+
state.value = AsyncState.Idle();
|
|
52
|
+
};
|
|
43
53
|
const start = async (...args) => {
|
|
44
54
|
if (disposed)
|
|
45
55
|
return;
|
|
46
56
|
current?.abort();
|
|
47
57
|
const controller = new AbortController();
|
|
48
58
|
current = controller;
|
|
59
|
+
panic.value = undefined;
|
|
49
60
|
state.value = AsyncState.Loading();
|
|
50
61
|
let result;
|
|
51
62
|
try {
|
|
@@ -56,6 +67,7 @@ export function useTask(run) {
|
|
|
56
67
|
if (controller.signal.aborted)
|
|
57
68
|
return;
|
|
58
69
|
current = undefined;
|
|
70
|
+
panic.value = new Panic(reason);
|
|
59
71
|
state.value = AsyncState.Idle();
|
|
60
72
|
return Promise.reject(reason);
|
|
61
73
|
}
|
|
@@ -70,5 +82,5 @@ export function useTask(run) {
|
|
|
70
82
|
abort();
|
|
71
83
|
});
|
|
72
84
|
}
|
|
73
|
-
return { state: shallowReadonly(state), run: start, abort };
|
|
85
|
+
return { state: shallowReadonly(state), panic: shallowReadonly(panic), run: start, abort, reset };
|
|
74
86
|
}
|
package/dist/url.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { safeHref, safeUrl, type UnsafeUrl } from "@evobug_com/safe-ts-std/url";
|
|
1
|
+
export { safeHref, type SafeHrefOptions, type SafeUrl, safeUrl, type SafeUrlOptions, type UnsafeUrl, } from "@evobug_com/safe-ts-std/url";
|
package/dist/url.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// URLs that are safe to bind to `:href`, `:src` (iframe), `:action`, `:formaction`.
|
|
2
2
|
// Defined in @evobug_com/safe-ts-std/url, which doesn't depend on Vue.
|
|
3
|
-
export { safeHref, safeUrl } from "@evobug_com/safe-ts-std/url";
|
|
3
|
+
export { safeHref, safeUrl, } from "@evobug_com/safe-ts-std/url";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evobug_com/safe-ts-vue",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "SAFE-TS helpers for Vue 3.5+: <Match> for enum values in templates, async tasks without races, template refs typed by where the element is, listeners and intervals tied to the component, typed props without casts, required injections, safe URLs for :href.",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "SAFE-TS helpers for Vue 3.5+: <Match> for enum values in templates, async tasks without races, template refs typed by where the element is, listeners and intervals tied to the component, typed props without casts, required injections, safe URLs for :href and typed safe HTML for v-html.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"safe-ts",
|
|
7
7
|
"typescript",
|
|
@@ -60,6 +60,10 @@
|
|
|
60
60
|
"types": "./dist/url.d.ts",
|
|
61
61
|
"default": "./dist/url.js"
|
|
62
62
|
},
|
|
63
|
+
"./html": {
|
|
64
|
+
"types": "./dist/html.d.ts",
|
|
65
|
+
"default": "./dist/html.js"
|
|
66
|
+
},
|
|
63
67
|
"./jsx-runtime": {
|
|
64
68
|
"types": "./dist/jsx-runtime.d.ts",
|
|
65
69
|
"default": "./dist/jsx-runtime.js"
|
|
@@ -75,7 +79,7 @@
|
|
|
75
79
|
"LICENSE-APACHE"
|
|
76
80
|
],
|
|
77
81
|
"peerDependencies": {
|
|
78
|
-
"@evobug_com/safe-ts-std": "^0.
|
|
82
|
+
"@evobug_com/safe-ts-std": "^0.4.0",
|
|
79
83
|
"vue": "^3.5.0"
|
|
80
84
|
},
|
|
81
85
|
"publishConfig": {
|