{"id":16720184,"url":"https://github.com/hsynlms/fastify-prettier","last_synced_at":"2025-04-10T09:52:59.877Z","repository":{"id":51305935,"uuid":"273084086","full_name":"hsynlms/fastify-prettier","owner":"hsynlms","description":"A Prettier plugin for Fastify.","archived":false,"fork":false,"pushed_at":"2022-08-09T14:12:57.000Z","size":161,"stargazers_count":8,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T08:47:51.019Z","etag":null,"topics":["beautify","fastify","fastify-plugin","json","prettier","prettify"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hsynlms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/funding.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"hsynlms"}},"created_at":"2020-06-17T21:43:36.000Z","updated_at":"2025-03-19T04:03:33.000Z","dependencies_parsed_at":"2022-09-26T21:31:38.375Z","dependency_job_id":null,"html_url":"https://github.com/hsynlms/fastify-prettier","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hsynlms%2Ffastify-prettier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hsynlms%2Ffastify-prettier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hsynlms%2Ffastify-prettier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hsynlms%2Ffastify-prettier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hsynlms","download_url":"https://codeload.github.com/hsynlms/fastify-prettier/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248198213,"owners_count":21063626,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["beautify","fastify","fastify-plugin","json","prettier","prettify"],"created_at":"2024-10-12T22:06:07.628Z","updated_at":"2025-04-10T09:52:59.851Z","avatar_url":"https://github.com/hsynlms.png","language":"JavaScript","funding_links":["https://github.com/sponsors/hsynlms"],"categories":["JavaScript"],"sub_categories":[],"readme":"# fastify-prettier\n\u003e A simple and lightweight beautifier plugin for [Fastify](https://github.com/fastify/fastify).\n\n[![Downloads](https://img.shields.io/npm/dm/fastify-prettier.svg)](https://npmjs.com/fastify-prettier)\n[![install size](https://packagephobia.com/badge?p=fastify-prettier)](https://packagephobia.com/result?p=fastify-prettier)\n\n`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.\n\n`fastify-prettier` uses `onSend` fastify hook to beautify the response payload before it gets sent.\n\n**Note:** `streams` and `buffers` are excluded in beautification process.\n**Note:** Fastify v4 support is shipped with v2.0.0.\n\n## Installation\n```\n$ npm install fastify-prettier\n```\n\n## Usage\n\n```js\nconst fastify = require('fastify')()\nconst fastifyPrettier = require('fastify-prettier')\n\nfastify.register(\n  fastifyPrettier,\n  {\n    fallbackOnError: false\n  }\n)\n\nfastify.get('/', (req, reply) =\u003e {\n  const obj = {\n    blackLivesMatter: true,\n    favSinger: 'Ahmet Kaya'\n  }\n\n  reply\n    .type('application/json')\n    .send(obj)\n})\n\nfastify.listen(3000, () =\u003e {\n  console.log('Fastify server is running on port: 3000')\n})\n\n// -------------------------------\n\n// http://localhost:3000 -\u003e will print out below result\n/*\n{\"blackLivesMatter\":true,\"favSinger\":\"Ahmet Kaya\"}\n*/\n\n// http://localhost:3000?pretty=true -\u003e will print out below result\n/*\n{\n  \"blackLivesMatter\": true,\n  \"favSinger\": \"Ahmet Kaya\"\n}\n*/\n```\n\nYou are allowed to change the query parameter option.\n\n```js\nfastify.register(\n  fastifyPrettier,\n  {\n    query: {\n      name: 'beautify',\n      value: 'yes'\n    }\n  }\n)\n\n// -------------------------------\n\n// http://localhost:3000 -\u003e will print out below result\n/*\n{\"blackLivesMatter\":true,\"favSinger\":\"Ahmet Kaya\"}\n*/\n\n// http://localhost:3000?beautify=yes -\u003e will print out below result\n/*\n{\n  \"blackLivesMatter\": true,\n  \"favSinger\": \"Ahmet Kaya\"\n}\n*/\n```\n\nYou can enable beautification for all outgoing payloads regardless the query parameter.\n\n```js\nfastify.register(\n  fastifyPrettier,\n  {\n    alwaysOn: true\n  }\n)\n\n// -------------------------------\n\n// http://localhost:3000 -\u003e will print out below result\n/*\n{\n  \"blackLivesMatter\": true,\n  \"favSinger\": \"Ahmet Kaya\"\n}\n*/\n```\n\n### Usage of Decorator\n\nFeel free to use `prettier` decorator which beautifies the given content through the provided options whenever you need.\n\n```js\nconst response = fastify.prettier(\n  // content type can be: boolean, number, object, array, string\n  content,\n  // prettier options (please see: prettierOpts)\n  { parser: 'html', htmlWhitespaceSensitivity: 'ignore' }\n)\n```\n\n## Options\n\n| Name                   | Type               | Default                                     | Description                                                          |\n| ---                    | ---                | ---                                         | ---                                                                  |\n| alwaysOn               | boolean            | false                                       | To make all the payloads beautified in anyway   |\n| fallbackOnError        | boolean            | true                                        | If something bad happens, send the original payload. If its `false`, an error will be thrown |\n| overrideContentLength  | boolean            | true                                        | Re-calculate `content-length` header for the beautified response                         |\n| query                  | object             | `{ name: 'pretty', value: 'true' }`         | The query parameter that triggers the plugin to beautify the outgoing payload |\n| 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 |\n| prettierOpts           | object             | `{ tabWidth: 2, parser: 'json-stringify' }` | Please take a look prettier [official documentation](https://prettier.io/docs/en/options.html) for more information |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhsynlms%2Ffastify-prettier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhsynlms%2Ffastify-prettier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhsynlms%2Ffastify-prettier/lists"}