mirror of
https://github.com/classchartsapi/classcharts-api-js.git
synced 2026-05-14 11:58:13 +00:00
fix: client getting logged out (#18)
This commit is contained in:
parent
8609ff4ba6
commit
ecbbc4777b
5 changed files with 24 additions and 8 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -117,4 +117,5 @@ dist
|
||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
tests/config.json
|
tests/config.json
|
||||||
|
tests/sandbox.js
|
||||||
|
|
@ -37,7 +37,6 @@ export class ClasschartsClient {
|
||||||
this.axios = axios.create({
|
this.axios = axios.create({
|
||||||
...axiosConfig,
|
...axiosConfig,
|
||||||
headers: {
|
headers: {
|
||||||
"User-Agent": "classcharts-api.js",
|
|
||||||
...axiosConfig?.headers,
|
...axiosConfig?.headers,
|
||||||
},
|
},
|
||||||
validateStatus: () => true,
|
validateStatus: () => true,
|
||||||
|
|
@ -45,16 +44,17 @@ export class ClasschartsClient {
|
||||||
}
|
}
|
||||||
public async makeAuthedRequest(
|
public async makeAuthedRequest(
|
||||||
path: string,
|
path: string,
|
||||||
options: Omit<AxiosRequestConfig, "path">
|
axiosOptions: Omit<AxiosRequestConfig, "path">,
|
||||||
|
options?: { includeMeta?: boolean }
|
||||||
) {
|
) {
|
||||||
if (!this.authCookies) throw new Error("Not authenticated");
|
if (!this.authCookies) throw new Error("Not authenticated");
|
||||||
const requestOptions: AxiosRequestConfig = {
|
const requestOptions: AxiosRequestConfig = {
|
||||||
...options,
|
...axiosOptions,
|
||||||
url: path,
|
url: path,
|
||||||
headers: {
|
headers: {
|
||||||
Cookie: this.authCookies.join(";"),
|
Cookie: this.authCookies.join(";"),
|
||||||
authorization: "Basic " + this.sessionId,
|
authorization: "Basic " + this.sessionId,
|
||||||
...options.headers,
|
...axiosOptions.headers,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const request = await this.axios.request(requestOptions);
|
const request = await this.axios.request(requestOptions);
|
||||||
|
|
@ -62,6 +62,9 @@ export class ClasschartsClient {
|
||||||
if (responseJSON.success == 0) {
|
if (responseJSON.success == 0) {
|
||||||
throw new Error(responseJSON.error);
|
throw new Error(responseJSON.error);
|
||||||
}
|
}
|
||||||
|
if (options?.includeMeta) {
|
||||||
|
return responseJSON;
|
||||||
|
}
|
||||||
return responseJSON.data;
|
return responseJSON.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -166,7 +169,6 @@ export class ClasschartsClient {
|
||||||
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
data[i].description_raw = data[i].description;
|
data[i].description_raw = data[i].description;
|
||||||
|
|
||||||
// homework.lesson.replace(/\\/g, '')
|
// homework.lesson.replace(/\\/g, '')
|
||||||
data[i].description = data[i].description.replace(/(<([^>]+)>)/gi, "");
|
data[i].description = data[i].description.replace(/(<([^>]+)>)/gi, "");
|
||||||
data[i].description = data[i].description.replace(/ /g, "");
|
data[i].description = data[i].description.replace(/ /g, "");
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,6 @@ export class ClasschartsParentClient extends ClasschartsClient {
|
||||||
formData.append("logintype", "existing");
|
formData.append("logintype", "existing");
|
||||||
formData.append("password", this.password);
|
formData.append("password", this.password);
|
||||||
formData.append("recaptcha-token", "no-token-avaliable");
|
formData.append("recaptcha-token", "no-token-avaliable");
|
||||||
|
|
||||||
const request = await this.axios.request({
|
const request = await this.axios.request({
|
||||||
url: BASE_URL + "/parent/login",
|
url: BASE_URL + "/parent/login",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|
|
||||||
|
|
@ -58,5 +58,19 @@ export class ClasschartsStudentClient extends ClasschartsClient {
|
||||||
const user = await this.getStudentInfo();
|
const user = await this.getStudentInfo();
|
||||||
this.studentId = user.id;
|
this.studentId = user.id;
|
||||||
this.studentName = user.name;
|
this.studentName = user.name;
|
||||||
|
const pingFormData = new URLSearchParams();
|
||||||
|
pingFormData.append("include_data", "true");
|
||||||
|
const pingData = await this.makeAuthedRequest(
|
||||||
|
this.API_BASE + "/ping",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
data: pingFormData.toString(),
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ includeMeta: true }
|
||||||
|
);
|
||||||
|
this.sessionId = pingData.meta.session_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ test("client returns activity data", () => {
|
||||||
|
|
||||||
test("client returns full activity", () => {
|
test("client returns full activity", () => {
|
||||||
return client
|
return client
|
||||||
.getFullActivity({ from: "2000-01-01", to: "2022-01-01" })
|
.getFullActivity({ from: "2022-01-01", to: "2022-09-01" })
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
let valid = false;
|
let valid = false;
|
||||||
if (data.length > 50) valid = true;
|
if (data.length > 50) valid = true;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue