Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency injection of fetch implementation on node #8

Open
cspotcode opened this issue Mar 25, 2020 · 3 comments
Open

Dependency injection of fetch implementation on node #8

cspotcode opened this issue Mar 25, 2020 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@cspotcode
Copy link

On node, is there a way to use dependency injection to provide a fetch implementation rather than declaring one globally? Is there a recommended implementation, maybe https://www.npmjs.com/package/isomorphic-fetch or https://www.npmjs.com/package/cross-fetch?

Ideally, I'd like to be able to do:

import {create} from 'gretchen';
import * as fetch from 'cross-fetch';
const gretch = create({fetch});
await gretch("/api/user/12").json();
@cspotcode cspotcode changed the title Recommended way to run on node? Dependency injection of fetch implementation on node Mar 25, 2020
@estrattonbailey estrattonbailey added the enhancement New feature or request label Mar 25, 2020
@estrattonbailey
Copy link
Collaborator

@cspotcode hello! Thanks for the question, and sorry for the delay.

At the moment, no, but polyfills should Just Work, if that's an option. Do you have a specific need to avoid polyfills right now? I'm curious to know, because it wouldn't be too hard to add this capability to the library if it could be a common use case.

@cspotcode
Copy link
Author

cspotcode commented Apr 10, 2020

Not a specific need; just a general desire to avoid modifying the global environment to avoid possible incompatibilities with other libraries.

If I require any modifications to the global environment, this means that any libraries I publish will also impose this global mutation on their consumers. It's possible that consumers will use another library that breaks when it encounters this global mutation. I don't have any specific examples, so it's possible that this is extremely rare. But when writing libraries, I try not to make that assumption, since I don't know who will be using the library.

I can send a PR to implement this, if you want.

@estrattonbailey
Copy link
Collaborator

Hello again @cspotcode 👋 heck of a year it's been lol, sorry I never got back to you on this.

I hear you on the globals. Since you posted this, gretchen was refactored into a state that might make this easier. However, it still relies on other globals like Request.

If you've got ideas on how we could implement this we'd love a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants