Skip to content

Commit 1d52f51

Browse files
committed
README.md edits
1 parent 5e7fc25 commit 1d52f51

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

Diff for: COMPARISON.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Comparison to other AJAX libraries/solutions
2+
3+
Explanations on why this library was made despite there being many libraries already out there.
4+
5+
6+
## Why not use fetch?
7+
8+
[Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) is sometimes cumbersome:
9+
10+
- Must set `Content-Type` header and encode body manually when sending JSON
11+
- Must call `response.json()` always when retrieving JSON
12+
- Forces proper CORS always
13+
- Does not reject on 4xx or 5xx responses status codes, only on network errors
14+
15+
16+
## Why not use axios?
17+
18+
[Axios](https://github.com/axios/axios) has a history of issues related to global defaults and side-effects (some [severe](https://github.com/axios/axios/issues/385)). Some have been [fixed](https://github.com/axios/axios/pull/1395) but [more](https://github.com/axios/axios/pull/2207) still arise.
19+
20+
It is cumbersome and error-prone to set request configuration with a (nested) object. Also, this design sometimes leads to cumbersome workarounds (e.g. [unsetting a header value](https://github.com/axios/axios/issues/382)).
21+
22+
23+
## Why not use jQuery.ajax?
24+
25+
If you have it in your project, sure. Not that common anymore.
26+
27+
Worth noting that older versions of jQuery, still widely in use, implement a [broken Promise implementation](https://stackoverflow.com/questions/23951745/is-any-jquery-version-compliant-to-promise-a-specifications/23958233#23958233).

Diff for: README.md

+38-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
- Understands Content-Type
1414
- Decodes JSON responses by default
1515
- [Works on modern browsers](#browser-support)
16-
- No external dependencies
17-
- See [polyfills](#polyfills) for a list of polyfills you might need for older browsers
16+
- See [polyfills](#polyfills) for a list of polyfills if you target very old browsers
17+
- No external dependencies
18+
- Quite small (<2.5KB minified)
1819
- [Fully tested](/test/specs)
1920

21+
Why not use fetch, axios, jQuery, etc..? See [COMPARISON.md](COMPARISON.md).
22+
2023

2124
## Installation
2225

@@ -158,6 +161,29 @@ console.log(req.toObject().url);
158161

159162
The following methods are available.
160163

164+
- [.get(url)](#get)
165+
- [.post(url)](#post)
166+
- [.method(method)](#method)
167+
- [.url(url)](#url)
168+
- [.baseUrl(url)](#baseurl)
169+
- [.query(object | string)](#query)
170+
- [.headers(object)](#headers)
171+
- [.amendHeaders(object)](#amendheaders)
172+
- [.header(key, value)](#header)
173+
- [.unsetHeader(name)](#unsetheader)
174+
- [.body(data)](#body)
175+
- [.json(value)](#json)
176+
- [.urlencoded(value)](#urlencoded)
177+
- [.timeout(milliseconds)](#timeout)
178+
- [.unsetTimeout()](#unsettimeout)
179+
- [.send([body])](#send)
180+
- [.sendUrlencoded(data)](#sendurlencoded)
181+
- [.sendJson(data)](#sendjson)
182+
- [.setResponseTransformers([])](#setresponsetransformers)
183+
- [.setAllowedStatusCode(allowed)](#setallowedstatuscode)
184+
- [.polyfills(polyfills)](#polyfills)
185+
- [.toObject() / .config() / .debug()](#toobject)
186+
161187
### get
162188

163189
```js
@@ -222,7 +248,7 @@ request.baseUrl('https://example.com/nested/foo').url('accounts') // => https:/
222248
Sets query parameters from an object. Overwrites existing query.
223249

224250
```js
225-
.query(object)
251+
.query(object | string)
226252
```
227253

228254
Where `object` is key-value object of query parameters to set (will be encoded using `URLSearchParams#toString`), or a valid query string.
@@ -437,6 +463,15 @@ request.setResponseTransformers([
437463
]);
438464
```
439465

466+
Reference to the original array is lost:
467+
468+
```js
469+
const array = [];
470+
const req = request.setResponseTransformers(array);
471+
array.push(someFunction);
472+
req.toObject().responseTransformers; // not affected by the push, still []
473+
```
474+
440475
### setAllowedStatusCode
441476

442477
By default any 2XX status code resolves the Promise and other status codes will reject it. This can be customized using `setAllowedStatusCode`.

0 commit comments

Comments
 (0)