@kiwilan/fastify-utils
v0.2.21
Published
Collection of utilities for fastify framework, built to improve DX.
Downloads
27
Maintainers
Readme
Fastify utils
Installation
npm add @kiwilan/fastify-utils tsx
Or with pnpm:
pnpm add @kiwilan/fastify-utils tsx
Usage
touch setup.js
touch .eslintrc
touch .env.example
cp .env.example .env
Dev setup
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
});
Routes
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: [] };
},
});
API key
In .env
# Could be left empty if you don't want to use it
API_KEY=
Build setup
In package.json
:
{
"scripts": {
"build": "rimraf build && npm run config && NODE_ENV=production tsx setup.js && npm run check:types",
"check:types": "tsc --noEmit"
}
}
Production setup
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'"
}
}
Build
pnpm package
{
"dependencies": {
"@kiwilan/fastify-utils": "file:~/kiwilan-fastify-utils.tgz"
}
}