# fastify-file-routes

> A Fastify plugin that provides a file system routes, based on the way Next.JS file system routing works, including all possible features.

Latest version **1.1.2** (published 2022-03-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-file-routes
pnpm add fastify-file-routes
yarn add fastify-file-routes
bun add fastify-file-routes
```

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2022-03-10 |
| First published | 2021-12-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 25.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 37 |
| Author | spa5k |
| Maintainers | unbuttun-spark |
| Keywords | cjs, esm, fastify, fastify-plugin, file-routes, next, nextjs, routes, typescript |

## Links

- npm: https://www.npmjs.com/package/fastify-file-routes
- Repository: https://github.com/spa5k/fastify-file-routes
- Homepage: https://github.com/spa5k/fastify-file-routes#readme
- Issues: https://github.com/spa5k/fastify-file-routes/issues
- npm.io page: https://npm.io/package/fastify-file-routes

## Dependencies (2)

- [picocolors](https://npm.io/package/picocolors.md) 1.0.0
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) 3.0.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2022-03-10
- 1.1.1 — 2021-12-21
- 1.1.0 — 2021-12-21
- 1.0.3 — 2021-12-21
- 1.0.2 — 2021-12-21
- 1.0.1 — 2021-12-21
- 1.0.0 — 2021-12-21

## README

# Fastify File Routes

<div align='center'>

![Banner](./banner.png)

[![NPM downloads](https://img.shields.io/npm/dm/fastify-file-routes.svg?style=for-the-badge)](https://www.npmjs.com/package/fastify-file-routes)
[![npm](https://img.shields.io/npm/v/fastify-file-routes?logo=npm&style=for-the-badge)](https://www.npmjs.com/package/fastify-file-routes)
![node-current](https://img.shields.io/badge/Node-%3E=14-success?style=for-the-badge&logo=node)

A Fastify plugin that provides a file system routes, based on the way Next.JS file system routing works, including all possible features.

</div>

## :sparkles: Features

    1. File System Routing.
    2. Index Routes.
    3. Nested Routes.
    4. Dynamic Route Segments.
    5. Catch All (Wildcard \*) Routes.
    6. Multiple parameters. eg /users/:id-:name

## :rocket: Installation

```sh
npm install fastify-file-routes
```

```yarn
yarn add fastify-file-routes
```

## :blue_book: Usage/Examples

### 1. Register the Plugin.

```typescript
import { fileRoutes } from "fastify-file-routes";

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

await app.register(fileRoutes, {
  routesDir: "./routes",
  prefix, // -> optional
});

await app.listen(3000);
```

### 2. Create the routes directory.

```sh
mkdir routes
```

### 3. Create your first route in the routes directory

```typescript
//file: `routes/some/route.ts`
//url:  `http://localhost/some/route`

import type { Route } from "fastify-file-routes";

export const routes: Route = {
  get: {
    handler: async (request, reply) => {
      await reply.send({
        some: "route",
      });
    },
  },
};
```

### 4. Access the Parameters.

```typescript
//file: `routes/users/[userId]/settings.js`
//mapped to: `http://localhost/users/:userId/settings`

export const routes: Route = {
  get: {
    handler: async (request, reply) => {
      const { params } = request;
      await reply.send(`photos of user ${params.userId}`);
    },
  },
};
```

### 5. Wildcard (\*) routes.

```typescript
//file: `routes/profile/[...id].ts  `
//mapped to: `http://localhost/profile/*`

export const routes: Route = {
  get: {
    handler: async (request, reply) => {
      const { params } = request;
      await reply.send(`wildcard route`);
    },
  },
};
```

### 6. Post Request..

```typescript
export const routes: Route = {
  post: {
    handler: async (_request, reply) => {
      await reply.send({
        post: "post user",
      });
    },
  },
};
```

### 7. Prefix Route

```typescript
//file: `routes/some/route.ts`
//url:  `http://localhost/api/some/route`

await app.register(fileRoutes, {
  routesDir: "./routes",
  prefix: "/api",
});

export const routes: Route = {
  post: {
    handler: async (_request, reply) => {
      await reply.send({
        post: "post user",
      });
    },
  },
};
```

### 8. Multiple Parameters

```typescript
//file: `routes/some/[param1]-[param2].ts`
//url:  `http://localhost/some/:param1-:param2`

await app.register(fileRoutes, {
  routesDir: "./routes",
});

export const routes: Route = {
  post: {
    handler: async (_request, reply) => {
      await reply.send({
        post: "multiple params",
      });
    },
  },
};
```

## :information_source: Info

1. Check the examples folder in /examples to see how to use the plugin.
2. route.prefixTrailingSlash has been set to 'both'.

## :arrow_forward: Route module definition

Method specification for attributes is available here: [Method specification](https://www.fastify.io/docs/latest/Routes/#full-declaration)

> :information_source: attributes `url` and `method` are dynamically provided

Allowed attributes mapped to Http methods in module:

- delete
- get
- head
- patch
- post
- put
- options

## :arrow_forward: Skipping files

to skip file in routes directory, prepend the `.` or `_` character to filename

examples:

```text
routes
├── .ignored-directory
├── _ignored-directory
├── .ignored-js-file.js
├── _ignored-js-file.js
├── .ignored-ts-file.ts
├── _ignored-ts-file.ts
├── ignored-js-test.test.js
└── ignored-ts-test.test.ts
```

> :warning: also any `*.test.js` and `*.test.ts` are skipped!

this is useful if you want to have a lib file which contains functions that don't have to be a route, so just create the file with `_` prepending character

## TODO

1. Adding support for optional wildcard routes - [[...id]].
2. More tests.
3. Better typescript stuff for validation and inferences in routes.

## Visualization of this Repo.

![Visualization of this repo](./diagram.svg)

## License

[MIT](https://choosealicense.com/licenses/mit/)

## Related/Acknowledgements

[Fastify - AutoRoutes](https://github.com/GiovanniCardamone/fastify-autoroutes) - Lots of code has been used from this project.

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