From 2ad7523a1ded17bbb20b491cd54cd9c960c825a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20=C3=96brink?= Date: Mon, 21 Dec 2020 19:48:50 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Image=20(#21)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 🎸 Image ✅ Closes: #10 --- lib/image.ts | 9 +++++++++ lib/index.ts | 6 ++++++ lib/login.test.ts | 1 + lib/types.ts | 1 + run.js | 19 +++++++++++++------ 5 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 lib/image.ts diff --git a/lib/image.ts b/lib/image.ts new file mode 100644 index 000000000..85f175912 --- /dev/null +++ b/lib/image.ts @@ -0,0 +1,9 @@ +import routes from './routes' +import { Fetch, RequestInit } from './types' + +export const image = (fetch: Fetch, init?: RequestInit) => async (imageUrl: string): Promise => { + const url = routes.image(imageUrl) + const response = await fetch(url, init) + const data = await response.blob() + return data +} diff --git a/lib/index.ts b/lib/index.ts index 7985a6187..a5cda8466 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -11,6 +11,7 @@ import { } from './children' import { news, News } from './news' import { user } from './user' +import { image } from './image' interface AsyncishFunction { (): void | Promise } @@ -90,6 +91,11 @@ export class Api extends EventEmitter { return data } + async getImage(imageUrl: string): Promise { + const data = await image(this.fetch, this.session)(imageUrl) + return data + } + async logout() { this.session = undefined await this.clearCookies() diff --git a/lib/login.test.ts b/lib/login.test.ts index 02f95b2c6..959e47b66 100644 --- a/lib/login.test.ts +++ b/lib/login.test.ts @@ -10,6 +10,7 @@ describe('login', () => { response = { json: jest.fn(), text: jest.fn(), + blob: jest.fn(), headers, } fetch = jest.fn().mockResolvedValue(response) diff --git a/lib/types.ts b/lib/types.ts index 1e50bbff2..8c10d5bb6 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -14,6 +14,7 @@ export interface Response { headers: Headers text: () => Promise json: () => Promise + blob: () => Promise } export interface Fetch { diff --git a/run.js b/run.js index a97078995..862599d19 100644 --- a/run.js +++ b/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() })