Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export types for schoology, canvas and google workspace #12

Open
wants to merge 4 commits into
base: prod
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
"words": true
}
],
"spaced-comment": [2, "always"],
"spaced-comment": [0, "always"],
"use-isnan": 2,
"valid-typeof": 2,
"vars-on-top": 2,
Expand Down
142 changes: 80 additions & 62 deletions src/canvas.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { AxiosResponse } from 'axios';
import {RequestConfig} from './types';
import OAuth from './oauth2';

export = Canvas;

interface Tokens {
accessToken: string;
refreshToken: string;
Expand All @@ -19,33 +17,19 @@ interface AuthURLOptions {
scopes: string[];
}

interface CanvasProfile {
id: string;
name: string;
primary_email: string;
locale: string;
}

declare enum SubmissionStates {
SUBMITTED = 'submitted',
GRADED = 'graded',
UNSUBMITTED = 'unsubmitted',
}
export = Canvas;
declare class Canvas {
constructor(options: {
orgName: string;
hostedUrl: string;
redirectUri: string;
accessToken: string;
refreshToken: string;
clientId: string;
clientSecret: string;
fxs: { getUserToken: GetUserToken, setUserToken: SetUserToken };
fxs: { getToken: GetUserToken, setToken: SetUserToken };
userId: string;
canvasUserId?: string;
});

orgName: string;
hostedUrl: string;
redirectUri: string;
accessToken: string;
Expand All @@ -57,60 +41,94 @@ declare class Canvas {
userId: string;
canvasUserId: string;

static SUBMISSION_STATE: SubmissionStates;
static SUBMISSION_STATE: Canvas.SubmissionStates;

build(): Promise<Canvas>;
getAuthorizationURL(options: AuthURLOptions): string;
getTokensFromCode(code: string): Promise<OAuth.PostResponse>;
handleError(err: Error, code: string, redirectUrl: string): void;
isTokenExpired(err: Error): boolean;
makeRequest(requestConfig: RequestConfig, retries: number): Promise<AxiosResponse>;
getProfile(): Promise<CanvasProfile>;
getProfile(): Promise<Canvas.Profile>;
getUserProfile(id: number): Promise<Canvas.Profile>;
getTokensFromUser(): Promise<void>;

getCourses(): Promise<Course[]>;
getAccounts(): Promise<Canvas.Account[]>;
getAccountUsers(id: number, data?: { enrollment_type: string[] }): Promise<Canvas.User[]>;

getCourses(): Promise<Canvas.Course[]>;
announce(args: { courseId: string; pinned?: boolean; title: string; message: string }): Promise<void>;
listStudents(args: { courseId: string }): Promise<Student[]>;
createAssignment(args: { courseId: string; assignmentName: string; assignmentDescription?: string; dueAt?: Date; unlockAt?: Date; }): Promise<Assignment>;
submitAssignment(args: { courseId: string; assignmentId: string; submissionUrl: string }): Promise<Submission>;
getSubmission(args: { courseId: string; assignmentId: string; studentCanvasId: string }): Promise<Submission>;
listSubmissions(args: {courseId: string; assignmentId: string}): Promise<Submission[]>;
gradeSubmission(args: { courseId: string; assignmentId: string; studentCanvasId: string; grade: number | string; comment?: string }): Promise<GradeSubmissionResponse>;
listStudents(args: { courseId: string }): Promise<Canvas.Student[]>;
createAssignment(args: { courseId: string; assignmentName: string; assignmentDescription?: string; dueAt?: Date; unlockAt?: Date; }): Promise<Canvas.Assignment>;
submitAssignment(args: { courseId: string; assignmentId: string; submissionUrl: string }): Promise<Canvas.Submission>;
getSubmission(args: { courseId: string; assignmentId: string; studentCanvasId: string }): Promise<Canvas.Submission>;
listSubmissions(args: {courseId: string; assignmentId: string}): Promise<Canvas.Submission[]>;
gradeSubmission(args: { courseId: string; assignmentId: string; studentCanvasId: string; grade: number | string; comment?: string }): Promise<Canvas.GradeSubmissionResponse>;
gradeMultipleSubmissions(args: { courseId: string; assignmentId: string; userGradesAndComments: {[studentCanvasId: string]: {grade: number | string, comment?: string } } }): Promise<{id: number; url: string}>;
}

interface GradeSubmissionResponse extends Submission {
all_submissions: Submission[];
}
interface Course {
id: number;
name: string;
}

interface Student {
id: number;
name: string;
email: string;
}

interface Assignment {
id: number;
description: string;
due_at?: Date;
unlock_at?: Date;
published: boolean;
}

interface Submission {
id: number;
url: string;
body?: string;
grade: string;
score: number; // float
user_id: string;
assignment_id: number;
submission_type: 'online_url';
graded_at: Date;
attempt: number;
workflow_state: 'submitted' | 'graded' | 'unsubmitted';
}
declare namespace Canvas {
interface GradeSubmissionResponse extends Submission {
all_submissions: Submission[];
}
interface Course {
id: number;
name: string;
}

export interface Account {
id: number,
name: string,
uuid: string,
}

export interface User {
id: number,
name: string,
first_name: string,
last_name: string,
login_id: string,
email?: string,
}

interface Student {
id: number;
name: string;
email: string;
}

interface Assignment {
id: number;
description: string;
due_at?: Date;
unlock_at?: Date;
published: boolean;
}

interface Submission {
id: number;
url: string;
body?: string;
grade: string;
score: number; // float
user_id: string;
assignment_id: number;
submission_type: 'online_url';
graded_at: Date;
attempt: number;
workflow_state: 'submitted' | 'graded' | 'unsubmitted';
}

export interface Profile {
id: string;
name: string;
primary_email: string;
locale: string;
}

enum SubmissionStates {
SUBMITTED = 'submitted',
GRADED = 'graded',
UNSUBMITTED = 'unsubmitted',
}
}
66 changes: 28 additions & 38 deletions src/canvas.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='./canvas.d.ts' />


const axios = require('axios');
const _ = require('lodash');
const OAuth = require('./oauth2');
Expand All @@ -11,7 +12,6 @@ const { paginatedCollect } = require('./helpers/utils');
*/
class Canvas {
constructor({
orgName,
hostedUrl,
redirectUri,
accessToken,
Expand All @@ -22,7 +22,6 @@ class Canvas {
userId, // mongoId
canvasUserId,
}) {
this.orgName = orgName;
this.hostedUrl = hostedUrl;
this.redirectUri = redirectUri;
this.accessToken = accessToken;
Expand Down Expand Up @@ -75,7 +74,7 @@ class Canvas {
}),
headers: {
'Content-Type': 'application/json',
}
},
});

this.accessToken = resp.data.access_token;
Expand All @@ -97,9 +96,9 @@ class Canvas {
});
this.canvasUserId = resp.data.id;
return resp.data;
} catch(err) {
} catch (err) {
throw new LMSError('Unable to fetch user profile', 'canvas.USER_PROFILE_ERROR', {
userId: this.userId
userId: this.userId,
});
}
}
Expand Down Expand Up @@ -147,8 +146,8 @@ class Canvas {

isTokenExpired(err) {
// check condition for token expiration, canvas sends a `WWW-Authenticate` header if 401 is for token expiry
const headers = _.get( err, 'response.headers', {} );
if ( headers['www-authenticate'] ) {
const headers = _.get(err, 'response.headers', {});
if (headers['www-authenticate']) {
return true;
}
return false;
Expand All @@ -171,7 +170,7 @@ class Canvas {
}),
headers: {
'Content-Type': 'application/json',
}
},
});
this.accessToken = resp.data.access_token;
this.canvasUserId = resp.data.user.id;
Expand Down Expand Up @@ -204,9 +203,9 @@ class Canvas {
}
const url = OAuth.makeURL(this.hostedUrl, requestConfig.url, requestConfig.query || {});
const response = await axios({
...requestConfig,
url,
headers: { Authorization: `Bearer ${this.accessToken}` },
...requestConfig,
url,
headers: { Authorization: `Bearer ${this.accessToken}` },
});
const { data, status } = response;
return { data, status };
Expand All @@ -222,7 +221,7 @@ class Canvas {
}
try {
await this.refreshUserToken(this.refreshToken);
} catch(err) {
} catch (err) {
console.error(err);
}

