From a64fd6a718e7195c670b79f5a6ce08629d7b1de6 Mon Sep 17 00:00:00 2001 From: James Cook Date: Wed, 10 Jan 2024 10:57:33 +0000 Subject: [PATCH] chore: syntax & biome.js changes --- .github/workflows/lint.yml | 3 --- .github/workflows/test.yml | 4 ---- biome.json | 16 ++++++++++++- scripts/build_npm.ts | 4 +++- src/core/baseClient.ts | 16 ++++++++----- src/core/parentClient.ts | 16 +++++++++---- src/core/studentClient.ts | 4 +++- src/types.ts | 46 +++++++++++++++++++------------------- 8 files changed, 67 insertions(+), 42 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 46a3f9c..5300cad 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,10 +2,7 @@ name: test on: push: branches: - - main pull_request: - branches: - - main jobs: lint: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cb4e8f8..fc3be1d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,11 +1,7 @@ name: test on: push: - branches: - - main pull_request: - branches: - - main jobs: test-deno: diff --git a/biome.json b/biome.json index bfd6919..7cbb3c0 100644 --- a/biome.json +++ b/biome.json @@ -6,7 +6,21 @@ "linter": { "enabled": true, "rules": { - "recommended": true + "recommended": true, + "complexity": { + "noExcessiveCognitiveComplexity": "warn" + }, + "correctness": { + "noUnusedVariables": "warn" + }, + "style": { + "useBlockStatements": "warn", + "useShorthandArrayType": "warn", + "useShorthandAssign": "warn" + }, + "suspicious": { + "noApproximativeNumericConstant": "error" + } } }, "files": { diff --git a/scripts/build_npm.ts b/scripts/build_npm.ts index 71f8508..b30ec27 100644 --- a/scripts/build_npm.ts +++ b/scripts/build_npm.ts @@ -1,7 +1,9 @@ // This dependancy cannot be moved to dev_deps.ts since dnt complains about a top-level await import { build, emptyDir } from "https://deno.land/x/dnt@0.39.0/mod.ts"; -if (!Deno.args[0]) throw new Error("No version specified"); +if (!Deno.args[0]) { + throw new Error("No version specified"); +} await emptyDir("./npm"); diff --git a/src/core/baseClient.ts b/src/core/baseClient.ts index 72e8bee..c957081 100644 --- a/src/core/baseClient.ts +++ b/src/core/baseClient.ts @@ -22,7 +22,7 @@ import { PING_INTERVAL } from "../utils/consts.ts"; /** * Shared client for both parent and student. This is not exported and should not be used directly */ -export class BaseClient { +export abstract class BaseClient { /** * @property studentId Currently selected student ID */ @@ -30,7 +30,7 @@ export class BaseClient { /** * @property authCookies Cookies used for authentication (set during login and can be empty) */ - public authCookies: Array; + public authCookies: string[]; /** * @property sessionId Session ID used for authentication */ @@ -50,6 +50,8 @@ export class BaseClient { this.authCookies = []; this.API_BASE = API_BASE; } + + abstract login(): Promise; /** * Revalidates the session ID. * @@ -82,9 +84,11 @@ export class BaseClient { public async makeAuthedRequest( path: string, fetchOptions: RequestInit, - options: { revalidateToken?: boolean } = { revalidateToken: true }, + options: { revalidateToken?: boolean; } = { revalidateToken: true }, ) { - if (!this.sessionId) throw new Error("No session ID"); + if (!this.sessionId) { + throw new Error("No session ID"); + } if (typeof options?.revalidateToken === "undefined") { options.revalidateToken = true; } @@ -226,7 +230,9 @@ export class BaseClient { * @returns Array of lessons */ async getLessons(options: GetLessonsOptions): Promise { - if (!options?.date) throw new Error("No date specified"); + if (!options?.date) { + throw new Error("No date specified"); + } const params = new URLSearchParams(); params.append("date", String(options?.date)); return await this.makeAuthedRequest( diff --git a/src/core/parentClient.ts b/src/core/parentClient.ts index a605bec..b0a874b 100644 --- a/src/core/parentClient.ts +++ b/src/core/parentClient.ts @@ -25,8 +25,12 @@ export class ParentClient extends BaseClient { * Authenticates with ClassCharts */ async login(): Promise { - if (!this.email) throw new Error("Email not provided"); - if (!this.password) throw new Error("Password not provided"); + if (!this.email) { + throw new Error("Email not provided"); + } + if (!this.password) { + throw new Error("Password not provided"); + } const formData = new URLSearchParams(); formData.append("_method", "POST"); formData.append("email", this.email); @@ -57,7 +61,9 @@ export class ParentClient extends BaseClient { ); this.sessionId = sessionID.session_id; this.pupils = await this.getPupils(); - if (!this.pupils) throw new Error("Account has no pupils attached"); + if (!this.pupils) { + throw new Error("Account has no pupils attached"); + } this.studentId = this.pupils[0].id; } /** @@ -77,7 +83,9 @@ export class ParentClient extends BaseClient { * @see getPupils */ selectPupil(pupilId: number) { - if (!pupilId) throw new Error("No pupil ID specified"); + if (!pupilId) { + throw new Error("No pupil ID specified"); + } const pupils = this.pupils; for (let i = 0; i < pupils.length; i++) { const pupil = pupils[i]; diff --git a/src/core/studentClient.ts b/src/core/studentClient.ts index b4264f5..db02a1c 100644 --- a/src/core/studentClient.ts +++ b/src/core/studentClient.ts @@ -35,7 +35,9 @@ export class StudentClient extends BaseClient { * Authenticates with ClassCharts */ async login(): Promise { - if (!this.studentCode) throw new Error("Student Code not provided"); + if (!this.studentCode) { + throw new Error("Student Code not provided"); + } const formData = new URLSearchParams(); formData.append("_method", "POST"); formData.append("code", this.studentCode.toUpperCase()); diff --git a/src/types.ts b/src/types.ts index 7e42d3a..7163d75 100644 --- a/src/types.ts +++ b/src/types.ts @@ -77,13 +77,13 @@ export interface BehaviourTimelinePoint { end: string; } export interface BehaviourResponseData { - timeline: Array; + timeline: BehaviourTimelinePoint[]; positive_reasons: Record; negative_reasons: Record; - other_positive: Array; - other_negative: Array; - other_positive_count: Array>; - other_negative_count: Array>; + other_positive: string[]; + other_negative: string[]; + other_positive_count: Record[]; + other_negative_count: Record[]; } export interface BehaviourResponseMeta { start_date: string; @@ -134,7 +134,7 @@ export interface ActivityPoint { detention_location: string | null; detention_type: string | null; } -export type ActivityResponseData = Array; +export type ActivityResponseData = ActivityPoint[]; interface ActivityResponseMeta { start_date: string; end_date: string; @@ -194,13 +194,13 @@ export interface Homework { allow_attachments: boolean; first_seen_date: string | null; last_seen_date: string | null; - attachments: Array; + attachments: unknown[]; has_feedback: boolean; }; - validated_links: Array; - validated_attachments: Array; + validated_links: unknown[]; + validated_attachments: ValidatedHomeworkAttachment[]; } -export type HomeworksResponseData = Array; +export type HomeworksResponseData = Homework[]; export interface HomeworksResponseMeta { start_date: string; end_date: string; @@ -286,10 +286,10 @@ export interface Badge { icon: string; colour: string; created_date: string; - pupil_badges: Array; + pupil_badges: PupilEvent[]; icon_url: string; } -export type BadgesResponseData = Array; +export type BadgesResponseData = Badge[]; export type BadgesResponseMeta = []; export type BadgesResponse = ClassChartsResponse< BadgesResponseData, @@ -336,7 +336,7 @@ export interface Detention { }; } -export type DetentionsData = Array; +export type DetentionsData = Detention[]; export interface DetentionsMeta { detention_alias_plural: string; @@ -357,11 +357,11 @@ export interface Announcement { sticky: "yes" | "no"; state: string | null; timestamp: string; - attachments: Array<{ + attachments: { filename: string; url: string; - }>; - for_pupils: Array; + }[]; + for_pupils: unknown[]; comment_visibility: string; allow_comments: "yes" | "no"; allow_reactions: "yes" | "no"; @@ -370,11 +370,11 @@ export interface Announcement { requires_consent: "yes" | "no"; can_change_consent: boolean; consent: unknown | null; - pupil_consents: Array; + pupil_consents: unknown[]; } export type AnnouncementsResponse = ClassChartsResponse< - Array, + Announcement[], [] >; @@ -397,7 +397,7 @@ export interface Pupil extends Student { announcements_count: number; messages_count: number; } -export type GetPupilsResponse = Array; +export type GetPupilsResponse = Pupil[]; export interface GetFullActivityOptions { /** @@ -430,8 +430,8 @@ export interface AttendancePeriod { } export interface AttendanceMeta { - dates: Array; - sessions: Array; + dates: string[]; + sessions: string[]; start_date: string; end_date: string; percentage: string; @@ -480,12 +480,12 @@ export type RewardPurchaseResponse = ClassChartsResponse< export interface PupilFieldsData { note: string; - fields: Array<{ + fields: { id: number; name: string; graphic: string; value: string; - }>; + }[]; } export type PupilFieldsResponse = ClassChartsResponse;