StatusFlow function is the core utility of the library that generates standardized HTTP response objects with bilingual support (English and Spanish).
Function Signature
Parameters
The function accepts a single options object with the following properties:The HTTP status code. Must be a valid HTTP status code (e.g., 200, 404, 500).If an invalid or unsupported code is provided, the function returns a generic error response.
The language for the response message.
'en': English'es': Spanish (default)
Additional custom fields to include in the response object.These fields are spread into the final response, allowing you to add application-specific data to the standardized response structure.
Custom message to replace the default message for the status code.When provided, this overrides the standard message from the status code database while preserving all other metadata.
StatusFlowOptions Interface
Return Value
The function returns an object with the following structure:Indicates whether the status code represents a successful response.
truefor 1xx, 2xx, and 3xx status codesfalsefor 4xx and 5xx status codes
The message associated with the status code in the specified language.If
overrideMessage was provided in the options, this will be the custom message.The HTTP status code that was requested.
Additional metadata about the status code.
Any additional fields provided in the
extra parameter are spread into the response.Examples
Basic Usage
Using English Language
With Custom Message
With Extra Fields
Error Case - Invalid Code
Use Cases
- Consistent API Responses: Generate uniform response structures across your API
- Bilingual Applications: Easily switch between English and Spanish responses
- Error Handling: Provide detailed error information with context
- Logging: Include structured metadata for debugging and monitoring
- Client Communication: Send standardized, localized error messages to clients
Related
- StatusFlowCodes - Use predefined constants for status codes
- Response Utilities - Helper functions for creating responses
- Middleware - Integrate StatusFlow with Express middleware