useResponseBody.ts 779 B

12345678910111213141516171819202122232425
  1. import { computed, ComputedRef } from "@nuxtjs/composition-api"
  2. import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  3. export default function useResponseBody(response: HoppRESTResponse): {
  4. responseBodyText: ComputedRef<string>
  5. } {
  6. const responseBodyText = computed(() => {
  7. if (
  8. response.type === "loading" ||
  9. response.type === "network_fail" ||
  10. response.type === "script_fail" ||
  11. response.type === "fail"
  12. )
  13. return ""
  14. if (typeof response.body === "string") return response.body
  15. else {
  16. const res = new TextDecoder("utf-8").decode(response.body)
  17. // HACK: Temporary trailing null character issue from the extension fix
  18. return res.replace(/\0+$/, "")
  19. }
  20. })
  21. return {
  22. responseBodyText,
  23. }
  24. }