gangway
Advanced tools
Comparing version 1.1.0 to 1.1.1
# Changelog | ||
## 1.1.1 | ||
- Resource READ operation should use optional id parameter | ||
## 1.1.0 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "gangway", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A client-side API abstraction layer", | ||
@@ -5,0 +5,0 @@ "main": "src/api.js", |
# Gangway | ||
A client-side API abstraction layer | ||
A client-side API abstraction layer. | ||
@@ -11,9 +11,11 @@ Gangway is our general purpose tool for working with APIs on the | ||
## Overview | ||
## Usage | ||
Gangway is a factory function that progressively layers configuration | ||
options for building an AJAX request with `superagent`: | ||
options for building an AJAX request with `superagent`. | ||
The first step is invoke Gangway with some options: | ||
```javascript | ||
var Gangway = require('../src') | ||
var Gangway = require('gangway') | ||
@@ -26,3 +28,8 @@ var API = Gangway({ | ||
}) | ||
``` | ||
With default configuration options out of the way, let's add some | ||
specific routes. | ||
```javascript | ||
API.route({ | ||
@@ -36,7 +43,22 @@ users: { | ||
}) | ||
``` | ||
`API.users.read()` will now perform a GET request to | ||
`/users/{id}`. The `?` in the path option specifies that it is | ||
optional. This is useful when using the same route for index and show | ||
endpoints for resources. | ||
For RESTful resources, adding routes this way can become | ||
tedious. Gangway provides another method for quickly building routes | ||
for RESTful resources: | ||
```javascript | ||
// this is equivalent to creating a create, read, update, and destroy | ||
// route. Options are folded into every route. | ||
API.resource("comments", { query: {} }) | ||
API.resource("comments", {}) | ||
``` | ||
From there, the Gangway instance is ready for use! | ||
```javascript | ||
// this will send a request to GET http://example.com/users | ||
@@ -47,2 +69,5 @@ API.users.read() | ||
API.users.read({ params: { id: '10' } }) | ||
// the same is true for routes added via API.resource | ||
API.comments.read({ params: { id: '2' }}) | ||
``` | ||
@@ -49,0 +74,0 @@ |
var ajax = require('./ajax') | ||
var route = require('./route') | ||
var resource = require('./resource') | ||
var url = require('./url') | ||
@@ -23,2 +24,6 @@ module.exports = function (config, routes) { | ||
resolve: function (path, params) { | ||
return url(config.baseURL, path, params) | ||
}, | ||
route: function (routes) { | ||
@@ -25,0 +30,0 @@ return route(API, routes) |
@@ -5,3 +5,2 @@ var assign = require('./assign') | ||
var route = {} | ||
var path = name + '/{id}' | ||
@@ -11,3 +10,3 @@ route[name] = { | ||
method: 'POST', | ||
path: path | ||
path: name + '/{id}' | ||
}), | ||
@@ -17,3 +16,3 @@ | ||
method: 'GET', | ||
path: path | ||
path: name + '/{id?}' | ||
}), | ||
@@ -23,3 +22,3 @@ | ||
method: 'PATCH', | ||
path: path | ||
path: name + '/{id}' | ||
}), | ||
@@ -29,3 +28,3 @@ | ||
method: 'DELETE', | ||
path: path | ||
path: name + '/{id}' | ||
}) | ||
@@ -32,0 +31,0 @@ } |
@@ -21,6 +21,14 @@ var API = require('../src/api') | ||
it ('generates a read route', function() { | ||
assert.equal(api.users.read.config.path, 'users/{id}') | ||
assert.equal(api.users.read.config.path, 'users/{id?}') | ||
assert.equal(api.users.read.config.method, 'GET') | ||
}) | ||
it ('uses an optional parameter for the ID of a resource for the read route', function() { | ||
assert.equal(api.resolve(api.users.read.config.path), | ||
'http://example.com/users') | ||
assert.equal(api.resolve(api.users.read.config.path, { id: 1 }), | ||
'http://example.com/users/1') | ||
}) | ||
it ('generates a update route', function() { | ||
@@ -55,3 +63,3 @@ assert.equal(api.users.update.config.path, 'users/{id}') | ||
it ('never assigns options over the path attribute', function() { | ||
assert.equal(api.candy.read.config.path, 'candy/{id}') | ||
assert.equal(api.candy.read.config.path, 'candy/{id?}') | ||
}) | ||
@@ -58,0 +66,0 @@ }) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
31916
600
98