Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added null checks for message.data & pong type checking #1027

Merged
merged 10 commits into from
Jan 27, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class RealtimeCallback(

open class RealtimeResponse(
val type: String,
val data: Any
val data: Any?
)

data class RealtimeResponseEvent<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,17 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
when (message.type) {
TYPE_ERROR -> handleResponseError(message)
TYPE_EVENT -> handleResponseEvent(message)
TYPE_PONG -> {}
ChiragAgg5k marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

private fun handleResponseError(message: RealtimeResponse) {
throw message.data.jsonCast<{{ spec.title | caseUcfirst }}Exception>()
throw message.data?.jsonCast<{{ spec.title | caseUcfirst }}Exception>() ?: RuntimeException("Data is not present")
}

private suspend fun handleResponseEvent(message: RealtimeResponse) {
val event = message.data.jsonCast<RealtimeResponseEvent<Any>>()
val event = message.data?.jsonCast<RealtimeResponseEvent<Any>>() ?: return
if (event.channels.isEmpty()) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion templates/flutter/lib/src/realtime_response.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RealtimeResponse {
factory RealtimeResponse.fromMap(Map<String, dynamic> map) {
return RealtimeResponse(
type: map['type'],
data: Map<String, dynamic>.from(map['data']),
data: Map<String, dynamic>.from(map['data'] ?? {}),
);
}

Expand Down
6 changes: 4 additions & 2 deletions templates/react-native/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type Headers = {
}

type RealtimeResponse = {
type: 'error' | 'event' | 'connected' | 'response';
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
type: 'error' | 'event' | 'connected' | 'response' | 'pong';
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown> | undefined;
}

type RealtimeRequest = {
Expand Down Expand Up @@ -301,6 +301,8 @@ class Client {
})
}
break;
case 'pong':
break; // Handle pong response if needed
case 'error':
throw message.data;
default:
Expand Down
6 changes: 4 additions & 2 deletions templates/web/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ type Headers = {
*/
type RealtimeResponse = {
/**
* Type of the response: 'error', 'event', 'connected', 'pong', or 'response'.
* Type of the response: 'error', 'event', 'connected', 'response' or 'pong'.
*/
type: 'error' | 'event' | 'connected' | 'response' | 'pong';

/**
* Data associated with the response based on the response type.
*/
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown> | undefined;
}

/**
Expand Down Expand Up @@ -496,6 +496,8 @@ class Client {
})
}
break;
case 'pong':
break; // Handle pong response if needed
case 'error':
throw message.data;
default:
Expand Down
Loading