Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions packages/config/FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,16 @@

6. Registers an `onRequest` hook that sets `request.config` to the same `ApiConfig` object on every incoming request.

## Utility Functions

7. Exports a `parse` utility for converting raw `string | undefined` env var values to typed values using a fallback to infer the target type:
- Returns `fallback` when `value` is `undefined`.
- Returns a `boolean` (via `!!JSON.parse(value)`) when `fallback` is a `boolean`.
- Returns a `number` (via `JSON.parse(value)`) when `fallback` is a `number`.
- Returns the raw `string` otherwise.

```typescript
parse(process.env.PORT, 3000); // → number
parse(process.env.DEBUG, false); // → boolean
parse(process.env.APP_NAME, "my-app"); // → string
parse(undefined, 3000); // → 3000 (fallback)
```

## Type Exports & Module Augmentation

8. Exports `ApiConfig` type — the shape of the full application configuration object.
9. Exports `AppConfig` type — the shape of an individual app entry within `ApiConfig.apps`.
10. Module-augments `FastifyInstance` to add `config: ApiConfig` and `hostname: string`.
11. Module-augments `FastifyRequest` to add `config: ApiConfig`.
7. Exports `ApiConfig` type — the shape of the full application configuration object.
8. Exports `AppConfig` type — the shape of an individual app entry within `ApiConfig.apps`.
9. Module-augments `FastifyInstance` to add `config: ApiConfig` and `hostname: string`.
10. Module-augments `FastifyRequest` to add `config: ApiConfig`.

## ApiConfig Shape

12. `ApiConfig` top-level fields:
11. `ApiConfig` top-level fields:
- `appName: string`
- `appOrigin: string[]`
- `apps?: AppConfig[]`
Expand All @@ -53,7 +38,7 @@
- `rest: { enabled: boolean }`
- `version: string`

13. `ApiConfig.logger` sub-object:
12. `ApiConfig.logger` sub-object:
- `level: Level` (required)
- `base?: LoggerOptions["base"]`
- `formatters?: LoggerOptions["formatters"]`
Expand Down
39 changes: 0 additions & 39 deletions packages/config/GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ We wrap this library with a different surface:
- `fastify.hostname` decorator derived from `baseUrl` and `port`
- `request.config` population via `onRequest`
- Type exports and Fastify module augmentation
- `parse` utility for typed env parsing

---

Expand Down Expand Up @@ -125,20 +124,6 @@ fastify.get("/me", async (request) => {
});
```

### `parse` — typed env var parser

The exported `parse` utility converts raw environment variables to their intended types. Pass a fallback value; its type determines how the string is coerced.

```typescript
import { parse } from "@prefabs.tech/fastify-config";

const config: ApiConfig = {
port: parse(process.env.PORT, 3000) as number,
env: parse(process.env.NODE_ENV, "development") as string,
// ...
};
```

Rules:

- `value === undefined` → returns `fallback`
Expand Down Expand Up @@ -200,30 +185,6 @@ The plugin ships with module augmentation for Fastify's types. No extra setup is

## Use Cases

### Building config from environment variables

Use `parse` to construct a fully typed `ApiConfig` from `process.env`:

```typescript
import { parse } from "@prefabs.tech/fastify-config";
import type { ApiConfig } from "@prefabs.tech/fastify-config";

const config: ApiConfig = {
appName: parse(process.env.APP_NAME, "my-api") as string,
appOrigin: (process.env.APP_ORIGIN ?? "http://localhost:3000").split(","),
baseUrl: parse(process.env.BASE_URL, "http://localhost") as string,
env: parse(process.env.NODE_ENV, "development") as string,
logger: {
level: parse(process.env.LOG_LEVEL, "info") as string as Level,
},
name: "my-api",
port: parse(process.env.PORT, 3000) as number,
protocol: parse(process.env.PROTOCOL, "http") as string,
rest: { enabled: parse(process.env.REST_ENABLED, true) as boolean },
version: parse(process.env.APP_VERSION, "0.0.0") as string,
};
```

### Accessing config in a route handler

Config is available on both the instance and the request object, so you can use whichever is in scope:
Expand Down
3 changes: 1 addition & 2 deletions packages/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ In a complex API or monorepo with multiple Fastify plugins and services, maintai
- **`fastify.config`** — decorates the Fastify instance with your `ApiConfig` object, accessible everywhere on the instance
- **`request.config`** — decorates every incoming request with the same config reference via an `onRequest` hook (useful for mercurius `buildContext`, route handlers, etc.)
- **`fastify.hostname`** — computed `${baseUrl}:${port}` string, derived from your config
- **`parse(value, fallback)`** — type-coercing env var parser: returns a boolean, number, or string based on the fallback type; returns the fallback when the value is `undefined`
- **`ApiConfig` type** — strongly typed interface covering app identity, origins, logging (pino), pagination, REST feature flag, and multi-tenant app list
- **`AppConfig` type** — per-app shape for multi-tenant configurations (`id`, `name`, `origin`, `supportedRoles`)

Expand All @@ -38,8 +37,8 @@ No sibling `@prefabs.tech` plugins need to be registered before this one.

```typescript
// config.ts
import { parse } from "@prefabs.tech/fastify-config";
import type { ApiConfig } from "@prefabs.tech/fastify-config";
import { parse } from "@prefabs.tech/utilities";

