Skip to content
JonathanMontane edited this page Apr 28, 2016 · 4 revisions

A Body component represents an environment that can be used in a Request. A body has two fields, type and constraints. The constraints the body defines are used to filter a ParameterContainer so that all the Parameters it contains are valid in the environment Body represents.

The constraints defined in the Body are Parameters with the fields key and value that are set. For instance, a Parameter with key: 'Content-Type', value: 'application/json' is a valid constraint object for the Body.

Import

/* if in src/ */
import { ParameterContainer } from './models/Core'

Interface

Body extends Immutable.Record using the following pattern:

Class Body extends Immutable.Record({
    type: null,
    constraints: Immutable.List()
})

Fields

Body.type
  • Body.type expects either null, urlEncoded, or formData. This Body.type is present as a helper to increase readability and ease of use. It should be redundant with the properties defined in the constraints. Other values are also accepted, but will not trigger any specific behavior for Paw Serializers.
Body.constraints
  • Body.constraints expects an Immutable.List of Parameters. These Parameters should all have their key and value fields set.

Methods

In addition to these fields, Body also provides a function that can help manipulate its data:

filter
  • Body.filter(parametersContainer) filters the ParameterContainer's content based its validity against the List of Parameters defined in its constraints field. Internally, this methods calls its sister function in ParameterContainer, paramContainer.filter(this.get('constraints')). For more information on the behavior of ParameterContainer.filter, please read the corresponding section in ParameterContainer.

Example

Please note that this example is abstract, and is there purely to represent the different interactions with Body. The data may be non-sensical.

import parameters from './samples/parameters-examples'
import containers from './samples/containers-examples'
import bodies from './samples/bodies-examples'

import { Body } from './models/Core'

let body = new Body({
    type: 'urlEncoded',
    constraints: new Immutable.List([
        new Parameter({
            key: 'Content-Type',
            value: 'application/x-www-form-urlencoded'
        }),
        new Parameter({
            key: 'source-type',
            value: 'png'
        })
    ])
})

body = body
    .set('type', null)
    .set('constraints', new Immutable.List([ parameters[0], parameters[1] ]))
    .setIn([ 'constraints', 0 ], parameters[2])

let filtered = body
    .filter(containers[0])
Clone this wiki locally