fix: 修复 语雀知识库link 404报错 (#2230)

This commit is contained in:
aha2mao
2019-05-27 11:06:06 +08:00
committed by DIYgod
parent da84ef9687
commit e808f8b210
+10 -8
View File
@@ -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;