-
Notifications
You must be signed in to change notification settings - Fork 153
/
ServerTaskDetails.vue
357 lines (321 loc) · 10 KB
/
ServerTaskDetails.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
<script setup lang="ts">
import JsonEditorVue from 'json-editor-vue'
import { createReusableTemplate } from '@vueuse/core'
import type { $Fetch } from 'ofetch'
import type { ServerRouteInput, ServerTaskInfo } from '~/../src/types'
const props = defineProps<{
task: ServerTaskInfo
}>()
const emit = defineEmits<{
(event: 'open-default-input'): void
}>()
const routeInputBodyJSON = ref<any>({ payload: {} })
const { inputDefaults } = useDevToolsOptions('serverRoutes')
const [DefineDefaultInputs, UseDefaultInputs] = createReusableTemplate()
const config = useServerConfig()
const response = reactive({
contentType: 'text/plain',
data: '' as any,
statusCode: 200,
error: undefined as Error | undefined,
fetchTime: 0,
})
const responseContent = computed(() => {
return JSON.stringify(response.data, null, 2)
})
const responseLang = computed(() => {
if (response.contentType.includes('application/json'))
return 'json'
return 'text'
})
const fetching = ref(false)
const started = ref(false)
const openInEditor = useOpenInEditor()
const activeTab = ref()
const tabInputs = ['json']
const selectedTabInput = ref(tabInputs[0])
const routeInputs = reactive({
query: [{ active: true, key: '', value: '', type: 'string' }] as ServerRouteInput[],
body: [{ active: true, key: '', value: '', type: 'string' }] as ServerRouteInput[],
headers: [] as ServerRouteInput[],
})
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),
}
})
const parsedBody = computed(() => {
return Object.keys(routeInputBodyJSON.value?.payload ?? {}).length
? {
...routeInputBodyJSON.value,
}
: undefined
})
const method = computed(() => {
return parsedBody.value ? 'POST' : 'GET'
})
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 = `/_nitro/tasks/${props.task.name}${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()
telemetry('server-tasks:run', {
method: method.value,
})
try {
response.data = await ($fetch as $Fetch)(finalURL.value, {
method: 'POST', // 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 tabs = computed(() => {
const items = []
items.push({
name: 'Query',
slug: 'query',
length: routeInputs.query.length,
})
items.push({
name: 'Body',
slug: 'body',
length: routeInputs.body.length,
})
return items
})
watchEffect(() => {
if (selectedTabInput.value === 'json') {
if (typeof routeInputBodyJSON.value === 'string') {
try {
routeInputBodyJSON.value = JSON.parse(routeInputBodyJSON.value)
}
catch {}
}
}
})
const savedRouteInputs = useLocalStorage<{ task: string, tab: string, inputs: any }[]>('nuxt-devtools:server-tasks:inputs', () => [], {
window: window.parent,
})
watchDebounced([routeInputs, activeTab], () => {
const savedEntry = savedRouteInputs.value?.find(entry => entry.task === props.task.name)
if (!savedEntry) {
const newEntry = {
task: props.task.name,
tab: 'query',
inputs: routeInputs,
}
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 } = savedEntry.inputs
Object.assign(routeInputs, { body, query, headers })
}
}, { immediate: true, deep: true, debounce: 500 })
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">
<div v-tooltip="`Method is ${method} as ${method === 'GET' ? 'no' : ''} json body is sent`">
<NButton
class="n-badge-base n-sm"
:class="getRequestMethodClass(method)" pointer-events-none font-mono
tabindex="-1"
>
{{ method }}
</NButton>
</div>
<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-task-url')"
/>
<NButton
v-tooltip="'Open in Editor'"
title="Open in Editor"
icon="carbon-launch"
n="xs blue"
:border="false"
@click="openInEditor(task.handler)"
/>
</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>
<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="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" />
Run task
</NButton>
</NPanelGrids>
<NLoading v-else-if="fetching" z-10 flex-auto backdrop-blur>
Running...
</NLoading>
<template v-else>
<div border="b base" flex="~ gap2" items-center px4 py2>
<div>Result</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>
<div flex-auto />
<div op50>
Tasks finished in
</div>
<NBadge n="green">
{{ response.fetchTime }} ms
</NBadge>
</div>
<NCodeBlock
flex-auto overflow-auto py-2
:code="responseContent"
:lang="responseLang"
/>
</template>
</div>
</template>