loopback-connector-rest
The LoopBack REST connector enables applications to interact with other (third party) REST APIs using a template-driven approach.It supports two different styles of API invocations:
Installation
In your application root directory, enter:
This will install the module from npm and add it as a dependency to the application’s file.
Use the data source generator to add a REST data source to your application.
For LoopBack 2.x:
For LoopBack 2.x or 3.0:
$ lb datasource
When prompted, scroll down in the list of connectors and choose REST services (supported by StrongLoop).This adds an entry to , for example:
...
"myRESTdatasource": {
"name": "myRESTdatasource",
"connector": "rest"
}
...
Configuring a REST data source
Configure the REST connector by editing datasources.json
manually (for example using the Google Maps API):
/server/datasources.json
The operations
property is an array of objects, each of which can have these properties:
template
: An object that defines a custom method using a template; see .functions
: An object that maps a JavaScript function to a list of parameter names.The example above creates a functiongeocode(street, city, zipcode)
whose first argument isstreet
, second iscity
, and third iszipcode
. LoopBack application code can call the function anywhere; for example, in a boot script, via middleware, or within a model’s JavaScript file if attached to the REST datasource.
You can configure options options
property at two levels:
- Data source level (common to all operations)
- Operation level (specific to the declaring operation)The following example sets
Accept
andContent-Type
to"application/json"
for all requests.It also setsstrictSSL
to false so the connector allows self-signed SSL certificates.
/server/datasources.json
{
"connector": "rest",
"debug": false,
"options": {
"headers": {
"accept": "application/json",
"content-type": "application/json"
},
"strictSSL": false
},
"operations": [
{
"template": {
"method": "GET",
"url": "http://maps.googleapis.com/maps/api/geocode/{format=json}",
"query": {
"address": "{street},{city},{zipcode}",
"sensor": "{sensor=false}"
},
"options": {
"strictSSL": true,
},
"responsePath": "$.results[0].geometry.location"
},
"functions": {
"geocode": ["street", "city", "zipcode"]
}
]
}
If the REST API supports create, read, update, and delete (CRUD) operations for resources,you can simply bind the model to a REST endpoint that follows REST conventions.
For example, the following methods would be mixed into your model class:
- create:
POST /users
- findById:
GET /users/:id
- delete:
DELETE /users/:id
- update:
PUT /users/:id
- find:
GET /users?limit=5&username=ray&order=email
For example:
/server/boot/script.js
module.exports = function(app) {
var ds = app.loopback.createDataSource({
connector: require("loopback-connector-rest"),
debug: false,
baseURL: 'http://localhost:3000'
});
var User = ds.createModel('user', {
name: String,
bio: String,
approved: Boolean,
joinedAt: Date,
age: Number
});
User.create(new User({
name: 'Mary'
}), function(err, user) {
console.log(user);
});
User.find(function(err, user) {
console.log(user);
});
User.findById(1, function(err, user) {
console.log(err, user);
});
User.update(new User({
name: 'Raymond'
}), function(err, user) {
console.log(err, user);
});
}
Setting the resource URL
You can set the remote URL when using create, read, update, or delete functionality by setting the resourceName
property on a model definition.This allows for a local model name that is different from the remote resource name.
For example:
var config = {
"name": "ServiceTransaction",
"base": "PersistedModel",
"resourceName": "transactions"
}
Now there will be a resource model named ServiceTransaction
, but whose URLs call out to baseUrl - '/transactions'
Without setting resourceName
the calls would have been made to baseUrl - '/ServiceTransaction'
.
Defining a custom method using a template
The template
object specifies the REST API invocation as a JSON template, with the following properties:
{name=defaultValue:type}
To specify that the variable value is required, add the prefix !
or ^
.
For example:
The following table provides several examples:
To use custom methods, configure the REST connector with the operations
property, which is an array of objects, each of which can have these properties:
template
defines the API structure.functions
defines JavaScript methods that accept the specified list of parameter names.
var loopback = require("loopback");
var ds = loopback.createDataSource({
connector: require("loopback-connector-rest"),
debug: false,
operations: [{
template: {
"method": "GET",
"url": "http://maps.googleapis.com/maps/api/geocode/{format=json}",
"headers": {
"accepts": "application/json",
"content-type": "application/json"
},
"query": {
"address": "{street},{city},{zipcode}",
"sensor": "{sensor=false}"
},
"responsePath": "$.results[0].geometry.location"
},
functions: {
"geocode": ["street", "city", "zipcode"]
}
}]
});
Now you can invoke the geocode API in Node.js as follows:
Model.geocode('107 S B St', 'San Mateo', '94401', processResponse);
By default, the REST connector also provides an ‘invoke’ method to call the REST API with an object of parameters, for example:
Model.invoke({street: '107 S B St', city: 'San Mateo', zipcode: '94401'}, processResponse);
NOTE: This feature is available with loopback-connector-rest
version 2.0.0 and later.
By default, variables in the template are mapped to HTTP sources based on their root property.
You can further customize the source in the parameter array of the function mapping, for example:
- p - path
- x - query
- a - body
- b - headerPlease note that path variables are appended to the path, for example,
/myOp/:p.
Tags: connectors