@listendoctor/sdk
v0.0.8
Published
A customizable widget for laboratory data management.
Downloads
6
Readme
Listen.Doctor JavaScript SDK
Official JavaScript SDK for Listen.Doctor. Power your apps with world-class speech and Language AI models.
Installation
To install the necessary dependencies, run:
npm install
Building the Project
To build the project, run:
npm run build
The build output will be placed in the dist
directory.
Project Structure
The project is organized into several directories to separate the application logic, public assets, and client-side resources efficiently:
/
├── dist/ # Compiled and bundled output files
│
├── src/ # Source files for the project
│ ├── services/ # Source code for services
│ │ ├── laboratory.ts
│ │ ├── main.ts
│ │ └── record.ts
│ │
│ ├── utils/ # Utility functions and helpers
│ │ ├── config.ts
│ │ ├── iam.ts
│ │ └── types.ts
│ │
│ └── index.ts # Main entry point for the TypeScript source code
│
├── test/ # Source files for the project
│ ├── dist/ # Compiled and bundled output files
│ │
│ ├── src/
│ │ └── index.ts
│ │
│ ├── index.html
│ ├── gulpfile.js
│ ├── package.json # Project metadata and dependencies
│ └── tsconfig.json # TypeScript configuration file
│
├── .gitignore # Files and directories to be ignored by Git
├── .npmignore # Files and directories to be ignored by NPM
├── package.json # Project metadata and dependencies
├── README.md # Documentation for the project
└── tsconfig.json # TypeScript configuration file
Dependencies
The project relies on the following dependencies:
socket.io-client
(version ^4.7.5): For real-time, bidirectional communication between the client and server.
Installation
# Install the Listen.Doctor JS SDK
# https://github.com/SocialDiabetes/listen.doctor-js-sdk
npm i @listendoctor/sdk
# Install dotenv to protect your credentials
npm install dotenv
Development Dependencies
typescript
(version ^5.5.4): TypeScript compiler.
Usage Guide
Customer Interaction Workflow with ListenDoctor Library
This guide provides an overview of how to interact with the ListenDoctor
library, including the process of authentication, token refresh, and accessing modules such as Lab and Record.
1. Initialization
Begin by initializing the ListenDoctor
class with your clientId
and clientSecret
:
import ListenDoctor from "@listendoctor/sdk";
require("dotenv").config();
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const ld = new ListenDoctor(clientId, clientSecret);
2. Authentication Process
- Upon initialization, the
ListenDoctor
instance triggers an authentication process. - The authentication request is sent to the server with the provided credentials.
- If successful, an access token and a refresh token are obtained and stored.
3. Accessing Modules (Lab/Record)
To access the Lab
or Record
module, use the corresponding methods:
- Token Validity: The
IAM
instance checks if the access token is valid. If it's not valid or is about to expire, it will attempt to refresh the token. - Scope Check: The system checks if the token has the required scope to access the requested module.
- If the scope is valid, a new instance of the requested module (
Record
orLaboratory
) is returned. - If the scope is invalid, an unauthorized error is thrown.
- If the scope is valid, a new instance of the requested module (
const record = await ld.record();
const lab = await ld.lab();
4. Token Refresh
- If the access token is near expiration, the
IAM
class automatically attempts to refresh it using the refresh token. - If the refresh fails (e.g., the refresh token is invalid), an error is logged, and access is denied.
5. Handling Unauthorized Access
- If the token lacks the required scope or the token refresh fails, the
IAM
class throws an unauthorized error, indicating that access to the requested module is denied.
Example
import ListenDoctor from "@listendoctor/sdk";
require("dotenv").config();
const initListenDoctor = async () => {
const client = process.env.CLIENT_ID;
const secret = process.env.CLIENT_SECRET;
if (!client || !secret) {
throw new Error("Missing client id or client secret");
}
// STEP 1: Create a ListenDoctor client using the Client ID and Secret
const ld = new ListenDoctor(client, secret);
// STEP 2: Get the Record instance
try {
const record = await ld.record();
console.log("Record:", record);
} catch (error) {
console.error("Error with record:", error);
}
// STEP 3: Get the Laboratory instance
let lab;
try {
lab = await ld.lab();
console.log("Lab:", lab);
if (!lab) {
throw new Error("No lab available");
}
} catch (error) {
console.error("Error with lab:", error);
}
};
initListenDoctor();