Skip to content

Conversation

@Ethan-Arrowood
Copy link
Member

@Ethan-Arrowood Ethan-Arrowood commented Nov 22, 2019

Checklist

While working on the new documentation I realized that when defining generic properties for routes can be extremely repetitive, and exhaustive when only defining one of the 4 available generics (Body, Querystring, Params, Headers). For example:

import fastify, { 
  RouteShorthandOptions, 
  RequestBodyDefault, 
  RequestParamsDefault, 
  RequestHeadersDefault } from 'fastify'

const server = fastify()

type QuerystringType = {
  username: string,
  password: number
}

server.get<
  RequestBodyDefault,
  QuerystringType,
  RequestParamsDefault, 
  RequestHeadersDefault
>('/auth', async (request, reply) => {
  const { username, password } = request.query
  return `logged in!`
}) 

In this example we only need to modify the Querystring generic, but are required to pass in the preceding Body generic and the remaining Params, Headers, and Context generics.

This PR merges the 4 generics related to the request object into a single generic property RequestGeneric, allowing the developer to specify only the properties necessary using named properties. Thus, simplifying the previous example into:

import fastify from 'fastify'

const server = fastify()

type QuerystringType = {
  username: string,
  password: number
}

server.get<{ Querystring: QuerystringType }>('/auth', async (request, reply) => {
  const { username, password } = request.query
  return `logged in!`
}) 

Something going on in the tsd library we're using to test the type definitions is causing a previously passing test to fail so I have an issue opened to resolve that. We can consider this PR blocked until that is fixed.

@Ethan-Arrowood Ethan-Arrowood added typescript TypeScript related v3.x Issue or pr related to Fastify v3 labels Nov 22, 2019
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Ethan-Arrowood Ethan-Arrowood mentioned this pull request Nov 25, 2019
4 tasks
@Ethan-Arrowood Ethan-Arrowood merged commit 433801e into fastify:next Nov 25, 2019
@Ethan-Arrowood Ethan-Arrowood deleted the use-object-generic branch November 25, 2019 18:34
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

typescript TypeScript related v3.x Issue or pr related to Fastify v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants