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

Reader

Direct Subclass:

CliArgs, WebRequest

A reader is used by the handler during the execution (Handler.runAction) to query the Input and Session information that is going be used during the execution of the action.

In case of new implements it's expected to implement the Reader._perform.

When a value is found for an input it's decoded using Input.parseValue where each input implementation has its own way of parsing the serialized data, to find out about how a value is serialized for an specific input type you could simply set an arbitrary value to an input then query it back through Input.serializeValue. The reference bellow shows the basic serialization for the inputs blundled with Mebo:

Input Type Scalar Serialization Vector Serialization (compatible with JSON)
Text 'value' '["valueA","valueB"]'
FilePath '/tmp/a.txt' '["/tmp/a.txt","/tmp/b.txt"]'
Bool 'true' or '1' '[true,false]' or '[1,0]'
Numeric '20' '[20,30]'
Email 'test@email.com' '["test@email.com","test2@email.com"]'
Ip '192.168.0.1' '["192.168.0.1","192.168.0.2"]'
Timestamp '2017-02-02T22:26:30.431Z' '["2017-02-02T22:26:30.431Z","2017-02-02T22:27:19.066Z"]'
Url 'http://www.google.com' '["http://www.google.com","http://www.wikipedia.com"]'
Version '10.1.1' '["10.1.1","10.2"]'
Buf 'aGVsbG8=' '["aGVsbG8=","d29ybGQ="]'
Hex 'ffff00' '["ffff00","ff"]'
Hash 'd65709ab' '["d65709ab","b94d6fe4"]'
UUID '075054e0-810a-11e6-8c1d-e5fb28c699ca' '["075054e0-810a-11e6-8c1d-e5fb28c699ca","98e631d3-6255-402a-88bd-66056e1ca9df"]'


Options: Custom options can be assigned to readers (Reader.setOption). They are passed from the handler to the reader during the handler's execution (Handler.runAction).

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

// setting reading options
myHandler.runAction('myAction', {
 someOption: 10,
});

When an action is executed through a handler it can define options via the Metadata support. Detailed information about that can be found at Metadata:

@Mebo.register('myAction')
class MyAction extends Mebo.Action{
   constructor(){
     super();

     // defining a custom reading option
     this.setMeta('$myOption', {
       someOption: 10,
     });

     // ...
   }
}

Hiding inputs from readers: A reader only sees inputs that are capable of serialization (Input.isSerializable) or visible inputs. Therefore, any input assigned with the property hidden is not visible by readers, for instance:

class Example extends Mebo.Action{
  constructor(){
    super();
    this.createInput('readerCantSeeMe: numeric', {hidden: true});
    this.createInput('readerSeeMe: numeric');
  }
}

Constructor Summary

Public Constructor
public

constructor(action: Action)

Creates a reader.

Method Summary

Public Methods
public

Returns the action associated with the reader.

public

Reads the autofill information and returns it through a plain object.

public

Reads the input values and returns it through a plain object.

public

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

Returns an option

public

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

Sets a value under the options

public

Returns a list of valid input names that should be used for the parsing.

Protected Methods
protected

_perform(inputList: Array<Input>): Promise<Object>

This method should be re-implemented by derived classes to perform the handler parsing.

Public Constructors

public constructor(action: Action) source

Creates a reader.

Params:

NameTypeAttributeDescription
action Action

action used for the querying the values

Public Methods

public action(): Action source

Returns the action associated with the reader.

Return:

Action

public async autofillValues(): Promise<Object> source

Reads the autofill information and returns it through a plain object.

If the autofill information is already assigned under autofill (Action.session) then that information is skipped otherwise it adds the parsed information the result.

Return:

Promise<Object>

public async inputValues(): Promise<object> source

Reads the input values and returns it through a plain object.

Return:

Promise<object>

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 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 validInputNames(): Array<string> source

Returns a list of valid input names that should be used for the parsing. This avoids hidden inputs to get exposed in the parsing.

Return:

Array<string>

Protected Methods

protected _perform(inputList: Array<Input>): Promise<Object> source

This method should be re-implemented by derived classes to perform the handler parsing.

It should return a plain object containing the input name and the value for that. Where any input value from either String or Array types are considered valid values that are later (Reader.inputValues, Reader.autofillValues) used to parse the value of the input (Input.parseValue), otherwise the value is ignored.

Only return the ones that were found by the parsing. Also, in case of any error during the parsing then an exception should be raised.

Params:

NameTypeAttributeDescription
inputList Array<Input>

Valid list of inputs that should be used for the parsing

Return:

Promise<Object>