Other Frameworks
Vla works with any server-side TypeScript framework. Here’s how to integrate it.
Install
Section titled “Install”npm install vlaCore Integration Pattern
Section titled “Core Integration Pattern”The integration pattern for any framework follows these steps:
1. Create a Kernel
Section titled “1. Create a Kernel”import { Kernel } from 'vla'
export const kernel = new Kernel()2. Set Up Request Scoping
Section titled “2. Set Up Request Scoping”Create a scoped kernel for each request using your framework’s middleware or hook system:
import { Vla } from 'vla'import { kernel } from './data/kernel'
// In your request handler/middlewarefunction handleRequest() { return Vla.withKernel(kernel.scoped(), () => { // Your request logic here })}3. Invoke Actions
Section titled “3. Invoke Actions”class GetUser extends Vla.Action { async handle(id: string) { return { id, name: 'User' } }}
// In your route handlerconst user = await GetUser.invoke('123')Two Integration Approaches
Section titled “Two Integration Approaches”Global Kernel Provider (Recommended for React-based frameworks)
Section titled “Global Kernel Provider (Recommended for React-based frameworks)”Use Vla.setInvokeKernelProvider() to provide a scoped kernel globally:
import { Vla } from 'vla'import { kernel } from './data/kernel'
Vla.setInvokeKernelProvider(() => { return kernel.scoped()})
// Now you can invoke actions anywhereconst user = await GetUser.invoke('123')For frameworks with request caching (like React’s cache()):
import { cache } from 'react'import { Vla } from 'vla'import { kernel } from './data/kernel'
Vla.setInvokeKernelProvider( cache(() => { return kernel.scoped() }))Middleware Pattern (Recommended for traditional servers)
Section titled “Middleware Pattern (Recommended for traditional servers)”Use Vla.withKernel() in middleware:
import { Vla } from 'vla'import { kernel } from './data/kernel'
app.use((req, res, next) => { Vla.withKernel(kernel.scoped(), next)})Adding Request Context
Section titled “Adding Request Context”To access framework-specific request data (cookies, headers, etc.):
// 1. Create a contextimport { Vla } from 'vla'
export const AppContext = Vla.createContext<{ request: YourFrameworkRequest}>()
// 2. Provide it when creating scoped kernelVla.setInvokeKernelProvider(() => { return kernel.scoped().context(AppContext, { request: getCurrentRequest() })})
// Or in middleware:app.use((req, res, next) => { Vla.withKernel( kernel.scoped().context(AppContext, { request: req }), next )})
// 3. Use in your classesclass GetCurrentUser extends Vla.Action { ctx = this.inject(AppContext)
async handle() { // Access request data const sessionId = this.ctx.request.cookies.get('session_id') }}That’s It
Section titled “That’s It”The key is to ensure a new scoped kernel is created for each request. Once that’s set up, you’re ready to use Vla. Check out the guides to learn how to structure your code with Actions, Services, and Repos.