ResponseMeta.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <div
  3. class="sticky top-0 z-10 flex items-start p-4 overflow-auto bg-primary hide-scrollbar whitespace-nowrap"
  4. >
  5. <div
  6. v-if="response == null"
  7. class="flex flex-col items-center justify-center flex-1 text-secondaryLight"
  8. >
  9. <div class="flex pb-4 my-4 space-x-2">
  10. <div class="flex flex-col items-end text-right space-y-4">
  11. <span class="flex items-center flex-1">
  12. {{ t("shortcut.request.send_request") }}
  13. </span>
  14. <span class="flex items-center flex-1">
  15. {{ t("shortcut.general.show_all") }}
  16. </span>
  17. <span class="flex items-center flex-1">
  18. {{ t("shortcut.general.command_menu") }}
  19. </span>
  20. <span class="flex items-center flex-1">
  21. {{ t("shortcut.general.help_menu") }}
  22. </span>
  23. </div>
  24. <div class="flex flex-col space-y-4">
  25. <div class="flex">
  26. <span class="shortcut-key">{{ getSpecialKey() }}</span>
  27. <span class="shortcut-key">G</span>
  28. </div>
  29. <div class="flex">
  30. <span class="shortcut-key">{{ getSpecialKey() }}</span>
  31. <span class="shortcut-key">K</span>
  32. </div>
  33. <div class="flex">
  34. <span class="shortcut-key">/</span>
  35. </div>
  36. <div class="flex">
  37. <span class="shortcut-key">?</span>
  38. </div>
  39. </div>
  40. </div>
  41. <ButtonSecondary
  42. :label="t('app.documentation')"
  43. to="https://docs.hoppscotch.io/features/response"
  44. svg="external-link"
  45. blank
  46. outline
  47. reverse
  48. />
  49. </div>
  50. <div v-else class="flex flex-col flex-1">
  51. <div
  52. v-if="response.type === 'loading'"
  53. class="flex flex-col items-center justify-center"
  54. >
  55. <SmartSpinner class="my-4" />
  56. <span class="text-secondaryLight">{{ t("state.loading") }}</span>
  57. </div>
  58. <div
  59. v-if="response.type === 'network_fail'"
  60. class="flex flex-col items-center justify-center flex-1 p-4"
  61. >
  62. <img
  63. :src="`/images/states/${$colorMode.value}/youre_lost.svg`"
  64. loading="lazy"
  65. class="inline-flex flex-col object-contain object-center w-32 h-32 my-4"
  66. :alt="`${t('error.network_fail')}`"
  67. />
  68. <span class="mb-2 font-semibold text-center">
  69. {{ t("error.network_fail") }}
  70. </span>
  71. <span
  72. class="max-w-sm mb-6 text-center whitespace-normal text-secondaryLight"
  73. >
  74. {{ t("helpers.network_fail") }}
  75. </span>
  76. <AppInterceptor class="border rounded border-dividerLight" />
  77. </div>
  78. <div
  79. v-if="response.type === 'script_fail'"
  80. class="flex flex-col items-center justify-center flex-1 p-4"
  81. >
  82. <img
  83. :src="`/images/states/${$colorMode.value}/youre_lost.svg`"
  84. loading="lazy"
  85. class="inline-flex flex-col object-contain object-center w-32 h-32 my-4"
  86. :alt="`${t('error.script_fail')}`"
  87. />
  88. <span class="mb-2 font-semibold text-center">
  89. {{ t("error.script_fail") }}
  90. </span>
  91. <span
  92. class="max-w-sm mb-6 text-center whitespace-normal text-secondaryLight"
  93. >
  94. {{ t("helpers.script_fail") }}
  95. </span>
  96. <div
  97. class="w-full px-4 py-2 overflow-auto font-mono text-red-400 whitespace-normal rounded bg-primaryLight"
  98. >
  99. {{ response.error.name }}: {{ response.error.message }}<br />
  100. {{ response.error.stack }}
  101. </div>
  102. </div>
  103. <div
  104. v-if="response.type === 'success' || response.type === 'fail'"
  105. class="flex items-center font-semibold text-tiny"
  106. >
  107. <div
  108. :class="statusCategory.className"
  109. class="inline-flex flex-1 space-x-4"
  110. >
  111. <span v-if="response.statusCode">
  112. <span class="text-secondary"> {{ t("response.status") }}: </span>
  113. {{ `${response.statusCode}\xA0 • \xA0`
  114. }}{{ getStatusCodeReasonPhrase(response.statusCode) }}
  115. </span>
  116. <span v-if="response.meta && response.meta.responseDuration">
  117. <span class="text-secondary"> {{ t("response.time") }}: </span>
  118. {{ `${response.meta.responseDuration} ms` }}
  119. </span>
  120. <span
  121. v-if="response.meta && response.meta.responseSize"
  122. v-tippy="
  123. readableResponseSize
  124. ? { theme: 'tooltip' }
  125. : { onShow: () => false }
  126. "
  127. :title="`${response.meta.responseSize} B`"
  128. >
  129. <span class="text-secondary"> {{ t("response.size") }}: </span>
  130. {{
  131. readableResponseSize
  132. ? readableResponseSize
  133. : `${response.meta.responseSize} B`
  134. }}
  135. </span>
  136. </div>
  137. </div>
  138. </div>
  139. </div>
  140. </template>
  141. <script setup lang="ts">
  142. import { computed } from "@nuxtjs/composition-api"
  143. import findStatusGroup from "~/helpers/findStatusGroup"
  144. import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  145. import { getPlatformSpecialKey as getSpecialKey } from "~/helpers/platformutils"
  146. import { useI18n } from "~/helpers/utils/composables"
  147. import { getStatusCodeReasonPhrase } from "~/helpers/utils/statusCodes"
  148. const t = useI18n()
  149. const props = defineProps<{
  150. response: HoppRESTResponse
  151. }>()
  152. /**
  153. * Gives the response size in a human readable format
  154. * (changes unit from B to MB/KB depending on the size)
  155. * If no changes (error res state) or value can be made (size < 1KB ?),
  156. * it returns undefined
  157. */
  158. const readableResponseSize = computed(() => {
  159. if (
  160. props.response.type === "loading" ||
  161. props.response.type === "network_fail" ||
  162. props.response.type === "script_fail" ||
  163. props.response.type === "fail"
  164. )
  165. return undefined
  166. const size = props.response.meta.responseSize
  167. if (size >= 100000) return (size / 1000000).toFixed(2) + " MB"
  168. if (size >= 1000) return (size / 1000).toFixed(2) + " KB"
  169. return undefined
  170. })
  171. const statusCategory = computed(() => {
  172. if (
  173. props.response.type === "loading" ||
  174. props.response.type === "network_fail" ||
  175. props.response.type === "script_fail" ||
  176. props.response.type === "fail"
  177. )
  178. return {
  179. name: "error",
  180. className: "text-red-500",
  181. }
  182. return findStatusGroup(props.response.statusCode)
  183. })
  184. </script>