Skip to content

Commit

Permalink
v0.4.0 update not_found
Browse files Browse the repository at this point in the history
spirit-js/spirit#10

not_found
- will go through render() process
- will optionally accept method argument for matching on specific
methods otherwise defaults to all
  • Loading branch information
hnry committed Nov 3, 2016
1 parent 91ebfaa commit bc7a486
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
11 changes: 7 additions & 4 deletions docs/api/api.md
Expand Up @@ -136,15 +136,18 @@ If a Route's body returns a number, this render function will run. Adding 100 to

`notFound` is an alias to `not_found`

A route that always returns a 404 Response with `body` as it's body.
Convience function for creating a route that always returns a 404 Response with `body` as it's body.

[Source: src/resource.js (not_found)](../../src/resource.js#L46)
`body` will go through _rendering_.

[Source: src/resource.js (not_found)](../../src/resource.js#L47)

#### Arguments
* body {undefined|string|buffer|stream} the body of not_found's Response
* method {string} optional, restricts not_found to a certain http method, otherwise matches all
* body {*} the body of not_found's Response

#### Return
{function} a routing function that always matches any request and runs (cannot be wrapped with middlewares)
{function} a routing function that always returns a 404 Response with `body`


-------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "spirit-router",
"version": "0.3.1",
"version": "0.4.0",
"description": "fast router for spirit",
"main": "index.js",
"scripts": {
Expand Down
32 changes: 32 additions & 0 deletions spec/resource-spec.js
Expand Up @@ -111,4 +111,36 @@ describe("not_found", () => {
expect(result.headers).toEqual({})
expect(result.body).toBe(undefined)
})

it("the body passed in will trigger rendering", () => {
let fn = not_found({ a: 1, b: 2 })
let result = fn({})
expect(result.status).toBe(404)
expect(result.headers).toEqual({
"Content-Type": "application/json"
})
expect(result.body).toBe(JSON.stringify({ a: 1, b: 2 }))

result = not_found([1, 2, 3])({})
expect(result.status).toBe(404)
expect(result.body).toBe(JSON.stringify([1, 2, 3]))
})

it("rendering is ignored if full response", () => {
let fn = not_found({ status: 123, headers: {}, body: { a: 1 }})
let result = fn({})
expect(result.status).toBe(404)
expect(result.headers).toEqual({})
expect(result.body).toEqual({ a: 1 })
})

it("optionally will only 404 on specific methods", () => {
let fn = not_found("get", "hi")
let result = fn({ method: "POST" })
expect(result).toBe(undefined)

result = fn({ method: "GET" })
expect(result.status).toBe(404)
expect(result.body).toBe("hi")
})
})
23 changes: 20 additions & 3 deletions src/resource.js
Expand Up @@ -2,6 +2,7 @@ const fs = require("fs")
const {response, file_response} = require("spirit").node
const Promise = require("bluebird")
const path = require("path")
const render = require("./render").render

const resources = (mount_path="", opts={}) => {
if (typeof mount_path === "object") {
Expand Down Expand Up @@ -43,9 +44,25 @@ const resources = (mount_path="", opts={}) => {
}
}

const not_found = (body) => {
return () => {
return response(body).status_(404)
/**
* Convienance route for always returning a 404 response
*
* @param {string} method - optional, match only for method, otherwise matches all methods
* @param {*} body - valid response body
* @returns {function} function that always returns a 404 of the `body` if `method` matches
*/
const not_found = (method, body) => {
if (!body) {
body = method
method = "*"
} else {
method = method.toUpperCase()
}

return (request) => {
if (method === "*" || request.method === method) {
return render(request, body).status_(404)
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/router.js
Expand Up @@ -167,9 +167,9 @@ const wrap = (route, middleware) => {
* The main purpose of this is to maintain handler (router)
* arguments when the handler is wrapped with middleware
*
* @param {function} handler final handler function
* @param {array} middleware an array of middleware functions
* @param {string} prop_name property name to set on the first argument (request), defaults to "_tmp"
* @param {function} handler - final handler function
* @param {array} middleware - an array of middleware functions
* @param {string} prop_name - property name to set on the first argument (request), defaults to "_tmp"
* @return {function} function that calls middleware and handler
*/
const compose_args = (handler, middleware, prop_name) => {
Expand Down

0 comments on commit bc7a486

Please sign in to comment.