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

feat: use native fetch

This commit is contained in:
James Cook 2023-05-30 18:37:24 +01:00
parent 00e3a13821
commit 6552c282d5
6 changed files with 13 additions and 92 deletions

View file

@ -1,4 +1,3 @@
import ky, { type Options as KyOptions } from "ky-universal";
import type {
ActivityResponse,
AnnouncementsResponse,
@ -87,7 +86,7 @@ export class BaseClient {
*/
public async makeAuthedRequest(
path: string,
kyOptions: KyOptions,
fetchOptions: RequestInit,
options?: { revalidateToken?: boolean }
) {
if (!this.sessionId) throw new Error("No session ID");
@ -98,20 +97,19 @@ export class BaseClient {
options.revalidateToken = true;
}
const requestOptions = {
...kyOptions,
...fetchOptions,
headers: {
Cookie: this?.authCookies?.join(";") ?? [],
Authorization: "Basic " + this.sessionId,
...kyOptions.headers,
...fetchOptions.headers,
},
credentials: undefined,
} satisfies KyOptions;
} satisfies RequestInit;
if (options?.revalidateToken === true && this.lastPing) {
if (Date.now() - this.lastPing + 5000 > PING_INTERVAL) {
await this.getNewSessionId();
}
}
const request = await ky(path, requestOptions);
const request = await fetch(path, requestOptions);
let responseJSON: ClassChartsResponse<unknown, unknown>;
try {
responseJSON = await request.json();

View file

@ -1,4 +1,3 @@
import ky from "ky-universal";
import type { GetPupilsResponse } from "../types.js";
import { BaseClient } from "./baseClient.js";
@ -37,7 +36,7 @@ export class ParentClient extends BaseClient {
const headers = new Headers({
"Content-Type": "application/x-www-form-urlencoded",
});
const response = await ky(BASE_URL + "/parent/login", {
const response = await fetch(BASE_URL + "/parent/login", {
method: "POST",
body: formData,
headers: headers,

View file

@ -1,7 +1,6 @@
import { API_BASE_STUDENT, BASE_URL } from "../utils/consts.js";
import { BaseClient } from "./baseClient.js";
import { parseCookies } from "../utils/utils.js";
import ky from "ky-universal";
/**
* Student Client
@ -40,11 +39,10 @@ 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 ky(BASE_URL + "/student/login", {
const request = await fetch(BASE_URL + "/student/login", {
method: "POST",
body: formData,
redirect: "manual",
throwHttpErrors: false,
credentials: undefined,
});
if (request.status != 302 || !request.headers.get("set-cookie")) {