Skip to content

Commit

Permalink
feat: ๐ŸŽธ Loads schedule (#16)
Browse files Browse the repository at this point in the history
* feat: ๐ŸŽธ Loads schedule

โœ… Closes: #13

* feat: ๐ŸŽธ Made running code from node easier

* feat: ๐ŸŽธ News

โœ… Closes: #8

* Accurate method calls and some parsing

* fix: ๐ŸŽธ Calendar and classmates are now called and parsed correctly
  • Loading branch information
JohanObrink committed Dec 21, 2020
1 parent 5996f1d commit 53d42de
Show file tree
Hide file tree
Showing 12 changed files with 644 additions and 56 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Expand Up @@ -102,5 +102,3 @@ dist

# TernJS port file
.tern-port

run.js
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -81,3 +81,11 @@ const sessionCookie = "some value";

api.setSessionCookie(sessionCookie); // will trigger `on('login')` event and set `.isLoggedIn = true`
```
## Try it out
1. Clone and enter repo: `git clone git@github.com:kolplattformen/embedded-api.git && cd embedded-api`
2. Install dependencies: `yarn`
3. Build package: `yarn build`
4. Run example: `node run [your personal number]`
5. Sign in with mobile BankID
18 changes: 15 additions & 3 deletions lib/children.ts
@@ -1,8 +1,11 @@
import { Moment } from 'moment'
import routes from './routes'
import {
CalendarItem, Child, Classmate, Fetch, RequestInit,
} from './types'
import { etjanst, child, calendarItem } from './parse'
import {
etjanst, child, calendarItem,
} from './parse'

export const list = (fetch: Fetch, init?: RequestInit) => async (): Promise<Child[]> => {
const url = routes.children
Expand All @@ -11,16 +14,25 @@ export const list = (fetch: Fetch, init?: RequestInit) => async (): Promise<Chil
return etjanst(data).map(child)
}

export const calendar = (fetch: Fetch, init?:RequestInit) => async (childId: string): Promise<CalendarItem[]> => {
export const calendar = (fetch: Fetch, init?: RequestInit) => async (childId: string): Promise<CalendarItem[]> => {
const url = routes.calendar(childId)
const response = await fetch(url, init)
const data = await response.json()
return etjanst(data).map(calendarItem)
}

export const classmates = (fetch: Fetch, init?:RequestInit) => async (childId: string): Promise<Classmate[]> => {
export const classmates = (fetch: Fetch, init?: RequestInit) => async (childId: string): Promise<Classmate[]> => {
const url = routes.classmates(childId)
const response = await fetch(url, init)
const data = await response.json()
return etjanst(data)
}

export const schedule = (fetch: Fetch, init?: RequestInit) => (
async (childId: string, from: Moment, to: moment.Moment): Promise<any> => {
const url = routes.schedule(childId, from.format('YYYY-MM-DD'), to.format('YYYY-MM-DD'))
const response = await fetch(url, init)
const data = await response.json()
return etjanst(data)
}
)
1 change: 1 addition & 0 deletions lib/h2m.d.ts
@@ -0,0 +1 @@
declare module 'h2m'
24 changes: 19 additions & 5 deletions lib/index.ts
@@ -1,11 +1,15 @@
import { Moment } from 'moment'
import { EventEmitter } from 'events'
import {
checkStatus, getSessionCookie, login, LoginStatus,
} from './login'
import {
CalendarItem, Child, Classmate, Fetch, RequestInit,
} from './types'
import { calendar, classmates, list } from './children'
import {
calendar, classmates, list, schedule,
} from './children'
import { news, News } from './news'

interface AsyncishFunction { (): void | Promise<void> }

Expand Down Expand Up @@ -50,13 +54,23 @@ export class Api extends EventEmitter {
return data
}

async getCalendar(childId: string): Promise<CalendarItem[]> {
const data = await calendar(this.fetch, this.session)(childId)
async getCalendar(child: Child): Promise<CalendarItem[]> {
const data = await calendar(this.fetch, this.session)(child.id)
return data
}

async getClassmates(child: Child): Promise<Classmate[]> {
const data = await classmates(this.fetch, this.session)(child.sdsId)
return data
}

async getSchedule(child: Child, from: Moment, to: Moment): Promise<any> {
const data = await schedule(this.fetch, this.session)(child.sdsId, from, to)
return data
}

async getClassmates(childId: string): Promise<Classmate[]> {
const data = await classmates(this.fetch, this.session)(childId)
async getNews(child: Child): Promise<News> {
const data = await news(this.fetch, this.session)(child.id)
return data
}

Expand Down
18 changes: 18 additions & 0 deletions lib/news.ts
@@ -0,0 +1,18 @@
import { etjanst, newsItem } from './parse'
import routes from './routes'
import { Fetch, NewsItem, RequestInit } from './types'

export class News {
public items: NewsItem[]

constructor(items: NewsItem[]) {
this.items = items
}
}

export const news = (fetch: Fetch, init?: RequestInit) => async (childId: string): Promise<News> => {
const url = routes.news(childId)
const response = await fetch(url, init)
const data = await response.json()
return new News(etjanst(data).newsItems.map(newsItem))
}

0 comments on commit 53d42de

Please sign in to comment.