HttpContext
This file defines the HttpContext
class, which encapsulates all contextual information about an HTTP request in the @sodacore/core
HTTP module. An instance of this class is passed as the last parameter to all HTTP controller methods, providing convenient access to request data, cookies, session, authentication, and utility methods.
Features
- Request and Server Access: Provides direct access to the underlying
Request
andServer
objects. - URL and Query Handling: Parses and exposes the request URL and query parameters.
- Cookie Management: Parses cookies and provides methods to access individual cookies.
- Session and Auth Storage: Manages session and authentication data using internal maps.
- Header and Body Access: Provides methods to retrieve headers and parse the request body as JSON or raw text.
- Utility Methods: Includes helpers for parameter conversion and method retrieval.
Usage
An instance of HttpContext
is automatically provided to controller methods:
async myControllerMethod(param1, param2, context: HttpContext) {
const userId = context.getAuth('userId');
const cookies = context.getCookies();
const query = context.getQuery();
const body = await context.getBody();
}
API
Constructor
constructor(request: Request, server: Server)
- request: The incoming HTTP request.
- server: The Bun server instance.
Methods
URL and Query
getUrl(): URL
Returns the parsed URL object for the request.getQuery<T = Record<string, any>>(name?: string): T | string | number
Returns a specific query parameter by name, or the entire query object.getParam(name?: string): string | number | URLSearchParams
Returns a specific URL parameter or the entire search params object.
Cookies
getCookies(): Map<string, string>
Returns a map of all cookies.getCookie(name: string): string | undefined
Returns the value of a specific cookie.
Session
getSession(key?: string): any
Returns a session value by key, or the entire session map.setSession(key: string, value: any): void
Sets a session value.getSessionKeys(): IterableIterator<string>
Returns an iterator of all session keys.
Authentication
setAuth(key: string, value: any): void
Sets an authentication value.getAuth<T = any>(key: string): T
Returns an authentication value by key.getAuthKeys(): IterableIterator<string>
Returns an iterator of all authentication keys.
Request and Server
getRequest(): Request
Returns the original request object.getServer(): Server
Returns the server instance.
Headers and Body
getHeader(name?: string): string | Headers
Returns a specific header value or all headers.getBody<T = any>(format: 'json' | 'raw' = 'json'): Promise<T>
Returns the request body as JSON or raw text.
Miscellaneous
getMethod(): string
Returns the HTTP method used for the request.
Private Methods
asNumber(value: any): any
Converts a value to a number if possible, otherwise returns the original value.
Notes
- The context is designed to be extensible and can be used by middleware to store additional data in the session or auth maps.
- All methods are synchronous except for
getBody
, which returns a promise. - This class is intended for internal use by the Sodacore HTTP controller system.