feat(fetch): add requestHeaders option, fixed #336

This commit is contained in:
qingwei.li 2018-02-10 17:31:26 +08:00 committed by cinwell.li
parent f960c196bf
commit 54ab4c9ff7
3 changed files with 20 additions and 13 deletions

View File

@ -9,7 +9,7 @@ const cache = {}
* @param {boolean} [hasBar=false] has progress bar
* @return { then(resolve, reject), abort }
*/
export function get (url, hasBar = false) {
export function get (url, hasBar = false, headers = {}) {
const xhr = new XMLHttpRequest()
const on = function () {
xhr.addEventListener.apply(xhr, arguments)
@ -21,6 +21,9 @@ export function get (url, hasBar = false) {
}
xhr.open('GET', url)
for (const i in headers) {
xhr.setRequestHeader(i, headers[i])
}
xhr.send()
return {

View File

@ -10,9 +10,11 @@ function loadNested (path, qs, file, next, vm, first) {
if (!path) return
get(vm.router.getFile(path + file) + qs).then(next, _ =>
loadNested(path, qs, file, next, vm)
)
get(
vm.router.getFile(path + file) + qs,
false,
vm.config.requestHeaders
).then(next, _ => loadNested(path, qs, file, next, vm))
}
export function fetchMixin (proto) {
@ -20,12 +22,12 @@ export function fetchMixin (proto) {
proto._fetch = function (cb = noop) {
const { path, query } = this.route
const qs = stringifyQuery(query, ['id'])
const { loadNavbar, loadSidebar } = this.config
const { loadNavbar, loadSidebar, requestHeaders } = this.config
// Abort last request
last && last.abort && last.abort()
last = get(this.router.getFile(path) + qs, true)
last = get(this.router.getFile(path) + qs, true, requestHeaders)
// Current page is html
this.isHTML = /\.html$/g.test(path)
@ -67,7 +69,7 @@ export function fetchMixin (proto) {
}
proto._fetchCover = function () {
const { coverpage } = this.config
const { coverpage, requestHeaders } = this.config
const query = this.route.query
const root = getParentPath(this.route.path)
@ -89,8 +91,8 @@ export function fetchMixin (proto) {
if (path) {
path = this.router.getFile(root + path)
this.coverIsHTML = /\.html$/g.test(path)
get(path + stringifyQuery(query, ['id'])).then(text =>
this._renderCover(text)
get(path + stringifyQuery(query, ['id']), false, requestHeaders).then(
text => this._renderCover(text)
)
} else {
this._renderCover()

View File

@ -165,9 +165,11 @@ export function init (config, vm) {
paths.forEach(path => {
if (INDEXS[path]) return count++
helper.get(vm.router.getFile(path)).then(result => {
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
len === ++count && saveData(config.maxAge)
})
helper
.get(vm.router.getFile(path), false, vm.config.requestHeaders)
.then(result => {
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
len === ++count && saveData(config.maxAge)
})
})
}