@@ -3,7 +3,11 @@ const url = require('url')
33const wfetch = require ( './fetch' )
44const store = require ( './store' )
55const { 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
711
812let instance
913let repoId
@@ -14,28 +18,39 @@ module.exports = class Yuque {
1418 }
1519
1620 static getInstance ( ) {
17- if ( instance ) {
18- return instance
21+ if ( ! instance ) {
22+ instance = new Yuque ( repoId )
1923 }
20- return new Yuque ( repoId )
24+ return instance
2125 }
2226
2327 static async fetch ( url , options = { } ) {
28+ debug ( 'fetch' , url , 'with' , JSON . stringify ( options , null , 2 ) )
2429 const { useCache = true } = options
25- const yuque = Yuque . getInstance ( )
26- const { store } = yuque
2730 const key = hash ( url + JSON . stringify ( options ) )
2831 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+ }
3341 }
3442
3543 let response
44+ const foptions = { }
45+ if ( AUTH_TOKEN ) {
46+ foptions . headers = {
47+ 'X-Auth-Token' : AUTH_TOKEN
48+ }
49+ }
50+
3651 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 )
3954 return response
4055 } catch ( e ) {
4156 return e
0 commit comments