mirror of
https://github.com/classchartsapi/classcharts-api-js.git
synced 2026-05-14 11:58:13 +00:00
chore: deno badge & formatting
This commit is contained in:
parent
55d91a52b7
commit
70c9d623fc
6 changed files with 26 additions and 25 deletions
|
|
@ -1,10 +1,11 @@
|
||||||
<h1 align="center">Classcharts API JS</h1>
|
<h1 align="center">Classcharts API JS</h1>
|
||||||
<div align="center">
|
<div align="center">
|
||||||
<img alt="Discord" src="https://img.shields.io/discord/918103752068726855">
|
<img alt="Discord" src="https://img.shields.io/discord/918103752068726855">
|
||||||
<img alt="npm" src="https://img.shields.io/npm/dm/classcharts-api">
|
<img alt="license" src="https://img.shields.io/npm/l/classcharts-api">
|
||||||
<img alt="NPM" src="https://img.shields.io/npm/l/classcharts-api">
|
|
||||||
<img alt="npm" src="https://img.shields.io/npm/v/classcharts-api">
|
<img alt="npm" src="https://img.shields.io/npm/v/classcharts-api">
|
||||||
|
<img alt="deno" src="https://shield.deno.dev/x/classcharts_api">
|
||||||
<img alt="node-current" src="https://img.shields.io/node/v/classcharts-api">
|
<img alt="node-current" src="https://img.shields.io/node/v/classcharts-api">
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p align="center">A Node.js and Deno wrapper for getting information from the Classcharts API.</p>
|
<p align="center">A Node.js and Deno wrapper for getting information from the Classcharts API.</p>
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ export class BaseClient {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: pingFormData,
|
body: pingFormData,
|
||||||
},
|
},
|
||||||
{ revalidateToken: false }
|
{ revalidateToken: false },
|
||||||
);
|
);
|
||||||
this.sessionId = pingData.meta.session_id;
|
this.sessionId = pingData.meta.session_id;
|
||||||
this.lastPing = Date.now();
|
this.lastPing = Date.now();
|
||||||
|
|
@ -86,7 +86,7 @@ export class BaseClient {
|
||||||
public async makeAuthedRequest(
|
public async makeAuthedRequest(
|
||||||
path: string,
|
path: string,
|
||||||
fetchOptions: RequestInit,
|
fetchOptions: RequestInit,
|
||||||
options?: { revalidateToken?: boolean }
|
options?: { revalidateToken?: boolean },
|
||||||
) {
|
) {
|
||||||
if (!this.sessionId) throw new Error("No session ID");
|
if (!this.sessionId) throw new Error("No session ID");
|
||||||
if (!options) {
|
if (!options) {
|
||||||
|
|
@ -114,7 +114,7 @@ export class BaseClient {
|
||||||
responseJSON = await request.json();
|
responseJSON = await request.json();
|
||||||
} catch {
|
} catch {
|
||||||
throw new Error(
|
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) {
|
||||||
|
|
@ -153,7 +153,7 @@ export class BaseClient {
|
||||||
this.API_BASE + "/activity/" + this.studentId + "?" + params.toString(),
|
this.API_BASE + "/activity/" + this.studentId + "?" + params.toString(),
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
@ -165,7 +165,7 @@ export class BaseClient {
|
||||||
* @see getActivity
|
* @see getActivity
|
||||||
*/
|
*/
|
||||||
async getFullActivity(
|
async getFullActivity(
|
||||||
options: GetFullActivityOptions
|
options: GetFullActivityOptions,
|
||||||
): Promise<ActivityResponse["data"]> {
|
): Promise<ActivityResponse["data"]> {
|
||||||
let data: ActivityResponse["data"] = [];
|
let data: ActivityResponse["data"] = [];
|
||||||
let prevLast: number | undefined;
|
let prevLast: number | undefined;
|
||||||
|
|
@ -194,7 +194,7 @@ export class BaseClient {
|
||||||
* @returns Array of behaviour points
|
* @returns Array of behaviour points
|
||||||
*/
|
*/
|
||||||
async getBehaviour(
|
async getBehaviour(
|
||||||
options?: GetBehaviourOptions
|
options?: GetBehaviourOptions,
|
||||||
): Promise<BehaviourResponse> {
|
): Promise<BehaviourResponse> {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
options?.from && params.append("from", options?.from);
|
options?.from && params.append("from", options?.from);
|
||||||
|
|
@ -203,7 +203,7 @@ export class BaseClient {
|
||||||
this.API_BASE + "/behaviour/" + this.studentId + "?" + params.toString(),
|
this.API_BASE + "/behaviour/" + this.studentId + "?" + params.toString(),
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
@ -223,7 +223,7 @@ export class BaseClient {
|
||||||
this.API_BASE + "/homeworks/" + this.studentId + "?" + params.toString(),
|
this.API_BASE + "/homeworks/" + this.studentId + "?" + params.toString(),
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
for (let i = 0; i < data.data.length; i++) {
|
for (let i = 0; i < data.data.length; i++) {
|
||||||
|
|
@ -231,11 +231,11 @@ export class BaseClient {
|
||||||
// homework.lesson.replace(/\\/g, '')
|
// homework.lesson.replace(/\\/g, '')
|
||||||
data.data[i].description = data.data[i].description.replace(
|
data.data[i].description = data.data[i].description.replace(
|
||||||
/(<([^>]+)>)/gi,
|
/(<([^>]+)>)/gi,
|
||||||
""
|
"",
|
||||||
);
|
);
|
||||||
data.data[i].description = data.data[i].description.replace(
|
data.data[i].description = data.data[i].description.replace(
|
||||||
/ /g,
|
/ /g,
|
||||||
""
|
"",
|
||||||
);
|
);
|
||||||
data.data[i].description = data.data[i].description.trim();
|
data.data[i].description = data.data[i].description.trim();
|
||||||
}
|
}
|
||||||
|
|
@ -254,7 +254,7 @@ export class BaseClient {
|
||||||
this.API_BASE + "/timetable/" + this.studentId + "?" + params.toString(),
|
this.API_BASE + "/timetable/" + this.studentId + "?" + params.toString(),
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
@ -266,7 +266,7 @@ export class BaseClient {
|
||||||
this.API_BASE + "/eventbadges/" + this.studentId,
|
this.API_BASE + "/eventbadges/" + this.studentId,
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
@ -279,7 +279,7 @@ export class BaseClient {
|
||||||
this.API_BASE + "/announcements/" + this.studentId,
|
this.API_BASE + "/announcements/" + this.studentId,
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
).data;
|
).data;
|
||||||
}
|
}
|
||||||
|
|
@ -293,7 +293,7 @@ export class BaseClient {
|
||||||
this.API_BASE + "/detentions/" + this.studentId,
|
this.API_BASE + "/detentions/" + this.studentId,
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
).data;
|
).data;
|
||||||
}
|
}
|
||||||
|
|
@ -303,7 +303,7 @@ export class BaseClient {
|
||||||
* @returns Array of dates of attendance
|
* @returns Array of dates of attendance
|
||||||
*/
|
*/
|
||||||
async getAttendance(
|
async getAttendance(
|
||||||
options?: GetAttendanceOptions
|
options?: GetAttendanceOptions,
|
||||||
): Promise<AttendanceResponse> {
|
): Promise<AttendanceResponse> {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
options?.from && params.append("from", options?.from);
|
options?.from && params.append("from", options?.from);
|
||||||
|
|
@ -317,7 +317,7 @@ export class BaseClient {
|
||||||
params.toString(),
|
params.toString(),
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
).data;
|
).data;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ export class ParentClient extends BaseClient {
|
||||||
"Unauthenticated: ClassCharts returned an error: " +
|
"Unauthenticated: ClassCharts returned an error: " +
|
||||||
response.status +
|
response.status +
|
||||||
" " +
|
" " +
|
||||||
response.statusText
|
response.statusText,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,7 +54,7 @@ export class ParentClient extends BaseClient {
|
||||||
// this.authCookies = cookies.split(";");
|
// this.authCookies = cookies.split(";");
|
||||||
const sessionCookies = parseCookies(cookies);
|
const sessionCookies = parseCookies(cookies);
|
||||||
const sessionID = JSON.parse(
|
const sessionID = JSON.parse(
|
||||||
String(sessionCookies["parent_session_credentials"])
|
String(sessionCookies["parent_session_credentials"]),
|
||||||
);
|
);
|
||||||
super.sessionId = sessionID.session_id;
|
super.sessionId = sessionID.session_id;
|
||||||
this.pupils = await this.getPupils();
|
this.pupils = await this.getPupils();
|
||||||
|
|
|
||||||
|
|
@ -46,14 +46,14 @@ export class StudentClient extends BaseClient {
|
||||||
if (request.status != 302 || !request.headers.get("set-cookie")) {
|
if (request.status != 302 || !request.headers.get("set-cookie")) {
|
||||||
await request.body?.cancel(); // Make deno tests happy by closing the body, unsure whether this is needed for the actual library
|
await request.body?.cancel(); // Make deno tests happy by closing the body, unsure whether this is needed for the actual library
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Unauthenticated: ClassCharts didn't return authentication cookies"
|
"Unauthenticated: ClassCharts didn't return authentication cookies",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const cookies = String(request.headers.get("set-cookie"));
|
const cookies = String(request.headers.get("set-cookie"));
|
||||||
this.authCookies = cookies.split(",");
|
this.authCookies = cookies.split(",");
|
||||||
const sessionCookies = parseCookies(cookies);
|
const sessionCookies = parseCookies(cookies);
|
||||||
const sessionID = JSON.parse(
|
const sessionID = JSON.parse(
|
||||||
String(sessionCookies["student_session_credentials"])
|
String(sessionCookies["student_session_credentials"]),
|
||||||
);
|
);
|
||||||
this.sessionId = sessionID.session_id;
|
this.sessionId = sessionID.session_id;
|
||||||
await this.getNewSessionId();
|
await this.getNewSessionId();
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ Deno.test("Throws when no student code is provided", async () => {
|
||||||
await client.login();
|
await client.login();
|
||||||
},
|
},
|
||||||
Error,
|
Error,
|
||||||
"Student Code not provided"
|
"Student Code not provided",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -20,6 +20,6 @@ Deno.test("Throws with invalid student code", async () => {
|
||||||
await client.login();
|
await client.login();
|
||||||
},
|
},
|
||||||
Error,
|
Error,
|
||||||
"Unauthenticated: ClassCharts didn't return authentication cookies"
|
"Unauthenticated: ClassCharts didn't return authentication cookies",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ export function parseCookies(input: string) {
|
||||||
for (const cookie of cookies) {
|
for (const cookie of cookies) {
|
||||||
const cookieSplit = cookie.split(";")[0].split("=");
|
const cookieSplit = cookie.split(";")[0].split("=");
|
||||||
output[leftTrim(decodeURIComponent(cookieSplit[0]))] = decodeURIComponent(
|
output[leftTrim(decodeURIComponent(cookieSplit[0]))] = decodeURIComponent(
|
||||||
cookieSplit[1]
|
cookieSplit[1],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue