TypeScript Definition

/** Widget element */
declare class SanaWidgetHTMLElement extends HTMLElement {
    displaymode: "LIGHTBOX" | "EMBEDDED";
    operationmode: "DEFAULT" | "SINGLE_ROOM";
    locale: Locale;
    singleroomid: string;
    uicustomizations: UICustomizations;
    whitelabelid: string;
    hiddenonstartup: boolean;
    singleroomheader: "DEFAULT" | "CONTEXT" | "NONE";
    uploadcallbackconfig: UploadCallbacksConfig;
    externaluserid: string;
    notificationicon: string;
    notificationappname: string;
    ssoname: string;
    securitytokenttl: number;

    show(): void;
    hide(): void;
    toggleVisibility(): boolean;
    deployLightbox(): void;
    foldLightbox(): void;
    getUnreadMessageNumber(): Promise<number>;
    markAllAsRead(): Promise<void>;
    getRoom(roomId: string): Promise<Room>;
    sendMessage(roomId: string, message: AbstractMessage): Promise<void>;
    sendFileMessage(securityToken: string, message: FileMessage): Promise<void>;
    createRoom(roomCreationRequest: RoomCreationRequest): Promise<Room>;
    enableLogs(): Promise<void>;
    disableLogs(): Promise<void>;
    logout(): Promise<void>;
    requestNotificationsPermission(): Promise<void>;
    disableNotifications(): Promise<void>;
    isNotificationsEnabled(): Promise<boolean>;

}

// ---- WIDGET API TYPES ----
/**
 * Erreur remonté en cas d'upload de fichier avec un securityToken invalid ou expiré
 */
declare class InvalidSecurityTokendError extends Error {
    constructor(securityToken: string);
}

declare class NonExistentUsersError extends Error {
    nonExistentUsers: string[];
    constructor(nonExistentUsers: string[]);
}

/**
 * Erreur remonté en cas d'erreur lors de l'upload de fichier avec un Uuid
 */
declare class SendFileError extends Error {
    constructor(uuid: string, error: string);
}

declare class SingleRoomError extends Error {
    constructor(message: string);
}

declare class WhitelabelError extends Error {
    constructor(message: string, error?: Error);
}

declare class ChatWidgetEvent {
    'm.rofim.is-ready': boolean;
    'm.rofim.display-mode': string;
    'm.rofim.operation-mode': string;
    'm.rofim.user-id': string;
    'm.rofim.unread-messages': number;
    constructor(ready: boolean, displayMode: 'LIGHTBOX' | 'EMBEDDED', operationMode: 'DEFAULT' | 'SINGLE_ROOM', userID: string, unreadMessages: number);
}

declare class AllNotificationsHaveBeenReadEvent extends ChatWidgetEvent {
    constructor(ready: boolean, displayMode: 'LIGHTBOX' | 'EMBEDDED', operationMode: 'DEFAULT' | 'SINGLE_ROOM', userID: string, unreadMessages: number);
}

declare class LightboxDeployedEvent extends ChatWidgetEvent {
    constructor(ready: boolean, displayMode: 'LIGHTBOX' | 'EMBEDDED', operationMode: 'DEFAULT' | 'SINGLE_ROOM', userID: string, unreadMessages: number);
}

declare class LightboxFoldedEvent extends ChatWidgetEvent {
    constructor(ready: boolean, displayMode: 'LIGHTBOX' | 'EMBEDDED', operationMode: 'DEFAULT' | 'SINGLE_ROOM', userID: string, unreadMessages: number);
}

declare class MessageReadEvent extends ChatWidgetEvent {
    'm.rofim.message-read': {
        'm.rofim.room.room-id': string;
        'm.rofim.room.unread-message': number;
    };
    constructor(ready: boolean, displayMode: 'LIGHTBOX' | 'EMBEDDED', operationMode: 'DEFAULT' | 'SINGLE_ROOM', userID: string, roomID: string, unreadMessages: number, roomUnreadMessage: number);
}

declare class NewMessageEvent extends ChatWidgetEvent {
    'm.rofim.new-message': {
        'm.rofim.room.room-id': string;
        'm.rofim.message.from': string;
        'm.rofim.room.unread-message': number;
        'm.rofim.message.type': 'TEXT' | 'FILE';
    };
    constructor(ready: boolean, displayMode: 'LIGHTBOX' | 'EMBEDDED', operationMode: 'DEFAULT' | 'SINGLE_ROOM', userID: string, roomID: string, from: string, unreadMessages: number, roomUnreadMessage: number, messageType: 'TEXT' | 'FILE');
}

