Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yeojz/fetched
A window.fetch request formatter with an ajax/superagent inspired API
https://github.com/yeojz/fetched
Last synced: 7 days ago
JSON representation
A window.fetch request formatter with an ajax/superagent inspired API
- Host: GitHub
- URL: https://github.com/yeojz/fetched
- Owner: yeojz
- License: mit
- Created: 2015-09-11T12:36:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-09-13T12:21:17.000Z (about 9 years ago)
- Last Synced: 2024-08-08T21:10:05.616Z (3 months ago)
- Language: JavaScript
- Homepage: https://github.com/yeojz/fetched
- Size: 207 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fetched
[![npm](https://img.shields.io/npm/v/fetched.svg?style=flat-square)](https://www.npmjs.com/package/fetched)
[![Build Status](https://img.shields.io/travis/yeojz/fetched.svg?style=flat-square)](https://travis-ci.org/yeojz/fetched)`fetched` provides a declarative wrapper for request parameters.
It's targeted towards `window.fetch` standard, but can be used to format
request object for other XMLHttpRequest librariesIn general, `fetched` is a xhr request formatter with an ajax/superagent inspired API
that is targeted toward `window.fetch` WHATWG standard / polyfill.## Installation
`npm install fetched --save`
To use `fetch`, You may need the following polyfills:
__fetch__
`npm install whatwg-fetch --save`__promise__
`npm install es6-promise --save`## Usage
For example, posting data to `http://example.com/api/me`
```js
import Fetched from 'fetched';let agent = new Fetched('http://localhost');
agent.post('/api/me')
.send({
username: 'my-username',
password: 'my-password'
})
.json()
.withCredentials()
.using(fetch); // uses window.fetch object. Can be other compatible HTTP request libraries.
```
The above will return a fetch promise object.To use with other libraries, you can do the following:
```
let result = agent.post('/api/me')
.send({
username: 'my-username',
password: 'my-password'
})
.json()
.withCredentials()
.format();
```
You should get the following output in your `result` variable:
```json
{
"resource": "http://localhost",
"params": {
"headers": {
"Accept": "application/json",
"Content-Type": "application/json"
},
"method": "post",
"body": "{'username':'my-username','password':'my-password'}",
"credentials": "include"
}
}
```## Note:
API is still a little unstable.