# Cyphlens SSE Client SDK 🚀  
A lightweight JavaScript library designed to simplify 2FA authentication with Cyphlens' Server-Sent Events (SSE) integration.

This SDK is bundled using **Rollup** and supports **ES Modules (ESM)**, **CommonJS (CJS)**, and **UMD (Universal Module Definition)** formats for maximum compatibility across environments.

---

## Features ✨  
- Easy integration with Cyphlens 2FA authentication  
- Real-time event listening via Server-Sent Events (SSE)  
- Lightweight and dependency-free  
- Cross-environment support (Node.js, browsers, and modern frameworks)  

---

## Installation 📦  

### Using NPM  
Install the SDK via npm:  
```sh
npm install @cyphlens/2fa-sse-sdk
```

### Using Yarn  
Install the SDK via Yarn:  
```sh 
yarn add @cyphlens/2fa-sse-sdk
```

### Using CDN (Browser)  
Include the SDK directly in your HTML:  
```html
<script src="https://cdn.jsdelivr.net/npm/@cyphlens/2fa-sse-sdk@2/dist/bundle.min.js"></script>
```

---

## Usage 🔧  

> 💡 **Tip:** Start with the **Sandbox** base URL for development and testing.

### Importing the SDK  

#### 1. ES Modules (ESM) - Modern JavaScript  
```javascript
import { Cyphlens, EventType } from "@cyphlens/2fa-sse-sdk";
```

#### 2. CommonJS (CJS) - Node.js  
```javascript
const { Cyphlens, EventType } = require("@cyphlens/2fa-sse-sdk");
```

#### 3. Browser (IIFE)  
After including the CDN script, the SDK is available as a global `Cyphlens` object:  
```javascript
const cyphlensClient = new Cyphlens("https://api.sandbox.cyphlens.com/b2b/v1");
```

---

### Example: Listening to 2FA Events  

#### 1. Initialize the Client  
```javascript
const baseUrl = "https://api.sandbox.cyphlens.com/b2b/v1";  // Use the correct Cyphlens base URL
const sessionId = "your-session-id";                        // Replace with the current session ID
const cyphlensClient = new Cyphlens(baseUrl);               // Initialize the Cyphlens client
```

#### 2. Listen for Events  
```javascript
// Define the callback for handling events
const onData = (eventType, data) => {
  if (eventType === EventType.MFASwipe) {
    const twoFactorEvent = data;
    console.log("2FA Status:", twoFactorEvent.status);
    // Handle status change (e.g., update UI or trigger logic)
  }
};
const onError = (error) => {
    console.error("SSE Error:", error);
    // Handle the error (e.g., retry connection, notify user)
};
// Start listening to events
cyphlensClient.listen(sessionId, onData, onError);
```

#### 3. Stop Listening  
```javascript
// Stop the event listener when no longer needed 
cyphlensClient.stop();
```

---

## API Reference 📚  

### ```new Cyphlens(baseUrl)```
Creates a new instance of the Cyphlens client.

- **baseUrl**: `string` — Cyphlens API base URL  
  - **Production**: `https://api.prod.cyphlens.com/b2b/v1`
  - **Sandbox**: `https://api.sandbox.cyphlens.com/b2b/v1`

### ```listen(sessionId, callback, errorCallback)```  
Starts listening for Server-Sent Events tied to the specified session.

- **sessionId**: `string` - The session ID to listen for events  
- **callback**: `(eventType: EventType, data: any) => void` - Callback function to handle incoming events  
- **errorCallback**: `(error: Event) => void` - Callback function to handle errors (optional)  

### ```stop()```  
Stops the event listener and closes the connection.

---

## Event Types  
- **`EventType.MFASwipe`**: Triggered when a 2FA swipe event occurs.  
  - **data**: `{ status: string, ... }` - Contains the event details, including the 2FA status.

---

## Types  
- `CyphlensErrorCallback`: `(error: Event) => void` - Type definition for the error callback function.

## Requirements ⚙️  
- **Node.js**: v14.x or higher (for CommonJS/ESM usage)  
- **Browsers**: Modern browsers with support for Server-Sent Events (SSE)  

---

## Contributing 🤝  
We welcome contributions! Please follow these steps:  
1. Fork the repository  
2. Create a feature branch (```git checkout -b feature/YourFeature```)  
3. Commit your changes (```git commit -m "Add your feature"```)  
4. Push to the branch (```git push origin feature/YourFeature```)  
5. Open a Pull Request  

---

## Maintainers

If you're looking for docs on maintaining this package, head over to [docs/](./docs/README.md)

#### Note:
	•	Ensure your local branch is up to date with origin/main (or your target branch) before tagging.
	•	You can verify the tag using git tag.

## License 📄  
This project is licensed under the Cyphlens License. See the [LICENSE](https://cyphlens.com/legal/terms-and-conditions.html) file for details.

