@@ -3,7 +3,11 @@ const url = require('url')
3
3
const wfetch = require ( './fetch' )
4
4
const store = require ( './store' )
5
5
const { hash, logger, chalk } = require ( '@vuepress/shared-utils' )
6
- const Yuque = require ( './yuque' )
6
+ const { PACKAGE_NAME } = require ( './constant' )
7
+ const debug = require ( 'debug' ) ( `${ PACKAGE_NAME } :yuque` )
8
+
9
+ const AUTH_TOKEN = process . env . YUQUE_QUTH_TOKEN
10
+ const SKIP_CACHE = process . env . SKIP_CACHE
7
11
8
12
let instance
9
13
let repoId
@@ -14,28 +18,39 @@ module.exports = class Yuque {
14
18
}
15
19
16
20
static getInstance ( ) {
17
- if ( instance ) {
18
- return instance
21
+ if ( ! instance ) {
22
+ instance = new Yuque ( repoId )
19
23
}
20
- return new Yuque ( repoId )
24
+ return instance
21
25
}
22
26
23
27
static async fetch ( url , options = { } ) {
28
+ debug ( 'fetch' , url , 'with' , JSON . stringify ( options , null , 2 ) )
24
29
const { useCache = true } = options
25
- const yuque = Yuque . getInstance ( )
26
- const { store } = yuque
27
30
const key = hash ( url + JSON . stringify ( options ) )
28
31
const cacheKey = `fetch.${ key } `
29
-
30
- if ( useCache && store . get ( cacheKey ) ) {
31
- logger . debug ( `Using cache for ${ chalk . yellow ( url ) } ` )
32
- return store . get ( cacheKey )
32
+ let yuque
33
+
34
+ if ( repoId && useCache && ! SKIP_CACHE ) {
35
+ yuque = Yuque . getInstance ( )
36
+ const cached = yuque . store . get ( cacheKey )
37
+ if ( cached ) {
38
+ debug ( `Using cache for ${ chalk . yellow ( url ) } ` )
39
+ return cached
40
+ }
33
41
}
34
42
35
43
let response
44
+ const foptions = { }
45
+ if ( AUTH_TOKEN ) {
46
+ foptions . headers = {
47
+ 'X-Auth-Token' : AUTH_TOKEN
48
+ }
49
+ }
50
+
36
51
try {
37
- response = await wfetch ( url , options )
38
- store . set ( cacheKey , response )
52
+ response = await wfetch ( url , foptions )
53
+ yuque && yuque . store . set ( cacheKey , response )
39
54
return response
40
55
} catch ( e ) {
41
56
return e
0 commit comments