Expand Down Expand Up @@ -295,8 +294,8 @@ class Canvas {
payload.only_visible_to_overrides = true;
payload.assignment_overrides = [
{
"student_ids": studentIds
}
'student_ids': studentIds,
},
];
}

Expand Down Expand Up @@ -325,8 +324,8 @@ class Canvas {
submission: {
submission_type: 'online_url',
url: submissionUrl,
}
}
},
},
});
return submission;
}
Expand Down Expand Up @@ -355,9 +354,9 @@ class Canvas {
return submission;
}

async listSubmissions({ courseId, assignmentId}) {
async listSubmissions({ courseId, assignmentId }) {
const submissions = await paginatedCollect(this, {
url: `api/v1/courses/${courseId}/assignments/${assignmentId}/submissions`
url: `api/v1/courses/${courseId}/assignments/${assignmentId}/submissions`,
});
return submissions;
}
Expand All @@ -371,14 +370,14 @@ class Canvas {
const { data: grades } = await this.makeRequest({
url: `/api/v1/courses/${courseId}/assignments/${assignmentId}/submissions/update_grades`,
method: 'POST',
data: { grade_data: gradeData }
data: { grade_data: gradeData },
});
return grades;
}

async getAccounts() {
const accounts = await paginatedCollect(this, {
url: `/api/v1/manageable_accounts`,
url: '/api/v1/manageable_accounts',
method: 'GET',
});
return accounts;
Expand All @@ -387,7 +386,7 @@ class Canvas {
/**
* Mainly added to fetch Teacher and ta, use enrollment_type in data
*/
async getAccountUsers(id, data = {enrollment_type: ['teacher', 'ta']}) {
async getAccountUsers(id, data = { enrollment_type: ['teacher', 'ta'] }) {
const users = await paginatedCollect(this, {
url: `/api/v1/accounts/${id}/users`,
method: 'GET',
Expand All @@ -397,23 +396,14 @@ class Canvas {
}

async getUserProfile(id) {
try {
const resp = await this.makeRequest({
url: `/api/v1/users/${id}/profile`,
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
this.canvasUserId = resp.data.id;
return resp.data;
} catch(err) {
throw new LMSError('Unable to fetch user profile', 'canvas.USER_PROFILE_ERROR', {
userId: this.userId,
id,
err,
});
}
const resp = await this.makeRequest({
url: `/api/v1/users/${id}/profile`,
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
return resp.data;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/edmodo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ interface Tokens {
refresh_token: string;
}

export = Edmodo;
/**
* @class Edmodo
*/
declare class Edmodo {
export default class Edmodo {
constructor(
name: string,
emitter: events.EventEmitter,
Expand Down
3 changes: 1 addition & 2 deletions src/error.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export = LMSError;
declare class LMSError extends Error {
export default class LMSError extends Error {
/**
* @param {string} message
* @param {string} [type=error]
Expand Down
Loading