diff --git a/src/core/baseClient.ts b/src/core/baseClient.ts index 6aec0e9..72e8bee 100644 --- a/src/core/baseClient.ts +++ b/src/core/baseClient.ts @@ -59,7 +59,7 @@ export class BaseClient { const pingFormData = new URLSearchParams(); pingFormData.append("include_data", "true"); const pingData = await this.makeAuthedRequest( - this.API_BASE + "/ping", + `${this.API_BASE}/ping`, { method: "POST", body: pingFormData, @@ -82,20 +82,17 @@ export class BaseClient { public async makeAuthedRequest( path: string, fetchOptions: RequestInit, - options?: { revalidateToken?: boolean }, + options: { revalidateToken?: boolean } = { revalidateToken: true }, ) { if (!this.sessionId) throw new Error("No session ID"); - if (!options) { - options = {}; - } - if (typeof options?.revalidateToken == "undefined") { + if (typeof options?.revalidateToken === "undefined") { options.revalidateToken = true; } const requestOptions = { ...fetchOptions, headers: { Cookie: this?.authCookies?.join(";") ?? [], - Authorization: "Basic " + this.sessionId, + Authorization: `Basic ${this.sessionId}`, "User-Agent": "classcharts-api https://github.com/classchartsapi/classcharts-api-js", ...fetchOptions.headers, @@ -107,16 +104,16 @@ export class BaseClient { } } const request = await fetch(path, requestOptions); - // deno-lint-ignore no-explicit-any + // biome-ignore lint/suspicious/noExplicitAny: let responseJSON: ClassChartsResponse; try { responseJSON = await request.json(); } catch { throw new Error( - "Error parsing JSON. Returned response: " + (await request.text()), + `Error parsing JSON. Returned response: ${await request.text()}`, ); } - if (responseJSON.success == 0) { + if (responseJSON.success === 0) { throw new Error(responseJSON.error); } return responseJSON; @@ -128,7 +125,7 @@ export class BaseClient { async getStudentInfo(): Promise { const body = new URLSearchParams(); body.append("include_data", "true"); - return await this.makeAuthedRequest(this.API_BASE + "/ping", { + return await this.makeAuthedRequest(`${this.API_BASE}/ping`, { method: "POST", body: body, }); @@ -147,7 +144,7 @@ export class BaseClient { options?.to && params.append("to", options?.to); options?.last_id && params.append("last_id", options?.last_id); return await this.makeAuthedRequest( - this.API_BASE + "/activity/" + this.studentId + "?" + params.toString(), + `${this.API_BASE}/activity/${this.studentId}?${params.toString()}`, { method: "GET", }, @@ -197,7 +194,7 @@ export class BaseClient { options?.from && params.append("from", options?.from); options?.to && params.append("to", options?.to); return await this.makeAuthedRequest( - this.API_BASE + "/behaviour/" + this.studentId + "?" + params.toString(), + `${this.API_BASE}/behaviour/${this.studentId}?${params.toString()}`, { method: "GET", }, @@ -217,7 +214,7 @@ export class BaseClient { options?.from && params.append("from", String(options?.from)); options?.to && params.append("to", String(options?.to)); return await this.makeAuthedRequest( - this.API_BASE + "/homeworks/" + this.studentId + "?" + params.toString(), + `${this.API_BASE}/homeworks/${this.studentId}?${params.toString()}`, { method: "GET", }, @@ -233,7 +230,7 @@ export class BaseClient { const params = new URLSearchParams(); params.append("date", String(options?.date)); return await this.makeAuthedRequest( - this.API_BASE + "/timetable/" + this.studentId + "?" + params.toString(), + `${this.API_BASE}/timetable/${this.studentId}?${params.toString()}`, { method: "GET", }, @@ -245,7 +242,7 @@ export class BaseClient { */ async getBadges(): Promise { return await this.makeAuthedRequest( - this.API_BASE + "/eventbadges/" + this.studentId, + `${this.API_BASE}/eventbadges/${this.studentId}`, { method: "GET", }, @@ -257,7 +254,7 @@ export class BaseClient { */ async getAnnouncements(): Promise { return await this.makeAuthedRequest( - this.API_BASE + "/announcements/" + this.studentId, + `${this.API_BASE}/announcements/${this.studentId}`, { method: "GET", }, @@ -269,7 +266,7 @@ export class BaseClient { */ async getDetentions(): Promise { return await this.makeAuthedRequest( - this.API_BASE + "/detentions/" + this.studentId, + `${this.API_BASE}/detentions/${this.studentId}`, { method: "GET", }, @@ -287,7 +284,7 @@ export class BaseClient { options?.from && params.append("from", options?.from); options?.to && params.append("to", options?.to); return await this.makeAuthedRequest( - this.API_BASE + "/attendance/" + this.studentId + "?" + params.toString(), + `${this.API_BASE}/attendance/${this.studentId}?${params.toString()}`, { method: "GET", }, @@ -299,7 +296,7 @@ export class BaseClient { */ async getPupilFields(): Promise { return await this.makeAuthedRequest( - this.API_BASE + "/customfields/" + this.studentId, + `${this.API_BASE}/customfields/${this.studentId}`, { method: "GET", }, diff --git a/src/core/parentClient.ts b/src/core/parentClient.ts index 7d04159..a605bec 100644 --- a/src/core/parentClient.ts +++ b/src/core/parentClient.ts @@ -36,13 +36,13 @@ export class ParentClient extends BaseClient { const headers = new Headers({ "Content-Type": "application/x-www-form-urlencoded", }); - const response = await fetch(BASE_URL + "/parent/login", { + const response = await fetch(`${BASE_URL}/parent/login`, { method: "POST", body: formData, headers: headers, redirect: "manual", }); - if (response.status != 302 || !response.headers.has("set-cookie")) { + if (response.status !== 302 || !response.headers.has("set-cookie")) { await response.body?.cancel(); // Make deno tests happy by closing the body, unsure whether this is needed for the actual library throw new Error( "Unauthenticated: ClassCharts didn't return authentication cookies", @@ -53,7 +53,7 @@ export class ParentClient extends BaseClient { // this.authCookies = cookies.split(";"); const sessionCookies = parseCookies(cookies); const sessionID = JSON.parse( - String(sessionCookies["parent_session_credentials"]), + String(sessionCookies.parent_session_credentials), ); this.sessionId = sessionID.session_id; this.pupils = await this.getPupils(); @@ -65,7 +65,7 @@ export class ParentClient extends BaseClient { * @returns an array of Pupils connected to this parent's account */ async getPupils(): Promise { - const response = await this.makeAuthedRequest(this.API_BASE + "/pupils", { + const response = await this.makeAuthedRequest(`${this.API_BASE}/pupils`, { method: "GET", }); return response.data; @@ -81,7 +81,7 @@ export class ParentClient extends BaseClient { const pupils = this.pupils; for (let i = 0; i < pupils.length; i++) { const pupil = pupils[i]; - if (pupil.id == pupilId) { + if (pupil.id === pupilId) { this.studentId = pupil.id; return; } @@ -102,7 +102,7 @@ export class ParentClient extends BaseClient { formData.append("current", currentPassword); formData.append("new", newPassword); formData.append("repeat", newPassword); - return await this.makeAuthedRequest(this.API_BASE + "/password", { + return await this.makeAuthedRequest(`${this.API_BASE}/password`, { method: "POST", body: formData, headers: { diff --git a/src/core/studentClient.ts b/src/core/studentClient.ts index 0514977..b4264f5 100644 --- a/src/core/studentClient.ts +++ b/src/core/studentClient.ts @@ -42,12 +42,12 @@ export class StudentClient extends BaseClient { formData.append("dob", this.dateOfBirth); formData.append("remember_me", "1"); formData.append("recaptcha-token", "no-token-available"); - const request = await fetch(BASE_URL + "/student/login", { + const request = await fetch(`${BASE_URL}/student/login`, { method: "POST", body: formData, redirect: "manual", }); - if (request.status != 302 || !request.headers.has("set-cookie")) { + if (request.status !== 302 || !request.headers.has("set-cookie")) { await request.body?.cancel(); // Make deno tests happy by closing the body, unsure whether this is needed for the actual library throw new Error( "Unauthenticated: ClassCharts didn't return authentication cookies", @@ -57,7 +57,7 @@ export class StudentClient extends BaseClient { this.authCookies = cookies.split(","); const sessionCookies = parseCookies(cookies); const sessionID = JSON.parse( - String(sessionCookies["student_session_credentials"]), + String(sessionCookies.student_session_credentials), ); this.sessionId = sessionID.session_id; await this.getNewSessionId(); @@ -71,7 +71,7 @@ export class StudentClient extends BaseClient { */ async getRewards(): Promise { return await this.makeAuthedRequest( - this.API_BASE + "/rewards/" + this.studentId, + `${this.API_BASE}/rewards/${this.studentId}`, { method: "GET", }, @@ -84,7 +84,7 @@ export class StudentClient extends BaseClient { * @returns An object containing the current student's balance and item ID purchased */ async purchaseReward(itemId: number): Promise { - return await this.makeAuthedRequest(this.API_BASE + "/purchase/" + itemId, { + return await this.makeAuthedRequest(`${this.API_BASE}/purchase/${itemId}`, { method: "POST", body: `pupil_id=${this.studentId}`, }); @@ -99,7 +99,7 @@ export class StudentClient extends BaseClient { async getStudentCode( options: GetStudentCodeOptions, ): Promise { - const data = await this.makeAuthedRequest(this.API_BASE + "/getcode", { + const data = await this.makeAuthedRequest(`${this.API_BASE}/getcode`, { method: "POST", body: JSON.stringify({ date: options.dateOfBirth,