unfetch icon indicating copy to clipboard operation
unfetch copied to clipboard

global default options and handlers

Open farskid opened this issue 8 years ago • 22 comments

Since fetch apis are used a lot in a typical web application, it would be nice to have a unfetch.defaults object that sets default parameters of all requests. A use case would be to set base URL of all requests.

unfetch.defaults.baseURL = 'my-api-endpoint'

farskid avatar Feb 23 '17 22:02 farskid

Sounds good, but don't believe we can get 500b, haha

tunnckoCore avatar Feb 23 '17 22:02 tunnckoCore

Well, having a useful feature to cost some bytes wouldn't harm I guess. Let's watch the package size to see if there is a chance for adding this feature.

farskid avatar Feb 23 '17 22:02 farskid

I'm agree with you and was thinking the same thing when mitt came - in its case we can get few more features if we allow 20-30bytes more, but @developit is holding strongly that it should be 200b. I'm agree with him too and there was a discussion like if we allow such things we'll get to point such as "let's add more more 1-2kb" haha and was mentioned momentjs that it started like micro lib.

So yea, it is good thing to try to be strict.

tunnckoCore avatar Feb 23 '17 22:02 tunnckoCore

@tunnckoCore HAHA did not know that momentjs was a micro library at the beginning. Looking forward to hear about the decision from @developit . This feature whether added or not, both seems logical to me at the moment. :)

farskid avatar Feb 23 '17 23:02 farskid

Yea, that's the exact comment https://github.com/developit/mitt/pull/11#issuecomment-273132616, and i'm totally agree with him.

tunnckoCore avatar Feb 23 '17 23:02 tunnckoCore

This discussion makes me happy :) Let's keep this on a list of nice-to-haves that we can look to if we save bytes.

Btw - for defaults, we should make a wrapper library, that way they would work for native fetch too 😀

developit avatar Feb 23 '17 23:02 developit

@developit You mean like an IIFE that wraps the native or unfetch altogether?

farskid avatar Feb 23 '17 23:02 farskid

Yup, a way to inject defaults by proxying window.fetch:

let real = window.fetch;
window.fetch = (url, options) => {
  // Apply URL and options transforms here
  return old(url, ootions)
    .then(options.after || Object);
               // ^ middleware!!
};

developit avatar Feb 23 '17 23:02 developit

Yeah, good pattern, agreed.

farskid avatar Feb 23 '17 23:02 farskid

Absolutely agree to be separate kinda nanolib. Hm, let's coin nano frameworks? haha

tunnckoCore avatar Feb 23 '17 23:02 tunnckoCore

Unlibs

developit avatar Feb 24 '17 00:02 developit

@tunnckoCore @farskid - what do we want to do with these types of issues/PRs? We can't merge them because of the nature of this library (it being a polyfill and thus bound by the spec it emulates), but I hate closing them when there's good discussion and tasks to be taken away. Maybe we need a repo/place where these ideas/implementations go so that we can build up a set of feature requests for wrapper libraries?

developit avatar Feb 25 '17 20:02 developit

@developit @tunnckoCore I guess a wrapper for addon features is a nice idea. Devs can either use the super light version or pay the price of not so much light version for the extra features such as global configurations, extra handlers, ...

farskid avatar Feb 26 '17 08:02 farskid

Exactly! And the wrapper will work regardless of whether unfetch or native fetch gets used.

developit avatar Feb 26 '17 14:02 developit

Yup so much like a higher order API. Would love to contribute to the wrapper.

farskid avatar Feb 26 '17 14:02 farskid

Exactly, that's the cool of micro things. : )

tunnckoCore avatar Feb 26 '17 14:02 tunnckoCore

Prior art: https://github.com/RickWong/fetch-plus

developit avatar Feb 26 '17 16:02 developit

I've written this in one of my projects sometime ago. fetch-plus seems a lot nicer though.

notpushkin avatar Mar 10 '17 16:03 notpushkin

@iamale totally stealing that

developit avatar Mar 10 '17 16:03 developit

Learned a lot just reading this thread!! There are a lot of good ideas happening here. Is there a way it can be preserved? If it were closed, it could still be linked to from a wiki or something.

MobiusHorizons avatar Mar 22 '17 11:03 MobiusHorizons

Heh - I think that's why we haven't closed it. Need to find a good home for these things, maybe in the wiki.

developit avatar Mar 22 '17 16:03 developit

Another thing that I had in mind since I wrote arequests is this:

const api = Afetch({url: "https://api.github.com"});
let mdo = await api.users["mdo"].get();
let json = await mdo.json();  // or whatever you'd do with a regular Fetch object

notpushkin avatar Apr 03 '17 09:04 notpushkin