11import 'dart:io' ;
22
33import 'package:dio/dio.dart' ;
4+ import 'package:flutter/foundation.dart' ;
45import 'package:dio/adapter.dart' ;
56import 'package:dio_cookie_manager/dio_cookie_manager.dart' ;
67import 'package:cookie_jar/cookie_jar.dart' ;
@@ -20,17 +21,21 @@ class Client {
2021 PersistCookieJar cookieJar;
2122
2223 Client ({this .endPoint = 'https://appwrite.io/v1' , this .selfSigned = false , Dio http}) : this .http = http ?? Dio () {
23-
24- type = (Platform .isIOS) ? 'ios' : type;
25- type = (Platform .isMacOS) ? 'macos' : type;
26- type = (Platform .isAndroid) ? 'android' : type;
27- type = (Platform .isLinux) ? 'linux' : type;
28- type = (Platform .isWindows) ? 'windows' : type;
29- type = (Platform .isFuchsia) ? 'fuchsia' : type;
24+ // Platform is not supported in web so if web, set type to web automatically and skip Platform check
25+ if (kIsWeb) {
26+ type = 'web' ;
27+ }else {
28+ type = (Platform .isIOS) ? 'ios' : type;
29+ type = (Platform .isMacOS) ? 'macos' : type;
30+ type = (Platform .isAndroid) ? 'android' : type;
31+ type = (Platform .isLinux) ? 'linux' : type;
32+ type = (Platform .isWindows) ? 'windows' : type;
33+ type = (Platform .isFuchsia) ? 'fuchsia' : type;
34+ }
3035
3136 this .headers = {
3237 'content-type' : 'application/json' ,
33- 'x-sdk-version' : 'appwrite:dart :0.2.3 ' ,
38+ 'x-sdk-version' : 'appwrite:flutter :0.3.0-dev.1 ' ,
3439 };
3540
3641 this .config = {};
@@ -78,17 +83,20 @@ class Client {
7883
7984 Future init () async {
8085 if (! initialized) {
81- final Directory cookieDir = await _getCookiePath ();
82-
83- cookieJar = new PersistCookieJar (dir: cookieDir.path);
86+ // if web skip cookie implementation and origin header as those are automatically handled by browsers
87+ if (! kIsWeb) {
88+ final Directory cookieDir = await _getCookiePath ();
89+ cookieJar = new PersistCookieJar (dir: cookieDir.path);
90+ this .http.interceptors.add (CookieManager (cookieJar));
91+ PackageInfo packageInfo = await PackageInfo .fromPlatform ();
92+ addHeader ('Origin' , 'appwrite-' + type + '://' + packageInfo.packageName);
93+ }else {
94+ // if web set httpClientAdapter as BrowserHttpClientAdapter with withCredentials true to make cookies work
95+ this .http.options.extra['withCredentials' ] = true ;
96+ }
8497
8598 this .http.options.baseUrl = this .endPoint;
8699 this .http.options.validateStatus = (status) => status < 400 ;
87- this .http.interceptors.add (CookieManager (cookieJar));
88-
89- PackageInfo packageInfo = await PackageInfo .fromPlatform ();
90-
91- addHeader ('Origin' , 'appwrite-' + type + '://' + packageInfo.packageName);
92100 }
93101 }
94102
@@ -114,6 +122,10 @@ class Client {
114122 }
115123
116124 if (method == HttpMethod .get ) {
125+ params.keys.forEach ((key) {if (params[key] is int || params[key] is double ) {
126+ params[key] = params[key].toString ();
127+ }});
128+
117129 return http.get (path, queryParameters: params, options: options);
118130 } else {
119131 return http.request (path, data: params, options: options);
0 commit comments