mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat: Add middleware as function
This commit is contained in:
parent
c17cb24aaa
commit
23919a9e01
@ -159,6 +159,7 @@ function callMiddleware (Components, context, layout) {
|
||||
}
|
||||
|
||||
midd = midd.map(name => {
|
||||
if (typeof name === 'function') return name
|
||||
if (typeof middleware[name] !== 'function') {
|
||||
unknownMiddleware = true
|
||||
this.error({ statusCode: 500, message: 'Unknown middleware ' + name })
|
||||
|
@ -103,6 +103,7 @@ export default async ssrContext => {
|
||||
*/
|
||||
let midd = <%= serialize(router.middleware, { isJSON: true }) %>
|
||||
midd = midd.map((name) => {
|
||||
if (typeof name === 'function') return name
|
||||
if (typeof middleware[name] !== 'function') {
|
||||
ssrContext.error({ statusCode: 500, message: 'Unknown middleware ' + name })
|
||||
}
|
||||
@ -134,6 +135,7 @@ export default async ssrContext => {
|
||||
}
|
||||
})
|
||||
midd = midd.map((name) => {
|
||||
if (typeof name === 'function') return name
|
||||
if (typeof middleware[name] !== 'function') {
|
||||
app.context.error({ statusCode: 500, message: 'Unknown middleware ' + name })
|
||||
}
|
||||
|
@ -154,6 +154,20 @@ test('/meta', async t => {
|
||||
t.deepEqual(state.meta, [{ works: true }])
|
||||
})
|
||||
|
||||
test('/fn-midd', async t => {
|
||||
await page.nuxt.navigate('/fn-midd', true)
|
||||
|
||||
t.is(await page.$text('.title'), 'You need to ask the permission')
|
||||
t.deepEqual(await page.nuxt.errorData(), { message: 'You need to ask the permission', statusCode: 403 })
|
||||
})
|
||||
|
||||
test('/fn-midd?please=true', async t => {
|
||||
await page.nuxt.navigate('/fn-midd?please=true', true)
|
||||
|
||||
const h1 = await page.$text('h1')
|
||||
t.true(h1.includes('Date:'))
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
test.after('Closing server and nuxt.js', t => {
|
||||
nuxt.close()
|
||||
|
@ -204,6 +204,17 @@ test('/meta', async t => {
|
||||
t.true(html.includes('"meta":[{"works":true}]'))
|
||||
})
|
||||
|
||||
test('/fn-midd', async t => {
|
||||
const err = await t.throws(rp(url('/fn-midd'), { resolveWithFullResponse: true }))
|
||||
t.is(err.statusCode, 403)
|
||||
t.true(err.response.body.includes('You need to ask the permission'))
|
||||
})
|
||||
|
||||
test('/fn-midd?please=true', async t => {
|
||||
const { html } = await nuxt.renderRoute('/fn-midd?please=true')
|
||||
t.true(html.includes('<h1>Date:'))
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
test.after('Closing server and nuxt.js', t => {
|
||||
nuxt.close()
|
||||
|
17
test/fixtures/basic/pages/fn-midd.vue
vendored
Normal file
17
test/fixtures/basic/pages/fn-midd.vue
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
<template>
|
||||
<h1>Date: {{ date }}</h1>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
let date
|
||||
|
||||
export default {
|
||||
middleware: ({ query, error }) => {
|
||||
if (!query.please) return error({ message: 'You need to ask the permission', statusCode: 403 })
|
||||
date = Date.now()
|
||||
},
|
||||
asyncData() {
|
||||
return { date }
|
||||
}
|
||||
}
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user