npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

swagger-codegen-typescript-koa2

v0.1.4

Published

Generate TypeScript Koa2 server skeleton codes from swagger spec.

Readme

swagger-codegen-typescript-koa2

Generate TypeScript Koa2 server skeleton codes from swagger spec.

usage

$ node swagger-codegen-typescript-koa2 <input.swagger.yaml> <output.ts>

and output.ts requires [email protected] outputs.

$ npx [email protected] -n "" -o swagger.d.ts <input.swagger.yaml>

example

input swagger file

paths:
  '/users/{user_id}':
    get:
      summary: get user
      operationId: get_user
      description: |
        get user
      parameters:
        - in: path
          required: true
          name: user_id
          type: integer
          description: user_id
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/User'
        default:
          description: ERR
          schema:
            $ref: '#/definitions/ErrorResponse'
definitions:
  User:
    type: object
    properties:
      user_id:
        type: integer
      name:
        type: string
  ErrorResponse:
    type: object
    required:
      - code
      - name
      - message
    properties:
      code:
        type: integer
      name:
        type: string
      message:
        type: string

output .ts file

// ---- GET /users/{user_id} -------------------
export namespace get_user {
  export type Request = {
    user_id: number
  }
  export function make_request(ctx: KoaRouter.IRouterContext): Request {
    return {
      user_id: sctk.decode_string_integer_bignumber_external(
        ctx.params.user_id,
        {
          in: 'path',
          required: true,
          name: 'user_id',
          type: 'integer',
          description: 'user_id'
        }
      )
    }
  }
  export type Response200 = d.User
  export type ResponseDefault = d.ErrorResponse
  export type Response = {
    status: number
    body: Response200 | ResponseDefault
  }
  export interface Handler {
    (req: Request): Promise<Response>
  }
  export function route(router: Router) {
    router.get('/simple-v1/users/{user_id}', async ctx_ => {
      ...
    });
  }
}

export interface Routes {
  get_user?: get_user.Handler
  post_users?: post_users.Handler
}
export class Router extends KoaRouter {
  swagger: Routes = {}
}

export function setup(
  app: Koa,
  swagger_filepath: string,
  routes_dirpath: string
): Router {
  ...
}

server code

You would implements the API such as:

import Server from './server';
import * as api_simple from 'swagger-generated/simple'; //tsconfig.paths

const get_user:api_simple.get_user.Handler = async (req) => {
  console.log('get_user');
  let res200: api_simple.get_user.Response200|undefined = undefined;
  let resDef: api_simple.get_user.ResponseDefault|undefined = undefined;

  if (req.user_id == 20070831) {
    res200 = { user_id: req.user_id, name: 'Hatsune Miku' };
    return { status: 200, body: res200 };
  } else if (req.user_id == 20071227) {
    res200 = { user_id: req.user_id, name: 'Kagamine Rin' };
    return { status: 200, body: res200 };
  } else {
    resDef = { code: 404, name: 'Not Found', message: `unknown user: ${req.user_id}` };
    return { status: 404, body: resDef };
  }
};

And setup and run app:

const server = new Server();
const router = api_simple.setup(server.app, './swagger/dist/simple/swagger.yaml', '');

router.swagger.get_user = get_user;
server.start();

call API

returns implemented result:

$ curl 'http://localhost:10080/simple-v1/users/20071227'
{"user_id":20071227,"name":"Kagamine Rin"}

$ curl 'http://localhost:10080/simple-v1/users/1'
{"code":404,"name":"Not Found","message":"unknown user: 1"}

type validation failed:

$ curl 'http://localhost:10080/simple-v1/users/string'
{"code":"SWAGGER_REQUEST_VALIDATION_FAILED","errors":[{"actual":"string","expected":{"type":"integer"},"where":"path"}]}

more...

If you see package.json or other settings, please check https://github.com/dai1975/swagger-codegen-typescript-koa2/tree/master/examle directory.