feat: 🎸 Image (#21)

* feat: 🎸 Image

 Closes: #10
This commit is contained in:
Johan Öbrink 2020-12-21 19:48:50 +01:00 committed by GitHub
parent 2da522f3e4
commit 2ad7523a1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 6 deletions

9
lib/image.ts Normal file
View File

@ -0,0 +1,9 @@
import routes from './routes'
import { Fetch, RequestInit } from './types'
export const image = (fetch: Fetch, init?: RequestInit) => async (imageUrl: string): Promise<Blob> => {
const url = routes.image(imageUrl)
const response = await fetch(url, init)
const data = await response.blob()
return data
}

View File

@ -11,6 +11,7 @@ import {
} from './children'
import { news, News } from './news'
import { user } from './user'
import { image } from './image'
interface AsyncishFunction { (): void | Promise<void> }
@ -90,6 +91,11 @@ export class Api extends EventEmitter {
return data
}
async getImage(imageUrl: string): Promise<Blob> {
const data = await image(this.fetch, this.session)(imageUrl)
return data
}
async logout() {
this.session = undefined
await this.clearCookies()

View File

@ -10,6 +10,7 @@ describe('login', () => {
response = {
json: jest.fn(),
text: jest.fn(),
blob: jest.fn(),
headers,
}
fetch = jest.fn().mockResolvedValue(response)

View File

@ -14,6 +14,7 @@ export interface Response {
headers: Headers
text: () => Promise<string>
json: () => Promise<any>
blob: () => Promise<Blob>
}
export interface Fetch {

19
run.js
View File

@ -48,17 +48,24 @@ async function run() {
// const schedule = await api.getSchedule(children[0], moment().subtract(1, 'week'), moment())
// console.log(schedule)
// console.log('news')
// const news = await api.getNews(children[0])
// console.log(news)
console.log('news')
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)
// const arrayBuffer = await blob.arrayBuffer()
// console.log(`data:${blob.type};base64,${Buffer.from(arrayBuffer).toString('base64')}`)
// console.log('menu')
// const menu = await api.getMenu(children[0])
// console.log(menu)
console.log('notifications')
const notifications = await api.getNotifications(children[0])
console.log(notifications)
// console.log('notifications')
// const notifications = await api.getNotifications(children[0])
// console.log(notifications)
await api.logout()
})