https://github.com/teclone/xhr
Fetch API compatible implementation in XMLHttpRequest. Fully promisified. Works in browser like environments, including React Native.
https://github.com/teclone/xhr
Last synced: 10 months ago
JSON representation
Fetch API compatible implementation in XMLHttpRequest. Fully promisified. Works in browser like environments, including React Native.
- Host: GitHub
- URL: https://github.com/teclone/xhr
- Owner: teclone
- License: mit
- Created: 2020-03-02T18:58:13.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-10T20:12:26.000Z (over 3 years ago)
- Last Synced: 2025-08-08T23:02:03.480Z (11 months ago)
- Language: TypeScript
- Size: 3.19 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Xhr
[](https://travis-ci.org/teclone/xhr)
[](https://coveralls.io/github/teclone/xhr?branch=master)
[](https://github.com/semantic-release/semantic-release)
[](https://badge.fury.io/js/%40teclone%2Fxhr)

Fetch API compatible implementation in XMLHttpRequest. Fully promisified. Works in browser like environments, including React Native.
## Installation
module is available on npm
```bash
npm install @teclone/xhr
```
## Usage
There are method names for all of these six http methods `get`, `head`, `options`, `post`, `delete`, `put` that you can use to make requests.
By default, the request will never throw even on errors (unlike the Fetch api spec). You need to check the **ok** property of the response object.
This can be changed by setting `options.throwIfNotOk` to `true` or by calling `Xhr.throwIfNotOk(true)` to set this for every request. The value of `throwIfNotOk` in ecah **request options** will always be prioritized ahead of the global settings.
```typescript
import { Xhr } from '@teclone/xhr';
Xhr.get(url, options).then(response => {
if (response.ok) {
// do something.
}
});
```
## Running in Mocked Browser Environments
To make it seemless when you want to run in mocked browser environment, **Xhr** module exports `install` method to make this possible. Call the `install` method, passing in the window and document object.
**Running in jsdom environment:**
```javascript
import JSDOM from 'jsdom';
import { Xhr } from '@teclone/xhr';
const dom = new JSDOM('', {
url: 'urltomock',
pretendToBeVisual: true,
});
Xhr.install(dom.window, dom.window.document);
```