Collection of utilities for fastify framework, built to improve DX.
npm add @kiwilan/fastify-utils tsx
Or with pnpm:
pnpm add @kiwilan/fastify-utils tsx
touch setup.js
touch .eslintrc
touch .env.example
cp .env.example .env
In .env
:
LOG_LEVEL=debug # debug | error | fatal | info | trace | warn | silent
PORT=3000
BASE_URL=localhost
HTTPS=false
CLUSTER=false
In setup.js
:
import { Compiler } from "fastify-utils";
Compiler.make({
// options
});
In package.json
:
{
"scripts": {
"postinstall": "npm run config",
"config": "node setup.js",
"dev": "npm run config && NODE_ENV=development tsx watch src .env"
}
}
In src/index.ts
:
import { Server } from "fastify-utils";
Server.run({
// options
});
In src/routes/index.ts
:
import { Route } from "fastify-utils";
export default Route.make({
method: "GET",
url: "/",
handler: async (request, reply) => {
return { hello: "world" };
},
});
And for src/routes/api/posts/index.ts
:
import { Route } from "fastify-utils";
export default Route.make({
method: "GET",
url: "/api/posts", // autocomplete
handler: async (request, reply) => {
return { posts: [] };
},
});
In .env
# Could be left empty if you don't want to use it
API_KEY=
In package.json
:
{
"scripts": {
"build": "rimraf build && npm run config && NODE_ENV=production tsx setup.js && npm run check:types",
"check:types": "tsc --noEmit"
}
}
In .env
:
LOG_LEVEL=error # debug | error | fatal | info | trace | warn | silent
PORT=3000 # pm2 port
BASE_URL=domain.com
HTTPS=true
In package.json
:
{
"scripts": {
"clean": "rimraf build && rimraf node_modules && pnpm install",
"start": "node build/index.mjs",
"pm2": "pm2 start --name 'fastify-utils' './build/index.mjs'"
}
}
pnpm package
{
"dependencies": {
"@kiwilan/fastify-utils": "file:~/kiwilan-fastify-utils.tgz"
}
}