Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Expose ramlUriParameters from the server handler
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Mar 3, 2016
1 parent 67cb954 commit 269931c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ osprey.loadFile(path)
### Server (Resource Handling)

```js
osprey.server(raml, options)
var handler = osprey.server(raml, options)

console.log(handler) //=> function (req, res, next) {}

console.log(handler.ramlUriParameters) //=> {} // A merged object of used URI parameters.
```

Undefined API requests will _always_ be rejected with a 404.
Expand Down Expand Up @@ -191,6 +195,16 @@ app.get('/{slug}', {
module.exports = app
```

You can initialize a `Router` with `ramlUriParameters`. This is helpful, since every router collects an object with merged URI parameters. For example, you can combine it with the `server` middleware to generate a router with your RAML URI parameters:

```js
var handler = osprey.server(raml)
var router = osprey.Router({ ramlUriParameters: handler.ramlUriParameters })

// Uses an existing `userId` URI parameter, if it exists.
router.get('/{userId}', function (req, res, next) {})
```

#### Handling Errors

Osprey returns a [middleware router instance](https://github.com/pillarjs/router), so you can mount this within any compatible application and handle errors with the framework. For example, using HTTP with [finalhandler](https://github.com/pillarjs/finalhandler) (the same module Express uses):
Expand Down
10 changes: 6 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ module.exports.notFoundHandler = notFoundHandler
* @return {Function}
*/
function createServer (raml, opts) {
var app = router()
var resourceHandler = resources(raml.resources, function (schema, path) {
return handler(schema, path, schema.method, options)
})

var app = router({ ramlUriParameters: resourceHandler.ramlUriParameters })
var options = opts || {}
var corsOpts = typeof options.cors === 'boolean' ? undefined : options.cors

Expand All @@ -34,9 +38,7 @@ function createServer (raml, opts) {
app.use(compression())
}

app.use(resources(raml.resources, function (schema, path) {
return handler(schema, path, schema.method, options)
}))
app.use(resourceHandler)

if (options.notFoundHandler !== false) {
app.use(notFoundHandler)
Expand Down
12 changes: 6 additions & 6 deletions osprey.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ exports.loadFile = function (path, options) {
.loadFile(path)
.then(function (raml) {
var middleware = []
var handler = server(raml, options.server)
var error = errorHandler(options.errorHandler)

if (options.security) {
middleware.push(security(raml, options.security))
}

// Always compose the server and error handler.
middleware.push(
server(raml, options.server),
errorHandler(options.errorHandler)
)
middleware.push(handler, error)

return compose(middleware)
var result = compose(middleware)
result.ramlUriParameters = handler.ramlUriParameters
return result
})
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"invariant": "^2.1.0",
"oauth2orize": "^1.0.1",
"osprey-method-handler": "^0.10.0",
"osprey-resources": "^0.5.0",
"osprey-router": "^0.2.0",
"osprey-resources": "^0.7.0",
"osprey-router": "^0.4.0",
"parseurl": "^1.3.0",
"passport": "^0.3.0",
"passport-http": "^0.3.0",
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/example.raml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ baseUri: http://example.com
hello:
type: string
pattern: "[a-z]+"

/users/{userId}:
uriParameters:
userId:
type: number
get:
8 changes: 8 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ describe('server', function () {

app.use(middleware)

expect(middleware.ramlUriParameters).to.deep.equal({
userId: {
type: 'number',
displayName: 'userId',
required: true
}
})

app.get('/users', success)
app.get('/unknown', success)

Expand Down

0 comments on commit 269931c

Please sign in to comment.