如何将路由配置参数传递给 hapijs 插件?
How to pass route configuration parameters to a hapijs plugin?
我正在尝试定义可由插件检查的附加路由参数。
插件看起来像这样:
import { Server, Request } from "@hapi/hapi"
declare module "@hapi/hapi" {
export interface PluginSpecificConfiguration {
permissions: string[]
}
}
const permissions = () => {
return {
plugin: {
name: "permissions",
register: async (app: Server) => {
app.ext("onRequest", (request: Request, h) => {
Log.info(
`permissions: ${request.route.settings.plugins?.permissions}`,
)
return h.continue
})
},
},
}
}
export default async ({ app }: { app: Server }) => {
app.register(permissions())
}
我的路线:
export default ({ app }: Readonly<{ app: Server }>) => {
app.route({
method: "GET",
options: {
auth: "session",
handler: async ({ auth, query }) => {
// omitted
},
plugins: {
permissions: ["permission 1"],
},
},
path: "/users",
})
}
但是,当插件收到请求时,permissions
字段总是 undefined
。
出于某种原因,插件信息未通过 onRequest
事件传递。但是它用于 onPreHandler
事件,因此我能够进行更改:
app.ext("onPreHandler", (request: Request, h) => {
Log.info(
`permissions: ${request.route.settings.plugins?.permissions}`,
)
return h.continue
})
我正在尝试定义可由插件检查的附加路由参数。
插件看起来像这样:
import { Server, Request } from "@hapi/hapi"
declare module "@hapi/hapi" {
export interface PluginSpecificConfiguration {
permissions: string[]
}
}
const permissions = () => {
return {
plugin: {
name: "permissions",
register: async (app: Server) => {
app.ext("onRequest", (request: Request, h) => {
Log.info(
`permissions: ${request.route.settings.plugins?.permissions}`,
)
return h.continue
})
},
},
}
}
export default async ({ app }: { app: Server }) => {
app.register(permissions())
}
我的路线:
export default ({ app }: Readonly<{ app: Server }>) => {
app.route({
method: "GET",
options: {
auth: "session",
handler: async ({ auth, query }) => {
// omitted
},
plugins: {
permissions: ["permission 1"],
},
},
path: "/users",
})
}
但是,当插件收到请求时,permissions
字段总是 undefined
。
出于某种原因,插件信息未通过 onRequest
事件传递。但是它用于 onPreHandler
事件,因此我能够进行更改:
app.ext("onPreHandler", (request: Request, h) => {
Log.info(
`permissions: ${request.route.settings.plugins?.permissions}`,
)
return h.continue
})