Skip to content

Commit 76e9fc2

Browse files
authored
feat: add logLevel parameter to RealtimeClientOptions (supabase#592)
* feat: add realtime log level parameter * append log_level parameter on realtime client rather than Supabase client initialization * minor edit of the comments * update test expected value * remove log_level parameter from supabase test * pass log level to realtime client * update test * remove unnecessary changes
1 parent d0f4e2d commit 76e9fc2

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

packages/realtime_client/lib/realtime_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export 'src/realtime_channel.dart' hide ToType;
22
export 'src/realtime_client.dart';
33
export 'src/realtime_presence.dart';
44
export 'src/transformers.dart' hide getEnrichedPayload, getPayloadRecords;
5+
export 'src/constants.dart' show RealtimeLogLevel;

packages/realtime_client/lib/src/constants.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ extension ChannelEventsExtended on ChannelEvents {
5353
class Transports {
5454
static const String websocket = 'websocket';
5555
}
56+
57+
enum RealtimeLogLevel { info, debug, warn, error }

packages/realtime_client/lib/src/realtime_client.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ class RealtimeClient {
8787
Map<String, String>? headers,
8888
this.params = const {},
8989
this.longpollerTimeout = 20000,
90-
}) : endPoint = '$endPoint/${Transports.websocket}',
90+
RealtimeLogLevel? logLevel,
91+
}) : endPoint = Uri.parse('$endPoint/${Transports.websocket}')
92+
.replace(
93+
queryParameters:
94+
logLevel == null ? null : {'log_level': logLevel.name},
95+
)
96+
.toString(),
9197
headers = {
9298
...Constants.defaultHeaders,
9399
if (headers != null) ...headers,
@@ -415,9 +421,10 @@ class RealtimeClient {
415421
}
416422

417423
var endpoint = Uri.parse(url);
418-
final searchParams = Map<String, dynamic>.from(endpoint.queryParameters);
419-
params.forEach((k, v) => searchParams[k] = v);
420-
endpoint = endpoint.replace(queryParameters: searchParams);
424+
endpoint = endpoint.replace(queryParameters: {
425+
...endpoint.queryParameters,
426+
...params,
427+
});
421428

422429
return endpoint.toString();
423430
}

packages/supabase/lib/src/realtime_client_options.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:realtime_client/realtime_client.dart';
2+
13
/// {@template realtime_client_options}
24
/// Options to pass to the RealtimeClient.
35
/// {@endtemplate}
@@ -7,6 +9,12 @@ class RealtimeClientOptions {
79
/// Defaults to 10 events per second
810
final int? eventsPerSecond;
911

12+
/// Level of realtime server logs to to be logged
13+
final RealtimeLogLevel? logLevel;
14+
1015
/// {@macro realtime_client_options}
11-
const RealtimeClientOptions({this.eventsPerSecond});
16+
const RealtimeClientOptions({
17+
this.eventsPerSecond,
18+
this.logLevel,
19+
});
1220
}

packages/supabase/lib/src/supabase_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ class SupabaseClient {
278278
if (eventsPerSecond != null) 'eventsPerSecond': '$eventsPerSecond'
279279
},
280280
headers: headers,
281+
logLevel: options.logLevel,
281282
);
282283
}
283284

packages/supabase/test/client_test.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import 'utils.dart';
88

99
void main() {
1010
group('Standard Header', () {
11-
const supabaseUrl = '';
12-
const supabaseKey = '';
11+
const supabaseUrl = 'https://nlbsnpoablmsiwndbmer.supabase.co';
12+
const supabaseKey =
13+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im53emxkenlsb2pyemdqemloZHJrIiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODQxMzI2ODAsImV4cCI6MTk5OTcwODY4MH0.MU-LVeAPic93VLcRsHktxzYtBKBUMWAQb8E-0AQETPs';
1314
late SupabaseClient client;
1415

1516
setUp(() {
@@ -31,6 +32,29 @@ void main() {
3132
client.storage.headers['X-Client-Info']!.split('/').first;
3233
expect(xClientHeaderBeforeSlash, 'supabase-dart');
3334
});
35+
36+
test('realtime URL is properly being set', () {
37+
var realtimeWebsocketURL = Uri.parse(client.realtime.endPointURL);
38+
expect(
39+
realtimeWebsocketURL.queryParameters,
40+
containsPair('apikey', supabaseKey),
41+
);
42+
expect(realtimeWebsocketURL.queryParameters['log_level'], isNull);
43+
44+
client = SupabaseClient(supabaseUrl, supabaseKey,
45+
realtimeClientOptions:
46+
RealtimeClientOptions(logLevel: RealtimeLogLevel.info));
47+
48+
realtimeWebsocketURL = Uri.parse(client.realtime.endPointURL);
49+
expect(
50+
realtimeWebsocketURL.queryParameters,
51+
containsPair('apikey', supabaseKey),
52+
);
53+
expect(
54+
realtimeWebsocketURL.queryParameters,
55+
containsPair('log_level', 'info'),
56+
);
57+
});
3458
});
3559

3660
group('auth', () {

0 commit comments

Comments
 (0)