@apicase/adapter-xhr
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -58,2 +58,7 @@ 'use strict'; | ||
}; | ||
try { | ||
res.body = JSON.parse(e.target.response); | ||
} catch (_) { | ||
res.body = e.target.response; | ||
} | ||
if (payload.validateStatus(e.target.status)) { | ||
@@ -60,0 +65,0 @@ resolve(res); |
@@ -56,2 +56,7 @@ const pathToRegexp = require('path-to-regexp') | ||
} | ||
try { | ||
res.body = JSON.parse(e.target.response) | ||
} catch (_) { | ||
res.body = e.target.response | ||
} | ||
if (payload.validateStatus(e.target.status)) { | ||
@@ -58,0 +63,0 @@ resolve(res) |
{ | ||
"name": "@apicase/adapter-xhr", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "XHR adapter for apicase-core", | ||
@@ -5,0 +5,0 @@ "main": "index.cjs.js", |
@@ -1,11 +0,27 @@ | ||
# apicase-adapter-xhr | ||
XHR adapter for apicase-core | ||
# @apicase/adapter-xhr | ||
XHR adapter for [@apicase/core](https://github.com/apicase/core) | ||
## Installation | ||
XHR adapter is out-of-the-box adapter and it's installed in apicase-core by default | ||
1. Install via NPM | ||
``` | ||
npm install @apicase/adapter-xhr | ||
``` | ||
2. Import it | ||
```javascript | ||
import { apicase } from '@apicase/core' | ||
import xhr from '@apicase/adapter-xhr' | ||
const xhrAPI = apicase(xhr) | ||
``` | ||
We use [**node-fetch**](https://www.npmjs.com/package/node-fetch) as polyfill for Node.js | ||
## Basic usage | ||
```javascript | ||
apicase.call({ | ||
adapter: 'xhr', | ||
const req = await xhrAPI({ | ||
url: '/api/posts', | ||
@@ -16,4 +32,8 @@ method: 'GET', | ||
}) | ||
.then(console.log) | ||
.catch(console.error) | ||
if (req.success) { | ||
console.log(req.result) | ||
} else { | ||
console.error(req.result) | ||
} | ||
``` | ||
@@ -42,4 +62,3 @@ | ||
```javascript | ||
apicase.call({ | ||
adapter: 'xhr', | ||
xhrAPI({ | ||
url: '/api/posts/:id', | ||
@@ -54,4 +73,3 @@ params: { id: 1 } | ||
```javascript | ||
apicase.call({ | ||
adapter: 'fetch', | ||
xhrAPI({ | ||
url: '/api/posts', | ||
@@ -70,7 +88,6 @@ method: 'POST', | ||
```javascript | ||
apicase.call({ | ||
adapter: 'fetch', | ||
xhrAPI({ | ||
url: '/api/posts', | ||
validator: (target, event) => | ||
target.status >= 200 && target.status <= 299 | ||
validateStatus: (status) => | ||
status >= 200 && status <= 299 | ||
}) | ||
@@ -80,31 +97,7 @@ ``` | ||
```javascript | ||
function defaultValidator (target, event) { | ||
return (target.status >= 200 && target.status <= 299) || target.status === 304 | ||
function defaultValidator (status) { | ||
return status >= 200 && status < 300 | ||
} | ||
``` | ||
### Progress and aborted hooks | ||
I know that people use XHR instead of fetch because of calls abortion and upload progress handling. | ||
So there are two custom hooks for that - **progress** and **aborted**: | ||
```javascript | ||
apicase.call({ | ||
url: '/upload', | ||
method: 'POST', | ||
body: ..., | ||
hooks: { | ||
// xhr.onprogress | ||
progress (event, next) { | ||
console.log(event) | ||
next() | ||
}, | ||
// xhr.onabort | ||
aborted (event, next) { | ||
console.log(event) | ||
next() | ||
} | ||
} | ||
}) | ||
``` | ||
> **NOTE**: **event** also has **options** property with request options because apicase calls all hooks with options injected. | ||
## Author | ||
@@ -111,0 +104,0 @@ [Anton Kosykh](https://github.com/Kelin2025) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
184
9350
103