@@ -3,7 +3,6 @@ import 'dart:convert';
3
3
import 'package:functions_client/src/constants.dart' ;
4
4
import 'package:functions_client/src/types.dart' ;
5
5
import 'package:http/http.dart' as http;
6
- import 'package:http/http.dart' ;
7
6
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart' ;
8
7
9
8
class FunctionsClient {
@@ -44,6 +43,20 @@ class FunctionsClient {
44
43
/// [headers] : object representing the headers to send with the request
45
44
///
46
45
/// [body] : the body of the request
46
+ ///
47
+ /// ```dart
48
+ /// // Call a standard function
49
+ /// final response = await supabase.functions.invoke('hello-world');
50
+ /// print(response.data);
51
+ ///
52
+ /// // Listen to Server Sent Events
53
+ /// final response = await supabase.functions.invoke('sse-function');
54
+ /// response.data
55
+ /// .transform(const Utf8Decoder())
56
+ /// .listen((val) {
57
+ /// print(val);
58
+ /// });
59
+ /// ```
47
60
Future <FunctionResponse > invoke (
48
61
String functionName, {
49
62
Map <String , String >? headers,
@@ -52,67 +65,42 @@ class FunctionsClient {
52
65
}) async {
53
66
final bodyStr = body == null ? null : await _isolate.encode (body);
54
67
55
- late final Response response;
56
68
final uri = Uri .parse ('$_url /$functionName ' );
57
69
58
70
final finalHeaders = < String , String > {
59
71
..._headers,
60
72
if (headers != null ) ...headers
61
73
};
62
74
63
- switch (method) {
64
- case HttpMethod .post:
65
- response = await (_httpClient? .post ?? http.post)(
66
- uri,
67
- headers: finalHeaders,
68
- body: bodyStr,
69
- );
70
- break ;
71
-
72
- case HttpMethod .get :
73
- response = await (_httpClient? .get ?? http.get )(
74
- uri,
75
- headers: finalHeaders,
76
- );
77
- break ;
78
-
79
- case HttpMethod .put:
80
- response = await (_httpClient? .put ?? http.put)(
81
- uri,
82
- headers: finalHeaders,
83
- body: bodyStr,
84
- );
85
- break ;
86
-
87
- case HttpMethod .delete:
88
- response = await (_httpClient? .delete ?? http.delete)(
89
- uri,
90
- headers: finalHeaders,
91
- );
92
- break ;
93
-
94
- case HttpMethod .patch:
95
- response = await (_httpClient? .patch ?? http.patch)(
96
- uri,
97
- headers: finalHeaders,
98
- body: bodyStr,
99
- );
100
- break ;
101
- }
75
+ final request = http.Request (method.name, uri);
102
76
77
+ finalHeaders.forEach ((key, value) {
78
+ request.headers[key] = value;
79
+ });
80
+ if (bodyStr != null ) request.body = bodyStr;
81
+ final response = await (_httpClient? .send (request) ?? request.send ());
103
82
final responseType = (response.headers['Content-Type' ] ??
104
83
response.headers['content-type' ] ??
105
84
'text/plain' )
106
85
.split (';' )[0 ]
107
86
.trim ();
108
87
109
- final data = switch (responseType) {
110
- 'application/json' => response.bodyBytes.isEmpty
88
+ final dynamic data;
89
+
90
+ if (responseType == 'application/json' ) {
91
+ final bodyBytes = await response.stream.toBytes ();
92
+ data = bodyBytes.isEmpty
111
93
? ""
112
- : await _isolate.decode (utf8.decode (response.bodyBytes)),
113
- 'application/octet-stream' => response.bodyBytes,
114
- _ => utf8.decode (response.bodyBytes),
115
- };
94
+ : await _isolate.decode (utf8.decode (bodyBytes));
95
+ } else if (responseType == 'application/octet-stream' ) {
96
+ data = await response.stream.toBytes ();
97
+ } else if (responseType == 'text/event-stream' ) {
98
+ data = response.stream;
99
+ } else {
100
+ final bodyBytes = await response.stream.toBytes ();
101
+ data = utf8.decode (bodyBytes);
102
+ }
103
+
116
104
if (200 <= response.statusCode && response.statusCode < 300 ) {
117
105
return FunctionResponse (data: data, status: response.statusCode);
118
106
} else {
0 commit comments