declare class ReadyEvent extends ChatWidgetEvent {
    'm.rofim.ready': {
        'm.rofim.failure-reason'?: string;
        'm.rofim.singleroom-id'?: string;
    };
    constructor(ready: boolean, displayMode: 'LIGHTBOX' | 'EMBEDDED', operationMode: 'DEFAULT' | 'SINGLE_ROOM', userID: string, unreadMessages: number, singleroomId?: string, failureReason?: string);
}

declare abstract class AbstractMessage {
    abstract type: 'TEXT' | 'PATIENT' | 'FILE';
}

declare class FileMessage extends AbstractMessage {
    type: 'TEXT' | 'PATIENT' | 'FILE';
    securityToken: string;
    uploadFile: AbstractUploadFile;
    constructor(securityToken: string, uploadFile: AbstractUploadFile);
}
declare abstract class AbstractUploadFile {
    /** Type de Fichier à uploader */
    abstract type: 'BLOB' | 'URL';
}

declare class PatientMessage extends AbstractMessage {
    type: 'TEXT' | 'PATIENT' | 'FILE';
    lastName: string;
    firstName: string;
    maidenName: string;
    age: number;
    birthdate: Date;
    sex: string;
    nss: string;
    ins: string;
    patientId: string;
    establishmentPatientId: string;
    establishmentPatientUrl: string;
    establishmentPatientStayId: string;
    twinRank: number;
    email: string;
    mobilePhone: string;
    body?: string;
    formattedBody?: string;
    constructor(lastName: string, firstName: string, maidenName: string, age: number, birthdate: Date, sex: string, nss: string, ins: string, patientId: string, establishmentPatientId: string, establishmentPatientUrl: string, establishmentPatientStayId: string, twinRank: number, email: string, mobilePhone: string, body?: string, formattedBody?: string);
}

declare class Room {
    roomId: string;
    name: string;
    members: string[];
    constructor(roomId: string, name: string, members: string[]);
}

declare class RoomCreationRequest {
    roomName: string;
    membersToInvite: string[];
    act?: Act;
    constructor(roomName: string, membersToInvite: string[]);
}
declare class Act {
    actType: string;
    actId: string;
    actUrl: string;
    constructor(actType: string, actId: string, actUrl: string);
}

declare class TextMessage extends AbstractMessage {
    type: 'TEXT' | 'PATIENT' | 'FILE';
    text: string;
    constructor(text: string);
}

/**
 * Configuration générale des callbacks d'upload de fichier.
 */
declare interface UploadCallbacksConfig {
    /** Liste des callbacks disponibles */
    callbacks: CallbackConfig[];
}
/** Le context qui sera définie dans les paramètres d'appels de la Callback */
declare interface CallbackContext {
    /** Identifiant à usage unique utilisé lors de l'upload des fichiers via API. */
    securityToken: string;
    /** La clé de la callback qui est éxécuté */
    callbackKey: string;
}
declare interface CallbackReference {
    /** Type de référence : 'function' pour fonction directe, 'alias' pour alias global */
    type: 'FUNCTION_NAME' | 'FUNCTION_OBJECT';
    /** La fonction elle-même (si type='function') ou l'alias string (si type='alias') */
    value: Function | string;
}
/**
 * Configuration d'une callback d'upload
 */
declare interface CallbackConfig {
    /** Identifiant unique de la callback */
    key: string;
    /** Nom affiché à l'utilisateur */
    buttonLabel: string;
    /** Description optionnelle (title) */
    buttonTitle?: string;
    /** Icône à afficher (classe CSS ou nom d'icône) */
    icon?: string;
    /** La fonction callback à exécuter OU l'alias de la fonction présente dans le DOM*/
    callbackRef: CallbackReference;
}

declare enum Locale {
    DE_DE = "de-DE",
    EN_GB = "en-GB",
    ES_ES = "es-ES",
    FR_FR = "fr-FR",
    IT_IT = "it-IT",
    PT_PT = "pt-PT"
}

declare interface UICustomizations {
    primaryColor?: string;
    surfaceColor?: string;
    radius?: number;
    sentMessageBackgroundColor?: string;
    sentMessageTextColor?: string;
    readMessageIconColor?: string;
}

Using TypeScript definition files helps ensure your code is more robust and less prone to errors by providing type safety and better development tools.

Copy the sana-widget.d.ts in your project, then import the elements you are using in your code from the sana-widget.d.ts