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

simple-canvas-interface

v0.1.3

Published

A dead simple interface for calling to canvas apis. Only handles throttling and pages.

Downloads

1

Readme

Simple Canvas Interface

I found many npm packages that purported to handle the canvas api. Unfortunately, they all had the sample problem of only being built to handle one api key whereas I wanted to handle many concurently without having to worry about rate limiting.

They also had many more features than I was looking for. I just wanted to be able to choose an endpoint, supply query string parameters, and get the response without needing to handle throttling or pagination.

When to use this module: When you want to send requests on behalf of many different students to your school's canvas given an endpoint. If you just want to use one access token or want high level functionality, there are many other other modules that do that better.

Usage:

Install the modules: npm install simple-canvas-interface

Import the module:

// .js file
const createCanvasInterface = require('simple-canvas-interface')

// .mjs file
import createCanvasInterface from 'simple-canvas-interface'

The createCanvasInterface is a function that takes the parameters:

  1. accessToken: The student's access token. This should be aquired the OAuth in production.
  2. subdomain: Your school's canvas subdomain.
  3. params: An object to set the options. They are -
    • debounceTime: If the throttle limit is reached, the request will be retried after debounceTime milliseconds. The default is 200.
    • defaultPageLength: For paginated endpoints, each request will request defaultPageLength pages. A higher number here will increase the wait time for each request, but decrease the number of calls made to get all data. The default is 100.
    • debug: If this is true a small amount of debug information will be printed.

The return value is a function that can be called with an endpoint and query string object which will send a request with throttling and pagination.

Note: The query string object will accept primative types and arrays.

Example

// Import the module
const createCanvasInterface = require('simple-canvas-interface')

const subdomain = YOUR_SUBDOMAIN

async function getActiveCourses (accessToken) {
  // We create a requester that will retry requests after 1 second and will get 10 pages in each request.
  const requester = createCanvasInterface(accessToken, subdomain, { debounceTime: 1000, defaultPageLength: 10 })
  
  // We want to request all active courses and get the names of the teachers in each course
  const courses = await requester('/api/v1/courses', {
    include: ['teachers'],
    enrollment_state: 'active'
  })
  // This will format the endpoint as /api/v1/courses?include[]=teachers&per_page=10 and then concatenate all pages while avoiding throttling.
  
  return courses
}

getActiveCourses(YOUR_ACCESS_TOKEN)
  .then(courses => {
    console.log('Courses', courses)
  })