react-native-cloud-storage 1.4.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +6 -5
  3. package/ios/CloudStorage.swift +52 -262
  4. package/ios/CloudStorage.xcodeproj/project.pbxproj +28 -0
  5. package/ios/CloudStorageEventEmitter.swift +4 -4
  6. package/ios/Utils/CloudKitUtils.swift +112 -0
  7. package/ios/Utils/CloudStorageError.swift +78 -0
  8. package/ios/Utils/FileUtils.swift +132 -0
  9. package/ios/Utils/Promise.swift +58 -0
  10. package/ios/Utils/Types.swift +36 -0
  11. package/lib/commonjs/RNCloudStorage.js +361 -66
  12. package/lib/commonjs/RNCloudStorage.js.map +1 -1
  13. package/lib/commonjs/expo-plugin/types/index.js.map +1 -1
  14. package/lib/commonjs/expo-plugin/withRNCloudStorage.js +2 -3
  15. package/lib/commonjs/expo-plugin/withRNCloudStorage.js.map +1 -1
  16. package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js +4 -7
  17. package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js.map +1 -1
  18. package/lib/commonjs/google-drive/client.js +16 -20
  19. package/lib/commonjs/google-drive/client.js.map +1 -1
  20. package/lib/commonjs/google-drive/index.js +42 -64
  21. package/lib/commonjs/google-drive/index.js.map +1 -1
  22. package/lib/commonjs/google-drive/types.js +1 -2
  23. package/lib/commonjs/google-drive/types.js.map +1 -1
  24. package/lib/commonjs/hooks/useCloudFile.js +14 -17
  25. package/lib/commonjs/hooks/useCloudFile.js.map +1 -1
  26. package/lib/commonjs/hooks/useIsCloudAvailable.js +11 -21
  27. package/lib/commonjs/hooks/useIsCloudAvailable.js.map +1 -1
  28. package/lib/commonjs/index.js +1 -7
  29. package/lib/commonjs/index.js.map +1 -1
  30. package/lib/commonjs/package.json +1 -0
  31. package/lib/commonjs/types/main.js +8 -3
  32. package/lib/commonjs/types/main.js.map +1 -1
  33. package/lib/commonjs/types/native.js +3 -3
  34. package/lib/commonjs/types/native.js.map +1 -1
  35. package/lib/commonjs/utils/CloudStorageError.js +1 -2
  36. package/lib/commonjs/utils/CloudStorageError.js.map +1 -1
  37. package/lib/commonjs/utils/helpers.js +8 -15
  38. package/lib/commonjs/utils/helpers.js.map +1 -1
  39. package/lib/module/RNCloudStorage.js +362 -65
  40. package/lib/module/RNCloudStorage.js.map +1 -1
  41. package/lib/module/expo-plugin/types/index.js +1 -1
  42. package/lib/module/expo-plugin/types/index.js.map +1 -1
  43. package/lib/module/expo-plugin/withRNCloudStorage.js +2 -0
  44. package/lib/module/expo-plugin/withRNCloudStorage.js.map +1 -1
  45. package/lib/module/expo-plugin/withRNCloudStorageIos.js +5 -5
  46. package/lib/module/expo-plugin/withRNCloudStorageIos.js.map +1 -1
  47. package/lib/module/google-drive/client.js +18 -20
  48. package/lib/module/google-drive/client.js.map +1 -1
  49. package/lib/module/google-drive/index.js +41 -62
  50. package/lib/module/google-drive/index.js.map +1 -1
  51. package/lib/module/google-drive/types.js +2 -0
  52. package/lib/module/google-drive/types.js.map +1 -1
  53. package/lib/module/hooks/useCloudFile.js +15 -16
  54. package/lib/module/hooks/useCloudFile.js.map +1 -1
  55. package/lib/module/hooks/useIsCloudAvailable.js +13 -21
  56. package/lib/module/hooks/useIsCloudAvailable.js.map +1 -1
  57. package/lib/module/index.js +2 -5
  58. package/lib/module/index.js.map +1 -1
  59. package/lib/module/package.json +1 -0
  60. package/lib/module/types/main.js +9 -0
  61. package/lib/module/types/main.js.map +1 -1
  62. package/lib/module/types/native.js +4 -1
  63. package/lib/module/types/native.js.map +1 -1
  64. package/lib/module/utils/CloudStorageError.js +2 -0
  65. package/lib/module/utils/CloudStorageError.js.map +1 -1
  66. package/lib/module/utils/helpers.js +8 -13
  67. package/lib/module/utils/helpers.js.map +1 -1
  68. package/lib/typescript/RNCloudStorage.d.ts +159 -39
  69. package/lib/typescript/RNCloudStorage.d.ts.map +1 -1
  70. package/lib/typescript/google-drive/client.d.ts +3 -3
  71. package/lib/typescript/google-drive/client.d.ts.map +1 -1
  72. package/lib/typescript/google-drive/index.d.ts +6 -18
  73. package/lib/typescript/google-drive/index.d.ts.map +1 -1
  74. package/lib/typescript/hooks/useCloudFile.d.ts +4 -7
  75. package/lib/typescript/hooks/useCloudFile.d.ts.map +1 -1
  76. package/lib/typescript/hooks/useIsCloudAvailable.d.ts +3 -2
  77. package/lib/typescript/hooks/useIsCloudAvailable.d.ts.map +1 -1
  78. package/lib/typescript/index.d.ts +0 -4
  79. package/lib/typescript/index.d.ts.map +1 -1
  80. package/lib/typescript/types/main.d.ts +33 -0
  81. package/lib/typescript/types/main.d.ts.map +1 -1
  82. package/lib/typescript/types/native.d.ts +2 -1
  83. package/lib/typescript/types/native.d.ts.map +1 -1
  84. package/lib/typescript/utils/helpers.d.ts +2 -9
  85. package/lib/typescript/utils/helpers.d.ts.map +1 -1
  86. package/package.json +9 -11
  87. package/src/RNCloudStorage.ts +387 -68
  88. package/src/google-drive/client.ts +8 -7
  89. package/src/google-drive/index.ts +38 -63
  90. package/src/hooks/useCloudFile.ts +13 -16
  91. package/src/hooks/useIsCloudAvailable.ts +12 -25
  92. package/src/index.ts +0 -5
  93. package/src/types/main.ts +38 -0
  94. package/src/types/native.ts +2 -1
  95. package/src/utils/helpers.ts +8 -15
  96. package/lib/commonjs/createRNCloudStorage.js +0 -48
  97. package/lib/commonjs/createRNCloudStorage.js.map +0 -1
  98. package/lib/module/createRNCloudStorage.js +0 -41
  99. package/lib/module/createRNCloudStorage.js.map +0 -1
  100. package/lib/typescript/createRNCloudStorage.d.ts +0 -3
  101. package/lib/typescript/createRNCloudStorage.d.ts.map +0 -1
  102. package/src/createRNCloudStorage.ts +0 -53
