https://github.com/pirxpilot/fetchagent
Flex API for global fetch
https://github.com/pirxpilot/fetchagent
Last synced: over 1 year ago
JSON representation
Flex API for global fetch
- Host: GitHub
- URL: https://github.com/pirxpilot/fetchagent
- Owner: pirxpilot
- Created: 2016-12-28T04:49:25.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2024-01-23T08:58:50.000Z (over 2 years ago)
- Last Synced: 2024-04-23T17:33:25.719Z (over 2 years ago)
- Language: JavaScript
- Size: 28.3 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
[![NPM version][npm-image]][npm-url]
[![Build Status][build-image]][build-url]
[![Dependency Status][deps-image]][deps-url]
# fetchagent
Flex API for global fetch
## Install
```sh
$ npm install --save fetchagent
```
## Usage
```js
var fa = require('fetchagent');
// using callbacks
fa
.get('http://httpbin.org/xml')
.end(function(err, response) {
if (err) {
console.log(err, status);
}
console.log("Received:", response);
});
// using promises
fa
.post('http://httpbin.org/post')
.send({ echo: 42 })
.json()
.then(function(response) {
console.log("Received:", response.echo);
});
```
# API
To configure request use one or more:
- `get(url)`, `put(url)`, `post(url)`, `delete(url)` - HTTP method to `url`
- `query()` - sets search params (a.k.a querystring)
- `set(name, vale)` - sets header `name` to `value`
- `set(obj)` - sets headers for all properties of the `object`
- `redirect(flag)` - pass truthy value if fetchagent is supposed to automatically handle redirects
To send request use on of:
- `end(callback)` - pass `callback(err, body)`
- `end()` - returns a Promise that resolves to a response
- `json()` - returns a Promise resolving to a parsed object
- `text()` - returns a Promise resolving to a body text
## License
MIT © [Damian Krzeminski](https://pirxpilot.me)
[npm-image]: https://img.shields.io/npm/v/fetchagent
[npm-url]: https://npmjs.org/package/fetchagent
[build-url]: https://github.com/pirxpilot/fetchagent/actions/workflows/check.yaml
[build-image]: https://img.shields.io/github/actions/workflow/status/pirxpilot/fetchagent/check.yaml?branch=main
[deps-image]: https://img.shields.io/librariesio/release/npm/fetchagent
[deps-url]: https://libraries.io/npm/fetchagent