Skip to content

HTTP Types

This file defines the core TypeScript types and interfaces used throughout the @sodacore/core HTTP module. These types provide strong typing for configuration, routing, middleware, controller method arguments, SSE packets, WebSocket events, and response transformation.


Types

Configuration

typescript
export type IConfig = {
	port: number,                // The port the HTTP server will listen on
	host?: string,               // Optional host address
	ssePath?: string,            // Optional path for SSE endpoint
};

WebSocket Events

typescript
export type IWebSocketEvents = 'open' | 'close' | 'message' | 'drain';
export type IWebSocketEventListener = (...args: any[]) => Promise<void>;

Defines supported WebSocket events and the listener function signature.


Controller Metadata

typescript
export type IControllerMetaMethodItem = {
	key: string,                 // Method name
	method: string,              // HTTP method (GET, POST, etc.)
	path: string,                // Route path
};

Server Context

typescript
export type IServerContext = {
	request: Request,            // The incoming HTTP request
	uniqueId: string,            // Unique identifier for the request
	channels: string[],          // Associated channels (for SSE/WebSocket)
};

SSE Packets

typescript
export type ISsePacket = {
	id?: string,                 // Optional packet ID
	data: Record<string, any>,   // Data payload to send to the client
};

Routing

typescript
export type IRoute = {
	methodName: string,          // Name of the controller method
	controller: any,             // Controller instance
	transformers: ((context: HttpContext, response: any) => Promise<any>)[], // Response transformers
};

export type IMethodRoutes = Record<string, IRoute>;
export type IRoutes = Record<string, IMethodRoutes>;

Defines the structure for routing tables, mapping HTTP methods and paths to controller methods and transformers.


Controller Method Arguments

typescript
export type IControllerMethodArgItem = {
	type: 'request' | 'server' | 'params' | 'query' | 'headers' | 'cookies' | 'body' | 'url' | 'method',
	index: number,               // Parameter index in the method signature
	name?: string,               // Optional name for params, query, headers, cookies
	format?: string,             // Optional format (e.g., 'json' or 'raw' for body)
};

Describes how to inject arguments into controller methods.


Middleware

typescript
export interface IMiddleware {
	handle: (context: HttpContext) => Promise<Response | boolean | void>,
}

Defines the interface for HTTP middleware classes.


Translation Service

typescript
export interface ITranslationService {
	hasTranslations: () => boolean,
	translate: (query: string, options: { lang: string, country?: string }) => string,
}

Interface for translation services used in response transformation or localization.


Response Transformation

typescript
export type ITransformFunction = (context: HttpContext, response: any) => MaybePromise<any>;

Defines the signature for response transformer functions.


Notes

  • These types are intended for internal use within the Sodacore HTTP module but can be imported for custom controllers, middleware, or plugins.
  • The routing and argument injection types enable flexible and type-safe controller method signatures.
  • Middleware and transformer interfaces allow for extensible request/response processing pipelines.

Released under the Apache-2.0 License.