# convfastify

> convfastify is a plugin for the Fastify framework that provides a conventional way of declaring routes

Latest version **2.0.0** (published 2024-09-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install convfastify
pnpm add convfastify
yarn add convfastify
bun add convfastify
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2024-09-06 |
| First published | 2023-11-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 19.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | DemonHa |
| Maintainers | demonha |

## Links

- npm: https://www.npmjs.com/package/convfastify
- Repository: https://github.com/DemonHa/convfastify
- Homepage: https://github.com/DemonHa/convfastify#readme
- Issues: https://github.com/DemonHa/convfastify/issues
- npm.io page: https://npm.io/package/convfastify

## Dependencies (6)

- [glob](https://npm.io/package/glob.md) ^10.3.10
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^4.5.1
- [@fastify/swagger](https://npm.io/package/@fastify/swagger.md) ^8.11.0
- [json-schema-to-ts](https://npm.io/package/json-schema-to-ts.md) ^2.9.2
- [@fastify/swagger-ui](https://npm.io/package/@fastify/swagger-ui.md) ^1.10.1
- [@fastify/type-provider-json-schema-to-ts](https://npm.io/package/@fastify/type-provider-json-schema-to-ts.md) ^2.2.2

## Recent versions

- 2.0.0 (latest) — 2024-09-06
- 1.1.0 — 2023-11-26
- 1.0.0 — 2023-11-03

## README

# convfastify

[![NPM Version](https://img.shields.io/npm/v/convfastify.svg?style=flat)]()
[![NPM License](https://img.shields.io/npm/l/all-contributors.svg?style=flat)](https://github.com/DemonHa/convfastify/blob/main/LICENSE)
[![npm downloads](https://img.shields.io/npm/dt/convfastify.svg)](https://www.npmjs.com/package/convfastify)
![test workflow](https://github.com/DemonHa/convfastify/actions/workflows/test.yml/badge.svg)

_convfastify_ is a plugin for the [Fastify](http://fastify.io/) framework that provides a conventional way of declaring routes.

Features:

- Autoload routes
- Typesafe route definition
- Preconfigured swagger

# Installation

For npm users:

```bash
npm i convfastify
```

For yarn users:

```bash
yarn add convfastify
```

# Quick Start

Register the plugin and load routes:

```js
const fastify = require("fastify");
const { default: convfastify } = require("convfastify");

const app = fastify({
  logger: true,
});

app.register(
  convfastify()
    .loadFrom(`${__dirname}/routes/**/*.js`)
    .serveSwagger()
    .register()
);

app.listen({
  port: 8080,
});
```

On the files under the `/routes` directory:

```js
const { route } = require("convfastify");

module.exports.default = route.define({
  method: "GET",
  url: "/",
  schema: {
    querystring: {
      type: "object",
      properties: {
        foo: { type: "number" },
        bar: { type: "string" },
      },
      required: ["foo", "bar"],
    },
    response: {
      200: {
        type: "object",
        properties: {
          message: { type: "string" },
        },
      },
    },
  },
  handler: (_, res) => {
    res.send({ message: "Hello World" });
  },
});
```

It will load the routes defined in the `routes` directory and serve swagger.

## ESM module example

Register the plugin and load routes:

```js
import fastify from "fastify";
import convfastify from "convfastify";

import { dirname } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));

const app = fastify({
  logger: true,
});

app.register(
  convfastify
    .default()
    .loadFrom(`${__dirname}/routes/**/*.js`)
    .serveSwagger()
    .register({ esm: true })
);

app.listen({
  port: 8080,
});
```

On the files under the `/routes` directory:

```js
import { route } from "convfastify";

export default route.define({
  method: "GET",
  url: "/",
  schema: {
    querystring: {
      type: "object",
      properties: {
        foo: { type: "number" },
        bar: { type: "string" },
      },
      required: ["foo", "bar"],
    },
    response: {
      200: {
        type: "object",
        properties: {
          message: { type: "string" },
        },
      },
    },
  },
  handler: (_, res) => {
    res.send({ message: "Hello World" });
  },
});
```

It will load the routes defined in the `routes` directory and serve swagger.

## Typescript example

Register the plugin and load routes:

```ts
import fastify from "fastify";
import convfastify from "convfastify";

const app = fastify({
  logger: true,
});

app.register(
  convfastify()
    // Load routes
    .loadFrom(`${__dirname}/routes/**/*.js`)
    // Serving swagger
    .serveSwagger()
    // Register the plugin
    .register()
);

app.listen({
  port: 8080,
});
```

On the files under the `/routes` directory:

```ts
import { route } from "convfastify";

export default route.define({
  method: "GET",
  url: "/",
  schema: {
    querystring: {
      type: "object",
      properties: {
        foo: { type: "number" },
        bar: { type: "string" },
      },
      required: ["foo", "bar"],
    },
    response: {
      200: {
        type: "object",
        properties: {
          message: { type: "string" },
        },
      },
    },
  },
  handler: (req, res) => {
    res.send({ message: "Hello World" });
  },
});
```

It will load the routes defined in the `routes` directory and serve swagger.

### Using different type provider

By default when you define routes, it is preconfigured to infer types using `json-schema-to-ts`, but you can use a different type provider.

Example:

```ts
import { route } from "convfastify";
import { TypeBoxTypeProvider, Type } from "@fastify/type-provider-typebox";

export default route.withType<TypeBoxTypeProvider>().define({
  url: "/",
  method: "GET",
  schema: {
    querystring: Type.Object({
      name: Type.String(),
    }),
    response: {
      200: Type.Object({
        name: Type.String(),
      }),
    },
  },
  handler: (req, res) => {
    res.send({ name: req.query.name });
  },
});
```

# V2 Migration Guide

This guide is intended to help with migration from convfastify v1 to v2.

## Braking changes

### `route` is an object

To support different type providers, `route` is not a function you can call directly but an object.

To migrate it with minimal effort:

- Create a file `src/route.ts`

```ts
import { route as convroute } from "convfastify";
import { TypeBoxTypeProvider } from "@fastify/type-provider-typebox";

// NOTE: in this example we are using `TypeBoxTypeProvider` but you can use any provider
export const route = convroute.withType<TypeBoxTypeProvider>().define;
```

Now you can import `route` from the `/src/route.ts` instead of `convfastify` directly on all of your routes

# API

### `convfastify().loadFrom(path)`

This method allows loading routes from a specified path or [glob pattern](https://github.com/isaacs/node-glob).

### `convfastify().serveSwagger(config)`

This method allows serving swagger for the loaded routes. It accepts configuration for [`swagger`](https://github.com/fastify/fastify-swagger#register-options) and [`swaggerUi`](https://github.com/fastify/fastify-swagger-ui#register-options).

### `convfastify().register(config)`

This method allows you to register the plugin to your fastify application.

It accepts configuration object for the plugin.

#### config

- `esm`: set it to true if you are using esm module resolution

---
_Source: https://npm.io/package/convfastify · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
