Ky: Can external "fetch dependency be injected to ky"

Created on 6 Jul 2020  路  7Comments  路  Source: sindresorhus/ky

I would like to inject an enhanced fetch to ky (fetch is a dependency to ky).

In order to use with https://github.com/AxaGuilDEv/react-oidc/tree/master/packages/context-fetch in a "functionnal way"

Is it possible to do something like :

const parsed = await ky(fetch).post('https://example.com', {json: {foo: true}}).json();

?

Regards,

Guillaume

enhancement help wanted

Most helpful comment

Would it be okay if it were just a normal Ky option, as below?

const parsed = await ky.post('https://example.com', {fetch, json: {foo: true}}).json();
const myKy = ky.extend({fetch});
const parsed = await myKy.post('https://example.com', {json: {foo: true}}).json();

All 7 comments

Would it be okay if it were just a normal Ky option, as below?

const parsed = await ky.post('https://example.com', {fetch, json: {foo: true}}).json();
const myKy = ky.extend({fetch});
const parsed = await myKy.post('https://example.com', {json: {foo: true}}).json();

Yes, it is

Isn't this a duplicate of #56?

@Skayo

Yes, you're correct, it is. And the concerns that Sindre expressed there are still valid.

However, it's worth noting that since then:

  • Ky gained an additional co-maintainer (me :))
  • We have made it easier to override our globals, including fetch (e.g. https://github.com/sindresorhus/ky/commit/f107e7b92d90e4b0287846a49ba3f30d9b9ca648 and PR https://github.com/sindresorhus/ky/pull/153)
  • We created ky-universal which could potentially benefit from this feature
  • Ky has stabilized a lot, we haven't tagged a 1.0 release yet but I've been happily using it in production for quite some time

It's definitely worth reconsidering this.

@sholladay
Yeah that is true.
Might even need this for a project myself.
Do you know how much work this would be?
Maybe I'll do a PR...

It should be pretty easy. You would need to update this part of the code where we execute the fetch, so that it would be something like options.fetch() instead of globals.fetch(). And you would set the default value for fetch to globals.fetch at the top of the Ky constructor, where we set our other option defaults. That should be about it, other than tests and docs.

@guillaume-chervet @sholladay
Check out #273!

Was this page helpful?
0 / 5 - 0 ratings