New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@robinbobin/react-native-google-drive-api-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@robinbobin/react-native-google-drive-api-wrapper

This wrapper facilitates the use of the Google Drive API in React Native projects.

  • 2.2.0
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.1K
increased by11.23%
Maintainers
1
Weekly downloads
 
Created
Source

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

  1. Installation
  2. Usage
    1. GDrive
    2. Uploaders
    3. Other entities

Installation

npm i --save @robinbobin/react-native-google-drive-api-wrapper

Usage

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));

GDrive


Common notes:

  • queryParameters and requestBody are described in the official documentation of the corresponding methods.

  1. About
  2. Files
  3. GDrive
  4. Permissions

About

This class gives information about the user, the user's Drive, and system capabilities.

NameTypeDescription
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.

Files

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.
    
NameTypeDescription
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 MetadataOnlyUploaderCreates a class instance to handle a metadata-only upload.
newMultipartUploader()Method, returns MultipartUploaderCreates a class instance to handle a multipart upload.
newResumableUploader()Method, returns ResumableUploaderCreates a class instance to handle a resumable upload.
newSimpleUploader()Method, returns SimpleUploaderCreates a class instance to handle a simple upload.

GDrive

A GDrive instance stores various api access parameters and the instances of the classes that wrap individual parts of the google drive api.

NameTypeDescription
aboutProperty, read/write
About instance
The instance to get this information.
accessTokenProperty, 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.
filesProperty, read/write
Files instance
The instance to manage files in a google drive.
fetchTimeoutProperty, read/write
number
A timeout in milliseconds for fetch() invocations. The default value is 1500. If the value is negative, fetch() will wait infinitely.
permissionsProperty, read/write
Permissions instance
The instance to manage file permissions.

Permissions

This class handles file permissions.

NameTypeDescription
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.

Uploaders

  1. MetadataOnlyUploader
  2. MultipartUploader
  3. ResumableUploader
  4. SimpleUploader
  5. Uploader

MetadataOnlyUploader

An Uploader descendant, this class handles metadata-only uploads. It doesn't have own methods or properties.

MultipartUploader

An Uploader descendant, this class handles multipart uploads.

NameTypeDescription
setIsBase64(
  isBase64: boolean
)
Method, returns thisIf 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 thisSets the boundary string to be used for this upload. The default value is foo_bar_baz.

ResumableUploader

An Uploader descendant, this class handles resumable uploads.

NameTypeDescription
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 thisThis method must be invoked to set the content length.
setMimeType(
  mimeType:  string
)
Method, returns thisSets the data MIME type when using multiple requests.
setShouldUseMultipleRequests(
  shouldUseMultipleRequests: boolean
)
Method, returns thisSpecifies whether multiple requests will be used to upload the data.
transferredByteCountRead property (Number)The current transferred byte count.
uploadChunk(chunk: DataType)MethodUploads a chunk of data, returning UploadChunkResultType, wrapped in a Promise.

SimpleUploader

An Uploader descendant, this class handles simple uploads. It doesn't have own methods or properties.

Uploader

Descendants of this class handle create and update requests.

NameTypeDescription
execute()Method, returns Promise<File resource>Executes the request.
setData(
  data:  Data,
  mimeType:  string
)
Method, returns thisSets the data and its MIME type.
setIdOfFileToUpdate(
  fileId: string
)
Method, returns thisIf 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 thisSets the query parameters (create, update).
setRequestBody(
  requestBody: object
)
Method, returns thisSets the request body (create, update).

Other entities

  1. CreateIfNotExistsResultType
  2. Data
  3. HttpError
  4. ListQueryBuilder
  5. MimeType
  6. RequestUploadStatusResultType
  7. UnexpectedFileCountError
  8. UploadChunkResultType

CreateIfNotExistsResultType

This interface describes the result type of Files.createIfNotExists().

NameTypeDescription
alreadyExistedbooleantrue if the file already existed before the method invocation, false otherwise.
resultobjectA file resource, describing the existing file, if alreadyExisted is true.
The result of invoking Uploader.execute() if alreadyExisted is false.

Data

Uint8Array | number[] | string

HttpError

An instance of this class is thrown when an api call fails.

NameTypeDescription
jsonProperty, read
object
An object describing the error. Can be undefined.
messageProperty, read
string
The original response text.
responseProperty, read
object
The result of fetch().

ListQueryBuilder

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")
});
NameTypeDescription
and()Method, returns thisands two subqueries.
contains(
  key: Key,
  value: Value,
  quoteValueIfString = true
)
Method, returns thiskey contains value
e(
  key: Key,
  value: Value,
  quoteValueIfString = true
)
Method, returns thiskey = value
g(
  key: Key,
  value: Value,
  quoteValueIfString = true
)
Method, returns thiskey > value
in(
  value: Value,
  key: Key,
  quoteValueIfString = true
)
Method, returns thisvalue in key
l(
  key: Key,
  value: Value,
  quoteValueIfString = true
)
Method, returns thiskey < value
operator(
  left: KeyOrValue,
  operator: string,
  right: KeyOrValue,
  quoteLeftIfString: boolean,
  quoteRightIfString: boolean
)
Method, returns thisA generic method to build all the other key/value relations.
or()Method, returns thisors two subqueries.
pop()Method, returns thisAdds ).
push()Method, returns thisAdds (.
toString()Method, returns stringStringifies the query.

MimeType

This enum holds commonly used MIME types.

NameType
BINARYapplication/octet-stream
CSVtext/csv
FOLDERapplication/vnd.google-apps.folder
JSONapplication/json
JSON_UTF8application/json; charset=UTF-8
PDFapplication/pdf
TEXTtext/plain

RequestUploadStatusResultType

This interface describes the result type of ResumableUploader.requestUploadStatus().

NameTypeDescription
isCompletebooleantrue if the upload is completed, false otherwise.
transferredByteCountnumberThe number of bytes currently transferred.

UnexpectedFileCountError

An instance of this class is thrown when the real number of files differs from the expected.

NameTypeDescription
expectedCountProperty, read
number[] | number
The expected count.
realCountProperty, read
number
Real count.

UploadChunkResultType

Extending RequestUploadStatusResultType, this interface describes the result type of ResumableUploader.uploadChunk().

NameTypeDescription
jsonany?This field will contain a file resource, describing the file, if isComplete is true.

Keywords

FAQs

Package last updated on 14 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc