Skip to content

feat: Add standard client headers #1130

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/supabase/lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import 'package:supabase/src/version.dart';
import 'dart:io' show Platform;

class Constants {
static const Map<String, String> defaultHeaders = {
static String get platform {
return Platform.operatingSystem;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is still draft, but still want to mention that this won't work on web.
You will either need a conditional import, or probably better use a constant to guard web with a definition like the kIsWeb in flutter:

const bool kIsWeb = bool.fromEnvironment('dart.library.js_util');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @Vinzent03

Do we have any test for asserting that it doesn't break in Web? Didn't see any fail on CI.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly not yet, but hopefully #1140 solves this

}

static String get platformVersion {
return Platform.operatingSystemVersion;
}

static final Map<String, String> defaultHeaders = {
'X-Client-Info': 'supabase-dart/$version',
'X-Supabase-Client-Platform': platform,
'X-Supabase-Client-Platform-Version': platformVersion,
};
}
45 changes: 45 additions & 0 deletions packages/supabase/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,51 @@ void main() {
await supabase.dispose();
});

test('X-Supabase-Client-Platform header is set properly', () {
expect(supabase.headers['X-Supabase-Client-Platform'],
Platform.operatingSystem);
expect(supabase.headers['X-Supabase-Client-Platform-Version'],
Platform.operatingSystemVersion);
});
test('X-Supabase-Client-Platform header is set properly on auth', () {
expect(supabase.auth.headers['X-Supabase-Client-Platform'],
Platform.operatingSystem);
expect(supabase.auth.headers['X-Supabase-Client-Platform-Version'],
Platform.operatingSystemVersion);
});

test('X-Supabase-Client-Platform header is set properly on storage', () {
expect(supabase.storage.headers['X-Supabase-Client-Platform'],
Platform.operatingSystem);
expect(supabase.storage.headers['X-Supabase-Client-Platform-Version'],
Platform.operatingSystemVersion);
});

test('X-Supabase-Client-Platform header is set properly on functions', () {
expect(supabase.functions.headers['X-Supabase-Client-Platform'],
Platform.operatingSystem);
expect(supabase.functions.headers['X-Supabase-Client-Platform-Version'],
Platform.operatingSystemVersion);
});

test('X-Supabase-Client-Platform header is set properly on rest', () {
expect(supabase.rest.headers['X-Supabase-Client-Platform'],
Platform.operatingSystem);
expect(supabase.rest.headers['X-Supabase-Client-Platform-Version'],
Platform.operatingSystemVersion);
});

test('X-Supabase-Client-Platform header is set properly on realtime',
() async {
final request = await getRealtimeRequest(
server: mockServer,
supabaseClient: supabase,
);
expect(request.headers['X-Supabase-Client-Platform']?.first,
Platform.operatingSystem);
expect(request.headers['X-Supabase-Client-Platform-Version']?.first,
Platform.operatingSystemVersion);
});
test('X-Client-Info header is set properly on realtime', () async {
final request = await getRealtimeRequest(
server: mockServer,
Expand Down
Loading