7.4.7 • Published 1 month ago

@smartlyio/oats-koa-adapter v7.4.7

Weekly downloads
41
License
MIT
Repository
github
Last release
1 month ago

Oats Koa Adapter

Oats Koa Adapter is a library that binds server endpoints written with Oats to Koa's Router object.

What is Oats?

Oats is a library that parses OpenAPI specifications and generates client and server code in TypeScript.

Koa is a web framework for node.js applications.

Installation

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

npm install 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

// 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 oatsRouter = koaAdapter.bind(oaserver.router, spec, requestContextCreator);

  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.

// 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.router 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.

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

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

// continue Koa setup...
// 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())
}
7.4.8-alpha.3

1 month ago

7.4.8-alpha.1

1 month ago

7.4.8-alpha.0

1 month ago

7.4.7

1 month ago

7.4.6

1 month ago

7.4.5

2 months ago

7.4.4

2 months ago

7.4.4-alpha.2

2 months ago

7.4.1-alpha.2

5 months ago

7.4.1

5 months ago

7.3.1-alpha.7

10 months ago

7.3.3-alpha.0

9 months ago

7.3.3-alpha.1

9 months ago

7.3.3-alpha.6

9 months ago

7.4.0

9 months ago

7.3.2

10 months ago

7.1.0

1 year ago

7.0.1-next.1

1 year ago

6.3.3-alpha.1

1 year ago

7.0.0

1 year ago

7.0.1-alpha.1

1 year ago

7.2.0

12 months ago

6.1.0

1 year ago

6.3.0

1 year ago

6.1.2

1 year ago

6.3.2

1 year ago

6.3.1

1 year ago

6.1.3

1 year ago

6.0.3

1 year ago

6.2.0

1 year ago

6.1.4-alpha.3

1 year ago

6.0.2

1 year ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.0-alpha.12

2 years ago

6.0.0-alpha.13

2 years ago

5.1.5

2 years ago

5.1.4

2 years ago

5.1.3

2 years ago

5.1.2

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

5.1.4-alpha.0

2 years ago

4.4.1-alpha.19

2 years ago

4.4.1-alpha.14

2 years ago

4.4.1-alpha.13

2 years ago

4.4.1-alpha.18

2 years ago

4.4.1-alpha.9

2 years ago

4.4.0

2 years ago

4.3.2

2 years ago

4.3.1

2 years ago

4.3.0

2 years ago

4.2.0

2 years ago

4.1.1-alpha.14

2 years ago

4.1.0

2 years ago

4.1.0-alpha.0

2 years ago

4.0.0-alpha.2

2 years ago

3.4.0

2 years ago

3.5.0

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

3.5.0-alpha.6

2 years ago

3.3.1-alpha.24

3 years ago

3.3.1

3 years ago

3.1.6-alpha.15

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.3.0-alpha.1

3 years ago

3.1.5

3 years ago

3.2.0-alpha.33

3 years ago

3.2.0-alpha.15

3 years ago

3.1.4-alpha.14

3 years ago

3.1.4

3 years ago

3.1.4-alpha.1

3 years ago

3.1.3

3 years ago

3.1.1

3 years ago

3.0.0

3 years ago

3.0.0-alpha.18

3 years ago

3.0.0-alpha.17

3 years ago

2.18.3-alpha.10

3 years ago

2.18.3-alpha.17

3 years ago

2.18.2

3 years ago

2.18.0

3 years ago

2.17.1-alpha.926

3 years ago

2.17.1-alpha.917

3 years ago

2.17.1-alpha.918

3 years ago

2.3.1

3 years ago

2.3.1-beta.1

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.2

3 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago