https://github.com/zurfyx/fetch-absolute
Fetch with default absolute URLs.
https://github.com/zurfyx/fetch-absolute
absolute fetch node-fetch url
Last synced: 11 months ago
JSON representation
Fetch with default absolute URLs.
- Host: GitHub
- URL: https://github.com/zurfyx/fetch-absolute
- Owner: zurfyx
- License: mit
- Created: 2017-02-26T13:43:47.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-26T13:44:09.000Z (almost 9 years ago)
- Last Synced: 2025-03-07T10:39:00.460Z (11 months ago)
- Topics: absolute, fetch, node-fetch, url
- Language: JavaScript
- Size: 2.93 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fetch Absolute
> Fetch with default absolute URLs.
[](https://travis-ci.org/zurfyx/fetch-absolute)
[](https://badge.fury.io/js/fetch-absolute)
Tired of writing `http://localhost` in front of every [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) request?
With fetch absolute you'll only write it once per host in your whole environment.
It works with both [window.fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and [node-fetch](https://www.npmjs.com/package/node-fetch).
## Install
```
npm install fetch-absolute
```
## Usage
```
var fetch = require('node-fetch');
var fetchAbsolute = require('fetch-absolute');
global.fetchApi = fetchAbsolute(fetch)('http://localhost:3030');
```
## Example
Using node-fetch and fetch-absolute with Node 7 async/await:
```
const fetch = require('node-fetch');
const fetchAbsolute = require('fetch-absolute');
const fetchApi = fetchAbsolute(fetch)('http://localhost:3030');
it('should should display "It works!"', async () => {
const response = await fetchApi('/');
const json = await response.json();
expect(json).to.eql({ msg: 'It works!' });
});
```
Requested absolute URLs must start with `/`, otherwise they will be treated as relative.
## Related
- [superagent-absolute](https://github.com/zurfyx/superagent-absolute) - Superagent with default absolute URLs.
## License
MIT © [Gerard Rovira Sánchez](//zurfyx.com)