@@ -1,137 +1,320 @@
1
- import { Platform } from 'react-native';
2
- import createRNCloudStorage from './createRNCloudStorage';
1
+ import {
2
+ CloudStorageProvider,
3
+ CloudStorageScope,
4
+ type CloudStorageFileStat,
5
+ type CloudStorageProviderOptions,
6
+ type DeepRequired,
7
+ } from './types/main';
8
+ import type NativeRNCloudStorage from './types/native';
9
+ import { isProviderSupported } from './utils/helpers';
10
+ import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
11
+ import CloudStorageError from './utils/CloudStorageError';
12
+ import { CloudStorageErrorCode } from './types/native';
3
13
  import GoogleDrive from './google-drive';
4
- import { CloudStorageScope, type CloudStorageFileStat } from './types/main';
5
- import { verifyLeadingSlash } from './utils/helpers';
6
-
7
- const nativeInstance = createRNCloudStorage();
8
- let defaultScope = CloudStorageScope.AppData;
9
-
10
- const RNCloudStorage = {
11
- getDefaultScope: () => defaultScope,
12
- setDefaultScope: (scope: CloudStorageScope) => (defaultScope = scope),
13
- getGoogleDriveAccessToken: () => GoogleDrive.accessToken,
14
- setGoogleDriveAccessToken: (accessToken: string | null) => (GoogleDrive.accessToken = accessToken),
15
- setThrowOnFilesWithSameName: (enable: boolean) => (GoogleDrive.throwOnFilesWithSameName = enable),
16
- setTimeout: (timeout: number) => (GoogleDrive.timeout = timeout),
17
- /* eslint-disable @typescript-eslint/no-unused-vars */
18
- subscribeToFilesWithSameName:
19
- Platform.OS === 'ios'
20
- ? // @ts-expect-error - subscriber is undefined; just a mock
21
- (subscriber: ({ path, fileIds }: { path: string; fileIds: string[] }) => void) => ({ remove: () => {} })
22
- : (nativeInstance as GoogleDrive).subscribeToFilesWithSameName.bind(nativeInstance),
23
- /* eslint-enable @typescript-eslint/no-unused-vars */
14
+
15
+ const LINKING_ERROR =
16
+ `The package 'react-native-cloud-storage' doesn't seem to be linked. Make sure: \n\n` +
17
+ Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
18
+ '- You rebuilt the app after installing the package\n' +
19
+ '- You are not using Expo Go\n';
20
+
21
+ // proxy NativeModules.CloudStorage to catch any errors thrown by the native module and wrap them in a CloudStorageError
22
+ const nativeIosInstance = NativeModules.CloudStorage
23
+ ? new Proxy(NativeModules.CloudStorage, {
24
+ get(target: NativeRNCloudStorage, prop: keyof NativeRNCloudStorage) {
25
+ const originalFunction = target[prop];
26
+ if (typeof originalFunction === 'function') {
27
+ return async (...args: any[]) => {
28
+ try {
29
+ // @ts-expect-error - we can't know the types of the functions and their arguments
30
+ return await originalFunction(...args);
31
+ } catch (error: any) {
32
+ if (error?.code && Object.values(CloudStorageErrorCode).includes(error.code)) {
33
+ throw new CloudStorageError(error?.message || '', error.code as CloudStorageErrorCode);
34
+ } else {
35
+ throw new CloudStorageError('Unknown error', CloudStorageErrorCode.UNKNOWN, error);
36
+ }
37
+ }
38
+ };
39
+ }
40
+ return originalFunction;
41
+ },
42
+ })
43
+ : null;
44
+
45
+ const defaultProviderOptions: DeepRequired<CloudStorageProviderOptions> = {
46
+ [CloudStorageProvider.ICloud]: {
47
+ scope: CloudStorageScope.AppData,
48
+ },
49
+ [CloudStorageProvider.GoogleDrive]: {
50
+ scope: CloudStorageScope.AppData,
51
+ accessToken: null,
52
+ strictFilenames: false,
53
+ timeout: 3000,
54
+ },
55
+ };
56
+
57
+ export default class RNCloudStorage {
58
+ private static defaultInstance: RNCloudStorage;
59
+ private provider: {
60
+ provider: CloudStorageProvider;
61
+ options: (typeof defaultProviderOptions)[keyof typeof defaultProviderOptions];
62
+ };
63
+ private cloudAvailabilityListeners: ((available: boolean) => void)[] = [];
64
+
65
+ //#region Constructor and configuration
66
+ /**
67
+ * Creates a new RNCloudStorage instance for the given provider.
68
+ * @param provider The provider to create the instance for. Defaults to the default provider for the current platform.
69
+ */
70
+ constructor(
71
+ provider?: CloudStorageProvider,
72
+ options?: CloudStorageProviderOptions[keyof CloudStorageProviderOptions]
73
+ ) {
74
+ if (provider && !isProviderSupported(provider)) {
75
+ throw new Error(`Provider ${provider} is not supported on the current platform.`);
76
+ }
77
+
78
+ this.provider = {
79
+ provider: provider ?? RNCloudStorage.getDefaultProvider(),
80
+ options: defaultProviderOptions[provider ?? RNCloudStorage.getDefaultProvider()],
81
+ };
82
+
83
+ this.setProvider(provider ?? RNCloudStorage.getDefaultProvider());
84
+ if (options) {
85
+ this.setProviderOptions(options);
86
+ }
87
+ }
88
+
89
+ private get nativeInstance(): NativeRNCloudStorage {
90
+ switch (this.provider.provider) {
91
+ case CloudStorageProvider.ICloud:
92
+ return (
93
+ nativeIosInstance ??
94
+ new Proxy(
95
+ {},
96
+ {
97
+ get() {
98
+ throw new Error(LINKING_ERROR);
99
+ },
100
+ }
101
+ )
102
+ );
103
+ default:
104
+ return new GoogleDrive(this.provider.options as DeepRequired<CloudStorageProviderOptions['googledrive']>);
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Gets the default CloudStorageProvider for the current platform.
110
+ * @returns The default CloudStorageProvider.
111
+ */
112
+ static getDefaultProvider(): CloudStorageProvider {
113
+ switch (Platform.OS) {
114
+ case 'ios':
115
+ return CloudStorageProvider.ICloud;
116
+ default:
117
+ return CloudStorageProvider.GoogleDrive;
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Gets the list of supported CloudStorageProviders on the current platform.
123
+ * @returns An array of supported CloudStorageProviders.
124
+ */
125
+ static getSupportedProviders(): CloudStorageProvider[] {
126
+ return Object.values(CloudStorageProvider).filter(isProviderSupported);
127
+ }
128
+
129
+ /**
130
+ * Gets the current CloudStorageProvider.
131
+ * @returns The current CloudStorageProvider.
132
+ */
133
+ getProvider(): CloudStorageProvider {
134
+ return this.provider.provider;
135
+ }
136
+
137
+ /**
138
+ * Sets the current CloudStorageProvider.
139
+ * @param provider The provider to set.
140
+ */
141
+ setProvider(provider: CloudStorageProvider): void {
142
+ if (!isProviderSupported(provider)) {
143
+ throw new Error(`Provider ${provider} is not supported on the current platform.`);
144
+ }
145
+
146
+ this.provider = {
147
+ provider,
148
+ options: defaultProviderOptions[provider],
149
+ };
150
+
151
+ // Emit an event to notify useIsCloudAvailable() hook consumers of the new cloud availability status
152
+ this.nativeInstance.isCloudAvailable().then((available) => {
153
+ this.cloudAvailabilityListeners.forEach((listener) => {
154
+ listener(available);
155
+ });
156
+ });
157
+
158
+ if (provider === CloudStorageProvider.ICloud) {
159
+ // Listen to native cloud availability change events
160
+ const eventEmitter = new NativeEventEmitter(NativeModules.CloudStorageEventEmitter);
161
+ eventEmitter.addListener('RNCloudStorage.cloud.availability-changed', (event: { available: boolean }) => {
162
+ this.cloudAvailabilityListeners.forEach((listener) => {
163
+ listener(event.available);
164
+ });
165
+ });
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Gets the current options for the current provider.
171
+ * @returns The current options for the current provider.
172
+ */
173
+ getProviderOptions(): CloudStorageProviderOptions[keyof CloudStorageProviderOptions] {
174
+ return this.provider.options;
175
+ }
24
176
 
177
+ /**
178
+ * Sets the options for the current provider.
179
+ * @param options The options to set for the provider.
180
+ */
181
+ setProviderOptions(options: CloudStorageProviderOptions[keyof CloudStorageProviderOptions]): void {
182
+ const newOptions = Object.fromEntries(Object.entries(options).filter(([_, v]) => v !== undefined));
183
+ this.provider.options = {
184
+ ...this.provider.options,
185
+ ...newOptions,
186
+ };
187
+
188
+ if (this.provider.provider === CloudStorageProvider.GoogleDrive && 'accessToken' in newOptions) {
189
+ // Emit an event to notify useIsCloudAvailable() hook consumers of the new cloud availability status
190
+ this.cloudAvailabilityListeners.forEach((listener) => {
191
+ listener(
192
+ !!(newOptions as Required<CloudStorageProviderOptions[CloudStorageProvider.GoogleDrive]>).accessToken?.length
193
+ );
194
+ });
195
+ }
196
+ }
197
+
198
+ subscribeToCloudAvailability(listener: (available: boolean) => void): void {
199
+ this.cloudAvailabilityListeners.push(listener);
200
+ }
201
+
202
+ unsubscribeFromCloudAvailability(listener: (available: boolean) => void): void {
203
+ this.cloudAvailabilityListeners = this.cloudAvailabilityListeners.filter((l) => l !== listener);
204
+ }
205
+ //#endregion
206
+
207
+ //#region File system operations
25
208
  /**
26
209
  * Tests whether or not the cloud storage is available. Always returns true for Google Drive. iCloud may be
27
210
  * unavailable right after app launch or if the user is not logged in.
28
211
  * @returns A promise that resolves to true if the cloud storage is available, false otherwise.
29
212
  */
30
- isCloudAvailable: async (): Promise<boolean> => {
31
- return nativeInstance.isCloudAvailable();
32
- },
213
+ isCloudAvailable(): Promise<boolean> {
214
+ return this.nativeInstance.isCloudAvailable();
215
+ }
33
216
 
34
217
  /**
35
218
  * Appends the data to the file at the given path, creating the file if it doesn't exist.
36
219
  * @param path The file to append to.
37
220
  * @param data The data to append.
38
- * @param scope The directory scope the path is in. Defaults to the set default scope.
221
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the current provider.
39
222
  * @returns A promise that resolves when the data has been appended.
40
223
  */
41
- appendFile: (path: string, data: string, scope?: CloudStorageScope): Promise<void> => {
42
- return nativeInstance.appendToFile(verifyLeadingSlash(path), data, scope ?? defaultScope);
43
- },
224
+ appendFile(path: string, data: string, scope?: CloudStorageScope): Promise<void> {
225
+ return this.nativeInstance.appendToFile(path, data, scope ?? this.provider.options.scope);
226
+ }
44
227
 
45
228
  /**
46
229
  * Tests whether or not the file at the given path exists.
47
230
  * @param path The path to test.
48
- * @param scope The directory scope the path is in. Defaults to the set default scope.
231
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
49
232
  * @returns A promise that resolves to true if the path exists, false otherwise.
50
233
  */
51
- exists: (path: string, scope?: CloudStorageScope): Promise<boolean> => {
52
- return nativeInstance.fileExists(verifyLeadingSlash(path), scope ?? defaultScope);
53
- },
234
+ exists(path: string, scope?: CloudStorageScope): Promise<boolean> {
235
+ return this.nativeInstance.fileExists(path, scope ?? this.provider.options.scope);
236
+ }
54
237
 
55
238
  /**
56
239
  * Writes to the file at the given path, creating it if it doesn't exist or overwriting it if it does.
57
240
  * @param path The file to write to.
58
241
  * @param data The data to write.
59
- * @param scope The directory scope the path is in. Defaults to the set default scope.
242
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
60
243
  * @returns A promise that resolves when the file has been written.
61
244
  */
62
- writeFile: (path: string, data: string, scope?: CloudStorageScope): Promise<void> => {
63
- return nativeInstance.createFile(verifyLeadingSlash(path), data, scope ?? defaultScope, true);
64
- },
245
+ writeFile(path: string, data: string, scope?: CloudStorageScope): Promise<void> {
246
+ return this.nativeInstance.createFile(path, data, scope ?? this.provider.options.scope, true);
247
+ }
65
248
 
66
249
  /**
67
250
  * Creates a new directory at the given path.
68
251
  * @param path The directory to create.
69
- * @param scope The directory scope the path is in. Defaults to the set default scope.
252
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
70
253
  * @returns A promise that resolves when the directory has been created.
71
254
  */
72
- mkdir: (path: string, scope?: CloudStorageScope): Promise<void> => {
73
- return nativeInstance.createDirectory(verifyLeadingSlash(path), scope ?? defaultScope);
74
- },
255
+ mkdir(path: string, scope?: CloudStorageScope): Promise<void> {
256
+ return this.nativeInstance.createDirectory(path, scope ?? this.provider.options.scope);
257
+ }
75
258
 
76
259
  /**
77
260
  * Lists the contents of the directory at the given path.
78
261
  * @param path The directory to list.
79
- * @param scope The directory scope the path is in. Defaults to the set default scope.
262
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
80
263
  * @returns A promise that resolves to an array of file names, excluding '.' and '..'.
81
264
  */
82
- readdir: (path: string, scope?: CloudStorageScope): Promise<string[]> => {
83
- return nativeInstance.listFiles(verifyLeadingSlash(path), scope ?? defaultScope);
84
- },
265
+ readdir(path: string, scope?: CloudStorageScope): Promise<string[]> {
266
+ return this.nativeInstance.listFiles(path, scope ?? this.provider.options.scope);
267
+ }
85
268
 
86
269
  /**
87
270
  * Reads the contents of the file at the given path.
88
271
  * @param path The file to read.
89
- * @param scope The directory scope the path is in. Defaults to the set default scope.
272
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
90
273
  * @returns A promise that resolves to the contents of the file.
91
274
  */
92
- readFile: (path: string, scope?: CloudStorageScope): Promise<string> => {
93
- return nativeInstance.readFile(verifyLeadingSlash(path), scope ?? defaultScope);
94
- },
275
+ readFile(path: string, scope?: CloudStorageScope): Promise<string> {
276
+ return this.nativeInstance.readFile(path, scope ?? this.provider.options.scope);
277
+ }
95
278
 
96
279
  /**
97
- * Downloads the file at the given path from iCloud. Does not have any effect on Google Drive.
280
+ * Downloads the file at the given path. Does not have any effect on Google Drive.
98
281
  * @param path The file to trigger the download for.
99
- * @param scope The directory scope the path is in. Defaults to the set default scope.
282
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
100
283
  * @returns A promise that resolves once the download has been triggered.
101
284
  */
102
- downloadFile: (path: string, scope?: CloudStorageScope): Promise<void> => {
103
- return nativeInstance.downloadFile(verifyLeadingSlash(path), scope ?? defaultScope);
104
- },
285
+ downloadFile(path: string, scope?: CloudStorageScope): Promise<void> {
286
+ return this.nativeInstance.downloadFile(path, scope ?? this.provider.options.scope);
287
+ }
105
288
 
106
289
  /**
107
290
  * Deletes the file at the given path.
108
291
  * @param path The file to delete.
109
- * @param scope The directory scope the path is in. Defaults to the set default scope.
292
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
110
293
  * @returns A promise that resolves when the file has been deleted.
111
294
  */
112
- unlink: (path: string, scope?: CloudStorageScope): Promise<void> => {
113
- return nativeInstance.deleteFile(verifyLeadingSlash(path), scope ?? defaultScope);
114
- },
295
+ unlink(path: string, scope?: CloudStorageScope): Promise<void> {
296
+ return this.nativeInstance.deleteFile(path, scope ?? this.provider.options.scope);
297
+ }
115
298
 
116
299
  /**
117
300
  * Deletes the directory at the given path.
118
301
  * @param path The directory to delete.
119
302
  * @param options Options for the delete operation. Defaults to { recursive: false }.
120
- * @param scope The directory scope the path is in. Defaults to the set default scope.
303
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
121
304
  * @returns A promise that resolves when the directory has been deleted.
122
305
  */
123
- rmdir: (path: string, options?: { recursive?: boolean }, scope?: CloudStorageScope): Promise<void> => {
124
- return nativeInstance.deleteDirectory(verifyLeadingSlash(path), options?.recursive ?? false, scope ?? defaultScope);
125
- },
306
+ rmdir(path: string, options?: { recursive?: boolean }, scope?: CloudStorageScope): Promise<void> {
307
+ return this.nativeInstance.deleteDirectory(path, options?.recursive ?? false, scope ?? this.provider.options.scope);
308
+ }
126
309
 
127
310
  /**
128
311
  * Gets the size, creation time, and modification time of the file at the given path.
129
312
  * @param path The file to stat.
130
- * @param scope The directory scope the path is in. Defaults to the set default scope.
313
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
131
314
  * @returns A promise that resolves to the CloudStorageFileStat object.
132
315
  */
133
- stat: async (path: string, scope?: CloudStorageScope): Promise<CloudStorageFileStat> => {
134
- const native = await nativeInstance.statFile(verifyLeadingSlash(path), scope ?? defaultScope);
316
+ async stat(path: string, scope?: CloudStorageScope): Promise<CloudStorageFileStat> {
317
+ const native = await this.nativeInstance.statFile(path, scope ?? this.provider.options.scope);
135
318
 
136
319
  return {
137
320
  ...native,
@@ -140,7 +323,143 @@ const RNCloudStorage = {
140
323
  isDirectory: () => native.isDirectory,
141
324
  isFile: () => native.isFile,
142
325
  };
143
- },
144
- };
326
+ }
327
+ //#endregion
328
+
329
+ //#region Static methods for default static instance
330
+ static getDefaultInstance(): RNCloudStorage {
331
+ if (!RNCloudStorage.defaultInstance) {
332
+ RNCloudStorage.defaultInstance = new RNCloudStorage();
333
+ }
334
+ return RNCloudStorage.defaultInstance;
335
+ }
336
+
337
+ /**
338
+ * Gets the current options for the provider of the default static instance.
339
+ * @returns The current options for the provider of the default static instance.
340
+ */
341
+ static getProviderOptions(): CloudStorageProviderOptions[keyof CloudStorageProviderOptions] {
342
+ return RNCloudStorage.getDefaultInstance().getProviderOptions();
343
+ }
344
+
345
+ /**
346
+ * Sets the options for the provider of the default static instance.
347
+ * @param options The options to set for the provider of the default static instance.
348
+ */
349
+ static setProviderOptions(options: CloudStorageProviderOptions[keyof CloudStorageProviderOptions]): void {
350
+ RNCloudStorage.getDefaultInstance().setProviderOptions(options);
351
+ }
352
+
353
+ /**
354
+ * Tests whether or not the file at the given path exists in the provider of the default static instance.
355
+ * @param path The path to test.
356
+ * @param scope The directory scope the path is in. Defaults to set default scope set for the current provider.
357
+ * @returns A promise that resolves to true if the path exists, false otherwise.
358
+ */
359
+ static exists(path: string, scope?: CloudStorageScope): Promise<boolean> {
360
+ return RNCloudStorage.getDefaultInstance().exists(path, scope);
361
+ }
362
+
363
+ /**
364
+ * Tests whether or not the cloud storage is available for the provider of the default static instance. Always returns true for Google Drive. iCloud may be
365
+ * unavailable right after app launch or if the user is not logged in.
366
+ * @returns A promise that resolves to true if the cloud storage is available, false otherwise.
367
+ */
368
+ static isCloudAvailable(): Promise<boolean> {
369
+ return RNCloudStorage.getDefaultInstance().isCloudAvailable();
370
+ }
371
+
372
+ /**
373
+ * Appends the data to the file at the given path in the provider of the default static instance, creating the file if it doesn't exist.
374
+ * @param path The file to append to.
375
+ * @param data The data to append.
376
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
377
+ * @returns A promise that resolves when the data has been appended.
378
+ */
379
+ static appendFile(path: string, data: string, scope?: CloudStorageScope): Promise<void> {
380
+ return RNCloudStorage.getDefaultInstance().appendFile(path, data, scope);
381
+ }
382
+
383
+ /**
384
+ * Writes to the file at the given path in the provider of the default static instance, creating it if it doesn't exist or overwriting it if it does.
385
+ * @param path The file to write to.
386
+ * @param data The data to write.
387
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
388
+ * @returns A promise that resolves when the file has been written.
389
+ */
390
+ static writeFile(path: string, data: string, scope?: CloudStorageScope): Promise<void> {
391
+ return RNCloudStorage.getDefaultInstance().writeFile(path, data, scope);
392
+ }
393
+
394
+ /**
395
+ * Creates a new directory at the given path in the provider of the default static instance.
396
+ * @param path The directory to create.
397
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
398
+ * @returns A promise that resolves when the directory has been created.
399
+ */
400
+ static mkdir(path: string, scope?: CloudStorageScope): Promise<void> {
401
+ return RNCloudStorage.getDefaultInstance().mkdir(path, scope);
402
+ }
403
+
404
+ /**
405
+ * Lists the contents of the directory at the given path in the provider of the default static instance.
406
+ * @param path The directory to list.
407
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
408
+ * @returns A promise that resolves to an array of file names, excluding '.' and '..'.
409
+ */
410
+ static readdir(path: string, scope?: CloudStorageScope): Promise<string[]> {
411
+ return RNCloudStorage.getDefaultInstance().readdir(path, scope);
412
+ }
145
413
 
146
- export default RNCloudStorage;
414
+ /**
415
+ * Reads the contents of the file at the given path in the provider of the default static instance.
416
+ * @param path The file to read.
417
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
418
+ * @returns A promise that resolves to the contents of the file.
419
+ */
420
+ static readFile(path: string, scope?: CloudStorageScope): Promise<string> {
421
+ return RNCloudStorage.getDefaultInstance().readFile(path, scope);
422
+ }
423
+
424
+ /**
425
+ * Downloads the file at the given path in the provider of the default static instance. Does not have any effect on Google Drive.
426
+ * @param path The file to trigger the download for.
427
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
428
+ * @returns A promise that resolves once the download has been triggered.
429
+ */
430
+ static downloadFile(path: string, scope?: CloudStorageScope): Promise<void> {
431
+ return RNCloudStorage.getDefaultInstance().downloadFile(path, scope);
432
+ }
433
+
434
+ /**
435
+ * Deletes the file at the given path in the provider of the default static instance.
436
+ * @param path The file to delete.
437
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
438
+ * @returns A promise that resolves when the file has been deleted.
439
+ */
440
+ static unlink(path: string, scope?: CloudStorageScope): Promise<void> {
441
+ return RNCloudStorage.getDefaultInstance().unlink(path, scope);
442
+ }
443
+
444
+ /**
445
+ * Deletes the directory at the given path in the provider of the default static instance.
446
+ * @param path The directory to delete.
447
+ * @param options Options for the delete operation. Defaults to { recursive: false }.
448
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
449
+ * @returns A promise that resolves when the directory has been deleted.
450
+ */
451
+ static rmdir(path: string, options?: { recursive?: boolean }, scope?: CloudStorageScope): Promise<void> {
452
+ return RNCloudStorage.getDefaultInstance().rmdir(path, options, scope);
453
+ }
454
+
455
+ /**
456
+ * Gets the size, creation time, and modification time of the file at the given path in the provider of the default static instance.
457
+ * @param path The file to stat.
458
+ * @param scope The directory scope the path is in. Defaults to the default scope set for the default static instance.
459
+ * @returns A promise that resolves to the CloudStorageFileStat object.
460
+ */
461
+ static stat(path: string, scope?: CloudStorageScope): Promise<CloudStorageFileStat> {
462
+ return RNCloudStorage.getDefaultInstance().stat(path, scope);
463
+ }
464
+ //#endregion
465
+ }
@@ -1,3 +1,4 @@
1
+ import { type CloudStorageProviderOptions, type DeepRequired } from '../types/main';
1
2
  import {
2
3
  MimeTypes,
3
4
  type GoogleDriveFile,
@@ -24,13 +25,11 @@ export class GoogleDriveHttpError extends Error {
24
25
  // TODO: fetch timeout
25
26
  // TODO: properly handle errors
26
27
  export default class GoogleDriveApiClient {
27
- public accessToken: string;
28
- public timeout: number;
29
28
  private _fetchTimeout: any;
29
+ private options: DeepRequired<CloudStorageProviderOptions['googledrive']>;
30
30
 
31
- constructor(accessToken: string = '') {
32
- this.accessToken = accessToken;
33
- this.timeout = 3000;
31
+ constructor(options: DeepRequired<CloudStorageProviderOptions['googledrive']>) {
32
+ this.options = options;
34
33
  }
35
34
 
36
35
  private buildQueryString(query: object): string {
@@ -52,6 +51,8 @@ export default class GoogleDriveApiClient {
52
51
  operation: `/${string}`,
53
52
  { queryParameters, baseUrl, ...options }: RequestInit & { queryParameters?: object; baseUrl?: string } = {}
54
53
  ): Promise<T> {
54
+ const { timeout, accessToken } = this.options;
55
+
55
56
  let path = `${baseUrl ?? BASE_URL}${operation}`;
56
57
  if (queryParameters) {
57
58
  path += this.buildQueryString(queryParameters);
@@ -60,12 +61,12 @@ export default class GoogleDriveApiClient {
60
61
  const abortController: AbortController = new AbortController();
61
62
  this._fetchTimeout = setTimeout(() => {
62
63
  abortController.abort();
63
- }, this.timeout);
64
+ }, timeout);
64
65
  const response = await fetch(path, {
65
66
  ...options,
66
67
  headers: {
67
68
  ...options.headers,
68
- Authorization: `Bearer ${this.accessToken}`,
69
+ Authorization: `Bearer ${accessToken}`,
69
70
  },
70
71
  signal: abortController.signal,
71
72
  });