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