GithubHelp home page GithubHelp logo

fastify / fastify-accepts Goto Github PK

View Code? Open in Web Editor NEW
66.0 66.0 9.0 136 KB

Add accepts parser to fastify

License: MIT License

JavaScript 67.11% TypeScript 32.89%
accept-header encoding fastify fastify-plugin

fastify-accepts's Introduction

CI Package Manager CI Web SIte js-standard-style CII Best Practices

NPM version NPM downloads Security Responsible Disclosure Discord Contribute with Gitpod Open Collective backers and sponsors


An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users. How can you efficiently handle the resources of your server, knowing that you are serving the highest number of requests as possible, without sacrificing security validations and handy development?

Enter Fastify. Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is inspired by Hapi and Express and as far as we know, it is one of the fastest web frameworks in town.

The main branch refers to the Fastify v5 release, which is not released/LTS yet. Check out the 4.x branch for v4.

Table of Contents

Quick start

Create a folder and make it your current working directory:

mkdir my-app
cd my-app

Generate a fastify project with npm init:

npm init fastify

Install dependencies:

npm i

To start the app in dev mode:

npm run dev

For production mode:

npm start

Under the hood npm init downloads and runs Fastify Create, which in turn uses the generate functionality of Fastify CLI.

Install

To install Fastify in an existing project as a dependency:

Install with npm:

npm i fastify

Install with yarn:

yarn add fastify

Example

// Require the framework and instantiate it

// ESM
import Fastify from 'fastify'

const fastify = Fastify({
  logger: true
})
// CommonJs
const fastify = require('fastify')({
  logger: true
})

// Declare a route
fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' })
})

// Run the server!
fastify.listen({ port: 3000 }, (err, address) => {
  if (err) throw err
  // Server is now listening on ${address}
})

with async-await:

// ESM
import Fastify from 'fastify'

const fastify = Fastify({
  logger: true
})
// CommonJs
const fastify = require('fastify')({
  logger: true
})

fastify.get('/', async (request, reply) => {
  reply.type('application/json').code(200)
  return { hello: 'world' }
})

fastify.listen({ port: 3000 }, (err, address) => {
  if (err) throw err
  // Server is now listening on ${address}
})

Do you want to know more? Head to the Getting Started.

Note

.listen binds to the local host, localhost, interface by default (127.0.0.1 or ::1, depending on the operating system configuration). If you are running Fastify in a container (Docker, GCP, etc.), you may need to bind to 0.0.0.0. Be careful when deciding to listen on all interfaces; it comes with inherent security risks. See the documentation for more information.

Core features

  • Highly performant: as far as we know, Fastify is one of the fastest web frameworks in town, depending on the code complexity we can serve up to 76+ thousand requests per second.
  • Extensible: Fastify is fully extensible via its hooks, plugins and decorators.
  • Schema based: even if it is not mandatory we recommend to use JSON Schema to validate your routes and serialize your outputs, internally Fastify compiles the schema in a highly performant function.
  • Logging: logs are extremely important but are costly; we chose the best logger to almost remove this cost, Pino!
  • Developer friendly: the framework is built to be very expressive and help the developer in their daily use, without sacrificing performance and security.

Benchmarks

Machine: EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD.

Method:: autocannon -c 100 -d 40 -p 10 localhost:3000 * 2, taking the second average

Framework Version Router? Requests/sec
Express 4.17.3 14,200
hapi 20.2.1 42,284
Restify 8.6.1 50,363
Koa 2.13.0 54,272
Fastify 4.0.0 77,193
-
http.Server 16.14.2 74,513

Benchmarks taken using https://github.com/fastify/benchmarks. This is a synthetic, "hello world" benchmark that aims to evaluate the framework overhead. The overhead that each framework has on your application depends on your application, you should always benchmark if performance matters to you.

Documentation

中文文档地址

Ecosystem

  • Core - Core plugins maintained by the Fastify team.
  • Community - Community supported plugins.
  • Live Examples - Multirepo with a broad set of real working examples.
  • Discord - Join our discord server and chat with the maintainers.

Support

Please visit Fastify help to view prior support issues and to ask new support questions.

Contributing

Whether reporting bugs, discussing improvements and new ideas or writing code, we welcome contributions from anyone and everyone. Please read the CONTRIBUTING guidelines before submitting pull requests.

Team

Fastify is the result of the work of a great community. Team members are listed in alphabetical order.

Lead Maintainers:

Fastify Core team

Fastify Plugins team

Great Contributors

Great contributors on a specific area in the Fastify ecosystem will be invited to join this group by Lead Maintainers.

Past Collaborators

Hosted by

We are a At-Large Project in the OpenJS Foundation.

Sponsors

Support this project by becoming a SPONSOR! Fastify has an Open Collective page where we accept and manage financial contributions.

Acknowledgements

This project is kindly sponsored by:

Past Sponsors:

This list includes all companies that support one or more of the team members in the maintenance of this project.

License

Licensed under MIT.

For your convenience, here is a list of all the licenses of our production dependencies:

  • MIT
  • ISC
  • BSD-3-Clause
  • BSD-2-Clause

fastify-accepts's People

Contributors

a-a-github-a-a avatar allevo avatar delvedor avatar dependabot-preview[bot] avatar dependabot[bot] avatar dimonnwc3 avatar eomm avatar fdawgs avatar frikille avatar github-actions[bot] avatar greenkeeper[bot] avatar jsumners avatar lependu avatar mcollina avatar optic-release-automation[bot] avatar salmanm avatar samchungy avatar serayaeryn avatar uzlopak avatar zekth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastify-accepts's Issues

ChainAlert: npm package release (2.3.0) has no matching tag in this repo

Dear fastify-accepts maintainers,
Thank you for your contribution to the open-source community.

This issue was automatically created to inform you a new version (2.3.0) of fastify-accepts was published without a matching tag in this repo.

As part of our efforts to fight software supply chain attacks, we would like to verify this release is known and intended, and not a result of an unauthorized activity.

If you find this behavior legitimate, kindly close and ignore this issue. Read more

badge

Accept type defaults to first item

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

2.1.0

Plugin version

No response

Node.js version

16.13.1

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.1

Description

When NO Accept: header is specified in the HTTP request,

req.accepts().type(a, b)

returns a by default whereas it should return null or undefined in such a case.

Steps to Reproduce

Issue a query with no Accept: header, and ask for a value in a type array.

Expected Behavior

return null when no Accept header match an array value
return undefined when no Accept header has been found.

Minimum requested version

The minimum requested version of Fastify for this plugin is v0.25.0, why?
Since you are just using decorateRequest you can use v0.13.1 :)

An in-range update of fastify-plugin is breaking the build 🚨

Version 1.2.0 of fastify-plugin was just published.

Branch Build failing 🚨
Dependency fastify-plugin
Current Version 1.1.3
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

fastify-plugin is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
  • coverage/coveralls First build on greenkeeper/fastify-plugin-1.2.0 at 95.0% Details

Release Notes v1.2.0
  • use fp meta name option instead of new symbol #44
Commits

