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

WebRequest

Extends:

Reader → WebRequest

Web request reader.

This reader is used by the Web handler to query values from a request.

This reader supports all serializable inputs. It deals with file uploads automatically; therefore any FilePath input becomes a potential upload field. When that is the case the input gets assigned with the file path about where the file has been uploaded to. By default it tries to keep the original uploaded file name by replacing any illegal character with underscore, however you can control this behavior via uploadPreserveName (if disabled each uploaded file gets named with an unique name).

This reader works by looking for the input names in the request, for instance:

http://.../?myInput=10&myOtherInput=20

class MyAction extends Mebo.Action {
  constructor(){
    super();
    this.createInput('myInput: numeric');
    this.createInput('myOtherInput: numeric');
  }
}

When a value is found for the input, it gets loaded via 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 the input you are interested then query it back through Input.serializeValue. Also, Mebo provides a reference datasheet about the serialization forms for the inputs bundled with it, found at Reader.

As supported for the cli handler (--help/-h) you can also query a basic help page for any action through the web handler by including: help to the query string: http://.../?help By doing that it renders a page with the description of the action and helpful information about the inputs. This setting is available through handler/web/allowHelp (default true).

You can define the description displayed in the help of each input by setting the input's property description. Also, the description for the action itself can be defined by setting the action's metadata description.

Route parameters: If an webfied action contains route parameters defined (/users/:userId/books/:bookId) this reader is going to try to find them under the action input names. Therefore when a route parameter matches to the name of an input then the value of the parameter is loaded to the input.

Vector Inputs: Supported conventions for array parameters:

  • Serialized vector value (JSON Style)

    http://.../?vectorInput=["a", "b", "c"]
    
  • Repeated param names

    http://.../?vectorInput[]=a&vectorInput[]=b&vectorInput[]=c
    

    or

    http://.../?vectorInput=a&vectorInput=b&vectorInput=c
    

Options Summary

Option Name Description
uploadDirectory directory used for placing file uploads in, default value (TMP_DIR/upload) driven by:
Settings.get('reader/webRequest/uploadDirectory')
uploadPreserveName enabled by default it tries to keep the original final name of uploads, any illegal character is replaced by underscore, otherwise if disabled it gives a random name to the upload, default value driven by:
Settings.get('reader/webRequest/uploadPreserveName')
uploadDefaultMaxFileSize total maximum file size about all uploads in bytes, default value (4 mb) driven by:
Settings.get('reader/webRequest/uploadDefaultMaxFileSize')
maxFields Limits the number of fields that the querystring parser will decode, default value (1000) driven by:
Settings.get('reader/webRequest/maxFields')
maxFieldsSize Limits the amount of memory all fields together (except files) can allocate in bytes, default value (2 mb) driven by:
Settings.get('reader/webRequest/maxFieldsSize') [2 mb]


Example about defining a custom uploadDefaultMaxFileSize option from inside of an action through the metadata support:

class MyAction extends Mebo.Action{
   constructor(){
     super();

     // 'uploadDefaultMaxFileSize' option
     this.setMeta('handler.web.readOptions', {
       uploadDefaultMaxFileSize: 10 * 1024 * 1024,
     });
   }
}

Constructor Summary

Public Constructor
public

constructor(action: Action, req: Object)

Creates a web request reader

Method Summary

Public Methods
public

Returns the request object created by express

Protected Methods
protected

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

Implements the web request reader

protected

async _renderHelp(inputList: Array<Input>): Promise<string>

Computes the output displayed as help

Inherited Summary

From class Reader
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

_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, req: Object) source

Creates a web request reader

Override:

Reader#constructor

Params:

NameTypeAttributeDescription
action Action

action that should be used by the reader

req Object

request object created by express-js

Public Methods

public request(): Object source

Returns the request object created by express

Return:

Object

See:

Protected Methods

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

Implements the web request reader

Override:

Reader#_perform

Params:

NameTypeAttributeDescription
inputList Array<Input>

Valid list of inputs that should be used for the parsing

Return:

Promise<Object>

protected async _renderHelp(inputList: Array<Input>): Promise<string> source

Computes the output displayed as help

Params:

NameTypeAttributeDescription
inputList Array<Input>

Valid list of inputs

Return:

Promise<string>