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

Writer

Direct Subclass:

CliOutput, WebResponse

A writer is used to output a value through the Handler.

The output is determined by the kind of value that's passed to the writer where exceptions are interpreted as error output otherwise, the value is interpreted as success value. Therefore, new implements are expected to implement both a success (Writer._successOutput) and error (Writer._errorOutput) outputs.

Options: Custom options can be assigned to writers (Writer.setOption). They are passed from the handler to the writer during the output process (Handler.output).

const myHandler = Mebo.Handler.create('someHandler');

// setting output options during the output
myHandler.output(value, {
 someOption: 10,
});

When an action is executed through a handler it can define options for readers and writers via Metadata support.

Options Summary

Option Name Description Default Value
convertBufferToReadableStream Tells if a buffer value should be converted to a readable stream
result Overrides the value returned by Writer.value to an arbitrary value (only affects the success output)

Constructor Summary

Public Constructor
public

constructor(value: *)

Creates a writer

Method Summary

Public Methods
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 Methods
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: *) source

Creates a writer

Params:

NameTypeAttributeDescription
value *

arbitrary value passed to the writer

Public Methods

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

Returns an option

Params:

NameTypeAttributeDescription
path string

path about where the option is localized (the levels must be separated by '.'). In case of an empty string it returns the entire options

defaultValue *
  • optional

default value returned in case a value was not found for the path

Return:

*

public serialize() source

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) source

Sets a value under the options

Params:

NameTypeAttributeDescription
path string

path about where the option should be stored under the options (the levels must be separated by '.')

value *

value that is going to be stored under the collection

merge boolean
  • optional
  • default: true

this option is used to decide in case of the last level is already existing under the collection, if the value should be either merged (default) or overridden.

public value(): * source

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

Return:

*

Protected Methods

protected _errorOutput(): Object source

Translates an Error to a data structure that is later serialized by a writer implementation as output. This method gets triggered when an exception is passed as value by the Handler.output.

By default the contents of the error output are driven by the err.message, however if an error contains err.toJSON property (ValidationFail.toJSON) then that's used instead of the message.

Also, you can avoid specific errors to be handled via output process by defining the member output assigned with false to the error (for instance err.output = false;). If that is in place the error gets thrown which triggers the event Handler.onErrorDuringOutput.

Tip: You can set the env variable NODE_ENV=development to get the traceback information included in the error output

Return:

Object

protected _successOutput(): Object | Stream source

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

/todo: This value is either driven by the option 'result' (when defined) or by the value defined at constructor time.

All writers shipped with Mebo have support for streams where in case of any readable stream or buffer value are piped to the output, otherwise the result is encoded using JSON (defined per writer bases).

Note: any Buffer value passed to this method gets automatically converted to a readable stream (this behavior is driven by the option 'convertBufferToReadableStream').

This method is called by Handler.output.

Return:

Object | Stream