Skip to content

Commit

Permalink
Add support for all account types
Browse files Browse the repository at this point in the history
  • Loading branch information
a2br committed Dec 24, 2020
1 parent 97139ab commit da75e48
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 40 deletions.
44 changes: 29 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,33 @@ Here's a quick example on how to get the homework of a day.
// Import 'Session' class from ed.js
import { Session } from "ecoledirecte.js";

// Create a new Session. Currently inert
const session = new Session("EDELEVE", "0");

// Bringing your session to life! Make sure to wrap this
// in try {} catch {} in your code.
const account = await session.login();

// Gets the homwork due for a specific date as a simplified
// array. To get the original response from ED, there's still
// a _raw prop.
const homework = await account.getHomework("2021-01-14");

// Some strings are available in their original shape
// (base64), in HTML and as simple text.
console.log(homework[2].contenuDeSeance.contenu.text);
async function getHomework(username, password, date) {
// Create a new Session. As long as it doesn't
// log in, it's inert.
const session = new Session(username, password);

// Bringing your session to life! Make sure to wrap this
// in try {} catch {} in your code.
const account = await session.login();

// We should check that the user is a student: otherwise,
// we wouldn't be able to get the homework.
if (!(account instanceof accounts.Student)) return;
// This works too:
if (account.type !== "student") return;

// Get the homework due for a specific date as a simplified
// array. If you need more, you can get the original
// response from ED in the _raw prop of each elem.
const homework = await account.getHomework(date);

// Some large strings are available in their original shape
// (base64), in HTML and as simple text.
console.log(homework[2].contenuDeSeance.contenu.text);

// Returning the array of homework!
return homework;
}

getHomework("EDELEVE", "0", "2021-01-14");
```
17 changes: 14 additions & 3 deletions lib/Session.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { _loginRes, account } from "./types";
import {
getMainAccount,
isFamilyAccount,
isStaffAccount,
isStudentAccount,
isTeacherAccount,
login,
loginFailed,
loginSucceeded,
} from "./functions";
import { Student } from "./Student";
import { Family, Staff, Student, Teacher } from "./account_types";

export class Session {
private _username: string;
Expand All @@ -31,12 +34,20 @@ export class Session {
this.loginRes = loginRes;
if (loginFailed(loginRes))
throw Error(`API ERR: ${loginRes.code} | ${loginRes.message}`);

// Login succeeded

const account = getMainAccount(loginRes.data.accounts);
if (isStudentAccount(account)) {
return new Student(this);
} else if (isFamilyAccount(account)) {
return new Family(this);
} else if (isTeacherAccount(account)) {
return new Teacher(this);
} else if (isStaffAccount(account)) {
return new Staff(this);
} else {
throw Error(`ACCOUNTS OF TYPE '${account.typeCompte}' ARE NOT SUPPORTED`);
throw Error(`UNKNOWN ACCOUNT TYPE: '${(account as any).typeCompte}'`);
}
}

Expand All @@ -52,6 +63,6 @@ export class Session {
* @returns EcoleDirecte auth token
*/
get token() {
return this.loginRes?.token || null;
return this.loginRes?.token;
}
}
24 changes: 24 additions & 0 deletions lib/account_types/Account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Session } from "../Session";
import { account } from "../types";
import { getMainAccount, loginFailed } from "../functions";

export class Account {
private __account: account;

constructor(session: Session) {
const { username, password } = session.credentials;

// Necessary checks
if (!session.loginRes || loginFailed(session.loginRes))
throw Error("Account class must have valid connection");
const mainAccount = getMainAccount(session.loginRes.data.accounts);

if (!session.token) throw Error("Account class MUST have token");

this.__account = mainAccount;
}

get doc() {
return this.__account;
}
}
28 changes: 28 additions & 0 deletions lib/account_types/Family.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Account } from "./Account";
import { Session } from "../Session";

import { _loginResSuccess, familyAccount } from "../types";
import { getMainAccount, isFamilyAccount } from "../functions";

export class Family extends Account {
public type: "family" = "family";
private account: familyAccount;
private token: string;

constructor(private session: Session) {
super(session);
const { username, password } = session.credentials;

const mainAccount = getMainAccount(
(session.loginRes as _loginResSuccess).data.accounts
);

if (!isFamilyAccount(mainAccount))
throw Error("Family class's main account is wrong");

if (!session.token) throw Error("Account class MUST have token");

this.account = mainAccount;
this.token = session.token;
}
}
28 changes: 28 additions & 0 deletions lib/account_types/Staff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Account } from "./Account";
import { Session } from "../Session";

import { _loginResSuccess, staffAccount } from "../types";
import { getMainAccount, isStaffAccount } from "../functions";

export class Staff extends Account {
public type: "staff" = "staff";
private account: staffAccount;
private token: string;

constructor(private session: Session) {
super(session);
const { username, password } = session.credentials;

const mainAccount = getMainAccount(
(session.loginRes as _loginResSuccess).data.accounts
);

if (!isStaffAccount(mainAccount))
throw Error("Staff class's main account is wrong");

if (!session.token) throw Error("Account class MUST have token");

this.account = mainAccount;
this.token = session.token;
}
}
27 changes: 16 additions & 11 deletions lib/Student.ts → lib/account_types/Student.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { htmlToText } from "html-to-text";

import { Session } from "./Session";
import { studentAccount } from "./types";
import { Account } from "./Account";
import { Session } from "../Session";

import { _loginResSuccess, studentAccount } from "../types";
import {
getMainAccount,
isStudentAccount,
loginFailed,
getTextbookPage,
textbookDateFailed,
} from "./functions";
} from "../functions";

export class Student {
export class Student extends Account {
public type: "student" = "student";
private account: studentAccount;
private token: string;

constructor(private session: Session) {
super(session);
const { username, password } = session.credentials;

// Necessary checks
if (!session.loginRes || loginFailed(session.loginRes))
throw Error("Student class must have valid connection");
const mainAccount = getMainAccount(session.loginRes.data.accounts);
const mainAccount = getMainAccount(
(session.loginRes as _loginResSuccess).data.accounts
);

if (!isStudentAccount(mainAccount))
throw Error("Student class's main account MUST be of type 'E'");
if (!session.token) throw Error("Student class MUST have token");
throw Error("Family class's main account is wrong");

if (!session.token) throw Error("Account class MUST have token");

this.account = mainAccount;
this.token = session.token;
Expand Down
28 changes: 28 additions & 0 deletions lib/account_types/Teacher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Account } from "./Account";
import { Session } from "../Session";

import { _loginResSuccess, teacherAccount } from "../types";
import { getMainAccount, isTeacherAccount } from "../functions";

export class Teacher extends Account {
public type: "teacher" = "teacher";
private account: teacherAccount;
private token: string;

constructor(private session: Session) {
super(session);
const { username, password } = session.credentials;

const mainAccount = getMainAccount(
(session.loginRes as _loginResSuccess).data.accounts
);

if (!isTeacherAccount(mainAccount))
throw Error("Teacher class's main account is wrong");

if (!session.token) throw Error("Account class MUST have token");

this.account = mainAccount;
this.token = session.token;
}
}
6 changes: 6 additions & 0 deletions lib/account_types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Family } from "./Family";
import { Staff } from "./Staff";
import { Student } from "./Student";
import { Teacher } from "./Teacher";

export { Family, Staff, Student, Teacher };
46 changes: 37 additions & 9 deletions lib/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { default as fetch } from "node-fetch";
import {
account,
studentAccount,
teacherAccount,
familyAccount,
_loginRes,
_loginResSuccess,
_loginResFailure,
_textbookDateRes,
_textbookDateResSuccess,
_textbookDateResFailure,
_textbookRes,
staffAccount,
} from "./types";

/**
Expand All @@ -33,10 +36,17 @@ export async function login(username: string, password: string) {
return body;
}

/**
* @returns The main account of the array
*/
export function getMainAccount(accounts: Array<account>) {
const mainAccount = accounts.find((acc) => acc.main) || accounts[0] || null;
return mainAccount;
}

/**
* @param id Account id
* @param token Auth token
* @param date Date of the textbook page (YYYY-MM-DD)
*/
export async function getTextbook(id: number, token: string) {
let urlencoded = new URLSearchParams();
Expand All @@ -58,6 +68,11 @@ export async function getTextbook(id: number, token: string) {
return body;
}

/**
* @param id Account id
* @param token Auth token
* @param date Date of the textbook page (YYYY-MM-DD)
*/
export async function getTextbookPage(id: number, token: string, date: string) {
let urlencoded = new URLSearchParams();
urlencoded.append(
Expand All @@ -77,14 +92,6 @@ export async function getTextbookPage(id: number, token: string, date: string) {
return body;
}

/**
* @returns The main account of the array
*/
export function getMainAccount(accounts: Array<account>) {
const mainAccount = accounts.find((acc) => acc.main) || accounts[0] || null;
return mainAccount;
}

//! TYPE GUARDS

//? LOGIN
Expand Down Expand Up @@ -112,6 +119,27 @@ export function isStudentAccount(account: account): account is studentAccount {
return account.typeCompte === "E";
}

/**
* @returns Whether `account` is `teacherAccount`
*/
export function isTeacherAccount(account: account): account is teacherAccount {
return account.typeCompte === "P";
}

/**
* @returns Whether `account` is `familyAccount`
*/
export function isFamilyAccount(account: account): account is familyAccount {
return account.typeCompte === "1";
}

/**
* @returns Whether `account` is `staffAccount`
*/
export function isStaffAccount(account: account): account is staffAccount {
return account.typeCompte === "A";
}

//? TEXTBOOK DATE

/**
Expand Down
9 changes: 8 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import * as accounts from "./account_types";
import { Session } from "./Session";
import * as types from "./types";

export { Session, types };
export { Session, types, accounts };

(async () => {
const session = new Session("EDPERSONNEL", "0");
const account = await session.login();
console.log(account.type);
})();
Loading

0 comments on commit da75e48

Please sign in to comment.