mirror of
https://github.com/classchartsapi/classcharts-api-js.git
synced 2026-05-14 19:59:37 +00:00
attendance filtering & meta responses
This commit is contained in:
parent
04b72babcf
commit
e9888e3f95
2 changed files with 61 additions and 11 deletions
|
|
@ -3,11 +3,15 @@ import type { AxiosRequestConfig, AxiosInstance } from "axios";
|
||||||
import type {
|
import type {
|
||||||
ActivityResponse,
|
ActivityResponse,
|
||||||
AnnouncementsResponse,
|
AnnouncementsResponse,
|
||||||
|
AttendanceDate,
|
||||||
AttendanceResponse,
|
AttendanceResponse,
|
||||||
|
AttendanceScheme,
|
||||||
BadgesResponse,
|
BadgesResponse,
|
||||||
BehaviourResponse,
|
BehaviourResponse,
|
||||||
DetentionsResponse,
|
DetentionsResponse,
|
||||||
|
ErrorResponse,
|
||||||
GetActivityOptions,
|
GetActivityOptions,
|
||||||
|
GetAttendanceOptions,
|
||||||
GetBehaviourOptions,
|
GetBehaviourOptions,
|
||||||
GetFullActivityOptions,
|
GetFullActivityOptions,
|
||||||
GetHomeworkOptions,
|
GetHomeworkOptions,
|
||||||
|
|
@ -15,7 +19,9 @@ import type {
|
||||||
Homework,
|
Homework,
|
||||||
HomeworksResponse,
|
HomeworksResponse,
|
||||||
LessonsResponse,
|
LessonsResponse,
|
||||||
|
ResponseFormat,
|
||||||
Student,
|
Student,
|
||||||
|
SuccessResponse,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
/**
|
/**
|
||||||
* The base client
|
* The base client
|
||||||
|
|
@ -35,9 +41,17 @@ export class ClasschartsClient {
|
||||||
this.API_BASE = API_BASE;
|
this.API_BASE = API_BASE;
|
||||||
this.axios = axios.create(axiosConfig);
|
this.axios = axios.create(axiosConfig);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Utilising Axios to create authenticated requests to endpoints which have not yet been incorporated
|
||||||
|
* @param {string} path URL Endpoint to make request to
|
||||||
|
* @param {AxiosRequestConfig} options Set optional parameters for Axios Request
|
||||||
|
* @param {boolean} includeMeta Boolean to determine if the meta property is included in the response
|
||||||
|
* @returns {object | SuccessResponse} If includeMeta is false, will return standard data object
|
||||||
|
*/
|
||||||
public async makeAuthedRequest(
|
public async makeAuthedRequest(
|
||||||
path: string,
|
path: string,
|
||||||
options: Omit<AxiosRequestConfig, "path">
|
options: Omit<AxiosRequestConfig, "path">,
|
||||||
|
includeMeta?: boolean,
|
||||||
) {
|
) {
|
||||||
if (!this.authCookies) throw new Error("Not authenticated");
|
if (!this.authCookies) throw new Error("Not authenticated");
|
||||||
const requestOptions: AxiosRequestConfig = {
|
const requestOptions: AxiosRequestConfig = {
|
||||||
|
|
@ -51,11 +65,14 @@ export class ClasschartsClient {
|
||||||
validateStatus: () => true,
|
validateStatus: () => true,
|
||||||
};
|
};
|
||||||
const request = await this.axios.request(requestOptions);
|
const request = await this.axios.request(requestOptions);
|
||||||
const responseJSON = request.data;
|
const responseJSON: ResponseFormat = request.data;
|
||||||
if (responseJSON.success == 0) {
|
if (responseJSON.success == 0) {
|
||||||
throw new Error(responseJSON.error);
|
const err: ErrorResponse = responseJSON;
|
||||||
|
throw new Error(err.error);
|
||||||
}
|
}
|
||||||
return responseJSON.data;
|
const data = responseJSON.data, meta = responseJSON.meta;
|
||||||
|
const res: SuccessResponse = { data, meta };
|
||||||
|
return includeMeta ? res : data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -219,14 +236,19 @@ export class ClasschartsClient {
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Gets the logged in student's attendance
|
* Gets the logged in student's attendance
|
||||||
|
* @param options GetAttendanceOptions
|
||||||
* @returns Array of dates of attendance
|
* @returns Array of dates of attendance
|
||||||
*/
|
*/
|
||||||
async listAttendance(): Promise<AttendanceResponse> {
|
async listAttendance(options?: GetAttendanceOptions): Promise<AttendanceScheme> {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
options?.from && params.append("from", String(options?.from));
|
||||||
|
options?.to && params.append("to", String(options?.to));
|
||||||
return await this.makeAuthedRequest(
|
return await this.makeAuthedRequest(
|
||||||
this.API_BASE + "/attendance/" + this.studentId,
|
this.API_BASE + "/attendance/" + this.studentId + '?' + params.toString(),
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
},
|
||||||
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
36
src/types.ts
36
src/types.ts
|
|
@ -1,4 +1,14 @@
|
||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
export interface ResponseFormat {
|
||||||
|
success?: number;
|
||||||
|
expired?: number;
|
||||||
|
error?: string;
|
||||||
|
data: Record<string, unknown> | Array<object> | any;
|
||||||
|
meta: Record<string, unknown> | Array<object> | any;
|
||||||
|
}
|
||||||
|
export type SuccessResponse = Pick<ResponseFormat, "success" | "data" | "meta">;
|
||||||
|
export type ErrorResponse = Omit<ResponseFormat, "data">;
|
||||||
|
|
||||||
export interface Student {
|
export interface Student {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
|
|
@ -310,18 +320,36 @@ export interface GetFullActivityOptions {
|
||||||
*/
|
*/
|
||||||
to: string;
|
to: string;
|
||||||
}
|
}
|
||||||
|
export interface AttendanceMeta {
|
||||||
|
dates: Array<string>;
|
||||||
|
end_date: string;
|
||||||
|
percentage: string;
|
||||||
|
percentage_singe_august: string;
|
||||||
|
sessions: Array<string>;
|
||||||
|
start_date: string;
|
||||||
|
}
|
||||||
|
export interface GetAttendanceOptions {
|
||||||
|
/**
|
||||||
|
* From date, in format YYYY-MM-DD
|
||||||
|
*/
|
||||||
|
from: string;
|
||||||
|
/**
|
||||||
|
* To date, in format YYYY-MM-DD
|
||||||
|
*/
|
||||||
|
to: string;
|
||||||
|
}
|
||||||
|
export type AttendanceStatus = "present" | "ignore" | "absent" | "excused";
|
||||||
export interface AttendanceDate {
|
export interface AttendanceDate {
|
||||||
AM: {
|
AM: {
|
||||||
code: string;
|
code: string;
|
||||||
status: "present" | "ignore";
|
status: AttendanceStatus;
|
||||||
late_minutes: number;
|
late_minutes: number;
|
||||||
};
|
};
|
||||||
PM: {
|
PM: {
|
||||||
code: string;
|
code: string;
|
||||||
status: "present" | "ignore";
|
status: AttendanceStatus;
|
||||||
late_minutes: number;
|
late_minutes: number;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
export type AttendanceScheme = { data: AttendanceDate; meta: AttendanceMeta };
|
||||||
export type AttendanceResponse = Array<Record<string, AttendanceDate>>;
|
export type AttendanceResponse = Array<Record<string, AttendanceDate>>;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue