1
1
'use strict' ;
2
2
3
3
const SDK = require ( '@yuque/sdk' ) ;
4
+ const deprecate = require ( 'depd' ) ( 'yuque-hexo' ) ;
5
+
4
6
5
7
function handler ( res ) {
6
8
// should handler error yourself
@@ -16,9 +18,65 @@ function handler(res) {
16
18
return res . data ;
17
19
}
18
20
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
+
19
73
class YuqueClient extends SDK {
20
74
constructor ( config ) {
21
75
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
+ }
22
80
const endpoint = baseUrl . endsWith ( '/' ) ? baseUrl : `${ baseUrl } /` ;
23
81
const superConfig = {
24
82
endpoint,
0 commit comments