LoopBack REST Builder

Class: RequestBuilder

RequestBuilder

Class: RequestBuilder Static Methods

RequestBuilder.compile(req)

Load the REST request from a JSON object

Arguments
Name Type Description
req object

The request json object

Returns
Name Type Description
result RequestBuilder

RequestBuilder([method], url, [requestFunc])

Arguments
Name Type Description
[method] string

HTTP method

url string or object

The HTTP URL or an object for the options including method, url properties

[requestFunc] function

Custom request with defaults

Returns
Name Type Description
result RequestBuilder

RequestBuilder.resource(modelCtor, baseUrl)

Attach a model to the REST connector

Arguments
Name Type Description
modelCtor function

The model constructor

baseUrl string

The base URL

Returns
Name Type Description
result RestResource

Class: RequestBuilder Instance Methods

requestBuilder._request(uri, options, cb)

Delegation to request Please note the cb takes (err, body, response)

Arguments
Name Type Description
uri string

The HTTP URI

options options

The options

cb function

The callback function

requestBuilder.attach(field, file, filename)

Queue the given file as an attachment with optional filename.

Arguments
Name Type Description
field string
file string
filename string
Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.auth(user, pass)

Set Authorization field value with user and pass.

Arguments
Name Type Description
user string

The user name

pass string

The password

Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.body(body)

Send body, defaulting the .type() to "json" when an object is given.

Examples:

  // manual json
  request.post('/user')
    .type('json')
    .body('{"name":"tj"}');

  // auto json
  request.post('/user')
    .body({ name: 'tj' });

  // manual x-www-form-urlencoded
  request.post('/user')
    .type('form')
    .body('name=tj');

  // auto x-www-form-urlencoded
  request.post('/user')
    .type('form')
    .body({ name: 'tj' });

  // string defaults to x-www-form-urlencoded
  request.post('/user')
    .body('name=tj')
    .body('foo=bar')
    .body('bar=baz');
Arguments
Name Type Description
body string or object
Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.buffer()

Enable / disable buffering.

Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.build(options)

Build the request by expanding the templatized properties with the named values from options

Arguments
Name Type Description
options object
Returns
Name Type Description
result

requestBuilder.header(field, val)

Set header field to val, or multiple fields with one object.

Examples:

 req.get('/')
   .header('Accept', 'application/json')
   .header('X-API-Key', 'foobar');

 req.get('/')
   .header({ Accept: 'application/json', 'X-API-Key': 'foobar' });
Arguments
Name Type Description
field string or object
val string
Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.invoke(parameters, cb)

Invoke a REST API with the provided parameter values in the parameters object

Arguments
Name Type Description
parameters object

An object that provide {name: value} for parameters

cb function

The callback function

requestBuilder.method(method)

Configure the HTTP method

Arguments
Name Type Description
method string

The HTTP method

Returns
Name Type Description
result RequestBuilder

requestBuilder.operation(parameterNames)

Map the request builder to a function

Arguments
Name Type Description
parameterNames String or Array.<String>

The parameter names that define the order of args. It be an array of strings or multiple string arguments

Returns
Name Type Description
result Function

A function to invoke the REST operation

requestBuilder.parse(fn)

Define the parser to be used for this response.

Arguments
Name Type Description
fn function

The parser function

Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.query(val)

Add query-string val.

Examples:

request.get('/shoes') .query('size=10') .query({ color: 'blue' })

Arguments
Name Type Description
val object or string
Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.redirects(n)

Set the max redirects to n.

Arguments
Name Type Description
n number
Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.responsePath(responsePath)

Set the response json path

Arguments
Name Type Description
responsePath string

The JSONPath to be applied against the HTTP body

Returns
Name Type Description
result RequestBuilder

requestBuilder.timeout(ms)

Set timeout to ms.

Arguments
Name Type Description
ms Number
Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.toJSON()

Serialize the RequestBuilder to a JSON object

Returns
Name Type Description
result [object Object]

requestBuilder.type(type)

Set Content-Type response header passed through mime.getType().

Examples:

 request.post('/')
   .type('xml')
   .body(xmlstring);

 request.post('/')
   .type('json')
   .body(jsonstring);

 request.post('/')
   .type('application/json')
   .body(jsonstring);
Arguments
Name Type Description
type string
Returns
Name Type Description
result RequestBuilder

for chaining

requestBuilder.url(url)

Configure the url

Arguments
Name Type Description
url string

The HTTP URL

Returns
Name Type Description
result RequestBuilder

LoopBack REST Connector

Class: RequestBuilder

RequestBuilder

Class: RequestBuilder Static Methods

initializeDataSource(dataSource, [callback])

Arguments
Name Type Description
dataSource DataSource

The loopback data source instance

[callback] function

The callback function

LoopBack REST Model API

Class: RestConnector

RestConnector

Class: RestConnector Static Methods

RestResource(pluralModelName, baseUrl, [requestFunc])

Arguments
Name Type Description
pluralModelName string

The model name

baseUrl string

The base URL

[requestFunc] function

Custom request with defaults

Returns
Name Type Description
result RestResource

Class: RestConnector Instance Methods

restResource.all(q, [cb])

Map the all/query operation to GET /{model}

Arguments
Name Type Description
q object

query string

[cb] function

callback with (err, results)

restResource.create(obj, [cb])

Map the create operation to HTTP POST /{model}

Arguments
Name Type Description
obj object

The HTTP body

[cb] function

The callback function

restResource.delete(id, [cb])

Map the delete operation to POST /{model}/{id}

Arguments
Name Type Description
id

The id value

[cb] function

The callback function

restResource.deleteAll(id, [cb])

Map the delete operation to POST /{model}

Arguments
Name Type Description
id

The id value

[cb] function

The callback function

restResource.find(id, [cb])

Map the find operation to GET /{model}/{id}

Arguments
Name Type Description
id

The id value

[cb] function

The callback function

restResource.update(id, obj, [cb])

Map the update operation to POST /{model}/{id}

Arguments
Name Type Description
id

The id value

obj object

The HTTP body

[cb] function

The callback function