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 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-openapi

v12.1.3

Published

An unopinionated OpenAPI framework for koa

Downloads

170

Readme

koa-openapi NPM version Downloads Coveralls Status

An unopinionated OpenAPI framework for Koa.

Notice: This README is a work in progress and is based on express-openapi's README. Consult that in addition to this until this Notice is removed.

Getting Started

To see example projects, look at our test suite.

This getting started guide will use the most fundamental concepts of OpenAPI and koa-openapi.

  1. Create your API's main apiDoc. You can create it anywhere. For this example we'll create it in under an api-v1/ directory:

    // ./api-v1/api-doc.js
    
    const apiDoc = {
      swagger: '2.0',
      basePath: '/v1',
      info: {
        title: 'A getting started API.',
        version: '1.0.0'
      },
      definitions: {
        World: {
          type: 'object',
          properties: {
            name: {
              description: 'The name of this world.',
              type: 'string'
            }
          },
          required: ['name']
        }
      },
      paths: {}
    };
    
    export default apiDoc;

    You may be wondering why paths can be an empty object literal. We'll get to that in a second.

    This is all that is required for our API's main apiDoc. To see the full list of values and options for the main apiDoc you can view The Schema.

    Note: You can also use a YAML string instead of javascript objects e.g.

    # ./api-v1/api-doc.yml
    swagger: '2.0'
    basePath: '/v1'
    info:
      title: 'A getting started API.'
      version: '1.0.0'
    definitions:
      World:
        type: 'object'
        properties:
          name:
            description: 'The name of this world.'
            type: 'string'
        required:
          - 'name'
    paths: {}
  2. Create path handlers.

    Our paths object was empty in the main apiDoc because koa-openapi will generate it for us based on the location of our path handlers. For this example we'll place our path handlers under api-v1/paths/.

    Let's create a worlds path:

    // ./api-v1/paths/worlds.js
    export default function(worldsService) {
      let operations = {
        GET
      };
    
      function GET(ctx, next) {
        ctx.status = 200;
        ctx.body = worldsService.getWorlds(req.query.worldName);
      }
    
      // NOTE: We could also use a YAML string here.
      GET.apiDoc = {
        summary: 'Returns worlds by name.',
        operationId: 'getWorlds',
        parameters: [
          {
            in: 'query',
            name: 'worldName',
            required: true,
            type: 'string'
          }
        ],
        responses: {
          200: {
            description: 'A list of worlds that match the requested name.',
            schema: {
              type: 'array',
              items: {
                $ref: '#/definitions/World'
              }
            }
          },
          default: {
            description: 'An error occurred',
            schema: {
              additionalProperties: true
            }
          }
        }
      };
    
      return operations;
    }

    In OpenAPI we define what operations a path exposes. Operations are exposed as HTTP methods.

    The apiDoc property of the GET http method configures the getWorld operation with koa-openapi. Without it koa-openapi would do nothing with it. We can see that worldName is a required query parameter. If we were to call this operation without worldName we would receive a 400 input validation error.

    In this example, we're also using dependency injection. This allows us to easily connect our path handlers with our API's services. We could've exposed an object literal instead of a function. The dependency injection approach is recommended which is why we use it here.

  3. Create services

    We referenced a worldsService in our path handler, let's create it now. It's best to place services that conform to your API's object definitions under a versioned folder. This keeps API versioned code separately and allows you to scale your app for multiple API versions.

    // ./api-v1/services/worldsService.js
    
    let worlds = {
      Earth: {
        name: 'Earth'
      }
    };
    
    const worldsService = {
      getWorlds(name) {
        return worlds[name] ? [worlds[name]] : [];
      }
    };
    
    export default worldsService;
  4. Initialize your koa router with koa-openapi

    We'll create our router as usual and we'll initialize it with koa-openapi:

    // ./app.js
    import Koa from 'koa';
    import Router from 'koa-router';
    import bodyParser from 'koa-bodyparser';
    import { initialize } from 'koa-openapi';
    import v1WorldsService from './api-v1/services/worldsService';
    import v1ApiDoc from './api-v1/api-doc';
    
    const app = new Koa();
    const router = new Router();
    
    app.use(bodyParser());
    
    initialize({
      router,
      apiDoc: v1ApiDoc,
      dependencies: {
        worldsService: v1WorldsService
      },
      paths: './api-v1/paths'
    });
    
    app.use( router.routes() );
    
    app.listen(3000);

Our paths are now active and we can test them out with Swagger UI. This getting started guide didn't cover everything. For more examples see the sample projects used in our extensive test suite.

LICENSE

The MIT License (MIT)

Copyright (c) 2018 Kogo Software LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.