react-native-cloud-storage 0.4.1 → 0.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 (59) hide show
  1. package/lib/commonjs/RNCloudStorage.js +12 -2
  2. package/lib/commonjs/RNCloudStorage.js.map +1 -1
  3. package/lib/commonjs/createRNCloudStorage.js +5 -5
  4. package/lib/commonjs/createRNCloudStorage.js.map +1 -1
  5. package/lib/commonjs/google-drive/index.js +82 -25
  6. package/lib/commonjs/google-drive/index.js.map +1 -1
  7. package/lib/commonjs/hooks/useCloudFile.js.map +1 -1
  8. package/lib/commonjs/index.js +7 -7
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/types/main.js +6 -6
  11. package/lib/commonjs/types/main.js.map +1 -1
  12. package/lib/commonjs/types/native.js +15 -13
  13. package/lib/commonjs/types/native.js.map +1 -1
  14. package/lib/commonjs/utils/{NativeStorageError.js → CloudStorageError.js} +3 -3
  15. package/lib/commonjs/utils/CloudStorageError.js.map +1 -0
  16. package/lib/module/RNCloudStorage.js +12 -2
  17. package/lib/module/RNCloudStorage.js.map +1 -1
  18. package/lib/module/createRNCloudStorage.js +6 -6
  19. package/lib/module/createRNCloudStorage.js.map +1 -1
  20. package/lib/module/google-drive/index.js +84 -27
  21. package/lib/module/google-drive/index.js.map +1 -1
  22. package/lib/module/hooks/useCloudFile.js.map +1 -1
  23. package/lib/module/index.js +3 -4
  24. package/lib/module/index.js.map +1 -1
  25. package/lib/module/types/main.js +4 -4
  26. package/lib/module/types/main.js.map +1 -1
  27. package/lib/module/types/native.js +13 -11
  28. package/lib/module/types/native.js.map +1 -1
  29. package/lib/module/utils/CloudStorageError.js +9 -0
  30. package/lib/module/utils/CloudStorageError.js.map +1 -0
  31. package/lib/typescript/RNCloudStorage.d.ts +15 -8
  32. package/lib/typescript/RNCloudStorage.d.ts.map +1 -1
  33. package/lib/typescript/google-drive/index.d.ts +21 -10
  34. package/lib/typescript/google-drive/index.d.ts.map +1 -1
  35. package/lib/typescript/hooks/useCloudFile.d.ts +2 -2
  36. package/lib/typescript/hooks/useCloudFile.d.ts.map +1 -1
  37. package/lib/typescript/index.d.ts +3 -4
  38. package/lib/typescript/index.d.ts.map +1 -1
  39. package/lib/typescript/types/main.d.ts +2 -2
  40. package/lib/typescript/types/main.d.ts.map +1 -1
  41. package/lib/typescript/types/native.d.ts +10 -8
  42. package/lib/typescript/types/native.d.ts.map +1 -1
  43. package/lib/typescript/utils/CloudStorageError.d.ts +8 -0
  44. package/lib/typescript/utils/CloudStorageError.d.ts.map +1 -0
  45. package/package.json +1 -1
  46. package/src/RNCloudStorage.ts +18 -8
  47. package/src/createRNCloudStorage.ts +6 -6
  48. package/src/google-drive/index.ts +118 -40
  49. package/src/hooks/useCloudFile.ts +2 -2
  50. package/src/index.ts +3 -4
  51. package/src/types/main.ts +2 -2
  52. package/src/types/native.ts +10 -8
  53. package/src/utils/CloudStorageError.ts +14 -0
  54. package/lib/commonjs/utils/NativeStorageError.js.map +0 -1
  55. package/lib/module/utils/NativeStorageError.js +0 -9
  56. package/lib/module/utils/NativeStorageError.js.map +0 -1
  57. package/lib/typescript/utils/NativeStorageError.d.ts +0 -8
  58. package/lib/typescript/utils/NativeStorageError.d.ts.map +0 -1
  59. package/src/utils/NativeStorageError.ts +0 -14
@@ -1,11 +1,21 @@
1
1
  import createRNCloudStorage from './createRNCloudStorage';
2
2
  import GoogleDriveApiClient from './google-drive';
3
- import type { StorageFileStat, StorageScope } from './types/main';
3
+ import type { CloudStorageFileStat, CloudStorageScope } from './types/main';
4
+ import { Platform } from 'react-native';
4
5
 
5
6
  const nativeInstance = createRNCloudStorage();
7
+
6
8
  const RNCloudStorage = {
7
- googleDriveAccessToken: GoogleDriveApiClient.accessToken,
9
+ getGoogleDriveAccessToken: () => GoogleDriveApiClient.accessToken,
8
10
  setGoogleDriveAccessToken: (accessToken: string) => (GoogleDriveApiClient.accessToken = accessToken),
11
+ setThrowOnFilesWithSameName: (enable: boolean) => (GoogleDriveApiClient.throwOnFilesWithSameName = enable),
12
+ /* eslint-disable @typescript-eslint/no-unused-vars */
13
+ subscribeToFilesWithSameName:
14
+ Platform.OS === 'ios'
15
+ ? // @ts-expect-error - subscriber is undefined; just a mock
16
+ (subscriber: ({ path, fileIds }: { path: string; fileIds: string[] }) => void) => ({ remove: () => {} })
17
+ : (nativeInstance as GoogleDriveApiClient).subscribeToFilesWithSameName.bind(nativeInstance),
18
+ /* eslint-enable @typescript-eslint/no-unused-vars */
9
19
 
10
20
  /**
11
21
  * Tests whether or not the cloud storage is available. Always returns true for Google Drive. iCloud may be
@@ -22,7 +32,7 @@ const RNCloudStorage = {
22
32
  * @param scope The directory scope the path is in.
23
33
  * @returns A promise that resolves to true if the path exists, false otherwise.
24
34
  */
25
- exists: (path: string, scope: StorageScope): Promise<boolean> => {
35
+ exists: (path: string, scope: CloudStorageScope): Promise<boolean> => {
26
36
  return nativeInstance.fileExists(path, scope);
27
37
  },
28
38
 
@@ -33,7 +43,7 @@ const RNCloudStorage = {
33
43
  * @param scope The directory scope the path is in.
34
44
  * @returns A promise that resolves when the file has been written.
35
45
  */
36
- writeFile: (path: string, data: string, scope: StorageScope): Promise<void> => {
46
+ writeFile: (path: string, data: string, scope: CloudStorageScope): Promise<void> => {
37
47
  return nativeInstance.createFile(path, data, scope, true);
38
48
  },
39
49
 
@@ -43,7 +53,7 @@ const RNCloudStorage = {
43
53
  * @param scope The directory scope the path is in.
44
54
  * @returns A promise that resolves to the contents of the file.
45
55
  */
46
- readFile: (path: string, scope: StorageScope): Promise<string> => {
56
+ readFile: (path: string, scope: CloudStorageScope): Promise<string> => {
47
57
  return nativeInstance.readFile(path, scope);
48
58
  },
49
59
 
@@ -53,7 +63,7 @@ const RNCloudStorage = {
53
63
  * @param scope The directory scope the path is in.
54
64
  * @returns A promise that resolves when the file has been deleted.
55
65
  */
56
- unlink: (path: string, scope: StorageScope): Promise<void> => {
66
+ unlink: (path: string, scope: CloudStorageScope): Promise<void> => {
57
67
  return nativeInstance.deleteFile(path, scope);
58
68
  },
59
69
 
@@ -61,9 +71,9 @@ const RNCloudStorage = {
61
71
  * Gets the size, creation time, and modification time of the file at the given path.
62
72
  * @param path The file to stat.
63
73
  * @param scope The directory scope the path is in.
64
- * @returns A promise that resolves to the StorageFileStat object.
74
+ * @returns A promise that resolves to the CloudStorageFileStat object.
65
75
  */
66
- stat: async (path: string, scope: StorageScope): Promise<StorageFileStat> => {
76
+ stat: async (path: string, scope: CloudStorageScope): Promise<CloudStorageFileStat> => {
67
77
  const native = await nativeInstance.statFile(path, scope);
68
78
 
69
79
  return {
@@ -1,8 +1,8 @@
1
1
  import { NativeModules, Platform } from 'react-native';
2
2
  import type NativeRNCloudStorage from './types/native';
3
3
  import GoogleDriveApiClient from './google-drive';
4
- import { NativeStorageErrorCode } from './types/native';
5
- import NativeStorageError from './utils/NativeStorageError';
4
+ import { CloudStorageErrorCode } from './types/native';
5
+ import CloudStorageError from './utils/CloudStorageError';
6
6
 
7
7
  const LINKING_ERROR =
8
8
  `The package 'react-native-cloud-storage' doesn't seem to be linked. Make sure: \n\n` +
@@ -10,7 +10,7 @@ const LINKING_ERROR =
10
10
  '- You rebuilt the app after installing the package\n' +
11
11
  '- You are not using Expo Go\n';
12
12
 
13
- // proxy NativeModules.CloudStorage to catch any errors thrown by the native module and wrap them in a NativeStorageError
13
+ // proxy NativeModules.CloudStorage to catch any errors thrown by the native module and wrap them in a CloudStorageError
14
14
  const nativeIosInstance = NativeModules.CloudStorage
15
15
  ? new Proxy(NativeModules.CloudStorage, {
16
16
  get(target: NativeRNCloudStorage, prop: keyof NativeRNCloudStorage) {
@@ -21,10 +21,10 @@ const nativeIosInstance = NativeModules.CloudStorage
21
21
  // @ts-expect-error - we can't know the types of the functions and their arguments
22
22
  return await originalFunction(...args);
23
23
  } catch (error: any) {
24
- if (error?.code && Object.values(NativeStorageErrorCode).includes(error.code)) {
25
- throw new NativeStorageError(error?.message || '', error.code as NativeStorageErrorCode);
24
+ if (error?.code && Object.values(CloudStorageErrorCode).includes(error.code)) {
25
+ throw new CloudStorageError(error?.message || '', error.code as CloudStorageErrorCode);
26
26
  } else {
27
- throw new NativeStorageError('Unknown error', NativeStorageErrorCode.UNKNOWN, error);
27
+ throw new CloudStorageError('Unknown error', CloudStorageErrorCode.UNKNOWN, error);
28
28
  }
29
29
  }
30
30
  };
@@ -1,26 +1,30 @@
1
- import { GDrive, MimeTypes } from 'react-native-google-drive-api-wrapper-js';
1
+ import { GDrive, HttpError, MimeTypes } from 'react-native-google-drive-api-wrapper-js';
2
2
  import type NativeRNCloudStorage from '../types/native';
3
3
  import {
4
- NativeStorageErrorCode,
5
- type NativeRNCloudStorageFileStat,
6
- type NativeRNCloudStorageScope,
4
+ CloudStorageErrorCode,
5
+ type NativeRNCloudCloudStorageFileStat,
6
+ type NativeRNCloudCloudStorageScope,
7
7
  } from '../types/native';
8
8
  import type { GoogleDriveDetailedFile, GoogleDriveFile, GoogleDriveListOperationResponse } from './types';
9
- import NativeStorageError from '../utils/NativeStorageError';
9
+ import CloudStorageError from '../utils/CloudStorageError';
10
10
 
11
- class GoogleDriveApiClient implements NativeRNCloudStorage {
11
+ export default class GoogleDriveApiClient implements NativeRNCloudStorage {
12
12
  private static drive: GDrive = new GDrive();
13
+ public static throwOnFilesWithSameName = false;
14
+ public filesWithSameNameSubscribers: (({ path, fileIds }: { path: string; fileIds: string[] }) => void)[];
13
15
 
14
16
  constructor() {
17
+ this.filesWithSameNameSubscribers = [];
15
18
  GoogleDriveApiClient.drive.fetchTimeout = 3000;
16
19
  return new Proxy(this, {
17
20
  // before calling any function, check if the access token is set
18
21
  get(target: GoogleDriveApiClient, prop: keyof GoogleDriveApiClient) {
19
- if (typeof target[prop] === 'function' && prop !== 'isCloudAvailable') {
22
+ const allowedFunctions = ['isCloudAvailable', 'subscribeToFilesWithSameName'];
23
+ if (typeof target[prop] === 'function' && !allowedFunctions.includes(prop.toString())) {
20
24
  if (!GoogleDriveApiClient.drive.accessToken) {
21
- throw new NativeStorageError(
25
+ throw new CloudStorageError(
22
26
  `Google Drive access token is not set, cannot call function ${prop.toString()}`,
23
- NativeStorageErrorCode.GOOGLE_DRIVE_ACCESS_TOKEN_MISSING
27
+ CloudStorageErrorCode.GOOGLE_DRIVE_ACCESS_TOKEN_MISSING
24
28
  );
25
29
  }
26
30
  }
@@ -31,17 +35,29 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
31
35
  }
32
36
 
33
37
  // when setting accessToken, set it on the GDrive instance
34
- public static set accessToken(accessToken: string) {
38
+ public static set accessToken(accessToken: string | undefined) {
35
39
  GoogleDriveApiClient.drive.accessToken = accessToken;
36
40
  }
37
41
 
38
- public static get accessToken(): string {
42
+ public static get accessToken(): string | undefined {
39
43
  return GoogleDriveApiClient.drive.accessToken;
40
44
  }
41
45
 
46
+ public subscribeToFilesWithSameName(subscriber: ({ path, fileIds }: { path: string; fileIds: string[] }) => void): {
47
+ remove: () => void;
48
+ } {
49
+ this.filesWithSameNameSubscribers.push(subscriber);
50
+
51
+ return {
52
+ remove: () => {
53
+ this.filesWithSameNameSubscribers = this.filesWithSameNameSubscribers.filter((s) => s !== subscriber);
54
+ },
55
+ };
56
+ }
57
+
42
58
  public isCloudAvailable: () => Promise<boolean> = async () => !!GoogleDriveApiClient.accessToken?.length;
43
59
 
44
- private getRootDirectory(scope: NativeRNCloudStorageScope): 'drive' | 'appDataFolder' {
60
+ private getRootDirectory(scope: NativeRNCloudCloudStorageScope): 'drive' | 'appDataFolder' {
45
61
  switch (scope) {
46
62
  case 'documents':
47
63
  return 'drive';
@@ -80,9 +96,9 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
80
96
  }
81
97
 
82
98
  if (!topDirectoryId) {
83
- throw new NativeStorageError(
99
+ throw new CloudStorageError(
84
100
  `Could not find top directory with name ${directoryTree[0]}`,
85
- NativeStorageErrorCode.DIRECTORY_NOT_FOUND
101
+ CloudStorageErrorCode.DIRECTORY_NOT_FOUND
86
102
  );
87
103
  }
88
104
 
@@ -91,15 +107,15 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
91
107
  for (let i = 1; i < directoryTree.length; i++) {
92
108
  const currentDirectory = files.find((f) => f.id === currentDirectoryId);
93
109
  if (!currentDirectory)
94
- throw new NativeStorageError(
110
+ throw new CloudStorageError(
95
111
  `Could not find directory with id ${currentDirectoryId}`,
96
- NativeStorageErrorCode.DIRECTORY_NOT_FOUND
112
+ CloudStorageErrorCode.DIRECTORY_NOT_FOUND
97
113
  );
98
114
  const nextDirectory = files.find((f) => f.name === directoryTree[i] && f.parents![0] === currentDirectoryId);
99
115
  if (!nextDirectory)
100
- throw new NativeStorageError(
116
+ throw new CloudStorageError(
101
117
  `Could not find directory with name ${directoryTree[i]}`,
102
- NativeStorageErrorCode.DIRECTORY_NOT_FOUND
118
+ CloudStorageErrorCode.DIRECTORY_NOT_FOUND
103
119
  );
104
120
  currentDirectoryId = nextDirectory.id;
105
121
  }
@@ -107,7 +123,7 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
107
123
  return currentDirectoryId;
108
124
  }
109
125
 
110
- private async listFiles(scope: NativeRNCloudStorageScope): Promise<GoogleDriveFile[]> {
126
+ private async listFiles(scope: NativeRNCloudCloudStorageScope): Promise<GoogleDriveFile[]> {
111
127
  const files: GoogleDriveListOperationResponse = await GoogleDriveApiClient.drive.files.list({
112
128
  spaces: [this.getRootDirectory(scope)],
113
129
  fields: 'files(id,kind,mimeType,name,parents,spaces)',
@@ -116,41 +132,105 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
116
132
  return files.files;
117
133
  }
118
134
 
119
- private async getFileId(path: string, scope: NativeRNCloudStorageScope): Promise<string> {
120
- const files = await this.listFiles(scope);
121
- const { directories, filename } = this.resolvePathToDirectories(path);
122
- const parentDirectoryId = this.findParentDirectoryId(files, directories);
123
- let file: GoogleDriveFile | undefined;
124
- if (parentDirectoryId === null) {
125
- /* when the file is supposes to be in the root directory, we need to get the file where the name is the filename
126
- and the first parent has an id which does not exist in the files array */
127
- file = files.find((f) => f.name === filename && !files.find((f2) => f2.id === f.parents![0]));
135
+ private checkIfMultipleFilesWithSameName(
136
+ path: string,
137
+ files: GoogleDriveFile[],
138
+ filename: string,
139
+ parentDirectoryId: string | null
140
+ ) {
141
+ let possibleFiles: GoogleDriveFile[];
142
+ if (parentDirectoryId) {
143
+ possibleFiles = files.filter((f) => f.name === filename && f.parents![0] === parentDirectoryId);
128
144
  } else {
129
- file = files.find((f) => f.name === filename && f.parents![0] === parentDirectoryId);
145
+ possibleFiles = files.filter((f) => f.name === filename && !files.find((f2) => f2.id === f.parents![0]));
146
+ }
147
+
148
+ if (possibleFiles.length <= 1) return;
149
+
150
+ if (GoogleDriveApiClient.throwOnFilesWithSameName) {
151
+ throw new CloudStorageError(
152
+ `Multiple files with the same name found at path ${path}: ${possibleFiles.map((f) => f.id).join(', ')}`,
153
+ CloudStorageErrorCode.MULTIPLE_FILES_SAME_NAME
154
+ );
155
+ } else {
156
+ this.filesWithSameNameSubscribers.forEach((s) => s({ path, fileIds: possibleFiles.map((f) => f.id) }));
157
+ }
158
+ }
159
+
160
+ private async getFileId(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string> {
161
+ try {
162
+ const files = await this.listFiles(scope);
163
+ const { directories, filename } = this.resolvePathToDirectories(path);
164
+ const parentDirectoryId = this.findParentDirectoryId(files, directories);
165
+ let file: GoogleDriveFile | undefined;
166
+ if (parentDirectoryId === null) {
167
+ this.checkIfMultipleFilesWithSameName(path, files, filename, null);
168
+ /* when the file is supposed to be in the root directory, we need to get the file where the name is the filename
169
+ and the first parent has an id which does not exist in the files array */
170
+ file = files.find((f) => f.name === filename && !files.find((f2) => f2.id === f.parents![0]));
171
+ } else {
172
+ this.checkIfMultipleFilesWithSameName(path, files, filename, parentDirectoryId);
173
+ file = files.find((f) => f.name === filename && f.parents![0] === parentDirectoryId);
174
+ }
175
+ if (!file) throw new CloudStorageError(`File not found`, CloudStorageErrorCode.FILE_NOT_FOUND);
176
+ if (file.mimeType === MimeTypes.FOLDER) {
177
+ throw new CloudStorageError(`Path ${path} is a directory`, CloudStorageErrorCode.FILE_NOT_FOUND);
178
+ }
179
+ return file.id;
180
+ } catch (e: unknown) {
181
+ if (e instanceof HttpError && e.json?.error?.status === 'UNAUTHENTICATED') {
182
+ throw new CloudStorageError(
183
+ `Could not authenticate with Google Drive`,
184
+ CloudStorageErrorCode.AUTHENTICATION_FAILED,
185
+ e.json
186
+ );
187
+ } else {
188
+ if (e instanceof CloudStorageError) throw e;
189
+ throw new CloudStorageError(`Could not get file id for path ${path}`, CloudStorageErrorCode.UNKNOWN, e);
190
+ }
130
191
  }
131
- if (!file) throw new NativeStorageError(`File not found`, NativeStorageErrorCode.FILE_NOT_FOUND);
132
- return file.id;
133
192
  }
134
193
 
135
- async fileExists(path: string, scope: NativeRNCloudStorageScope): Promise<boolean> {
194
+ async fileExists(path: string, scope: NativeRNCloudCloudStorageScope): Promise<boolean> {
136
195
  try {
137
196
  await this.getFileId(path, scope);
138
197
  return true;
139
198
  } catch (e: any) {
140
- if (e instanceof NativeStorageError && e.code === NativeStorageErrorCode.FILE_NOT_FOUND) return false;
199
+ if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) return false;
141
200
  else throw e;
142
201
  }
143
202
  }
144
203
 
145
- async createFile(path: string, data: string, scope: NativeRNCloudStorageScope, overwrite: boolean): Promise<void> {
204
+ async createFile(
205
+ path: string,
206
+ data: string,
207
+ scope: NativeRNCloudCloudStorageScope,
208
+ overwrite: boolean
209
+ ): Promise<void> {
146
210
  let fileId: string | undefined;
147
211
  if (overwrite) {
148
212
  try {
149
213
  fileId = await this.getFileId(path, scope);
150
214
  } catch (e: any) {
151
- /* do nothing, simply create the file */
215
+ if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) {
216
+ /* do nothing, simply create the file */
217
+ } else {
218
+ throw e;
219
+ }
220
+ }
221
+ } else {
222
+ try {
223
+ await this.getFileId(path, scope);
224
+ throw new CloudStorageError(`File ${path} already exists`, CloudStorageErrorCode.FILE_ALREADY_EXISTS);
225
+ } catch (e: any) {
226
+ if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) {
227
+ /* do nothing, simply create the file */
228
+ } else {
229
+ throw e;
230
+ }
152
231
  }
153
232
  }
233
+
154
234
  const uploader = GoogleDriveApiClient.drive.files.newMultipartUploader().setData(data, MimeTypes.TEXT);
155
235
  if (fileId) uploader.setIdOfFileToUpdate(fileId);
156
236
  else {
@@ -169,18 +249,18 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
169
249
  await uploader.execute();
170
250
  }
171
251
 
172
- async readFile(path: string, scope: NativeRNCloudStorageScope): Promise<string> {
252
+ async readFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string> {
173
253
  const fileId = await this.getFileId(path, scope);
174
254
  const content = await GoogleDriveApiClient.drive.files.getText(fileId);
175
255
  return content;
176
256
  }
177
257
 
178
- async deleteFile(path: string, scope: NativeRNCloudStorageScope): Promise<void> {
258
+ async deleteFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<void> {
179
259
  const fileId = await this.getFileId(path, scope);
180
260
  await GoogleDriveApiClient.drive.files.delete(fileId);
181
261
  }
182
262
 
183
- async statFile(path: string, scope: NativeRNCloudStorageScope): Promise<NativeRNCloudStorageFileStat> {
263
+ async statFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<NativeRNCloudCloudStorageFileStat> {
184
264
  const fileId = await this.getFileId(path, scope);
185
265
  const file: GoogleDriveDetailedFile = await GoogleDriveApiClient.drive.files.get(fileId, {
186
266
  fields: 'id,kind,mimeType,name,parents,spaces,size,createdTime,modifiedTime',
@@ -194,5 +274,3 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
194
274
  };
195
275
  }
196
276
  }
197
-
198
- export default GoogleDriveApiClient;
@@ -1,8 +1,8 @@
1
- import type { StorageScope } from '../types/main';
1
+ 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: StorageScope) => {
5
+ export const useCloudFile = (path: string, scope: CloudStorageScope) => {
6
6
  const [content, setContent] = useState<string | null>(null);
7
7
 
8
8
  const read = useCallback(async () => {
package/src/index.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import RNCloudStorage from './RNCloudStorage';
2
- import { NativeStorageErrorCode } from './types/native';
2
+ import { CloudStorageErrorCode } from './types/native';
3
3
  export * from './types/main';
4
4
  export * from './hooks/useCloudFile';
5
5
  export * from './hooks/useIsCloudAvailable';
6
- import NativeStorageError from './utils/NativeStorageError';
6
+ import CloudStorageError from './utils/CloudStorageError';
7
7
 
8
- export { NativeStorageError };
9
- export { NativeStorageErrorCode };
8
+ export { CloudStorageError, CloudStorageErrorCode };
10
9
  export default RNCloudStorage;
package/src/types/main.ts CHANGED
@@ -1,9 +1,9 @@
1
- export enum StorageScope {
1
+ export enum CloudStorageScope {
2
2
  Documents = 'documents',
3
3
  AppData = 'app_data',
4
4
  }
5
5
 
6
- export interface StorageFileStat {
6
+ export interface CloudStorageFileStat {
7
7
  size: number;
8
8
  birthtimeMs: number;
9
9
  mtimeMs: number;
@@ -1,6 +1,6 @@
1
- export type NativeRNCloudStorageScope = 'documents' | 'app_data';
1
+ export type NativeRNCloudCloudStorageScope = 'documents' | 'app_data';
2
2
 
3
- export interface NativeRNCloudStorageFileStat {
3
+ export interface NativeRNCloudCloudStorageFileStat {
4
4
  size: number;
5
5
  birthtimeMs: number;
6
6
  mtimeMs: number;
@@ -8,10 +8,12 @@ export interface NativeRNCloudStorageFileStat {
8
8
  isFile: boolean;
9
9
  }
10
10
 
11
- export enum NativeStorageErrorCode {
11
+ export enum CloudStorageErrorCode {
12
12
  FILE_NOT_FOUND = 'ERR_FILE_NOT_FOUND',
13
13
  DIRECTORY_NOT_FOUND = 'ERR_NO_DIRECTORY_FOUND',
14
14
  FILE_ALREADY_EXISTS = 'ERR_FILE_EXISTS',
15
+ MULTIPLE_FILES_SAME_NAME = 'ERR_MULTIPLE_FILES_SAME_NAME',
16
+ AUTHENTICATION_FAILED = 'ERR_AUTHENTICATION_FAILED',
15
17
  WRITE_ERROR = 'ERR_WRITE_ERROR',
16
18
  READ_ERROR = 'ERR_READ_ERROR',
17
19
  DELETE_ERROR = 'ERR_DELETE_ERROR',
@@ -21,10 +23,10 @@ export enum NativeStorageErrorCode {
21
23
  }
22
24
 
23
25
  export default interface NativeRNCloudStorage {
24
- fileExists: (path: string, scope: NativeRNCloudStorageScope) => Promise<boolean>;
25
- createFile: (path: string, data: string, scope: NativeRNCloudStorageScope, overwrite: boolean) => Promise<void>;
26
- readFile: (path: string, scope: NativeRNCloudStorageScope) => Promise<string>;
27
- deleteFile: (path: string, scope: NativeRNCloudStorageScope) => Promise<void>;
28
- statFile: (path: string, scope: NativeRNCloudStorageScope) => Promise<NativeRNCloudStorageFileStat>;
26
+ fileExists: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<boolean>;
27
+ createFile: (path: string, data: string, scope: NativeRNCloudCloudStorageScope, overwrite: boolean) => Promise<void>;
28
+ readFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<string>;
29
+ deleteFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<void>;
30
+ statFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<NativeRNCloudCloudStorageFileStat>;
29
31
  isCloudAvailable: () => Promise<boolean>;
30
32
  }
@@ -0,0 +1,14 @@
1
+ import type { CloudStorageErrorCode } from '../types/native';
2
+
3
+ class CloudStorageError extends Error {
4
+ code: CloudStorageErrorCode;
5
+ details?: any;
6
+
7
+ constructor(message: string, code: CloudStorageErrorCode, details?: any) {
8
+ super(message);
9
+ this.code = code;
10
+ this.details = details;
11
+ }
12
+ }
13
+
14
+ export default CloudStorageError;
@@ -1 +0,0 @@
1
- {"version":3,"names":["NativeStorageError","Error","constructor","message","code","details","_default","exports","default"],"sourceRoot":"../../../src","sources":["utils/NativeStorageError.ts"],"mappings":";;;;;;AAEA,MAAMA,kBAAkB,SAASC,KAAK,CAAC;EAIrCC,WAAWA,CAACC,OAAe,EAAEC,IAA4B,EAAEC,OAAa,EAAE;IACxE,KAAK,CAACF,OAAO,CAAC;IACd,IAAI,CAACC,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,OAAO,GAAGA,OAAO;EACxB;AACF;AAAC,IAAAC,QAAA,GAEcN,kBAAkB;AAAAO,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
@@ -1,9 +0,0 @@
1
- class NativeStorageError extends Error {
2
- constructor(message, code, details) {
3
- super(message);
4
- this.code = code;
5
- this.details = details;
6
- }
7
- }
8
- export default NativeStorageError;
9
- //# sourceMappingURL=NativeStorageError.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["NativeStorageError","Error","constructor","message","code","details"],"sourceRoot":"../../../src","sources":["utils/NativeStorageError.ts"],"mappings":"AAEA,MAAMA,kBAAkB,SAASC,KAAK,CAAC;EAIrCC,WAAWA,CAACC,OAAe,EAAEC,IAA4B,EAAEC,OAAa,EAAE;IACxE,KAAK,CAACF,OAAO,CAAC;IACd,IAAI,CAACC,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,OAAO,GAAGA,OAAO;EACxB;AACF;AAEA,eAAeL,kBAAkB"}
@@ -1,8 +0,0 @@
1
- import type { NativeStorageErrorCode } from '../types/native';
2
- declare class NativeStorageError extends Error {
3
- code: NativeStorageErrorCode;
4
- details?: any;
5
- constructor(message: string, code: NativeStorageErrorCode, details?: any);
6
- }
7
- export default NativeStorageError;
8
- //# sourceMappingURL=NativeStorageError.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"NativeStorageError.d.ts","sourceRoot":"","sources":["../../../src/utils/NativeStorageError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAE9D,cAAM,kBAAmB,SAAQ,KAAK;IACpC,IAAI,EAAE,sBAAsB,CAAC;IAC7B,OAAO,CAAC,EAAE,GAAG,CAAC;gBAEF,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,sBAAsB,EAAE,OAAO,CAAC,EAAE,GAAG;CAKzE;AAED,eAAe,kBAAkB,CAAC"}
@@ -1,14 +0,0 @@
1
- import type { NativeStorageErrorCode } from '../types/native';
2
-
3
- class NativeStorageError extends Error {
4
- code: NativeStorageErrorCode;
5
- details?: any;
6
-
7
- constructor(message: string, code: NativeStorageErrorCode, details?: any) {
8
- super(message);
9
- this.code = code;
10
- this.details = details;
11
- }
12
- }
13
-
14
- export default NativeStorageError;