# express-openapi-typescript

> Utilities for adding types to express routes according to an OpenAPI specification.

Latest version **1.0.3** (published 2023-10-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install express-openapi-typescript
pnpm add express-openapi-typescript
yarn add express-openapi-typescript
bun add express-openapi-typescript
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2023-10-05 |
| First published | 2023-10-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 59.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Zach Shaver |
| Maintainers | shaverz |

## Links

- npm: https://www.npmjs.com/package/express-openapi-typescript
- npm.io page: https://npm.io/package/express-openapi-typescript

## Recent versions

- 1.0.3 (latest) — 2023-10-05
- 1.0.2 — 2023-10-04
- 1.0.1 — 2023-10-04
- 1.0.0 — 2023-10-03

## README

# 👌 express-openapi-typescript

Utilities for adding types to express routes according to an OpenAPI specification.

## Install

```bash
npm install express-openapi-typescript openapi-typescript
```

## Usage

### Generate types

Use [`openapi-typescript`](https://www.npmjs.com/package/openapi-typescript) to generate the types file containing the `paths` interface:

```bash
npx openapi-typescript ./path/to/my/schema.yaml -o ./path/to/my/schema.d.ts
```

⚠️ Note: Only `"application/json"` content types are supported currently.

### Typed router

The recommended method is wrapping an express `Router` with an `OpenApiRouter`.
The `delete`, `get`, `patch`, `post`, and `put` methods will be typed according to the fully qualified (openapi) route that is passed in as a type parameter:

```typescript
import express, { json, Router } from "express";
import { OpenApiRouter } from "express-openapi-typescript";

import { paths } from "./path/to/my/schema.d.ts";

const app = express();

app.use(json());

const router = OpenApiRouter<paths>(Router());

// Route corresponding to 'POST /pet' path in openapi spec
router.post<"/pet">("/", (req, res) => {
  // req.body, req.query, req.params, and res.json() are fully typed
  res.json({
    name: req.body.name,
    photoUrls: req.body.photoUrls,
  });
});

// The original Router() instance is stored in `router` property
app.use("/pet", router.router);
```

Note that the express route is relative to the router, and so may not be the exact same as the router.

### Typed request handlers

The same functionality can be acheived without extending the router, which may be required in some use cases, by casting the request handler functions themselves:

```typescript
import express, { json, Router } from "express";
import {
  DeleteHandler,
  GetHandler,
  PatchHandler,
  PostHandler,
  PutHandler,
} from "express-openapi-typescript";

import { paths } from "./path/to/my/schema.d.ts";

const app = express();

app.use(json());

const router = Router();

router.post("/", ((req, res) => {
  // req.body, req.query, req.params, and res.json() are fully typed
  res.json({
    name: req.body.name,
    photoUrls: req.body.photoUrls,
  });
  // Route corresponding to 'POST /pet' path in openapi spec
}) as PostHandler<paths, "/pet">);

app.use("/pet", router);
```

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