-
Notifications
You must be signed in to change notification settings - Fork 19
Body
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.
/* if in src/ */
import { ParameterContainer } from './models/Core'
Body extends Immutable.Record using the following pattern:
Class Body extends Immutable.Record({
type: null,
constraints: Immutable.List()
})
-
Body.type
expects eithernull
,urlEncoded
, orformData
. ThisBody.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
expects an Immutable.List ofParameters
. These Parameters should all have theirkey
andvalue
fields set.
In addition to these fields, Body
also provides a function that can help manipulate its data:
-
Body.filter(parametersContainer)
filters the ParameterContainer's content based its validity against the List of Parameters defined in itsconstraints
field. Internally, this methods calls its sister function in ParameterContainer,paramContainer.filter(this.get('constraints'))
. For more information on the behavior ofParameterContainer.filter
, please read the corresponding section in ParameterContainer.
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])