Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pinkhominid/network-event

A global normalized event for browser network requests (fetch & XHR).
https://github.com/pinkhominid/network-event

browser event network request

Last synced: 27 days ago
JSON representation

A global normalized event for browser network requests (fetch & XHR).

Awesome Lists containing this project

README

        

# Network Event

A global normalized event for browser network requests (fetch & XHR).

```sh
npm i network-event
```

```js
import { NETWORK_EVENT, PENDING_STATE, COMPLETE_STATE } from 'network-event';

window.addEventListener(NETWORK_EVENT, async e => {
switch (e.detail.state) {
case PENDING_STATE: console.log(e.detail); break;
case COMPLETE_STATE: console.log(e.detail, await e.detail.json()); break;
}
});

fetch('https://jsonplaceholder.typicode.com/users');

const req = new XMLHttpRequest();
req.open('get', 'https://jsonplaceholder.typicode.com/users');
req.send();
```

```sh
▸ Object { state: "pending", url: "https://jsonplaceholder.typicode.com/users" }

▸ Object { state: "pending", url: "https://jsonplaceholder.typicode.com/users" }

▸ Object { state: "complete", url: "https://jsonplaceholder.typicode.com/users", status: 200, "content-type": "application/json; charset=utf-8", text: send(), json: send() }
▸ Array(10) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…} ]

▸ Object { state: "complete", url: "https://jsonplaceholder.typicode.com/users", status: 200, "content-type": "application/json; charset=utf-8", text: fetch(), json: fetch() }
▸ Array(10) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…} ]
```