Skip to content

Commit

Permalink
feat: Add setHeader method on postgrest builder (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 authored Aug 15, 2024
1 parent 37b1630 commit efe8e5d
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 0 deletions.
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

0 comments on commit efe8e5d

Please sign in to comment.