react-native-cloud-storage 0.6.0 → 1.0.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 (77) hide show
  1. package/README.md +0 -2
  2. package/ios/CloudStorage.m +1 -0
  3. package/ios/CloudStorage.swift +106 -65
  4. package/ios/CloudStorage.xcodeproj/project.pbxproj +1 -3
  5. package/ios/CloudStorage.xcodeproj/project.xcworkspace/contents.xcworkspacedata +4 -0
  6. package/ios/CloudStorage.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  7. package/ios/CloudStorage.xcodeproj/project.xcworkspace/xcuserdata/max.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  8. package/ios/CloudStorage.xcodeproj/xcuserdata/max.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  9. package/lib/commonjs/RNCloudStorage.js +26 -12
  10. package/lib/commonjs/RNCloudStorage.js.map +1 -1
  11. package/lib/commonjs/expo-plugin/types/index.js +2 -0
  12. package/lib/commonjs/expo-plugin/types/index.js.map +1 -0
  13. package/lib/commonjs/expo-plugin/withRNCloudStorage.js +1 -1
  14. package/lib/commonjs/expo-plugin/withRNCloudStorage.js.map +1 -1
  15. package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js +3 -3
  16. package/lib/commonjs/expo-plugin/withRNCloudStorageIos.js.map +1 -1
  17. package/lib/commonjs/google-drive/index.js +33 -4
  18. package/lib/commonjs/google-drive/index.js.map +1 -1
  19. package/lib/commonjs/hooks/useCloudFile.js +14 -3
  20. package/lib/commonjs/hooks/useCloudFile.js.map +1 -1
  21. package/lib/commonjs/hooks/useIsCloudAvailable.js +0 -2
  22. package/lib/commonjs/hooks/useIsCloudAvailable.js.map +1 -1
  23. package/lib/commonjs/index.js +10 -0
  24. package/lib/commonjs/index.js.map +1 -1
  25. package/lib/commonjs/types/native.js +1 -1
  26. package/lib/commonjs/utils/helpers.js +23 -0
  27. package/lib/commonjs/utils/helpers.js.map +1 -0
  28. package/lib/module/RNCloudStorage.js +26 -12
  29. package/lib/module/RNCloudStorage.js.map +1 -1
  30. package/lib/module/expo-plugin/types/index.js +2 -0
  31. package/lib/module/expo-plugin/types/index.js.map +1 -0
  32. package/lib/module/expo-plugin/withRNCloudStorage.js +1 -2
  33. package/lib/module/expo-plugin/withRNCloudStorage.js.map +1 -1
  34. package/lib/module/expo-plugin/withRNCloudStorageIos.js +3 -3
  35. package/lib/module/expo-plugin/withRNCloudStorageIos.js.map +1 -1
  36. package/lib/module/google-drive/index.js +33 -4
  37. package/lib/module/google-drive/index.js.map +1 -1
  38. package/lib/module/hooks/useCloudFile.js +15 -3
  39. package/lib/module/hooks/useCloudFile.js.map +1 -1
  40. package/lib/module/hooks/useIsCloudAvailable.js +0 -2
  41. package/lib/module/hooks/useIsCloudAvailable.js.map +1 -1
  42. package/lib/module/index.js +5 -1
  43. package/lib/module/index.js.map +1 -1
  44. package/lib/module/types/native.js +1 -1
  45. package/lib/module/utils/helpers.js +16 -0
  46. package/lib/module/utils/helpers.js.map +1 -0
  47. package/lib/typescript/RNCloudStorage.d.ts +22 -13
  48. package/lib/typescript/RNCloudStorage.d.ts.map +1 -1
  49. package/lib/typescript/expo-plugin/types/index.d.ts +7 -0
  50. package/lib/typescript/expo-plugin/types/index.d.ts.map +1 -0
  51. package/lib/typescript/expo-plugin/withRNCloudStorage.d.ts +1 -6
  52. package/lib/typescript/expo-plugin/withRNCloudStorage.d.ts.map +1 -1
  53. package/lib/typescript/expo-plugin/withRNCloudStorageIos.d.ts +3 -2
  54. package/lib/typescript/expo-plugin/withRNCloudStorageIos.d.ts.map +1 -1
  55. package/lib/typescript/google-drive/index.d.ts +8 -1
  56. package/lib/typescript/google-drive/index.d.ts.map +1 -1
  57. package/lib/typescript/hooks/useCloudFile.d.ts +13 -2
  58. package/lib/typescript/hooks/useCloudFile.d.ts.map +1 -1
  59. package/lib/typescript/hooks/useIsCloudAvailable.d.ts.map +1 -1
  60. package/lib/typescript/index.d.ts +4 -1
  61. package/lib/typescript/index.d.ts.map +1 -1
  62. package/lib/typescript/types/native.d.ts +2 -1
  63. package/lib/typescript/types/native.d.ts.map +1 -1
  64. package/lib/typescript/utils/helpers.d.ts +10 -0
  65. package/lib/typescript/utils/helpers.d.ts.map +1 -0
  66. package/package.json +24 -20
  67. package/react-native-cloud-storage.podspec +7 -1
  68. package/src/RNCloudStorage.ts +33 -19
  69. package/src/expo-plugin/types/index.ts +6 -0
  70. package/src/expo-plugin/withRNCloudStorage.ts +2 -7
  71. package/src/expo-plugin/withRNCloudStorageIos.ts +8 -10
  72. package/src/google-drive/index.ts +42 -4
  73. package/src/hooks/useCloudFile.ts +14 -3
  74. package/src/hooks/useIsCloudAvailable.ts +0 -2
  75. package/src/index.ts +5 -1
  76. package/src/types/native.ts +2 -1
  77. package/src/utils/helpers.ts +17 -0
