-
Notifications
You must be signed in to change notification settings - Fork 153
/
ServerRouteDetails.vue
610 lines (558 loc) · 17.8 KB
/
ServerRouteDetails.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<script setup lang="ts">
import JsonEditorVue from 'json-editor-vue'
import { createReusableTemplate, watchDebounced } from '@vueuse/core'
import type { $Fetch } from 'ofetch'
import type { CodeSnippet, ServerRouteInfo, ServerRouteInput } from '~/../src/types'
const props = defineProps<{
route: ServerRouteInfo
}>()
const emit = defineEmits<{
(event: 'open-default-input'): void
}>()
const [DefineDefaultInputs, UseDefaultInputs] = createReusableTemplate()
const config = useServerConfig()
const client = useClient()
const response = reactive({
contentType: 'text/plain',
data: '' as any,
statusCode: 200,
error: undefined as Error | undefined,
fetchTime: 0,
})
const responseLang = computed(() => {
if (response.contentType.includes('application/json'))
return 'json'
if (response.contentType.includes('text/html'))
return 'html'
if (response.contentType.includes('text/css'))
return 'css'
if (response.contentType.includes('text/javascript'))
return 'javascript'
if (response.contentType.includes('image') || response.contentType.includes('video'))
return 'media'
if (response.contentType.includes('text/xml') || response.contentType.includes('application/xml'))
return 'xml'
if (response.contentType.includes('application/pdf'))
return 'pdf'
return 'text'
})
const responseContent = computed(() => {
if (responseLang.value === 'json')
return JSON.stringify(response.data, null, 2)
if (responseLang.value === 'media' || responseLang.value === 'pdf') {
const blob = new Blob([response.data], { type: response.contentType })
return URL.createObjectURL(blob)
}
return response.data
})
const fetching = ref(false)
const started = ref(false)
const openInEditor = useOpenInEditor()
const parsedRoute = computed(() => props.route.route?.split(/((?:\*\*)?:[\w_]+)/g))
const paramNames = computed(() => parsedRoute.value?.filter(i => i.startsWith(':') || i.startsWith('**:')) || [])
const routeMethod = ref(props.route.method || 'GET')
const routeParams = ref<{ [key: string]: string }>({})
const routeInputs = reactive({
query: [{ active: true, key: '', value: '', type: 'string' }] as ServerRouteInput[],
body: [{ active: true, key: '', value: '', type: 'string' }] as ServerRouteInput[],
headers: [{ active: true, key: 'Content-Type', value: 'application/json', type: 'string' }] as ServerRouteInput[],
})
const routeInputBodyJSON = ref<any>({})
const {
inputDefaults,
sendFrom,
} = useDevToolsOptions('serverRoutes')
const resolvedSendFrom = computed(() => {
if (!client?.value?.app?.$fetch)
return 'devtools'
return sendFrom.value
})
const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD']
// https://github.com/unjs/h3/blob/main/src/utils/body.ts#L19
const bodyPayloadMethods = ['PATCH', 'POST', 'PUT', 'DELETE']
const hasBody = computed(() => bodyPayloadMethods.includes(routeMethod.value.toUpperCase()))
const activeTab = ref()
const tabInputs = ['input', 'json']
const selectedTabInput = ref(tabInputs[0])
// TODO: fix routeInputs[activeTab.value] type
type RouteInputs = keyof typeof routeInputs
const currentParams = computed({
get: () => routeInputs[activeTab.value as RouteInputs],
set: (value: any) => {
routeInputs[activeTab.value as RouteInputs] = value
},
})
const parsedQuery = computed(() => {
return {
...parseInputs(inputDefaults.value.query),
...parseInputs(routeInputs.query),
}
})
const parsedHeader = computed(() => {
return {
...parseInputs(inputDefaults.value.headers),
...parseInputs(routeInputs.headers),
}
})
const parsedBody = computed(() => {
return hasBody.value
? selectedTabInput.value === 'json'
? {
...parseInputs(inputDefaults.value.body),
...routeInputBodyJSON.value,
}
: {
...parseInputs(inputDefaults.value.body),
...parseInputs(routeInputs.body),
}
: undefined
})
const domain = computed(() => {
let url = window?.location.origin
if (url.charAt(url.length - 1) === '/')
url = url.slice(0, -1)
return url
})
const finalPath = computed(() => {
let query = new URLSearchParams(parsedQuery.value).toString()
if (query)
query = `?${query}`
const path = (parsedRoute.value?.map((i) => {
if (i.startsWith(':') || i.startsWith('**:'))
return routeParams.value[i] || i
return i
}).join('') || '') + query
let base = config.value?.app.baseURL || ''
if (base === './' || base === '.')
base = ''
if (base.endsWith('/'))
base = base.slice(0, -1)
return (base + path)
})
const finalURL = computed(() => domain.value + finalPath.value)
function parseInputs(inputs: ServerRouteInput[]) {
const formatted = Object.fromEntries(
inputs.filter(({ active, key, value }) => active && key && value !== undefined).map(({ key, value }) => [key, value]),
)
return Object.entries(formatted).length ? formatted : undefined
}
async function fetchData() {
started.value = true
fetching.value = true
const start = Date.now()
const f = resolvedSendFrom.value === 'app'
? client.value!.app!.$fetch
: $fetch as $Fetch
telemetry('server-routes:fetch', {
method: routeMethod.value,
sendFrom: resolvedSendFrom.value,
})
try {
response.data = await f(finalURL.value, {
method: routeMethod.value.toUpperCase() as any,
headers: parsedHeader.value,
query: parsedQuery.value,
body: parsedBody.value,
onResponse({ response: res }) {
response.contentType = (res.headers.get('content-type') || '').toString().toLowerCase().trim()
response.statusCode = res.status
response.error = undefined
},
onResponseError(res) {
response.error = res.response._data
response.data = res.response._data
},
})
}
catch (err: any) {
}
fetching.value = false
response.fetchTime = Date.now() - start
}
const codeSnippets = computed(() => {
const snippets: CodeSnippet[] = []
const items: string[] = []
const headers = Object.entries(parsedHeader.value)
.filter(([key, value]) => key && value && !(key === 'Content-Type' && value === 'application/json'))
.map(([key, value]) => ` '${key}': '${value}'`).join(',\n')
if (routeMethod.value.toUpperCase() !== 'GET')
items.push(`method: '${routeMethod.value.toUpperCase()}'`)
if (headers)
items.push(`headers: {\n${headers}\n}`)
if (parsedBody.value)
items.push(`body: ${JSON.stringify(parsedBody.value, null, 2)}`)
const options = items.length
? `, {
${items.join(',\n').split('\n').map(line => ` ${line}`).join('\n')}
}`
: ''
snippets.push({
name: 'useFetch',
lang: 'javascript',
docs: ComposablesDocs.nuxt.useFetch,
code: `const { data, pending, error, refresh } = useFetch('${finalPath.value}'${options})`,
})
snippets.push({
name: '$fetch',
lang: 'javascript',
docs: ComposablesDocs.nuxt.$fetch,
code: `await $fetch('${finalPath.value}'${options})`,
})
return snippets
})
const cookies = ref(getCookies())
const newCookie = reactive({ key: '', value: '' })
const tabs = computed(() => {
const items = []
if (paramNames.value.length) {
items.push({
name: 'Params',
slug: 'params',
length: paramNames.value.length,
})
}
items.push({
name: 'Query',
slug: 'query',
length: routeInputs.query.length,
})
if (hasBody.value) {
items.push({
name: 'Body',
slug: 'body',
length: routeInputs.body.length,
})
}
items.push({
name: 'Headers',
slug: 'headers',
length: routeInputs.headers.length,
})
items.push({
name: 'Cookies',
slug: 'cookies',
length: cookies.value.length,
})
items.push({
name: 'Snippets',
slug: 'snippet',
})
return items
})
function getCookies() {
return document.cookie.split('; ').map((i) => {
const [key, value] = i.split('=')
return { key, value }
}).filter(i => i.key)
}
function updateCookie(key: string, value: any) {
if (!key)
return
const exist = cookies.value.find(cookie => cookie.key === key)
const cookie = useCookie(key)
if (exist !== undefined) {
if (value === undefined)
cookies.value = cookies.value.filter(cookie => cookie.key !== key)
}
else {
cookies.value.push({ key, value })
newCookie.key = ''
newCookie.value = ''
}
cookie.value = value
}
watchEffect(() => {
if (selectedTabInput.value === 'json') {
if (typeof routeInputBodyJSON.value === 'string')
routeInputBodyJSON.value = JSON.parse(routeInputBodyJSON.value)
}
})
const savedRouteInputs = useLocalStorage<{ path: string, tab: string, inputs: any }[]>('nuxt-devtools:server-routes:inputs', () => [], {
window: window.parent,
})
watchDebounced([routeInputs, activeTab], () => {
const savedEntry = savedRouteInputs.value?.find((entry: any) => entry.path === props.route.filepath)
if (!savedEntry) {
const newEntry = {
path: props.route.filepath,
tab: paramNames.value.length ? 'params' : 'query',
inputs: {
...routeInputs,
...(paramNames.value.length ? { params: routeParams.value } : {}),
},
}
savedRouteInputs.value.push(newEntry)
if (!activeTab.value)
activeTab.value = newEntry.tab
}
else {
if (!activeTab.value)
activeTab.value = savedEntry.tab
if (savedEntry.tab !== activeTab.value)
savedEntry.tab = activeTab.value
// update routeInputs with local storage
const { body, query, headers, params } = savedEntry.inputs
Object.assign(routeInputs, { body, query, headers })
routeParams.value = params
}
}, { immediate: true, deep: true, debounce: 500 })
function clearSavedCache() {
savedRouteInputs.value = []
routeInputs.body = []
routeInputs.query = []
routeInputs.headers = []
routeParams.value = {}
activeTab.value = paramNames.value.length ? 'params' : 'query'
}
const copy = useCopy()
</script>
<template>
<div h-full w-full flex="~ col">
<div flex="~ col gap-2" flex-none p4 n-navbar-glass>
<div flex="~ gap2 items-center">
<NButton
v-if="route.method"
class="n-badge-base n-sm"
:class="getRequestMethodClass(routeMethod)"
pointer-events-none font-mono tabindex="-1"
>
{{ routeMethod.toUpperCase() }}
</NButton>
<NSelect
v-else
v-model="routeMethod"
class="n-badge-base n-sm"
:class="getRequestMethodClass(routeMethod)"
>
<option v-for="method of methods" :key="method" :class="getRequestMethodClass(method)">
{{ method.toUpperCase() }}
</option>
</NSelect>
<div relative w-full>
<NTextInput
:model-value="finalPath"
readonly flex-auto font-mono
p="x5 y2"
n="sm"
/>
<div absolute right-2 top-1.5 flex="~ gap-1">
<NButton
v-tooltip="'Copy URL'"
title="Copy URL"
n="xs blue"
icon="carbon:copy"
:border="false"
@click="copy(finalURL, 'server-route-url')"
/>
<NButton
v-tooltip="'Open in Editor'"
title="Open in Editor"
icon="carbon-launch"
n="xs blue"
:border="false"
@click="openInEditor(route.filepath)"
/>
</div>
</div>
<NButton h-full n="primary solid" @click="fetchData">
<NIcon icon="carbon:send" />
</NButton>
</div>
</div>
<div flex="~ gap2 wrap" w-full items-center px4 pb2 text-center text-sm border="b base">
<NButton
v-for="tab of tabs"
:key="tab.slug"
v-tooltip="tab.name"
:class="activeTab === tab.slug ? 'text-primary n-primary' : 'border-transparent shadow-none'"
@click="activeTab = tab.slug"
>
<NIcon :icon="ServerRouteTabIcons[tab.slug]" />
<div class="hidden md:block">
{{ tab.name }}
{{ tab?.length ? `(${tab.length})` : '' }}
<span>
{{ inputDefaults[tab.slug]?.length ? `(${inputDefaults[tab.slug].length})` : '' }}
</span>
</div>
</NButton>
<div flex-auto />
<div text-xs op50>
Send from
</div>
<NSelect
v-model="resolvedSendFrom"
class="n-xs"
:disabled="!client?.app?.$fetch"
>
<option value="app">
App
</option>
<option value="devtools">
DevTools
</option>
</NSelect>
<NButton v-tooltip="'Clear Inputs Saved Cache'" n="orange" class="p-3" icon="i-carbon-clean" @click="clearSavedCache" />
</div>
<div
v-if="activeTab === 'params'"
border="b base" items-center px4 py2
grid="~ cols-[max-content_1fr] gap-2"
>
<template v-for="name in paramNames" :key="name">
<div text-right font-mono>
{{ name }}
</div>
<NTextInput
v-model="routeParams[name]"
:placeholder="name"
flex-1
/>
</template>
</div>
<div
v-if="activeTab === 'cookies'"
border="b base" p4 flex="~ col gap-4" font-mono
>
<div v-for="cookie in cookies" :key="cookie.key" flex="~ gap-4 items-center">
<NTextInput
placeholder="Key..."
:model-value="cookie.key"
disabled op-70
/>
<NTextInput
placeholder="Value..."
:model-value="cookie.value"
flex-1 n="primary"
@input="updateCookie(cookie.key, $event.target?.value)"
/>
<NButton title="Delete" n="red" @click="updateCookie(cookie.key, undefined)">
<NIcon icon="i-carbon-trash-can" />
</NButton>
</div>
<div flex="~ gap-4">
<NTextInput
v-model="newCookie.key"
placeholder="Key"
n="primary" flex-1
/>
<NTextInput
v-model="newCookie.value"
placeholder="Value"
n="primary" flex-1
/>
<NButton title="Add" n="primary" @click="updateCookie(newCookie.key, newCookie.value)">
<NIcon icon="i-carbon-save" />
</NButton>
</div>
</div>
<DefineDefaultInputs>
<ServerRouteInputs v-model="currentParams" :default="{ active: true, type: 'string' }" max-h-xs of-auto>
<template v-if="inputDefaults[activeTab]?.length">
<div flex="~ gap2" mb--2 items-center op50>
<div w-5 x-divider />
<div flex-none>
Default Inputs
</div>
<NButton
icon="i-carbon-edit"
:border="false"
@click="emit('open-default-input')"
/>
<div x-divider />
</div>
<ServerRouteInputs v-model="inputDefaults[activeTab]" disabled p0 />
</template>
</ServerRouteInputs>
</DefineDefaultInputs>
<div v-if="activeTab === 'snippet'">
<CodeSnippets
v-if="codeSnippets.length"
:code-snippets="codeSnippets"
/>
</div>
<div v-else-if="currentParams" border="b base" relative n-code-block>
<template v-if="activeTab === 'body'">
<div flex="~ wrap" w-full>
<template v-for="item of tabInputs" :key="item">
<button
px4 py2 border="r base"
hover="bg-active"
:class="{ 'border-b': item !== selectedTabInput }"
@click="selectedTabInput = item"
>
<div :class="{ op30: item !== selectedTabInput } " font-mono>
{{ item }}
</div>
</button>
</template>
<div border="b base" flex-auto />
</div>
<UseDefaultInputs v-if="selectedTabInput === 'input'" />
<JsonEditorVue
v-else-if="selectedTabInput === 'json'"
v-model="routeInputBodyJSON"
:class="[$colorMode.value === 'dark' ? 'jse-theme-dark' : 'light']"
class="json-editor-vue of-auto text-sm outline-none"
v-bind="$attrs" mode="text" :navigation-bar="false" :indentation="2" :tab-size="2"
/>
</template>
<UseDefaultInputs v-else />
</div>
<NPanelGrids v-if="!started">
<NButton n="primary" @click="fetchData">
<NIcon icon="carbon:send" />
Send request
</NButton>
</NPanelGrids>
<NLoading v-else-if="fetching" z-10 flex-auto backdrop-blur>
Fetching...
</NLoading>
<template v-else>
<div border="b base" flex="~ gap2" items-center px4 py2>
<div>Response</div>
<NBadge
v-if="response.error"
n="red"
>
Error
</NBadge>
<NBadge :n="response.error ? 'orange' : 'green'" v-text="response.statusCode" />
<code v-if="response.contentType" text-xs op50>
{{ response.contentType }}
</code>
<DataSchemaButton
v-if="response.contentType === 'application/json'"
:getter="() => ({ input: responseContent })"
/>
<div flex-auto />
<div op50>
Request finished in
</div>
<NBadge n="green">
{{ response.fetchTime }} ms
</NBadge>
</div>
<div v-if="responseLang === 'pdf'" flex-auto overflow-auto>
<div border="~ base" h-full w-full rounded>
<object :data="responseContent" type="application/pdf" flex-auto width="100%" height="100%" rounded />
</div>
</div>
<!-- Rich response data -->
<NCodeBlock
v-else-if="responseLang !== 'media'"
flex-auto overflow-auto py-2
:code="responseContent"
:lang="responseLang"
/>
<div v-else flex-auto overflow-auto p4>
<div border="~ base" rounded>
<img v-if="response.contentType.includes('image')" rounded :src="responseContent">
<video v-else controls rounded>
<source :src="responseContent" type="video/mp4">
</video>
</div>
</div>
</template>
</div>
</template>