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

feat: add all functions of avaliable classcharts

This commit is contained in:
James Cook 2021-10-28 20:40:04 +01:00
parent 7e988a06f9
commit 18c80a13b9
6 changed files with 178 additions and 47 deletions

View file

@ -1,7 +1,18 @@
import Undici from 'undici'
import { DisplayDate, Homework, User } from '../types'
import { RequestOptions } from 'undici/types/dispatcher'
import {
ActivityResponse,
BehaviourResponse,
GetActivityOptions,
GetBehaviourOptions,
GetHomeworkOptions,
GetLessonsOptions,
Homework,
HomeworksResponse,
LessonsResponse,
Student,
} from '../types'
import { API_BASE, BASE_URL } from './consts'
export class ClasschartsClient {
public studentCode = ''
public dateOfBirth = ''
@ -13,9 +24,34 @@ export class ClasschartsClient {
this.studentCode = String(studentCode)
this.dateOfBirth = String(dateOfBirth)
}
private async makeAuthedRequest(
path: string,
options: Omit<RequestOptions, 'origin' | 'path'>
) {
if (!this.authCookies) throw new Error('Not authenticated')
const requestOptions: Omit<RequestOptions, 'origin' | 'path'> = {
...options,
headers: {
Cookie: this.authCookies.join(';'),
authorization: 'Basic ' + this.sessionId,
},
}
const request = await Undici.request(path, requestOptions)
let responseJSON
try {
responseJSON = await request.body.json()
} catch (err) {
throw new Error('Invalid JSON response, check your dates')
}
if (responseJSON.success == 0) {
throw new Error(responseJSON.error)
}
return responseJSON.data
}
async init() {
const formData = new URLSearchParams()
if (!this.studentCode) throw new Error('Student Code not inputted')
const formData = new URLSearchParams()
formData.append('_method', 'POST')
formData.append('code', this.studentCode.toUpperCase())
formData.append('dob', this.dateOfBirth)
@ -49,60 +85,64 @@ export class ClasschartsClient {
/**
*
* @returns {User}
* @returns {Promise<Student>}
*/
async getStudentInfo(): Promise<User> {
async getStudentInfo(): Promise<Student> {
if (!this.authCookies) throw new Error('Not authenticated')
const request = await Undici.request(API_BASE + '/ping', {
const data = await this.makeAuthedRequest(API_BASE + '/ping', {
method: 'POST',
body: 'include_date=true',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Cookie: this.authCookies.join(';'),
authorization: 'Basic ' + this.sessionId,
},
})
const data = await request.body.json()
return data.data?.user
return data?.user
}
async getActivity(
options: GetActivityOptions | null
): Promise<ActivityResponse> {
const params = new URLSearchParams()
options?.from && params.append('form', options?.from)
options?.to && params.append('to', options?.to)
return this.makeAuthedRequest(
API_BASE + '/activity/' + this.sessionId + '?' + params.toString(),
{
method: 'GET',
}
)
}
async getBehaviour(
options: GetBehaviourOptions | null
): Promise<BehaviourResponse> {
const params = new URLSearchParams()
options?.from && params.append('form', options?.from)
options?.to && params.append('to', options?.to)
options?.last_id && params.append('last_id', options?.last_id)
return await this.makeAuthedRequest(
API_BASE + '/behaviour/' + this.studentId + '?' + params.toString(),
{
method: 'GET',
}
)
}
/**
* Gets all the homework from
* @param displayDate {DisplayDate}
* @param fromDate
* @param toDate
* @returns {Array<Homework>}
* @returns {Promise<Array<Homework>>}
*/
async listHomeworks(
displayDate: DisplayDate,
fromDate: string,
toDate: string
): Promise<Array<Homework>> {
options: GetHomeworkOptions | null
): Promise<HomeworksResponse> {
if (!this.authCookies) throw new Error('Not authenticated')
const params = new URLSearchParams()
params.append('display_date', String(displayDate))
fromDate && params.append('from', String(fromDate))
toDate && params.append('to', String(toDate))
const request = await Undici.request(
params.append('display_date', String(options?.displayDate))
options?.fromDate && params.append('from', String(options?.fromDate))
options?.toDate && params.append('to', String(options?.toDate))
let data: Array<Homework> = await this.makeAuthedRequest(
API_BASE + '/homeworks/' + this.studentId + '?' + params.toString(),
{
method: 'GET',
headers: {
Cookie: this.authCookies.join(';'),
authorization: 'Basic ' + this.sessionId,
},
}
)
let responseJSON
try {
responseJSON = await request.body.json()
} catch (err) {
throw new Error('Invalid JSON response, check your dates')
}
if (responseJSON.success == 0) {
throw new Error(responseJSON.error)
}
let data: Array<Homework> = responseJSON?.data
for (let i = 0; i < data.length; i++) {
// homework.lesson.replace(/\\/g, '')
data[i].description = data[i].description.replace(
@ -114,4 +154,21 @@ export class ClasschartsClient {
}
return data
}
/**
*
* @param date
* @returns {Promise<LessonsResponse>}
*/
async getLessons(options: GetLessonsOptions): Promise<LessonsResponse> {
if (!this.authCookies) throw new Error('Not authenticated')
if (!options?.date) throw new Error('No date specified')
const params = new URLSearchParams()
params.append('date', String(options?.date))
return await this.makeAuthedRequest(
API_BASE + '/timetable/' + this.studentId + '?' + params.toString(),
{
method: 'GET',
}
)
}
}