Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kiwilan/fastify-utils

Collection of utilities for fastify framework, built to improve DX.
https://github.com/kiwilan/fastify-utils

fastify typescript utils

Last synced: 19 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# Fastify utils






Social oEmbed



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


NPM
Downloads
License



Fastify
TypeScript
Node.js



tests
codecov

## Installation

```bash
npm add @kiwilan/fastify-utils tsx
```

Or with [pnpm](https://pnpm.js.org/):

```bash
pnpm add @kiwilan/fastify-utils tsx
```

## Usage

```bash
touch setup.js
touch .eslintrc
touch .env.example
```

```bash
cp .env.example .env
```

### Dev setup

In `.env`:

```bash
LOG_LEVEL=debug # debug | error | fatal | info | trace | warn | silent

PORT=3000
BASE_URL=localhost
HTTPS=false

CLUSTER=false
```

In `setup.js`:

```javascript
import { Compiler } from "fastify-utils";

Compiler.make({
// options
});
```

In `package.json`:

```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`:

```typescript
import { Server } from "fastify-utils";

Server.run({
// options
});
```

#### Routes

In `src/routes/index.ts`:

```typescript
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`:

```typescript
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`

```bash
# Could be left empty if you don't want to use it
API_KEY=
```

### Build setup

In `package.json`:

```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`:

```bash
LOG_LEVEL=error # debug | error | fatal | info | trace | warn | silent

PORT=3000 # pm2 port
BASE_URL=domain.com
HTTPS=true
```

In `package.json`:

```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

```bash
pnpm package
```

```json
{
"dependencies": {
"@kiwilan/fastify-utils": "file:~/kiwilan-fastify-utils.tgz"
}
}
```

## License

[MIT](LICENSE)