Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit c0530d9

Browse files
committed
feat($fetch): new env: SKIP_CACHE and AUTH_TOKEN
1 parent 5665483 commit c0530d9

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

lib/yuque.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ const url = require('url')
33
const wfetch = require('./fetch')
44
const store = require('./store')
55
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
711

812
let instance
913
let 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

Comments
 (0)