npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

drive-uploady

v0.3.0

Published

wrapper for react-uploady to upload to google-drive

Downloads

30

Readme

Drive Uploady

Provides a custom React Uploady provider for uploading to Google Drive. All Uploady functionality such as hooks and components (ex: Upload-Preview) can be used with this package.

Uploads are sent to the multipart endpoint: Google Drive docs.

Note: no support for URL based uploads, only files.

Installation

#Yarn:  
   $ yarn add drive-uploady @rpldy/uploady

#NPM:
   $ npm i drive-uploady @rpldy/uploady

Props

| Name (* = mandatory) | Type | Default | Description | |-----------------------------------------|-----------------------------------|--------------------|-----------------------------------------------------------------------------------------------------------| | clientId* (unless getToken is provided) | string | | The API client Id. Obtained from the Google dev console |
| scopes* (unless getToken is provided) | string | | The scopes your app requires (Drive docs) | | gApiScriptIdPrefix | string | "uploady-drive-" | The id of the script tag (loading google api) that will be added to the page | | getToken | GetTokenMethod | | provide a function that will provide the (access) token | | queryParams | Object | | Optional query parameters |

All other Uploady props can be passed as well. See docs here.

Note: no support for concurrent > 1

Example

import React from "react";
import DriveUploady from "drive-uploady";
import UploadButton from "@rpldy/upload-button";

export const App = () => {
  return (
    <DriveUploady
      clientId="<my-client-id>"
      scope="https://www.googleapis.com/auth/drive.file"
    >
      <h2>Drive Uploady</h2>

      <UploadButton>Upload to Drive</UploadButton>
    </DriveUploady>
  );
};

Upload to folder

import React from "react";
import DriveUploady from "drive-uploady";
import UploadButton from "@rpldy/upload-button";

export const App = () => {
  return (
    <DriveUploady
      clientId="<my-client-id>"
      scope="https://www.googleapis.com/auth/drive.file"
      params={{ parents: ["folder-id"] }}
    >
      <h2>Drive Uploady</h2>

      <UploadButton>Upload to Drive</UploadButton>
    </DriveUploady>
  );
};

Authentication

By default, Drive-Uploady will load and use its own Google Authentication Provider. The process involves loading the scripts from Google:

  1. https://apis.google.com/js/api.js
  2. https://accounts.google.com/gsi/client

Note: Using two scripts is due to Google's deprecation decision.

Once the scripts are loaded. Internally, a TokenClient will be created (google.accounts.oauth2.initTokenClient) and will be used in order to retrieve an access token for the user.

The user will be shown a pop-up through which they can sign-in (if not already) and approve the application to access their Drive.

The application (created in the API Console) must have the right scope (ex: https://www.googleapis.com/auth/drive.file).

As long as the page isn't refreshed and the token is still valid, the user will not be prompted to approve again when uploading additional files. In case the token is expired, the user will be prompted again.

Own Authentication

In case you are already implementing your own use of the oauth flow with Google. Drive-Uploady let's you pass in a getToken method as a prop that will be used to retrieve the access token when needed.

All scripts and authentication will be assumed to have been loaded and set up separately from Drive-Uploady.

GetTokenMethod

export type AuthToken = {
  access_token: string;
  expires_in: number;
};

export type GetTokenMethod = (cb: (token: AuthToken) => void) => void;

Example use of getToken() implementation:

import React from "react";
import DriveUploady from "drive-uploady";
import UploadButton from "@rpldy/upload-button";

const getToken = (cb) => {
  const tokenClient = window.google.accounts.oauth2.initTokenClient({
    client_id: "MY-CLIENT_ID",
    scope: "MY-SCOPE",
    callback: (response) => {
      cb(response);
    },
  });

  tokenClient.requestAccessToken({ prompt: "consent" });
};

export const App = () => {
  return (
    <DriveUploady
      getToken={getToken}
    >
      <h2>Drive Uploady</h2>

      <UploadButton>Upload to Drive</UploadButton>
    </DriveUploady>
  );
};

Revoke Token

The library provides a utility function to revoke the token previously retrieved on the page:

import { revokeToken } from "drive-uploady";

const RevokeButton = () => {
	return (
    <button onClick={revokeToken}>Revoke Token</button>
  );
};