Skip to content

Collection of utilities for fastify framework, built to improve DX.

License

Notifications You must be signed in to change notification settings

kiwilan/fastify-utils

Repository files navigation

Fastify utils

Social oEmbed

Collection of utilities for fastify framework, built to improve DX.

NPM Downloads License

Fastify TypeScript Node.js

tests codecov

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"
  }
}

License

MIT

About

Collection of utilities for fastify framework, built to improve DX.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project