Socket
Socket
Sign inDemoInstall

fastify

Package Overview
Dependencies
124
Maintainers
2
Versions
282
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.23.0 to 0.24.0

test/beforeHandler.test.js

4

benchmarks/koa.js

@@ -6,6 +6,6 @@ 'use string'

app.use(function * () {
this.body = JSON.stringify({ hello: 'world' })
app.use(async (ctx) => {
ctx.body = JSON.stringify({ hello: 'world' })
})
app.listen(3000)

@@ -34,8 +34,10 @@ <h1 align="center">Fastify</h1>

const schema = {
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
const opts = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}

@@ -47,3 +49,3 @@ }

// Declare a route with an output schema
fastify.get('/', schema, function (request, reply) {
fastify.get('/', opts, function (request, reply) {
reply.send({ hello: 'world' })

@@ -50,0 +52,0 @@ })

@@ -47,1 +47,26 @@ <h1 align="center">Fastify</h1>

Talking about scope, the hooks works in a slightly different way from the Request/Reply encapsulation model. For instance, `onRequest`, `preRouting` and `onClose` are never encapsulated, not matter where you are declaring them, while the `preHandler` hook is always encapsulated if you declare it inside a `register`.
<a name="before-handler"></a>
### beforeHandler
Despite the name, `beforeHandler` is not a standard hook like `preHandler`, but is a function that your register right in the route option that will be executed only in the specified route. Can be useful if you need to handle the authentication at route level instead of at hook level (`preHandler` for example.)
**`beforeHandler` is executed always after the `preHandler` hook.**
```js
fastify.addHook('preHandler', (request, reply, done) => {
// your code
done()
})
fastify.route({
method: 'GET',
url: '/',
schema: { ... },
beforeHandler: function (request, reply, done) {
// your code
done()
},
handler: function (request, reply) {
reply.send({ hello: 'world' })
}
})
```

@@ -13,7 +13,7 @@ <h1 align="center">Fastify</h1>

500 ◀─┴─▶ run Middlewares
4**/5** ◀─┴─▶ run Middlewares
500 ◀─┴─▶ preRouting Hook
4**/5** ◀─┴─▶ preRouting Hook
500 ◀─┴─▶ Routing
4**/5** ◀─┴─▶ Routing

@@ -26,7 +26,9 @@ 404 ◀─┴─▶ Parsing

500 ◀─┴─▶ User Handler
4**/5** ◀─┴─▶ beforeHandler
└─▶ Reply
4**/5** ◀─┴─▶ User Handler
└─▶ Outgoing Response
└─▶ Reply
└─▶ Outgoing Response
```

@@ -21,3 +21,3 @@ <h1 align="center">Fastify</h1>

fastify.get('/', schema, function (req, reply) {
fastify.get('/', options, function (req, reply) {
req.log.info('Some info about the current request')

@@ -24,0 +24,0 @@ reply.send({ hello: 'world' })

@@ -24,3 +24,3 @@ <h1 align="center">Fastify</h1>

<a name="route-prefixing-option"></a>
### Route Prefixing option
#### Route Prefixing option
If you pass an option with the key `prefix` with a `string` value, Fastify will use it to prefix all the routes inside the register, for more info check [here](https://github.com/fastify/fastify/blob/master/docs/Routes.md#route-prefixing).

@@ -27,0 +27,0 @@

@@ -13,3 +13,3 @@ <h1 align="center">Fastify</h1>

```js
fastify.get('/', schema, function (request, reply) {
fastify.get('/', options, function (request, reply) {
// You code

@@ -54,3 +54,3 @@ reply

```js
fastify.get('/json', schema, function (request, reply) {
fastify.get('/json', options, function (request, reply) {
reply.send({ hello: 'world' })

@@ -64,3 +64,3 @@ })

```js
fastify.get('/promises', schema, function (request, reply) {
fastify.get('/promises', options, function (request, reply) {
const promise = new Promise(...)

@@ -73,3 +73,3 @@ reply

fastify.get('/async-await', schema, async function (request, reply) {
fastify.get('/async-await', options, async function (request, reply) {
var res = await new Promise(function (resolve) {

@@ -76,0 +76,0 @@ setTimeout(resolve, 200, { hello: 'world' })

@@ -13,3 +13,3 @@ <h1 align="center">Fastify</h1>

```js
fastify.post('/:params', schema, function (request, reply) {
fastify.post('/:params', options, function (request, reply) {
console.log(request.body)

@@ -16,0 +16,0 @@ console.log(request.query)

@@ -24,2 +24,3 @@ <h1 align="center">Fastify</h1>

schema allows us to have 10-20% more throughput.
* `beforeHandler(request, reply, done)`: a [function](https://github.com/fastify/fastify/blob/master/docs/Hooks.md#before-handler) called just before the request handler, useful if you need to perform authentication at route level for example.
* `handler(request, reply)`: the function that will handle this request.

@@ -40,8 +41,4 @@

querystring: {
name: {
type: 'string'
},
excitement: {
type: 'integer'
}
name: { type: 'string' },
excitement: { type: 'integer' }
},

@@ -52,5 +49,3 @@ response: {

properties: {
hello: {
type: 'string'
}
hello: { type: 'string' }
}

@@ -66,13 +61,47 @@ }

```js
fastify.route({
method: 'GET',
url: '/',
schema: { ... },
beforeHandler: function (request, reply, done) {
// your authentication logic
done()
},
handler: function (request, reply) {
reply.send({ hello: 'world' })
}
})
```
<a name="shorthand-declaration"></a>
### Shorthand declaration
The above route declaration is more *Hapi*-like, but if you prefer an *Express/Restify* approach, we support it as well:
`fastify.get(path, [schema], handler)`
`fastify.head(path, [schema], handler)`
`fastify.post(path, [schema], handler)`
`fastify.put(path, [schema], handler)`
`fastify.delete(path, [schema], handler)`
`fastify.options(path, [schema], handler)`
`fastify.patch(path, [schema], handler)`
The above route declaration is more *Hapi*-like, but if you prefer an *Express/Restify* approach, we support it as well:
`fastify.get(path, [options], handler)`
`fastify.head(path, [options], handler)`
`fastify.post(path, [options], handler)`
`fastify.put(path, [options], handler)`
`fastify.delete(path, [options], handler)`
`fastify.options(path, [options], handler)`
`fastify.patch(path, [options], handler)`
Example:
```js
const opts = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}
}
fastify.get('/', opts, (req, reply) => {
reply.send({ hello: 'world' })
})
```
<a name="route-prefixing"></a>

@@ -79,0 +108,0 @@ ### Route Prefixing

@@ -18,8 +18,10 @@ <h1 align="center">Fastify</h1>

const schema = {
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
const options = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}

@@ -31,3 +33,3 @@ }

function start (opts, callback) {
fastify.get('/', schema, function (request, reply) {
fastify.get('/', options, function (request, reply) {
reply.send({ hello: 'world' })

@@ -97,2 +99,6 @@ })

Fastify supports fake http injection thanks to [shot](https://github.com/hapijs/shot).
To support this method, you should list `shot` library in your package as dev dependency using
```
npm install shot --save-dev
```
You just need to use the api `inject`:

@@ -115,8 +121,10 @@ ```js

const schema = {
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
const options = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}

@@ -127,3 +135,3 @@ }

fastify.get('/', schema, function (request, reply) {
fastify.get('/', options, function (request, reply) {
reply.send({ hello: 'world' })

@@ -130,0 +138,0 @@ })

@@ -5,9 +5,11 @@ 'use strict'

const schema = {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string'
const opts = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -20,7 +22,7 @@ }

fastify
.get('/', schema, function (req, reply) {
.get('/', opts, function (req, reply) {
reply.header('Content-Type', 'application/json').code(200)
reply.send({ hello: 'world' })
})
.get('/promise', schema, function (req, reply) {
.get('/promise', opts, function (req, reply) {
const promise = new Promise(function (resolve, reject) {

@@ -31,3 +33,3 @@ resolve({ hello: 'world' })

})
.get('/return-promise', schema, function (req, reply) {
.get('/return-promise', opts, function (req, reply) {
const promise = new Promise(function (resolve, reject) {

@@ -43,3 +45,3 @@ resolve({ hello: 'world' })

})
.post('/', schema, function (req, reply) {
.post('/', opts, function (req, reply) {
reply.send({ hello: 'world' })

@@ -50,6 +52,6 @@ })

})
.delete('/', schema, function (req, reply) {
.delete('/', opts, function (req, reply) {
reply.send({ hello: 'world' })
})
.patch('/', schema, function (req, reply) {
.patch('/', opts, function (req, reply) {
reply.send({ hello: 'world' })

@@ -56,0 +58,0 @@ })

@@ -34,3 +34,3 @@ 'use strict'

fastify.get('/', opts.schema, function (req, reply) {
fastify.get('/', opts, function (req, reply) {
reply.send({ hello: 'world' })

@@ -37,0 +37,0 @@ })

@@ -11,9 +11,11 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
const opts = {
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -26,3 +28,3 @@ }

fastify
.get('/', schema, function (req, reply) {
.get('/', opts, function (req, reply) {
reply.header('Content-Type', 'application/json').code(200)

@@ -29,0 +31,0 @@ reply.send({ hello: 'world' })

@@ -13,9 +13,11 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
const opts = {
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -28,3 +30,3 @@ }

fastify
.get('/', schema, function (req, reply) {
.get('/', opts, function (req, reply) {
reply.header('Content-Type', 'application/json').code(200)

@@ -31,0 +33,0 @@ reply.send({ hello: 'world' })

@@ -5,6 +5,6 @@ 'use strict'

fastify
.get('/', opts.schema, function (req, reply) {
.get('/', opts, function (req, reply) {
reply.send({ hello: 'world' })
})
.post('/', opts.schema, function (req, reply) {
.post('/', opts, function (req, reply) {
reply.send({ hello: 'world' })

@@ -11,0 +11,0 @@ })

@@ -5,3 +5,3 @@ 'use strict'

const schema = {
const opts = {
schema: {

@@ -19,5 +19,5 @@ response: {

fastify.register(function (instance, opts, next) {
fastify.register(function (instance, options, next) {
// the route will be '/english/hello'
instance.get('/hello', schema, (req, reply) => {
instance.get('/hello', opts, (req, reply) => {
reply.send({ greet: 'hello' })

@@ -28,5 +28,5 @@ })

fastify.register(function (instance, opts, next) {
fastify.register(function (instance, options, next) {
// the route will be '/italian/hello'
instance.get('/hello', schema, (req, reply) => {
instance.get('/hello', opts, (req, reply) => {
reply.send({ greet: 'ciao' })

@@ -33,0 +33,0 @@ })

@@ -10,3 +10,4 @@ 'use strict'

const fastseries = require('fastseries')
const shot = require('shot')
var shot = null
try { shot = require('shot') } catch (e) { }

@@ -242,34 +243,34 @@ const Reply = require('./lib/reply')

// Shorthand methods
function _delete (url, schema, handler) {
return _route(this, 'DELETE', url, schema, handler)
function _delete (url, opts, handler) {
return _route(this, 'DELETE', url, opts, handler)
}
function _get (url, schema, handler) {
return _route(this, 'GET', url, schema, handler)
function _get (url, opts, handler) {
return _route(this, 'GET', url, opts, handler)
}
function _head (url, schema, handler) {
return _route(this, 'HEAD', url, schema, handler)
function _head (url, opts, handler) {
return _route(this, 'HEAD', url, opts, handler)
}
function _patch (url, schema, handler) {
return _route(this, 'PATCH', url, schema, handler)
function _patch (url, opts, handler) {
return _route(this, 'PATCH', url, opts, handler)
}
function _post (url, schema, handler) {
return _route(this, 'POST', url, schema, handler)
function _post (url, opts, handler) {
return _route(this, 'POST', url, opts, handler)
}
function _put (url, schema, handler) {
return _route(this, 'PUT', url, schema, handler)
function _put (url, opts, handler) {
return _route(this, 'PUT', url, opts, handler)
}
function _options (url, schema, handler) {
return _route(this, 'OPTIONS', url, schema, handler)
function _options (url, opts, handler) {
return _route(this, 'OPTIONS', url, opts, handler)
}
function _route (self, method, url, schema, handler) {
if (!handler && typeof schema === 'function') {
handler = schema
schema = {}
function _route (self, method, url, options, handler) {
if (!handler && typeof options === 'function') {
handler = options
options = {}
}

@@ -279,9 +280,10 @@ return route({

url,
schema,
handler,
schema: options.schema || {},
Reply: self._Reply,
Request: self._Request,
contentTypeParser: self._contentTypeParser,
hooks: self._hooks,
RoutePrefix: self._RoutePrefix
preHandler: self._hooks.preHandler,
RoutePrefix: self._RoutePrefix,
beforeHandler: options.beforeHandler
})

@@ -305,3 +307,3 @@ }

opts.contentTypeParser = opts.contentTypeParser || this._contentTypeParser
opts.hooks = opts.hooks || this._hooks
opts.preHandler = opts.preHandler || this._hooks.preHandler
opts.RoutePrefix = opts.RoutePrefix || this._RoutePrefix

@@ -363,2 +365,4 @@

function inject (opts, cb) {
if (!shot) throw new Error('"shot" library is not installed: "npm install shot --save-dev"')
if (started) {

@@ -365,0 +369,0 @@ shot.inject(this, opts, cb)

@@ -95,3 +95,3 @@ /* eslint-disable no-useless-return */

hookIterator,
store.hooks.preHandler,
store.preHandler,
preHandlerCallback

@@ -101,6 +101,6 @@ )

function State (request, reply, handle) {
function State (request, reply, store) {
this.request = request
this.reply = reply
this.handle = handle
this.store = store
}

@@ -118,3 +118,9 @@

}
var result = this.handle.handler(this.request, this.reply)
if (this.store.beforeHandler) {
setImmediate(this.store.beforeHandler, this.request, this.reply, done(this))
return
}
var result = this.store.handler(this.request, this.reply)
if (result && typeof result.then === 'function') {

@@ -125,2 +131,15 @@ this.reply.send(result)

function done (that) {
return function _done (err) {
if (err) {
that.reply.send(err)
return
}
var result = that.store.handler(that.request, that.reply)
if (result && typeof result.then === 'function') {
that.reply.send(result)
}
}
}
function wrapReplyEnd (req, res, statusCode, payload) {

@@ -127,0 +146,0 @@ const reply = new Reply(req, res, null)

@@ -13,5 +13,5 @@ /* eslint-disable no-useless-return */

function Reply (req, res, handle) {
function Reply (req, res, store) {
this.res = res
this.handle = handle
this.store = store
this._req = req

@@ -90,3 +90,3 @@ this.sent = false

// Here we are assuming that the custom serializer is a json serializer
const str = this._serializer ? this._serializer(payload) : serialize(this.handle, payload, this.res.statusCode)
const str = this._serializer ? this._serializer(payload) : serialize(this.store, payload, this.res.statusCode)
if (!this.res.getHeader('Content-Length')) {

@@ -153,5 +153,5 @@ this.res.setHeader('Content-Length', Buffer.byteLength(str))

function buildReply (R) {
function _Reply (req, res, handle) {
function _Reply (req, res, store) {
this.res = res
this.handle = handle
this.store = store
this._req = req

@@ -158,0 +158,0 @@ this.sent = false

{
"name": "fastify",
"version": "0.23.0",
"version": "0.24.0",
"description": "Fast and low overhead web framework, for Node.js",

@@ -61,2 +61,3 @@ "main": "fastify.js",

"snazzy": "^7.0.0",
"shot": "^3.4.2",
"split2": "^2.1.1",

@@ -76,9 +77,8 @@ "standard": "^10.0.2",

"fastseries": "^1.7.2",
"find-my-way": "^0.2.4",
"find-my-way": "^1.0.1",
"middie": "^0.3.0",
"pino-http": "^2.6.1",
"pump": "^1.0.2",
"shot": "^3.4.2",
"xtend": "^4.0.1"
}
}

@@ -66,4 +66,7 @@ <div align="center">

Bearer auth plugin for Fastify
- [`fastify-pigeon`](https://github.com/fastify/fastify-pigeon) Bankai assets compiler for Fastify
- [`fastify-react`](https://github.com/fastify/fastify-react) React server side rendering support for Fastify with Next
- [`fastify-pigeon`](https://github.com/fastify/fastify-pigeon) [Bankai](https://github.com/yoshuawuyts/bankai) assets compiler for Fastify
- [`fastify-react`](https://github.com/fastify/fastify-react) React server side rendering support for Fastify with [Next](https://github.com/zeit/next.js/)
- [`fastify-jwt`](https://github.com/fastify/fastify-jwt) JWT utils for Fastify, internally uses [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken)
- [`fastify-websocket`](https://github.com/fastify/fastify-websocket) WebSocket support for Fastify. Built upon [websocket-stream](https://github.com/maxogden/websocket-stream)
- [`fastify-helmet`](https://github.com/fastify/fastify-helmet) Important security headers for Fastify
- *More coming soon*

@@ -70,0 +73,0 @@

@@ -8,9 +8,11 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
const opts = {
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -22,3 +24,3 @@ }

fastify.get('/', schema, function (req, reply) {
fastify.get('/', opts, function (req, reply) {
reply.send({ hello: 'world' })

@@ -25,0 +27,0 @@ })

@@ -7,9 +7,11 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
const opts = {
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -27,3 +29,3 @@ }

try {
fastify.get('/', schema, async function awaitMyFunc (req, reply) {
fastify.get('/', opts, async function awaitMyFunc (req, reply) {
await sleep(200)

@@ -41,3 +43,3 @@ return { hello: 'world' }

try {
fastify.get('/no-await', schema, async function (req, reply) {
fastify.get('/no-await', opts, async function (req, reply) {
return { hello: 'world' }

@@ -44,0 +46,0 @@ })

@@ -8,9 +8,11 @@ 'use strict'

const noop = () => {}
const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
const opts = {
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -24,3 +26,3 @@ }

t.plan(1)
t.type(fastify.get('/', schema, noop), fastify)
t.type(fastify.get('/', opts, noop), fastify)
})

@@ -30,3 +32,3 @@

t.plan(1)
t.type(fastify.post('/', schema, noop), fastify)
t.type(fastify.post('/', opts, noop), fastify)
})

@@ -39,5 +41,5 @@

url: '/other',
schema: schema,
schema: opts.schema,
handler: noop
}), fastify)
})

@@ -9,8 +9,10 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -23,7 +25,9 @@ }

const querySchema = {
querystring: {
type: 'object',
properties: {
hello: {
type: 'integer'
schema: {
querystring: {
type: 'object',
properties: {
hello: {
type: 'integer'
}
}

@@ -35,10 +39,12 @@ }

const paramsSchema = {
params: {
type: 'object',
properties: {
foo: {
type: 'string'
},
test: {
type: 'integer'
schema: {
params: {
type: 'object',
properties: {
foo: {
type: 'string'
},
test: {
type: 'integer'
}
}

@@ -45,0 +51,0 @@ }

@@ -10,8 +10,10 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -24,8 +26,10 @@ }

const numberSchema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'number'
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'number'
}
}

@@ -38,7 +42,9 @@ }

const querySchema = {
querystring: {
type: 'object',
properties: {
hello: {
type: 'integer'
schema: {
querystring: {
type: 'object',
properties: {
hello: {
type: 'integer'
}
}

@@ -50,10 +56,12 @@ }

const paramsSchema = {
params: {
type: 'object',
properties: {
foo: {
type: 'string'
},
test: {
type: 'integer'
schema: {
params: {
type: 'object',
properties: {
foo: {
type: 'string'
},
test: {
type: 'integer'
}
}

@@ -60,0 +68,0 @@ }

@@ -9,5 +9,7 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'null'
schema: {
response: {
'2xx': {
type: 'null'
}
}

@@ -18,7 +20,9 @@ }

const querySchema = {
querystring: {
type: 'object',
properties: {
hello: {
type: 'integer'
schema: {
querystring: {
type: 'object',
properties: {
hello: {
type: 'integer'
}
}

@@ -30,10 +34,12 @@ }

const paramsSchema = {
params: {
type: 'object',
properties: {
foo: {
type: 'string'
},
test: {
type: 'integer'
schema: {
params: {
type: 'object',
properties: {
foo: {
type: 'string'
},
test: {
type: 'integer'
}
}

@@ -40,0 +46,0 @@ }

@@ -12,8 +12,10 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -20,0 +22,0 @@ }

@@ -11,8 +11,10 @@ 'use strict'

const schema = {
body: {
type: 'object',
properties: {
hello: {
type: 'integer'
const opts = {
schema: {
body: {
type: 'object',
properties: {
hello: {
type: 'integer'
}
}

@@ -26,3 +28,3 @@ }

try {
fastify[loMethod]('/', schema, function (req, reply) {
fastify[loMethod]('/', opts, function (req, reply) {
reply.send(req.body)

@@ -29,0 +31,0 @@ })

@@ -68,3 +68,3 @@ /* eslint-disable no-useless-return */

Request: Request,
hooks: new Hooks()
preHandler: new Hooks().preHandler
}

@@ -71,0 +71,0 @@ buildSchema(handle)

@@ -27,3 +27,3 @@ 'use strict'

t.strictEqual(reply.res, response)
t.strictEqual(reply.handle, handle)
t.strictEqual(reply.store, handle)
})

@@ -30,0 +30,0 @@

@@ -8,17 +8,19 @@ 'use strict'

const schema = {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string'
const opts = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
},
'2xx': {
type: 'object',
properties: {
hello: {
type: 'number'
},
'2xx': {
type: 'object',
properties: {
hello: {
type: 'number'
}
}

@@ -33,3 +35,3 @@ }

try {
fastify.get('/string', schema, function (req, reply) {
fastify.get('/string', opts, function (req, reply) {
reply.code(200).send({ hello: 'world' })

@@ -46,3 +48,3 @@ })

try {
fastify.get('/number', schema, function (req, reply) {
fastify.get('/number', opts, function (req, reply) {
reply.code(201).send({ hello: 55 })

@@ -59,3 +61,3 @@ })

try {
fastify.get('/wrong-object-for-schema', schema, function (req, reply) {
fastify.get('/wrong-object-for-schema', opts, function (req, reply) {
// will send { hello: null }

@@ -74,3 +76,3 @@ reply.code(201).send({ hello: 'world' })

// no checks
fastify.get('/empty', schema, function (req, reply) {
fastify.get('/empty', opts, function (req, reply) {
reply.code(204).send()

@@ -77,0 +79,0 @@ })

@@ -9,9 +9,11 @@ 'use strict'

const schema = {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
const opts = {
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}

@@ -26,3 +28,3 @@ }

try {
fastify.get('/', schema, function (req, reply) {
fastify.get('/', opts, function (req, reply) {
const promise = new Promise((resolve, reject) => {

@@ -42,3 +44,3 @@ resolve({ hello: 'world' })

try {
fastify.get('/return', schema, function (req, reply) {
fastify.get('/return', opts, function (req, reply) {
const promise = new Promise((resolve, reject) => {

@@ -58,3 +60,3 @@ resolve({ hello: 'world' })

try {
fastify.get('/error', schema, function (req, reply) {
fastify.get('/error', opts, function (req, reply) {
const promise = new Promise((resolve, reject) => {

@@ -74,3 +76,3 @@ reject(new Error('some error'))

try {
fastify.get('/bluebird', schema, function (req, reply) {
fastify.get('/bluebird', opts, function (req, reply) {
const promise = new Bluebird((resolve, reject) => {

@@ -90,3 +92,3 @@ resolve({ hello: 'world' })

try {
fastify.get('/bluebird-error', schema, function (req, reply) {
fastify.get('/bluebird-error', opts, function (req, reply) {
const promise = new Bluebird((resolve, reject) => {

@@ -93,0 +95,0 @@ reject(new Error('some error'))

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc