1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 03:56:59 +00:00

feat: i know how to use optional arguments

This commit is contained in:
James Cook 2021-11-23 22:23:23 +00:00
parent d0ed8b1cd7
commit 0f8f0a5027
23 changed files with 110 additions and 82 deletions

View file

@ -16,39 +16,39 @@ import {
} from './types'
export async function getStudentInfo(
studentCode: string,
dateOfBirth: string | null
dateOfBirth?: string
): Promise<Student> {
const client = new ClasschartsClient(studentCode, dateOfBirth)
return await client.getStudentInfo()
}
export async function getActivity(
studentCode: string,
dateOfBirth: string | null,
options: GetActivityOptions | null
dateOfBirth?: string,
options?: GetActivityOptions
): 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
dateOfBirth?: string,
options?: GetBehaviourOptions
): 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
dateOfBirth?: string,
options?: GetHomeworkOptions
): Promise<HomeworksResponse> {
const client = new ClasschartsClient(studentCode, dateOfBirth)
return await client.listHomeworks(options)
}
export async function getLessons(
studentCode: string,
dateOfBirth: string | null,
options: GetLessonsOptions
dateOfBirth?: string,
options?: GetLessonsOptions
): Promise<LessonsResponse> {
const client = new ClasschartsClient(studentCode, dateOfBirth)
return await client.getLessons(options)

View file

@ -28,7 +28,7 @@ export class ClasschartsClient {
* @param studentCode Classcharts student code
* @param dateOfBirth Student's date of birth
*/
constructor(studentCode: string, dateOfBirth: string) {
constructor(studentCode: string, dateOfBirth?: string) {
this.studentCode = String(studentCode)
this.dateOfBirth = String(dateOfBirth)
}
@ -110,7 +110,7 @@ export class ClasschartsClient {
* @returns Activity data
*/
async getActivity(
options: GetActivityOptions | null
options?: GetActivityOptions
): Promise<ActivityResponse> {
const params = new URLSearchParams()
options?.from && params.append('form', options?.from)
@ -128,7 +128,7 @@ export class ClasschartsClient {
* @returns Array of behaviour points
*/
async getBehaviour(
options: GetBehaviourOptions | null
options?: GetBehaviourOptions
): Promise<BehaviourResponse> {
const params = new URLSearchParams()
options?.from && params.append('form', options?.from)
@ -147,7 +147,7 @@ export class ClasschartsClient {
* @returns Array of homeworks
*/
async listHomeworks(
options: GetHomeworkOptions | null
options?: GetHomeworkOptions
): Promise<HomeworksResponse> {
if (!this.authCookies) throw new Error('Not authenticated')
const params = new URLSearchParams()
@ -176,7 +176,7 @@ export class ClasschartsClient {
* @param options GetLessonsOptions
* @returns Array of lessons
*/
async getLessons(options: GetLessonsOptions): 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()

View file

@ -1,29 +0,0 @@
import fs from 'fs'
// DO NOT DELETE THIS FILE
// This file is used by build system to build a clean npm package with the compiled js files in the root of the package.
// It will not be included in the npm package.
function main() {
const source = fs
.readFileSync(__dirname + '/../package.json')
.toString('utf-8')
const sourceObj = JSON.parse(source)
sourceObj.scripts = {}
sourceObj.devDependencies = {}
if (sourceObj.main.startsWith('dist/')) {
sourceObj.main = sourceObj.main.slice(5)
}
fs.writeFileSync(
__dirname + '/package.json',
Buffer.from(JSON.stringify(sourceObj, null, 2), 'utf-8')
)
fs.writeFileSync(
__dirname + '/version.txt',
Buffer.from(sourceObj.version, 'utf-8')
)
fs.copyFileSync(__dirname + '/../.npmignore', __dirname + '/.npmignore')
fs.copyFileSync(__dirname + '/../README.MD', __dirname + '/README.MD')
}
main()

View file

@ -1,10 +1,10 @@
import { ClasschartsClient } from '../client'
const { code, dob } = require('../../src/tests/config.json')
async function main() {
const client = new ClasschartsClient(code, dob)
await client.init()
console.log(await client.getBehaviour(null))
console.log(await client.getActivity(null))
const client = new ClasschartsClient(code, dob)
await client.init()
console.log(await client.getBehaviour())
console.log(await client.getActivity())
}
main()

10
src/types.d.ts vendored
View file

@ -37,8 +37,8 @@ export interface Student {
detention_alias_plural_uc: string
}
export interface GetActivityOptions {
from: string | undefined
to: string | undefined
from?: string
to?: string
}
export interface ActivityTimelinePoint {
positive: number
@ -57,9 +57,9 @@ export interface ActivityResponse {
other_negative_count: Array<any>
}
export interface GetBehaviourOptions {
from: string | undefined
to: string | undefined
last_id: string | undefined
from?: string
to?: string
last_id?: string
}
export interface BehaviourPoint {
id: number