From efe8e5df7935b75b580e2ead01b9c08ac7b94c2c Mon Sep 17 00:00:00 2001 From: Vinzent Date: Thu, 15 Aug 2024 17:00:04 +0200 Subject: [PATCH] feat: Add setHeader method on postgrest builder (#1003) --- .../postgrest/lib/src/postgrest_builder.dart | 6 ++++ .../lib/src/postgrest_filter_builder.dart | 7 ++++ .../lib/src/postgrest_query_builder.dart | 12 +++++++ .../lib/src/postgrest_transform_builder.dart | 7 ++++ .../lib/src/raw_postgrest_builder.dart | 7 ++++ .../lib/src/response_postgrest_builder.dart | 7 ++++ packages/postgrest/test/basic_test.dart | 34 +++++++++++++++++++ .../postgrest/test/custom_http_client.dart | 10 ++++++ 8 files changed, 90 insertions(+) diff --git a/packages/postgrest/lib/src/postgrest_builder.dart b/packages/postgrest/lib/src/postgrest_builder.dart index fa461e5e..dab11063 100644 --- a/packages/postgrest/lib/src/postgrest_builder.dart +++ b/packages/postgrest/lib/src/postgrest_builder.dart @@ -93,6 +93,12 @@ class PostgrestBuilder implements Future { ); } + PostgrestBuilder setHeader(String key, String value) { + return _copyWith( + headers: {..._headers, key: value}, + ); + } + Future _execute() async { final String? method = _method; diff --git a/packages/postgrest/lib/src/postgrest_filter_builder.dart b/packages/postgrest/lib/src/postgrest_filter_builder.dart index e51d295a..a289f953 100644 --- a/packages/postgrest/lib/src/postgrest_filter_builder.dart +++ b/packages/postgrest/lib/src/postgrest_filter_builder.dart @@ -484,4 +484,11 @@ class PostgrestFilterBuilder extends PostgrestTransformBuilder { query.forEach((k, v) => url = appendSearchParams(k, 'eq.$v', url)); return copyWithUrl(url); } + + @override + PostgrestFilterBuilder setHeader(String key, String value) { + return PostgrestFilterBuilder( + _copyWith(headers: {..._headers, key: value}), + ); + } } diff --git a/packages/postgrest/lib/src/postgrest_query_builder.dart b/packages/postgrest/lib/src/postgrest_query_builder.dart index cba9cbc7..2c1b0eba 100644 --- a/packages/postgrest/lib/src/postgrest_query_builder.dart +++ b/packages/postgrest/lib/src/postgrest_query_builder.dart @@ -256,4 +256,16 @@ class PostgrestQueryBuilder extends RawPostgrestBuilder { count: option, )); } + + @override + PostgrestQueryBuilder setHeader(String key, String value) { + return PostgrestQueryBuilder( + url: _url, + headers: {..._headers, key: value}, + httpClient: _httpClient, + method: _method, + schema: _schema, + isolate: _isolate, + ); + } } diff --git a/packages/postgrest/lib/src/postgrest_transform_builder.dart b/packages/postgrest/lib/src/postgrest_transform_builder.dart index 726eac00..504e083d 100644 --- a/packages/postgrest/lib/src/postgrest_transform_builder.dart +++ b/packages/postgrest/lib/src/postgrest_transform_builder.dart @@ -6,6 +6,13 @@ class PostgrestTransformBuilder extends RawPostgrestBuilder { PostgrestTransformBuilder copyWithUrl(Uri url) => PostgrestTransformBuilder(_copyWith(url: url)); + @override + PostgrestTransformBuilder setHeader(String key, String value) { + return PostgrestTransformBuilder( + _copyWith(headers: {..._headers, key: value}), + ); + } + /// Performs horizontal filtering with SELECT. /// /// ```dart diff --git a/packages/postgrest/lib/src/raw_postgrest_builder.dart b/packages/postgrest/lib/src/raw_postgrest_builder.dart index 71575792..5cf38804 100644 --- a/packages/postgrest/lib/src/raw_postgrest_builder.dart +++ b/packages/postgrest/lib/src/raw_postgrest_builder.dart @@ -41,6 +41,13 @@ class RawPostgrestBuilder extends PostgrestBuilder { )); } + @override + RawPostgrestBuilder 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 diff --git a/packages/postgrest/lib/src/response_postgrest_builder.dart b/packages/postgrest/lib/src/response_postgrest_builder.dart index be69b498..88efac96 100644 --- a/packages/postgrest/lib/src/response_postgrest_builder.dart +++ b/packages/postgrest/lib/src/response_postgrest_builder.dart @@ -16,6 +16,13 @@ class ResponsePostgrestBuilder extends PostgrestBuilder { converter: builder._converter, ); + @override + ResponsePostgrestBuilder 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 diff --git a/packages/postgrest/test/basic_test.dart b/packages/postgrest/test/basic_test.dart index 59d6a407..02f98275 100644 --- a/packages/postgrest/test/basic_test.dart +++ b/packages/postgrest/test/basic_test.dart @@ -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(); diff --git a/packages/postgrest/test/custom_http_client.dart b/packages/postgrest/test/custom_http_client.dart index 6aafcd72..f2da3983 100644 --- a/packages/postgrest/test/custom_http_client.dart +++ b/packages/postgrest/test/custom_http_client.dart @@ -1,8 +1,18 @@ import 'package:http/http.dart'; class CustomHttpClient extends BaseClient { + BaseRequest? lastRequest; @override Future 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(),