https://github.com/mesosphere-backup/connections
Provides different connection types with a unified interface
https://github.com/mesosphere-backup/connections
connections xhr
Last synced: 8 months ago
JSON representation
Provides different connection types with a unified interface
- Host: GitHub
- URL: https://github.com/mesosphere-backup/connections
- Owner: mesosphere-backup
- License: apache-2.0
- Created: 2017-10-24T13:20:23.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-07T09:22:17.000Z (almost 3 years ago)
- Last Synced: 2025-02-20T03:19:02.588Z (8 months ago)
- Topics: connections, xhr
- Language: JavaScript
- Size: 148 KB
- Stars: 2
- Watchers: 6
- Forks: 1
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Connections [](https://travis-ci.org/dcos-labs/connections)
---
👩🔬 Please be aware that this package is still experimental —
changes to the interface and underlying implementation are likely,
and future development or maintenance is not guaranteed.---
This package provides different connection types with a unified interface.
## Example
The following example will create an `XHRConnection`, add a complete event
listener and open the connection. The complete listener will get called once
the JSON file is loaded and the connection is closed.```javascript
import { XHRConnection, ConnectionEvent } from "@dcos/connections";const connection = new XHRConnection("http://example.com/file.json");
connection.addListener(ConnectionEvent.COMPLETE, (event) => console.log(event));
connection.open();
```## Abstract Connection
The `AbstractConnection` provides the default properties, methods, states, and
event-definitions to implement a custom connection.## Connection Event
The `ConnectionEvent` provides a common interface for all different event types.
### Types
Use the following event types...
* `OPEN` when opening the connection
* `DATA` every time data is received from the server
* `ERROR` when an error occurred
* `COMPLETE` when the connection closes without any errors
* `ABORT` when the connection was aborted by something### Target
The event `target` always points to the connection.
## XHR Connection
The `XHRConnection` wraps the native `XMLHttpRequest` to provide a unified and
easy to use interface.### Example
The following example will create an `XHRConnection` to post "data" to an
API server.```javascript
const connection = new XHRConnection("http://example.com/api", {
method: "POST",
body: "data"
});
connection.open();
```### Parameters
#### URL
This defines the resource location that you want to connect to.
#### Options (Optional)
An optional object containing custom settings that you want to apply to the connection.
* `headers`: Any headers you want to add to your request
* `method`: The request method (default: `"GET"`)
* `body`: Any data you want send with your request
* `responseType`: The response type you expect (default: `"json"`) If the server returns data that is not compatible the value of `response` will be `null`.##### Response Types
Value | Data type of response property
-- | --
"arraybuffer" | ArrayBuffer
"blob" | Blob
"document" | Document
"json" | JavaScript object, parsed from a JSON string returned by the server
"text" | DOMString