unjs / ofetch

😱 A better fetch API. Works on node, browser and workers.
MIT License
4.17k stars 129 forks source link

Support Request / Response progress #45

Open acidjazz opened 2 years ago

acidjazz commented 2 years ago

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?

bf commented 2 years ago

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

kissu commented 2 years ago

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

mohammedmoutawakkil commented 2 years ago

any news ?!

PhE commented 2 years ago

any news ?!

no progress :smile:

capoia commented 2 years ago

i need this too D:

nickchomey commented 1 year ago

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

4KDA commented 1 year ago

news?

Dmytro-Tihunov commented 1 year ago

Still nothing ?)

acidjazz commented 1 year ago

🙏 🙏 🤞 🤞

cdwmhcc commented 1 year ago

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

alirayaneh commented 1 year ago

is there any solution for this problem ?

ademyalcin27 commented 9 months ago

Is there any news about this ?

clinton9ice commented 8 months ago

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%.

sohaha commented 8 months ago

any news ?!

EduardoRocha234 commented 6 months ago

any news?

Megachill commented 4 months ago

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

meyt commented 4 months ago

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)
}
bradley-varol commented 2 months ago

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

EduardoRocha234 commented 2 months ago

My solution,

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

pi0 commented 2 months ago

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:

bradley-varol commented 2 months ago

@pi0 why is Axios considered "legacy"?

pi0 commented 2 months ago

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