# My Fastify Server

A configurable Fastify server creation utility, providing common plugins out of the box (such as form body parsing, CORS, and Helmet). It also supports an optional `typeProvider` for enhanced TypeScript type checking in routes. Ideal for high-concurrency environments.

## Features

1. **Quick Start**  
   Create a Fastify instance with a single function call: `createFastifyServer(...)`.
2. **Common Plugins**  
   Integrates [@fastify/formbody], [@fastify/cors], and [@fastify/helmet] by default, reducing repetitive setup.
3. **Optional Type Providers**  
   Supports `withTypeProvider()` for seamless schema-to-type inference in TypeScript (e.g., using `@fastify/type-provider-typebox`).
4. **High Concurrency**  
   Fastify itself is designed for high-performance, I/O-intensive scenarios. Paired with environments like Node 18+ or Firebase Functions (Gen2), it can handle multiple concurrent requests efficiently.
5. **TypeScript-First**  
   Written in TypeScript and publishes `.d.ts` files for comprehensive type definitions.

---

## Installation

```bash
npm install my-fastify-server
# or
pnpm add my-fastify-server
# or
yarn add my-fastify-server

```

## Quick Usage

```ts

import { createFastifyServer } from 'my-fastify-server';

async function main() {
  // 1. Create the Fastify instance
  const app = createFastifyServer({
    logger: {
      level: 'info',
      formatters: { level: (label) => ({ level: label }) },
    },
  });

  // 2. Register a sample route
  app.get('/ping', async (request, reply) => {
    return { pong: true };
  });

  // 3. Start the server
  try {
    await app.listen({ port: 3000 });
    console.log('Server started at http://localhost:3000');
  } catch (err) {
    app.log.error(err);
    process.exit(1);
  }
}

main();
```

When you open http://localhost:3000/ping, you should see { "pong": true }.

---

## Using a Custom Type Provider


To leverage @fastify/type-provider-typebox or other type providers for enhanced schema-based type inference:

```ts
import { createFastifyServer } from 'my-fastify-server';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';

async function main() {
  const app = createFastifyServer({
    logger: {
      level: 'info',
      formatters: { level: (label) => ({ level: label }) },
    },
    // Pass your chosen type provider here
    typeProvider: TypeBoxTypeProvider(),
  });

  // Using withTypeProvider to define strongly-typed routes
  const typedApp = app.withTypeProvider<TypeBoxTypeProvider>();

  typedApp.route({
    method: 'POST',
    url: '/echo',
    schema: {
      body: {
        type: 'object',
        properties: {
          msg: { type: 'string' },
        },
        required: ['msg'],
      },
      response: {
        200: {
          type: 'object',
          properties: {
            echo: { type: 'string' },
          },
        },
      },
    },
    handler: async (request, reply) => {
      return { echo: request.body.msg };
    },
  });

  await app.listen({ port: 3000 });
  console.log('Server with custom type provider is running on http://localhost:3000');
}

main();
```