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 NodeJS or CommonJS module
|
48 | const regrest = require("regrest");
|
49 | // Or using ES6 module
|
50 | import regrest from "regrest"
|
51 | // Without synthetic default imports (e.g. TypeScript)
|
52 | import * as regrest from "regrest"
|
53 |
|
54 | // Use Promise
|
55 | regrest
|
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
|
63 | const 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 |
|
75 | getGoogle();
|
76 |
|
77 | // Or use callbacks
|
78 | // WE DON'T DO THAT HERE
|
79 | ```
|
80 |
|
81 | Performing a `POST` request
|
82 |
|
83 | ```js
|
84 | regrest
|
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 *
|
114 | const 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 | };
|
122 | ```
|
123 |
|
124 | ### Response object attributes
|
125 |
|
126 | ```js
|
127 | {
|
128 | // Contains the status code of the response, e.g. 404 for a not found resource, 200 for a success
|
129 | status: 200,
|
130 | // A message related to the status attribute, e.g. OK for a status 200
|
131 | statusText: "OK",
|
132 | // The headers that the server responded with
|
133 | headers: {},
|
134 | // Response content as a string
|
135 | text: "",
|
136 | // Response content as JSON
|
137 | json: {}
|
138 | };
|
139 | ```
|
140 |
|
141 | ### Errors handling
|
142 |
|
143 | ```js
|
144 | regrest.get("/McNullington").catch(error => {
|
145 | if (error.response) {
|
146 | // A request was made
|
147 | // Server responded with status call out of the 200 - 400 range
|
148 | console.log(error.response.status);
|
149 | console.log(error.response.statusText);
|
150 | console.log(error.response.headers);
|
151 | console.log(error.response.text);
|
152 | console.log(error.response.json);
|
153 | } else if (error.request) {
|
154 | // A request was made, but no response was received
|
155 | } else {
|
156 | // Something else happened
|
157 | console.log(error.message);
|
158 | }
|
159 | });
|
160 | ```
|