api-client/components/http/Request.vue

291 lines
7.2 KiB
Vue
Raw Normal View History

2021-07-13 05:37:29 +00:00
<template>
2021-07-17 17:40:28 +00:00
<div class="bg-primary flex p-4 top-0 z-10 sticky">
2021-07-13 05:37:29 +00:00
<div class="relative inline-flex">
<span class="select-wrapper">
<tippy
ref="options"
interactive
tabindex="-1"
trigger="click"
theme="popover"
arrow
>
<template #trigger>
<input
id="method"
class="
bg-primaryLight
2021-07-17 17:40:28 +00:00
border border-divider
rounded-l
2021-07-17 17:40:28 +00:00
cursor-pointer
flex
2021-07-23 19:12:31 +00:00
font-semibold font-mono
2021-07-26 03:38:06 +00:00
h-8
2021-07-13 05:37:29 +00:00
text-secondaryDark
2021-07-26 03:38:06 +00:00
py-1
2021-07-17 17:40:28 +00:00
px-4
2021-07-13 05:37:29 +00:00
transition
2021-07-24 10:58:32 +00:00
w-28
2021-07-17 17:40:28 +00:00
truncate
2021-07-13 05:37:29 +00:00
focus:outline-none focus:border-accent
"
:value="newMethod$"
autofocus
2021-07-17 17:40:28 +00:00
readonly
2021-07-13 05:37:29 +00:00
/>
</template>
<SmartItem
2021-07-13 23:49:08 +00:00
v-for="(method, index) in methods"
2021-07-13 05:37:29 +00:00
:key="`method-${index}`"
2021-07-13 23:49:08 +00:00
:label="method"
2021-07-13 05:37:29 +00:00
class="font-mono"
@click.native="
2021-07-13 23:49:08 +00:00
updateMethod(method)
2021-07-13 05:37:29 +00:00
$refs.options.tippy().hide()
"
/>
</tippy>
</span>
</div>
<div class="flex-1 inline-flex">
<input
id="url"
v-model="newEndpoint$"
class="
2021-07-17 17:40:28 +00:00
bg-primaryLight
border border-divider
2021-07-26 03:38:06 +00:00
flex
2021-07-23 19:12:31 +00:00
font-semibold font-mono
2021-07-26 03:38:06 +00:00
flex-1
2021-07-13 05:37:29 +00:00
text-secondaryDark
2021-07-26 03:38:06 +00:00
py-1
2021-07-17 17:40:28 +00:00
px-4
2021-07-13 05:37:29 +00:00
transition
2021-07-17 17:40:28 +00:00
truncate
2021-07-13 05:37:29 +00:00
focus:outline-none focus:border-accent
"
name="url"
type="text"
spellcheck="false"
:placeholder="$t('url')"
@keyup.enter="newSendRequest()"
/>
</div>
<!-- <SmartUrlField v-else v-model="uri" /> -->
2021-07-13 05:37:29 +00:00
<div class="flex">
2021-07-17 17:40:28 +00:00
<ButtonPrimary
2021-07-13 05:37:29 +00:00
id="send"
class="rounded-none w-18"
:label="!loading ? $t('send') : $t('cancel')"
:shortcut="[getSpecialKey(), 'G']"
2021-07-17 17:40:28 +00:00
@click.native="!loading ? newSendRequest() : cancelRequest()"
/>
<span class="inline-flex">
<tippy
ref="sendOptions"
interactive
tabindex="-1"
trigger="click"
theme="popover"
arrow
>
<template #trigger>
2021-07-26 03:38:06 +00:00
<ButtonPrimary class="rounded-l-none" icon="keyboard_arrow_down" />
2021-07-17 17:40:28 +00:00
</template>
<SmartItem
:label="$t('import_curl')"
icon="import_export"
@click.native="
showCurlImportModal = !showCurlImportModal
$refs.sendOptions.tippy().hide()
2021-07-13 05:37:29 +00:00
"
2021-07-17 17:40:28 +00:00
/>
<SmartItem
:label="$t('show_code')"
icon="code"
@click.native="
showCodegenModal = !showCodegenModal
$refs.sendOptions.tippy().hide()
"
/>
<SmartItem
ref="clearAll"
:label="$t('clear_all')"
icon="clear_all"
@click.native="
clearContent()
2021-07-17 17:40:28 +00:00
$refs.sendOptions.tippy().hide()
"
/>
</tippy>
2021-07-13 05:37:29 +00:00
</span>
2021-07-17 17:40:28 +00:00
<ButtonSecondary
2021-07-26 03:38:06 +00:00
class="rounded-r-none h-8 ml-2"
2021-07-17 17:40:28 +00:00
:label="$t('save')"
:shortcut="[getSpecialKey(), 'S']"
2021-07-17 17:40:28 +00:00
outline
@click.native="showSaveRequestModal = true"
2021-07-17 17:40:28 +00:00
/>
<span class="inline-flex">
<tippy
ref="saveOptions"
interactive
tabindex="-1"
trigger="click"
theme="popover"
arrow
>
<template #trigger>
<ButtonSecondary
icon="keyboard_arrow_down"
outline
2021-07-26 03:38:06 +00:00
class="rounded-l-none h-8"
2021-07-17 17:40:28 +00:00
/>
</template>
<input
id="request-name"
v-model="requestName"
2021-07-24 05:53:10 +00:00
:placeholder="$t('request_name')"
2021-07-17 17:40:28 +00:00
name="request-name"
type="text"
2021-07-24 05:53:10 +00:00
class="mb-2 input"
2021-07-17 17:40:28 +00:00
/>
<SmartItem
ref="copyRequest"
:label="$t('copy_request_link')"
:icon="navigatorShare ? 'share' : 'content_copy'"
@click.native="
copyRequest()
$refs.saveOptions.tippy().hide()
2021-07-13 05:37:29 +00:00
"
2021-07-17 17:40:28 +00:00
/>
<SmartItem
ref="saveRequest"
:label="$t('save_to_collections')"
icon="create_new_folder"
@click.native="
showSaveRequestModal = true
2021-07-17 17:40:28 +00:00
$refs.saveOptions.tippy().hide()
"
/>
</tippy>
</span>
2021-07-13 05:37:29 +00:00
</div>
2021-07-18 04:33:52 +00:00
<HttpImportCurl
:show="showCurlImportModal"
@hide-modal="showCurlImportModal = false"
/>
<HttpCodegenModal
:show="showCodegenModal"
@hide-modal="showCodegenModal = false"
/>
<CollectionsSaveRequest
mode="rest"
:show="showSaveRequestModal"
@hide-modal="showSaveRequestModal = false"
/>
2021-07-13 05:37:29 +00:00
</div>
</template>
<script>
import { defineComponent } from "@nuxtjs/composition-api"
2021-07-13 05:37:29 +00:00
import {
updateRESTResponse,
restEndpoint$,
setRESTEndpoint,
restMethod$,
updateRESTMethod,
resetRESTRequest,
useRESTRequestName,
2021-07-13 05:37:29 +00:00
} from "~/newstore/RESTSession"
2021-07-17 17:40:28 +00:00
import { getPlatformSpecialKey } from "~/helpers/platformutils"
import { runRESTRequest$ } from "~/helpers/RequestRunner"
2021-07-13 05:37:29 +00:00
export default defineComponent({
setup() {
return {
requestName: useRESTRequestName(),
}
},
2021-07-13 05:37:29 +00:00
data() {
return {
newMethod$: "",
2021-07-13 23:49:08 +00:00
methods: [
2021-07-13 05:37:29 +00:00
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"CONNECT",
"OPTIONS",
"TRACE",
"PATCH",
"CUSTOM",
],
name: "",
newEndpoint$: "",
showCurlImportModal: false,
showCodegenModal: false,
navigatorShare: navigator.share,
2021-07-17 17:40:28 +00:00
loading: false,
showSaveRequestModal: false,
2021-07-13 05:37:29 +00:00
}
},
subscriptions() {
return {
newMethod$: restMethod$,
newEndpoint$: restEndpoint$,
}
},
watch: {
newEndpoint$(newVal) {
setRESTEndpoint(newVal)
},
},
methods: {
2021-07-17 17:40:28 +00:00
getSpecialKey: getPlatformSpecialKey,
2021-07-13 05:37:29 +00:00
updateMethod(method) {
updateRESTMethod(method)
},
newSendRequest() {
2021-07-17 17:40:28 +00:00
this.loading = true
2021-07-13 05:37:29 +00:00
this.$subscribeTo(
runRESTRequest$(),
2021-07-13 05:37:29 +00:00
(responseState) => {
console.log(responseState)
updateRESTResponse(responseState)
2021-07-21 21:43:15 +00:00
},
() => {
this.loading = false
},
() => {
2021-07-17 17:40:28 +00:00
this.loading = false
2021-07-13 05:37:29 +00:00
}
)
},
copyRequest() {
if (navigator.share) {
const time = new Date().toLocaleTimeString()
const date = new Date().toLocaleDateString()
navigator
.share({
title: "Hoppscotch",
text: `Hoppscotch • Open source API development ecosystem at ${time} on ${date}`,
url: window.location.href,
})
.then(() => {})
.catch(() => {})
} else {
this.$clipboard(window.location.href)
this.$toast.info(this.$t("copied_to_clipboard"), {
icon: "done",
})
}
},
clearContent() {
resetRESTRequest()
},
2021-07-13 05:37:29 +00:00
},
})
2021-07-13 05:37:29 +00:00
</script>