ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

Support Request / Response progress

Open acidjazz opened this issue 3 years ago • 22 comments

I know fetch itself didn't have it for a while, last I recall it was coming in 2020 with some command like

allowHTTP1ForStreamingUpload

Do we have any recommendations on what people might want to use for nuxt3?

acidjazz avatar Dec 25 '21 05:12 acidjazz

I have same issue in nuxt3. Is there any way to show a progress bar for file uploads with ohmyfetch?

bf avatar Aug 31 '22 16:08 bf

Small bump here on demand of this question: https://stackoverflow.com/q/73811890/8816585

kissu avatar Sep 22 '22 09:09 kissu

any news ?!

mohammedmoutawakkil avatar Oct 29 '22 20:10 mohammedmoutawakkil

any news ?!

no progress :smile:

PhE avatar Nov 02 '22 18:11 PhE

i need this too D:

capoia avatar Nov 09 '22 18:11 capoia

It seems this should now be possible with Chromium versions > 105. It would be cool if it could be integrated into ofetch!

https://developer.chrome.com/articles/fetch-streaming-requests/#streaming-request-bodies fetch() upload streaming - Chrome Platform Status (chromestatus.com)

Here's an example implementation: https://stackoverflow.com/a/52860605/19510854

nickchomey avatar Dec 01 '22 13:12 nickchomey

news?

4KDA avatar Jul 08 '23 10:07 4KDA

Still nothing ?)

Dmytro-Tihunov avatar Oct 27 '23 10:10 Dmytro-Tihunov

🙏 🙏 🤞 🤞

acidjazz avatar Oct 28 '23 00:10 acidjazz

https://ladjs.github.io/superagent/#progress-tracking

cdwmhcc avatar Nov 07 '23 12:11 cdwmhcc

is there any solution for this problem ?

alirayaneh avatar Nov 22 '23 10:11 alirayaneh

Is there any news about this ?

ademyalcin27 avatar Feb 20 '24 22:02 ademyalcin27

Has any solution been provided on this yet? I have read few stuff around it on the solution chrome provided using the ReadeableStream api but it's still not recommendable 100%.

clinton9ice avatar Mar 09 '24 18:03 clinton9ice

any news ?!

sohaha avatar Mar 12 '24 09:03 sohaha

any news?

EduardoRocha234 avatar May 24 '24 18:05 EduardoRocha234

+1 can we please get this? We had this for ages with axios and now its been taken while switching to NUXT3 🙈🤷‍♂️

Megachill avatar Jul 17 '24 16:07 Megachill

My solution,

Create composables/useUploader.js:


export function useUploader() {
  const config = useRuntimeConfig()
  const baseUrl = config.public.apiBase
  const authCookieName = 'THEAUTHCOOKIE'
  
  return {
    upload(url, form, onProgress) {
      return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('POST', baseUrl + url, true)

        // authorization
        const token = useCookie(authCookieName)
        if (token.value) {
          xhr.setRequestHeader('Authorization', `Token ${token.value}`)
        }

        xhr.upload.onprogress = function (event) {
          if (!event.lengthComputable) return
          onProgress((event.loaded / event.total) * 100)
        }
        xhr.onload = function () {
          if (xhr.status >= 200 && xhr.status < 300) {
            try {
              resolve(JSON.parse(xhr.responseText))
            } catch (error) {
              reject(new Error('Failed to parse response as JSON'))
            }
          } else {
            reject(new Error(`Upload failed with status: ${xhr.status}`))
          }
        }
        xhr.onerror = function () {
          reject(new Error('Upload failed due to a network error'))
        }
        xhr.send(form)
      })
    }
  }
}

and use it like:

const uploader = useUploader()
const video = ref(null)

async function submit() {
  const formData = new FormData()
  const onProgress = v => console.log('Upload progress', v)
  formData.append('file', video)
  const result = await uploader.upload('/media', formData, onProgress)
  console.log(result)
}

meyt avatar Jul 27 '24 17:07 meyt

Really frustrating that Nuxt switched from axios to ofetch, despite ofetch seemingly lacking feature parity... 🤯

bradley-varol avatar Sep 20 '24 17:09 bradley-varol

My solution,

https://stackoverflow.com/a/78849307/22387795

EduardoRocha234 avatar Sep 20 '24 23:09 EduardoRocha234

I appreciate the responses and solutions and understand it is a common requirement in frontend applications to track request/response progress when making HTTP calls but please do not respond with repetitive responses, it is an open issue.

ofetch is based on fetch API that is natively supported in both modern servers and browser runtimes but sadly, the standard spec does not allow tracking requests and responses (it is not a limitation of ofetch.)

There are currently two solutions:

  • Using an alternative method for making HTTP requests (XHR in browsers / HTTP module in node)
  • Using native fetch and wrapper around request and response streams as proposed in #277 ( request streaming is not possible in all browsers/http servers).

pi0 avatar Sep 23 '24 09:09 pi0

@pi0 why is Axios considered "legacy"?

bradley-varol avatar Sep 23 '24 10:09 bradley-varol

Dear @bradley-varol I would be happy to answer this, but it is out of this issue's context. replying notifies 20+ people.

pi0 avatar Sep 23 '24 10:09 pi0