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 |
|
7 | Micro 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 | --- | --- | --- | --- | --- | --- |
|
20 | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
|
21 |
|
22 | ### NOTE
|
23 |
|
24 | If 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 |
|
28 | Using npm:
|
29 |
|
30 | ```bash
|
31 | $ npm install regrest
|
32 | ```
|
33 |
|
34 | Using cdn:
|
35 |
|
36 | ```html
|
37 | <script src="https://unpkg.com/regrest/build/regrest.min.js"></script>
|
38 | ```
|
39 |
|
40 | ## 🎬 Example
|
41 |
|
42 | Regrest is designed to be the simplest way possible to make http calls
|
43 |
|
44 | Performing a `GET` request
|
45 |
|
46 | ```js
|
47 | // Import using Node or CommonJS module
|
48 | const regrest = require("regrest");
|
49 | // Or using ES6 module, also work for TypeScript
|
50 | import regrest from "regrest"
|
51 |
|
52 | // Use Promise
|
53 | regrest
|
54 | .get("/man/bear/pig")
|
55 | // Print the raw response string
|
56 | .then(response => console.log(response.text))
|
57 | // Print any error if occurred
|
58 | .catch(error => console.log(`*** Error: ${error}`));
|
59 |
|
60 | // Or use the new async/await keywords
|
61 | const getGoogle = async () => {
|
62 | try {
|
63 | // Store the response in a variable
|
64 | const response = await regrest.get("/foo/bar.json");
|
65 | // print out the parsed response
|
66 | console.log(response.json);
|
67 | } catch (error) {
|
68 | // Print any error if occurred
|
69 | console.log(`*** Error: ${error}`);
|
70 | }
|
71 | };
|
72 |
|
73 | getGoogle();
|
74 |
|
75 | // Or use callbacks
|
76 | // WE DON'T DO THAT HERE
|
77 | ```
|
78 |
|
79 | Performing a `POST` request
|
80 |
|
81 | ```js
|
82 | regrest
|
83 | .post("/comment", JSON.stringify({ name: "Foo", comment: "Bar" }))
|
84 | .then(response => console.log(response.status, response.statusText))
|
85 | .catch(error => console.log(error));
|
86 | ```
|
87 |
|
88 | ## 📚 Documentation
|
89 |
|
90 | ### Convenience methods
|
91 |
|
92 | ##### regrest.request(config)
|
93 |
|
94 | ##### regrest.get(url[, config])
|
95 |
|
96 | ##### regrest.head(url[, config])
|
97 |
|
98 | ##### regrest.post(url[, data[, config]])
|
99 |
|
100 | ##### regrest.put(url[, data[, config]])
|
101 |
|
102 | ##### regrest.delete(url[, config])
|
103 |
|
104 | ##### regrest.options(url[, config])
|
105 |
|
106 | ##### regrest.patch(url[, data[, config]])
|
107 |
|
108 | ### Config options
|
109 |
|
110 | ```js
|
111 | // Default options are marked with *
|
112 | const config = {
|
113 | method: "GET", // *GET, POST, PUT, DELETE, etc.
|
114 | url: "https://some-domain.com/api/",
|
115 | headers: { "Content-Type": "application/json; charset=utf-8" }, // *{}
|
116 | params: { UID: 9873 },
|
117 | data: JSON.stringify(data) // *null
|
118 | maxRedirects: 10 // *5
|
119 | };
|
120 | ```
|
121 |
|
122 | ### Response object attributes
|
123 |
|
124 | ```js
|
125 | {
|
126 | // Contains the status code of the response, e.g. 404 for a not found resource, 200 for a success
|
127 | status: 200,
|
128 | // A message related to the status attribute, e.g. OK for a status 200
|
129 | statusText: "OK",
|
130 | // The headers that the server responded with
|
131 | headers: {},
|
132 | // Response content as a string
|
133 | text: "",
|
134 | // Response content as JSON
|
135 | json: {}
|
136 | };
|
137 | ```
|
138 |
|
139 | ### Errors handling
|
140 |
|
141 | ```js
|
142 | regrest.get("/McNullington").catch(error => {
|
143 | if (error.response) {
|
144 | // A request was made
|
145 | // Server responded with status call out of the 200 - 400 range
|
146 | console.log(error.response.status);
|
147 | console.log(error.response.statusText);
|
148 | console.log(error.response.headers);
|
149 | console.log(error.response.text);
|
150 | console.log(error.response.json);
|
151 | } else if (error.request) {
|
152 | // A request was made, but no response was received
|
153 | } else {
|
154 | // Something else happened
|
155 | console.log(error.message);
|
156 | }
|
157 | });
|
158 | ```
|