1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 03:56:59 +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

View file

@ -1,5 +1,5 @@
import axios from "axios";
import { AxiosRequestConfig } from "axios";
import type { AxiosRequestConfig, AxiosInstance } from "axios";
import type {
ActivityResponse,
AnnouncementsResponse,
@ -16,7 +16,6 @@ import type {
LessonsResponse,
Student,
} from "./types";
/**
* The base client
*/
@ -26,12 +25,14 @@ export class ClasschartsClient {
protected authCookies: Array<string> | undefined;
protected sessionId = "";
protected API_BASE = "";
protected axios: AxiosInstance;
/**
*
* @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.axios = axios.create(axiosConfig);
}
public async makeAuthedRequest(
path: string,
@ -48,7 +49,7 @@ export class ClasschartsClient {
},
validateStatus: () => true,
};
const request = await axios.request(requestOptions);
const request = await this.axios.request(requestOptions);
const responseJSON = request.data;
if (responseJSON.success == 0) {
throw new Error(responseJSON.error);

View file

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

View file

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