feat: 🎸 getSessionCookie and removed News object (#24)

* feat: 🎸 getSessionCookie

 Closes: #22

* feat: 🎸 Removed unnecessary News object wrapper

BREAKING CHANGE: 🧨 Call signature of getNews changed

 Closes: #23
This commit is contained in:
Johan Öbrink 2020-12-21 20:09:21 +01:00 committed by GitHub
parent 18434e0cde
commit 91ba6833b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 16 deletions

View File

@ -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', () => {
@ -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

View File

@ -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'
@ -30,6 +30,10 @@ export class Api extends EventEmitter {
this.clearCookies = clearCookies
}
getSessionCookie() {
return this.session?.headers?.Cookie
}
setSessionCookie(cookie: string) {
this.session = {
headers: {
@ -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
}

View File

@ -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
run.js
View File

@ -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)
@ -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')}`)