Skip to main content
Sonamu provides multiple methods to access the Context. These methods are useful when you need Context outside of API methods.

Sonamu.getContext()

Returns the Context of the currently executing request. Managed through AsyncLocalStorage, so it can be called anywhere within the same request stack.

Signature

Return Value

  • Context: The Context object of the current request
  • Test Environment: Empty Context object (request and reply are null)

Exceptions

  • Error: When Context cannot be found (production environment only)

Usage Examples

Using in Helper Functions

Using in Service Layer

Test Environment Behavior

In test environments, returns an empty Context without throwing errors even when Context is not injected:
For tests that need actual Context, use runWithMockContext, or testAs when you need to specify user info:

File Upload Context

For file upload requests, uploaded file information is accessed through Sonamu.getContext(). Available only in methods with the @upload decorator. Two properties are used depending on the upload mode:
  • Buffer mode (default): bufferedFiles - files loaded in memory, suitable for MD5 calculation/image processing
  • Stream mode: uploadedFiles - files streamed directly to storage, suitable for large files

Signature

Type

Notes

  • File properties are only set when using the @upload decorator
  • In buffer mode (default), use bufferedFiles; in stream mode, use uploadedFiles
  • For single file uploads, access via bufferedFiles?.[0] or uploadedFiles?.[0]

Usage Examples

Single File Upload (Buffer Mode)

Multiple File Upload (Buffer Mode)

Large File Upload (Stream Mode)

Using in Helper Functions

BufferedFile (Buffer Mode)

UploadedFile (Stream Mode)

Precautions

AsyncLocalStorage Stack

getContext() works through AsyncLocalStorage, so it must be called within the same asynchronous execution stack:

@upload Decorator Required

The bufferedFiles/uploadedFiles properties can only be used in methods with the @upload decorator: