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

@canopy-pte-ltd/canopy-toolkit

v1.1.19

Published

Canopy Toolkit for Javascript

Downloads

45

Readme

Canopy Toolkit for Javascript

Usage

Installation

yarn add @canopy-pte-ltd/canopy-toolkit

# or 

npm install @canopy-pte-ltd/canopy-toolkit

Client Options

The following options are supported when creating or restoring a client.

Note: Use the same set of options for both creating and restoring a client.

export type ClientOptions = {
  // The API Server to connect to
  apiServer: string;

  // The WebSocket server to connect to (Optional)
  websocketServer?: string;

  // Should the Websocket Proxy be disabled (Default: false)
  disableWebsocketProxy?: boolean;

  // The API Version to use (Default: v1)
  apiVersion?: string;

  // The App ID to set in the requests (Default: Canopy JS SDK)
  appId?: string;

  // The locale the responses should be in (Default: en)
  locale?: string;

  // Enable logging to the console (Default: false)
  enableLogging?: boolean;

  // Persist the login session to localStorage (Default: false)
  persistSession?: boolean;

  // The localStorage key to persist under (Default: canopy-session)
  persistSessionKey?: string

  // HTTP Proxy server (Works only inside Node.js - requires https-proxy-agent)
  httpProxyServer?: string;

  // Log websocket response time metrics (Default: false)
  logWebsocketMetrics?: boolean;

  // Callback to invoke when token is refreshed (Optional)
  onTokenRefresh?: () => void;

  // Callback to invoke when a user is logged in (Optional)
  onUserLogin?: () => void;

  // Callback to invoke when user is logged out (Optional)
  onUserLogout?: (previousSession: SessionResponse) => void;

  // Callback to invoke when the user's session times out. (Optional)
  onSessionTimeout?: (previousSession: SessionResponse) => void;
};

Authentication

Username + password login
import { CanopyClient } from '@canopy-pte-ltd/canopy-toolkit';

// Create the client
// - The client can be created in a common place and shared across the app.
const client = new CanopyClient({
  apiServer      : 'https://dev-api-gateway.kurtosys.org',
  persistSession : true
});

// Login with username & password
await client.login(username, password);
2FA code required
import { TwoFactorCodeRequired } from '@canopy-pte-ltd/canopy-toolkit';
  • client.login would raise a TwoFactorCodeRequired exception.

  • Get the MFA code from the user and call client.responseToLoginChallenge

    await client.respondToLoginChallenge(123587);
2FA activation required
import { TwoFactorActivationRequired } from '@canopy-pte-ltd/canopy-toolkit';
  • client.login would raise a TwoFactorActivationRequired exception.

  • The exception would contain a response parameter with the 2FA secret key using with a QR Code is to be generated and displayed.

  • Request the user to scan the QR code into their authentication application

  • Get the MFA code from the user and call client.responseToLoginChallenge

    await client.respondToLoginChallenge(123587);
Password reset is required

This login challenge is currently unsupported by this library

import { PasswordResetRequired } from '@canopy-pte-ltd/canopy-toolkit';
  • client.login would raise a PasswordResetRequired exception.
  • Request the user to login in Visualizer to reset the password.
Restore a session

On page-refresh, the existing user session if any can be restored into the client as long as the session is valid.

import { CanopyClient } from '@canopy-pte-ltd/canopy-toolkit';

const preAuthenticatedClient = CanopyClient.restoreSession({
  apiServer      : 'https://dev-api-gateway.kurtosys.org',
  persistSession : true
});

If a valid session is not available to restore from, one of the following exceptions will be thrown. In that case, create a fresh client and log the user in using the login methods documented above.

  • SessionNotFoundError - a session could not be found
  • SessionRestoreError - a session was found but an error occurred while trying to restore it
  • SessionExpiredError - a session was found, but it was expired.

Making requests

Request Options:
export type RequestOptions = {
  // The javascript object to send as json (Does not apply to GET)
  data?: any;
  
  // Should the request be made via websocket (default: false)
  useWebsocket?: boolean;
  
  // Should the accounts information be skipped from the query parameters (default: false)
  withoutAccounts?: boolean;
  
  // Should the namespace prefix (eg. /api/v1) be skipped (default: false) 
  withoutNamespace?: boolean;
  
  // Override the auth token to use
  authToken?: string | boolean;
  
  // List of user accounts to make the request for (default: none)
  accounts?: (number | string)[] | null;
  
  // Additional headers to set
  headers?: KeyValueParams;
  
  // Should the headers set here replace the default headers added to all requests
  replaceHeaders?: boolean;
  
  // Call the api as a specific user ID
  asUser?: number | string;
  
  // Query parameters to set
  queryParams?: KeyValueParams;
  
  // API Version to use (eg. v1) 
  apiVersion?: string;
};
Methods

All methods support the request options documented above as the last parameter.

// GET /api/v1/user_details
const response = await client.get<ResponseType>('user_details');

// POST /api/v1/user_details
const response = await client.post<ResponseType>('user_details', {
  someData: 'xyzzy'
});

// PUT /api/v1/user_details
const response = await client.put<ResponseType>('user_details', {
  someData: 'xyzzy'
});

// PATCH /api/v1/user_details
const response = await client.patch<ResponseType>('user_details', {
  someData: 'xyzzy'
});

// DELETE /api/v1/user_details
const response = await client.delete<ResponseType>('user_details');

Logging out

await client.logout();