• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

koajs/convert: Convert koa generator-based middleware to promise-based middlewar ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

koajs/convert

开源软件地址:

https://github.com/koajs/convert

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-convert

Build Status NPM version License Code Size

Convert Koa legacy (0.x & 1.x) generator middleware to modern promise middleware (2.x).

It could also convert modern promise middleware back to legacy generator middleware (useful to help modern middleware support Koa v0.x or v1.x).

Note

Router middleware is special case here. Because it reimplements middleware composition internally, we cannot not simply convert it.

You may use following packages for routing, which are koa 2.x ready now:

Installation

# npm ..
$ npm i koa-convert
# yarn ..
$ yarn add koa-convert

Usage

const Koa = require('koa') // koa v2.x
const convert = require('koa-convert')
const app = new Koa()

app.use(modernMiddleware)

app.use(convert(legacyMiddleware))

app.use(convert.compose(legacyMiddleware, modernMiddleware))

function * legacyMiddleware (next) {
  // before
  yield next
  // after
}

function modernMiddleware (ctx, next) {
  // before
  return next().then(() => {
    // after
  })
}

Distinguish legacy and modern middleware

In koa 0.x and 1.x (without experimental flag), app.use has an assertion that all (legacy) middleware must be generator function and it's tested with fn.constructor.name == 'GeneratorFunction' at here.

Therefore, we can distinguish legacy and modern middleware with fn.constructor.name == 'GeneratorFunction'.

Migration

app.use(legacyMiddleware) is everywhere in 0.x and 1.x and it would be painful to manually change all of them to app.use(convert(legacyMiddleware)).

You can use following snippet to make migration easier.

const _use = app.use
app.use = x => _use.call(app, convert(x))

The above snippet will override app.use method and implicitly convert all legacy generator middleware to modern promise middleware.

Therefore, you can have both app.use(modernMiddleware) and app.use(legacyMiddleware) and your 0.x or 1.x should work without modification.

Complete example:

const Koa = require('koa') // v2.x
const convert = require('koa-convert')
const app = new Koa()

// ---------- override app.use method ----------

const _use = app.use
app.use = x => _use.call(app, convert(x))

// ---------- end ----------

app.use(modernMiddleware)

// this will be converted to modern promise middleware implicitly
app.use(legacyMiddleware)

function * legacyMiddleware (next) {
  // before
  yield next
  // after
}

function modernMiddleware (ctx, next) {
  // before
  return next().then(() => {
    // after
  })
}

API

convert()

Convert legacy generator middleware to modern promise middleware.

modernMiddleware = convert(legacyMiddleware)

convert.compose()

Convert and compose multiple middleware (could mix legacy and modern ones) and return modern promise middleware.

composedModernMiddleware = convert.compose(legacyMiddleware, modernMiddleware)
// or
composedModernMiddleware = convert.compose([legacyMiddleware, modernMiddleware])

convert.back()

Convert modern promise middleware back to legacy generator middleware.

This is useful to help modern promise middleware support koa 0.x or 1.x.

legacyMiddleware = convert.back(modernMiddleware)

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
i5ting/koa2vue2ssr: koa 2.x + vue 2.x server side rendering发布时间:2022-06-24
下一篇:
nodoherty/ColdFusion-Koans发布时间:2022-06-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap