2017-05-04 07:57:10 +00:00
|
|
|
import Vue from 'vue'
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2018-10-09 12:07:23 +00:00
|
|
|
// window.{{globals.loadedCallback}} hook
|
2017-07-10 22:53:06 +00:00
|
|
|
// Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
|
2018-11-08 09:15:56 +00:00
|
|
|
if (process.client) {
|
2018-10-09 12:07:23 +00:00
|
|
|
window.<%= globals.readyCallback %>Cbs = []
|
|
|
|
window.<%= globals.readyCallback %> = (cb) => {
|
|
|
|
window.<%= globals.readyCallback %>Cbs.push(cb)
|
2017-07-10 22:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function empty () {}
|
2018-08-08 10:11:08 +00:00
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function globalHandleError (error) {
|
2018-08-08 10:11:08 +00:00
|
|
|
if (Vue.config.errorHandler) {
|
|
|
|
Vue.config.errorHandler(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function interopDefault (promise) {
|
2018-10-24 13:46:06 +00:00
|
|
|
return promise.then(m => m.default || m)
|
2018-10-16 21:56:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 15:15:27 +00:00
|
|
|
<% if (features.asyncData) { %>
|
2019-09-10 09:51:14 +00:00
|
|
|
export function applyAsyncData (Component, asyncData) {
|
2019-02-08 16:33:45 +00:00
|
|
|
if (
|
|
|
|
// For SSR, we once all this function without second param to just apply asyncData
|
|
|
|
// Prevent doing this for each SSR request
|
|
|
|
!asyncData && Component.options.__hasNuxtData
|
|
|
|
) {
|
2017-07-06 21:33:11 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-08 16:33:45 +00:00
|
|
|
|
|
|
|
const ComponentData = Component.options._originDataFn || Component.options.data || function () { return {} }
|
|
|
|
Component.options._originDataFn = ComponentData
|
|
|
|
|
2017-04-14 14:31:14 +00:00
|
|
|
Component.options.data = function () {
|
2019-09-29 08:06:26 +00:00
|
|
|
const data = ComponentData.call(this, 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 }
|
|
|
|
}
|
2019-02-08 16:33:45 +00:00
|
|
|
|
|
|
|
Component.options.__hasNuxtData = true
|
|
|
|
|
2017-04-14 14:31:14 +00:00
|
|
|
if (Component._Ctor && Component._Ctor.options) {
|
|
|
|
Component._Ctor.options.data = Component.options.data
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 15:15:27 +00:00
|
|
|
<% } %>
|
2017-04-14 14:31:14 +00:00
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function sanitizeComponent (Component) {
|
2017-10-13 21:53:04 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function getMatchedComponents (route, matches = false, prop = 'components') {
|
2018-08-15 13:23:03 +00:00
|
|
|
return Array.prototype.concat.apply([], route.matched.map((m, index) => {
|
2019-09-05 15:15:27 +00:00
|
|
|
return Object.keys(m[prop]).map((key) => {
|
2018-01-24 06:33:27 +00:00
|
|
|
matches && matches.push(index)
|
2019-09-05 15:15:27 +00:00
|
|
|
return m[prop][key]
|
2016-11-07 01:34:58 +00:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function getMatchedComponentsInstances (route, matches = false) {
|
2019-09-05 15:15:27 +00:00
|
|
|
return getMatchedComponents(route, matches, 'instances')
|
2016-11-22 23:27:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function flatMapComponents (route, fn) {
|
2018-08-15 13:23:03 +00:00
|
|
|
return Array.prototype.concat.apply([], route.matched.map((m, index) => {
|
|
|
|
return Object.keys(m.components).reduce((promises, key) => {
|
2018-08-13 16:27:20 +00:00
|
|
|
if (m.components[key]) {
|
|
|
|
promises.push(fn(m.components[key], m.instances[key], m, key, index))
|
|
|
|
} else {
|
|
|
|
delete m.components[key]
|
|
|
|
}
|
|
|
|
return promises
|
|
|
|
}, [])
|
2016-11-07 01:34:58 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function resolveRouteComponents (route, fn) {
|
2017-11-24 08:33:47 +00:00
|
|
|
return Promise.all(
|
2019-08-18 19:35:16 +00:00
|
|
|
flatMapComponents(route, async (Component, instance, match, key) => {
|
2017-10-13 21:53:04 +00:00
|
|
|
// If component is a function, resolve it
|
|
|
|
if (typeof Component === 'function' && !Component.options) {
|
|
|
|
Component = await Component()
|
|
|
|
}
|
2019-08-18 19:35:16 +00:00
|
|
|
match.components[key] = Component = sanitizeComponent(Component)
|
|
|
|
return typeof fn === 'function' ? fn(Component, instance, match, key) : Component
|
2017-10-13 21:53:04 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export async function getRouteData (route) {
|
2019-02-13 14:37:12 +00:00
|
|
|
if (!route) {
|
|
|
|
return
|
|
|
|
}
|
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,
|
2018-12-04 12:19:05 +00:00
|
|
|
meta: getMatchedComponents(route).map((Component, index) => {
|
2018-12-09 12:07:06 +00:00
|
|
|
return { ...Component.options.meta, ...(route.matched[index] || {}).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
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export async function setContext (app, context) {
|
2017-10-13 21:53:04 +00:00
|
|
|
// If context not defined, create it
|
|
|
|
if (!app.context) {
|
|
|
|
app.context = {
|
|
|
|
isStatic: process.static,
|
|
|
|
isDev: <%= isDev %>,
|
|
|
|
isHMR: false,
|
|
|
|
app,
|
|
|
|
<%= (store ? 'store: app.store,' : '') %>
|
|
|
|
payload: context.payload,
|
|
|
|
error: context.error,
|
|
|
|
base: '<%= router.base %>',
|
2018-10-24 13:46:06 +00:00
|
|
|
env: <%= JSON.stringify(env) %><%= isTest ? '// eslint-disable-line' : '' %>
|
2017-10-13 21:53:04 +00:00
|
|
|
}
|
|
|
|
// Only set once
|
2019-02-08 10:05:01 +00:00
|
|
|
if (context.req) {
|
|
|
|
app.context.req = context.req
|
|
|
|
}
|
|
|
|
if (context.res) {
|
|
|
|
app.context.res = context.res
|
|
|
|
}
|
|
|
|
if (context.ssrContext) {
|
|
|
|
app.context.ssrContext = context.ssrContext
|
|
|
|
}
|
2018-08-15 13:23:03 +00:00
|
|
|
app.context.redirect = (status, path, query) => {
|
|
|
|
if (!status) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.context._redirected = true
|
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') {
|
2019-03-20 12:09:14 +00:00
|
|
|
path = app.router.resolve(path).route.fullPath
|
2017-12-21 04:55:32 +00:00
|
|
|
}
|
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({
|
2019-05-16 10:08:44 +00:00
|
|
|
path,
|
|
|
|
query,
|
|
|
|
status
|
2017-11-28 09:10:20 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
path = formatUrl(path, query)
|
|
|
|
if (process.server) {
|
2018-01-15 11:22:51 +00:00
|
|
|
app.context.next({
|
2019-05-16 10:08:44 +00:00
|
|
|
path,
|
|
|
|
status
|
2018-01-15 11:22:51 +00:00
|
|
|
})
|
2017-11-28 09:10:20 +00:00
|
|
|
}
|
2018-11-08 09:15:56 +00:00
|
|
|
if (process.client) {
|
2018-01-16 06:34:40 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Location/replace
|
|
|
|
window.location.replace(path)
|
|
|
|
|
|
|
|
// Throw a redirect error
|
|
|
|
throw new Error('ERR_REDIRECT')
|
2017-11-28 09:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 21:53:04 +00:00
|
|
|
}
|
2018-10-09 12:07:23 +00:00
|
|
|
if (process.server) {
|
|
|
|
app.context.beforeNuxtRender = fn => context.beforeRenderFns.push(fn)
|
|
|
|
}
|
2018-11-08 09:15:56 +00:00
|
|
|
if (process.client) {
|
2018-10-09 12:07:23 +00:00
|
|
|
app.context.nuxtState = window.<%= globals.context %>
|
|
|
|
}
|
2017-08-31 12:46:06 +00:00
|
|
|
}
|
2019-02-13 14:37:12 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
// Dynamic keys
|
2019-02-13 14:37:12 +00:00
|
|
|
const [currentRouteData, fromRouteData] = await Promise.all([
|
|
|
|
getRouteData(context.route),
|
|
|
|
getRouteData(context.from)
|
|
|
|
])
|
|
|
|
|
|
|
|
if (context.route) {
|
|
|
|
app.context.route = currentRouteData
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context.from) {
|
|
|
|
app.context.from = fromRouteData
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
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
|
2019-09-05 15:15:27 +00:00
|
|
|
app.context.isHMR = <% if(isDev) { %>Boolean(context.isHMR)<% } else { %>false<% } %>
|
2017-10-13 21:53:04 +00:00
|
|
|
app.context.params = app.context.route.params || {}
|
|
|
|
app.context.query = app.context.route.query || {}
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
2019-09-05 15:15:27 +00:00
|
|
|
<% if (features.middleware) { %>
|
2019-09-10 09:51:14 +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)
|
2018-10-24 13:46:06 +00:00
|
|
|
.then(() => {
|
|
|
|
return middlewareSeries(promises.slice(1), appContext)
|
|
|
|
})
|
2017-02-03 14:09:27 +00:00
|
|
|
}
|
2019-09-05 15:15:27 +00:00
|
|
|
<% } %>
|
2019-09-10 09:51:14 +00:00
|
|
|
export function promisify (fn, context) {
|
2019-09-05 15:15:27 +00:00
|
|
|
<% if (features.deprecations) { %>
|
2016-11-19 21:16:26 +00:00
|
|
|
let promise
|
|
|
|
if (fn.length === 2) {
|
2018-11-06 13:02:07 +00:00
|
|
|
<% if (isDev) { %>
|
|
|
|
console.warn('Callback-based asyncData, fetch or middleware calls are deprecated. ' +
|
|
|
|
'Please switch to promises or async/await syntax')
|
|
|
|
<% } %>
|
|
|
|
|
2016-11-19 21:16:26 +00:00
|
|
|
// 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
|
|
|
}
|
2019-09-05 15:15:27 +00:00
|
|
|
<% } else { %>
|
2019-09-10 09:51:14 +00:00
|
|
|
const promise = fn(context)
|
2019-09-05 15:15:27 +00:00
|
|
|
<% } %>
|
|
|
|
if (promise && promise instanceof Promise && typeof promise.then === 'function') {
|
|
|
|
return promise
|
2016-11-19 21:16:26 +00:00
|
|
|
}
|
2019-09-05 15:15:27 +00:00
|
|
|
return Promise.resolve(promise)
|
2016-11-19 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
// Imported from vue-router
|
2019-09-10 09:51:14 +00:00
|
|
|
export function getLocation (base, mode) {
|
2019-03-23 08:40:38 +00:00
|
|
|
let path = decodeURI(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)
|
|
|
|
}
|
2019-03-23 08:40:38 +00:00
|
|
|
return (path || '/') + window.location.search + window.location.hash
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
2016-11-10 23:01:36 +00:00
|
|
|
|
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=)}
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
export function compile (str, options) {
|
2016-12-16 16:45:05 +00:00
|
|
|
return tokensToFunction(parse(str, options))
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function getQueryDiff (toQuery, fromQuery) {
|
2017-11-06 17:30:37 +00:00
|
|
|
const diff = {}
|
|
|
|
const queries = { ...toQuery, ...fromQuery }
|
|
|
|
for (const k in queries) {
|
|
|
|
if (String(toQuery[k]) !== String(fromQuery[k])) {
|
|
|
|
diff[k] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:51:14 +00:00
|
|
|
export function normalizeError (err) {
|
2018-11-14 19:17:44 +00:00
|
|
|
let message
|
|
|
|
if (!(err.message || typeof err === 'string')) {
|
|
|
|
try {
|
|
|
|
message = JSON.stringify(err, null, 2)
|
|
|
|
} catch (e) {
|
|
|
|
message = `[${err.constructor.name}]`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message = err.message || err
|
|
|
|
}
|
|
|
|
return {
|
2018-11-30 19:15:29 +00:00
|
|
|
...err,
|
2019-05-16 10:08:44 +00:00
|
|
|
message,
|
2018-11-14 19:17:44 +00:00
|
|
|
statusCode: (err.statusCode || err.status || (err.response && err.response.status) || 500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
function parse (str, options) {
|
2018-10-24 13:46:06 +00:00
|
|
|
const tokens = []
|
|
|
|
let key = 0
|
|
|
|
let index = 0
|
|
|
|
let path = ''
|
|
|
|
const defaultDelimiter = (options && options.delimiter) || '/'
|
|
|
|
let res
|
2016-12-16 16:45:05 +00:00
|
|
|
|
|
|
|
while ((res = PATH_REGEXP.exec(str)) != null) {
|
2018-10-24 13:46:06 +00:00
|
|
|
const m = res[0]
|
|
|
|
const escaped = res[1]
|
|
|
|
const offset = res.index
|
2016-12-16 16:45:05 +00:00
|
|
|
path += str.slice(index, offset)
|
|
|
|
index = offset + m.length
|
|
|
|
|
|
|
|
// Ignore already escaped sequences.
|
|
|
|
if (escaped) {
|
|
|
|
path += escaped[1]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:46:06 +00:00
|
|
|
const next = str[index]
|
|
|
|
const prefix = res[2]
|
|
|
|
const name = res[3]
|
|
|
|
const capture = res[4]
|
|
|
|
const group = res[5]
|
|
|
|
const modifier = res[6]
|
|
|
|
const asterisk = res[7]
|
2016-12-16 16:45:05 +00:00
|
|
|
|
|
|
|
// Push the current path onto the tokens.
|
|
|
|
if (path) {
|
|
|
|
tokens.push(path)
|
|
|
|
path = ''
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:46:06 +00:00
|
|
|
const partial = prefix != null && next != null && next !== prefix
|
|
|
|
const repeat = modifier === '+' || modifier === '*'
|
|
|
|
const optional = modifier === '?' || modifier === '*'
|
|
|
|
const delimiter = res[2] || defaultDelimiter
|
|
|
|
const pattern = capture || group
|
2016-12-16 16:45:05 +00:00
|
|
|
|
|
|
|
tokens.push({
|
|
|
|
name: name || key++,
|
|
|
|
prefix: prefix || '',
|
2019-05-16 10:08:44 +00:00
|
|
|
delimiter,
|
|
|
|
optional,
|
|
|
|
repeat,
|
|
|
|
partial,
|
|
|
|
asterisk: Boolean(asterisk),
|
2016-12-16 16:45:05 +00:00
|
|
|
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}
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
function encodeURIComponentPretty (str, slashAllowed) {
|
2019-09-05 15:15:27 +00:00
|
|
|
const re = slashAllowed ? /[?#]/g : /[/?#]/g
|
|
|
|
return encodeURI(str).replace(re, (c) => {
|
2016-12-16 16:45:05 +00:00
|
|
|
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode the asterisk parameter. Similar to `pretty`, but allows slashes.
|
|
|
|
*
|
|
|
|
* @param {string}
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
function encodeAsterisk (str) {
|
2019-09-05 15:15:27 +00:00
|
|
|
return encodeURIComponentPretty(str, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape a regular expression string.
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
function escapeString (str) {
|
2019-09-05 15:15:27 +00:00
|
|
|
return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape the capturing group by escaping special characters and meaning.
|
|
|
|
*
|
|
|
|
* @param {string} group
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
function escapeGroup (group) {
|
2019-09-05 15:15:27 +00:00
|
|
|
return group.replace(/([=!:$/()])/g, '\\$1')
|
2016-12-16 16:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose a method for transforming tokens into the path function.
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
function tokensToFunction (tokens) {
|
2016-12-16 16:45:05 +00:00
|
|
|
// Compile all the tokens into regexps.
|
2018-10-24 13:46:06 +00:00
|
|
|
const matches = new Array(tokens.length)
|
2016-12-16 16:45:05 +00:00
|
|
|
|
|
|
|
// Compile all the patterns before compilation.
|
2018-10-24 13:46:06 +00:00
|
|
|
for (let i = 0; i < tokens.length; i++) {
|
2016-12-16 16:45:05 +00:00
|
|
|
if (typeof tokens[i] === 'object') {
|
|
|
|
matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:46:06 +00:00
|
|
|
return function (obj, opts) {
|
|
|
|
let path = ''
|
|
|
|
const data = obj || {}
|
|
|
|
const options = opts || {}
|
|
|
|
const encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent
|
2016-12-16 16:45:05 +00:00
|
|
|
|
2018-10-24 13:46:06 +00:00
|
|
|
for (let i = 0; i < tokens.length; i++) {
|
|
|
|
const token = tokens[i]
|
2016-12-16 16:45:05 +00:00
|
|
|
|
|
|
|
if (typeof token === 'string') {
|
|
|
|
path += token
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-22 20:08:27 +00:00
|
|
|
const value = data[token.name || 'pathMatch']
|
2018-10-24 13:46:06 +00:00
|
|
|
let segment
|
2016-12-16 16:45:05 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:46:06 +00:00
|
|
|
for (let j = 0; j < value.length; j++) {
|
2016-12-16 16:45:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
function formatUrl (url, query) {
|
2019-09-05 15:15:27 +00:00
|
|
|
<% if (features.clientUseUrl) { %>
|
|
|
|
url = new URL(url, top.location.href)
|
|
|
|
for (const key in query) {
|
|
|
|
const value = query[key]
|
|
|
|
if (value == null) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
for (const arrayValue of value) {
|
|
|
|
url.searchParams.append(key, arrayValue)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
url.searchParams.append(key, value)
|
|
|
|
}
|
|
|
|
url.searchParams.sort()
|
|
|
|
return url.toString()
|
|
|
|
<% } else { %>
|
2017-11-28 09:10:20 +00:00
|
|
|
let protocol
|
2018-10-24 13:46:06 +00:00
|
|
|
const index = url.indexOf('://')
|
2017-11-28 09:10:20 +00:00
|
|
|
if (index !== -1) {
|
|
|
|
protocol = url.substring(0, index)
|
|
|
|
url = url.substring(index + 3)
|
2018-10-12 22:31:19 +00:00
|
|
|
} else if (url.startsWith('//')) {
|
2017-11-28 09:10:20 +00:00
|
|
|
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) {
|
2019-05-16 10:08:44 +00:00
|
|
|
[path, hash] = parts
|
2017-11-28 09:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result += path ? '/' + path : ''
|
|
|
|
|
|
|
|
if (query && JSON.stringify(query) !== '{}') {
|
|
|
|
result += (url.split('?').length === 2 ? '&' : '?') + formatQuery(query)
|
|
|
|
}
|
|
|
|
result += hash ? '#' + hash : ''
|
|
|
|
|
|
|
|
return result
|
2019-09-05 15:15:27 +00:00
|
|
|
<% } %>
|
2017-11-28 09:10:20 +00:00
|
|
|
}
|
2019-09-05 15:15:27 +00:00
|
|
|
<% if (!features.clientUseUrl) { %>
|
2017-11-28 09:10:20 +00:00
|
|
|
/**
|
|
|
|
* Transform data object to query string
|
|
|
|
*
|
|
|
|
* @param {object} query
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2019-09-10 09:51:14 +00:00
|
|
|
function formatQuery (query) {
|
2018-08-06 00:12:44 +00:00
|
|
|
return Object.keys(query).sort().map((key) => {
|
2018-10-24 13:46:06 +00:00
|
|
|
const val = query[key]
|
2017-11-28 09:10:20 +00:00
|
|
|
if (val == null) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
if (Array.isArray(val)) {
|
|
|
|
return val.slice().map(val2 => [key, '=', val2].join('')).join('&')
|
|
|
|
}
|
|
|
|
return key + '=' + val
|
|
|
|
}).filter(Boolean).join('&')
|
|
|
|
}
|
2019-09-05 15:15:27 +00:00
|
|
|
<% } %>
|