Add possibility to pass extra arguments to generate.routes method

This commit is contained in:
pimlie 2017-11-24 18:02:53 +01:00 committed by Pooya Parsa
parent 947f2844bb
commit 5bacd36590
4 changed files with 126 additions and 8 deletions

View File

@ -52,18 +52,17 @@ export default class Generator {
} }
} }
async initRoutes() { async initRoutes(...args) {
// Resolve config.generate.routes promises before generating the routes // Resolve config.generate.routes promises before generating the routes
let generateRoutes = [] let generateRoutes = []
if (this.options.router.mode !== 'hash') { if (this.options.router.mode !== 'hash') {
try { try {
generateRoutes = await promisifyRoute(this.options.generate.routes || []) generateRoutes = await promisifyRoute(this.options.generate.routes || [], ...args)
} catch (e) { } catch (e) {
console.error('Could not resolve routes') // eslint-disable-line no-console console.error('Could not resolve routes') // eslint-disable-line no-console
throw e // eslint-disable-line no-unreachable throw e // eslint-disable-line no-unreachable
} }
} }
// Generate only index.html for router.mode = 'hash' // Generate only index.html for router.mode = 'hash'
let routes = (this.options.router.mode === 'hash') ? ['/'] : flatRoutes(this.options.router.routes) let routes = (this.options.router.mode === 'hash') ? ['/'] : flatRoutes(this.options.router.routes)
routes = this.decorateWithPayloads(routes, generateRoutes) routes = this.decorateWithPayloads(routes, generateRoutes)

View File

@ -34,23 +34,23 @@ export function isUrl(url) {
return (url.indexOf('http') === 0 || url.indexOf('//') === 0) return (url.indexOf('http') === 0 || url.indexOf('//') === 0)
} }
export function promisifyRoute(fn) { export function promisifyRoute(fn, ...args) {
// If routes is an array // If routes is an array
if (Array.isArray(fn)) { if (Array.isArray(fn)) {
return Promise.resolve(fn) return Promise.resolve(fn)
} }
// If routes is a function expecting a callback // If routes is a function expecting a callback
if (fn.length === 1) { if (fn.length === arguments.length) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fn(function (err, routeParams) { fn((err, routeParams) => {
if (err) { if (err) {
reject(err) reject(err)
} }
resolve(routeParams) resolve(routeParams)
}) }, ...args)
}) })
} }
let promise = fn() let promise = fn(...args)
if (!promise || (!(promise instanceof Promise) && (typeof promise.then !== 'function'))) { if (!promise || (!(promise instanceof Promise) && (typeof promise.then !== 'function'))) {
promise = Promise.resolve(promise) promise = Promise.resolve(promise)
} }

76
test/generator.test.js Normal file
View File

@ -0,0 +1,76 @@
import test from 'ava'
import { Nuxt, Generator } from '../index.js'
test('initRoutes with routes (fn => array)', async t => {
const array = ['/1', '/2', '/3', '/4']
const config = {
generate: {
routes: array
}
}
const nuxt = new Nuxt(config)
const generator = new Generator(nuxt)
const routes = await generator.initRoutes()
t.is(routes.length, array.length)
routes.map((route, index) => {
t.is(route.route, array[index])
})
})
test('initRoutes with routes (fn())', async t => {
const array = ['/1', '/2', '/3', '/4']
const config = {
generate: {
routes() {
return array
}
}
}
const nuxt = new Nuxt(config)
const generator = new Generator(nuxt)
const routes = await generator.initRoutes()
t.is(routes.length, array.length)
routes.map((route, index) => {
t.is(route.route, array[index])
})
})
test('initRoutes with routes (fn(args))', async t => {
const config = {
generate: {
routes(array) {
return array
}
}
}
const nuxt = new Nuxt(config)
const generator = new Generator(nuxt)
const array = ['/1', '/2', '/3', '/4']
const routes = await generator.initRoutes(array)
t.is(routes.length, array.length)
routes.map((route, index) => {
t.is(route.route, array[index])
})
})
test('initRoutes with routes (fn(cb, args))', async t => {
const config = {
generate: {
routes(cb, arg1, arg2, arg3, arg4) {
cb(null, [ arg1, arg2, arg3, arg4 ])
}
}
}
const nuxt = new Nuxt(config)
const generator = new Generator(nuxt)
const array = ['/1', '/2', '/3', '/4']
const routes = await generator.initRoutes(...array)
t.is(routes.length, array.length)
routes.map((route, index) => {
t.is(route.route, array[index])
})
})

View File

@ -70,6 +70,21 @@ test('promisifyRoute (fn => promise)', t => {
}) })
}) })
test('promisifyRoute ((fn(args) => promise))', t => {
const fn = function (array) {
return new Promise((resolve) => {
resolve(array)
})
}
const array = [1, 2, 3]
const promise = Utils.promisifyRoute(fn, array)
t.is(typeof promise, 'object')
return promise
.then((res) => {
t.is(res, array)
})
})
test('promisifyRoute (fn(cb) with error)', t => { test('promisifyRoute (fn(cb) with error)', t => {
const fn = function (cb) { const fn = function (cb) {
cb(new Error('Error here')) cb(new Error('Error here'))
@ -82,6 +97,19 @@ test('promisifyRoute (fn(cb) with error)', t => {
}) })
}) })
test('promisifyRoute (fn(cb, args) with error)', t => {
const fn = function (cb, array) {
cb(new Error('Error here: ' + array.join()))
}
const array = [1, 2, 3, 4]
const promise = Utils.promisifyRoute(fn, array)
t.is(typeof promise, 'object')
return promise
.catch((e) => {
t.is(e.message, 'Error here: ' + array.join())
})
})
test('promisifyRoute (fn(cb) with result)', t => { test('promisifyRoute (fn(cb) with result)', t => {
const array = [1, 2, 3, 4] const array = [1, 2, 3, 4]
const fn = function (cb) { const fn = function (cb) {
@ -95,6 +123,21 @@ test('promisifyRoute (fn(cb) with result)', t => {
}) })
}) })
test('promisifyRoute (fn(cb, args) with result)', t => {
const fn = function (cb, array, object) {
cb(null, { array, object })
}
const array = [1, 2, 3, 4]
const object = { a: 1 }
const promise = Utils.promisifyRoute(fn, array, object)
t.is(typeof promise, 'object')
return promise
.then((res) => {
t.is(res.array, array)
t.is(res.object, object)
})
})
test('chainFn (mutate, mutate)', t => { test('chainFn (mutate, mutate)', t => {
// Pass more than one argument to test that they're actually taken into account // Pass more than one argument to test that they're actually taken into account
const firstFn = function (obj, count) { const firstFn = function (obj, count) {