This wrapper facilitates the use of the google drive api.
It doesn't provide any authorization mechanism, so another package has to be used. I use @react-native-google-signin/google-signin (thanks for the great work, vonovak!).
Table of contents
- Installation
- Usage
- GDrive
- Uploaders
- Other entities
npm i --save @robinbobin/react-native-google-drive-api-wrapper
If something doesn't work as expected, please do have a look at an example project before opening an issue.
Quick example:
// = List files, create a binary file and read it = //
import { GoogleSignin } from "@react-native-google-signin/google-signin";
import {
GDrive,
MimeType
} from "@robinbobin/react-native-google-drive-api-wrapper";
// = Somewhere in your code = //
GoogleSignin.configure(...);
await GoogleSignin.signIn();
const gdrive = new GDrive();
gdrive.accessToken = (await GoogleSignin.getTokens()).accessToken;
console.log(await gdrive.files.list());
const id = (await gdrive.files.newMultipartUploader()
.setData([1, 2, 3, 4, 5], MimeType.BINARY)
.setRequestBody({
name: "multipart_bin"
})
.execute()
).id;
console.log(await gdrive.files.getBinary(id));
Common notes:
queryParameters
and requestBody
are described in the official documentation of the corresponding methods.
- About
- Files
- GDrive
- Permissions
This class gives information about the user, the user's Drive, and system capabilities.
Name | Type | Description |
---|
get(queryParametersOrFields) | Method, returns Promise< About resource> | Gets information about the user, the user's Drive, and system capabilities.
queryParametersOrFields can be an object containing the query parameters or a string, containing a fields value. |
This class is used to manage files in a google drive.
Notes:
-
The parameter range
for the methods that accept it is specified as here with one exception:
<unit> is always bytes and mustn't be set. E.g.:
await gdrive.files.getBinary(fileId, null, "1-1");
will return the byte at index one.
Name | Type | Description |
---|
copy( fileId: string, queryParameters?: object, requestBody: object = {}
) | Method, returns Promise< File resource> | Creates a copy of a file. |
createIfNotExists( queryParameters: object, uploader: Uploader
) | Method, returns Promise< CreateIfNotExistsResultType > | Invokes uploader.execute() , if the file described with queryParameters doesn't exist. Throws UnexpectedFileCountError if there are 2 or more files matching queryParameters . |
delete(fileId: string) | Method, returns Promise<void> | Deletes a file. |
emptyTrash() | Method, returns Promise<void> | Permanently deletes all of the user's trashed files. |
export( fileId: string, queryParameters: object
) | Method, returns Promise< File resource> | Exports a Google Doc to the requested MIME type. |
generateIds( queryParameters?: object
) | Method, returns Promise< object > | Generates file IDs. This info might seem interesting. |
get( fileId: string, queryParameters?: object, range?: string
) | Method, returns Promise< Response > | Gets the file's metadata or content. |
getBinary( fileId: string, queryParameters?: object, range?: string
) | Method, returns Promise< Uint8Array > | Gets the content of a binary file. |
getContent( fileId: string, queryParameters?: object, range?: string
) | Method, returns Promise< Response > | Gets the content of any file. |
getJson( fileId: string, queryParameters?: object
) | Method, returns Promise<object> | Gets the content of a json text file. |
getMetadata( fileId: string, queryParameters?: object
) | Method, returns Promise< File resource> | Gets a file's metadata. |
getText( fileId: string, queryParameters?: object, range?: string
) | Method, returns Promise<string> | Gets the content of a text file. |
list( queryParameters?: object
) | Method, returns Promise< object > | Lists files.
queryParameters.q can be a query string or a ListQueryBuilder instance. |
newMetadataOnlyUploader() | Method, returns MetadataOnlyUploader | Creates a class instance to handle a metadata-only upload. |
newMultipartUploader() | Method, returns MultipartUploader | Creates a class instance to handle a multipart upload. |
newResumableUploader() | Method, returns ResumableUploader | Creates a class instance to handle a resumable upload. |
newSimpleUploader() | Method, returns SimpleUploader | Creates a class instance to handle a simple upload. |
A GDrive
instance stores various api access parameters and the instances of the classes that wrap individual parts of the google drive api.
Name | Type | Description |
---|
about | Property, read/write
About instance | The instance to get this information. |
accessToken | Property, read/write
access token | An access token to be used in subsequent calls to the apis. Get the token from a package you choose to use. |
files | Property, read/write
Files instance | The instance to manage files in a google drive. |
fetchTimeout | Property, read/write
number | A timeout in milliseconds for fetch() invocations. The default value is 1500 . If the value is negative, fetch() will wait infinitely. |
permissions | Property, read/write
Permissions instance | The instance to manage file permissions. |
This class handles file permissions.
Name | Type | Description |
---|
create( fileId: string, requestBody: object, queryParameters?: object
) | Method, returns Promise< Permissions resource> | Creates a permission. |
delete( fileId: string, permissionId: string, queryParameters?: object
) | Method, returns Promise<void> | Deletes a permission. |
- MetadataOnlyUploader
- MultipartUploader
- ResumableUploader
- SimpleUploader
- Uploader
An Uploader descendant, this class handles metadata-only
uploads. It doesn't have own methods or properties.
An Uploader descendant, this class handles multipart
uploads.
Name | Type | Description |
---|
setIsBase64( isBase64: boolean
) | Method, returns this | If the data set with setData() is in Base64, invoke this method to add the header Content-Transfer-Encoding: base64 which is recognized by Google Drive. |
setMultipartBoundary( multipartBoundary: string
) | Method, returns this | Sets the boundary string to be used for this upload. The default value is foo_bar_baz . |
An Uploader descendant, this class handles resumable
uploads.
Name | Type | Description |
---|
execute() | Method. Returns Promise< UploadChunkResultType > if data was set. Returns Promise<this> otherwise. | This method sends the initial upload request. |
requestUploadStatus() | Method, returns Promise< RequestUploadStatusResultType > | Returns the current upload status. |
setContentLength( contentLength: number
) | Method, returns this | This method must be invoked to set the content length. |
setMimeType( mimeType: string
) | Method, returns this | Sets the data MIME type when using multiple requests. |
setShouldUseMultipleRequests( shouldUseMultipleRequests: boolean
) | Method, returns this | Specifies whether multiple requests will be used to upload the data. |
transferredByteCount | Read property (Number) | The current transferred byte count. |
uploadChunk(chunk: DataType) | Method | Uploads a chunk of data, returning UploadChunkResultType , wrapped in a Promise . |
An Uploader descendant, this class handles simple
uploads. It doesn't have own methods or properties.
Descendants of this class handle create and update requests.
Name | Type | Description |
---|
execute() | Method, returns Promise< File resource> | Executes the request. |
setData( data: Data , mimeType: string
) | Method, returns this | Sets the data and its MIME type. |
setIdOfFileToUpdate( fileId: string
) | Method, returns this | If this method is invoked and fileId is a string, the request becomes an update request. Otherwise it's a creation request. |
setQueryParameters( queryParameters: object
) | Method, returns this | Sets the query parameters (create, update). |
setRequestBody( requestBody: object
) | Method, returns this | Sets the request body (create, update). |
- CreateIfNotExistsResultType
- Data
- HttpError
- ListQueryBuilder
- MimeType
- RequestUploadStatusResultType
- UnexpectedFileCountError
- UploadChunkResultType
This interface describes the result type of Files.createIfNotExists()
.
Name | Type | Description |
---|
alreadyExisted | boolean | true if the file already existed before the method invocation, false otherwise. |
result | object | A file resource, describing the existing file, if alreadyExisted is true . The result of invoking Uploader.execute() if alreadyExisted is false . |
Uint8Array | number[] | string
An instance of this class is thrown when an api call fails.
Name | Type | Description |
---|
json | Property, read
object | An object describing the error. Can be undefined . |
message | Property, read
string | The original response text. |
response | Property, read
object | The result of fetch() . |
A helper class for building Files.list()
queries. It uses the following type declarations:
type Key = string
type Value = boolean | number | string
type KeyOrValue = Key | Value
Example:
// = List files contained in the root folder and named "Untitled" = //
const folderIdNotItsName = "root";
await gdrive.files.list({
q: new ListQueryBuilder()
.e("name", "Untitled")
.and()
.in(folderIdNotItsName, "parents")
});
Name | Type | Description |
---|
and() | Method, returns this | and s two subqueries. |
contains( key: Key, value: Value, quoteValueIfString = true
) | Method, returns this | key contains value |
e( key: Key, value: Value, quoteValueIfString = true
) | Method, returns this | key = value |
g( key: Key, value: Value, quoteValueIfString = true
) | Method, returns this | key > value |
in( value: Value, key: Key, quoteValueIfString = true
) | Method, returns this | value in key |
l( key: Key, value: Value, quoteValueIfString = true
) | Method, returns this | key < value |
operator( left: KeyOrValue, operator: string, right: KeyOrValue, quoteLeftIfString: boolean, quoteRightIfString: boolean
) | Method, returns this | A generic method to build all the other key/value relations. |
or() | Method, returns this | or s two subqueries. |
pop() | Method, returns this | Adds ) . |
push() | Method, returns this | Adds ( . |
toString() | Method, returns string | Stringifies the query. |
This enum holds commonly used MIME types.
Name | Type |
---|
BINARY | application/octet-stream |
CSV | text/csv |
FOLDER | application/vnd.google-apps.folder |
JSON | application/json |
JSON_UTF8 | application/json; charset=UTF-8 |
PDF | application/pdf |
TEXT | text/plain |
This interface describes the result type of ResumableUploader.requestUploadStatus().
Name | Type | Description |
---|
isComplete | boolean | true if the upload is completed, false otherwise. |
transferredByteCount | number | The number of bytes currently transferred. |
An instance of this class is thrown when the real number of files differs from the expected.
Name | Type | Description |
---|
expectedCount | Property, read
number[] | number | The expected count. |
realCount | Property, read
number | Real count. |
Extending RequestUploadStatusResultType, this interface describes the result type of ResumableUploader.uploadChunk().
Name | Type | Description |
---|
json | any? | This field will contain a file resource, describing the file, if isComplete is true . |