TypeScript

Fastify is shipped with a typings file, but it still require to install @types/node, depending on the Node.js version that you are using.

Types support

We do care about the TypeScript community, but the framework is written in plain JavaScript and currently no one of the core team is a TypeScript user while only one of the collaborators is. We do our best to have the typing updated with the latest version of the API, but it can happen that the typings are not in sync.Luckly this is Open Source and you can contribute to fix them, we will be very happy to accept the fix and release it as soon as possible as a patch release. Checkout the contributing rules!

Plugins may or may not include typings. See Plugin Types for more information.

Example

This example TypeScript app closely aligns with the JavaScript examples:

  1. import * as fastify from 'fastify'
  2. import { Server, IncomingMessage, ServerResponse } from 'http'
  3. // Create a http server. We pass the relevant typings for our http version used.
  4. // By passing types we get correctly typed access to the underlying http objects in routes.
  5. // If using http2 we'd pass <http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
  6. const server: fastify.FastifyInstance<Server, IncomingMessage, ServerResponse> = fastify({})
  7. const opts: fastify.RouteShorthandOptions = {
  8. schema: {
  9. response: {
  10. 200: {
  11. type: 'object',
  12. properties: {
  13. pong: {
  14. type: 'string'
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. server.get('/ping', opts, (request, reply) => {
  22. console.log(reply.res) // this is the http.ServerResponse with correct typings!
  23. reply.code(200).send({ pong: 'it worked!' })
  24. })

Generic Parameters

Since you can validate the querystring, params, body, and headers, you can also override the default types of those values on the request interface:

  1. import * as fastify from 'fastify'
  2. const server = fastify({})
  3. interface Query {
  4. foo?: number
  5. }
  6. interface Params {
  7. bar?: string
  8. }
  9. interface Body {
  10. baz?: string
  11. }
  12. interface Headers {
  13. a?: string
  14. }
  15. const opts: fastify.RouteShorthandOptions = {
  16. schema: {
  17. querystring: {
  18. type: 'object',
  19. properties: {
  20. foo: {
  21. type: 'number'
  22. }
  23. }
  24. },
  25. params: {
  26. type: 'object',
  27. properties: {
  28. bar: {
  29. type: 'string'
  30. }
  31. }
  32. },
  33. body: {
  34. type: 'object',
  35. properties: {
  36. baz: {
  37. type: 'string'
  38. }
  39. }
  40. },
  41. headers: {
  42. type: 'object',
  43. properties: {
  44. a: {
  45. type: 'string'
  46. }
  47. }
  48. }
  49. }
  50. }
  51. server.get<Query, Params, Body, Headers>('/ping/:bar', opts, (request, reply) => {
  52. console.log(request.query) // this is of type Query!
  53. console.log(request.params) // this is of type Params!
  54. console.log(request.body) // this is of type Body!
  55. console.log(request.headers) // this is of type Headers!
  56. reply.code(200).send({ pong: 'it worked!' })
  57. })

All generic types are optional, so you can also pass types for the parts you validate with schemas:

  1. import * as fastify from 'fastify'
  2. const server = fastify({})
  3. interface Params {
  4. bar?: string
  5. }
  6. const opts: fastify.RouteShorthandOptions = {
  7. schema: {
  8. params: {
  9. type: 'object',
  10. properties: {
  11. bar: {
  12. type: 'string'
  13. }
  14. }
  15. },
  16. }
  17. }
  18. server.get<fastify.DefaultQuery, Params, unknown>('/ping/:bar', opts, (request, reply) => {
  19. console.log(request.query) // this is of type fastify.DefaultQuery!
  20. console.log(request.params) // this is of type Params!
  21. console.log(request.body) // this is of type unknown!
  22. console.log(request.headers) // this is of type fastify.DefaultHeader because typescript will use the default type value!
  23. reply.code(200).send({ pong: 'it worked!' })
  24. })
  25. // Given that you haven't validated the querystring, body, or headers, it would be best
  26. // to type those params as 'unknown'. However, it's up to you. The example below is the
  27. // best way to prevent you from shooting yourself in the foot. In other words, don't
  28. // use values you haven't validated.
  29. server.get<unknown, Params, unknown, unknown>('/ping/:bar', opts, (request, reply) => {
  30. console.log(request.query) // this is of type unknown!
  31. console.log(request.params) // this is of type Params!
  32. console.log(request.body) // this is of type unknown!
  33. console.log(request.headers) // this is of type unknown!
  34. reply.code(200).send({ pong: 'it worked!' })
  35. })

HTTP Prototypes

By default, fastify will determine which version of http is being used based on the options you pass to it. If for any reason you need to override this you can do so as shown below:

  1. interface CustomIncomingMessage extends http.IncomingMessage {
  2. getClientDeviceType: () => string
  3. }
  4. // Passing overrides for the http prototypes to fastify
  5. const server: fastify.FastifyInstance<http.Server, CustomIncomingMessage, http.ServerResponse> = fastify()
  6. server.get('/ping', (request, reply) => {
  7. // Access our custom method on the http prototype
  8. const clientDeviceType = request.raw.getClientDeviceType()
  9. reply.send({ clientDeviceType: `you called this endpoint from a ${clientDeviceType}` })
  10. })

In this example we pass a modified http.IncomingMessage interface since it has been extended elsewhere in our application.

Contributing

TypeScript related changes can be considered to fall into one of two categories:

  • Core - The typings bundled with fastify
  • Plugins - Fastify ecosystem plugins
    Make sure to read our CONTRIBUTING.md file before getting started to make sure things go smoothly!

Core Types

When updating core types you should make a PR to this repository. Ensure you:

  • Update examples/typescript-server.ts to reflect the changes (if necessary)
  • Update test/types/index.ts to validate changes work as expected

Plugin Types

Typings for plugins are hosted in DefinitelyTyped. This means when using plugins you should install like so:

  1. npm install fastify-url-data @types/fastify-url-data

After this you should be good to go. Some types might not be available yet, so don't be shy about contributing.

Authoring Plugin Types

Typings for many plugins that extend the FastifyRequest and FastifyReply objects can be achieved as shown below.

This code demonstrates adding types for fastify-url-data to your application.

  1. // filename: custom-types.d.ts
  2. // Core typings and values
  3. import fastify = require('fastify');
  4. // Extra types that will be used for plugin typings
  5. import { UrlObject } from 'url';
  6. // Extend FastifyReply with the "fastify-url-data" plugin
  7. declare module 'fastify' {
  8. interface FastifyRequest {
  9. urlData (): UrlObject
  10. }
  11. }
  12. declare function urlData (): void
  13. declare namespace urlData {}
  14. export = urlData;

Now you can use fastify-url-data like so:

  1. import * as fastify from 'fastify'
  2. import * as urlData from 'fastify-url-data'
  3. /// <reference types="./custom-types.d.ts"/>
  4. const server = fastify();
  5. server.register(urlData)
  6. server.get('/data', (request, reply) => {
  7. console.log(request.urlData().auth)
  8. console.log(request.urlData().host)
  9. console.log(request.urlData().port)
  10. console.log(request.urlData().query)
  11. reply.send({msg: 'ok'})
  12. })
  13. server.listen(3030)

Remember, if you author typings for a plugin you should publish them to DefinitelyTyped!

原文: https://www.fastify.io/docs/v2.0.x/TypeScript