Socket
Socket
Sign inDemoInstall

koa-convert

Package Overview
Dependencies
1
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    koa-convert

convert koa generator-based middleware to promise-based middleware


Version published
Maintainers
1
Install size
21.9 kB
Created

Package description

What is koa-convert?

The koa-convert npm package is designed to help transition from Koa's old middleware signature (generator functions) to the new middleware signature (async functions). It wraps Koa's old generator-based middleware to be compatible with the new Koa v2.x async middleware signature. This allows developers to use legacy middleware that hasn't yet been updated to the new format, facilitating a smoother upgrade path to Koa v2.x.

What are koa-convert's main functionalities?

Converting generator-based middleware

This feature allows you to wrap old Koa middleware that uses generator functions into a form that's compatible with Koa v2.x's async middleware. The code sample demonstrates how to convert a simple timing middleware to the new format.

const convert = require('koa-convert');
const oldMiddleware = function *(next) {
  const start = new Date;
  yield next;
  const ms = new Date - start;
  console.log(`${this.method} ${this.url} - ${ms}ms`);
};

app.use(convert(oldMiddleware));

Other packages similar to koa-convert

Readme

Source

koa-convert

Convert koa generator-based middleware to promise-based middleware.

Installation

$ npm install koa-convert
  • koa #415
  • koa-compose #27

Usage

const convert = require('koa-convert')

//
// convert a generator-based middleware to promise-based middleware
//
let promiseBased = convert(function* generatorBased(next) {
    yield next
    // or
    // yield* next
})

//
// convert array of middleware
//
let mws = [
    // will convert it to promise-based middleware
    function* generatorMW (next) {
        yield next
    },
    // will convert it to promise-based middleware
    function* generatorMW(next) {
        yield* next
    },
    // return itself if it's not generator-based middleware
    function (ctx, next) {
        return next()
    },
    // return itself if it's not generator-based middleware
    async function (ctx, next) {
        await next()
    },
].map(convert)

License

MIT

FAQs

Last updated on 11 Oct 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc