# rototill

> A route building utility for Express to help with validation, typings, and OpenAPI specs for REST APIs

Latest version **0.7.0** (published 2023-10-09) · ISC license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2023-10-09 |
| First published | 2022-12-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 42.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | drewpom |

## Links

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

## Recent versions

- 0.7.0 (latest) — 2023-10-09
- 0.6.3 — 2022-12-22
- 0.6.2 — 2022-12-21
- 0.6.1 — 2022-12-21
- 0.6.0 — 2022-12-21
- 0.5.0 — 2022-12-20
- 0.4.2 — 2022-12-20
- 0.4.1 — 2022-12-20
- 0.4.0 — 2022-12-20
- 0.3.0 — 2022-12-20
- 0.2.1 — 2022-12-19
- 0.2.0 — 2022-12-19
- 0.1.1 — 2022-12-17
- 0.1.0 — 2022-12-17

## README

# Rototill
A route building utility for Express to help with validation, typings, and OpenAPI specs for REST APIs

## Motivation
I wanted a light-weight way to have type-checked Express routes. [tsoa](https://github.com/lukeautry/tsoa) is a great library that achieves the same goal, however, the use of code generation added an extra build and development step that I wanted to avoid.

## Usage
Rototill exports a single `Rototill` class that can be used to build any number of routes. You can create multiple instances of `Rototill` for different sections of your API or you can use one single instance.

Example:
```
// A route builder
import {Rototill, HTTPMethod} from '../src/index.js';
import { JSONSchemaType } from 'ajv';

export const productsApi = new Rototill();

const productIdSchema: JSONSchemaType<{
  id: string,
}> = {
  type: 'object',
  required: ['id'],
  properties: {
    id: {
      type: 'string',
    },
  },
}

productsApi
  .createRoute(HTTPMethod.Post, '/:id', builder => 
    builder
      .params(productIdSchema)
      .body<{
        name: string,
      }>({
        type: 'object',
        required: ['name'],
        properties: {
          name: {
            type: 'string',
          },
        },
      })
      .handler(({ params, body }) => {
        return {
          id: params.id,
          name: body.name,
        };
      })
  )

// Main app entry point
const app = express()
const port = 3000

app.use(bodyParser.json());

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.use('/products', productsApi.routes());
```

### Dependency Injection
Context for all routes can be injected when building the Express router. This is useful for injecting services during server creation. The value injected to the `routes` method will be available in the route handler and all middleware as `{ context: "your value" }`

```

export type ServerContext = {
  productLoader(productId: string): Promise<Product | null>,
};

// A route builder
import {Rototill, HTTPMethod} from '../src/index.js';
import { JSONSchemaType } from 'ajv';

export const productsApi = new Rototill<ServerContext>();

const productIdSchema: JSONSchemaType<{
  id: string,
}> = {
  type: 'object',
  required: ['id'],
  properties: {
    id: {
      type: 'string',
    },
  },
}

productsApi
  .createRoute(HTTPMethod.Post, '/:id', builder => 
    builder
      .params(productIdSchema)
      .body<{
        name: string,
      }>({
        type: 'object',
        required: ['name'],
        properties: {
          name: {
            type: 'string',
          },
        },
      })
      .handler(async ({ params, body, context }) => {
        const product = await context.productLoader.fetchProduct(params.id);

        return {
          id: params.id,
          name: body.name,
        };
      })
  )

// Main app entry point
const app = express()
const port = 3000

app.use(bodyParser.json());

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.use('/products', productsApi.routes({
  async productLoader(productId) {
    return fetchProduct(productId);
  },
}));

```

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