Skip to main content
Context is an object that provides access to HTTP request-related information during Sonamu API method execution. It can be injected as a parameter to API methods and is managed through AsyncLocalStorage.

Type Definition

The Context type is composed of Intersection Types (&):
  • Base Context object (request, reply, etc.)
  • AuthContext: Authentication-related properties (user, session)
  • ContextExtend: Project-specific extension properties
This structure allows Context to be extensible while maintaining type safety.

Context Properties

request

The Fastify Request object representing the current HTTP request. Provides access to request URL, method, query parameters, etc.

reply

The Fastify Reply object representing the current HTTP response. Used for setting response headers, changing status codes, etc.

headers

HTTP request headers. Same value as request.headers, provided for convenience.

createSSE

Function for creating Server-Sent Events (SSE) streams. Enables creating type-safe event streams using Zod schemas. Usage Example:

naiteStore

Storage used by the Naite testing framework. Used for storing mocked data or snapshot information during tests.

locale

The locale (language setting) of the current request. This property always has a value. Parses the Accept-Language header and automatically selects one of the supported locales. If no match is found, the defaultLocale is used. Configuration Example:
Usage Example:

bufferedFiles

List of files uploaded in buffer mode. This property exists when the @upload decorator is applied with the default (consume: "buffer") or @upload({ consume: "buffer" }) configuration. Each file is loaded in memory, allowing flexible operations like MD5 calculation or image processing. BufferedFile Key Properties and Methods:
Basic Usage Example:
Image Processing Example:

uploadedFiles

List of files uploaded in stream mode. This property exists when @upload({ consume: "stream", destination: "..." }) is configured. Files have already been streamed to storage, so only metadata like URL/key is accessible. This mode is suitable for large file uploads. UploadedFile Key Properties and Methods:
Stream Mode Usage Example:
File Upload Handling: - Must be used with the @upload decorator - Buffer mode (default): Use bufferedFiles - loaded in memory, suitable for MD5 calculation/image processing - Stream mode: Use uploadedFiles - streamed directly to storage, suitable for large files - See @upload decorator documentation for more details

AuthContext Properties

Context also includes properties from AuthContext:
  • user: Current authenticated user information (User | null)
  • session: Current session information (Session | null)

Extending Context

To add custom properties to Context in your project, you can extend the ContextExtend interface:
These extended properties can be injected through contextProvider: