Skip to content

Commit

Permalink
feat: setHeader method on postgrest builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Aug 12, 2024
1 parent 37b1630 commit 20a53c1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
6 changes: 6 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,10 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
query.forEach((k, v) => url = appendSearchParams(k, 'eq.$v', url));
return copyWithUrl(url);
}

PostgrestFilterBuilder<T> setHeader(String key, String value) {
return PostgrestFilterBuilder(
_copyWith(headers: {..._headers, key: value}),
);
}
}
11 changes: 11 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,15 @@ class PostgrestQueryBuilder<T> extends RawPostgrestBuilder<T, T, T> {
count: option,
));
}

PostgrestQueryBuilder<T> setHeader(String key, String value) {
return PostgrestQueryBuilder(
url: _url,
headers: {..._headers, key: value},
httpClient: _httpClient,
method: _method,
schema: _schema,
isolate: _isolate,
);
}
}
26 changes: 26 additions & 0 deletions packages/postgrest/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,32 @@ void main() {
);
});

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

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

// Other following requests should not have the header
await postgrest.rpc('func').select();
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('users').setHeader("myKey", "myValue").select();
expect(httpClient.lastRequest!.headers, containsPair("myKey", "myValue"));

// Other following requests should not have the header
await postgrest.from('users').select();
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
3 changes: 3 additions & 0 deletions packages/postgrest/test/custom_http_client.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:http/http.dart';

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

//Return custom status code to check for usage of this client.
return StreamedResponse(
request.finalize(),
Expand Down

0 comments on commit 20a53c1

Please sign in to comment.