1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 19:59:37 +00:00

feat: custom axios config support

This commit is contained in:
James Cook 2022-05-07 12:58:42 +01:00
parent d5b07cffc5
commit 75bc77fc0e
6 changed files with 24 additions and 14 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,5 @@
import axios from "axios"; import axios from "axios";
import { AxiosRequestConfig } from "axios"; import type { AxiosRequestConfig, AxiosInstance } from "axios";
import type { import type {
ActivityResponse, ActivityResponse,
AnnouncementsResponse, AnnouncementsResponse,
@ -16,7 +16,6 @@ import type {
LessonsResponse, LessonsResponse,
Student, Student,
} from "./types"; } from "./types";
/** /**
* The base client * The base client
*/ */
@ -26,12 +25,14 @@ export class ClasschartsClient {
protected authCookies: Array<string> | undefined; protected authCookies: Array<string> | undefined;
protected sessionId = ""; protected sessionId = "";
protected API_BASE = ""; protected API_BASE = "";
protected axios: AxiosInstance;
/** /**
* *
* @param API_BASE Base API URL, this is different depending if its called as a parent or student * @param API_BASE Base API URL, this is different depending if its called as a parent or student
*/ */
constructor(API_BASE: string) { constructor(API_BASE: string, axiosConfig?: AxiosRequestConfig) {
this.API_BASE = API_BASE; this.API_BASE = API_BASE;
this.axios = axios.create(axiosConfig);
} }
public async makeAuthedRequest( public async makeAuthedRequest(
path: string, path: string,
@ -48,7 +49,7 @@ export class ClasschartsClient {
}, },
validateStatus: () => true, validateStatus: () => true,
}; };
const request = await axios.request(requestOptions); const request = await this.axios.request(requestOptions);
const responseJSON = request.data; const responseJSON = request.data;
if (responseJSON.success == 0) { if (responseJSON.success == 0) {
throw new Error(responseJSON.error); throw new Error(responseJSON.error);

View file

@ -1,3 +1,3 @@
export * from "./parentClient"; export * from "./parentClient";
export * from "./studentClient"; export * from "./studentClient";
// export * from "./types"; export * from "./types";

View file

@ -1,4 +1,4 @@
import axios from "axios"; import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
import type { GetPupilsResponse } from "./types"; import type { GetPupilsResponse } from "./types";
import { ClasschartsClient } from "./baseClient"; import { ClasschartsClient } from "./baseClient";
@ -16,8 +16,12 @@ export class ClasschartsParentClient extends ClasschartsClient {
* @param email Parents email address * @param email Parents email address
* @param password Parents password * @param password Parents password
*/ */
constructor(email: string, password: string) { constructor(
super(API_BASE_PARENT); email: string,
password: string,
axiosConfig?: AxiosRequestConfig
) {
super(API_BASE_PARENT, axiosConfig);
this.email = String(email); this.email = String(email);
this.password = String(password); this.password = String(password);
} }
@ -34,7 +38,7 @@ export class ClasschartsParentClient extends ClasschartsClient {
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 axios.request({ const request = await this.axios.request({
url: BASE_URL + "/parent/login", url: BASE_URL + "/parent/login",
method: "POST", method: "POST",
data: formData.toString(), data: formData.toString(),

View file

@ -1,4 +1,5 @@
import axios from "axios"; import axios from "axios";
import type { AxiosRequestConfig } from "axios";
import { API_BASE_STUDENT, BASE_URL } from "./consts"; import { API_BASE_STUDENT, BASE_URL } from "./consts";
import { ClasschartsClient } from "./baseClient"; import { ClasschartsClient } from "./baseClient";
/** /**
@ -14,8 +15,12 @@ export class ClasschartsStudentClient extends ClasschartsClient {
* @param studentCode Classcharts student code * @param studentCode Classcharts student code
* @param dateOfBirth Student's date of birth * @param dateOfBirth Student's date of birth
*/ */
constructor(studentCode: string, dateOfBirth?: string) { constructor(
super(API_BASE_STUDENT); studentCode: string,
dateOfBirth?: string,
axiosConfig?: AxiosRequestConfig
) {
super(API_BASE_STUDENT, axiosConfig);
this.studentCode = String(studentCode); this.studentCode = String(studentCode);
this.dateOfBirth = String(dateOfBirth); this.dateOfBirth = String(dateOfBirth);
} }
@ -31,7 +36,7 @@ export class ClasschartsStudentClient extends ClasschartsClient {
formData.append("dob", this.dateOfBirth); formData.append("dob", this.dateOfBirth);
formData.append("remember_me", "1"); formData.append("remember_me", "1");
formData.append("recaptcha-token", "no-token-avaliable"); formData.append("recaptcha-token", "no-token-avaliable");
const request = await axios.request({ const request = await this.axios.request({
url: BASE_URL + "/student/login", url: BASE_URL + "/student/login",
method: "POST", method: "POST",
data: formData.toString(), data: formData.toString(),