spraypaint.js icon indicating copy to clipboard operation
spraypaint.js copied to clipboard

Enable custom http client like Axios

Open lucianholt97 opened this issue 4 years ago • 9 comments

It would be really helpful to allow overwriting or mock the default http client. This is especially useful when the existing app is based on axios with many predefined axios interceptors and also when using server side rendering.

lucianholt97 avatar Sep 29 '20 11:09 lucianholt97

I have a structure where requests go over localhost for serverside calls and use a base url for client side calls. I have an axios interceptor that handles that currently, and it would be nice to be able to continue using that.

I have another non technical reason to +1 this request this as well - when making the case for spraypaint for some corporate clients who currently use axios across multiple apps, even though it doesn't get them anything, not having the ability to slot axios in has come up in several meetings and it would be nice to put that argument to bed (and then not use it.)

gone avatar Dec 18 '20 19:12 gone

I want this feature too. it seems to make fetch api adaptable , as https://github.com/graphiti-api/spraypaint.js/blob/c4e085a7f2ed202a83f207219f8bdc959d8cb54b/src/request.ts#L107

openxyz avatar Jan 01 '21 16:01 openxyz

Started a PR (#90) for this thinking I needed it, but ended up not. If you get a chance to try it out in the wild let me know if it does/doesn't work for your use case(s) :)

jkcorrea avatar Feb 06 '21 03:02 jkcorrea

A very hacky solution, but in theory, since axios internally uses XMLHttpRequest and not fetch, you can override window.fetch.

const _fetch = window.fetch;
window.fetch = async function fetch(url, init) {
  if (typeof url !== 'string') {
    return _fetch(url, init);
  }
  const { method = 'get', headers } = init || {};
  const res = await axios({
    method,
    url,
    headers,
    // ... anything else that you feel makes sense
    responseType: 'arraybuffer',
  });
  const response = new Response(res.data, {
    status: res.status,
    statusText: res.statusText,
    headers: res.headers,
  });
  // Object.defineProperty(response, 'url', { enumerable: true, value: url });
  return response;
};

(I'll leave TypeScript types as an exercise to the user)

Alternatively you can override spraypaint's Request._fetch itself.

avioli avatar Mar 17 '21 13:03 avioli

I want to migrate our current app that uses Axios for api calls to spraypaint, is this still an issue?

lartheon avatar Jul 29 '21 07:07 lartheon

I see no issue, since you are moving from axios to plain spraypaint.js, but there's a PR to specify a fetcher, which you can hope get merged someday: https://github.com/graphiti-api/spraypaint.js/pull/90

avioli avatar Jul 29 '21 09:07 avioli

I see no issue, since you are moving from axios to plain spraypaint.js, but there's a PR to specify a fetcher, which you can hope get merged someday: #90

oh wow Feb 6th, yeh someday

lartheon avatar Jul 29 '21 15:07 lartheon

This one flew under my radar :) It's now on it. /cc @wadetandy

richmolj avatar Jul 29 '21 16:07 richmolj

Oh yeah i totally missed this as well, sorry about that. Will take a look.

wadetandy avatar Jul 29 '21 17:07 wadetandy