npm package: @getevy/stately
Description: The Javascript helper library to manage Evy offers
Author: Evy
Latest version: 0.0.32

# Evy Stately

Stately is a headless library to help you build your Evy UI in Javascript or Typescript.

Stately provides methods for key actions in the lifecycle of insurance in the UI:

- getting offers
- attaching offers
- retrieving offers from the cart (stored in local storage)

## Installation

```sh
npm i @getevy/stately
```

## Initialization

```javascript
import { Evy as Stately } from '@getevy/stately';

const stately = new Stately({ apiKey, apiURL });
```

The `configuration` is an object.

| Key        | **Required** | **Description**              |
| ---------- | ------------ | ---------------------------- |
| **apiKey** | **yes**      | your API key                 |
| **apiURL** | no           | the URL of the API to target |

`stately` is an EventEmitter. The methods are used to modify the state and request values from the Evy API. Once the data are up to date, events are triggered to allow you to update the UI.

During the initialization, `stately` will retrieve previous data from the local storage.

## Methods

### `addProduct`

Fetches offers for a specific product.

**Parameters**

```javascript
stately.addProduct(productId: string, price: number) => Promise<void>
```

| Parameter     | **Required** | **Description**                      |
| ------------- | ------------ | ------------------------------------ |
| **productId** | **yes**      | ID of the product as provided to Evy |
| **price**     | **yes**      | Price of the product                 |

This method will emit `OFFERS_LOADED` when the offer data is retrieved from the server.

### `setAttachedOffer`

Attaches an offer to a product.

```typescript
stately.setAttachedOffer(offerId: string, productId: string, quantity: number) => void
```

**Parameters**

| Parameter     | **Required** | **Description**                      |
| ------------- | ------------ | ------------------------------------ |
| **offerId**   | **yes**      | ID of the offer                      |
| **productId** | **yes**      | ID of the product as provided to Evy |
| **quantity**  | **yes**      | Price of the product                 |

This method will emit `ATTACH_OFFER` when the offer data is attached and synchronized with the local storage.

### `getAttachedOffers`

Provides all the offers attached to products.

```javascript
stately.getAttachedOffers() => Record<AttachedOfferIdentifier, AttachedOffer>
```

**AttachedOffer**

| Key          | **type** | **Description**                                          |
| ------------ | -------- | -------------------------------------------------------- |
| **offer**    | IOffer   | The complete offer object                                |
| **quantity** | number   | The number of times the offer is attached to the product |

### `clearOffers`

Removes the offers attached on all the products.

```javascript
stately.clearOffers() => void
```

### `cancelAttachedOffer`

Removes the offers for a single product.

```javascript
stately.cancelAttachedOffer(productId: string) => void
```

## Events

### `OFFERS_LOADED`

This event is triggered when the offers are updated.

### `ATTACH_OFFER`

This event is triggered when an offer is attached to a product.