Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/conorturner/urequest
Promise based zero dependency node.js http request module
https://github.com/conorturner/urequest
http http-client https nodejs npm-package request
Last synced: 4 days ago
JSON representation
Promise based zero dependency node.js http request module
- Host: GitHub
- URL: https://github.com/conorturner/urequest
- Owner: conorturner
- License: mit
- Created: 2018-06-14T19:25:35.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-29T08:53:37.000Z (about 5 years ago)
- Last Synced: 2023-11-07T00:43:43.513Z (about 1 year ago)
- Topics: http, http-client, https, nodejs, npm-package, request
- Language: JavaScript
- Homepage:
- Size: 70.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# urequest
[![npm](https://img.shields.io/npm/dt/urequest.svg?style=for-the-badge)](https://www.npmjs.com/package/urequest)
[![Travis](https://img.shields.io/travis/conorturner/urequest.svg?style=for-the-badge)](https://travis-ci.org/conorturner/urequest)
[![Coveralls github](https://img.shields.io/coveralls/github/conorturner/urequest.svg?style=for-the-badge)](https://coveralls.io/github/conorturner/urest)
![license](https://img.shields.io/github/license/conorturner/urequest.svg?style=for-the-badge)
![npm bundle size (minified)](https://img.shields.io/bundlephobia/min/urequest.svg?style=for-the-badge)## Install
```bash
$ npm install urequest
```## Basic Usage
```javascript
const { URequest } = require("urequest");
const u = new URequest();u.request("https://www.google.com")
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error.statusCode);
console.error(error.body);
});
```## Options
| Property | Description | Example | Default |
|----------|:-------------------------------------:|--------------------------|---------|
| method | HTTP Method | POST | GET |
| port | Destination port on Server | 8080 | 80 |
| headers | HTTP Headers | {"a-header": "value"} | {} |
| qs | Query string parameters | ?a=b&b=1,2,3 | {} |
| uri | Standard URI | https://www.a.com:8080 | |
| body | HTTP Body | Buffer, string or object | |
| path | Path will be appended to the uri path | /path/to/data | |
| json | If the request and response are JSON | {json: true} | false |## Examples
#### Catch Errors
```javascript
const options = { uri: "http://localhost:8123/error", json: true };u.request(options)
.then(done)
.catch(({ body, statusCode }) => {
expect(statusCode).to.be.a("number");
expect(body).to.be.an("object");
})
```#### Post JSON
```javascript
const body = {
message: "hi"
};const options = {
method: "POST",
uri: "http://my-api/echo",
body,
json: true
};u.request(options)
.then(result => ...
```**Note:** If the response body is an invalid JSON, the promise will be rejected with the body variable undefined.
#### Overwriting URI Components
```javascript
const options = {
uri: "http://my-api:8080/echo",
port: 80,
path: "/echo",
qs: {
utms: "abc123",
arr: [1,2,3]
}
json: true
};// Will resolve http://my-api:80/echo/echo?utms=abc123&arr=1,2,3
```## Neutron
Neutron is a compression wrapper library built around node zlib and streams API's. It provides a set of simple interface methods supporting both gzip and deflate.
#### Compress and Decompress
```js
const { Neutron } = require("urequest");
const inputString = "compress me!"
const buffer = Buffer.from(inputString);
const encoding = "gzip";const compressedStream = Neutron.compress(buffer, encoding);
const decompressedStream = Neutron.decompress(compressedStream, encoding);Neutron.flattenStream(decompressedStream)
.then(output => {
expect(output.toString()).to.equal(inputString);
done();
})
```#### URest Middleware and Interceptors
```javascript
// serverconst { Rest, JsonBodyParser } = require("urest");
const { Neutron } = require("urequest");
const app = new Rest();app.pre(Neutron.middleware());
app.pre(JsonBodyParser.middleware());
app.post("/echo", (req, res) => res.send(req.body));
app.native().listen(1234);// client
const { URequest } = require("urequest");
const u = new URequest();const body = {
message: "hi"
};const options = {
method: "POST",
uri: "http://localhost:1234/echo",
body,
json: true,
gzip: true
};u.request(options)
.then(result => {
expect(result).to.deep.equal(body);
})
```