Skip to content

Commit

Permalink
feat: 🎸 Added language support (kolplattformen#121)
Browse files Browse the repository at this point in the history
* feat: 🎸 Added language support

BREAKING CHANGE: 🧨 getTimetable requires language
  • Loading branch information
JohanObrink authored Apr 26, 2021
1 parent 766f4ff commit 9dcdf78
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 242 deletions.
2 changes: 1 addition & 1 deletion lib/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('api', () => {
const skola24Children = await api.getSkola24Children()
expect(skola24Children).toHaveLength(1)

const timetable = await api.getTimetable(skola24Children[0], 2021, 15)
const timetable = await api.getTimetable(skola24Children[0], 2021, 15, 'sv')
expect(timetable).toHaveLength(32)

done()
Expand Down
7 changes: 4 additions & 3 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DateTime } from 'luxon'
import { EventEmitter } from 'events'
import { decode } from 'he'
import * as html from 'node-html-parser'
import { Language } from '@skolplattformen/curriculum/dist/translations'
import { URLSearchParams } from './URLSearchParams'
import { checkStatus, LoginStatusChecker } from './loginStatus'
import {
Expand Down Expand Up @@ -354,7 +355,7 @@ export class Api extends EventEmitter {
public async getSchedule(
child: EtjanstChild,
from: DateTime,
to: DateTime
to: DateTime,
): Promise<ScheduleItem[]> {
if (this.isFake) return fakeResponse(fake.schedule(child))

Expand Down Expand Up @@ -515,7 +516,7 @@ export class Api extends EventEmitter {
return key as string
}

public async getTimetable(child: Skola24Child, week: number, year: number): Promise<any> {
public async getTimetable(child: Skola24Child, week: number, year: number, lang: Language): Promise<any> {
if (this.isFake) return fakeResponse(fake.timetable(child))

const url = routes.timetable
Expand Down Expand Up @@ -548,7 +549,7 @@ export class Api extends EventEmitter {
const response = await this.fetch(`timetable_${child.personGuid}_${year}_${week}`, url, session)
const json = await response.json()

return parse.timetable(json, year, week)
return parse.timetable(json, year, week, lang)
}

public async logout() {
Expand Down
8 changes: 4 additions & 4 deletions lib/parse/__tests__/timetable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('Timetable', () => {
})
describe('timetableEntry', () => {
it('parses basic timeTableEntry data correctly', () => {
const entry = timetableEntry(response.data.lessonInfo[1], 2021, 15)
const entry = timetableEntry(response.data.lessonInfo[1], 2021, 15, 'sv')

expect(entry.id).toEqual('ZTQ1NWE0N2EtNzAwOS0wZTAzLTQ1ZDYtNTA1NWI4Y2JhNDYw')
expect(entry.code).toEqual('BL')
Expand All @@ -127,7 +127,7 @@ describe('Timetable', () => {
expect(entry.blockName).toEqual('block')
})
it('parses dates correctly', () => {
const entry = timetableEntry(response.data.lessonInfo[1], 2021, 15)
const entry = timetableEntry(response.data.lessonInfo[1], 2021, 15, 'sv')

expect(entry.dateStart).toEqual('2021-04-12T09:40:00.000+02:00')
expect(entry.dateEnd).toEqual('2021-04-12T11:35:00.000+02:00')
Expand All @@ -136,10 +136,10 @@ describe('Timetable', () => {
describe('timetable', () => {
it('throws error', () => {
response.error = 'b0rk'
expect(() => timetable(response, 2021, 15)).toThrow('b0rk')
expect(() => timetable(response, 2021, 15, 'sv')).toThrow('b0rk')
})
it('parses lessonInfo', () => {
const table = timetable(response, 2021, 15)
const table = timetable(response, 2021, 15, 'sv')

expect(table).toHaveLength(2)
expect(table[0].id).toEqual('N2FjMDc1NjYtZmM2Yy0wZDQyLTY3M2YtZWI5NGNiZDA3ZGU4')
Expand Down
11 changes: 6 additions & 5 deletions lib/parse/timetable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import parse from '@skolplattformen/curriculum'
import { Language } from '@skolplattformen/curriculum/dist/translations'
import { DateTime } from 'luxon'
import { TimetableEntry } from '../types'

Expand Down Expand Up @@ -37,12 +38,12 @@ export interface TimetableResponse {
}

interface EntryParser {
(args: TimetableResponseEntry, year: number, week: number): TimetableEntry
(args: TimetableResponseEntry, year: number, week: number, lang: Language): TimetableEntry
}
export const timetableEntry: EntryParser = ({
guidId, texts: [code, teacher, location], timeStart, timeEnd, dayOfWeekNumber, blockName,
}, year, week) => ({
...parse(code),
}, year, week, lang) => ({
...parse(code, lang),
id: guidId,
blockName,
dayOfWeek: dayOfWeekNumber,
Expand All @@ -54,9 +55,9 @@ export const timetableEntry: EntryParser = ({
dateEnd: calculateDate(year, week, dayOfWeekNumber, timeEnd),
})

export const timetable = (response: TimetableResponse, year: number, week: number) => {
export const timetable = (response: TimetableResponse, year: number, week: number, lang: Language) => {
if (response.error) {
throw new Error(response.error)
}
return response.data.lessonInfo.map((entry) => timetableEntry(entry, year, week))
return response.data.lessonInfo.map((entry) => timetableEntry(entry, year, week, lang))
}
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@
"publish-package": "npm publish --access public"
},
"devDependencies": {
"@react-native-cookies/cookies": "^6.0.4",
"@types/jest": "^26.0.19",
"@types/luxon": "^1.25.0",
"@types/node-fetch": "^2.5.7",
"@react-native-cookies/cookies": "^6.0.7",
"@types/he": "^1.1.1",
"@types/jest": "^26.0.22",
"@types/luxon": "^1.26.4",
"@types/node-fetch": "^2.5.10",
"@types/tough-cookie": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^4.10.0",
"@typescript-eslint/parser": "^4.10.0",
"eslint": "^7.16.0",
"eslint-config-airbnb-typescript": "^12.0.0",
"eslint-config-prettier": "^8.1.0",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"eslint": "^7.24.0",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-config-prettier": "^8.2.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-prettier": "^3.4.0",
"fetch-cookie": "^0.11.0",
"https-proxy-agent": "^5.0.0",
"jest": "^26.6.3",
"node-blob": "^0.0.2",
"node-fetch": "^2.6.1",
"prettier": "^2.2.1",
"tough-cookie": "^4.0.0",
"ts-jest": "^26.4.4",
"typescript": "^4.1.3"
"ts-jest": "^26.5.5",
"typescript": "^4.2.4"
},
"dependencies": {
"@skolplattformen/curriculum": "^1.2.0",
"@types/he": "^1.1.1",
"@skolplattformen/curriculum": "^1.3.0",
"camelcase-keys": "^6.2.2",
"change-case": "^4.1.2",
"events": "^3.2.0",
"events": "^3.3.0",
"h2m": "^0.7.0",
"he": "^1.2.0",
"js-htmlencode": "^0.3.0",
"luxon": "^1.25.0",
"luxon": "^1.26.0",
"node-html-parser": "^2.1.0"
}
}
Loading

0 comments on commit 9dcdf78

Please sign in to comment.