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

feat: Add setHeader method on postgrest builder #1003

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions packages/postgrest/lib/src/postgrest_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
);
}

PostgrestBuilder<T, S, R> setHeader(String key, String value) {
return _copyWith(
headers: {..._headers, key: value},
);
}

Future<T> _execute() async {
final String? method = _method;

Expand Down
7 changes: 7 additions & 0 deletions packages/postgrest/lib/src/postgrest_filter_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,11 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
query.forEach((k, v) => url = appendSearchParams(k, 'eq.$v', url));
return copyWithUrl(url);
}

@override
PostgrestFilterBuilder<T> setHeader(String key, String value) {
return PostgrestFilterBuilder(
_copyWith(headers: {..._headers, key: value}),
);
}
}
12 changes: 12 additions & 0 deletions packages/postgrest/lib/src/postgrest_query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,16 @@ class PostgrestQueryBuilder<T> extends RawPostgrestBuilder<T, T, T> {
count: option,
));
}

@override
PostgrestQueryBuilder<T> setHeader(String key, String value) {
return PostgrestQueryBuilder(
url: _url,
headers: {..._headers, key: value},
httpClient: _httpClient,
method: _method,
schema: _schema,
isolate: _isolate,
);
}
}
7 changes: 7 additions & 0 deletions packages/postgrest/lib/src/postgrest_transform_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ class PostgrestTransformBuilder<T> extends RawPostgrestBuilder<T, T, T> {
PostgrestTransformBuilder<T> copyWithUrl(Uri url) =>
PostgrestTransformBuilder(_copyWith(url: url));

@override
PostgrestTransformBuilder<T> setHeader(String key, String value) {
return PostgrestTransformBuilder(
_copyWith(headers: {..._headers, key: value}),
);
}

/// Performs horizontal filtering with SELECT.
///
/// ```dart
Expand Down
7 changes: 7 additions & 0 deletions packages/postgrest/lib/src/raw_postgrest_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class RawPostgrestBuilder<T, S, R> extends PostgrestBuilder<T, S, R> {
));
}

@override
RawPostgrestBuilder<T, S, R> setHeader(String key, String value) {
return PostgrestFilterBuilder(
_copyWithType(headers: {..._headers, key: value}),
);
}

/// Converts any response that comes from the server into a type-safe response.
///
/// ```dart
Expand Down
7 changes: 7 additions & 0 deletions packages/postgrest/lib/src/response_postgrest_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class ResponsePostgrestBuilder<T, S, R> extends PostgrestBuilder<T, S, R> {
converter: builder._converter,
);

@override
ResponsePostgrestBuilder<T, S, R> setHeader(String key, String value) {
return ResponsePostgrestBuilder(
_copyWith(headers: {..._headers, key: value}),
);
}

/// Converts any response that comes from the server into a type-safe response.
///
/// ```dart
Expand Down
34 changes: 34 additions & 0 deletions packages/postgrest/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,40 @@ void main() {
);
});

test('set header on rpc', () async {
final httpClient = CustomHttpClient();
final postgrest = PostgrestClient(rootUrl, httpClient: httpClient);

await postgrest
.rpc('empty-succ')
.setHeader("myKey", "myValue")
.select()
.head();
expect(httpClient.lastRequest!.headers, containsPair("myKey", "myValue"));

// Other following requests should not have the header
await postgrest.rpc('empty-succ').select().head();
expect(httpClient.lastRequest!.headers,
isNot(containsPair("myKey", "myValue")));
});

test('set header on query builder', () async {
final httpClient = CustomHttpClient();
final postgrest = PostgrestClient(rootUrl, httpClient: httpClient);

await postgrest
.from('empty-succ')
.setHeader("myKey", "myValue")
.select()
.head();
expect(httpClient.lastRequest!.headers, containsPair("myKey", "myValue"));

// Other following requests should not have the header
await postgrest.from('empty-succ').select().head();
expect(httpClient.lastRequest!.headers,
isNot(containsPair("myKey", "myValue")));
});

test('switch schema', () async {
final postgrest = PostgrestClient(rootUrl, schema: 'personal');
final res = await postgrest.from('users').select();
Expand Down
10 changes: 10 additions & 0 deletions packages/postgrest/test/custom_http_client.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import 'package:http/http.dart';

class CustomHttpClient extends BaseClient {
BaseRequest? lastRequest;
@override
Future<StreamedResponse> send(BaseRequest request) async {
lastRequest = request;

if (request.url.path.endsWith("empty-succ")) {
return StreamedResponse(
Stream.empty(),
200,
request: request,
);
}
//Return custom status code to check for usage of this client.
return StreamedResponse(
request.finalize(),
Expand Down
Loading