@@ -123,7 +123,21 @@ export default class GoogleDriveApiClient implements NativeRNCloudStorage {
123
123
  return currentDirectoryId;
124
124
  }
125
125
 
126
- private async listFiles(scope: NativeRNCloudCloudStorageScope): Promise<GoogleDriveFile[]> {
126
+ /**
127
+ * Gets the Google Drive ID of the root directory for the given scope.
128
+ * @param scope The scope to get the root directory for.
129
+ * @returns A promise that resolves to the ID of the root directory or null if it could not be found.
130
+ */
131
+ private async getRootDirectoryId(scope: NativeRNCloudCloudStorageScope): Promise<string | null> {
132
+ const files = await this.listInternalFiles(scope);
133
+ for (const file of files) {
134
+ if (!files.find((f) => f.id === file.parents![0])) return file.parents![0] ?? null;
135
+ }
136
+
137
+ return null;
138
+ }
139
+
140
+ private async listInternalFiles(scope: NativeRNCloudCloudStorageScope): Promise<GoogleDriveFile[]> {
127
141
  const files: GoogleDriveListOperationResponse = await GoogleDriveApiClient.drive.files.list({
128
142
  spaces: [this.getRootDirectory(scope)],
129
143
  fields: 'files(id,kind,mimeType,name,parents,spaces)',
@@ -163,7 +177,18 @@ export default class GoogleDriveApiClient implements NativeRNCloudStorage {
163
177
  throwIfDirectory = true
164
178
  ): Promise<string> {
165
179
  try {
166
- const files = await this.listFiles(scope);
180
+ const files = await this.listInternalFiles(scope);
181
+
182
+ if (path === '' || path === '/') {
183
+ const rootDirectoryId = await this.getRootDirectoryId(scope);
184
+ if (!rootDirectoryId)
185
+ throw new CloudStorageError(
186
+ `Root directory in scope ${scope} not found`,
187
+ CloudStorageErrorCode.DIRECTORY_NOT_FOUND
188
+ );
189
+ return rootDirectoryId;
190
+ }
191
+
167
192
  const { directories, filename } = this.resolvePathToDirectories(path);
168
193
  const parentDirectoryId = this.findParentDirectoryId(files, directories);
169
194
  let file: GoogleDriveFile | undefined;
@@ -238,7 +263,7 @@ export default class GoogleDriveApiClient implements NativeRNCloudStorage {
238
263
  const uploader = GoogleDriveApiClient.drive.files.newMultipartUploader().setData(data, MimeTypes.TEXT);
239
264
  if (fileId) uploader.setIdOfFileToUpdate(fileId);
240
265
  else {
241
- const files = await this.listFiles(scope);
266
+ const files = await this.listInternalFiles(scope);
242
267
  const { directories, filename } = this.resolvePathToDirectories(path);
243
268
  const parentDirectoryId = this.findParentDirectoryId(files, directories);
244
269
  uploader.setRequestBody({
@@ -253,6 +278,19 @@ export default class GoogleDriveApiClient implements NativeRNCloudStorage {
253
278
  await uploader.execute();
254
279
  }
255
280
 
281
+ async listFiles(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string[]> {
282
+ const allFiles = await this.listInternalFiles(scope);
283
+ if (path !== '') {
284
+ const fileId = await this.getFileId(path, scope, false);
285
+ const files = allFiles.filter((f) => (f.parents ?? [])[0] === fileId);
286
+
287
+ return Array.from(new Set(files.map((f) => f.name)));
288
+ } else {
289
+ const rootDirectoryId = await this.getRootDirectoryId(scope);
290
+ return Array.from(new Set(allFiles.filter((f) => (f.parents ?? [])[0] === rootDirectoryId).map((f) => f.name)));
291
+ }
292
+ }
293
+
256
294
  async createDirectory(path: string, scope: NativeRNCloudCloudStorageScope): Promise<void> {
257
295
  try {
258
296
  await this.getFileId(path, scope);
@@ -268,7 +306,7 @@ export default class GoogleDriveApiClient implements NativeRNCloudStorage {
268
306
  }
269
307
 
270
308
  const uploader = GoogleDriveApiClient.drive.files.newMetadataOnlyUploader();
271
- const files = await this.listFiles(scope);
309
+ const files = await this.listInternalFiles(scope);
272
310
  const { directories, filename } = this.resolvePathToDirectories(path);
273
311
  const parentDirectoryId = this.findParentDirectoryId(files, directories);
274
312
  uploader.setRequestBody({
@@ -2,7 +2,13 @@ import type { CloudStorageScope } from '../types/main';
2
2
  import RNCloudStorage from '../RNCloudStorage';
3
3
  import { useCallback, useEffect, useState } from 'react';
4
4
 
5
- export const useCloudFile = (path: string, scope: CloudStorageScope) => {
5
+ /**
6
+ * A utility hook for reading and writing to a single file in the cloud.
7
+ * @param path The path to the file.
8
+ * @param scope The directory scope the path is in. If not provided, defaults to the default scope set in the library.
9
+ * @returns An object containing the file's contents and functions for reading, writing, and removing the file.
10
+ */
11
+ export const useCloudFile = (path: string, scope?: CloudStorageScope) => {
6
12
  const [content, setContent] = useState<string | null>(null);
7
13
 
8
14
  const read = useCallback(async () => {
@@ -18,7 +24,7 @@ export const useCloudFile = (path: string, scope: CloudStorageScope) => {
18
24
  read();
19
25
  }, [read]);
20
26
 
21
- const update = useCallback(
27
+ const write = useCallback(
22
28
  async (newContent: string) => {
23
29
  await RNCloudStorage.writeFile(path, newContent, scope);
24
30
  read();
@@ -34,7 +40,12 @@ export const useCloudFile = (path: string, scope: CloudStorageScope) => {
34
40
  return {
35
41
  content,
36
42
  read,
37
- update,
43
+ write,
38
44
  remove,
45
+ /**
46
+ * @deprecated Use `write` instead.
47
+ * @alias write
48
+ */
49
+ update: write,
39
50
  };
40
51
  };
@@ -11,14 +11,12 @@ export const useIsCloudAvailable = (iCloudTimeout = 10) => {
11
11
  const newIsAvailable = await RNCloudStorage.isCloudAvailable();
12
12
  setIsAvailable(newIsAvailable);
13
13
  if (Platform.OS === 'ios' && newIsAvailable) {
14
- console.log('Stopped because iCloud became available');
15
14
  clearInterval(interval);
16
15
  }
17
16
  }, 500);
18
17
 
19
18
  if (Platform.OS === 'ios') {
20
19
  setTimeout(() => {
21
- console.log('Stopped because of timeout');
22
20
  clearInterval(interval);
23
21
  }, iCloudTimeout * 1000);
24
22
  }
package/src/index.ts CHANGED
@@ -5,5 +5,9 @@ export * from './hooks/useCloudFile';
5
5
  export * from './hooks/useIsCloudAvailable';
6
6
  import CloudStorageError from './utils/CloudStorageError';
7
7
 
8
- export { CloudStorageError, CloudStorageErrorCode };
8
+ export { RNCloudStorage as CloudStorage, CloudStorageError, CloudStorageErrorCode };
9
+
10
+ /**
11
+ * @deprecated Use the named export `CloudStorage` instead.
12
+ */
9
13
  export default RNCloudStorage;
@@ -11,7 +11,7 @@ export interface NativeRNCloudCloudStorageFileStat {
11
11
  export enum CloudStorageErrorCode {
12
12
  FILE_NOT_FOUND = 'ERR_FILE_NOT_FOUND',
13
13
  PATH_IS_DIRECTORY = 'ERR_PATH_IS_DIRECTORY',
14
- DIRECTORY_NOT_FOUND = 'ERR_NO_DIRECTORY_FOUND',
14
+ DIRECTORY_NOT_FOUND = 'ERR_DIRECTORY_NOT_FOUND',
15
15
  FILE_ALREADY_EXISTS = 'ERR_FILE_EXISTS',
16
16
  MULTIPLE_FILES_SAME_NAME = 'ERR_MULTIPLE_FILES_SAME_NAME',
17
17
  AUTHENTICATION_FAILED = 'ERR_AUTHENTICATION_FAILED',
@@ -27,6 +27,7 @@ export default interface NativeRNCloudStorage {
27
27
  fileExists: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<boolean>;
28
28
  createFile: (path: string, data: string, scope: NativeRNCloudCloudStorageScope, overwrite: boolean) => Promise<void>;
29
29
  createDirectory: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<void>;
30
+ listFiles: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<string[]>;
30
31
  readFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<string>;
31
32
  deleteFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<void>;
32
33
  statFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<NativeRNCloudCloudStorageFileStat>;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Checks if the path starts with a leading slash and adds one if it doesn't to maintain backwards compatibility.
3
+ * Will log a warning to the console if it had to add a leading slash. Will throw an error in the future.
4
+ *
5
+ * @param path The path to check.
6
+ * @returns The path with a leading slash, if it didn't have one already.
7
+ * @private
8
+ */
9
+ export const verifyLeadingSlash = (path: string) => {
10
+ if (!path.startsWith('/')) {
11
+ console.warn(
12
+ `[react-native-cloud-storage] Path "${path}" did not start with a leading slash. This is deprecated and will be an error in the future.`
13
+ );
14
+ return `/${path}`;
15
+ }
16
+ return path;
17
+ };