All files / src/types websocket.ts

64.28% Statements 72/112
100% Branches 2/2
0% Functions 0/6
64.28% Lines 72/112

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1132x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x           2x 2x               2x               2x 2x       2x 2x               2x 2x           2x 2x             2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
// src/types/websocket.ts
 
/**
 * WebSocket message types for real-time notifications
 */
 
/**
 * Deal information for real-time notifications
 */
export interface DealInfo {
  item_name: string;
  best_price_in_cents: number;
  store_name: string;
  store_id: number;
}

/**
 * Base WebSocket message structure
 */
export interface WebSocketMessage<T = unknown> {
  type: WebSocketMessageType;
  data: T;
  timestamp: string;
}

/**
 * Available WebSocket message types
 */
export type WebSocketMessageType =
  | 'deal-notification'
  | 'system-message'
  | 'ping'
  | 'pong'
  | 'error'
  | 'connection-established';

/**
 * Deal notification message payload
 */
export interface DealNotificationData {
  notification_id?: string;
  deals: DealInfo[];
  user_id: string;
  message: string;
}

/**
 * System message payload
 */
export interface SystemMessageData {
  message: string;
  severity: 'info' | 'warning' | 'error';
}

/**
 * Error message payload
 */
export interface ErrorMessageData {
  message: string;
  code?: string;
}

/**
 * Connection established payload
 */
export interface ConnectionEstablishedData {
  user_id: string;
  message: string;
}
 
/**
 * Type-safe message creators
 */
export const createWebSocketMessage = {
  dealNotification: (data: DealNotificationData): WebSocketMessage<DealNotificationData> => ({
    type: 'deal-notification',
    data,
    timestamp: new Date().toISOString(),
  }),
 
  systemMessage: (data: SystemMessageData): WebSocketMessage<SystemMessageData> => ({
    type: 'system-message',
    data,
    timestamp: new Date().toISOString(),
  }),
 
  error: (data: ErrorMessageData): WebSocketMessage<ErrorMessageData> => ({
    type: 'error',
    data,
    timestamp: new Date().toISOString(),
  }),
 
  connectionEstablished: (
    data: ConnectionEstablishedData,
  ): WebSocketMessage<ConnectionEstablishedData> => ({
    type: 'connection-established',
    data,
    timestamp: new Date().toISOString(),
  }),
 
  ping: (): WebSocketMessage<Record<string, never>> => ({
    type: 'ping',
    data: {},
    timestamp: new Date().toISOString(),
  }),
 
  pong: (): WebSocketMessage<Record<string, never>> => ({
    type: 'pong',
    data: {},
    timestamp: new Date().toISOString(),
  }),
};