# JavaScript

### Overview

This library contains all the API calls needed to communicate with the HAT.

* Authentication
* Fetch Applications, Data Plugs and Data Debits
* Read and write data to endpoints
* File API

## Setting Up Environment

### Supported Environments

The @dataswift/hat-js library works in all modern browsers. Some older browsers do not support all the features required. If you want to support these browsers you need to load polyfill for [Promise](https://github.com/taylorhakes/promise-polyfill).

### Installation

Install the **@dataswift/hat-js** package via [npm](https://www.npmjs.com/package/@dataswift/hat-js) or pull from our CDN.

**NPM installation**

```
npm install @dataswift/hat-js
```

**CDN Link**

```
<script src="https://cdn.dataswift.io/libs/hatjs/hat-0.3.0.min.js"></script>
```

### Usage

#### Importing

**ES6**

```javascript
import { HatClient } from "@dataswift/hat-js";
```

**ES5 Modules**

```javascript
var HatClient = require("@dataswift/hat-js").HatClient;
```

#### Initialisation

**Configuration type:**

```typescript
interface HatClientConfig {
    apiVersion?: string; // Api Version for the HAT. eg. v2.6
    hatDomain?: string; // The HAT domain of the user. eg. testing.hubat.net
    token?: string; // The Application token.
    secure?: boolean; // If you want to run the HAT locally, you have to modify this field to 'false'.
    onTokenChange?: Function;
}
```

**Example:**

```javascript
const config = {
    token: "<access-token>",
    apiVersion: 'v2.6',
    secure: true,
    onTokenChange: (newToken) => storeNewToken(newToken)
};

const hat = new HatClient(config);
```

###
