UNPKG

4.75 kBMarkdownView Raw
1# 🚀 Regrest - Micro HTTP client
2
3[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
4[![npm version](https://badge.fury.io/js/regrest.svg)](https://badge.fury.io/js/regrest)
5[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
6
7Micro Promise based HTTP client for the browser and node.js
8
9## ✨ Features
10
11- Make [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) from the browser
12- Make [http](http://nodejs.org/api/http.html) requests from node.js
13- Supports the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API
14- Built in [TypeScript](https://www.typescriptlang.org/) support
15
16## 👍🏻 Browser Support
17
18![Chrome](https://raw.github.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/src/safari/safari_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/src/opera/opera_48x48.png) | ![Edge](https://raw.github.com/alrra/browser-logos/master/src/edge/edge_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/src/archive/internet-explorer_9-11/internet-explorer_9-11_48x48.png) |
19--- | --- | --- | --- | --- | --- |
20Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
21
22### NOTE
23
24If you intend to support Internet Explorer, be sure to have a [poly-fill](https://github.com/taylorhakes/promise-polyfill) that adds a global `Promise` object
25
26## 🏗 Installing
27
28Using npm:
29
30```bash
31$ npm install regrest
32```
33
34Using cdn:
35
36```html
37<script src="https://unpkg.com/regrest/build/regrest.min.js"></script>
38```
39
40## 🎬 Example
41
42Regrest is designed to be the simplest way possible to make http calls
43
44Performing a `GET` request
45
46```js
47// Import using NodeJS or CommonJS module
48const regrest = require("regrest");
49// Or using ES6 module
50import regrest from "regrest"
51// Without synthetic default imports (e.g. TypeScript)
52import * as regrest from "regrest"
53
54// Use Promise
55regrest
56 .get("/man/bear/pig")
57 // Print the raw response string
58 .then(response => console.log(response.text))
59 // Print any error if occurred
60 .catch(error => console.log(`*** Error: ${error}`));
61
62// Or use the new async/await keywords
63const getGoogle = async () => {
64 try {
65 // Store the response in a variable
66 const response = await regrest.get("/foo/bar.json");
67 // print out the parsed response
68 console.log(response.json);
69 } catch (error) {
70 // Print any error if occurred
71 console.log(`*** Error: ${error}`);
72 }
73};
74
75getGoogle();
76
77// Or use callbacks
78// WE DON'T DO THAT HERE
79```
80
81Performing a `POST` request
82
83```js
84regrest
85 .post("/comment", JSON.stringify({ name: "Foo", comment: "Bar" }))
86 .then(response => console.log(response.status, response.statusText))
87 .catch(error => console.log(error));
88```
89
90## 📚 Documentation
91
92### Convenience methods
93
94##### regrest.request(config)
95
96##### regrest.get(url[, config])
97
98##### regrest.head(url[, config])
99
100##### regrest.post(url[, data[, config]])
101
102##### regrest.put(url[, data[, config]])
103
104##### regrest.delete(url[, config])
105
106##### regrest.options(url[, config])
107
108##### regrest.patch(url[, data[, config]])
109
110### Config options
111
112```js
113// Default options are marked with *
114const config = {
115 method: "GET", // *GET, POST, PUT, DELETE, etc.
116 url: "https://some-domain.com/api/",
117 headers: { "Content-Type": "application/json; charset=utf-8" }, // *{}
118 params: { UID: 9873 },
119 data: JSON.stringify(data), // *null
120 maxRedirects: 10, // *5
121 withCredentials: true // *false, true
122};
123```
124
125### Response object attributes
126
127```js
128{
129 // Contains the status code of the response, e.g. 404 for a not found resource, 200 for a success
130 status: 200,
131 // A message related to the status attribute, e.g. OK for a status 200
132 statusText: "OK",
133 // The headers that the server responded with
134 headers: {},
135 // Response content as a string
136 text: "",
137 // Response content as JSON
138 json: {}
139};
140```
141
142### Errors handling
143
144```js
145regrest.get("/McNullington").catch(error => {
146 if (error.response) {
147 // A request was made
148 // Server responded with status call out of the 200 - 400 range
149 console.log(error.response.status);
150 console.log(error.response.statusText);
151 console.log(error.response.headers);
152 console.log(error.response.text);
153 console.log(error.response.json);
154 } else if (error.request) {
155 // A request was made, but no response was received
156 } else {
157 // Something else happened
158 console.log(error.message);
159 }
160});
161```