tus / tus-js-client

A pure JavaScript client for the tus resumable upload protocol
https://tus.io/
MIT License
2.11k stars 316 forks source link

Add an HttpStack for Axios #570

Open SpecLad opened 1 year ago

SpecLad commented 1 year ago

Is your feature request related to a problem? Please describe. In our project we mostly use Axios for making HTTP queries, with some default settings configured globally (e.g. default HTTP headers). We also use tus-js-client, but since it uses its own HTTP stack, we have to replicate the default Axios settings into their tus-js-client equivalents. This causes code duplication, and it's easy to forget to replicate a setting, which leads to divergent behavior.

Describe the solution you'd like It would be nice if I could write something like this and have tus-js-client work through Axios:

import Axios from 'axios';

const upload = new tus.Upload(file, { httpStack: tus.AxiosHttpStack(Axios), ... });

// or
const axiosInstance = Axios.create({ ... });
const upload = new tus.Upload(file, { httpStack: tus.AxiosHttpStack(axiosInstance ), ... });

That way, all Axios settings would automatically be applied.

Describe alternatives you've considered I'm aware that I could just implement this AxiosHttpStack on my side. However, Axios is a popular library, so I think many users could find this useful if it was included in tus-js-client.

Can you provide help with implementing this feature? In principle, yes, although I don't know when I'll be able to get around to it.

Additional context N/A

Acconut commented 1 year ago

What config settings in Axios could be set that way? From https://axios-http.com/docs/config_defaults, I feel like only a base URL and headers can be configured.

In general, I do see a use for this, but don't think we will have time ourselves to work on this. So if anybody else is interested, we are happy to assist.

SpecLad commented 1 year ago

There's a bunch of stuff: https://axios-http.com/docs/req_config, https://axios-http.com/docs/interceptors.

Acconut commented 1 year ago

Ok, then let's see if somebody wants to tackle this.

ArunBohra12 commented 1 year ago

Hello, @Acconut. I would love to take this issue and work on it.

Acconut commented 1 year ago

That's great to hear! Let me know if you need some help getting started. It should be as simple as copying https://github.com/tus/tus-js-client/blob/main/lib/browser/httpStack.js and replacing XMLHttpRequest with Axios.

kalemi19 commented 1 month ago

Curious to see this implemented

Acconut commented 1 month ago

@kalemi19 Let me know if you are interested in helping with this