Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function makePublicRouterInstance (router) {
const instance = {}
for (const property of urlPropertyFields) {
if (typeof router[property] === 'object') {
instance[property] = {...router[property]} // makes sure query is not stateful
continue
}
instance[property] = router[property]
}
// Events is a static property on the router, the router doesn't have to be initialized to use it
instance.events = _Router.events
propertyFields.forEach((field) => {
// Here we need to use Object.defineProperty because, we need to return
// the property assigned to the actual router
// The value might get changed as we change routes and this is the
// proper way to access it
Object.defineProperty(instance, field, {
get () {
return router[field]
}
})
})
coreMethodFields.forEach((field) => {
instance[field] = (...args) => {
return router[field](...args)
SingletonRouter.ready(() => {
_Router.events.on(event, (...args) => {
const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(1)}`
if (SingletonRouter[eventField]) {
try {
SingletonRouter[eventField](...args)
} catch (err) {
console.error(`Error when running the Router event: ${eventField}`)
console.error(`${err.message}\n${err.stack}`)
}
}
})
})
})
get () {
return _Router.events
}
})