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.
- package/lib/commonjs/RNCloudStorage.js +2 -1
- package/lib/commonjs/RNCloudStorage.js.map +1 -1
- package/lib/commonjs/createRNCloudStorage.js +5 -5
- package/lib/commonjs/createRNCloudStorage.js.map +1 -1
- package/lib/commonjs/google-drive/index.js +12 -9
- package/lib/commonjs/google-drive/index.js.map +1 -1
- package/lib/commonjs/hooks/useCloudFile.js.map +1 -1
- package/lib/commonjs/hooks/useIsCloudAvailable.js +18 -15
- package/lib/commonjs/hooks/useIsCloudAvailable.js.map +1 -1
- package/lib/commonjs/index.js +7 -7
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/types/main.js +6 -6
- package/lib/commonjs/types/main.js.map +1 -1
- package/lib/commonjs/types/native.js +13 -13
- package/lib/commonjs/types/native.js.map +1 -1
- package/lib/commonjs/utils/{NativeStorageError.js → CloudStorageError.js} +3 -3
- package/lib/commonjs/utils/CloudStorageError.js.map +1 -0
- package/lib/module/RNCloudStorage.js +2 -1
- package/lib/module/RNCloudStorage.js.map +1 -1
- package/lib/module/createRNCloudStorage.js +6 -6
- package/lib/module/createRNCloudStorage.js.map +1 -1
- package/lib/module/google-drive/index.js +13 -10
- package/lib/module/google-drive/index.js.map +1 -1
- package/lib/module/hooks/useCloudFile.js.map +1 -1
- package/lib/module/hooks/useIsCloudAvailable.js +18 -15
- package/lib/module/hooks/useIsCloudAvailable.js.map +1 -1
- package/lib/module/index.js +4 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/types/main.js +4 -4
- package/lib/module/types/main.js.map +1 -1
- package/lib/module/types/native.js +11 -11
- package/lib/module/types/native.js.map +1 -1
- package/lib/module/utils/CloudStorageError.js +9 -0
- package/lib/module/utils/CloudStorageError.js.map +1 -0
- package/lib/typescript/RNCloudStorage.d.ts +8 -7
- package/lib/typescript/RNCloudStorage.d.ts.map +1 -1
- package/lib/typescript/google-drive/index.d.ts +8 -8
- package/lib/typescript/google-drive/index.d.ts.map +1 -1
- package/lib/typescript/hooks/useCloudFile.d.ts +2 -2
- package/lib/typescript/hooks/useCloudFile.d.ts.map +1 -1
- package/lib/typescript/hooks/useIsCloudAvailable.d.ts +1 -1
- package/lib/typescript/hooks/useIsCloudAvailable.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +4 -4
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/types/main.d.ts +2 -2
- package/lib/typescript/types/main.d.ts.map +1 -1
- package/lib/typescript/types/native.d.ts +8 -8
- package/lib/typescript/types/native.d.ts.map +1 -1
- package/lib/typescript/utils/CloudStorageError.d.ts +8 -0
- package/lib/typescript/utils/CloudStorageError.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/RNCloudStorage.ts +8 -7
- package/src/createRNCloudStorage.ts +6 -6
- package/src/google-drive/index.ts +31 -26
- package/src/hooks/useCloudFile.ts +2 -2
- package/src/hooks/useIsCloudAvailable.ts +20 -15
- package/src/index.ts +4 -4
- package/src/types/main.ts +2 -2
- package/src/types/native.ts +8 -8
- package/src/utils/CloudStorageError.ts +14 -0
- package/lib/commonjs/utils/NativeStorageError.js.map +0 -1
- package/lib/module/utils/NativeStorageError.js +0 -9
- package/lib/module/utils/NativeStorageError.js.map +0 -1
- package/lib/typescript/utils/NativeStorageError.d.ts +0 -8
- package/lib/typescript/utils/NativeStorageError.d.ts.map +0 -1
- 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
|
-
|
|
5
|
-
type
|
|
6
|
-
type
|
|
4
|
+
CloudStorageErrorCode,
|
|
5
|
+
type NativeRNCloudCloudStorageFileStat,
|
|
6
|
+
type NativeRNCloudCloudStorageScope,
|
|
7
7
|
} from '../types/native';
|
|
8
8
|
import type { GoogleDriveDetailedFile, GoogleDriveFile, GoogleDriveListOperationResponse } from './types';
|
|
9
|
-
import
|
|
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
|
|
21
|
+
throw new CloudStorageError(
|
|
22
22
|
`Google Drive access token is not set, cannot call function ${prop.toString()}`,
|
|
23
|
-
|
|
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 () =>
|
|
42
|
+
public isCloudAvailable: () => Promise<boolean> = async () => !!GoogleDriveApiClient.accessToken?.length;
|
|
43
43
|
|
|
44
|
-
private getRootDirectory(scope:
|
|
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
|
|
83
|
+
throw new CloudStorageError(
|
|
84
84
|
`Could not find top directory with name ${directoryTree[0]}`,
|
|
85
|
-
|
|
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
|
|
94
|
+
throw new CloudStorageError(
|
|
95
95
|
`Could not find directory with id ${currentDirectoryId}`,
|
|
96
|
-
|
|
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
|
|
100
|
+
throw new CloudStorageError(
|
|
101
101
|
`Could not find directory with name ${directoryTree[i]}`,
|
|
102
|
-
|
|
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:
|
|
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:
|
|
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
|
|
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:
|
|
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
|
|
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(
|
|
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:
|
|
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:
|
|
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:
|
|
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 {
|
|
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:
|
|
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 = (
|
|
7
|
-
const [
|
|
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
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
|
6
|
+
import CloudStorageError from './utils/CloudStorageError';
|
|
7
7
|
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
8
|
+
export { CloudStorageError };
|
|
9
|
+
export { CloudStorageErrorCode };
|
|
10
10
|
export default RNCloudStorage;
|
package/src/types/main.ts
CHANGED
package/src/types/native.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type NativeRNCloudCloudStorageScope = 'documents' | 'app_data';
|
|
2
2
|
|
|
3
|
-
export interface
|
|
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
|
|
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:
|
|
25
|
-
createFile: (path: string, data: string, scope:
|
|
26
|
-
readFile: (path: string, scope:
|
|
27
|
-
deleteFile: (path: string, scope:
|
|
28
|
-
statFile: (path: string, scope:
|
|
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 +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;
|