mirror of
https://github.com/classchartsapi/classcharts-api-js.git
synced 2026-05-14 03:56:59 +00:00
feat: helper functions
This commit is contained in:
parent
67fe788090
commit
97292756dc
6 changed files with 102 additions and 20 deletions
55
src/api.ts
Normal file
55
src/api.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
* Helper functions for requesting individual students instead of making a whole client
|
||||
* Using a client should be prefered over performance reasons
|
||||
*/
|
||||
import { ClasschartsClient } from '.'
|
||||
import {
|
||||
ActivityResponse,
|
||||
BehaviourResponse,
|
||||
GetActivityOptions,
|
||||
GetBehaviourOptions,
|
||||
GetHomeworkOptions,
|
||||
GetLessonsOptions,
|
||||
HomeworksResponse,
|
||||
LessonsResponse,
|
||||
Student,
|
||||
} from './types'
|
||||
export async function getStudentInfo(
|
||||
studentCode: string,
|
||||
dateOfBirth: string | null
|
||||
): Promise<Student> {
|
||||
const client = new ClasschartsClient(studentCode, dateOfBirth)
|
||||
return await client.getStudentInfo()
|
||||
}
|
||||
export async function getActivity(
|
||||
studentCode: string,
|
||||
dateOfBirth: string | null,
|
||||
options: GetActivityOptions | null
|
||||
): Promise<ActivityResponse> {
|
||||
const client = new ClasschartsClient(studentCode, dateOfBirth)
|
||||
return await client.getActivity(options)
|
||||
}
|
||||
export async function getBehaviour(
|
||||
studentCode: string,
|
||||
dateOfBirth: string | null,
|
||||
options: GetBehaviourOptions | null
|
||||
): Promise<BehaviourResponse> {
|
||||
const client = new ClasschartsClient(studentCode, dateOfBirth)
|
||||
return await client.getBehaviour(options)
|
||||
}
|
||||
export async function listHomeworks(
|
||||
studentCode: string,
|
||||
dateOfBirth: string | null,
|
||||
options: GetHomeworkOptions | null
|
||||
): Promise<HomeworksResponse> {
|
||||
const client = new ClasschartsClient(studentCode, dateOfBirth)
|
||||
return await client.listHomeworks(options)
|
||||
}
|
||||
export async function getLessons(
|
||||
studentCode: string,
|
||||
dateOfBirth: string | null,
|
||||
options: GetLessonsOptions
|
||||
): Promise<LessonsResponse> {
|
||||
const client = new ClasschartsClient(studentCode, dateOfBirth)
|
||||
return await client.getLessons(options)
|
||||
}
|
||||
|
|
@ -11,8 +11,11 @@ import {
|
|||
HomeworksResponse,
|
||||
LessonsResponse,
|
||||
Student,
|
||||
} from '../types'
|
||||
} from './types'
|
||||
import { API_BASE, BASE_URL } from './consts'
|
||||
/**
|
||||
* The base client
|
||||
*/
|
||||
export class ClasschartsClient {
|
||||
public studentCode = ''
|
||||
public dateOfBirth = ''
|
||||
|
|
@ -20,7 +23,12 @@ export class ClasschartsClient {
|
|||
public studentName = ''
|
||||
private authCookies: Array<string> | undefined
|
||||
private sessionId = ''
|
||||
constructor(studentCode: unknown, dateOfBirth: unknown) {
|
||||
/**
|
||||
*
|
||||
* @param studentCode Classcharts student code
|
||||
* @param dateOfBirth Student's date of birth
|
||||
*/
|
||||
constructor(studentCode: string, dateOfBirth: string | null) {
|
||||
this.studentCode = String(studentCode)
|
||||
this.dateOfBirth = String(dateOfBirth)
|
||||
}
|
||||
|
|
@ -48,8 +56,10 @@ export class ClasschartsClient {
|
|||
}
|
||||
return responseJSON.data
|
||||
}
|
||||
|
||||
async init() {
|
||||
/**
|
||||
* Initialises the client and authenticates with classcharts
|
||||
*/
|
||||
async init(): Promise<void> {
|
||||
if (!this.studentCode) throw new Error('Student Code not inputted')
|
||||
const formData = new URLSearchParams()
|
||||
formData.append('_method', 'POST')
|
||||
|
|
@ -82,6 +92,10 @@ export class ClasschartsClient {
|
|||
this.studentId = user.id
|
||||
this.studentName = user.name
|
||||
}
|
||||
/**
|
||||
* Gets general information about the logged in student
|
||||
* @returns Student object
|
||||
*/
|
||||
async getStudentInfo(): Promise<Student> {
|
||||
if (!this.authCookies) throw new Error('Not authenticated')
|
||||
const data = await this.makeAuthedRequest(API_BASE + '/ping', {
|
||||
|
|
@ -90,6 +104,11 @@ export class ClasschartsClient {
|
|||
})
|
||||
return data?.user
|
||||
}
|
||||
/**
|
||||
* Get's the logged in student's general activity
|
||||
* @param options GetActivityOptions
|
||||
* @returns Activity data
|
||||
*/
|
||||
async getActivity(
|
||||
options: GetActivityOptions | null
|
||||
): Promise<ActivityResponse> {
|
||||
|
|
@ -103,6 +122,11 @@ export class ClasschartsClient {
|
|||
}
|
||||
)
|
||||
}
|
||||
/**
|
||||
* Gets the logged in students behaviour points
|
||||
* @param options GetBehaviourOptions
|
||||
* @returns Array of behaviour points
|
||||
*/
|
||||
async getBehaviour(
|
||||
options: GetBehaviourOptions | null
|
||||
): Promise<BehaviourResponse> {
|
||||
|
|
@ -117,6 +141,11 @@ export class ClasschartsClient {
|
|||
}
|
||||
)
|
||||
}
|
||||
/**
|
||||
* Gets a list of the logged in student's homeworks
|
||||
* @param options GetHomeworkOptions
|
||||
* @returns Array of homeworks
|
||||
*/
|
||||
async listHomeworks(
|
||||
options: GetHomeworkOptions | null
|
||||
): Promise<HomeworksResponse> {
|
||||
|
|
@ -142,6 +171,11 @@ export class ClasschartsClient {
|
|||
}
|
||||
return data
|
||||
}
|
||||
/**
|
||||
* Gets the logged in student's lessons for a day
|
||||
* @param options GetLessonsOptions
|
||||
* @returns Array of lessons
|
||||
*/
|
||||
async getLessons(options: GetLessonsOptions): Promise<LessonsResponse> {
|
||||
if (!this.authCookies) throw new Error('Not authenticated')
|
||||
if (!options?.date) throw new Error('No date specified')
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
export * from './client'
|
||||
export * from './api'
|
||||
|
|
|
|||
|
|
@ -22,14 +22,6 @@ function main() {
|
|||
__dirname + '/version.txt',
|
||||
Buffer.from(sourceObj.version, 'utf-8')
|
||||
)
|
||||
try {
|
||||
fs.mkdirSync(__dirname + '/types/')
|
||||
} catch {}
|
||||
|
||||
fs.copyFileSync(
|
||||
__dirname + '/../types/index.d.ts',
|
||||
__dirname + '/types/index.d.ts'
|
||||
)
|
||||
fs.copyFileSync(__dirname + '/../.npmignore', __dirname + '/.npmignore')
|
||||
}
|
||||
|
||||
|
|
|
|||
145
src/types.d.ts
vendored
Normal file
145
src/types.d.ts
vendored
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
export interface Student {
|
||||
id: number
|
||||
name: string
|
||||
first_name: string
|
||||
last_name: string
|
||||
avatar_url: string
|
||||
display_behaviour: boolean
|
||||
display_parent_behaviour: boolean
|
||||
display_homework: boolean
|
||||
display_rewards: boolean
|
||||
display_detentions: boolean
|
||||
display_report_cards: boolean
|
||||
display_classes: boolean
|
||||
display_announcements: boolean
|
||||
display_attendance: boolean
|
||||
display_attendance_type: string
|
||||
display_attendance_percentage: boolean
|
||||
display_activity: boolean
|
||||
display_mental_health: boolean
|
||||
display_timetable: boolean
|
||||
is_disabled: boolean
|
||||
display_two_way_communications: boolean
|
||||
display_absences: boolean
|
||||
can_upload_attachments: string | null
|
||||
display_event_badges: boolean
|
||||
display_avatars: boolean
|
||||
display_concern_submission: boolean
|
||||
display_custom_fields: boolean
|
||||
pupil_concerns_help_text: string
|
||||
allow_pupils_add_timetable_notes: boolean
|
||||
announcements_count: number
|
||||
messages_count: number
|
||||
pusher_channel_name: string
|
||||
has_birthday: boolean
|
||||
has_new_survey: boolean
|
||||
survey_id: number | null
|
||||
detention_alias_plural_uc: string
|
||||
}
|
||||
export interface GetActivityOptions {
|
||||
from: string | undefined
|
||||
to: string | undefined
|
||||
}
|
||||
export interface ActivityTimelinePoint {
|
||||
positive: number
|
||||
negative: number
|
||||
name: string
|
||||
start: string
|
||||
end: string
|
||||
}
|
||||
export interface ActivityResponse {
|
||||
timeline: Array<ActivityTimelinePoint>
|
||||
positiveReasons: Record<string, string>
|
||||
negative_reasons: Record<string, string>
|
||||
other_positive: Array<any>
|
||||
other_negative: Array<any>
|
||||
other_positive_count: Array<any>
|
||||
other_negative_count: Array<any>
|
||||
}
|
||||
export interface GetBehaviourOptions {
|
||||
from: string | undefined
|
||||
to: string | undefined
|
||||
last_id: string | undefined
|
||||
}
|
||||
export interface BehaviourPoint {
|
||||
id: number
|
||||
type: string
|
||||
polarity: string
|
||||
reason: string
|
||||
score: number
|
||||
timestamp: string
|
||||
timestamp_custom_time: string | null
|
||||
style: {
|
||||
border_color: string | null
|
||||
custom_class: string | null
|
||||
}
|
||||
pupil_name: string
|
||||
lesson_name: string
|
||||
teacher_name: string
|
||||
room_name: string | null
|
||||
note: string
|
||||
_can_delete: string
|
||||
detention_date: string | null
|
||||
detention_time: string | null
|
||||
detention_location: string | null
|
||||
detention_type: string | null
|
||||
}
|
||||
export type BehaviourResponse = Array<BehaviourPoint>
|
||||
export type DisplayDate = 'due_date' | 'issue_date'
|
||||
export interface GetHomeworkOptions {
|
||||
displayDate: DisplayDate
|
||||
fromDate: string
|
||||
toDate: string
|
||||
}
|
||||
export interface Homework {
|
||||
lesson: string
|
||||
subject: string
|
||||
teacher: string
|
||||
homework_type: string
|
||||
id: number
|
||||
title: string
|
||||
meta_title: string
|
||||
description: string
|
||||
issue_date: string
|
||||
due_date: string
|
||||
completion_time_unit: string
|
||||
completion_time_value: string
|
||||
publish_time: string
|
||||
status: {
|
||||
id: number
|
||||
state: null
|
||||
mark: null
|
||||
mark_relative: number
|
||||
ticked: boolean
|
||||
allow_attachments: string
|
||||
first_seen_date: string
|
||||
last_seen_date: string
|
||||
attachments: Array<any>
|
||||
has_feedback: boolean
|
||||
}
|
||||
validated_links: Array<any>
|
||||
validated_attachments: Array<any>
|
||||
}
|
||||
export type HomeworksResponse = Array<Homework>
|
||||
export interface GetLessonsOptions {
|
||||
date: string
|
||||
}
|
||||
export interface Lesson {
|
||||
teacher_name: string
|
||||
lesson_name: string
|
||||
subject_name: string
|
||||
is_alternative_lesson: boolean
|
||||
period_name: string
|
||||
period_number: string
|
||||
room_name: string
|
||||
date: string
|
||||
start_time: string
|
||||
end_time: string
|
||||
key: number
|
||||
note_abstract: string
|
||||
note: string
|
||||
pupil_note_abstract: string
|
||||
pupil_note: string
|
||||
pupil_note_raw: string
|
||||
}
|
||||
export type LessonsResponse = Array<Lesson>
|
||||
Loading…
Add table
Add a link
Reference in a new issue