Skip to content

Commit

Permalink
feat: add additional http request headers #76 (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyford authored and slightfoot committed Dec 21, 2024
1 parent 9290362 commit c724afd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/clerk_auth/lib/clerk_auth.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Package that will allow you to authenticate and use Clerk from Dart code.
library clerk_auth;

export 'clerk_constants.dart';
export 'src/clerk_api/api.dart';
export 'src/clerk_auth/auth.dart';
export 'src/clerk_auth/persistor.dart';
Expand Down
8 changes: 8 additions & 0 deletions packages/clerk_auth/lib/clerk_constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// constant values
sealed class ClerkConstants {
/// value for the `clerk-api-version` header in API requests
static const clerkApiVersion = '2024-10-01';

/// value for the `x-flutter-sdk-version` header in API requests
static const flutterSdkVersion = '0.0.4-dev';
}
6 changes: 6 additions & 0 deletions packages/clerk_auth/lib/src/clerk_api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class Api with Logging {
static const _kErrorsKey = 'errors';
static const _kClientKey = 'client';
static const _kResponseKey = 'response';
static const _kClerkAPIVersion = 'clerk-api-version';
static const _kXFlutterSDKVersion = 'x-flutter-sdk-version';
static const _kXMobile = 'x-mobile';

static const _defaultPollDelay = Duration(seconds: 55);

Expand Down Expand Up @@ -674,6 +677,9 @@ class Api with Logging {
: 'application/x-www-form-urlencoded',
if (_tokenCache.clientToken.isNotEmpty) //
HttpHeaders.authorizationHeader: _tokenCache.clientToken,
_kClerkAPIVersion: ClerkConstants.clerkApiVersion,
_kXFlutterSDKVersion: ClerkConstants.flutterSdkVersion,
_kXMobile: '1',
...?headers,
};
}
Expand Down
8 changes: 6 additions & 2 deletions packages/clerk_auth/lib/src/clerk_auth/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ class DefaultHttpClient implements HttpClient {
}) async {
final request = Request(method.toString(), uri);

if (headers != null) request.headers.addAll(headers);
if (headers case Map<String, String> headers) {
request.headers.addAll(headers);
}

if (params != null) request.bodyFields = params.toStringMap();
if (params case Map<String, dynamic> params) {
request.bodyFields = params.toStringMap();
}

final streamedResponse = await request.send();
return Response.fromStream(streamedResponse);
Expand Down
17 changes: 12 additions & 5 deletions packages/clerk_auth/test/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class TestHttpClient implements HttpClient {
final hdrs = {...?headers}
..remove(HttpHeaders.acceptHeader)
..remove(HttpHeaders.contentTypeHeader)
..remove(HttpHeaders.authorizationHeader);
..remove(HttpHeaders.authorizationHeader)
..remove('clerk-api-version')
..remove('x-flutter-sdk-version')
..remove('x-mobile');

final queryParams = {
...uri.queryParameters,
Expand All @@ -77,14 +80,18 @@ class TestHttpClient implements HttpClient {
..remove('_is_native')
..remove('_clerk_js_version');

final path =
'${Uri(path: uri.path, queryParameters: queryParams.isNotEmpty ? queryParams : null)}';
final path = Uri(
path: uri.path,
queryParameters: queryParams.isNotEmpty ? queryParams : null,
).toString();

return [
method,
path,
if (hdrs.isNotEmpty) _mapToString(hdrs),
if (body?.isNotEmpty == true) _mapToString(body!),
if (hdrs.isNotEmpty) //
_mapToString(hdrs),
if (body?.isNotEmpty == true) //
_mapToString(body!),
].join(' ');
}

Expand Down

0 comments on commit c724afd

Please sign in to comment.