Skip to content

Commit 0f3182c

Browse files
authored
fix: Use per client fetch instance (supabase#818)
* fix: use per client fetch instance * test: adapt tests to missing global fetch * test: remove mocktail usage
1 parent a789d79 commit 0f3182c

File tree

8 files changed

+82
-187
lines changed

8 files changed

+82
-187
lines changed

packages/storage_client/lib/src/fetch.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import 'package:storage_client/src/types.dart';
1111

1212
import 'file_io.dart' if (dart.library.js) './file_stub.dart';
1313

14-
Fetch storageFetch = Fetch();
15-
1614
class Fetch {
1715
final Client? httpClient;
1816

packages/storage_client/lib/src/storage_bucket_api.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import 'package:http/http.dart';
2+
import 'package:meta/meta.dart';
23
import 'package:storage_client/src/fetch.dart';
34
import 'package:storage_client/src/types.dart';
45

56
class StorageBucketApi {
67
final String url;
78
final Map<String, String> headers;
9+
@internal
10+
late Fetch storageFetch;
811

912
StorageBucketApi(this.url, this.headers, {Client? httpClient}) {
1013
storageFetch = Fetch(httpClient);

packages/storage_client/lib/src/storage_client.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ class SupabaseStorageClient extends StorageBucketApi {
4848
///
4949
/// [id] The bucket id to operate on.
5050
StorageFileApi from(String id) {
51-
return StorageFileApi(url, headers, id, _defaultRetryAttempts);
51+
return StorageFileApi(
52+
url,
53+
headers,
54+
id,
55+
_defaultRetryAttempts,
56+
storageFetch,
57+
);
5258
}
5359

5460
void setAuth(String jwt) {

packages/storage_client/lib/src/storage_file_api.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ class StorageFileApi {
1010
final Map<String, String> headers;
1111
final String? bucketId;
1212
final int _retryAttempts;
13+
final Fetch _storageFetch;
1314

1415
const StorageFileApi(
1516
this.url,
1617
this.headers,
1718
this.bucketId,
1819
this._retryAttempts,
20+
this._storageFetch,
1921
);
2022

2123
String _getFinalPath(String path) {
@@ -51,7 +53,7 @@ class StorageFileApi {
5153
assert(retryAttempts == null || retryAttempts >= 0,
5254
'retryAttempts has to be greater or equal to 0');
5355
final finalPath = _getFinalPath(path);
54-
final response = await storageFetch.postFile(
56+
final response = await _storageFetch.postFile(
5557
'$url/object/$finalPath',
5658
file,
5759
fileOptions,
@@ -86,7 +88,7 @@ class StorageFileApi {
8688
assert(retryAttempts == null || retryAttempts >= 0,
8789
'retryAttempts has to be greater or equal to 0');
8890
final finalPath = _getFinalPath(path);
89-
final response = await storageFetch.postBinaryFile(
91+
final response = await _storageFetch.postBinaryFile(
9092
'$url/object/$finalPath',
9193
data,
9294
fileOptions,
@@ -121,7 +123,7 @@ class StorageFileApi {
121123
var url = Uri.parse('${this.url}/object/upload/sign/$finalPath');
122124
url = url.replace(queryParameters: {'token': token});
123125

124-
await storageFetch.putFile(
126+
await _storageFetch.putFile(
125127
url.toString(),
126128
file,
127129
fileOptions,
@@ -155,7 +157,7 @@ class StorageFileApi {
155157
var url = Uri.parse('${this.url}/object/upload/sign/$path0');
156158
url = url.replace(queryParameters: {'token': token});
157159

158-
await storageFetch.putBinaryFile(
160+
await _storageFetch.putBinaryFile(
159161
url.toString(),
160162
data,
161163
fileOptions,
@@ -175,7 +177,7 @@ class StorageFileApi {
175177
Future<SignedUploadURLResponse> createSignedUploadUrl(String path) async {
176178
final finalPath = _getFinalPath(path);
177179

178-
final data = await storageFetch.post(
180+
final data = await _storageFetch.post(
179181
'$url/object/upload/sign/$finalPath',
180182
{},
181183
options: FetchOptions(headers: headers),
@@ -220,7 +222,7 @@ class StorageFileApi {
220222
assert(retryAttempts == null || retryAttempts >= 0,
221223
'retryAttempts has to be greater or equal to 0');
222224
final finalPath = _getFinalPath(path);
223-
final response = await storageFetch.putFile(
225+
final response = await _storageFetch.putFile(
224226
'$url/object/$finalPath',
225227
file,
226228
fileOptions,
@@ -256,7 +258,7 @@ class StorageFileApi {
256258
assert(retryAttempts == null || retryAttempts >= 0,
257259
'retryAttempts has to be greater or equal to 0');
258260
final finalPath = _getFinalPath(path);
259-
final response = await storageFetch.putBinaryFile(
261+
final response = await _storageFetch.putBinaryFile(
260262
'$url/object/$finalPath',
261263
data,
262264
fileOptions,
@@ -276,7 +278,7 @@ class StorageFileApi {
276278
/// `folder/image-new.png`.
277279
Future<String> move(String fromPath, String toPath) async {
278280
final options = FetchOptions(headers: headers);
279-
final response = await storageFetch.post(
281+
final response = await _storageFetch.post(
280282
'$url/object/move',
281283
{
282284
'bucketId': bucketId,
@@ -297,7 +299,7 @@ class StorageFileApi {
297299
/// `folder/image-copy.png`.
298300
Future<String> copy(String fromPath, String toPath) async {
299301
final options = FetchOptions(headers: headers);
300-
final response = await storageFetch.post(
302+
final response = await _storageFetch.post(
301303
'$url/object/copy',
302304
{
303305
'bucketId': bucketId,
@@ -326,7 +328,7 @@ class StorageFileApi {
326328
}) async {
327329
final finalPath = _getFinalPath(path);
328330
final options = FetchOptions(headers: headers);
329-
final response = await storageFetch.post(
331+
final response = await _storageFetch.post(
330332
'$url/object/sign/$finalPath',
331333
{
332334
'expiresIn': expiresIn,
@@ -354,7 +356,7 @@ class StorageFileApi {
354356
int expiresIn,
355357
) async {
356358
final options = FetchOptions(headers: headers);
357-
final response = await storageFetch.post(
359+
final response = await _storageFetch.post(
358360
'$url/object/sign/$bucketId',
359361
{
360362
'expiresIn': expiresIn,
@@ -391,7 +393,7 @@ class StorageFileApi {
391393
fetchUrl = fetchUrl.replace(queryParameters: queryParams);
392394

393395
final response =
394-
await storageFetch.get(fetchUrl.toString(), options: options);
396+
await _storageFetch.get(fetchUrl.toString(), options: options);
395397
return response as Uint8List;
396398
}
397399

@@ -424,7 +426,7 @@ class StorageFileApi {
424426
/// name. For example: `remove(['folder/image.png'])`.
425427
Future<List<FileObject>> remove(List<String> paths) async {
426428
final options = FetchOptions(headers: headers);
427-
final response = await storageFetch.delete(
429+
final response = await _storageFetch.delete(
428430
'$url/object/$bucketId',
429431
{'prefixes': paths},
430432
options: options,
@@ -451,7 +453,7 @@ class StorageFileApi {
451453
...searchOptions.toMap(),
452454
};
453455
final options = FetchOptions(headers: headers);
454-
final response = await storageFetch.post(
456+
final response = await _storageFetch.post(
455457
'$url/object/list/$bucketId',
456458
body,
457459
options: options,

packages/storage_client/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ dependencies:
1313
http_parser: ^4.0.1
1414
mime: ^1.0.2
1515
retry: ^3.1.0
16+
meta: ^1.7.0
1617

1718
dev_dependencies:
18-
mocktail: ^0.3.0
1919
test: ^1.21.4
2020
lints: ^2.1.1
2121
path: ^1.8.2

0 commit comments

Comments
 (0)