From e808f8b210be482872a315720ce4a2e417024caa Mon Sep 17 00:00:00 2001 From: aha2mao Date: Mon, 27 May 2019 11:06:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20=E8=AF=AD=E9=9B=80?= =?UTF-8?q?=E7=9F=A5=E8=AF=86=E5=BA=93link=20404=E6=8A=A5=E9=94=99=20(#223?= =?UTF-8?q?0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/yuque/doc.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/routes/yuque/doc.js b/lib/routes/yuque/doc.js index 46577434e6..a4a09226bf 100644 --- a/lib/routes/yuque/doc.js +++ b/lib/routes/yuque/doc.js @@ -1,20 +1,21 @@ const axios = require('@/utils/axios'); const config = require('@/config'); +// token通过process.env.YUQUE_TOKEN或者在config.js 配置 +const { token = '' } = config.yuque; module.exports = async (ctx) => { const { repo_id } = ctx.params; const baseUrl = 'https://www.yuque.com'; const repoUrl = `${baseUrl}/api/v2/repos/${repo_id}`; const docsUrl = `${repoUrl}/docs`; - const headers = {}; - if (config.yuque.token) { - headers['X-Auth-Token'] = config.yuque.token; - } + const fetchData = (url) => axios({ url, method: 'get', - headers, + headers: { + 'X-Auth-Token': token, + }, }); const repoDetail = await fetchData(repoUrl); @@ -22,6 +23,7 @@ module.exports = async (ctx) => { name: repo, user: { name }, description, + namespace, } = repoDetail.data.data; const docsDetail = await fetchData(docsUrl); const docs = docsDetail.data.data; @@ -37,15 +39,15 @@ module.exports = async (ctx) => { const item = { title: doc.title, description: doc.description, - pubDate: doc.created_at, - link: `${baseUrl}/${repo_id}/${doc.id}`, + pubDate: doc.published_at, + link: `${baseUrl}/${namespace}/${doc.id}`, }; const key = `yuque${doc.id}`; const value = await ctx.cache.get(key); if (value) { item.description = value; - } else { + } else if (token) { const docDetail = await fetchData(`${docsUrl}/${doc.id}`); const { body_html } = docDetail.data.data; item.description = body_html;