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.lookup().

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

Class: RestConnector

RestConnector

Class: RestConnector Static Methods

RestConnector(baseURL, settings)

Arguments
Name Type Description
baseURL string

The base URL

settings object

The settings

Class: RestConnector Instance Methods

restConnector.all(model, filter, [callback])

Query all instances for a given model based on the filter

Arguments
Name Type Description
model string

The model name

filter object

The filter object

[callback] function

The callback function

restConnector.count(model, [callback], where)

Count cannot not be supported efficiently.

Arguments
Name Type Description
model string

The model name

[callback] function

The callback function

where object

The where object

restConnector.create(model, data, [callback])

Create an instance of the model with the given data

Arguments
Name Type Description
model string

The model name

data object

The model instance data

[callback] function

The callback function

restConnector.define(definition)

Hook for defining a model by the data source

Arguments
Name Type Description
definition object

The model description

restConnector.destroy(model, id, [callback])

Delete an instance for a given model/id

Arguments
Name Type Description
model string

The model name

id

The id value

[callback] function

The callback function

restConnector.destroyAll(model, [callback])

Delete all instances for a given model

Arguments
Name Type Description
model string

The model name

[callback] function

The callback function

restConnector.exists(model, id, [callback])

Check the existence of a given model/id

Arguments
Name Type Description
model string

The model name

id

The id value

[callback] function

The callback function

restConnector.find(model, id, [callback])

Find an instance of a given model/id

Arguments
Name Type Description
model string

The model name

id

The id value

[callback] function

The callback function

restConnector.getResource(model)

Get a REST resource client for the given model

Arguments
Name Type Description
model string

The model name

Returns
Name Type Description
result

restConnector.getTypes()

Get types associated with the connector

Returns
Name Type Description
result Array.<String>

The types for the connector

restConnector.installPostProcessor(definition)

Install the post processor

Arguments
Name Type Description
definition object

The model description

restConnector.postProcess(model, data, many)

Post-process the response data

Arguments
Name Type Description
model string

The model name

data

The response data

many boolean

Is it an array

restConnector.preProcess(data)

Pre-process the request data

Arguments
Name Type Description
data

The request data

Returns
Name Type Description
result [object Object]

restConnector.responseHandler(model, [callback])

A factory to build callback function for a response

Arguments
Name Type Description
model string

The model name

[callback] function

The callback function

Returns
Name Type Description
result function

restConnector.save(model, data, [callback])

Save an instance of a given model

Arguments
Name Type Description
model string

The model name

data object

The model instance data

[callback] function

The callback function

restConnector.updateAttributes(model, id, data, [callback])

Update attributes for a given model/id

Arguments
Name Type Description
model string

The model name

id

The id value

data object

The model instance data

[callback] function

The callback function

restConnector.updateOrCreate(model, data, [callback])

Update or create an instance of the model

Arguments
Name Type Description
model string

The model name

data object

The model instance data

[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