Skip to content

Commit

Permalink
feat: add @fastify/swagger-ui and build for prod (#50)
Browse files Browse the repository at this point in the history
* feat: add fastify swagger ui to prod

* doc: update README.md
  • Loading branch information
wd-David authored Dec 21, 2022
1 parent 626c38d commit 10ea46d
Show file tree
Hide file tree
Showing 7 changed files with 501 additions and 656 deletions.
7 changes: 0 additions & 7 deletions .lintstagedrc

This file was deleted.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ Opinionated boilerplate to build a Fastify app with better DX.

---

## Article
## Articles

[Better Backend DX: Fastify + ESBuild = ⚡️](https://davipon.hashnode.dev/better-backend-dx-fastify-esbuild)
[Better Backend DX: JSON Schema + TypeScript + Swagger = ✨ Vol. 1](https://davipon.hashnode.dev/better-backend-dx-json-schema-typescript-swagger-vol-1)
[Better Backend DX: JSON Schema + TypeScript + Swagger = ✨ Vol. 2](https://davipon.hashnode.dev/better-backend-dx-json-schema-typescript-swagger-vol-2)

## Features

- Use `@fastify/autoload` for filesystem-based routes & plugins.
- Use [`esbuild-kit/tsx`](https://github.com/esbuild-kit/tsx) to reduce feedback loop during devlopment.
- Use `esbuild` to bundle production code.
- Use `json-schema-to-ts` to validate & type your route
- Auto-generated Swagger UI: `http://localhost:3000/documentation` (production ready)

---

Expand Down
28 changes: 26 additions & 2 deletions esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import glob from 'tiny-glob'
import path from 'node:path'
import { cp } from 'node:fs/promises'
import { build } from 'esbuild'
import type { Plugin } from 'esbuild'
import esbuildPluginPino from 'esbuild-plugin-pino'
import glob from 'tiny-glob'

/** esbuild plugin to copy static folder to outdir */
function esbuildPluginFastifySwaggerUi(): Plugin {
return {
name: '@fastify/swagger-ui',
setup(build) {
const { outdir } = build.initialOptions
const fastifySwaggerUi = path.dirname(
require.resolve('@fastify/swagger-ui')
)
const source = path.join(fastifySwaggerUi, 'static')
const dest = path.join(outdir, 'static')

build.onEnd(async () => cp(source, dest, { recursive: true }))
}
}
}

;(async function () {
// Get all ts files
const entryPoints = await glob('src/**/*.ts')
Expand All @@ -14,6 +35,9 @@ import esbuildPluginPino from 'esbuild-plugin-pino'
platform: 'node',
format: 'cjs',
sourcemap: true,
plugins: [esbuildPluginPino({ transports: ['pino-pretty'] })]
plugins: [
esbuildPluginPino({ transports: ['pino-pretty'] }),
esbuildPluginFastifySwaggerUi()
]
})
})()
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
"license": "ISC",
"devDependencies": {
"@types/node": "18.11.17",
"@typescript-eslint/eslint-plugin": "5.46.1",
"@typescript-eslint/parser": "5.46.1",
"esbuild": "0.16.9",
"esbuild-plugin-pino": "1.2.7",
"@typescript-eslint/eslint-plugin": "5.47.0",
"@typescript-eslint/parser": "5.47.0",
"esbuild": "0.16.10",
"esbuild-plugin-pino": "1.2.8",
"eslint": "8.30.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.2.1",
"json-schema-to-ts": "2.6.2",
"lint-staged": "13.1.0",
"pino": "8.8.0",
"pino-pretty": "9.1.1",
"prettier": "2.8.1",
Expand All @@ -38,6 +37,7 @@
"@fastify/autoload": "5.6.0",
"@fastify/sensible": "5.2.0",
"@fastify/swagger": "8.2.1",
"@fastify/swagger-ui": "^1.3.0",
"fastify": "4.10.2",
"fastify-plugin": "4.4.0"
}
Expand Down
Loading

0 comments on commit 10ea46d

Please sign in to comment.