The new version differs by 3 commits.

  • 9e164ab Bumped v1.2.0.
  • cdac717 chore(package): update ts-node to version 7.0.0 (#43)
  • 60832a3 have fp use meta name option instead of new symbol (#44)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Typescript Build Error on Accepts

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Hey there,

Not sure whether this should be raised as a bug or not but when trying to install this in a TS project as this may be intended but I received the following error:

https://github.com/seek-oss/slowify/actions/runs/5167692569/jobs/9308739911

Error: tsc      │ node_modules/@fastify/accepts/types/index.d.ts(1,25): error TS7016: Could not find a declaration file for module 'accepts'. '/home/runner/work/slowify/slowify/node_modules/accepts/index.js' implicitly has an 'any' type.

Could @types/accepts be made a dependency for this library to avoid consumers needing to install it themselves?

An in-range update of accepts is breaking the build 🚨

The dependency accepts was updated from 1.3.5 to 1.3.6.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

accepts is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes for 1.3.6
  • deps: mime-types@~2.1.24
    • deps: mime-db@~1.40.0
Commits

The new version differs by 47 commits.

There are 47 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of fastify is breaking the build 🚨

Version 1.8.0 of fastify was just published.

Branch Build failing 🚨
Dependency fastify
Current Version 1.7.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

fastify is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Release Notes v1.8.0

Features

  • Configure request id header key - #1024
  • Support case insensitivity. - #1017
  • Feature/#713 beforeHandler for setNotFoundHandler - #1005

Fixes

Docs

  • Update Ecosystem.md - #874
  • add-chinese-doc-link - #1021
  • Update Middlewares.md - #996
  • Rename swaggergen to openapi glue - #1013
  • Update Reply.md - #1003
  • Add fastify-wamp-router plugin to docs/Ecosystem.md - #1002
  • Add missing documentation links - #1001
  • fix syntax in doc - #997
  • Add fastify-xml-body-parser in community list - #782

Typescript

  • fix listen ts types - #1000

Internals

  • test: add test case for request.id - #1030
  • Add test for checking multipart request using form-data module - #1026
  • Resolves issue #1006 - #1008
Commits

The new version differs by 19 commits.

  • 55fc867 Bumped v1.8.0
  • e58c627 test: add test case for request.id (#1030)
  • 7f734fd Add test for checking multipart request using form-data module (#1026)
  • 4949a9c Configure request id header key (#1024)
  • 7476cb4 Update Ecosystem.md (#874)
  • 42d10f6 New instructions have been added for use with fastif-cli (#1020)
  • a137087 add-chinese-doc-link (#1021)
  • 92ee7db Support case insensitivity. (#1017)
  • 0dfb356 Update Middlewares.md (#996)
  • 8955f27 Resolves issue #1006 (#1008)
  • dd337fd Feature/#713 beforeHandler for setNotFoundHandler (#1005)
  • 1b92295 Rename swaggergen to openapi glue (#1013)
  • 2f96a80 fix listen ts types (#1000)
  • 68114ee Add fastify-wamp-router plugin to docs/Ecosystem.md (#1002)
  • 724199f Update Reply.md (#1003)

There are 19 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Expose accepts() methods in the Request

I think having some more accessors in the Request  might be pretty handy, as an example:

fastify.post('/', function (req, reply) {
  switch(req.type(['json', 'html'])) {
    case 'json':
      reply.type('application/json').send({hello: 'world'})
      break
    case 'html':
      reply.type('text/html').send('<b>hello, world!</b>')
      break
    default:
      reply.send(Boom.notAcceptable('unacceptable'))
      break
  }
})

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml
  • The new Node.js version is in-range for the engines in 1 of your package.json files, so that was left alone

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of request is breaking the build 🚨

Version 2.82.0 of request just got published.

Branch Build failing 🚨
Dependency request
Current Version 2.81.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As request is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪

Status Details
  • coverage/coveralls Coverage pending from Coveralls.io Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Commits

The new version differs by 27 commits.

  • 0ab5c36 2.82.0
  • ffdf0d3 Updating deps.
  • 4386836 Merge branch 'master' of github.com:request/request
  • 1527407 Merge pull request #2703 from ryysud/add-nodejs-v8-to-travis
  • 3afcbf8 Merge branch 'master' of github.com:request/request
  • 479143d Update of hawk and qs to latest version (#2751)
  • 169be11 Add Node.js v8 to Travis CI
  • 643c43a Fixed some text in README.md (#2658)
  • e8fca51 chore(package): update aws-sign2 to version 0.7.0 (#2635)
  • e999203 Update README to simplify & update convenience methods (#2641)
  • 6f286c8 lint fix, PR from pre-standard was merged with passing tests
  • a765593 Add convenience method for HTTP OPTIONS (#2541)
  • 52d6945 Add promise support section to README (#2605)
  • b12a624 refactor(lint): replace eslint with standard (#2579)
  • 29a0b17 Merge pull request #2598 from request/greenkeeper-codecov-2.0.2

There are 27 commits in total.

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.