Note:
HeliumTS is under pre-beta and active development. Expect bugs and breaking changes. If you find any issues, please report them in our GitHub
A stable release is planned for early December 2025.
Context API
Every RPC method and HTTP handler in Helium receives a HeliumContext object as the second parameter. This context provides access to request metadata, including the client IP, headers, and other connection information.
Context Structure
1interface HeliumContext {2 req: {3 ip: string; // Client IP (respects trustProxyDepth config)4 headers: http.IncomingHttpHeaders; // Request headers5 url?: string; // Request URL6 method?: string; // HTTP method7 raw: http.IncomingMessage; // Raw Node.js request object8 };9 [key: string]: unknown; // Custom properties from middleware10}
Usage in RPC Methods
1import { defineMethod } from "heliumts/server";23export const getClientInfo = defineMethod(async (args, ctx) => {4 // Access client IP (extracted based on trustProxyDepth configuration)5 console.log("Client IP:", ctx.req.ip);67 // Access request headers8 const userAgent = ctx.req.headers["user-agent"];9 const acceptLanguage = ctx.req.headers["accept-language"];1011 // Access WebSocket upgrade request details12 console.log("Connection URL:", ctx.req.url);1314 return {15 ip: ctx.req.ip,16 userAgent,17 language: acceptLanguage,18 };19});
Usage in HTTP Handlers
1import { defineHTTPRequest } from "heliumts/server";23export const apiEndpoint = defineHTTPRequest("POST", "/api/data", async (req, ctx) => {4 // Access client IP5 console.log("Client IP:", ctx.req.ip);67 // Access request headers8 const authorization = ctx.req.headers["authorization"];910 // Check if request is from a specific IP range11 if (ctx.req.ip.startsWith("10.0.")) {12 return { error: "Internal network not allowed" };13 }1415 return { success: true };16});
Custom Context Properties
Middleware can add custom properties to the context:
1import { middleware } from "heliumts/server";23export const authMiddleware = middleware(async (context, next) => {4 // Add custom property5 context.ctx.user = await getUserFromToken(context.ctx.req.headers["authorization"]);67 await next();8});