1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 11:58:13 +00:00

chore: syntax & biome.js changes

This commit is contained in:
James Cook 2024-01-10 10:57:33 +00:00
parent 790a5a6aa6
commit a64fd6a718
8 changed files with 67 additions and 42 deletions

View file

@ -22,7 +22,7 @@ import { PING_INTERVAL } from "../utils/consts.ts";
/**
* Shared client for both parent and student. This is not exported and should not be used directly
*/
export class BaseClient {
export abstract class BaseClient {
/**
* @property studentId Currently selected student ID
*/
@ -30,7 +30,7 @@ export class BaseClient {
/**
* @property authCookies Cookies used for authentication (set during login and can be empty)
*/
public authCookies: Array<string>;
public authCookies: string[];
/**
* @property sessionId Session ID used for authentication
*/
@ -50,6 +50,8 @@ export class BaseClient {
this.authCookies = [];
this.API_BASE = API_BASE;
}
abstract login(): Promise<void>;
/**
* Revalidates the session ID.
*
@ -82,9 +84,11 @@ export class BaseClient {
public async makeAuthedRequest(
path: string,
fetchOptions: RequestInit,
options: { revalidateToken?: boolean } = { revalidateToken: true },
options: { revalidateToken?: boolean; } = { revalidateToken: true },
) {
if (!this.sessionId) throw new Error("No session ID");
if (!this.sessionId) {
throw new Error("No session ID");
}
if (typeof options?.revalidateToken === "undefined") {
options.revalidateToken = true;
}
@ -226,7 +230,9 @@ export class BaseClient {
* @returns Array of lessons
*/
async getLessons(options: GetLessonsOptions): Promise<LessonsResponse> {
if (!options?.date) throw new Error("No date specified");
if (!options?.date) {
throw new Error("No date specified");
}
const params = new URLSearchParams();
params.append("date", String(options?.date));
return await this.makeAuthedRequest(

View file

@ -25,8 +25,12 @@ export class ParentClient extends BaseClient {
* Authenticates with ClassCharts
*/
async login(): Promise<void> {
if (!this.email) throw new Error("Email not provided");
if (!this.password) throw new Error("Password not provided");
if (!this.email) {
throw new Error("Email not provided");
}
if (!this.password) {
throw new Error("Password not provided");
}
const formData = new URLSearchParams();
formData.append("_method", "POST");
formData.append("email", this.email);
@ -57,7 +61,9 @@ export class ParentClient extends BaseClient {
);
this.sessionId = sessionID.session_id;
this.pupils = await this.getPupils();
if (!this.pupils) throw new Error("Account has no pupils attached");
if (!this.pupils) {
throw new Error("Account has no pupils attached");
}
this.studentId = this.pupils[0].id;
}
/**
@ -77,7 +83,9 @@ export class ParentClient extends BaseClient {
* @see getPupils
*/
selectPupil(pupilId: number) {
if (!pupilId) throw new Error("No pupil ID specified");
if (!pupilId) {
throw new Error("No pupil ID specified");
}
const pupils = this.pupils;
for (let i = 0; i < pupils.length; i++) {
const pupil = pupils[i];

View file

@ -35,7 +35,9 @@ export class StudentClient extends BaseClient {
* Authenticates with ClassCharts
*/
async login(): Promise<void> {
if (!this.studentCode) throw new Error("Student Code not provided");
if (!this.studentCode) {
throw new Error("Student Code not provided");
}
const formData = new URLSearchParams();
formData.append("_method", "POST");
formData.append("code", this.studentCode.toUpperCase());