Skip to content

Latest commit

 

History

History
33 lines (23 loc) · 1.41 KB

File metadata and controls

33 lines (23 loc) · 1.41 KB

API First going from Typebox to Swagger with Fastify (Sandbox)

Features

  • Fastify + Typebox + TypeScript
  • Generated Swagger Schema based on registered routes and Typebox schemas
  • Swagger UI

Usage

  • npm run start and then, via httpie or cURL:
    • GET all users http localhost:8080/users?name=Taylor
    • CREATE new user http POST localhost:8080/users name=Alex
    • Check the generate Schema: http GET http://localhost:8080/docs/json
    • View the Swagger UI

Troubleshooting

  • Take care to always add routes inside a .register block. Otherwise swagger does not seem to pick up on them.

Created via

  • npm init -y
  • npm i fastify
  • npm i -D typescript @types/node ts-node
  • npx tsc --init and configure outdir: "dist", "target": "es2017" and other smaller things.
  • mkdir src && touch src/index.ts and put code from fastify swagger.
  • Add "build": "tsc" and "start": "ts-node src/server.ts" to package.json.
  • npm i @sinclair/typebox @fastify/type-provider-typebox @fastify/swagger and add /users with schema to src/index.ts according to the official docs

References