# @smartlyio/oats-koa-adapter

> Koa adapter for Oats

Latest version **7.9.4** (published 2026-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @smartlyio/oats-koa-adapter
pnpm add @smartlyio/oats-koa-adapter
yarn add @smartlyio/oats-koa-adapter
bun add @smartlyio/oats-koa-adapter
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 7.9.4 |
| Published | 2026-10-26 |
| First published | 2019-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 41 |
| Author | Smartly.io |
| Maintainers | smartlyio |
| Keywords | oats, koa, openapi3, rest, generator, typescript, server, client |

## Links

- npm: https://www.npmjs.com/package/@smartlyio/oats-koa-adapter
- Repository: https://github.com/smartlyio/oats
- npm.io page: https://npm.io/package/@smartlyio/oats-koa-adapter

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 7.9.4 (latest) — 2026-10-26
- 7.6.2-alpha.20 (canary) — 2025-12-14
- 2.3.1-beta.1 (beta) — 2021-04-29
- 7.9.3 — 2026-09-17
- 7.9.2 — 2026-09-17
- 7.9.1 — 2026-09-04
- 7.9.0 — 2026-09-04
- 7.8.0 — 2026-06-01
- 7.7.8 — 2026-04-01
- 7.7.7 — 2026-03-31
- 7.7.6 — 2026-03-11
- 7.6.3 — 2025-12-16
- 7.6.2-alpha.19 — 2025-12-12
- 7.6.1 — 2025-05-23
- 7.6.0 — 2025-01-09
- … 106 more at https://npm.io/package/@smartlyio/oats-koa-adapter/versions

## README

# Oats Koa Adapter

Oats Koa Adapter is a library that binds server endpoints written with [Oats](https://github.com/smartlyio/oats) to [Koa's](https://koajs.com/) `Router` object.

## What is Oats?

[Oats](https://github.com/smartlyio/oats) is a library that parses OpenAPI specifications and generates client and server code in TypeScript.

[Koa](https://koajs.com/) is a web framework for node.js applications.

## Installation

Use `npm` or `yarn` to install `oats-koa-adapter`.

```bash
npm install @smartlyio/oats-koa-adapter
```

## Usage

Oats Koa Adapter exports a single `bind` function. This function is generally used when setting up the router for your application.

### Basic Example

```ts
// router.ts

import * as koaAdapter from '@smartlyio/koa-oats-adapter';
import * as Router from 'koa-router'
import * as oaserver from '<Your Generated Server>';
import spec from '<Your Route Definitions>';

export const router = () => {
  const requestContextCreator = (ctx: any): any => ctx;

  const middlewares = [(_ctx, next) => { return next() }];
  
  const oatsRouter = koaAdapter.bind({ 
    handler: oaserver.createRouter(), 
    spec, 
    requestContextCreator,
    middlewares
  });
  
  return new Router().use(oatsRouter.routes())
};


```

### Defining Types for Request Context

There are two ways to add types to your context, each with their own benefits and drawbacks.

#### Using `any`

This approach uses the default behavior in the library, but uses TypeScript's `any` to force router to work in the bind function.

```ts
// router.ts

import * as koaAdapter from '@smartlyio/koa-oats-adapter';
import * as Router from 'koa-router'
import * as oaserver from '<Your Generated Server>';
import spec from '<Your Route Definitions>';

interface RequestContext {
  id: string
}

export const router = () => {
  const requestContextCreator = (ctx: any): RequestContext => ctx;

  const oatsRouter = koaAdapter.bind<
    oaserver.EndpointsWithContext<RequestContext>,
    RequestContext
  >(oaserver.createRouter() as any, spec, requestContextCreator);

  return new Router().use(oatsRouter.routes());
}
```

#### Configuring Generated Code

This approach injects itself into the default behavior of the generated code, using the `oats-runtime` directly and avoiding the use of `any`.

```ts
// app.ts
import * as Koa from 'koa'
import { Context, State } from './router'

const app = new Koa<State, Context<State>>()

// continue Koa setup...
```

```ts
// router.ts

import * as koaAdapter from '@smartlyio/koa-oats-adapter';
import * as oatsRuntime from "@smartlyio/oats-runtime";
import * as oaserver from '<Your Generated Server>';
import spec from '<Your Route Definitions>';

export interface State {
  id: string
}

export interface Context<S> {
  state: S
}

type Spec = oaserver.EndpointsWithContext<Context<State>>;

export const router = () => {
  const { createHandlerFactory } = oatsRuntime.server;
  const handler = createHandlerFactory<Spec>(oaserver.endpointHandlers);

  const copyKoaCtxToOatsCtx = (ctx: any): Context<State> => ({ ...ctx });
  const oatsRouter = koaAdapter.bind<Spec, Context<State>>(handler, spec, copyKoaCtxToOatsCtx);

  return new Router().use(oatsRouter.routes())
}
```

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