2017-05-04 07:57:10 +00:00
|
|
|
import Vue from 'vue'
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2017-04-14 14:31:14 +00:00
|
|
|
const noopData = () => ({})
|
|
|
|
|
2017-07-10 22:53:06 +00:00
|
|
|
// window.onNuxtReady(() => console.log('Ready')) hook
|
|
|
|
// Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
|
|
|
|
if (process.browser) {
|
|
|
|
window._nuxtReadyCbs = []
|
|
|
|
window.onNuxtReady = function (cb) {
|
|
|
|
window._nuxtReadyCbs.push(cb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
export function applyAsyncData(Component, asyncData) {
|
2017-04-14 14:31:14 +00:00
|
|
|
const ComponentData = Component.options.data || noopData
|
2017-07-06 21:33:11 +00:00
|
|
|
// Prevent calling this method for each request on SSR context
|
2017-08-29 18:53:50 +00:00
|
|
|
if (!asyncData && Component.options.hasAsyncData) {
|
2017-07-06 21:33:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
Component.options.hasAsyncData = true
|
2017-04-14 14:31:14 +00:00
|
|
|
Component.options.data = function () {
|
|
|
|
const data = ComponentData.call(this)
|
2017-08-29 18:53:50 +00:00
|
|
|
if (this.$ssrContext) {
|
2017-08-25 05:56:38 +00:00
|
|
|
asyncData = this.$ssrContext.asyncData[Component.cid]
|
2017-07-06 21:33:11 +00:00
|
|
|
}
|
2017-04-14 14:31:14 +00:00
|
|
|
return { ...data, ...asyncData }
|
|
|
|
}
|
|
|
|
if (Component._Ctor && Component._Ctor.options) {
|
|
|
|
Component._Ctor.options.data = Component.options.data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
export function sanitizeComponent(Component) {
|
|
|
|
// If Component already sanitized
|
|
|
|
if (Component.options && Component._Ctor === Component) {
|
|
|
|
return Component
|
|
|
|
}
|
2017-05-04 07:57:10 +00:00
|
|
|
if (!Component.options) {
|
|
|
|
Component = Vue.extend(Component) // fix issue #6
|
|
|
|
Component._Ctor = Component
|
|
|
|
} else {
|
|
|
|
Component._Ctor = Component
|
|
|
|
Component.extendOptions = Component.options
|
|
|
|
}
|
|
|
|
// For debugging purpose
|
|
|
|
if (!Component.options.name && Component.options.__file) {
|
|
|
|
Component.options.name = Component.options.__file
|
|
|
|
}
|
|
|
|
return Component
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
export function getMatchedComponents(route) {
|
2016-11-07 01:34:58 +00:00
|
|
|
return [].concat.apply([], route.matched.map(function (m) {
|
|
|
|
return Object.keys(m.components).map(function (key) {
|
|
|
|
return m.components[key]
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
export function getMatchedComponentsInstances(route) {
|
2016-11-22 23:27:07 +00:00
|
|
|
return [].concat.apply([], route.matched.map(function (m) {
|
|
|
|
return Object.keys(m.instances).map(function (key) {
|
|
|
|
return m.instances[key]
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
export function flatMapComponents(route, fn) {
|
2016-11-07 01:34:58 +00:00
|
|
|
return Array.prototype.concat.apply([], route.matched.map(function (m, index) {
|
|
|
|
return Object.keys(m.components).map(function (key) {
|
|
|
|
return fn(m.components[key], m.instances[key], m, key, index)
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-11-24 08:33:47 +00:00
|
|
|
export function resolveRouteComponents(route) {
|
|
|
|
return Promise.all(
|
2017-10-13 21:53:04 +00:00
|
|
|
flatMapComponents(route, async (Component, _, match, key) => {
|
|
|
|
// If component is a function, resolve it
|
|
|
|
if (typeof Component === 'function' && !Component.options) {
|
|
|
|
Component = await Component()
|
|
|
|
}
|
|
|
|
return match.components[key] = sanitizeComponent(Component)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-12-01 09:25:21 +00:00
|
|
|
export async function getRouteData(route) {
|
2017-10-13 21:53:04 +00:00
|
|
|
// Make sure the components are resolved (code-splitting)
|
|
|
|
await resolveRouteComponents(route)
|
|
|
|
// Send back a copy of route with meta based on Component definition
|
|
|
|
return {
|
|
|
|
...route,
|
|
|
|
meta: getMatchedComponents(route).map((Component) => {
|
|
|
|
return Component.options.meta || {}
|
2016-11-11 00:35:05 +00:00
|
|
|
})
|
2016-11-10 23:01:36 +00:00
|
|
|
}
|
2017-10-13 21:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function setContext(app, context) {
|
|
|
|
const route = (context.to ? context.to : context.route)
|
|
|
|
// If context not defined, create it
|
|
|
|
if (!app.context) {
|
|
|
|
app.context = {
|
2017-10-20 10:05:22 +00:00
|
|
|
get isServer() {
|
|
|
|
console.warn('context.isServer has been deprecated, please use process.server instead.')
|
|
|
|
return process.server
|
|
|
|
},
|
|
|
|
get isClient() {
|
|
|
|
console.warn('context.isClient has been deprecated, please use process.client instead.')
|
|
|
|
return process.client
|
|
|
|
},
|
2017-10-13 21:53:04 +00:00
|
|
|
isStatic: process.static,
|
|
|
|
isDev: <%= isDev %>,
|
|
|
|
isHMR: false,
|
|
|
|
app,
|
|
|
|
<%= (store ? 'store: app.store,' : '') %>
|
|
|
|
payload: context.payload,
|
|
|
|
error: context.error,
|
|
|
|
base: '<%= router.base %>',
|
|
|
|
env: <%= JSON.stringify(env) %>
|
|
|
|
}
|
|
|
|
// Only set once
|
|
|
|
if (context.req) app.context.req = context.req
|
|
|
|
if (context.res) app.context.res = context.res
|
|
|
|
app.context.redirect = function (status, path, query) {
|
|
|
|
if (!status) return
|
2017-10-24 21:38:31 +00:00
|
|
|
app.context._redirected = true // Used in middleware
|
2017-10-13 21:53:04 +00:00
|
|
|
// if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' })
|
2017-12-21 04:55:32 +00:00
|
|
|
let pathType = typeof path
|
|
|
|
if (typeof status !== 'number' && (pathType === 'undefined' || pathType === 'object')) {
|
2017-10-13 21:53:04 +00:00
|
|
|
query = path || {}
|
|
|
|
path = status
|
2017-12-21 04:55:32 +00:00
|
|
|
pathType = typeof path
|
2017-10-13 21:53:04 +00:00
|
|
|
status = 302
|
|
|
|
}
|
2017-12-21 04:55:32 +00:00
|
|
|
if (pathType === 'object') {
|
|
|
|
path = app.router.resolve(path).href
|
|
|
|
}
|
2017-11-28 14:05:17 +00:00
|
|
|
// "/absolute/route", "./relative/route" or "../relative/route"
|
|
|
|
if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) {
|
2017-11-28 09:10:20 +00:00
|
|
|
app.context.next({
|
|
|
|
path: path,
|
|
|
|
query: query,
|
|
|
|
status: status
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
path = formatUrl(path, query)
|
|
|
|
if (process.server) {
|
2018-01-15 11:22:51 +00:00
|
|
|
app.context.next({
|
|
|
|
path: path,
|
|
|
|
status: status
|
|
|
|
})
|
2017-11-28 09:10:20 +00:00
|
|
|
}
|
|
|
|
if (process.client) {
|
|
|
|
window.location = path
|
2018-01-15 11:22:51 +00:00
|
|
|
return new Promise((resolve) => {
|
2018-01-15 12:51:51 +00:00
|
|
|
// Wait for browser to redirect...
|
2018-01-15 11:22:51 +00:00
|
|
|
})
|
2017-11-28 09:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 21:53:04 +00:00
|
|
|
}
|
2017-10-20 10:05:22 +00:00
|
|
|
if (process.server) app.context.beforeNuxtRender = (fn) => context.beforeRenderFns.push(fn)
|
|
|
|
if (process.client) app.context.nuxtState = window.__NUXT__
|
2017-08-31 12:46:06 +00:00
|
|
|
}
|
2017-10-13 21:53:04 +00:00
|
|
|
// Dynamic keys
|
|
|
|
app.context.next = context.next
|
2017-10-28 20:42:51 +00:00
|
|
|
app.context._redirected = false
|
2017-11-02 16:47:33 +00:00
|
|
|
app.context._errored = false
|
2017-10-13 21:53:04 +00:00
|
|
|
app.context.isHMR = !!context.isHMR
|
|
|
|
if (context.route) app.context.route = await getRouteData(context.route)
|
|
|
|
app.context.params = app.context.route.params || {}
|
|
|
|
app.context.query = app.context.route.query || {}
|
|
|
|
if (context.from) app.context.from = await getRouteData(context.from)
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
export function middlewareSeries(promises, appContext) {
|
2017-11-02 16:47:33 +00:00
|
|
|
if (!promises.length || appContext._redirected || appContext._errored) {
|
2017-02-03 14:09:27 +00:00
|
|
|
return Promise.resolve()
|
|
|
|
}
|
2017-10-24 21:19:14 +00:00
|
|
|
return promisify(promises[0], appContext)
|
2017-02-03 14:09:27 +00:00
|
|
|
.then(() => {
|
2017-10-24 21:19:14 +00:00
|
|
|
return middlewareSeries(promises.slice(1), appContext)
|
2017-02-03 14:09:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
export function promisify(fn, context) {
|
2016-11-19 21:16:26 +00:00
|
|
|
let promise
|
|
|
|
if (fn.length === 2) {
|
|
|
|
// fn(context, callback)
|
|
|
|
promise = new Promise((resolve) => {
|
2016-11-21 13:14:35 +00:00
|
|
|
fn(context, function (err, data) {
|
2016-11-19 21:16:26 +00:00
|
|
|
if (err) {
|
|
|
|
context.error(err)
|
|
|
|
}
|
|
|
|
data = data || {}
|
|
|
|
resolve(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
2016-11-21 13:14:35 +00:00
|
|
|
promise = fn(context)
|
2016-11-19 21:16:26 +00:00
|
|
|
}
|
2017-04-06 09:18:27 +00:00
|
|
|
if (!promise || (!(promise instanceof Promise) && (typeof promise.then !== 'function'))) {
|
2016-11-19 21:16:26 +00:00
|
|
|
promise = Promise.resolve(promise)
|
|
|
|
}
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
// Imported from vue-router
|
2017-10-13 21:53:04 +00:00
|
|
|
export function getLocation(base, mode) {
|
2016-11-07 01:34:58 +00:00
|
|
|
var path = window.location.pathname
|
2017-08-30 10:13:01 +00:00
|
|
|
if (mode === 'hash') {
|
|
|
|
return window.location.hash.replace(/^#\//, '')
|
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
if (base && path.indexOf(base) === 0) {
|
|
|
|
path = path.slice(base.length)
|
|
|
|
}
|
|
|
|
return (path || '/') + window.location.search + window.location.hash
|
|
|
|
}
|
2016-11-10 23:01:36 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
export function urlJoin() {
|
2016-11-10 23:01:36 +00:00
|
|
|
return [].slice.call(arguments).join('/').replace(/\/+/g, '/')
|
|
|
|
}
|
2016-12-16 16:45:05 +00:00
|
|
|
|
|
|
|
// Imported from path-to-regexp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile a string to a template function for the path.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @param {Object=} options
|
|
|
|
* @return {!function(Object=, Object=)}
|
|
|
|
*/
|
2017-10-13 21:53:04 +00:00
|
|
|
export function compile(str, options) {
|
2016-12-16 16:45:05 +00:00
|
|
|
return tokensToFunction(parse(str, options))
|
|
|
|
}
|
|
|
|
|
2017-11-06 17:30:37 +00:00
|
|
|
export function getQueryDiff(toQuery, fromQuery) {
|
|
|
|
const diff = {}
|
|
|
|
const queries = { ...toQuery, ...fromQuery }
|
|
|
|
for (const k in queries) {
|
|
|
|
if (String(toQuery[k]) !== String(fromQuery[k])) {
|
|
|
|
diff[k] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff
|
|
|
|
}
|
|
|
|
|
2016-12-16 16:45:05 +00:00
|
|
|
/**
|
|
|
|
* The main path matching regexp utility.
|
|
|
|
*
|
|
|
|
* @type {RegExp}
|
|
|
|
*/
|
|
|
|
const PATH_REGEXP = new RegExp([
|
|
|
|
// Match escaped characters that would otherwise appear in future matches.
|
|
|
|
// This allows the user to escape special characters that won't transform.
|
|
|
|
'(\\\\.)',
|
|
|
|
// Match Express-style parameters and un-named parameters with a prefix
|
|
|
|
// and optional suffixes. Matches appear as:
|
|
|
|
//
|
|
|
|
// "/:test(\\d+)?" => ["/", "test", "\d+", undefined, "?", undefined]
|
|
|
|
// "/route(\\d+)" => [undefined, undefined, undefined, "\d+", undefined, undefined]
|
|
|
|
// "/*" => ["/", undefined, undefined, undefined, undefined, "*"]
|
|
|
|
'([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))'
|
|
|
|
].join('|'), 'g')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a string for the raw tokens.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @param {Object=} options
|
|
|
|
* @return {!Array}
|
|
|
|
*/
|
2017-10-13 21:53:04 +00:00
|
|
|
function parse(str, options) {
|
2016-12-16 16:45:05 +00:00
|
|
|
var tokens = []
|
|
|
|
var key = 0
|
|
|
|
var index = 0
|
|
|
|
var path = ''
|
|
|
|
var defaultDelimiter = options && options.delimiter || '/'
|
|
|
|
var res
|
|
|
|
|
|
|
|
while ((res = PATH_REGEXP.exec(str)) != null) {
|
|
|
|
var m = res[0]
|
|
|
|
var escaped = res[1]
|
|
|
|
var offset = res.index
|
|
|
|
path += str.slice(index, offset)
|
|
|
|
index = offset + m.length
|
|
|
|
|
|
|
|
// Ignore already escaped sequences.
|
|
|
|
if (escaped) {
|
|
|
|
path += escaped[1]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var next = str[index]
|
|
|
|
var prefix = res[2]
|
|
|
|
var name = res[3]
|
|
|
|
var capture = res[4]
|
|
|
|
var group = res[5]
|
|
|
|
var modifier = res[6]
|
|
|
|
var asterisk = res[7]
|
|
|
|
|
|
|
|
// Push the current path onto the tokens.
|
|
|
|
if (path) {
|
|
|
|
tokens.push(path)
|
|
|
|
path = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
var partial = prefix != null && next != null && next !== prefix
|
|
|
|
var repeat = modifier === '+' || modifier === '*'
|
|
|
|
var optional = modifier === '?' || modifier === '*'
|
|
|
|
var delimiter = res[2] || defaultDelimiter
|
|
|
|
var pattern = capture || group
|
|
|
|
|
|
|
|
tokens.push({
|
|
|
|
name: name || key++,
|
|
|
|
prefix: prefix || '',
|
|
|
|
delimiter: delimiter,
|
|
|
|
optional: optional,
|
|
|
|
repeat: repeat,
|
|
|
|
partial: partial,
|
|
|
|
asterisk: !!asterisk,
|
|
|
|
pattern: pattern ? escapeGroup(pattern) : (asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match any characters still remaining.
|
|
|
|
if (index < str.length) {
|
|
|
|
path += str.substr(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the path exists, push it onto the end.
|
|
|
|
if (path) {
|
|
|
|
tokens.push(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prettier encoding of URI path segments.
|
|
|
|
*
|
|
|
|
* @param {string}
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-10-13 21:53:04 +00:00
|
|
|
function encodeURIComponentPretty(str) {
|
2016-12-16 16:45:05 +00:00
|
|
|
return encodeURI(str).replace(/[\/?#]/g, function (c) {
|
|
|
|
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode the asterisk parameter. Similar to `pretty`, but allows slashes.
|
|
|
|
*
|
|
|
|
* @param {string}
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-10-13 21:53:04 +00:00
|
|
|
function encodeAsterisk(str) {
|
2016-12-16 16:45:05 +00:00
|
|
|
return encodeURI(str).replace(/[?#]/g, function (c) {
|
|
|
|
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose a method for transforming tokens into the path function.
|
|
|
|
*/
|
2017-10-13 21:53:04 +00:00
|
|
|
function tokensToFunction(tokens) {
|
2016-12-16 16:45:05 +00:00
|
|
|
// Compile all the tokens into regexps.
|
|
|
|
var matches = new Array(tokens.length)
|
|
|
|
|
|
|
|
// Compile all the patterns before compilation.
|
|
|
|
for (var i = 0; i < tokens.length; i++) {
|
|
|
|
if (typeof tokens[i] === 'object') {
|
|
|
|
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
return function(obj, opts) {
|
2016-12-16 16:45:05 +00:00
|
|
|
var path = ''
|
|
|
|
var data = obj || {}
|
|
|
|
var options = opts || {}
|
|
|
|
var encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent
|
|
|
|
|
|
|
|
for (var i = 0; i < tokens.length; i++) {
|
|
|
|
var token = tokens[i]
|
|
|
|
|
|
|
|
if (typeof token === 'string') {
|
|
|
|
path += token
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var value = data[token.name]
|
|
|
|
var segment
|
|
|
|
|
|
|
|
if (value == null) {
|
|
|
|
if (token.optional) {
|
|
|
|
// Prepend partial segment prefixes.
|
|
|
|
if (token.partial) {
|
|
|
|
path += token.prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
throw new TypeError('Expected "' + token.name + '" to be defined')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
if (!token.repeat) {
|
|
|
|
throw new TypeError('Expected "' + token.name + '" to not repeat, but received `' + JSON.stringify(value) + '`')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.length === 0) {
|
|
|
|
if (token.optional) {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
throw new TypeError('Expected "' + token.name + '" to not be empty')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var j = 0; j < value.length; j++) {
|
|
|
|
segment = encode(value[j])
|
|
|
|
|
|
|
|
if (!matches[i].test(segment)) {
|
|
|
|
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received `' + JSON.stringify(segment) + '`')
|
|
|
|
}
|
|
|
|
|
|
|
|
path += (j === 0 ? token.prefix : token.delimiter) + segment
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
segment = token.asterisk ? encodeAsterisk(value) : encode(value)
|
|
|
|
|
|
|
|
if (!matches[i].test(segment)) {
|
|
|
|
throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
|
|
|
|
}
|
|
|
|
|
|
|
|
path += token.prefix + segment
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape a regular expression string.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-10-13 21:53:04 +00:00
|
|
|
function escapeString(str) {
|
2016-12-16 16:45:05 +00:00
|
|
|
return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape the capturing group by escaping special characters and meaning.
|
|
|
|
*
|
|
|
|
* @param {string} group
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-10-13 21:53:04 +00:00
|
|
|
function escapeGroup(group) {
|
2016-12-16 16:45:05 +00:00
|
|
|
return group.replace(/([=!:$\/()])/g, '\\$1')
|
|
|
|
}
|
2017-11-28 09:10:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format given url, append query to url query string
|
|
|
|
*
|
|
|
|
* @param {string} url
|
|
|
|
* @param {string} query
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function formatUrl (url, query) {
|
|
|
|
let protocol
|
|
|
|
let index = url.indexOf('://')
|
|
|
|
if (index !== -1) {
|
|
|
|
protocol = url.substring(0, index)
|
|
|
|
url = url.substring(index + 3)
|
|
|
|
} else if (url.indexOf('//') === 0) {
|
|
|
|
url = url.substring(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
let parts = url.split('/')
|
|
|
|
let result = (protocol ? protocol + '://' : '//') + parts.shift()
|
|
|
|
|
|
|
|
let path = parts.filter(Boolean).join('/')
|
|
|
|
let hash
|
|
|
|
parts = path.split('#')
|
|
|
|
if (parts.length === 2) {
|
|
|
|
path = parts[0]
|
|
|
|
hash = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
result += path ? '/' + path : ''
|
|
|
|
|
|
|
|
if (query && JSON.stringify(query) !== '{}') {
|
|
|
|
result += (url.split('?').length === 2 ? '&' : '?') + formatQuery(query)
|
|
|
|
}
|
|
|
|
result += hash ? '#' + hash : ''
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform data object to query string
|
|
|
|
*
|
|
|
|
* @param {object} query
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function formatQuery (query) {
|
|
|
|
return Object.keys(query).sort().map(key => {
|
|
|
|
var val = query[key]
|
|
|
|
if (val == null) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
if (Array.isArray(val)) {
|
|
|
|
return val.slice().map(val2 => [key, '=', val2].join('')).join('&')
|
|
|
|
}
|
|
|
|
return key + '=' + val
|
|
|
|
}).filter(Boolean).join('&')
|
|
|
|
}
|