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

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-05-25 18:18:31 +01:00
import type { AxiosRequestConfig } from "axios";
2022-03-12 11:37:28 +00:00
import type { GetPupilsResponse } from "./types";
2022-03-12 11:30:03 +00:00
2022-03-12 12:08:41 +00:00
import { ClasschartsClient } from "./baseClient";
2022-03-12 11:30:03 +00:00
import { API_BASE_PARENT, BASE_URL } from "./consts";
2022-08-03 16:53:46 +01:00
import { parseCookies } from "./utils";
2022-03-12 11:30:03 +00:00
/**
2023-04-07 13:47:08 +01:00
* Parent Client
2022-03-12 11:30:03 +00:00
*/
2023-04-07 13:47:08 +01:00
export class ParentClient extends ClasschartsClient {
2022-03-12 11:34:56 +00:00
private password = "";
private email = "";
2022-03-12 12:00:28 +00:00
// @ts-expect-error Init in .login
public pupils: GetPupilsResponse;
2022-03-12 11:30:03 +00:00
/**
*
* @param email Parents email address
* @param password Parents password
*/
2022-05-07 12:58:42 +01:00
constructor(
email: string,
password: string,
axiosConfig?: AxiosRequestConfig
) {
super(API_BASE_PARENT, axiosConfig);
2022-03-12 11:30:03 +00:00
this.email = String(email);
this.password = String(password);
}
/**
* Logs the user in the client and authenticates with classcharts
*/
async login(): Promise<void> {
if (!this.email) throw new Error("Email not inputted");
const formData = new URLSearchParams();
formData.append("_method", "POST");
formData.append("email", this.email);
formData.append("logintype", "existing");
formData.append("password", this.password);
formData.append("recaptcha-token", "no-token-avaliable");
2022-05-07 12:58:42 +01:00
const request = await this.axios.request({
2022-03-24 18:56:33 +00:00
url: BASE_URL + "/parent/login",
2022-03-12 11:30:03 +00:00
method: "POST",
2022-03-24 18:56:33 +00:00
data: formData.toString(),
maxRedirects: 0,
2022-03-12 11:30:03 +00:00
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
2022-03-24 18:56:33 +00:00
validateStatus: () => true,
2022-03-12 11:30:03 +00:00
});
2022-03-24 18:56:33 +00:00
if (request.status != 302 || !request.headers["set-cookie"])
2022-03-12 11:30:03 +00:00
throw new Error("Unauthenticated: Classcharts returned an error");
2022-08-03 16:53:46 +01:00
const cookies = String(request.headers["set-cookie"]);
2023-04-07 13:47:08 +01:00
// this.authCookies = cookies.split(";");
2022-08-03 16:53:46 +01:00
const sessionCookies = parseCookies(cookies);
const sessionID = JSON.parse(
String(sessionCookies["parent_session_credentials"])
2022-03-12 11:30:03 +00:00
);
this.sessionId = sessionID.session_id;
2022-03-12 12:00:28 +00:00
this.pupils = await this.getPupils();
if (!this.pupils) throw new Error("Account has no pupils attached");
this.studentId = this.pupils[0].id;
2022-03-12 11:30:03 +00:00
}
/**
* Get Pupil details
2023-04-07 13:47:08 +01:00
* @returns an array of Pupils connected to this parent's account
2022-03-12 11:30:03 +00:00
*/
2022-03-12 11:34:56 +00:00
async getPupils(): Promise<GetPupilsResponse> {
2022-03-12 11:37:28 +00:00
return this.makeAuthedRequest(this.API_BASE + "/pupils", {
2022-03-12 11:34:56 +00:00
method: "GET",
});
}
2022-03-12 12:00:28 +00:00
/**
2022-03-12 12:02:30 +00:00
* Selects a pupil to be used with API requests
2022-03-12 12:00:28 +00:00
* @param pupilId Pupil ID obtained from this.pupils or getPupils
*/
async selectPupil(pupilId: number): Promise<void> {
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];
if (pupil.id == pupilId) {
this.studentId = pupil.id;
return;
}
}
throw new Error("No pupil with specified ID returned");
}
2022-03-12 11:30:03 +00:00
}