Skip to content

Commit

Permalink
feat: 🎸 getSessionCookie and removed News object (kolplattformen#24)
Browse files Browse the repository at this point in the history
* feat: 🎸 getSessionCookie

✅ Closes: kolplattformen#22

* feat: 🎸 Removed unnecessary News object wrapper

BREAKING CHANGE: 🧨 Call signature of getNews changed

✅ Closes: kolplattformen#23
  • Loading branch information
JohanObrink authored Dec 21, 2020
1 parent 18434e0 commit 91ba683
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const api = init(fetch, () => cookieJar.removeAllCookies());
api.on("login", async () => {
// do stuff
console.log(api.isLoggedIn) // true
console.log(api.getSessionCookie) // session cookie if you want to save it
await api.logout()
});
api.on('logout', () => {
Expand Down Expand Up @@ -90,6 +91,10 @@ const menu = await api.getMenu(children[0])

// Get notifications
const notifications = await api.getNotifications(children[0])

// Get image
const blob = await api.getImage(news[0].imageUrl)
const src = URL.createObjectURL(blob)
```
### Setting session cookie
Expand Down
10 changes: 7 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
checkStatus, getSessionCookie, login, LoginStatus,
} from './login'
import {
CalendarItem, Child, Classmate, Fetch, MenuItem, RequestInit,
CalendarItem, Child, Classmate, Fetch, MenuItem, NewsItem, RequestInit,
} from './types'
import {
calendar, classmates, list, menu, notifications, schedule,
} from './children'
import { news, News } from './news'
import { news } from './news'
import { user } from './user'
import { image } from './image'

Expand All @@ -30,6 +30,10 @@ export class Api extends EventEmitter {
this.clearCookies = clearCookies
}

getSessionCookie() {
return this.session?.headers?.Cookie
}

setSessionCookie(cookie: string) {
this.session = {
headers: {
Expand Down Expand Up @@ -76,7 +80,7 @@ export class Api extends EventEmitter {
return data
}

async getNews(child: Child): Promise<News> {
async getNews(child: Child): Promise<NewsItem[]> {
const data = await news(this.fetch, this.session)(child.id)
return data
}
Expand Down
12 changes: 2 additions & 10 deletions lib/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ 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> => {
export const news = (fetch: Fetch, init?: RequestInit) => async (childId: string): Promise<NewsItem[]> => {
const url = routes.news(childId)
const response = await fetch(url, init)
const data = await response.json()
return new News(etjanst(data).newsItems.map(newsItem))
return etjanst(data).newsItems.map(newsItem)
}
8 changes: 5 additions & 3 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ async function run() {
api.on('login', async () => {
console.log('Logged in')

console.log(api.getSessionCookie())

// console.log('user')
// const user = await api.getUser()
// console.log(user)
Expand All @@ -52,9 +54,9 @@ async function run() {
const news = await api.getNews(children[0])
console.log(news)

console.log('image')
const blob = await api.getImage(news.items[0].imageUrl)
console.log(blob)
// console.log('image')
// const blob = await api.getImage(news[0].imageUrl)
// console.log(blob)

// const arrayBuffer = await blob.arrayBuffer()
// console.log(`data:${blob.type};base64,${Buffer.from(arrayBuffer).toString('base64')}`)
Expand Down

0 comments on commit 91ba683

Please sign in to comment.