const config: ApiConfig = {
appName: process.env.APP_NAME as string,
Expand Down
4 changes: 2 additions & 2 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"typecheck": "tsc --noEmit -p tsconfig.json --composite false"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@types/node": "24.10.15",
"@vitest/coverage-istanbul": "3.2.4",
"eslint": "9.39.4",
Expand Down
67 changes: 0 additions & 67 deletions packages/config/src/__test__/parse.test.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ declare module "fastify" {
}
}

export { default as parse } from "./parse";

export { default } from "./plugin";

export type { ApiConfig, AppConfig } from "./types";
24 changes: 0 additions & 24 deletions packages/config/src/parse.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/error-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"stacktracey": "2.2.0"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@types/node": "24.10.15",
"@types/stack-trace": "0.0.33",
"@vitest/coverage-istanbul": "3.2.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/firebase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
"zod": "3.25.76"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/fastify-config": "0.94.1",
"@prefabs.tech/fastify-error-handler": "0.94.1",
"@prefabs.tech/fastify-graphql": "0.94.1",
"@prefabs.tech/fastify-slonik": "0.94.1",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@types/node": "24.10.15",
"@vitest/coverage-istanbul": "3.2.4",
"eslint": "9.39.4",
Expand Down
1 change: 0 additions & 1 deletion packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export { default as schema } from "./schema";
Add a `graphql` block to your config in `config/index.ts`:

```typescript
import { parse } from "@prefabs.tech/fastify-config";
import dotenv from "dotenv";

import { resolvers, schema } from "../src/graphql";
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"graphql-tag": "2.12.6"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/fastify-config": "0.94.1",
"@prefabs.tech/fastify-slonik": "0.94.1",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@types/node": "24.10.15",
"@vitest/coverage-istanbul": "3.2.4",
"eslint": "9.39.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/mailer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"nodemailer-mjml": "1.6.0"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/fastify-config": "0.94.1",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@types/mjml": "4.7.4",
"@types/node": "24.10.15",
"@types/nodemailer": "6.4.23",
Expand Down
4 changes: 2 additions & 2 deletions packages/s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
"uuid": "9.0.1"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/fastify-config": "0.94.1",
"@prefabs.tech/fastify-error-handler": "0.94.1",
"@prefabs.tech/fastify-graphql": "0.94.1",
"@prefabs.tech/fastify-slonik": "0.94.1",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@types/node": "24.10.15",
"@vitest/coverage-istanbul": "3.2.4",
"eslint": "9.39.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/slonik/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pnpm add --filter "@scope/project" @prefabs.tech/fastify-config @prefabs.tech/fa
Add a `slonik` block to your config:

```typescript
import { parse } from "@prefabs.tech/fastify-config";
import { parse } from "@prefabs.tech/utilities";
import dotenv from "dotenv";

import type { ApiConfig } from "@prefabs.tech/fastify-config";
Expand Down
4 changes: 2 additions & 2 deletions packages/slonik/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"slonik-interceptor-query-logging": "46.8.0"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/fastify-config": "0.94.1",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@slonik/driver": "46.8.0",
"@types/humps": "2.0.6",
"@types/node": "24.10.15",
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"@fastify/swagger-ui": "5.2.6"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@types/node": "24.10.15",
"@vitest/coverage-istanbul": "3.2.4",
"eslint": "9.39.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/user/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
"validator": "13.15.35"
},
"devDependencies": {
"@prefabs.tech/eslint-config": "0.7.0",
"@prefabs.tech/eslint-config": "0.8.0",
"@prefabs.tech/fastify-config": "0.94.1",
"@prefabs.tech/fastify-error-handler": "0.94.1",
"@prefabs.tech/fastify-graphql": "0.94.1",
"@prefabs.tech/fastify-mailer": "0.94.1",
"@prefabs.tech/fastify-s3": "0.94.1",
"@prefabs.tech/fastify-slonik": "0.94.1",
"@prefabs.tech/tsconfig": "0.7.0",
"@prefabs.tech/tsconfig": "0.8.0",
"@types/humps": "2.0.6",
"@types/node": "24.10.15",
"@types/validator": "13.15.10",
Expand Down
Loading
Loading