Skip to content

Commit

Permalink
feat: 🎸 Image (kolplattformen#21)
Browse files Browse the repository at this point in the history
* feat: 🎸 Image

✅ Closes: kolplattformen#10
  • Loading branch information
JohanObrink authored Dec 21, 2020
1 parent 2da522f commit 2ad7523
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/image.ts
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> }

Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions lib/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('login', () => {
response = {
json: jest.fn(),
text: jest.fn(),
blob: jest.fn(),
headers,
}
fetch = jest.fn().mockResolvedValue(response)
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Response {
headers: Headers
text: () => Promise<string>
json: () => Promise<any>
blob: () => Promise<Blob>
}

export interface Fetch {
Expand Down
19 changes: 13 additions & 6 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down

0 comments on commit 2ad7523

Please sign in to comment.