Skip to content

Commit aae718d

Browse files
committed
chore: compatibility with older yuque client (without token)
1 parent e08ff8b commit aae718d

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ A downloader for articles from yuque
3030

3131
## Config
3232

33-
如果你的知识库是私有的,请使用环境变量注入 Token 信息,在语雀上点击个人头像->设置—> Token 即可获取。
33+
如果你的知识库是私有的,请使用环境变量 (YUQUE_TOKEN=xxx) 注入 Token 信息,在语雀上点击个人头像->设置—> Token 即可获取。
3434

3535
> package.json
3636

lib/yuque.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
const SDK = require('@yuque/sdk');
4+
const deprecate = require('depd')('yuque-hexo');
5+
46

57
function handler(res) {
68
// should handler error yourself
@@ -16,9 +18,65 @@ function handler(res) {
1618
return res.data;
1719
}
1820

21+
'use strict';
22+
23+
const urllib = require('urllib');
24+
const debug = require('debug')('yuque-hexo:client');
25+
26+
/**
27+
* doesn't support token
28+
*/
29+
class AnonymousYuqueClient {
30+
constructor(config) {
31+
const { baseUrl, login, repo } = config;
32+
this.config = Object.assign({}, config);
33+
this.config.namespace = `${login}/${repo}`;
34+
debug(`create client: baseUrl: ${baseUrl}, login: ${login}, repo: ${repo}`);
35+
}
36+
37+
async _fetch(method, api, data) {
38+
const { baseUrl, namespace, timeout = 30000 } = this.config;
39+
const path = `${baseUrl}/repos/${namespace}${api}`;
40+
debug(`request data: api: ${path}, data: ${data}`);
41+
try {
42+
const result = await urllib.request(path, {
43+
dataType: 'json',
44+
method,
45+
data,
46+
timeout,
47+
});
48+
return result.data;
49+
} catch (error) {
50+
throw new Error(`请求数据失败: ${error.message}`);
51+
}
52+
}
53+
54+
async getArticles() {
55+
const api = '/docs';
56+
const result = await this._fetch('GET', api);
57+
return result;
58+
}
59+
60+
async getArticle(slug) {
61+
const api = `/docs/${slug}?raw=true`;
62+
const result = await this._fetch('GET', api);
63+
return result;
64+
}
65+
66+
// async getToc() {
67+
// const api = '/toc';
68+
// const result = await this._fetch('GET', api);
69+
// return result;
70+
// }
71+
}
72+
1973
class YuqueClient extends SDK {
2074
constructor(config) {
2175
const { baseUrl, login, repo, token } = config;
76+
if (!token) {
77+
deprecate('TOKEN of yuque will be required while verion >v1.6.0.');
78+
return new AnonymousYuqueClient(config);
79+
}
2280
const endpoint = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
2381
const superConfig = {
2482
endpoint,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"chalk": "^2.4.1",
3030
"common-bin": "^2.7.3",
3131
"debug": "^3.1.0",
32+
"depd": "^2.0.0",
3233
"ejs": "^2.6.1",
3334
"hexo-front-matter": "^0.2.3",
3435
"html-entities": "^1.2.1",

0 commit comments

Comments
 (0)