Nerfing Powerful Megalomaniacs
    Have ideas to improve npm?Join in the discussion! »

    koa-micro-ts
    TypeScript icon, indicating that this package has built-in type declarations

    2.1.1 • Public • Published

    koa-micro-ts

    Microservice framework based on koa

       _                               _                      _
      | | _____   __ _       _ __ ___ (_) ___ _ __ ___       | |_ ___
      | |/ / _ \ / _` |_____| '_ ` _ \| |/ __| '__/ _ \ _____| __/ __|
      |   < (_) | (_| |_____| | | | | | | (__| | | (_) |_____| |_\__ \
      |_|\_\___/ \__,_|     |_| |_| |_|_|\___|_|  \___/       \__|___/
    
         Koa TypeScript Microservice Framework - batteries included
    
    

    NPM Version NPM Downloads Git Issues Closed Issues deps status Caretaker MIT license

    Quick Start

    This package provides a minimalistic, simple to use, koa based micro service template. A few common used middleware packages are already included. To keep it small as possible, we added some own tiny libraries like CORS, JWT-wrapper, AUTO ROUTES, LOGGER, VALIDATORS. Included middleware/libs:

    • body parser
    • basic router
    • auto router - smart auto-generation of routes - detailed docs AUTOROUTES.md
    • CORS - detailed docs CORS.md
    • JWT - detailed docs JWT.md
    • static files serving
    • validators - detailed docs VALIDATORS.md
    • health API endpoint - detailed docs HEALTH.md
    • graceful shutdown - detailed docs SHUTDOWN.md
    • logger - detailed docs LOGGER.md
    • parsing command line arguments - detailed docs ARGS.md
    • dev/production mode detection - see below in this README.md
    • catch errors - detailed docs CATCHERRORS.md

    Most of these modules can be enabled with just one line of code. Configuration is super simple and lets you create your micro service within minutes.

    Installation

    $ npm install koa-micro-ts

    Usage

    Here is an example how you can use koa-micro-ts. Depending on your use case most of the things here are optional and only required if you want to use them:

    import { app, Application } from 'koa-micro-ts';
    import * as path from 'path';
    
    // setting variables only for demo purposes.
    // You can set this as environment variables
    process.env.APP_NAME = 'micro-service';
    process.env.VERSION = 'micro-version';
    
    // enable helpth endpoint (defaults to /health)
    app.health();
    
    // enable helmet (optional)
    app.helmet();
    
    // enable cors (optional)
    app.cors();
    
    // parse command line params (optional)
    app.getArgs();
    
    // catch uncatched errors - must be 'used' before adding routes
    app.catchErrors();
    
    // set up static server (optional)
    app.static(path.join(__dirname, '/public'));
    
    // using router
    const router: any = app.newRouter();
    
    router.get('/route', (ctx: Application.Context, next: Application.Next) => {
      ctx.body = 'OK from static route';
    });
    
    app.useRouter(router);
    
    // enable gracefull shutdown (optional)
    app.gracefulShutdown();
    
    app.start(3000);

    Have a look at the function reference APP.md for all options

    Auto-Routes

    This is one of the smart features of this package:

    autoRoute allows you to just write your API endpoints and place them into a directory structure. When calling app.autoRoute(...directory..., mountpoint), this directory will be parsed recursivly and all TypeScript files with extension .route.ts are added as routes. All routes then will be mounted to the given mountpoint. Your API structure then matches exactly your directory structure. This makes writing and maintaining your API endpoints super simple.

    Detailed docs with examples can be found here: AUTOROUTES.md

    Dev / Production Mode

    The app instance has a development property that is set to true when providing a --dev or --development argument during startup or if the environment variable DEVELOPMENT exists.

    You can use this property e.g. like this:

    if (app.development) {
      ...
    }
    

    Examples

    The example in the path examples shows how to use koa-micro-ts and

    • enable health endpoint
    • enable helmet
    • enable cors
    • serving static pages
    • using standard router
    • using auto routes

    Building Example App

    git clone https://github.com/sebhildebrandt/koa-micro.git
    cd koa-micro
    npm install
    npm run build-example
    npm run example
    

    Now try the following routes in your browser:

    Static Page:

    • http://localhost:3000/

    Standard Routes

    • http://localhost:3000/route
    • http://localhost:3000/route2

    Health Routes

    • http://localhost:3000/liveness
    • http://localhost:3000/readyness

    Routes from autoRouter

    • http://localhost:3000/api/v1/
    • http://localhost:3000/api/v1/hello/
    • http://localhost:3000/api/v1/error/
    • http://localhost:3000/api/v1/resource/?param=value

    Advanced usage

    As koa-micro-ts uses some external packages, you can also refer to the documentation of the used packages to see their options:

    License MIT license

    The MIT License (MIT)

    Copyright © 2021 Sebastian Hildebrandt, +innovations.

    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.

    Further details see LICENSE file.

    Install

    npm i koa-micro-ts

    DownloadsWeekly Downloads

    184

    Version

    2.1.1

    License

    MIT

    Unpacked Size

    102 kB

    Total Files

    33

    Last publish

    Collaborators

    • avatar