From 97292756dc876ed90f84de7ced78cb7d7280e1bc Mon Sep 17 00:00:00 2001 From: James Cook <47297250+jamesatjaminit@users.noreply.github.com> Date: Fri, 29 Oct 2021 15:46:33 +0100 Subject: [PATCH] feat: helper functions --- src/api.ts | 55 ++++++++++++++++++++++++++++++ src/client.ts | 42 ++++++++++++++++++++--- src/index.ts | 1 + src/release.ts | 8 ----- types/index.d.ts => src/types.d.ts | 14 ++++---- tsconfig.json | 2 +- 6 files changed, 102 insertions(+), 20 deletions(-) create mode 100644 src/api.ts rename types/index.d.ts => src/types.d.ts (93%) diff --git a/src/api.ts b/src/api.ts new file mode 100644 index 0000000..f676f7b --- /dev/null +++ b/src/api.ts @@ -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 { + const client = new ClasschartsClient(studentCode, dateOfBirth) + return await client.getStudentInfo() +} +export async function getActivity( + studentCode: string, + dateOfBirth: string | null, + options: GetActivityOptions | null +): Promise { + const client = new ClasschartsClient(studentCode, dateOfBirth) + return await client.getActivity(options) +} +export async function getBehaviour( + studentCode: string, + dateOfBirth: string | null, + options: GetBehaviourOptions | null +): Promise { + const client = new ClasschartsClient(studentCode, dateOfBirth) + return await client.getBehaviour(options) +} +export async function listHomeworks( + studentCode: string, + dateOfBirth: string | null, + options: GetHomeworkOptions | null +): Promise { + const client = new ClasschartsClient(studentCode, dateOfBirth) + return await client.listHomeworks(options) +} +export async function getLessons( + studentCode: string, + dateOfBirth: string | null, + options: GetLessonsOptions +): Promise { + const client = new ClasschartsClient(studentCode, dateOfBirth) + return await client.getLessons(options) +} diff --git a/src/client.ts b/src/client.ts index 956dbc1..467d4f8 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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 | 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 { 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 { 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 { @@ -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 { @@ -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 { @@ -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 { if (!this.authCookies) throw new Error('Not authenticated') if (!options?.date) throw new Error('No date specified') diff --git a/src/index.ts b/src/index.ts index 83dae76..5992cba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export * from './client' +export * from './api' diff --git a/src/release.ts b/src/release.ts index 803d76a..1c2dd69 100644 --- a/src/release.ts +++ b/src/release.ts @@ -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') } diff --git a/types/index.d.ts b/src/types.d.ts similarity index 93% rename from types/index.d.ts rename to src/types.d.ts index 7a8d8f3..5345f49 100644 --- a/types/index.d.ts +++ b/src/types.d.ts @@ -51,10 +51,10 @@ export interface ActivityResponse { timeline: Array positiveReasons: Record negative_reasons: Record - other_positive: Array - other_negative: Array - other_positive_count: Array - other_negative_count: Array + other_positive: Array + other_negative: Array + other_positive_count: Array + other_negative_count: Array } export interface GetBehaviourOptions { from: string | undefined @@ -114,11 +114,11 @@ export interface Homework { allow_attachments: string first_seen_date: string last_seen_date: string - attachments: Array + attachments: Array has_feedback: boolean } - validated_links: Array - validated_attachments: Array + validated_links: Array + validated_attachments: Array } export type HomeworksResponse = Array export interface GetLessonsOptions { diff --git a/tsconfig.json b/tsconfig.json index 313538b..0e9c292 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -87,6 +87,6 @@ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ + // "skipLibCheck": true /* Skip type checking all .d.ts files. */ } } \ No newline at end of file