@sirenapp/js-sdk
Version:
JavaScript middleware designed to streamline interaction for managing and displaying in-app notifications seamlessly
268 lines (267 loc) • 7.25 kB
TypeScript
import "promise-polyfill/src/polyfill";
import type { EventType, ApiOperationType, BulkUpdateType } from "./constants/generic";
/**
* An interface representing the response structure of the Notifications API.
*/
export interface NotificationsApiResponse {
data?: NotificationDataType[] | null;
error?: SirenErrorType | null;
meta?: {
currentPage: string;
first: string;
last: string;
pageSize: string;
totalElements: string;
totalPages: string;
} | null;
}
/**
* An interface representing the structure of a single notification data object.
*/
export interface NotificationDataType {
id: string;
createdAt: string;
message: {
channel: string;
header: string;
subHeader: string;
body: string;
actionUrl: string;
thumbnailUrl?: string | null;
avatar: {
imageUrl: string;
actionUrl: string | null;
};
additionalData: string;
};
requestId: string;
isRead: boolean;
}
/**
* An interface representing the response structure of the Unviewed Count API.
*/
export interface UnviewedCountApiResponse {
data?: {
id: string;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
createdBy: string;
updatedBy: string;
deletedBy: string | null;
projectEnvironmentId: string;
referenceId: string;
providerIntegrationId: string;
lastOpenedAt: string;
totalUnviewed: number;
} | null;
error?: SirenErrorType | null;
}
/**
* A type representing the structure of a siren error object .
*/
export type SirenErrorType = {
Type: string;
Code: string;
Message: string;
};
/**
* A type for a function that takes a `SirenErrorType` object as an argument.
* This function is used as a callback function to handle errors.
*/
export type ErrorCallbackType = (response: SirenErrorType) => void;
declare global {
interface Window {
Siren: object;
}
}
/**
* A type defining the configuration options for making HTTP requests.
*/
export type HttpOptions = {
errorMessage?: string;
headers?: any;
method?: string;
path: string;
token: string;
errorLevel?: ErrorLevel;
queryParams?: FetchNotificationsParamsType;
onError?: ErrorCallbackType;
operation?: ApiOperationType;
};
export type ErrorLevel = 'silent' | 'info' | 'warn' | 'error' | 'fatal';
/**
* An interface defining the structure of query parameters used for fetching notifications.
*/
export type FetchNotificationsParamsType = {
/**
* Optional page number for pagination (defaults to 1).
*/
page?: number;
/**
* Optional number of notifications to retrieve per page (defaults to 10).
*/
size?: number;
/**
* Optional ISO formatted date and time string representing the starting date filter for notifications.
*/
start?: string;
/**
* Optional ISO formatted date and time string representing the ending date filter for notifications.
*/
end?: string;
/**
* Optional boolean flag indicating whether to filter notifications based on their read status (true for read, false for unread).
* Excluding the flag will retrieve both read and unread notifications
*/
isRead?: boolean;
/**
* Option for sorting notifications
*/
sort?: "createdAt" | "updatedAt";
/**
* Option for filtering based on category
*/
category?: string;
};
/**
* Defining the configuration options for initialization.
*/
export type InitConfigType = {
token: string;
recipientId: string;
onError: ErrorCallbackType;
actionCallbacks?: ActionCallbackType;
};
/**
* Defining the structure of callback functions for handling different notification-related events.
*/
export type ActionCallbackType = {
onEventReceive?: (response: NotificationsApiResponse | UnviewedCountApiResponse, eventType: EventType) => void;
};
/**
* Defining the structure of a message object within a notification.
*/
type Message = {
channel: string;
header: string;
subHeader: string;
body: string;
actionUrl: string;
thumbnailUrl?: string | null;
avatar: {
imageUrl: string;
actionUrl: string | null;
};
additionalData: string;
};
/**
* Defining the structure of a inAppRecipient object within a notification.
*/
type InAppRecipient = {
id: string;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
createdBy: string;
updatedBy: string;
deletedBy: string | null;
projectEnvironmentId: string;
referenceId: string;
providerIntegrationId: string;
lastOpenedAt: string;
};
/**
* Defining the structure of the response object from the Unviewed Count API.
*/
export type UnViewedCountApiResponse = {
id: string;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
createdBy: string;
updatedBy: string;
deletedBy: string | null;
projectEnvironmentId: string;
referenceId: string;
providerIntegrationId: string;
lastOpenedAt: string;
totalUnviewed: number;
};
/**
* Defining the structure of the response object from the Verify Token API.
*/
export type VerifyTokenResponse = {
data?: {
status: string;
} | null;
error?: SirenErrorType | null;
};
/**
* Defining the structure of the response object for various notification actions.
*/
export type ActionResponse = {
data?: {
status: string;
} | null;
error?: SirenErrorType | null;
};
/**
* Defining the structure of a notification object containing detailed information.
*/
export type NotificationDetails = {
id: string;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
createdBy: string;
updatedBy: string;
deletedBy: string | null;
projectEnvironmentId: string;
message: Message;
inAppRecipient: InAppRecipient;
isRead: boolean;
isDelivered: boolean;
requestId: string;
};
/**
* Defining the structure of the response object for marking a notification as viewed.
*/
export type MarkAsViewedResponse = {
data?: Omit<UnViewedCountApiResponse, 'totalUnviewed'> | null;
error?: SirenErrorType | null;
};
/**
* Defining the structure of the response object containing notification api response data.
*/
export type NotificationDataResponse = {
data?: NotificationDetails | null;
error?: SirenErrorType | null;
};
/**
* Defining the structure of the response object returned to user while calling unviewed count api.
*/
export type UnviewedCountReturnResponse = {
data?: {
unviewedCount: number;
} | null;
error?: SirenErrorType | null;
};
/**
* Defining the structure of the input param from user calling bulk update functions.
*/
export type BulkUpdateParamsType = {
startDate: string;
isRead?: boolean;
category?: string;
};
/**
* Defining the structure of input param from user while calling bulk update api.
*/
export type BulkUpdateApiParams = {
until: string;
isRead?: boolean;
operation: BulkUpdateType;
category?: string;
};
export {};