Home Intro Source Mebo GitHub
import WebResponse from 'mebo/src/Writers/WebResponse.js'
public class | source

WebResponse

Extends:

Writer → WebResponse

Web output writer.

This writer is used by the output of the web handler (Web).

In case the value is an exception then it's treated as WebResponse._errorOutput otherwise the value is treated as WebResponse._successOutput.

When an action is executed through a handler it can define options for readers and writers via Metadata support. For instance, you can use it to provide a custom result for a specific handler:

class MyAction extends Mebo.Action{

   // ...

   async _perform(data){
     // ...
   }

   async _after(err, value){
     // defining a custom result that only affects the web handler
     // this call could be done inside of the _perform method. However, we
     // are defining it inside of the _after to keep _perform as
     // abstract as possible. Since, _after is always called (even during
     // an error) after the execution of the action, it provides a way to
     // hook and define custom metadata related with the result.
     if (!err){
         // defining a custom output option
         this.setMeta('$webResult', {
             message: 'My custom web result!',
         });
     }
   }

   // ...
}

Options Summary

Option Name Description Default Value
headers plain object containing the header names (in camel case convention) that should be used in the response {}
headersOnly if enabled ends the response without any data
status success status code (the error status code is driven by the status defined as a member of the exception) 200
root plain object that gets deep merged at the root of the json output of a success result, for instance:
{data: {...}, <rootContentsA>: ..., <rootContentsB> : ...}
{}
result Overrides the value returned by Writer.value to an arbitrary value (only affects the success output)
resultLabel custom label used by the success output when the value is serialized using json. This label is used to hold the result under data, for instance:
{data: {<resultLabel>: value}}

In case of undefined (default) then a fallback label is used based on the value type:
- primitive values are held under 'value'
- array value is held under 'items'
- object is assigned with '' (empty string)
* when an empty string is used, the value gets merged to the result.data


When defining options through the metadata support, it can done using option vars. Mebo comes bundled with pre-defined option vars for most of the options available for the readers & writers. The complete list of the option vars can be found at Metadata documentation.

Example of defining the headers option from inside of an action through the metadata support:

// defining 'Content-Type' header
class MyAction extends Mebo.Action{
   async _perform(data){

     // 'Content-Type' header
     this.setMeta('$webHeaders', {
       contentType: 'application/octet-stream',
     });

     // ...
   }
}

Also, headers can be defined through 'before action middlewares' (Web.addBeforeAction and Web.addBeforeAuthAction)

Constructor Summary

Public Constructor
public

constructor(value: *, res: Object)

Creates a web response writer

Method Summary

Public Methods
public

Returns the response object created by express

Protected Methods
protected

Implements the response for an error value.

protected

Implements the response for a success value.

Inherited Summary

From class Writer
public

option(path: string, defaultValue: *): *

Returns an option

public

Serializes a writer value (Writer.value) in case the value is an exception it's serialize as Writer._errorOutput otherwise it's serialized as Writer._successOutput.

public

setOption(path: string, value: *, merge: boolean)

Sets a value under the options

public

value(): *

Returns the value that should be serialized (Writer.serialize) by the writer.

protected

Translates an Error to a data structure that is later serialized by a writer implementation as output.

protected

Translates the success value to a data structure that is later serialized by a handler implementation as output.

Public Constructors

public constructor(value: *, res: Object) source

Creates a web response writer

Override:

Writer#constructor

Params:

NameTypeAttributeDescription
value *

arbitrary value passed to the writer

res Object

express res object

Public Methods

public response(): Object source

Returns the response object created by express

Return:

Object

See:

Protected Methods

protected _errorOutput() source

Implements the response for an error value.

Any error can carry a HTTP status code. It is done by defining status to any error (for instance err.status = 501;). This practice can be found in all errors shipped with mebo (Conflict, NoContent, NotFound and ValidationFail). In case none status is found in the error then 500 is used automatically.

The error response gets automatically encoded using json, following the basics of google's json style guide. In case of an error status 500 the standard result is ignored and a message Internal Server Error is used instead.

Further information can be found at base class documentation Writer._errorOutput.

Override:

Writer#_errorOutput

protected _successOutput() source

Implements the response for a success value.

A readable stream value is piped using 'application/octet-stream' by default (if it has not been defined by the header option 'contentType'), otherwise for non-readable stream value it's automatically encoded using json, following the basics of google's json style guide.

Further information can be found at base class documentation Writer._successOutput.

Override:

Writer#_successOutput

See: