react-native-cloud-storage 0.4.0 → 0.4.2

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 (66) hide show
  1. package/lib/commonjs/RNCloudStorage.js +2 -1
  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 +12 -9
  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/hooks/useIsCloudAvailable.js +18 -15
  9. package/lib/commonjs/hooks/useIsCloudAvailable.js.map +1 -1
  10. package/lib/commonjs/index.js +7 -7
  11. package/lib/commonjs/index.js.map +1 -1
  12. package/lib/commonjs/types/main.js +6 -6
  13. package/lib/commonjs/types/main.js.map +1 -1
  14. package/lib/commonjs/types/native.js +13 -13
  15. package/lib/commonjs/types/native.js.map +1 -1
  16. package/lib/commonjs/utils/{NativeStorageError.js → CloudStorageError.js} +3 -3
  17. package/lib/commonjs/utils/CloudStorageError.js.map +1 -0
  18. package/lib/module/RNCloudStorage.js +2 -1
  19. package/lib/module/RNCloudStorage.js.map +1 -1
  20. package/lib/module/createRNCloudStorage.js +6 -6
  21. package/lib/module/createRNCloudStorage.js.map +1 -1
  22. package/lib/module/google-drive/index.js +13 -10
  23. package/lib/module/google-drive/index.js.map +1 -1
  24. package/lib/module/hooks/useCloudFile.js.map +1 -1
  25. package/lib/module/hooks/useIsCloudAvailable.js +18 -15
  26. package/lib/module/hooks/useIsCloudAvailable.js.map +1 -1
  27. package/lib/module/index.js +4 -4
  28. package/lib/module/index.js.map +1 -1
  29. package/lib/module/types/main.js +4 -4
  30. package/lib/module/types/main.js.map +1 -1
  31. package/lib/module/types/native.js +11 -11
  32. package/lib/module/types/native.js.map +1 -1
  33. package/lib/module/utils/CloudStorageError.js +9 -0
  34. package/lib/module/utils/CloudStorageError.js.map +1 -0
  35. package/lib/typescript/RNCloudStorage.d.ts +8 -7
  36. package/lib/typescript/RNCloudStorage.d.ts.map +1 -1
  37. package/lib/typescript/google-drive/index.d.ts +8 -8
  38. package/lib/typescript/google-drive/index.d.ts.map +1 -1
  39. package/lib/typescript/hooks/useCloudFile.d.ts +2 -2
  40. package/lib/typescript/hooks/useCloudFile.d.ts.map +1 -1
  41. package/lib/typescript/hooks/useIsCloudAvailable.d.ts +1 -1
  42. package/lib/typescript/hooks/useIsCloudAvailable.d.ts.map +1 -1
  43. package/lib/typescript/index.d.ts +4 -4
  44. package/lib/typescript/index.d.ts.map +1 -1
  45. package/lib/typescript/types/main.d.ts +2 -2
  46. package/lib/typescript/types/main.d.ts.map +1 -1
  47. package/lib/typescript/types/native.d.ts +8 -8
  48. package/lib/typescript/types/native.d.ts.map +1 -1
  49. package/lib/typescript/utils/CloudStorageError.d.ts +8 -0
  50. package/lib/typescript/utils/CloudStorageError.d.ts.map +1 -0
  51. package/package.json +1 -1
  52. package/src/RNCloudStorage.ts +8 -7
  53. package/src/createRNCloudStorage.ts +6 -6
  54. package/src/google-drive/index.ts +31 -26
  55. package/src/hooks/useCloudFile.ts +2 -2
  56. package/src/hooks/useIsCloudAvailable.ts +20 -15
  57. package/src/index.ts +4 -4
  58. package/src/types/main.ts +2 -2
  59. package/src/types/native.ts +8 -8
  60. package/src/utils/CloudStorageError.ts +14 -0
  61. package/lib/commonjs/utils/NativeStorageError.js.map +0 -1
  62. package/lib/module/utils/NativeStorageError.js +0 -9
  63. package/lib/module/utils/NativeStorageError.js.map +0 -1
  64. package/lib/typescript/utils/NativeStorageError.d.ts +0 -8
  65. package/lib/typescript/utils/NativeStorageError.d.ts.map +0 -1
  66. package/src/utils/NativeStorageError.ts +0 -14
@@ -1,12 +1,12 @@
1
1
  import { GDrive, 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
11
  class GoogleDriveApiClient implements NativeRNCloudStorage {
12
12
  private static drive: GDrive = new GDrive();
@@ -16,11 +16,11 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
16
16
  return new Proxy(this, {
17
17
  // before calling any function, check if the access token is set
18
18
  get(target: GoogleDriveApiClient, prop: keyof GoogleDriveApiClient) {
19
- if (typeof target[prop] === 'function') {
19
+ if (typeof target[prop] === 'function' && prop !== 'isCloudAvailable') {
20
20
  if (!GoogleDriveApiClient.drive.accessToken) {
21
- throw new NativeStorageError(
21
+ throw new CloudStorageError(
22
22
  `Google Drive access token is not set, cannot call function ${prop.toString()}`,
23
- NativeStorageErrorCode.GOOGLE_DRIVE_ACCESS_TOKEN_MISSING
23
+ CloudStorageErrorCode.GOOGLE_DRIVE_ACCESS_TOKEN_MISSING
24
24
  );
25
25
  }
26
26
  }
@@ -31,17 +31,17 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
31
31
  }
32
32
 
33
33
  // when setting accessToken, set it on the GDrive instance
34
- public static set accessToken(accessToken: string) {
34
+ public static set accessToken(accessToken: string | undefined) {
35
35
  GoogleDriveApiClient.drive.accessToken = accessToken;
36
36
  }
37
37
 
38
- public static get accessToken(): string {
38
+ public static get accessToken(): string | undefined {
39
39
  return GoogleDriveApiClient.drive.accessToken;
40
40
  }
41
41
 
42
- public isCloudAvailable: () => Promise<boolean> = async () => true;
42
+ public isCloudAvailable: () => Promise<boolean> = async () => !!GoogleDriveApiClient.accessToken?.length;
43
43
 
44
- private getRootDirectory(scope: NativeRNCloudStorageScope): 'drive' | 'appDataFolder' {
44
+ private getRootDirectory(scope: NativeRNCloudCloudStorageScope): 'drive' | 'appDataFolder' {
45
45
  switch (scope) {
46
46
  case 'documents':
47
47
  return 'drive';
@@ -80,9 +80,9 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
80
80
  }
81
81
 
82
82
  if (!topDirectoryId) {
83
- throw new NativeStorageError(
83
+ throw new CloudStorageError(
84
84
  `Could not find top directory with name ${directoryTree[0]}`,
85
- NativeStorageErrorCode.DIRECTORY_NOT_FOUND
85
+ CloudStorageErrorCode.DIRECTORY_NOT_FOUND
86
86
  );
87
87
  }
88
88
 
@@ -91,15 +91,15 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
91
91
  for (let i = 1; i < directoryTree.length; i++) {
92
92
  const currentDirectory = files.find((f) => f.id === currentDirectoryId);
93
93
  if (!currentDirectory)
94
- throw new NativeStorageError(
94
+ throw new CloudStorageError(
95
95
  `Could not find directory with id ${currentDirectoryId}`,
96
- NativeStorageErrorCode.DIRECTORY_NOT_FOUND
96
+ CloudStorageErrorCode.DIRECTORY_NOT_FOUND
97
97
  );
98
98
  const nextDirectory = files.find((f) => f.name === directoryTree[i] && f.parents![0] === currentDirectoryId);
99
99
  if (!nextDirectory)
100
- throw new NativeStorageError(
100
+ throw new CloudStorageError(
101
101
  `Could not find directory with name ${directoryTree[i]}`,
102
- NativeStorageErrorCode.DIRECTORY_NOT_FOUND
102
+ CloudStorageErrorCode.DIRECTORY_NOT_FOUND
103
103
  );
104
104
  currentDirectoryId = nextDirectory.id;
105
105
  }
@@ -107,7 +107,7 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
107
107
  return currentDirectoryId;
108
108
  }
109
109
 
110
- private async listFiles(scope: NativeRNCloudStorageScope): Promise<GoogleDriveFile[]> {
110
+ private async listFiles(scope: NativeRNCloudCloudStorageScope): Promise<GoogleDriveFile[]> {
111
111
  const files: GoogleDriveListOperationResponse = await GoogleDriveApiClient.drive.files.list({
112
112
  spaces: [this.getRootDirectory(scope)],
113
113
  fields: 'files(id,kind,mimeType,name,parents,spaces)',
@@ -116,7 +116,7 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
116
116
  return files.files;
117
117
  }
118
118
 
119
- private async getFileId(path: string, scope: NativeRNCloudStorageScope): Promise<string> {
119
+ private async getFileId(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string> {
120
120
  const files = await this.listFiles(scope);
121
121
  const { directories, filename } = this.resolvePathToDirectories(path);
122
122
  const parentDirectoryId = this.findParentDirectoryId(files, directories);
@@ -128,21 +128,26 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
128
128
  } else {
129
129
  file = files.find((f) => f.name === filename && f.parents![0] === parentDirectoryId);
130
130
  }
131
- if (!file) throw new NativeStorageError(`File not found`, NativeStorageErrorCode.FILE_NOT_FOUND);
131
+ if (!file) throw new CloudStorageError(`File not found`, CloudStorageErrorCode.FILE_NOT_FOUND);
132
132
  return file.id;
133
133
  }
134
134
 
135
- async fileExists(path: string, scope: NativeRNCloudStorageScope): Promise<boolean> {
135
+ async fileExists(path: string, scope: NativeRNCloudCloudStorageScope): Promise<boolean> {
136
136
  try {
137
137
  await this.getFileId(path, scope);
138
138
  return true;
139
139
  } catch (e: any) {
140
- if (e instanceof NativeStorageError && e.code === NativeStorageErrorCode.FILE_NOT_FOUND) return false;
140
+ if (e instanceof CloudStorageError && e.code === CloudStorageErrorCode.FILE_NOT_FOUND) return false;
141
141
  else throw e;
142
142
  }
143
143
  }
144
144
 
145
- async createFile(path: string, data: string, scope: NativeRNCloudStorageScope, overwrite: boolean): Promise<void> {
145
+ async createFile(
146
+ path: string,
147
+ data: string,
148
+ scope: NativeRNCloudCloudStorageScope,
149
+ overwrite: boolean
150
+ ): Promise<void> {
146
151
  let fileId: string | undefined;
147
152
  if (overwrite) {
148
153
  try {
@@ -169,18 +174,18 @@ class GoogleDriveApiClient implements NativeRNCloudStorage {
169
174
  await uploader.execute();
170
175
  }
171
176
 
172
- async readFile(path: string, scope: NativeRNCloudStorageScope): Promise<string> {
177
+ async readFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<string> {
173
178
  const fileId = await this.getFileId(path, scope);
174
179
  const content = await GoogleDriveApiClient.drive.files.getText(fileId);
175
180
  return content;
176
181
  }
177
182
 
178
- async deleteFile(path: string, scope: NativeRNCloudStorageScope): Promise<void> {
183
+ async deleteFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<void> {
179
184
  const fileId = await this.getFileId(path, scope);
180
185
  await GoogleDriveApiClient.drive.files.delete(fileId);
181
186
  }
182
187
 
183
- async statFile(path: string, scope: NativeRNCloudStorageScope): Promise<NativeRNCloudStorageFileStat> {
188
+ async statFile(path: string, scope: NativeRNCloudCloudStorageScope): Promise<NativeRNCloudCloudStorageFileStat> {
184
189
  const fileId = await this.getFileId(path, scope);
185
190
  const file: GoogleDriveDetailedFile = await GoogleDriveApiClient.drive.files.get(fileId, {
186
191
  fields: 'id,kind,mimeType,name,parents,spaces,size,createdTime,modifiedTime',
@@ -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 () => {
@@ -3,23 +3,28 @@ import { Platform } from 'react-native';
3
3
  import RNCloudStorage from '../RNCloudStorage';
4
4
 
5
5
  // TODO: there must be a better way to do this without a timeout?
6
- export const useIsCloudAvailable = (timeout = 10) => {
7
- const [isCloudAvailable, setIsCloudAvailable] = useState(Platform.OS !== 'ios');
8
- const [firstCheck] = useState(new Date());
6
+ export const useIsCloudAvailable = (iCloudTimeout = 10) => {
7
+ const [isAvailable, setIsAvailable] = useState(false);
9
8
 
10
9
  useEffect(() => {
11
- const checkCloudAvailability = async () => {
12
- const isAvailable = await RNCloudStorage.isCloudAvailable();
13
- if (isAvailable) {
14
- setIsCloudAvailable(true);
15
- } else if (new Date().getTime() - firstCheck.getTime() < timeout * 1000) {
16
- setTimeout(checkCloudAvailability, 500);
17
- } else {
18
- console.log('Cloud storage is not available.');
10
+ const interval = setInterval(async () => {
11
+ const newIsAvailable = await RNCloudStorage.isCloudAvailable();
12
+ setIsAvailable(newIsAvailable);
13
+ if (Platform.OS === 'ios' && newIsAvailable) {
14
+ console.log('Stopped because iCloud became available');
15
+ clearInterval(interval);
19
16
  }
20
- };
21
- checkCloudAvailability();
22
- }, [firstCheck, timeout]);
17
+ }, 500);
23
18
 
24
- return isCloudAvailable;
19
+ if (Platform.OS === 'ios') {
20
+ setTimeout(() => {
21
+ console.log('Stopped because of timeout');
22
+ clearInterval(interval);
23
+ }, iCloudTimeout * 1000);
24
+ }
25
+
26
+ return () => clearInterval(interval);
27
+ }, [iCloudTimeout]);
28
+
29
+ return isAvailable;
25
30
  };
package/src/index.ts CHANGED
@@ -1,10 +1,10 @@
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 };
9
+ export { CloudStorageErrorCode };
10
10
  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,7 +8,7 @@ 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',
@@ -21,10 +21,10 @@ export enum NativeStorageErrorCode {
21
21
  }
22
22
 
23
23
  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>;
24
+ fileExists: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<boolean>;
25
+ createFile: (path: string, data: string, scope: NativeRNCloudCloudStorageScope, overwrite: boolean) => Promise<void>;
26
+ readFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<string>;
27
+ deleteFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<void>;
28
+ statFile: (path: string, scope: NativeRNCloudCloudStorageScope) => Promise<NativeRNCloudCloudStorageFileStat>;
29
29
  isCloudAvailable: () => Promise<boolean>;
30
30
  }
@@ -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;