@kth/canvas-api
Functions for communicating with Canvas LMS
Last updated 2 months ago by kth-ci .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @kth/canvas-api 
SYNC missed versions from official npm registry.

Canvas API client

Node.JS HTTP client based on got for the Canvas LMS API

Build Status

  1. Install
  2. Usage
  3. API reference

Install

npm i @kth/canvas-api

Usage

const CanvasApi = require('@kth/canvas-api')
// or, with ES modules:
// import CanvasApi from '@kth/canvas-api/esm'

async function start() {
  const canvas = CanvasApi('https://kth.instructure.com/api/v1', 'XXXX~xxxx')
  const { body } = await canvas.get('/accounts/1')
}

start()

Examples

Create a course

const CanvasApi = require('./index')
const canvas = CanvasApi('https://kth.instructure.com/api/v1', 'XXXX~xxxx')

async function start() {
  const { body } = await canvas.requestUrl('/accounts/1/courses', 'POST')
  console.log(`Created! https://kth.test.instructure.com/courses/${body.id}`)
}
start()

See the full API here

Array vs iterable

Array example. Get a list of integration IDs of sections within a course

It is easier to use an array if you want to use JavaScript array methods (map, filter, etc.), when you want to retrieve the entire collection or when you know that the collection has a small size.

const CanvasApi = require('@kth/canvas-api')

const courseId = 'XXXX'
const canvas = CanvasApi('https://kth.instructure.com/api/v1', 'XXXX~xxxx')

async function start() {
  const sections = (await canvas.list(`/courses/${courseId}/sections`).toArray())
    .map(section => section.integration_id)

  console.log(sections)
}
start()

Iterable example. Get 5 courses that contain the word "sustain" in their name

It is better to use an iterable if you don't want to fetch all the resources in a collection (in this case we are interested in 5 courses that...)

const CanvasApi = require('@kth/canvas-api')
const canvas = CanvasApi('https://kth.instructure.com/api/v1', 'XXXX~xxxx')

function isSustainable (course) {
  return course.name.toLowerCase().indexOf('sustain') !== -1
}

async function start() {
  const courses = canvas.list('/accounts/1/courses')
  const result = []

  for await (const course of courses) {
    if (isSustainable(course)) {
      result.push(course.name)
    }

    if (result.length >= 5) {
      break
    }
  }

  console.log(result)
}
start()

Current Tags

  • 2.1.1                                ...           latest (2 months ago)

12 Versions

  • 2.1.1                                ...           2 months ago
  • 2.1.0                                ...           9 months ago
  • 2.1.0-beta.2                                ...           a year ago
  • 2.1.0-beta.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.1.1 [deprecated]           ...           a year ago
  • 1.1.0 [deprecated]           ...           a year ago
  • 1.0.3 [deprecated]           ...           a year ago
  • 1.0.2 [deprecated]           ...           a year ago
  • 1.0.2-0                                ...           a year ago
  • 1.0.1 [deprecated]           ...           a year ago
  • 1.0.0 [deprecated]           ...           a year ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 12
Last Month 12
Dependencies (5)
Dev Dependencies (5)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |