Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added request.is() decorator #6

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ fastify.get('/', (req, reply) => {
})
```

#### `request.is`
The `request` interface is decorated with [`jshttp/type-is`](https://github.com/jshttp/type-is), the API is the same, but you don't need to pass the request object.
```js
fastify.get('/', (req, reply) => {
reply.send(req.is(['html', 'json']))
})
```

#### `assert`
Verify if a given condition is true, if not it throws the specified http error.<br> Very useful if you work with *async* routes.
```js
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fp = require('fastify-plugin')
// External utilities
const forwarded = require('forwarded')
const proxyaddr = require('proxy-addr')
const typeis = require('type-is')
// Internals Utilities
const httpErrors = require('./lib/httpErrors')
const assert = require('./lib/assert')
Expand All @@ -22,6 +23,10 @@ function fastifySensible (fastify, opts, next) {
return proxyaddr(this.raw, trust)
})

fastify.decorateRequest('is', function (types) {
return typeis(this.raw, Array.isArray(types) ? types : [types])
})

fastify.decorateReply('vary', vary)

// TODO: benchmark if this closure causes some performance drop
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"fastify-plugin": "^0.2.2",
"forwarded": "^0.1.2",
"proxy-addr": "^2.0.3",
"type-is": "^1.6.16",
"vary": "^1.1.2"
}
}
53 changes: 53 additions & 0 deletions test/is.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

const { test } = require('tap')
const Fastify = require('fastify')
const Sensible = require('../index')

test('request.is API', t => {
t.plan(3)

const fastify = Fastify()
fastify.register(Sensible)

fastify.get('/', (req, reply) => {
reply.send(req.is('json'))
})

fastify.inject({
method: 'GET',
url: '/',
payload: { foo: 'bar' }
}, (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 200)
t.deepEqual(
res.payload,
'json'
)
})
})

test('request.is API (with array)', t => {
t.plan(3)

const fastify = Fastify()
fastify.register(Sensible)

fastify.get('/', (req, reply) => {
reply.send(req.is(['html', 'json']))
})

fastify.inject({
method: 'GET',
url: '/',
payload: { foo: 'bar' }
}, (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 200)
t.deepEqual(
res.payload,
'json'
)
})
})