classify method

MessageType classify(
  1. Map<String, dynamic> json
)

Determine the type of a parsed message.

  • Has 'id' and 'method' => request
  • Has 'id' but no 'method' => response
  • Has 'method' but no 'id' => notification

Implementation

MessageType classify(Map<String, dynamic> json) {
  final hasId = json.containsKey('id');
  final hasMethod = json.containsKey('method');
  if (hasId && hasMethod) return MessageType.request;
  if (hasId && !hasMethod) return MessageType.response;
  if (!hasId && hasMethod) return MessageType.notification;
  throw BridgeError.invalidRequest('Cannot classify message');
}