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

Accept a fetch option for dependency injection #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ fetch('https://example.com', {
})
```

### options.fetch

```js
options.fetch = window.fetch
```

It is possible to pass an `options.fetch` function, which this library will use in place of the browser's `fetch` method. This may be useful for unit-testing.

## Environment

Node and browser (es2015)
7 changes: 6 additions & 1 deletion src/apigen.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ function fetchMethod (methodName, url, definition, config) {
const fetchConfiguration = {body, method: 'POST'}
Object.assign(fetchConfiguration, config.fetchConfiguration)

fetch(url, fetchConfiguration).then(response => {
// We have to use the naked `fetch` on Safari (or `this` will be wrong),
// unless the config has provided one to use instead:
const promise = config.fetch != null
? config.fetch(url, fetchConfiguration)
: fetch(url, fetchConfiguration)
promise.then(response => {
if (response.status >= 200 && response.status < 300) {
return response.json()
} else {
Expand Down