BFF patterns
Checking more folders at build time
routePatterns
Extend routePatterns to server/routes and layers.
By default only server/api/** must use defineBffHandler. If you serve user data from server/routes/ too, add those folders. The array replaces the default, so keep server/api/** in it.
nuxt.config.ts
export default defineNuxtConfig({
modules: ['@kurashina-design/nuxt-bff-guard'],
bffGuard: {
verifyUser: './server/auth/verify-user.ts',
routePatterns: [
'server/api/**',
'server/routes/account/**',
'server/routes/export/**',
],
},
})
server/routes/export/orders.csv.get.ts
import { defineBffHandler } from '#bff-guard'
export default defineBffHandler(async (event) => {
setResponseHeader(event, 'content-type', 'text/csv; charset=utf-8')
return toCsv(await listOrders(event.context.user!.id))
})
Layers
Patterns apply to every Nitro scan directory, including the server/ folders of Nuxt layers. A route that a shared layer adds to server/api must use the guard as well, so a layer cannot add an unprotected API to your app without the build noticing.
Files that are not routes
Every script file inside a checked folder is treated as a route, as Nitro does. Keep helpers, schemas and types in server/utils/ or another folder outside the patterns.