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

pcloud-client

v0.1.0

Published

A JavaScript/TypeScript library for interacting with the pCloud file hosting service.

Downloads

16

Readme

PCloudClient

PCloudClient is a TypeScript library for interacting with the pCloud API. It provides a simple and intuitive interface to perform operations such as listing files and folders, uploading files, and more.

Features

  • Easy authentication with pCloud API using OAuth access tokens
  • List files and folders in a pCloud directory
  • Upload files to pCloud
  • Configurable API endpoint (US or EU)
  • Built-in error handling for API responses
  • Helper functions for OAuth authentication flow

Installation

To install PCloudClient, use npm:

npm install pcloud-client

Or if you prefer yarn:

yarn add pcloud-client

Authentication

Before using the PCloudClient, you need to obtain an OAuth access token. The library provides helper functions to assist with the OAuth flow:

1. Generate OAuth URL

Use the getOAuthUrl function to generate the URL for the user to authorize your application:

import { getOAuthUrl } from "pcloud-client";

const clientId = "your-client-id";
const redirectUri = "your-redirect-uri"; // Optional

const authUrl = getOAuthUrl(clientId, redirectUri);
console.log("Authorize the app by visiting:", authUrl);

Direct the user to this URL. After authorization, they will be redirected to your redirectUri with an authorization code.

2. Exchange Authorization Code for Access Token

Once you have the authorization code, use the getAccessToken function to exchange it for an access token:

import { getAccessToken, ApiEndpoint } from "pcloud-client";

const clientId = "your-client-id";
const clientSecret = "your-client-secret";
const authorizationCode = "code-from-redirect";

try {
  const accessToken = await getAccessToken(
    clientId,
    clientSecret,
    authorizationCode,
    ApiEndpoint.US
  );
  console.log("Access Token:", accessToken);
} catch (error) {
  console.error("Error getting access token:", error);
}

This access token can now be used to initialize the PCloudClient.

Usage

Here's a basic example of how to use PCloudClient:

import { PCloudClient, ApiEndpoint } from "pcloud-client";

// Initialize the client with the obtained access token
const client = new PCloudClient("your-access-token", ApiEndpoint.US);

// List files in a folder
const folderId = 0; // Root folder
client
  .listFolders(folderId)
  .then((response) => console.log(response))
  .catch((error) => console.error(error));

// Upload a file
const fileContent = Buffer.from("Hello, pCloud!");
client
  .uploadFile(folderId, "hello.txt", fileContent)
  .then((response) => console.log(response))
  .catch((error) => console.error(error));

API

PCloudClient

Constructor

new PCloudClient(accessToken: string, apiEndpoint?: ApiEndpoint)

Creates a new instance of PCloudClient.

  • accessToken: Your pCloud OAuth access token
  • apiEndpoint: (Optional) The API endpoint to use. Defaults to ApiEndpoint.US

Methods

listFolders
listFolders(folderId: number, options?: object): Promise<any>

Lists folders and files in a specified folder.

  • folderId: ID of the folder to list contents from
  • options: (Optional) Additional listing options
    • recursive: If true, lists contents recursively
    • showDeleted: If true, includes deleted items
    • noFiles: If true, excludes files from the listing
    • noShares: If true, excludes shared items
uploadFile
uploadFile(folderId: number, fileName: string, fileContent: Buffer, options?: {
  noPartial?: boolean;
  progressHash?: string;
  renameIfExists?: boolean;
  mtime?: number;
  ctime?: number;
}): Promise<any>

Uploads a file to a specified folder.

  • folderId: ID of the folder to upload the file to
  • fileName: Name of the file to upload
  • fileContent: Content of the file to upload (as a Buffer)
  • options: (Optional) Additional upload options
    • noPartial: If true, partially uploaded files will not be saved
    • progressHash: Hash used for observing upload progress
    • renameIfExists: If true, the uploaded file will be renamed if a file with the requested name exists in the folder
    • mtime: If set, file modified time is set (Unix timestamp in seconds)
    • ctime: If set, file created time is set. Requires mtime to be set as well (Unix timestamp in seconds)
createFolder
createFolder(folderId: number, name: string): Promise<any>

Create a folder in a specified parent folder.

  • folderId: ID of the parent folder where the new folder will be created
  • name: Name of the new folder
deleteFolderRecursive
deleteFolderRecursive(folderId: number): Promise<any>

Deletes a folder recursively.

  • folderId: ID of the folder to delete
renameFolder
renameFolder(folderId: number): Promise<any>

Renames and/or moves a folder.

  • folderId: ID of the folder to delete
  • options: (Optional) Options for renaming/moving the folder
    • toFolderId: ID of the destination folder
    • toName: New name for the folder
    • toPath: New path for the folder

Authentication Helper Functions

These functions are not part of the PCloudClient class but are provided to help with the OAuth authentication process.

getOAuthUrl

getOAuthUrl(clientId: string, redirectUri?: string): string

Generates an OAuth URL for authentication with pCloud.

  • clientId: The OAuth Client ID provided by pCloud
  • redirectUri: (Optional) The redirect URI after authentication

getAccessToken

getAccessToken(clientId: string, clientSecret: string, code: string, apiEndpoint?: ApiEndpoint): Promise<string>

Exchanges an OAuth authorization code for an OAuth access token with pCloud.

  • clientId: The OAuth Client ID provided by pCloud
  • clientSecret: The OAuth Client Secret provided by pCloud
  • code: The authorization code received after user authorization
  • apiEndpoint: (Optional) The API endpoint to use. Defaults to ApiEndpoint.US

Error Handling

PCloudClient throws a PCloudError for any API errors. You can catch and handle these errors in your code:

try {
  const result = await client.listFolders(invalidFolderId);
} catch (error) {
  if (error instanceof PCloudError) {
    console.error(`API Error: ${error.message}`);
  } else {
    console.error("An unexpected error occurred:", error);
  }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.