2013-08-27 15:18:32 +00:00
|
|
|
## REST API
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
LoopBack automatically binds a model to a list of HTTP endpoints that provide REST APIs for CRUD and other remote
|
|
|
|
operations.
|
|
|
|
|
2013-08-27 15:18:32 +00:00
|
|
|
### Sample Model
|
2013-08-22 18:15:31 +00:00
|
|
|
|
2013-08-23 23:37:01 +00:00
|
|
|
We use the following `Location` model as an example to illustrate generated REST APIs.
|
2013-08-22 18:15:31 +00:00
|
|
|
|
2013-08-23 23:37:01 +00:00
|
|
|
{
|
|
|
|
"name": "Location",
|
|
|
|
"options": {
|
|
|
|
"idInjection": false
|
|
|
|
},
|
|
|
|
"properties": {
|
|
|
|
"id": {
|
|
|
|
"type": "String",
|
|
|
|
"length": 20,
|
|
|
|
"id": 1
|
|
|
|
},
|
|
|
|
"street": {
|
|
|
|
"type": "String",
|
|
|
|
"required": false,
|
|
|
|
"length": 20
|
|
|
|
},
|
|
|
|
"city": {
|
|
|
|
"type": "String",
|
|
|
|
"required": false,
|
|
|
|
"length": 20
|
|
|
|
},
|
|
|
|
"zipcode": {
|
|
|
|
"type": "Number",
|
|
|
|
"required": false,
|
|
|
|
"length": 20
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
"type": "String",
|
|
|
|
"required": false,
|
|
|
|
"length": 20
|
|
|
|
},
|
|
|
|
"geo": {
|
|
|
|
"type": "GeoPoint"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 18:15:31 +00:00
|
|
|
|
2013-08-23 23:37:01 +00:00
|
|
|
By default, the REST APIs are mounted to `/pluralFormOfTheModelName`, for example, `/locations`.
|
2013-08-22 18:15:31 +00:00
|
|
|
|
2013-08-23 23:37:01 +00:00
|
|
|
### CRUD remote methods
|
2013-08-22 18:15:31 +00:00
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
- Model.create: POST /locations
|
|
|
|
- Model.upsert: PUT /locations
|
|
|
|
- Model.exists: GET /locations/:id/exists
|
|
|
|
- Model.findById: GET /locations/:id
|
|
|
|
- Model.find: GET /locations
|
|
|
|
- Model.findOne: GET /locations/findOne
|
|
|
|
- Model.deleteById: DELETE /locations/:id
|
|
|
|
- Model.count: GET /locations/count
|
|
|
|
- Model.prototype.updateAttributes: PUT /locations/:id
|
2013-08-22 18:15:31 +00:00
|
|
|
|
2013-08-23 23:37:01 +00:00
|
|
|
### Custom remote methods
|
|
|
|
|
|
|
|
To expose a JavaScript method as REST API, we can simply describe the method as follows:
|
|
|
|
|
|
|
|
loopback.remoteMethod(
|
|
|
|
RentalLocation.nearby,
|
|
|
|
{
|
|
|
|
description: 'Find nearby locations around the given geo point',
|
|
|
|
accepts: [
|
|
|
|
{arg: 'here', type: 'GeoPoint', required: true},
|
|
|
|
{arg: 'page', type: 'Number'},
|
|
|
|
{arg: 'max', type: 'Number', description: 'max distance in miles'}
|
|
|
|
],
|
|
|
|
returns: {arg: 'locations', root: true},
|
|
|
|
http: {verb: 'POST', path: '/nearby'}
|
|
|
|
}
|
|
|
|
);
|
2013-08-22 18:15:31 +00:00
|
|
|
|
2013-08-23 23:37:01 +00:00
|
|
|
The remoting is defined using the following properties:
|
2013-08-26 17:20:06 +00:00
|
|
|
|
2013-08-23 23:37:01 +00:00
|
|
|
- description: Description of the REST API
|
|
|
|
- accepts: An array of parameter description
|
|
|
|
- returns: Description of the return value
|
|
|
|
- http: Binding to the HTTP endpoint
|
|
|
|
|
2013-08-27 15:18:32 +00:00
|
|
|
###Generated APIs
|
2013-08-23 23:37:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
###create
|
|
|
|
|
|
|
|
Create a new instance of the model and persist it into the data source
|
|
|
|
|
|
|
|
####Definition
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
POST /locations
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **data** The model instance data
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
2013-08-26 17:20:06 +00:00
|
|
|
curl -X POST -H "Content-Type:application/json" -d '{"name": "L1", "street": "107 S B St", "city": "San Mateo", "zipcode": "94401"}' http://localhost:3000/locations
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Example Response
|
2013-08-23 23:37:01 +00:00
|
|
|
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###upsert
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Update an existing model instance or insert a new one into the data source
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
PUT /locations
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **data** The model instance data
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
2013-08-26 17:20:06 +00:00
|
|
|
curl -X PUT -H "Content-Type:application/json" -d '{"name": "L1", "street": "107 S B St", "city": "San Mateo", "zipcode": "94401"}' http://localhost:3000/locations
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Example Response
|
2013-08-23 23:37:01 +00:00
|
|
|
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###exists
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Check whether a model instance exists by id in the data source
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
GET /locations/exists
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **id** The model id
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
|
|
|
curl http://localhost:3000/locations/exists/88
|
|
|
|
|
|
|
|
####Example Response
|
|
|
|
|
|
|
|
{
|
|
|
|
"exists": true
|
|
|
|
}
|
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###findById
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Find a model instance by id from the data source
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
GET /locations/{id}
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **id** The model id
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
|
|
|
curl http://localhost:3000/locations/88
|
|
|
|
|
|
|
|
####Example Response
|
|
|
|
|
|
|
|
{
|
|
|
|
"id": "88",
|
|
|
|
"street": "390 Lang Road",
|
|
|
|
"city": "Burlingame",
|
|
|
|
"zipcode": 94010,
|
|
|
|
"name": "Bay Area Firearms",
|
|
|
|
"geo": {
|
|
|
|
"lat": 37.5874391,
|
|
|
|
"lng": -122.3381437
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###find
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Find all instances of the model matched by filter from the data source
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
GET /locations
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **filter** The filter that defines where, order, fields, skip, and limit
|
|
|
|
|
|
|
|
Properties for the filter object:
|
2013-08-22 18:15:31 +00:00
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
where - Object { key: val, key2: {gt: 'val2'}}
|
|
|
|
include - String, Object or Array.
|
|
|
|
order - String "key1 ASC, key2 DESC"
|
|
|
|
limit - Number The max number of items
|
|
|
|
skip - Number The number of items to be skipped
|
|
|
|
fields - Object|Array|String A list of properties to be included or excluded
|
|
|
|
['foo'] or 'foo' - include only the foo property
|
|
|
|
['foo', 'bar'] - include the foo and bar properties
|
|
|
|
{foo: true} - include only foo
|
|
|
|
{bat: false} - include all properties, exclude bat
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Example Request
|
|
|
|
curl http://localhost:3000/locations
|
|
|
|
|
|
|
|
####Example Response
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"id": "87",
|
|
|
|
"street": "7153 East Thomas Road",
|
|
|
|
"city": "Scottsdale",
|
|
|
|
"zipcode": 85251,
|
|
|
|
"name": "Phoenix Equipment Rentals",
|
|
|
|
"geo": {
|
|
|
|
"lat": 33.48034450000001,
|
|
|
|
"lng": -111.9271738
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "88",
|
|
|
|
"street": "390 Lang Road",
|
|
|
|
"city": "Burlingame",
|
|
|
|
"zipcode": 94010,
|
|
|
|
"name": "Bay Area Firearms",
|
|
|
|
"geo": {
|
|
|
|
"lat": 37.5874391,
|
|
|
|
"lng": -122.3381437
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###findOne
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Find first instance of the model matched by filter from the data source
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
GET /locations/findOne
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **filter** The filter that defines where, order, fields, skip, and limit
|
|
|
|
|
|
|
|
Properties for the filter object:
|
|
|
|
|
|
|
|
where - Object { key: val, key2: {gt: 'val2'}}
|
|
|
|
include - String, Object or Array.
|
|
|
|
order - String "key1 ASC, key2 DESC"
|
|
|
|
limit - Number The max number of items
|
|
|
|
skip - Number The number of items to be skipped
|
|
|
|
fields - Object|Array|String A list of properties to be included or excluded
|
|
|
|
['foo'] or 'foo' - include only the foo property
|
|
|
|
['foo', 'bar'] - include the foo and bar properties
|
|
|
|
{foo: true} - include only foo
|
|
|
|
{bat: false} - include all properties, exclude bat
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
|
|
|
curl http://localhost:3000/locations/findOne
|
|
|
|
|
|
|
|
####Example Response
|
|
|
|
|
|
|
|
{
|
|
|
|
"id": "87",
|
|
|
|
"street": "7153 East Thomas Road",
|
|
|
|
"city": "Scottsdale",
|
|
|
|
"zipcode": 85251,
|
|
|
|
"name": "Phoenix Equipment Rentals",
|
|
|
|
"geo": {
|
|
|
|
"lat": 33.48034450000001,
|
|
|
|
"lng": -111.9271738
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###deleteById
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Delete a model instance by id from the data source
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
DELETE /locations/{id}
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **id** The model id
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
|
|
|
curl -X DELETE http://localhost:3000/locations/88
|
|
|
|
|
|
|
|
####Example Response
|
|
|
|
|
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###count
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Count instances of the model matched by where from the data source
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
GET /locations/count
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **where** The criteria to match model instances
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
|
|
|
curl http://localhost:3000/locations/count
|
|
|
|
|
|
|
|
####Example Response
|
|
|
|
|
|
|
|
{
|
|
|
|
count: 6
|
|
|
|
}
|
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###nearby
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Find nearby locations around the geo point
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
GET /locations/nearby
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **here** geo location object with `lat` and `lng` properties
|
|
|
|
* **page** number of pages (page size=10)
|
|
|
|
* **max** max distance in miles
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
|
|
|
curl http://localhost:3000/locations/nearby?here[lat]=37.587409&here[lng]=-122.338225
|
|
|
|
|
|
|
|
####Example Response
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"id": "88",
|
|
|
|
"street": "390 Lang Road",
|
|
|
|
"city": "Burlingame",
|
|
|
|
"zipcode": 94010,
|
|
|
|
"name": "Bay Area Firearms",
|
|
|
|
"geo": {
|
|
|
|
"lat": 37.5874391,
|
|
|
|
"lng": -122.3381437
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "89",
|
|
|
|
"street": "1850 El Camino Real",
|
|
|
|
"city": "Menlo Park",
|
|
|
|
"zipcode": 94027,
|
|
|
|
"name": "Military Weaponry",
|
|
|
|
"geo": {
|
|
|
|
"lat": 37.459525,
|
|
|
|
"lng": -122.194253
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
###updateAttributes
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
2013-08-26 17:20:06 +00:00
|
|
|
Update attributes for a model instance and persist it into the data source
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
PUT /locations/{id}
|
|
|
|
|
|
|
|
####Arguments
|
2013-08-26 17:20:06 +00:00
|
|
|
* **data** An object containing property name/value pairs
|
|
|
|
* **id** The model id
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
2013-08-26 17:20:06 +00:00
|
|
|
curl -X PUT -H "Content-Type:application/json" -d '{"name': "L2"}' http://localhost:3000/locations/88
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Example Response
|
2013-08-23 23:37:01 +00:00
|
|
|
|
2013-08-22 18:15:31 +00:00
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
###getInventory
|
|
|
|
|
|
|
|
|
|
|
|
Follow the relations from location to inventory to get a list of items for a given location
|
|
|
|
|
|
|
|
####Definition
|
|
|
|
|
|
|
|
|
|
|
|
GET /locations/{id}/inventory
|
|
|
|
|
|
|
|
####Arguments
|
|
|
|
* **where**
|
|
|
|
* **id**
|
|
|
|
|
|
|
|
|
|
|
|
####Example Request
|
|
|
|
curl http://localhost:3000/locations/88/inventory
|
|
|
|
|
|
|
|
####Example Response
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"productId": "2",
|
|
|
|
"locationId": "88",
|
|
|
|
"available": 10,
|
|
|
|
"total": 10
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"productId": "3",
|
|
|
|
"locationId": "88",
|
|
|
|
"available": 1,
|
|
|
|
"total": 1
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
####Potential Errors
|
|
|
|
* None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|