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

canvas-api-ts

v0.6.1

Published

Strongly typed canvas lms api

Downloads

25

Readme

Type driven canvas lms api

A typed api wrapper for canvas lms api.

There is an set of low level interface provides full control over the canvas restful endpoints, and a high level interface that makes things easier.

  • This api is in it's early age, more apis and tests will be added later one. It will be stable when it reaches 1.0.0.

The most frustrated part of using apis is probably being not sure about what parameter to send and what kind of value the api will response. It can sometimes take hours of trails and errors just to understand what an api does.

This api wrapper is aimed to solve the problem. Your api request and response are backed by the type system and ensure you easier api using experience and type safty.

Config

To use the api you first need canvas token. You can find out about how to get one from here

Once you get the token, create a new file .env in your projecet directory, and add following content:

CANVAS_API_TOKEN=11824~7jh4ElSYbEdsdlajlsdladTHfyzHMb6xusdahsk0N3HL7xAokxQ9mYC
CANVAS_API_URL=https://canvas.ubc.ca

CANVAS_API_URL is the domain name of your institution with protocol in the front, CANVAS_API_TOKEN is the token you get from canvas website.

If you want to upload your project somewhere MAKE SURE the .env file is not also shared. The api token basically gives anyone all the credential of your account.

Example

High level interface

High level interface can be used easily. Functionalities are separated into parts to enable more customizations.

import {File} from 'canvas-api-ts';


// fille wil be download at the dir
const main = async () => {
  // download the first file

  // getFiles return the representation of file in canvas server.
  // the actual file is stored in a different server and need to be
  // fetched separately.

  const folders = await File.getUserFolders("self");
  const COSC221 = folders.filter(e => e.name.startsWith("COSC 211"))[0];

  // pull out a list of promises
  const filePromises = await File.fetchAllFromAFolder(COSC221, {});

  // Promise.all the list of promises to avoid block.
  const files = await Promise.all(filePromises);

  // Here is essentially synchronous code.
  for (const file of files) {
    // write file stream to the directory.
    await File.store('./directory', file);
  }
};

main();

Low level interface

Each endpoint is reprensented as a type. call canvas with the corresponding type as it's type parameter to send a request. By default your token will be added into the request header. There is also

import {FilesAPI, canvas} from 'canvas-api-ts';
async function foo() {
  const result = await canvas<FilesAPI.Quota.GetUserQuota>({
    uri: "/api/v1/users/:user_id/files/quota",
    uriParams: {user_id: "self"},
    method: "GET",
    param: null,
    extraHeaders: {...}
  });
  console.log(result.quota_used);
}

If you are using tsserver, most values in the parameters are super narrowed types and can be autocompleted directly. Request parameter param and response type are also typed.

Convention

API is the family of all API types, *API is the concrete type represents a canvas api end point. e.g AccountAPI.

Currently Supported APIs

  • Account
  • Assignment
  • Progress
  • File
  • File Upload
  • Announcement
  • Conversation
  • Course
  • User

Note

The best document is probably the type definition itself. Types in API namespace are structured follow the canvas lms api document, more information can be found there.

License

MIT

Be really careful about not sharing your token.