@evobug_com/safe-ts-skill 0.0.0-stage → 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/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +15 -2
- package/install.mjs +62 -0
- package/package.json +35 -4
- package/safe-ts-migrate/SKILL.md +111 -0
- package/safe-ts-migrate/references/architecture.md +139 -0
- package/safe-ts-migrate/references/behaviour.md +86 -0
- package/safe-ts-migrate/references/census.md +114 -0
- package/safe-ts-migrate/references/escape-hatches.md +62 -0
- package/safe-ts-migrate/references/final-pass.md +44 -0
- package/safe-ts-migrate/references/order-and-gates.md +96 -0
- package/safe-ts-migrate/references/patterns-core.md +1074 -0
- package/safe-ts-migrate/references/patterns-react.md +230 -0
- package/safe-ts-migrate/references/patterns-vue.md +636 -0
- package/safe-ts-migrate/references/review-checklist.md +63 -0
- package/safe-ts-migrate/references/templates.md +235 -0
- package/safe-ts-migrate/references/tool-gaps.md +124 -0
- package/safe-ts-migrate/scripts/census.mjs +671 -0
- package/safe-ts-migrate/scripts/safe-ts.mjs +79 -0
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
# Pattern catalogue: Vue
|
|
2
|
+
|
|
3
|
+
Vue components and composables, before and after. The blocks follow the convention of
|
|
4
|
+
[patterns-core.md](patterns-core.md): ` ```vue bad <rules> ` is the code before with the rules
|
|
5
|
+
`safe-ts check` reports (none listed: the checker is silent), ` ```vue good ` / ` ```ts good `
|
|
6
|
+
the target. Examples import `fetchUser(id, signal)` and friends from `./api`: request functions
|
|
7
|
+
that return `Promise<Result<T, ApiError>>`, as in [patterns-core.md](patterns-core.md#requests-return-a-result).
|
|
8
|
+
Blocks marked `no-tsc` import `pinia`, which the SAFE-TS repository doesn't install: the checker
|
|
9
|
+
reads them, tsc doesn't.
|
|
10
|
+
|
|
11
|
+
Vue guide pages: Async state, State and props, Values that may be missing, Errors in handlers,
|
|
12
|
+
Cleanup, Security, Setup.
|
|
13
|
+
|
|
14
|
+
## Contents
|
|
15
|
+
|
|
16
|
+
- [A loading, error, data trio](#a-loading-error-data-trio)
|
|
17
|
+
- [A ref filled after mount](#a-ref-filled-after-mount)
|
|
18
|
+
- [A refresh that blanks the list](#a-refresh-that-blanks-the-list)
|
|
19
|
+
- [A submit with a busy flag and an error message](#a-submit-with-a-busy-flag-and-an-error-message)
|
|
20
|
+
- [Showing an enum in a template](#showing-an-enum-in-a-template)
|
|
21
|
+
- [A template ref](#a-template-ref)
|
|
22
|
+
- [Module state](#module-state)
|
|
23
|
+
- [A store action that throws](#a-store-action-that-throws)
|
|
24
|
+
- [A prop changed by the child](#a-prop-changed-by-the-child)
|
|
25
|
+
- [v-html and bound URLs](#v-html-and-bound-urls)
|
|
26
|
+
- [A Result returned to Vue](#a-result-returned-to-vue)
|
|
27
|
+
- [A listener on window](#a-listener-on-window)
|
|
28
|
+
- [Typed props in defineComponent](#typed-props-in-definecomponent)
|
|
29
|
+
|
|
30
|
+
## A loading, error, data trio
|
|
31
|
+
|
|
32
|
+
`loading`, `error` and `data` refs can disagree, and an older response can overwrite a newer one.
|
|
33
|
+
`useTask` keeps one `AsyncState` (`Idle`, `Loading`, `Ready` with `value`, `Failed` with
|
|
34
|
+
`error`), aborts the previous run when a new one starts, and aborts when the component
|
|
35
|
+
unmounts. In a component, `vue-split-state` reports the refs written around an `await` (a lone
|
|
36
|
+
`loading` flag too, and an `error = ref('')` set after one), and `vue-async-race` the missing
|
|
37
|
+
latest-wins check. The census counts the flags and error refs everywhere, stores and
|
|
38
|
+
composables included (`loading-flag`, `error-state`, `trio-files`).
|
|
39
|
+
|
|
40
|
+
```vue bad vue-async-race vue-split-state
|
|
41
|
+
<script setup lang="ts">
|
|
42
|
+
import { ref, watch } from "vue";
|
|
43
|
+
import { isErr } from "@evobug_com/safe-ts-std/result";
|
|
44
|
+
import { fetchUser, type User } from "./api";
|
|
45
|
+
|
|
46
|
+
const props = defineProps<{ id: string }>();
|
|
47
|
+
const loading = ref(false);
|
|
48
|
+
const failed = ref("");
|
|
49
|
+
const user = ref<User>();
|
|
50
|
+
|
|
51
|
+
watch(() => props.id, async (id) => {
|
|
52
|
+
loading.value = true;
|
|
53
|
+
const result = await fetchUser(id);
|
|
54
|
+
loading.value = false;
|
|
55
|
+
if (isErr(result)) failed.value = result.error.message;
|
|
56
|
+
else user.value = result;
|
|
57
|
+
}, { immediate: true });
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<template>
|
|
61
|
+
<p v-if="loading">Loading…</p>
|
|
62
|
+
<p v-else-if="failed !== ''">{{ failed }}</p>
|
|
63
|
+
<p v-else-if="user">{{ user.name }}</p>
|
|
64
|
+
</template>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```vue good
|
|
68
|
+
<script setup lang="ts">
|
|
69
|
+
import { watch } from "vue";
|
|
70
|
+
import { Match } from "@evobug_com/safe-ts-vue/match";
|
|
71
|
+
import { useTask } from "@evobug_com/safe-ts-vue/task";
|
|
72
|
+
import { fetchUser } from "./api";
|
|
73
|
+
|
|
74
|
+
const props = defineProps<{ id: string }>();
|
|
75
|
+
const { state, run } = useTask((signal, id: string) => fetchUser(id, signal));
|
|
76
|
+
|
|
77
|
+
watch(() => props.id, (id) => run(id), { immediate: true });
|
|
78
|
+
</script>
|
|
79
|
+
|
|
80
|
+
<template>
|
|
81
|
+
<Match :value="state">
|
|
82
|
+
<template #Loading>Loading…</template>
|
|
83
|
+
<template #Ready="{ value }">{{ value.name }}</template>
|
|
84
|
+
<template #Failed="{ error }">Couldn't load the user: {{ error.message }}</template>
|
|
85
|
+
<template #_></template>
|
|
86
|
+
</Match>
|
|
87
|
+
</template>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- `run` resolves when the run is over; it rejects only when the work threw instead of returning
|
|
91
|
+
an `Err` (a bug). Returned from a `watch` callback, a hook or a template handler, Vue reports
|
|
92
|
+
that rejection to `app.config.errorHandler`.
|
|
93
|
+
- The work's `Err` becomes `Failed`. A throw does not: the state goes back to `Idle`, and the
|
|
94
|
+
task's `panic` holds what was thrown (a std `Panic`) until the next run or `reset()`. Give
|
|
95
|
+
`Idle` a slot (or `#_`) that reads `panic` and says something went wrong, so a bug doesn't
|
|
96
|
+
leave a blank area.
|
|
97
|
+
- `abort()` stops a run in progress; `reset()` goes back to `Idle` from `Ready`, `Failed` or a
|
|
98
|
+
run in progress, and clears `panic`. Use `reset()` to dismiss an answer or an error.
|
|
99
|
+
- The work takes the task's `signal` and passes it to the request; with the idiomatic layer on,
|
|
100
|
+
`unused-task-signal` reports a task that never reads it.
|
|
101
|
+
- Write `<Match :value="...">`. The older `:on` still works at runtime but fails vue-tsc
|
|
102
|
+
(TS2345: Vue reads a prop named `on…` as an event listener).
|
|
103
|
+
|
|
104
|
+
## A ref filled after mount
|
|
105
|
+
|
|
106
|
+
`ref<T>()` loaded in `onMounted` starts as `undefined`, so the template has to cope with it, and a
|
|
107
|
+
failure has nowhere to go (`vue-ref-without-value`).
|
|
108
|
+
|
|
109
|
+
```vue bad vue-ref-without-value
|
|
110
|
+
<script setup lang="ts">
|
|
111
|
+
import { onMounted, ref } from "vue";
|
|
112
|
+
import { isErr } from "@evobug_com/safe-ts-std/result";
|
|
113
|
+
import { fetchOrders, type Order } from "./api";
|
|
114
|
+
|
|
115
|
+
const orders = ref<Order[]>();
|
|
116
|
+
|
|
117
|
+
onMounted(async () => {
|
|
118
|
+
const result = await fetchOrders();
|
|
119
|
+
if (!isErr(result)) orders.value = result;
|
|
120
|
+
});
|
|
121
|
+
</script>
|
|
122
|
+
|
|
123
|
+
<template>
|
|
124
|
+
<p v-if="orders">{{ orders.length }} orders</p>
|
|
125
|
+
</template>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
```vue good
|
|
129
|
+
<script setup lang="ts">
|
|
130
|
+
import { onMounted } from "vue";
|
|
131
|
+
import { Match } from "@evobug_com/safe-ts-vue/match";
|
|
132
|
+
import { useTask } from "@evobug_com/safe-ts-vue/task";
|
|
133
|
+
import { fetchOrders } from "./api";
|
|
134
|
+
|
|
135
|
+
const { state, run } = useTask((signal) => fetchOrders(signal));
|
|
136
|
+
|
|
137
|
+
onMounted(() => run());
|
|
138
|
+
</script>
|
|
139
|
+
|
|
140
|
+
<template>
|
|
141
|
+
<Match :value="state">
|
|
142
|
+
<template #Ready="{ value }">{{ value.length }} orders</template>
|
|
143
|
+
<template #Failed="{ error }">Couldn't load the orders: {{ error.message }}</template>
|
|
144
|
+
<template #_>Loading…</template>
|
|
145
|
+
</Match>
|
|
146
|
+
</template>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Start a load in `onMounted` when the work reads template refs (`useElement`): setup runs before
|
|
150
|
+
the elements exist. That includes a `load` function passed to a child component that starts
|
|
151
|
+
it from its own setup. `vue-template-ref` reports a function that reads a `useElement` before
|
|
152
|
+
its first `await` and is called during setup, run by an immediate watcher (also through
|
|
153
|
+
`useTask`'s `run`), or passed as a prop.
|
|
154
|
+
|
|
155
|
+
## A refresh that blanks the list
|
|
156
|
+
|
|
157
|
+
`run()` sets `Loading`, which carries no value: a list shown only in `#Ready` disappears while it
|
|
158
|
+
reloads, and a page that refreshes after each edit flickers or loses its scroll position. Where
|
|
159
|
+
the old list must stay on screen, keep the last answer next to the task.
|
|
160
|
+
|
|
161
|
+
```vue good
|
|
162
|
+
<script setup lang="ts">
|
|
163
|
+
import { onMounted, shallowRef, watch } from "vue";
|
|
164
|
+
import { is, variant } from "@evobug_com/safe-ts-std/enum";
|
|
165
|
+
import { AsyncState } from "@evobug_com/safe-ts-vue/state";
|
|
166
|
+
import { useTask } from "@evobug_com/safe-ts-vue/task";
|
|
167
|
+
import { fetchOrders, type Order } from "./api";
|
|
168
|
+
|
|
169
|
+
const { state, run } = useTask((signal) => fetchOrders(signal));
|
|
170
|
+
// The last list loaded, kept on screen while a refresh runs.
|
|
171
|
+
const shown = shallowRef<readonly Order[]>([]);
|
|
172
|
+
watch(state, (now) => {
|
|
173
|
+
const ready = variant(now, AsyncState.Ready);
|
|
174
|
+
if (ready !== undefined) shown.value = ready.value;
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
onMounted(() => run());
|
|
178
|
+
</script>
|
|
179
|
+
|
|
180
|
+
<template>
|
|
181
|
+
<p v-if="is(state, AsyncState.Loading)">Refreshing…</p>
|
|
182
|
+
<p v-if="is(state, AsyncState.Failed)">Couldn't refresh the orders.</p>
|
|
183
|
+
<ul>
|
|
184
|
+
<li v-for="order in shown" :key="order.id">{{ order.total }}</li>
|
|
185
|
+
</ul>
|
|
186
|
+
</template>
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## A submit with a busy flag and an error message
|
|
190
|
+
|
|
191
|
+
A `saving` flag and an `error = ref('')` around an `await` are the trio again, written for an
|
|
192
|
+
action, and `vue-split-state` reports them (a lone flag set around an `await` too). The task's
|
|
193
|
+
state says whether it is running and why it failed, and the typed input stays where it is when
|
|
194
|
+
the server refuses it.
|
|
195
|
+
|
|
196
|
+
```vue bad vue-split-state
|
|
197
|
+
<script setup lang="ts">
|
|
198
|
+
import { ref } from "vue";
|
|
199
|
+
import { isErr } from "@evobug_com/safe-ts-std/result";
|
|
200
|
+
import { saveDraft } from "./api";
|
|
201
|
+
|
|
202
|
+
const text = ref("");
|
|
203
|
+
const saving = ref(false);
|
|
204
|
+
const error = ref("");
|
|
205
|
+
|
|
206
|
+
async function save(): Promise<void> {
|
|
207
|
+
saving.value = true;
|
|
208
|
+
const saved = await saveDraft(text.value);
|
|
209
|
+
saving.value = false;
|
|
210
|
+
error.value = isErr(saved) ? saved.error.message : "";
|
|
211
|
+
}
|
|
212
|
+
</script>
|
|
213
|
+
|
|
214
|
+
<template>
|
|
215
|
+
<textarea v-model="text" />
|
|
216
|
+
<button :disabled="saving" @click="save">Save</button>
|
|
217
|
+
<p v-if="error !== ''">{{ error }}</p>
|
|
218
|
+
</template>
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
```vue good
|
|
222
|
+
<script setup lang="ts">
|
|
223
|
+
import { ref } from "vue";
|
|
224
|
+
import { is, variant } from "@evobug_com/safe-ts-std/enum";
|
|
225
|
+
import { AsyncState } from "@evobug_com/safe-ts-vue/state";
|
|
226
|
+
import { useTask } from "@evobug_com/safe-ts-vue/task";
|
|
227
|
+
import { saveDraft } from "./api";
|
|
228
|
+
|
|
229
|
+
const text = ref("");
|
|
230
|
+
const { state: saving, run: save } = useTask((signal, draft: string) => saveDraft(draft, signal));
|
|
231
|
+
</script>
|
|
232
|
+
|
|
233
|
+
<template>
|
|
234
|
+
<textarea v-model="text" />
|
|
235
|
+
<button :disabled="is(saving, AsyncState.Loading)" @click="save(text)">Save</button>
|
|
236
|
+
<p v-if="is(saving, AsyncState.Failed)">Couldn't save: {{ variant(saving, AsyncState.Failed)?.error.message }}</p>
|
|
237
|
+
</template>
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
A dialog that closed when its handler threw must stay open when the handler now returns an
|
|
241
|
+
`Err`: check what the caller does with the result before changing a handler's contract (see
|
|
242
|
+
[behaviour.md](behaviour.md)).
|
|
243
|
+
|
|
244
|
+
## Showing an enum in a template
|
|
245
|
+
|
|
246
|
+
Three forms, all checked for exhaustiveness or explicitness:
|
|
247
|
+
|
|
248
|
+
- `<Match :value="value">` with one slot per variant (or `#_` last): checked by `vue-match`,
|
|
249
|
+
rewritten at build time into a `v-if` chain, and accepted by vue-tsc.
|
|
250
|
+
- `match(...)` in a `computed`, when every variant shows the same element with different text:
|
|
251
|
+
checked by TypeScript.
|
|
252
|
+
- A `v-if` / `v-else-if` chain of `is(state, AsyncState.Loading)` that names every variant,
|
|
253
|
+
without a final `v-else`: `vue-exhaustive-v-if` reports a chain that misses one, as it does
|
|
254
|
+
for a chain on `.tag` (which `prefer-is-over-tag` reports, with the idiomatic layer on).
|
|
255
|
+
One vue-tsc limit: inside an event handler of a `v-else-if` / `v-else` branch under `v-for`,
|
|
256
|
+
the row narrowed by `is` is `never`; give that branch its own `v-if` (Vue guide: Async state).
|
|
257
|
+
|
|
258
|
+
```vue bad vue-exhaustive-v-if
|
|
259
|
+
<script setup lang="ts">
|
|
260
|
+
import { watch } from "vue";
|
|
261
|
+
import { is } from "@evobug_com/safe-ts-std/enum";
|
|
262
|
+
import { AsyncState } from "@evobug_com/safe-ts-vue/state";
|
|
263
|
+
import { useTask } from "@evobug_com/safe-ts-vue/task";
|
|
264
|
+
import { fetchUser } from "./api";
|
|
265
|
+
|
|
266
|
+
const props = defineProps<{ id: string }>();
|
|
267
|
+
const { state, run } = useTask((signal, id: string) => fetchUser(id, signal));
|
|
268
|
+
|
|
269
|
+
watch(() => props.id, (id) => run(id), { immediate: true });
|
|
270
|
+
</script>
|
|
271
|
+
|
|
272
|
+
<template>
|
|
273
|
+
<p v-if="is(state, AsyncState.Loading)">Loading…</p>
|
|
274
|
+
<p v-else-if="is(state, AsyncState.Ready)">{{ state.value.name }}</p>
|
|
275
|
+
</template>
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
```vue good
|
|
279
|
+
<script setup lang="ts">
|
|
280
|
+
import { computed, watch } from "vue";
|
|
281
|
+
import { match } from "@evobug_com/safe-ts-std/enum";
|
|
282
|
+
import { useTask } from "@evobug_com/safe-ts-vue/task";
|
|
283
|
+
import { fetchUser } from "./api";
|
|
284
|
+
|
|
285
|
+
const props = defineProps<{ id: string }>();
|
|
286
|
+
const { state, run } = useTask((signal, id: string) => fetchUser(id, signal));
|
|
287
|
+
|
|
288
|
+
watch(() => props.id, (id) => run(id), { immediate: true });
|
|
289
|
+
|
|
290
|
+
const status = computed(() =>
|
|
291
|
+
match(state.value, {
|
|
292
|
+
Idle: () => "",
|
|
293
|
+
Loading: () => "Loading…",
|
|
294
|
+
Ready: ({ value }) => value.name,
|
|
295
|
+
Failed: ({ error }) => `Couldn't load the user: ${error.message}`,
|
|
296
|
+
}),
|
|
297
|
+
);
|
|
298
|
+
</script>
|
|
299
|
+
|
|
300
|
+
<template>
|
|
301
|
+
<p>{{ status }}</p>
|
|
302
|
+
</template>
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
`<Match>` doesn't take an `Option` of an enum: put it under `v-if="value"` with a `v-else` for
|
|
306
|
+
the missing case.
|
|
307
|
+
|
|
308
|
+
## A template ref
|
|
309
|
+
|
|
310
|
+
`ref<HTMLInputElement | null>(null)` (and `useTemplateRef`) is `null` until mount, and a `!`
|
|
311
|
+
hides the one case where that is real. `useElement` is typed without `null`, and the checker
|
|
312
|
+
proves the element is always rendered and read after mount (`vue-template-ref`). For an
|
|
313
|
+
element under `v-if`, `useOptionalElement` (`undefined` while missing); under `v-for`,
|
|
314
|
+
`useElements`.
|
|
315
|
+
|
|
316
|
+
```vue bad no-null
|
|
317
|
+
<script setup lang="ts">
|
|
318
|
+
import { onMounted, ref } from "vue";
|
|
319
|
+
|
|
320
|
+
const input = ref<HTMLInputElement | null>(null);
|
|
321
|
+
onMounted(() => input.value?.focus());
|
|
322
|
+
</script>
|
|
323
|
+
|
|
324
|
+
<template>
|
|
325
|
+
<input ref="input" />
|
|
326
|
+
</template>
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
```vue good
|
|
330
|
+
<script setup lang="ts">
|
|
331
|
+
import { onMounted } from "vue";
|
|
332
|
+
import { useElement } from "@evobug_com/safe-ts-vue/element";
|
|
333
|
+
|
|
334
|
+
const input = useElement<HTMLInputElement>("input");
|
|
335
|
+
onMounted(() => input.value.focus());
|
|
336
|
+
</script>
|
|
337
|
+
|
|
338
|
+
<template>
|
|
339
|
+
<input ref="input" />
|
|
340
|
+
<button @click="input.select()">Select all</button>
|
|
341
|
+
</template>
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
## Module state
|
|
345
|
+
|
|
346
|
+
A `ref` or `let` at the top of a composable file (or a plain `<script>` block) is one value for
|
|
347
|
+
every component and every test (`no-mutable-global`). `<script setup>` is per instance, so state
|
|
348
|
+
there is fine. For state many components share, the target is a Pinia setup store, or a
|
|
349
|
+
`createX()` provided at startup and read with `injectRequired` (Vue guide: State and props).
|
|
350
|
+
|
|
351
|
+
```ts bad no-mutable-global
|
|
352
|
+
import { ref } from "vue";
|
|
353
|
+
|
|
354
|
+
const count = ref(0);
|
|
355
|
+
|
|
356
|
+
export function useCounter() {
|
|
357
|
+
const increment = (): void => {
|
|
358
|
+
count.value++;
|
|
359
|
+
};
|
|
360
|
+
return { count, increment };
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
```ts good no-tsc
|
|
365
|
+
import { defineStore } from "pinia";
|
|
366
|
+
import { ref } from "vue";
|
|
367
|
+
|
|
368
|
+
export const useCounterStore = defineStore("counter", () => {
|
|
369
|
+
const count = ref(0);
|
|
370
|
+
const increment = (): void => {
|
|
371
|
+
count.value++;
|
|
372
|
+
};
|
|
373
|
+
return { count, increment };
|
|
374
|
+
});
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
- A store keeps the state; readers use `store.count` (or `storeToRefs(store)`), writers call its
|
|
378
|
+
functions. Destructuring the store copies the values (`vue-lost-reactivity`); a constant the
|
|
379
|
+
store returns (a `markRaw` router) is read as `store.router`.
|
|
380
|
+
- A counter used to keep only the newest answer (`let latest = 0`, compared after an `await`)
|
|
381
|
+
is what `useTask` does: called in a setup store, a task belongs to the store's effect scope
|
|
382
|
+
and is aborted when the store is disposed. With the idiomatic layer on, `latest-wins-counter`
|
|
383
|
+
reports the hand-written counter, at module level or in a store's setup function.
|
|
384
|
+
- Readers outside components that imported the old module-level `ref` can read a module-level
|
|
385
|
+
`computed(() => useXStore().value)` bridge while they migrate (Vue guide: State and props).
|
|
386
|
+
- Two modules that must not import each other but need an event between them (a list that
|
|
387
|
+
refreshes when a thread is read elsewhere) share a small store: a counter the writer bumps
|
|
388
|
+
and the reader `watch`es. Don't rely on another store's `$onAction` hook: it stops firing
|
|
389
|
+
as soon as the call moves out of that store.
|
|
390
|
+
|
|
391
|
+
```ts good no-tsc
|
|
392
|
+
import { defineStore } from "pinia";
|
|
393
|
+
import { useTask } from "@evobug_com/safe-ts-vue/task";
|
|
394
|
+
import { fetchOrders } from "./api";
|
|
395
|
+
|
|
396
|
+
export const useOrdersStore = defineStore("orders", () => {
|
|
397
|
+
const orders = useTask((signal) => fetchOrders(signal));
|
|
398
|
+
return { orders: orders.state, load: orders.run };
|
|
399
|
+
});
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
## A store action that throws
|
|
403
|
+
|
|
404
|
+
A store action that rejects (or throws "not hydrated") makes every caller wrap it. Return a
|
|
405
|
+
`Result` instead, and update every caller in the same change. The checker follows a `Result`
|
|
406
|
+
through Pinia setup stores and options stores' `actions`, so `must-use-result` reports a caller
|
|
407
|
+
that drops it (`await store.rename(x);`, `store.rename(x).catch(() => {})`), and
|
|
408
|
+
`catch-on-result` (idiomatic) a `try` left around it. It doesn't see a store reached through a
|
|
409
|
+
parameter or kept in a `ref`: search for each member's name too (`rg -n '\.rename\('`) and read
|
|
410
|
+
every site.
|
|
411
|
+
|
|
412
|
+
```ts good no-tsc
|
|
413
|
+
import { defineStore } from "pinia";
|
|
414
|
+
import { ref } from "vue";
|
|
415
|
+
import type { Result } from "@evobug_com/safe-ts-std/result";
|
|
416
|
+
import { type ApiError, renameUser } from "./api";
|
|
417
|
+
|
|
418
|
+
export const useProfileStore = defineStore("profile", () => {
|
|
419
|
+
const name = ref("");
|
|
420
|
+
const rename = async (next: string): Promise<Result<void, ApiError>> => {
|
|
421
|
+
const renamed = await renameUser(next);
|
|
422
|
+
if (renamed === undefined) name.value = next;
|
|
423
|
+
return renamed;
|
|
424
|
+
};
|
|
425
|
+
return { name, rename };
|
|
426
|
+
});
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
A caller that showed a server's message inline (a 422 refusal) must still show it: match on the
|
|
430
|
+
error kinds where the old code tested the HTTP library's error, not a generic toast. With the
|
|
431
|
+
idiomatic layer on, `misplaced-axios-check` reports an `isAxiosError(r.error)` left on a store
|
|
432
|
+
member that now has its own error type.
|
|
433
|
+
|
|
434
|
+
## A prop changed by the child
|
|
435
|
+
|
|
436
|
+
Vue makes the props object read-only, not the objects inside it, so `props.user.email = ""`
|
|
437
|
+
changes the parent's data behind its back (`no-param-mutation`). Ask the parent with an event
|
|
438
|
+
or `defineModel()`. When the write was a deliberate sync of a store's own object, keep a local
|
|
439
|
+
`ref` for the control, send the change, and reload the store after the server answers. When the
|
|
440
|
+
child really owns one field of what it is given, say so in the prop's type,
|
|
441
|
+
`defineProps<{ draft: { text: Mut<Ref<string>> } }>()`: a `Mut<...>` field is the child's to
|
|
442
|
+
change, and the other fields stay borrowed.
|
|
443
|
+
|
|
444
|
+
```vue bad no-param-mutation
|
|
445
|
+
<script setup lang="ts">
|
|
446
|
+
type User = { name: string; email: string };
|
|
447
|
+
|
|
448
|
+
const props = defineProps<{ user: User }>();
|
|
449
|
+
|
|
450
|
+
function clearEmail(): void {
|
|
451
|
+
props.user.email = "";
|
|
452
|
+
}
|
|
453
|
+
</script>
|
|
454
|
+
|
|
455
|
+
<template>
|
|
456
|
+
<input v-model="props.user.name" />
|
|
457
|
+
<button @click="clearEmail">Clear email</button>
|
|
458
|
+
</template>
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
```vue good
|
|
462
|
+
<script setup lang="ts">
|
|
463
|
+
const name = defineModel<string>("name", { required: true });
|
|
464
|
+
const emit = defineEmits<{ clearEmail: [] }>();
|
|
465
|
+
</script>
|
|
466
|
+
|
|
467
|
+
<template>
|
|
468
|
+
<input v-model="name" />
|
|
469
|
+
<button @click="emit('clearEmail')">Clear email</button>
|
|
470
|
+
</template>
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
## v-html and bound URLs
|
|
474
|
+
|
|
475
|
+
`v-html` inserts HTML as it is, and a bound `:href` takes `javascript:` URLs (`vue-v-html`,
|
|
476
|
+
`vue-unsafe-url`). Render text where you can; bind URLs through `safeHref`, which keeps
|
|
477
|
+
relative, `http:`, `https:`, `mailto:` and `tel:` URLs and gives `"#"` for anything else.
|
|
478
|
+
Another scheme the app needs goes in its options: `safeHref(url, { schemes: ["blob"] })`
|
|
479
|
+
(`javascript:`, `vbscript:` and `data:` stay refused even when listed). When it must be HTML,
|
|
480
|
+
sanitize it. Declare the project's sanitizer once with `defineSanitizer`
|
|
481
|
+
(`@evobug_com/safe-ts-vue/html`): what it returns is typed `Html`, so `v-html` accepts it in a
|
|
482
|
+
`computed`, a prop typed `Html` or either branch of a `?:`, and vue-tsc refuses a plain string
|
|
483
|
+
for that prop. `vue-v-html` also accepts DOMPurify, `sanitize-html` and `xss` called where it
|
|
484
|
+
can see them. The same goes for URLs: `safeHref` returns a `SafeUrl`, and a prop typed `SafeUrl`
|
|
485
|
+
needs no check in the child. `defineSanitizer` is where the project vouches for a function:
|
|
486
|
+
keep it in one module, and list it in the architecture document (guide: Proof types).
|
|
487
|
+
|
|
488
|
+
```vue bad vue-v-html vue-unsafe-url
|
|
489
|
+
<script setup lang="ts">
|
|
490
|
+
type Comment = { readonly author: string; readonly website: string; readonly body: string };
|
|
491
|
+
|
|
492
|
+
const props = defineProps<{ comment: Comment }>();
|
|
493
|
+
</script>
|
|
494
|
+
|
|
495
|
+
<template>
|
|
496
|
+
<a :href="props.comment.website">{{ props.comment.author }}</a>
|
|
497
|
+
<div v-html="props.comment.body"></div>
|
|
498
|
+
</template>
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
```vue good
|
|
502
|
+
<script setup lang="ts">
|
|
503
|
+
import { safeHref } from "@evobug_com/safe-ts-vue/url";
|
|
504
|
+
|
|
505
|
+
type Comment = { readonly author: string; readonly website: string; readonly body: string };
|
|
506
|
+
|
|
507
|
+
const props = defineProps<{ comment: Comment }>();
|
|
508
|
+
</script>
|
|
509
|
+
|
|
510
|
+
<template>
|
|
511
|
+
<a :href="safeHref(props.comment.website)">{{ props.comment.author }}</a>
|
|
512
|
+
<p>{{ props.comment.body }}</p>
|
|
513
|
+
</template>
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
A binding whose branches are each a checked URL or no URL is accepted too:
|
|
517
|
+
`ok ? safeHref(url) : undefined`, `safeHref(url) || undefined`, `ok && safeHref(url)`. A
|
|
518
|
+
written-out URL that `safeHref` would refuse (`safeHref('flow://watch')` without its scheme in
|
|
519
|
+
`schemes`) is reported: the link would always be `#`.
|
|
520
|
+
|
|
521
|
+
## A Result returned to Vue
|
|
522
|
+
|
|
523
|
+
Vue drops what a handler returns, so `@click="publish"` with a `Result` loses the error
|
|
524
|
+
(`must-use-result`). The handler decides what the user sees.
|
|
525
|
+
|
|
526
|
+
```vue bad must-use-result
|
|
527
|
+
<script setup lang="ts">
|
|
528
|
+
import { err, type Result } from "@evobug_com/safe-ts-std/result";
|
|
529
|
+
|
|
530
|
+
const props = defineProps<{ draft: string }>();
|
|
531
|
+
|
|
532
|
+
function publish(): Result<void, string> {
|
|
533
|
+
return props.draft === "" ? err("Write something first") : undefined;
|
|
534
|
+
}
|
|
535
|
+
</script>
|
|
536
|
+
|
|
537
|
+
<template>
|
|
538
|
+
<button @click="publish">Publish</button>
|
|
539
|
+
</template>
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
```vue good
|
|
543
|
+
<script setup lang="ts">
|
|
544
|
+
import { computed } from "vue";
|
|
545
|
+
import { err, type Option, type Result } from "@evobug_com/safe-ts-std/result";
|
|
546
|
+
|
|
547
|
+
const props = defineProps<{ draft: string }>();
|
|
548
|
+
|
|
549
|
+
function publishBlock(draft: string): Result<void, string> {
|
|
550
|
+
return draft === "" ? err("Write something first") : undefined;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const problem = computed((): Option<string> => publishBlock(props.draft)?.error);
|
|
554
|
+
</script>
|
|
555
|
+
|
|
556
|
+
<template>
|
|
557
|
+
<button :disabled="problem !== undefined">Publish</button>
|
|
558
|
+
<p v-if="problem !== undefined">{{ problem }}</p>
|
|
559
|
+
</template>
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
## A listener on window
|
|
563
|
+
|
|
564
|
+
A listener or interval started by a component keeps running after it unmounts
|
|
565
|
+
(`listener-cleanup`). `useListener` and `useInterval` stop with the component.
|
|
566
|
+
|
|
567
|
+
```vue bad listener-cleanup
|
|
568
|
+
<script setup lang="ts">
|
|
569
|
+
import { onMounted, ref } from "vue";
|
|
570
|
+
|
|
571
|
+
const width = ref(0);
|
|
572
|
+
|
|
573
|
+
function measure(): void {
|
|
574
|
+
width.value = window.innerWidth;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
onMounted(() => {
|
|
578
|
+
window.addEventListener("resize", measure);
|
|
579
|
+
});
|
|
580
|
+
</script>
|
|
581
|
+
|
|
582
|
+
<template>
|
|
583
|
+
<p>{{ width }}px</p>
|
|
584
|
+
</template>
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
```vue good
|
|
588
|
+
<script setup lang="ts">
|
|
589
|
+
import { ref } from "vue";
|
|
590
|
+
import { useListener } from "@evobug_com/safe-ts-vue/listen";
|
|
591
|
+
|
|
592
|
+
const width = ref(window.innerWidth);
|
|
593
|
+
|
|
594
|
+
function measure(): void {
|
|
595
|
+
width.value = window.innerWidth;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
useListener(window, "resize", measure);
|
|
599
|
+
</script>
|
|
600
|
+
|
|
601
|
+
<template>
|
|
602
|
+
<p>{{ width }}px</p>
|
|
603
|
+
</template>
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
## Typed props in defineComponent
|
|
607
|
+
|
|
608
|
+
Vue's documented way to type an options-object prop is a cast (`String as PropType<...>`,
|
|
609
|
+
`no-unchecked-cast`). `prop` and `oneOf` build the same declaration with the type attached.
|
|
610
|
+
|
|
611
|
+
```ts bad no-unchecked-cast
|
|
612
|
+
import { defineComponent, h, type PropType } from "vue";
|
|
613
|
+
|
|
614
|
+
export const Panel = defineComponent({
|
|
615
|
+
props: {
|
|
616
|
+
side: { type: String as PropType<"left" | "right">, default: "left" },
|
|
617
|
+
},
|
|
618
|
+
setup(props) {
|
|
619
|
+
return () => h("aside", { class: props.side });
|
|
620
|
+
},
|
|
621
|
+
});
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
```ts good
|
|
625
|
+
import { defineComponent, h } from "vue";
|
|
626
|
+
import { oneOf } from "@evobug_com/safe-ts-vue/prop";
|
|
627
|
+
|
|
628
|
+
export const Panel = defineComponent({
|
|
629
|
+
props: {
|
|
630
|
+
side: oneOf(["left", "right"], { default: "left" }),
|
|
631
|
+
},
|
|
632
|
+
setup(props) {
|
|
633
|
+
return () => h("aside", { class: props.side });
|
|
634
|
+
},
|
|
635
|
+
});
|
|
636
|
+
```
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Review checklist
|
|
2
|
+
|
|
3
|
+
For a reviewer of a migration change, human or agent. Correctness first, then architecture: a
|
|
4
|
+
change can pass every gate and still be a minimal conversion.
|
|
5
|
+
|
|
6
|
+
## Gates and behaviour
|
|
7
|
+
|
|
8
|
+
- [ ] Every gate in [order-and-gates.md](order-and-gates.md) ran on this change, and the output is
|
|
9
|
+
in the hand-off report. `safe-ts check` ran with `--deny-warnings` and `"idiomatic": true`
|
|
10
|
+
(a plain run exits 0 with every idiomatic finding left).
|
|
11
|
+
- [ ] The census baseline wasn't rewritten, or the commit that rewrote it says why, and the
|
|
12
|
+
exception list has the line.
|
|
13
|
+
- [ ] Request bodies of the touched write flows are the same as before, or each difference is
|
|
14
|
+
intended and written down. Fields cleared with `null` still send `null`.
|
|
15
|
+
- [ ] Failure paths still show what they showed: inline messages are still inline, dialogs stay
|
|
16
|
+
open with the input kept when the server refuses.
|
|
17
|
+
|
|
18
|
+
## Signatures that changed
|
|
19
|
+
|
|
20
|
+
- [ ] Every exported function and store member that now returns a `Result` (or takes or returns
|
|
21
|
+
`Option` instead of `null`) is listed in the hand-off report.
|
|
22
|
+
- [ ] Every caller of each, in every area, was found by searching its name and read: no leftover
|
|
23
|
+
`try`/`catch` or `.catch` around a call that no longer rejects, no dropped `Result`. The
|
|
24
|
+
checker reports these through store members and factories; the search covers the rest
|
|
25
|
+
(parameters, class fields, a `ref` or `Map`).
|
|
26
|
+
|
|
27
|
+
## Architecture
|
|
28
|
+
|
|
29
|
+
- [ ] No new `loading` / `error` / `data` state: async work goes through `useTask`, shown with
|
|
30
|
+
`<Match>` or `match`.
|
|
31
|
+
- [ ] No `''`, `-1` or `0` meaning "none" or "no error"; no boolean sets that are one state.
|
|
32
|
+
- [ ] Closed sets are `Enum`s, read in the project's one style (`match`, `is`, `variant`,
|
|
33
|
+
`<Match>`); no unions told apart by `'key' in`.
|
|
34
|
+
- [ ] Errors are `Result`s with named error types, passed up with `q(...)` (and a mapping or
|
|
35
|
+
`mapErr` when the type changes), not re-wrapped by hand.
|
|
36
|
+
- [ ] No failure turned into `undefined` (`unwrapOr(x, undefined)`, a helper that wraps it,
|
|
37
|
+
`.catch(() => undefined)`).
|
|
38
|
+
- [ ] Outside data goes through `validate<T>` / `parseJson<T>` or a pack's safe version; no casts
|
|
39
|
+
or hand-written guards on it.
|
|
40
|
+
- [ ] `jsonNull` appears only in outgoing data; no `Option<T | JsonNull>` in app types.
|
|
41
|
+
- [ ] Request bodies that change a record are `Patch<T>`; server fields from a list that can
|
|
42
|
+
grow are `Open<K>` (and `.value` is what goes back); markup and URL props are typed `Html`
|
|
43
|
+
/ `SafeUrl`, and no brand is made by a cast.
|
|
44
|
+
- [ ] Optional keys use the one form (`...(x === undefined ? {} : { x })`, or `Option<T>` fields
|
|
45
|
+
always written); wire-keyed tables use `getOwn(table, key, fallback)`; base64 uses
|
|
46
|
+
`toBase64` / `fromBase64`.
|
|
47
|
+
- [ ] No module state; shared state lives in a store or a provided context.
|
|
48
|
+
- [ ] `panic` only for invariants; never on a loading state or server data.
|
|
49
|
+
- [ ] Helpers are the architecture document's sanctioned ones; no new wrapper around std.
|
|
50
|
+
|
|
51
|
+
## Escape hatches
|
|
52
|
+
|
|
53
|
+
- [ ] Each new `unsafe`, `unsafeAsync`, `panic`, `trust`, `LegacyNull`, `void`, cast,
|
|
54
|
+
`safe-ts-ignore`, rule level in package.json or legacy marker has a reason, fits the policy
|
|
55
|
+
in [escape-hatches.md](escape-hatches.md), and is within the area's budget.
|
|
56
|
+
- [ ] No legacy file was put in `safeTs({ exclude })`, and no project-local bridge type stands in
|
|
57
|
+
for `LegacyNull`.
|
|
58
|
+
- [ ] `unsafeAsync` around project code passes `Panic`, or there is a reason it doesn't.
|
|
59
|
+
|
|
60
|
+
## Docs and conventions
|
|
61
|
+
|
|
62
|
+
- [ ] The project's conventions (`CLAUDE.md` / `AGENTS.md`) and gate fix texts name the forms this
|
|
63
|
+
skill's catalogue names, and nothing else.
|