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:runWithMockContext:
File Upload Context
For file upload requests, uploaded file information is accessed throughSonamu.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
@uploaddecorator - In buffer mode (default), use
bufferedFiles; in stream mode, useuploadedFiles - For single file uploads, access via
bufferedFiles?.[0]oruploadedFiles?.[0]
Usage Examples
Single File Upload (Buffer Mode)
Multiple File Upload (Buffer Mode)
Large File Upload (Stream Mode)
Using in Helper Functions
Related Types
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
ThebufferedFiles/uploadedFiles properties can only be used in methods with the @upload decorator: