Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-prettier

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-prettier - npm Package Compare versions

Comparing version 1.1.9 to 2.0.0

.editorconfig

14

example.js
'use strict'
// get required node modules
const fastify = require('fastify')()

@@ -8,12 +7,8 @@ const fastifyPrettier = require('./src/index')

// defaults
const defaults = { port: 3000 }
;(async () => {
// register the plugin
await fastify.register(fastifyPrettier)
// register test route
fastify.get('/', (req, reply) => {
// create an object
const obj = {

@@ -30,10 +25,7 @@ blackLivesMatter: true,

// set return type
reply.type('application/json')
// return the object
reply.send(obj)
reply
.type('application/json')
.send(obj)
})
// initialize the fastify server
fastify.listen(defaults.port, () => {

@@ -40,0 +32,0 @@ console.log(

{
"name": "fastify-prettier",
"version": "1.1.9",
"version": "2.0.0",
"description": "a simple and lightweight beautifier plugin for Fastify.",

@@ -9,4 +9,3 @@ "main": "src/index.js",

"dev": "node example.js",
"lint": "npm run lint:standard",
"lint:standard": "standard --verbose | snazzy || exit 0"
"lint": "standard --verbose"
},

@@ -32,14 +31,13 @@ "repository": {

"dependencies": {
"fastify-plugin": "^3.0.0",
"is-stream": "^2.0.0",
"prettier": "^2.3.0"
"fastify-plugin": "^4.1.0",
"is-stream": "^2.0.1",
"prettier": "^2.7.1"
},
"devDependencies": {
"chalk": "^4.1.1",
"fastify": "^3.15.1",
"jest": "^26.6.3",
"snazzy": "^9.0.0",
"standard": "^16.0.3",
"chalk": "^5.0.1",
"fastify": "^4.4.0",
"jest": "^28.1.3",
"standard": "^17.0.0",
"xml2js": "^0.4.23"
}
}
# fastify-prettier
> A simple and lightweight beautifier plugin for [Fastify](https://github.com/fastify/fastify).
[![NPM](https://nodei.co/npm/fastify-prettier.png)](https://nodei.co/npm/fastify-prettier/)
[![Downloads](https://img.shields.io/npm/dm/fastify-prettier.svg)](https://npmjs.com/fastify-prettier)
[![install size](https://packagephobia.com/badge?p=fastify-prettier)](https://packagephobia.com/result?p=fastify-prettier)

@@ -11,39 +12,15 @@ `fastify-prettier` has support of beautifying payloads via query parameter to make responses more readable for developers/humans. Decorator`prettier` can also be used anywhere in the fastify server as the content beautifier. The plugin itself uses [prettier](https://github.com/prettier/prettier) under the hood and is capable of parsing/formatting anything that prettier can.

**Note:** `streams` and `buffers` are excluded in beautification process.
**Note:** Fastify v4 support is shipped with v2.0.0.
**Note:** `fastify v3` compatibility shipped with v1.1.5
## Options
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| alwaysOn | boolean | false | To make all the payloads beautified in anyway |
| fallbackOnError | boolean | true | If something bad happens, send the original payload. If its `false`, an error will be thrown |
| overrideContentLength | boolean | true | Re-calculate `content-length` header for the beautified response |
| query | object | `{ name: 'pretty', value: 'true' }` | The query parameter that triggers the plugin to beautify the outgoing payload |
| enableOnSendHook | boolean | true | Allow the plugin to get injected into fastify `onSend` hook to beautify outgoing responses. The prettier decorator can still be used even if that option is disabled |
| prettierOpts | object | `{ tabWidth: 2, parser: 'json-stringify' }` | Please take a look prettier [official documentation](https://prettier.io/docs/en/options.html) for more information |
## Decorator
Feel free to use `prettier` decorator which beautifies the given content through the provided options whenever you need.
```js
// example of using prettier decorator
const response = fastify.prettier(
// content type can be: boolean, number, object, array, string
content,
// prettier options (please see: prettierOpts)
{ parser: 'html', htmlWhitespaceSensitivity: 'ignore' }
)
## Installation
```
$ npm install fastify-prettier
```
## Usage
## Examples
```js
// get required modules
const fastify = require('fastify')()
const fastifyPrettier = require('fastify-prettier')
// register fastify-prettier plugin
fastify.register(

@@ -56,5 +33,3 @@ fastifyPrettier,

// test fastify server route
fastify.get('/', (req, reply) => {
// create an object
const obj = {

@@ -65,10 +40,7 @@ blackLivesMatter: true,

// set return type
reply.type('application/json')
// return the object
reply.send(obj)
reply
.type('application/json')
.send(obj)
})
// initialize the fastify server
fastify.listen(3000, () => {

@@ -97,3 +69,2 @@ console.log('Fastify server is running on port: 3000')

```js
// register fastify-prettier plugin
fastify.register(

@@ -128,3 +99,2 @@ fastifyPrettier,

```js
// register fastify-prettier plugin
fastify.register(

@@ -148,9 +118,24 @@ fastifyPrettier,

## Installation
`npm install fastify-prettier`
### Usage of Decorator
## Contribution
Contributions and pull requests are kindly welcomed!
Feel free to use `prettier` decorator which beautifies the given content through the provided options whenever you need.
## License
This project is licensed under the terms of the [MIT license](https://github.com/hsynlms/fastify-prettier/blob/master/LICENSE).
```js
const response = fastify.prettier(
// content type can be: boolean, number, object, array, string
content,
// prettier options (please see: prettierOpts)
{ parser: 'html', htmlWhitespaceSensitivity: 'ignore' }
)
```
## Options
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| alwaysOn | boolean | false | To make all the payloads beautified in anyway |
| fallbackOnError | boolean | true | If something bad happens, send the original payload. If its `false`, an error will be thrown |
| overrideContentLength | boolean | true | Re-calculate `content-length` header for the beautified response |
| query | object | `{ name: 'pretty', value: 'true' }` | The query parameter that triggers the plugin to beautify the outgoing payload |
| enableOnSendHook | boolean | true | Allow the plugin to get injected into fastify `onSend` hook to beautify outgoing responses. The prettier decorator can still be used even if that option is disabled |
| prettierOpts | object | `{ tabWidth: 2, parser: 'json-stringify' }` | Please take a look prettier [official documentation](https://prettier.io/docs/en/options.html) for more information |
'use strict'
// get required modules
const fastifyPlugin = require('fastify-plugin')

@@ -9,3 +8,2 @@ const prettier = require('prettier')

// plugin defaults
const defaults = {

@@ -27,5 +25,3 @@ decorator: 'prettier',

// declaration of prettier plugin for fastify
function prettierPlugin (fastify, opts, done) {
// combine defaults with provided options
const options = Object.assign({}, defaults, opts)

@@ -35,6 +31,4 @@

const amazeMe = (content, opts) => {
// declaration of stringified content
let strContent = ''
// validations
if (typeof content === 'function') {

@@ -48,3 +42,2 @@ throw Error(`${pkg.name} cannot beautify 'function' type objects`)

// return amazed result
return prettier.format(

@@ -56,3 +49,2 @@ strContent,

// register the amazer as a decorator as well
fastify.decorate(options.decorator, amazeMe)

@@ -68,10 +60,7 @@

// new payload variable declaration
// set current payload as fallback
let prettifiedPayload = payload
// indicates if the body is prettified or not
let isPrettified = false
// check options
if (options.alwaysOn === true ||

@@ -84,6 +73,4 @@ // eslint-disable-next-line

// successfully prettified
isPrettified = true
} catch (err) {
// something bad happened
if (options.fallbackOnError === false) {

@@ -104,3 +91,2 @@ // throw the error if fallback is disabled

// done, sent back the new payload
return prettifiedPayload

@@ -110,13 +96,11 @@ })

// done
done()
}
// export the plugin
module.exports = fastifyPlugin(
prettierPlugin,
{
fastify: '3.x',
fastify: '4.x',
name: pkg.name
}
)
'use strict'
// get required node modules
const Fastify = require('fastify')

@@ -9,11 +8,7 @@ const fastifyPrettier = require('./src/index')

// fastify server generator
const generateServer = async pluginOpts => {
// initialize fastify server
const fastify = new Fastify()
// register the plugin
await fastify.register(fastifyPrettier, pluginOpts)
// return the instance
return fastify

@@ -25,163 +20,138 @@ }

// eslint-disable-next-line
test('prettify empty response', async done => {
// initialize a fastify server
const fastify = await generateServer()
test('prettify empty response', done => {
generateServer()
.then(fastify => {
fastify.get('/', (req, reply) => {
reply.send()
})
// define a route
fastify.get('/', (req, reply) => {
// send response
reply.send()
})
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(res.payload).toBe('')
done()
// test
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(res.payload).toBe('')
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('prettify empty string response', async done => {
// initialize a fastify server
const fastify = await generateServer()
test('prettify empty string response', done => {
generateServer()
.then(fastify => {
fastify.get('/', (req, reply) => {
reply.send('')
})
// define a route
fastify.get('/', (req, reply) => {
// send response
reply.send('')
})
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(res.payload).toBe('')
done()
// test
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(res.payload).toBe('')
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('prettify json response', async done => {
// initialize a fastify server
const fastify = await generateServer()
test('prettify json response', done => {
generateServer()
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
reply
.type('application/json')
.send(obj)
})
// set return type
reply.type('application/json')
// send response
reply.send(obj)
})
// test
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
})
// eslint-disable-next-line
test('buffer response returned as it is', async done => {
// initialize a fastify server
const fastify = await generateServer()
// define a route
fastify.get('/', (req, reply) => {
// send file buffer response
fs.readFile('test.json', (err, fileBuffer) => {
reply.send(err || fileBuffer)
})
})
// test
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err1, res1) => {
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err2, res2) => {
(err, res) => {
// eslint-disable-next-line
expect(res2.payload).toBe(res1.payload)
expect(
/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
}
)
})
})
// eslint-disable-next-line
test('stream response returned as it is', async done => {
// initialize a fastify server
const fastify = await generateServer()
test('buffer response returned as it is', done => {
generateServer()
.then(fastify => {
fastify.get('/', (req, reply) => {
fs.readFile('test.json', (err, fileBuffer) => {
reply.send(err || fileBuffer)
})
})
// define a route
fastify.get('/', (req, reply) => {
// create test file read stream
const stream = fs.createReadStream('test.json', 'utf8')
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err1, res1) => {
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err2, res2) => {
// eslint-disable-next-line
expect(res2.payload).toBe(res1.payload)
done()
// send stream response
reply.send(stream)
})
fastify.close()
}
)
}
)
})
})
// test
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err1, res1) => {
// eslint-disable-next-line
test('stream response returned as it is', done => {
generateServer()
.then(fastify => {
fastify.get('/', (req, reply) => {
const stream = fs.createReadStream('test.json', 'utf8')
reply.send(stream)
})
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err2, res2) => {
// eslint-disable-next-line
expect(res2.payload).toBe(res1.payload)
done()
(err1, res1) => {
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err2, res2) => {
// eslint-disable-next-line
expect(res2.payload).toBe(res1.payload)
done()
// close fastify server
fastify.close()
fastify.close()
}
)
}
)
}
)
})
})
// eslint-disable-next-line
test('prettify html response', async done => {
// initialize a fastify server
const fastify = await generateServer({
test('prettify html response', done => {
generateServer({
prettierOpts: {

@@ -192,217 +162,183 @@ parser: 'html',

})
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'html'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'html'
}
const response =
(new xml2js.Builder({ headless: true, renderOpts: false })).buildObject(obj)
// generate xml
const response = (new xml2js.Builder({ headless: true, renderOpts: false })).buildObject(obj)
reply
.type('text/html')
.send(response)
})
// set return type
reply.type('text/html')
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/<root>\n\s\s<test>true<\/test>\n\s\s<format>html<\/format>\n<\/root>/gi.test(res.payload)
).toBe(true)
done()
// send response
reply.send(response)
})
// test
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/<root>\n\s\s<test>true<\/test>\n\s\s<format>html<\/format>\n<\/root>/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('prettify html response (by using decorator)', async done => {
// initialize a fastify server
const fastify = await generateServer()
test('prettify html response (by using decorator)', done => {
generateServer()
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'html'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'html'
}
const response = fastify.prettier(
(new xml2js.Builder({ headless: true, renderOpts: false })).buildObject(obj),
{ parser: 'html', htmlWhitespaceSensitivity: 'ignore' }
)
// use prettier decorator
const response = fastify.prettier(
(new xml2js.Builder({ headless: true, renderOpts: false })).buildObject(obj),
// override prettier options
{ parser: 'html', htmlWhitespaceSensitivity: 'ignore' }
)
reply
.type('text/html')
.send(response)
})
// set return type
reply.type('text/html')
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/<root>\n\s\s<test>true<\/test>\n\s\s<format>html<\/format>\n<\/root>/gi.test(res.payload)
).toBe(true)
done()
// send response
reply.send(response)
})
// test
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/<root>\n\s\s<test>true<\/test>\n\s\s<format>html<\/format>\n<\/root>/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('prettify error response', async done => {
// initialize a fastify server
const fastify = await generateServer()
test('prettify error response', done => {
generateServer()
.then(fastify => {
fastify.inject(
{ method: 'GET', url: '/error?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/\{\n\s\s"message":\s"Route GET:\/error\?pretty=true\snot\sfound",\n\s\s"error":\s"Not\sFound",\n\s\s"statusCode":\s404\n\}/gi.test(res.payload)
).toBe(true)
done()
// test
fastify.inject(
{ method: 'GET', url: '/error?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/\{\n\s\s"message":\s"Route GET:\/error\?pretty=true\snot\sfound",\n\s\s"error":\s"Not\sFound",\n\s\s"statusCode":\s404\n\}/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('non-prettified response', async done => {
// initialize a fastify server
const fastify = await generateServer()
test('non-prettified response', done => {
generateServer()
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
reply
.type('application/json')
.send(obj)
})
// set return type
reply.type('application/json')
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(res.payload).toBe('{"test":true,"format":"json"}')
done()
// send response
reply.send(obj)
})
// test
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(res.payload).toBe('{"test":true,"format":"json"}')
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('alwaysOn option of the plugin active', async done => {
// initialize a fastify server
const fastify = await generateServer({ alwaysOn: true })
test('alwaysOn option of the plugin active', done => {
generateServer({ alwaysOn: true })
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
reply
.type('application/json')
.send(obj)
})
// set return type
reply.type('application/json')
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload)
).toBe(true)
done()
// send response
reply.send(obj)
})
// test
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('alwaysOn option of the plugin passive', async done => {
// initialize a fastify server
const fastify = await generateServer({ alwaysOn: false })
test('alwaysOn option of the plugin passive', done => {
generateServer({ alwaysOn: false })
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
reply
.type('application/json')
.send(obj)
})
// set return type
reply.type('application/json')
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
!(/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload))
).toBe(true)
done()
// send response
reply.send(obj)
})
// test
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
!(/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload))
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('query option of the plugin active', async done => {
// initialize a fastify server
const fastify = await generateServer(
test('query option of the plugin active', done => {
generateServer(
{

@@ -415,238 +351,203 @@ query: {

)
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
reply
.type('application/json')
.send(obj)
})
// set return type
reply.type('application/json')
fastify.inject(
{ method: 'GET', url: '/?beautify=yes' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload)
).toBe(true)
done()
// send response
reply.send(obj)
})
// test
fastify.inject(
{ method: 'GET', url: '/?beautify=yes' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('query option of the plugin passive', async done => {
// initialize a fastify server
const fastify = await generateServer(
{ query: false }
)
test('query option of the plugin passive', done => {
generateServer({ query: false })
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
reply
.type('application/json')
.send(obj)
})
// set return type
reply.type('application/json')
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
!(/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload))
).toBe(true)
done()
// send response
reply.send(obj)
})
// test
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
!(/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload))
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})
// eslint-disable-next-line
test('overrideContentLength option of the plugin active', async done => {
// initialize a fastify server
const fastify = await generateServer(
test('overrideContentLength option of the plugin active', done => {
generateServer(
{ overrideContentLength: true }
)
.then(fastify => {
fastify.get('/', async (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', async (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
// throw an error in async handler
reply
.type('application/json')
.send(obj.exception.throw())
})
// set return type
reply.type('application/json')
// send response by throwing an error in async handler
reply.send(obj.exception.throw())
})
// test
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err1, res1) => {
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err2, res2) => {
// eslint-disable-next-line
expect(
res2.headers['content-length'] - res1.headers['content-length'] === 14
).toBe(true)
done()
(err1, res1) => {
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err2, res2) => {
// eslint-disable-next-line
expect(
res2.headers['content-length'] - res1.headers['content-length'] === 14
).toBe(true)
done()
// close fastify server
fastify.close()
fastify.close()
}
)
}
)
}
)
})
})
// eslint-disable-next-line
test('overrideContentLength option of the plugin passive', async done => {
// initialize a fastify server
const fastify = await generateServer(
test('overrideContentLength option of the plugin passive', done => {
generateServer(
{ overrideContentLength: false }
)
.then(fastify => {
fastify.get('/', async (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', async (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
// throw an error in async handler
reply
.type('application/json')
.send(obj.exception.throw())
})
// set return type
reply.type('application/json')
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err1, res1) => {
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err2, res2) => {
// eslint-disable-next-line
expect(
res2.headers['content-length'] === res1.headers['content-length']
).toBe(true)
done()
// send response by throwing an error in async handler
reply.send(obj.exception.throw())
})
fastify.close()
}
)
}
)
})
})
// test
fastify.inject(
{ method: 'GET', url: '/' },
// eslint-disable-next-line
(err1, res1) => {
// eslint-disable-next-line
test('enableOnSendHook option of the plugin active', done => {
generateServer(
{ enableOnSendHook: true }
)
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'json'
}
reply
.type('application/json')
.send(obj)
})
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err2, res2) => {
(err, res) => {
// eslint-disable-next-line
expect(
res2.headers['content-length'] === res1.headers['content-length']
/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
}
)
})
})
// eslint-disable-next-line
test('enableOnSendHook option of the plugin active', async done => {
// initialize a fastify server
const fastify = await generateServer(
{ enableOnSendHook: true }
)
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
// set return type
reply.type('application/json')
// send response
reply.send(obj)
})
// test
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(
/\{\n\s\s"test":\strue,\n\s\s"format":\s"json"\n\}/gi.test(res.payload)
).toBe(true)
done()
// close fastify server
fastify.close()
}
)
})
// eslint-disable-next-line
test('enableOnSendHook option of the plugin passive', async done => {
// initialize a fastify server
const fastify = await generateServer(
test('enableOnSendHook option of the plugin passive', done => {
generateServer(
{ enableOnSendHook: false }
)
.then(fastify => {
fastify.get('/', (req, reply) => {
const obj = {
test: true,
format: 'json'
}
// define a route
fastify.get('/', (req, reply) => {
// variable definition
const obj = {
test: true,
format: 'json'
}
reply
.type('application/json')
.send(obj)
})
// set return type
reply.type('application/json')
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(res.payload).toBe('{"test":true,"format":"json"}')
done()
// send response
reply.send(obj)
})
// test
fastify.inject(
{ method: 'GET', url: '/?pretty=true' },
// eslint-disable-next-line
(err, res) => {
// eslint-disable-next-line
expect(res.payload).toBe('{"test":true,"format":"json"}')
done()
// close fastify server
fastify.close()
}
)
fastify.close()
}
)
})
})

@@ -100,2 +100,2 @@ [{"id":1,"first_name":"Yorgo","car_model":"Silverado","gender":"Male"},

{"id":99,"first_name":"Jaquith","car_model":"Mazda2","gender":"Female"},
{"id":100,"first_name":"Andres","car_model":"Yukon","gender":"Male"}]
{"id":100,"first_name":"Andres","car_model":"Yukon","gender":"Male"}]

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc