@antelopejs/dms-api 0.3.0 → 0.4.1
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 +88 -148
- package/dist/db/models/api_settings.model.d.ts +1 -0
- package/dist/db/models/request_log.model.d.ts +1 -0
- package/dist/db/models/routes.model.d.ts +1 -0
- package/dist/db/models/routes_statistics.model.d.ts +1 -0
- package/frontend-vue/app/components/blocks/HealthHero.vue +11 -5
- package/frontend-vue/app/components/blocks/LiveTraffic.vue +3 -2
- package/frontend-vue/app/components/blocks/RequestDetail.vue +35 -33
- package/frontend-vue/app/components/blocks/RetentionInput.vue +51 -20
- package/frontend-vue/app/components/blocks/RouteTester.vue +86 -28
- package/frontend-vue/app/components/blocks/RouteTree.vue +9 -9
- package/frontend-vue/app/components/blocks/ScopeChip.vue +11 -7
- package/frontend-vue/app/composables/useClientValue.ts +18 -0
- package/frontend-vue/app/composables/useRouteDetail.ts +22 -106
- package/frontend-vue/app/utils/http.ts +10 -0
- package/frontend-vue/app/utils/testerRequest.ts +9 -7
- package/frontend-vue/dms.server.ts +9 -0
- package/frontend-vue/package.json +1 -1
- package/frontend-vue/server/tester.ts +266 -0
- package/frontend-vue/tests/clientValue.test.ts +64 -0
- package/frontend-vue/tests/dmsServer.test.ts +44 -0
- package/frontend-vue/tests/testerRequest.test.ts +1 -1
- package/frontend-vue/tests/testerRoute.test.ts +299 -0
- package/package.json +29 -13
- package/skills/dms-api/SKILL.md +31 -0
- package/frontend-vue/app/composables/useApiResource.ts +0 -92
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
computed,
|
|
4
|
+
onBeforeUnmount,
|
|
5
|
+
onMounted,
|
|
6
|
+
reactive,
|
|
7
|
+
ref,
|
|
8
|
+
shallowRef,
|
|
9
|
+
useId,
|
|
10
|
+
watch,
|
|
11
|
+
} from 'vue'
|
|
3
12
|
import {
|
|
4
13
|
byteLength,
|
|
5
14
|
formatBytes,
|
|
@@ -9,6 +18,7 @@ import {
|
|
|
9
18
|
} from '../../utils/format'
|
|
10
19
|
import {
|
|
11
20
|
acceptsRequestBody,
|
|
21
|
+
errorStatus,
|
|
12
22
|
isWritingMethod,
|
|
13
23
|
pathSegments,
|
|
14
24
|
statusText,
|
|
@@ -111,6 +121,20 @@ interface CapturedRow {
|
|
|
111
121
|
|
|
112
122
|
type CapturedState = 'idle' | 'loading' | 'found' | 'missing' | 'error'
|
|
113
123
|
|
|
124
|
+
/** A backend read of the tester's own flows (a replay, a capture lookup). */
|
|
125
|
+
type ReadJson = <R>(url: string) => Promise<R>
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A read the tester runs when it needs it (a replay, a lookup after a call),
|
|
129
|
+
* in the browser only and never again on the page's refreshes.
|
|
130
|
+
*/
|
|
131
|
+
const ON_DEMAND = {
|
|
132
|
+
immediate: false,
|
|
133
|
+
server: false,
|
|
134
|
+
watch: false,
|
|
135
|
+
refreshOnPageRefresh: false,
|
|
136
|
+
} as const
|
|
137
|
+
|
|
114
138
|
// The tester's own reads of the request logs: by its permission, not the
|
|
115
139
|
// request logs page's.
|
|
116
140
|
const LOGS_API = '/api/monitoring/logs/tester'
|
|
@@ -159,7 +183,7 @@ const { formatLocale: locale } = useRegionalFormat({ locale: 'format' })
|
|
|
159
183
|
const dateFormat = useDateFormat()
|
|
160
184
|
const toast = useToast()
|
|
161
185
|
const { confirm } = useConfirm()
|
|
162
|
-
const { loggedIn } =
|
|
186
|
+
const { loggedIn } = useDmsUserSession()
|
|
163
187
|
const currentRoute = useDmsRoute()
|
|
164
188
|
const router = useDmsRouter()
|
|
165
189
|
const { $authFetch } = useAuthFetch()
|
|
@@ -170,6 +194,8 @@ const {
|
|
|
170
194
|
} = useRouteDetail(props.fetchUrl, props.queryKey)
|
|
171
195
|
const apiOrigin = useApiOrigin(() => route.value?.apiOrigin)
|
|
172
196
|
const blockerId = useId()
|
|
197
|
+
// Not a `useApiData` read: each call mints a short-lived token, which must
|
|
198
|
+
// never be shared between calls, cached or handed over with the page.
|
|
173
199
|
const sourceTokens = createSourceTokens(() =>
|
|
174
200
|
$authFetch<TesterSourceToken>(TESTER_SOURCE_URL),
|
|
175
201
|
)
|
|
@@ -202,6 +228,9 @@ const capturedLog = ref<CapturedRequest | null>(null)
|
|
|
202
228
|
|
|
203
229
|
// The route the fields belong to, the call in flight and its route.
|
|
204
230
|
let activeRef: string | null = null
|
|
231
|
+
// The route the fields were filled for: they come from this browser's drafts,
|
|
232
|
+
// history and headers, so the tester shows once mounted, never on a server.
|
|
233
|
+
const filledRef = ref<string | null>(null)
|
|
205
234
|
let inFlight: { controller: AbortController; ref: string } | null = null
|
|
206
235
|
let confirming = false
|
|
207
236
|
let unmounted = false
|
|
@@ -612,6 +641,7 @@ function loadRoute(next: string) {
|
|
|
612
641
|
sending.value = false
|
|
613
642
|
}
|
|
614
643
|
activeRef = next
|
|
644
|
+
filledRef.value = next
|
|
615
645
|
history.value = loadHistory(next)
|
|
616
646
|
const draft = loadDraft(next)
|
|
617
647
|
if (draft) {
|
|
@@ -626,12 +656,14 @@ function loadRoute(next: string) {
|
|
|
626
656
|
if (typeof replayId === 'string' && replayId) void replay(replayId, next)
|
|
627
657
|
}
|
|
628
658
|
|
|
629
|
-
|
|
630
|
-
(
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
659
|
+
onMounted(() =>
|
|
660
|
+
watch(
|
|
661
|
+
() => route.value?.ref,
|
|
662
|
+
(next) => {
|
|
663
|
+
if (next) loadRoute(next)
|
|
664
|
+
},
|
|
665
|
+
{ immediate: true },
|
|
666
|
+
),
|
|
635
667
|
)
|
|
636
668
|
|
|
637
669
|
// --- Replay ---
|
|
@@ -642,13 +674,23 @@ function dropReplayParam() {
|
|
|
642
674
|
return router.replace({ query: rest })
|
|
643
675
|
}
|
|
644
676
|
|
|
677
|
+
const replayedId = shallowRef<string | null>(null)
|
|
678
|
+
const replayRead = useApiData<CapturedRequest>(
|
|
679
|
+
() =>
|
|
680
|
+
replayedId.value
|
|
681
|
+
? `${LOGS_API}/${encodeURIComponent(replayedId.value)}`
|
|
682
|
+
: null,
|
|
683
|
+
ON_DEMAND,
|
|
684
|
+
)
|
|
685
|
+
|
|
645
686
|
async function replay(id: string, ref: string) {
|
|
646
687
|
void dropReplayParam()
|
|
647
688
|
try {
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
)
|
|
651
|
-
|
|
689
|
+
replayedId.value = id
|
|
690
|
+
await replayRead.execute()
|
|
691
|
+
if (replayRead.error.value) throw replayRead.error.value
|
|
692
|
+
const log = replayRead.data.value
|
|
693
|
+
if (activeRef !== ref || !log) return
|
|
652
694
|
const capturedRef = log.route
|
|
653
695
|
if (capturedRef !== ref) {
|
|
654
696
|
toast.add({
|
|
@@ -904,10 +946,11 @@ function commit(
|
|
|
904
946
|
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
905
947
|
|
|
906
948
|
async function findCaptured(
|
|
949
|
+
read: ReadJson,
|
|
907
950
|
target: TesterResult,
|
|
908
951
|
): Promise<CapturedRequest | null> {
|
|
909
952
|
const id = target.requestId as string
|
|
910
|
-
const page = await
|
|
953
|
+
const page = await read<{ results: CapturedRow[] }>(
|
|
911
954
|
`${LOGS_API}?${new URLSearchParams({ requestId: id })}`,
|
|
912
955
|
)
|
|
913
956
|
// Only the call sent: a list that ignored the filter is no answer.
|
|
@@ -923,29 +966,44 @@ async function findCaptured(
|
|
|
923
966
|
(found.route && activeRef && found.route !== activeRef)
|
|
924
967
|
)
|
|
925
968
|
return null
|
|
926
|
-
return
|
|
927
|
-
`${LOGS_API}/${encodeURIComponent(found._id)}`,
|
|
928
|
-
)
|
|
969
|
+
return read<CapturedRequest>(`${LOGS_API}/${encodeURIComponent(found._id)}`)
|
|
929
970
|
}
|
|
930
971
|
|
|
972
|
+
const capturedTarget = shallowRef<TesterResult | null>(null)
|
|
973
|
+
const captured = useApiData<CapturedRequest | null>(
|
|
974
|
+
() =>
|
|
975
|
+
capturedTarget.value?.requestId
|
|
976
|
+
? `dms-api:tester-captured:${capturedTarget.value.requestId}`
|
|
977
|
+
: null,
|
|
978
|
+
({ $authFetch, signal }) =>
|
|
979
|
+
findCaptured(
|
|
980
|
+
(url) => $authFetch(url, { signal }),
|
|
981
|
+
capturedTarget.value as TesterResult,
|
|
982
|
+
),
|
|
983
|
+
ON_DEMAND,
|
|
984
|
+
)
|
|
985
|
+
|
|
931
986
|
async function lookUpCaptured(target: TesterResult, retry: boolean) {
|
|
932
987
|
const id = target.requestId
|
|
933
988
|
if (!id) return
|
|
934
989
|
capturedState.value = 'loading'
|
|
990
|
+
const current = () =>
|
|
991
|
+
result.value?.requestId === id && capturedTarget.value?.requestId === id
|
|
935
992
|
for (const delay of retry ? CAPTURE_DELAYS_MS : [0]) {
|
|
936
993
|
if (delay) await wait(delay)
|
|
937
994
|
if (result.value?.requestId !== id) return
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
995
|
+
capturedTarget.value = target
|
|
996
|
+
await captured.execute()
|
|
997
|
+
if (!current()) return
|
|
998
|
+
if (captured.error.value) {
|
|
999
|
+
capturedState.value = 'error'
|
|
1000
|
+
return
|
|
1001
|
+
}
|
|
1002
|
+
const log = captured.data.value
|
|
1003
|
+
if (log) {
|
|
1004
|
+
capturedLog.value = log
|
|
1005
|
+
capturedState.value = 'found'
|
|
1006
|
+
keepCaptured(id, log)
|
|
949
1007
|
return
|
|
950
1008
|
}
|
|
951
1009
|
}
|
|
@@ -1204,7 +1262,7 @@ const shortcut = computed(() =>
|
|
|
1204
1262
|
|
|
1205
1263
|
<template>
|
|
1206
1264
|
<div v-if="!selection.ref.value" />
|
|
1207
|
-
<USkeleton v-else-if="pending" class="h-72" />
|
|
1265
|
+
<USkeleton v-else-if="pending || (route && !filledRef)" class="h-72" />
|
|
1208
1266
|
<div
|
|
1209
1267
|
v-else-if="route"
|
|
1210
1268
|
class="flex flex-col gap-4"
|
|
@@ -81,13 +81,13 @@ const { formatLocale: locale, formatNumber } = useRegionalFormat({
|
|
|
81
81
|
locale: 'format',
|
|
82
82
|
})
|
|
83
83
|
const selection = useRouteSelection(props.queryKey)
|
|
84
|
-
const { data, error, pending, refresh } =
|
|
84
|
+
const { data, error, pending, refresh } = useApiData<{
|
|
85
85
|
routes: RouteRow[]
|
|
86
86
|
/** Minutes of traffic each state is judged on. */
|
|
87
87
|
healthWindowMinutes?: number
|
|
88
88
|
/** Minutes of traffic each request count covers. */
|
|
89
89
|
requestsWindowMinutes?: number
|
|
90
|
-
}>(() => props.fetchUrl)
|
|
90
|
+
}>(() => props.fetchUrl, { refreshOnPageRefresh: false })
|
|
91
91
|
|
|
92
92
|
// Live, the states and counts are read again in place, at the page's pace:
|
|
93
93
|
// the list keeps its order, its scroll and the focus.
|
|
@@ -377,13 +377,13 @@ const count = (value: number) =>
|
|
|
377
377
|
<template>
|
|
378
378
|
<DmsCard
|
|
379
379
|
:padded="false"
|
|
380
|
-
class="
|
|
380
|
+
class="@5xl:max-h-[calc(100dvh-2rem)] @5xl:min-h-[24rem] flex flex-col overflow-hidden"
|
|
381
381
|
>
|
|
382
382
|
<!-- Stacked above the detail, the list folds to the route it shows. -->
|
|
383
383
|
<button
|
|
384
384
|
ref="foldToggle"
|
|
385
385
|
type="button"
|
|
386
|
-
class="hover:bg-elevated flex items-center gap-2 px-3 py-2.5 text-left text-[13px]
|
|
386
|
+
class="hover:bg-elevated @5xl:hidden flex items-center gap-2 px-3 py-2.5 text-left text-[13px]"
|
|
387
387
|
:aria-expanded="listOpen"
|
|
388
388
|
aria-controls="api-route-list"
|
|
389
389
|
@click="listOpen = !listOpen"
|
|
@@ -407,7 +407,7 @@ const count = (value: number) =>
|
|
|
407
407
|
|
|
408
408
|
<div
|
|
409
409
|
id="api-route-list"
|
|
410
|
-
class="border-default flex min-h-0 flex-1 flex-col
|
|
410
|
+
class="border-default @max-5xl:border-t flex min-h-0 flex-1 flex-col"
|
|
411
411
|
:class="!listOpen && '@max-5xl:hidden'"
|
|
412
412
|
>
|
|
413
413
|
<div class="border-default flex flex-col gap-2.5 border-b p-3">
|
|
@@ -461,7 +461,7 @@ const count = (value: number) =>
|
|
|
461
461
|
|
|
462
462
|
<div
|
|
463
463
|
ref="scroller"
|
|
464
|
-
class="relative min-h-0 flex-1 overflow-y-auto py-1.5
|
|
464
|
+
class="@max-5xl:max-h-[50vh] relative min-h-0 flex-1 overflow-y-auto py-1.5"
|
|
465
465
|
>
|
|
466
466
|
<p
|
|
467
467
|
v-if="healthWindow && routes.length"
|
|
@@ -469,7 +469,7 @@ const count = (value: number) =>
|
|
|
469
469
|
>
|
|
470
470
|
{{ t('api.routes.tree.window', { window: healthWindow }) }}
|
|
471
471
|
</p>
|
|
472
|
-
<div v-if="pending" class="flex flex-col gap-2 px-3 py-2">
|
|
472
|
+
<div v-if="pending && !data" class="flex flex-col gap-2 px-3 py-2">
|
|
473
473
|
<USkeleton v-for="index in 8" :key="index" class="h-6 w-full" />
|
|
474
474
|
</div>
|
|
475
475
|
<DmsEmptyState
|
|
@@ -668,8 +668,8 @@ const count = (value: number) =>
|
|
|
668
668
|
@click="toggleAll"
|
|
669
669
|
/>
|
|
670
670
|
</UTooltip>
|
|
671
|
-
<UKbd value="↑" size="sm" class="
|
|
672
|
-
<UKbd value="↓" size="sm" class="
|
|
671
|
+
<UKbd value="↑" size="sm" class="@max-5xl:hidden shrink-0" />
|
|
672
|
+
<UKbd value="↓" size="sm" class="@max-5xl:hidden shrink-0" />
|
|
673
673
|
</div>
|
|
674
674
|
</div>
|
|
675
675
|
</DmsCard>
|
|
@@ -25,8 +25,9 @@ const props = defineProps<{
|
|
|
25
25
|
|
|
26
26
|
const { t } = useI18n()
|
|
27
27
|
const { formatNumber } = useRegionalFormat({ locale: 'format' })
|
|
28
|
-
const summary =
|
|
28
|
+
const summary = useApiData<{ scope: Scope; routes: number }>(
|
|
29
29
|
() => props.fetchUrl,
|
|
30
|
+
{ refreshOnPageRefresh: false },
|
|
30
31
|
)
|
|
31
32
|
|
|
32
33
|
const scope = computed(() => summary.data.value?.scope ?? props.scope ?? 'own')
|
|
@@ -44,27 +45,30 @@ const label = computed(() =>
|
|
|
44
45
|
*/
|
|
45
46
|
const REFRESH_INTERVAL_MS = 60_000
|
|
46
47
|
|
|
47
|
-
// When the chip last asked, so that a failing read is
|
|
48
|
+
// When the chip last asked (the mount reads it), so that a failing read is
|
|
49
|
+
// not retried each tick.
|
|
48
50
|
let lastAskedAt = Date.now()
|
|
49
51
|
|
|
50
|
-
function
|
|
51
|
-
const last = Math.max(lastAskedAt, summary.receivedAt.value ?? 0)
|
|
52
|
-
if (Date.now() - last < REFRESH_INTERVAL_MS) return
|
|
52
|
+
function read() {
|
|
53
53
|
lastAskedAt = Date.now()
|
|
54
54
|
void summary.refresh()
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
function refreshIfStale() {
|
|
58
|
+
if (Date.now() - lastAskedAt >= REFRESH_INTERVAL_MS) read()
|
|
59
|
+
}
|
|
60
|
+
|
|
57
61
|
usePageLiveRefresh(refreshIfStale)
|
|
58
62
|
if (props.realtimeTopic) useLiveMissedErrors(props.realtimeTopic)
|
|
59
63
|
|
|
60
64
|
function refreshWhenShown() {
|
|
61
|
-
if (document.visibilityState === 'visible')
|
|
65
|
+
if (document.visibilityState === 'visible') read()
|
|
62
66
|
}
|
|
63
67
|
|
|
64
68
|
// The first activation follows the mount, which already read the scope.
|
|
65
69
|
let activated = false
|
|
66
70
|
onActivated(() => {
|
|
67
|
-
if (activated)
|
|
71
|
+
if (activated) read()
|
|
68
72
|
activated = true
|
|
69
73
|
})
|
|
70
74
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { computed, onMounted, ref, type ComputedRef } from 'vue'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `read()` once the browser has mounted the component, `undefined` before:
|
|
5
|
+
* for a value the server and the browser may write differently, which the
|
|
6
|
+
* server's HTML must not hold. A date written with its weekday is one: the
|
|
7
|
+
* ICU of Node and the one of the browser disagree on its punctuation
|
|
8
|
+
* ("Thursday, 15 October" against "Thursday 15 October" in `en-GB`), and the
|
|
9
|
+
* hydration would report the mismatch. Meant for what the first paint can do
|
|
10
|
+
* without, such as a tooltip.
|
|
11
|
+
*/
|
|
12
|
+
export function useClientValue<T>(read: () => T): ComputedRef<T | undefined> {
|
|
13
|
+
const mounted = ref(false)
|
|
14
|
+
onMounted(() => {
|
|
15
|
+
mounted.value = true
|
|
16
|
+
})
|
|
17
|
+
return computed(() => (mounted.value ? read() : undefined))
|
|
18
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { computed
|
|
1
|
+
import { computed } from 'vue'
|
|
2
|
+
import { errorStatus } from '../utils/http'
|
|
2
3
|
import type { RouteState } from '../utils/routeExplorer'
|
|
3
4
|
|
|
4
5
|
/** One step of the request pipeline of a route, in the order it runs. */
|
|
@@ -134,120 +135,35 @@ export interface RouteDetail {
|
|
|
134
135
|
apiOrigin: string | null
|
|
135
136
|
}
|
|
136
137
|
|
|
137
|
-
/** The detail every block of the page reads, fetched once per route. */
|
|
138
|
-
interface DetailSlot {
|
|
139
|
-
url: string | null
|
|
140
|
-
data: RouteDetail | null
|
|
141
|
-
error: unknown
|
|
142
|
-
pending: boolean
|
|
143
|
-
refreshing: boolean
|
|
144
|
-
receivedAt: number | null
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const emptySlot = (): DetailSlot => ({
|
|
148
|
-
url: null,
|
|
149
|
-
data: null,
|
|
150
|
-
error: null,
|
|
151
|
-
pending: true,
|
|
152
|
-
refreshing: false,
|
|
153
|
-
receivedAt: null,
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
/** The request still running for a slot, and the blocks reading it. */
|
|
157
|
-
const running = new WeakMap<object, AbortController>()
|
|
158
|
-
const readers = new WeakMap<object, number>()
|
|
159
|
-
|
|
160
138
|
/**
|
|
161
139
|
* The route the page's `?route=` names, shared by the blocks that show it:
|
|
162
140
|
* the header, the documentation, the tester and the parts of the page that
|
|
163
|
-
* wait for a valid route read one payload
|
|
164
|
-
*
|
|
165
|
-
* refresh keeps the last data and
|
|
141
|
+
* wait for a valid route read one `useApiData` payload (one request per
|
|
142
|
+
* route, which a server render reads and hands over). Another route shows
|
|
143
|
+
* nothing until its payload is in; a failed refresh keeps the last data and
|
|
144
|
+
* says so through `error`.
|
|
166
145
|
*/
|
|
167
146
|
export function useRouteDetail(fetchUrl: string, queryKey = 'route') {
|
|
168
147
|
const selection = useRouteSelection(queryKey)
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
148
|
+
// The header reads it again, on the Statistics tab only: the
|
|
149
|
+
// documentation and the tester must not move with the page's refreshes.
|
|
150
|
+
const detail = useApiData<RouteDetail>(
|
|
151
|
+
() =>
|
|
152
|
+
selection.ref.value
|
|
153
|
+
? `${fetchUrl}?${new URLSearchParams({ route: selection.ref.value })}`
|
|
154
|
+
: null,
|
|
155
|
+
{ refreshOnPageRefresh: false },
|
|
173
156
|
)
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
if (import.meta.env.SSR) {
|
|
177
|
-
return {
|
|
178
|
-
selection,
|
|
179
|
-
missing: computed(() => false),
|
|
180
|
-
data: ref<RouteDetail | null>(null),
|
|
181
|
-
error: ref<unknown>(null),
|
|
182
|
-
pending: ref(true),
|
|
183
|
-
refreshing: ref(false),
|
|
184
|
-
receivedAt: ref<number | null>(null),
|
|
185
|
-
refresh: async () => {},
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const { $authFetch } = useAuthFetch()
|
|
190
|
-
const state = useDmsState<DetailSlot>(
|
|
191
|
-
`dms-api-route-detail:${fetchUrl}`,
|
|
192
|
-
emptySlot,
|
|
157
|
+
const data = computed(() =>
|
|
158
|
+
detail.placeholder.value ? null : detail.data.value,
|
|
193
159
|
)
|
|
194
|
-
|
|
195
|
-
async function load(url: string | null, force = false) {
|
|
196
|
-
const slot = state.value
|
|
197
|
-
if (!force && slot.url === url) return
|
|
198
|
-
running.get(slot)?.abort()
|
|
199
|
-
running.delete(slot)
|
|
200
|
-
if (slot.url !== url) {
|
|
201
|
-
slot.url = url
|
|
202
|
-
slot.data = null
|
|
203
|
-
slot.error = null
|
|
204
|
-
}
|
|
205
|
-
if (!url) {
|
|
206
|
-
slot.pending = false
|
|
207
|
-
return
|
|
208
|
-
}
|
|
209
|
-
const controller = new AbortController()
|
|
210
|
-
running.set(slot, controller)
|
|
211
|
-
slot.pending = slot.data === null
|
|
212
|
-
slot.refreshing = slot.data !== null
|
|
213
|
-
try {
|
|
214
|
-
const response = await $authFetch<RouteDetail>(url, {
|
|
215
|
-
signal: controller.signal,
|
|
216
|
-
})
|
|
217
|
-
if (running.get(slot) !== controller) return
|
|
218
|
-
slot.data = markRaw(response)
|
|
219
|
-
slot.error = null
|
|
220
|
-
slot.receivedAt = Date.now()
|
|
221
|
-
} catch (cause) {
|
|
222
|
-
if (running.get(slot) !== controller) return
|
|
223
|
-
if ((cause as { name?: string })?.name !== 'AbortError')
|
|
224
|
-
slot.error = cause
|
|
225
|
-
} finally {
|
|
226
|
-
if (running.get(slot) === controller) {
|
|
227
|
-
running.delete(slot)
|
|
228
|
-
slot.pending = false
|
|
229
|
-
slot.refreshing = false
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// The first block of a page visit refreshes what an earlier visit left.
|
|
235
|
-
const slot = state.value
|
|
236
|
-
const count = readers.get(slot) ?? 0
|
|
237
|
-
readers.set(slot, count + 1)
|
|
238
|
-
onBeforeUnmount(() => readers.set(slot, (readers.get(slot) ?? 1) - 1))
|
|
239
|
-
if (count === 0 && slot.url !== null) void load(target.value, true)
|
|
240
|
-
|
|
241
|
-
watch(target, (url) => void load(url), { immediate: true })
|
|
242
|
-
|
|
243
160
|
return {
|
|
244
161
|
selection,
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
pending: computed(() =>
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
refresh: () => load(target.value, true),
|
|
162
|
+
data,
|
|
163
|
+
error: detail.error,
|
|
164
|
+
/** Nothing to show yet for the route named: its first read is running. */
|
|
165
|
+
pending: computed(() => detail.pending.value && data.value === null),
|
|
166
|
+
refresh: () => detail.refresh(),
|
|
167
|
+
missing: computed(() => errorStatus(detail.error.value) === 404),
|
|
252
168
|
}
|
|
253
169
|
}
|
|
@@ -125,3 +125,13 @@ export function parseRouteRef(
|
|
|
125
125
|
const match = ref?.trim().match(/^([A-Za-z]+)\s+(\/\S*)$/)
|
|
126
126
|
return match ? { method: match[1].toUpperCase(), path: match[2] } : null
|
|
127
127
|
}
|
|
128
|
+
|
|
129
|
+
/** The HTTP status of a failed request, when it has one. */
|
|
130
|
+
export function errorStatus(error: unknown): number | undefined {
|
|
131
|
+
const value = error as {
|
|
132
|
+
statusCode?: number
|
|
133
|
+
status?: number
|
|
134
|
+
response?: { status?: number }
|
|
135
|
+
}
|
|
136
|
+
return value?.statusCode ?? value?.status ?? value?.response?.status
|
|
137
|
+
}
|
|
@@ -29,20 +29,22 @@ export type BodyMode = 'json' | 'text' | 'form' | 'none'
|
|
|
29
29
|
/** Who the call is authenticated as, as the DMS proxy sends it. */
|
|
30
30
|
export type AuthMode = 'session' | 'manual' | 'none'
|
|
31
31
|
|
|
32
|
-
// --- The
|
|
33
|
-
//
|
|
34
|
-
// what it answers when it does not forward a call. Its error bodies hold one
|
|
32
|
+
// --- The tester's server route (`/api/_dms-api/tester`) ---
|
|
33
|
+
// `server/tester.ts`, in this module's `dms.server.ts`: the limits it applies,
|
|
34
|
+
// and what it answers when it does not forward a call. Its error bodies hold one
|
|
35
35
|
// generic text per status, so the status, the elapsed time and the checks
|
|
36
36
|
// below are what tell the causes apart.
|
|
37
37
|
|
|
38
|
+
/** Where the tester's server route answers. */
|
|
39
|
+
export const TESTER_ROUTE = '/api/_dms-api/tester'
|
|
38
40
|
/** The proxy reads at most this much of the request (the JSON envelope). */
|
|
39
41
|
export const PROXY_BODY_LIMIT = 64 * 1024
|
|
40
42
|
/**
|
|
41
43
|
* The proxy fails (502) when the API answers more than this, and returns
|
|
42
44
|
* neither the status nor the start of the body: `limitedText()` throws
|
|
43
|
-
* "Backend response too large" past `RESPONSE_LIMIT` (tester.
|
|
44
|
-
* and
|
|
45
|
-
*
|
|
45
|
+
* "Backend response too large" past `RESPONSE_LIMIT` (`server/tester.ts`),
|
|
46
|
+
* and `testerRoute()` turns every error into the same 502 "DMS tester request
|
|
47
|
+
* failed", as for an API down. Only the
|
|
46
48
|
* capture of the call tells the two apart. For the DMS: return the status,
|
|
47
49
|
* the headers and the first 64 KiB with a `truncated: true` flag instead of
|
|
48
50
|
* throwing, and give the 502 a `reason` (`too_large`, `timeout`,
|
|
@@ -481,7 +483,7 @@ export async function sendTesterRequest(
|
|
|
481
483
|
try {
|
|
482
484
|
let response: Response
|
|
483
485
|
try {
|
|
484
|
-
response = await fetch(
|
|
486
|
+
response = await fetch(TESTER_ROUTE, {
|
|
485
487
|
method: 'POST',
|
|
486
488
|
headers: { 'content-type': 'application/json' },
|
|
487
489
|
credentials: 'same-origin',
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineDmsServer } from '#dms/server'
|
|
2
|
+
import { testerRoute } from './server/tester'
|
|
3
|
+
|
|
4
|
+
// The API tester's call forwarding: served by this module, under its own
|
|
5
|
+
// namespace. The frontend server has no tester route of its own, and answers
|
|
6
|
+
// the path's other methods with a 405 itself.
|
|
7
|
+
export default defineDmsServer((server) => {
|
|
8
|
+
server.registerRoute('POST', '/api/_dms-api/tester', testerRoute)
|
|
9
|
+
})
|