Skip to main content

Getting Started

Vramework is a minimal TypeScript framework for building function-based APIs that run on serverless platforms or traditional servers with ease.

Installation & Requirements
Requires Node 18+.
Install via npm install @vramework/core or yarn add @vramework/core.

This guide walks you through the setup steps. For more complete examples, see our repos on GitHub.

Step One

Types

Define four key types to get started: Config, UserSession,SingletonServices, and SessionServices.

import {
    CoreConfig,
    CoreServices,
    CoreSingletonServices,
    CoreUserSession,
  } from '@vramework/core'
  
  // This contains all the configuration for the application, created once on start.
  export interface Config extends CoreConfig {}
  
  // This holds the user-session data, retrieved via the HTTPSessionService on each request.
  export interface UserSession extends CoreUserSession {}
  
  // Singleton services, created once on start, shared across all requests.
  export type SingletonServices = CoreSingletonServices & {
    config: Config,
    jwt: JWTService<UserSession>
  }
  
  // Session services, created/destroyed on each request.
  export interface Services extends CoreServices<SingletonServices> {}
  

Step Two

Type Implementations

Next, create the implementations of these types that the Vramework CLI uses.

/**
   * Loads configuration for the application (created once at startup).
   */
  export const createConfig: CreateConfig<Config> = async () => {
    return {
      logLevel: LogLevel.info
    }
  }
  
  /**
   * Creates the singleton services for the application (created once at startup).
   */
  export const createSingletonServices: CreateSingletonServices<
    Config,
    SingletonServices
  > = async (config: Config): Promise<SingletonServices> => {
    const variablesService = new LocalVariablesService()
    const logger = new ConsoleLogger()
    const jwt = new JoseJWTService<UserSession>(keys, logger)
    const httpSessionService = new VrameworkHTTPSessionService<UserSession>(jwt, {})
  
    return {
      config,
      logger,
      variablesService,
      jwt,
      httpSessionService
    }
  }
  
  /**
   * Creates the session services on each request.
   */
  export const createSessionServices: CreateSessionServices<
    SingletonServices,
    Services,
    UserSession
  > = async (_services, _session) => {
    return {}
  }
  

Step Three

Logic Functions

With dependencies in place, define your logic functions. For instance:

// The Function
import { APIFunction } from './vramework/types' 
const getTodo: APIFunction<{ todoId: string }, Todo> = async (
  services,
  data,
  userSession
) => {
  // This method doesn't exist to make it easier to read
  return await getTodo(services.database, data.todoId)
}

// The Wiring
import { addRoute } from '@vramework/core'
addRoute({
  method: 'get',
  route: '/todo/:todoId',
  func: getTodo,
  permissions: {
    isTodoCreator: [isTodoCreator, withinAPILimits],
    isAdmin
  },
  auth: true,
  docs: {
    errors: [NotFoundError],
    description: 'Updates a todo',
    tags: ['todos']
  }
})

Step Four

Vramework CLI

vramework.config.json instructs the CLI where to find routes and generate additional types:

{
    "$schema": "https://raw.githubusercontent.com/vramework/vramework/refs/heads/master/packages/cli/cli.schema.json",
    "tsconfig": "./tsconfig.json",
    "routeDirectories": ["src"],
    "outDir": ".vramework"
  }
  
Generate Necessary Files
Then run npx @vramework/cli to generate all the necessary files.

Step Five

Integration and Deployment

Vramework can integrate with various platforms:

WebSocket Examples
Examples with WebSockets are not yet included in this section.

Required: @vramework/express-middleware

import express from 'express'
  import { vrameworkMiddleware } from '@vramework/express-middleware'
  import { createSessionServices } from '../src/services.js'
  
  import '../.vramework/vramework-bootstrap'
  
  const start = async () => {
    const app = express()
    const port = 3000
  
    const config = await createConfig()
    const singletonServices = await createSingletonServices(config)
  
    app.use(
      vrameworkMiddleware(singletonServices, createSessionServices, {
        respondWith404: false,
      })
    )
  
    app.listen(port, () => {
      console.log(`Example app listening on port ${port}`)
    })
  }
  
  start()
  
Express Deployment
This app starts an Express server on port 3000. The vramework middleware processes routes.

Next Steps

Where to Go from Here

You now have a working understanding of Vramework. Explore advanced topics like authentication, real-time messaging, or connect with the community on GitHub!

Happy Coding!
We look forward to your feedback and contributions.