From 4208fec4707b2412b25cb27d2b84562a3be6af76 Mon Sep 17 00:00:00 2001 From: Rango Ack Date: Tue, 26 Sep 2023 13:20:18 +0800 Subject: [PATCH] add a news api --- content/links.json | 2 +- pages/index.vue | 8 +++++--- server/api/news.ts | 8 ++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 server/api/news.ts diff --git a/content/links.json b/content/links.json index 7a05152..536f6b6 100644 --- a/content/links.json +++ b/content/links.json @@ -44,7 +44,7 @@ "tryit": "https://bridge-buidl.vite.net/" }, "news": { - "api": "https://vite.org/api/discover_en.json", + "api": "/api/news", "home": "https://medium.com/vitelabs", "weeklyReport": "https://medium.com/vitelabs/tagged/project-updates", "ann": "https://medium.com/vitelabs/announcements/home", diff --git a/pages/index.vue b/pages/index.vue index 2ef7045..a48f45b 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -14,14 +14,15 @@ const news = reactive([]); onMounted(async () => { try { - const { data }: {data: any} = await useFetch(links.news.api); + const resp = await fetch(links.news.api); + const data = await resp.json(); let n = 9; if (isMobile) { n = 5; } - if (data.value) { - const found = data.value.tags?.find((x: any) => x.tag === 'news'); + if (data) { + const found = data.tags?.find((x: any) => x.tag === 'news'); if (found) { const list = found.list.slice(0, n).map((x: any) => { const d = new Date(x.createTime * 1000); @@ -36,6 +37,7 @@ onMounted(async () => { } } } catch (e) { + console.log(e); /* handle error */ } }) diff --git a/server/api/news.ts b/server/api/news.ts new file mode 100644 index 0000000..d0a1817 --- /dev/null +++ b/server/api/news.ts @@ -0,0 +1,8 @@ +export default defineEventHandler(async (event) => { + setHeaders(event, { + 'Content-Type': 'application/json; charset=utf-8' + }); + const response = await fetch('https://static.vite.net//testnet-vite-1257137467/discover/discover_en.json'); + const body = await response.text(); + return body; +})