parent
2da522f3e4
commit
2ad7523a1d
|
@ -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
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -10,6 +10,7 @@ describe('login', () => {
|
|||
response = {
|
||||
json: jest.fn(),
|
||||
text: jest.fn(),
|
||||
blob: jest.fn(),
|
||||
headers,
|
||||
}
|
||||
fetch = jest.fn().mockResolvedValue(response)
|
||||
|
|
|
@ -14,6 +14,7 @@ export interface Response {
|
|||
headers: Headers
|
||||
text: () => Promise<string>
|
||||
json: () => Promise<any>
|
||||
blob: () => Promise<Blob>
|
||||
}
|
||||
|
||||
export interface Fetch {
|
||||
|
|
19
run.js
19
run.js
|
@ -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()
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue