mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-11-21 14:13:36 +08:00
docs:更新文档
This commit is contained in:
1
node_modules/koa-convert/.npmignore
generated
vendored
Normal file
1
node_modules/koa-convert/.npmignore
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules
|
||||
5
node_modules/koa-convert/.travis.yml
generated
vendored
Normal file
5
node_modules/koa-convert/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "4"
|
||||
- "5"
|
||||
script: "npm run test"
|
||||
22
node_modules/koa-convert/LICENSE
generated
vendored
Normal file
22
node_modules/koa-convert/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 yunsong
|
||||
|
||||
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.
|
||||
|
||||
136
node_modules/koa-convert/README.md
generated
vendored
Normal file
136
node_modules/koa-convert/README.md
generated
vendored
Normal file
@@ -0,0 +1,136 @@
|
||||
|
||||
# koa-convert
|
||||
|
||||
[](https://npmjs.org/package/koa-convert)
|
||||
[](https://travis-ci.org/gyson/koa-convert)
|
||||
|
||||
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 0.x or 1.x ).
|
||||
|
||||
## Note
|
||||
|
||||
It should be able to convert any legacy generator middleware to modern promise middleware ( or convert it back ).
|
||||
|
||||
Please let me know ( send a issue ) if you fail to do so.
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
$ npm install koa-convert
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
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](https://github.com/koajs/koa/blob/7fe29d92f1e826d9ce36029e1b9263b94cba8a7c/lib/application.js#L105).
|
||||
|
||||
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.
|
||||
|
||||
```js
|
||||
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:
|
||||
|
||||
```js
|
||||
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.
|
||||
|
||||
```js
|
||||
modernMiddleware = convert(legacyMiddleware)
|
||||
```
|
||||
|
||||
#### `convert.compose()`
|
||||
|
||||
Convert and compose multiple middleware (could mix legacy and modern ones) and return modern promise middleware.
|
||||
|
||||
```js
|
||||
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.
|
||||
|
||||
```js
|
||||
legacyMiddleware = convert.back(modernMiddleware)
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
60
node_modules/koa-convert/index.js
generated
vendored
Normal file
60
node_modules/koa-convert/index.js
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
'use strict'
|
||||
|
||||
const co = require('co')
|
||||
const compose = require('koa-compose')
|
||||
|
||||
module.exports = convert
|
||||
|
||||
function convert (mw) {
|
||||
if (typeof mw !== 'function') {
|
||||
throw new TypeError('middleware must be a function')
|
||||
}
|
||||
if (mw.constructor.name !== 'GeneratorFunction') {
|
||||
// assume it's Promise-based middleware
|
||||
return mw
|
||||
}
|
||||
const converted = function (ctx, next) {
|
||||
return co.call(ctx, mw.call(ctx, createGenerator(next)))
|
||||
}
|
||||
converted._name = mw._name || mw.name
|
||||
return converted
|
||||
}
|
||||
|
||||
function * createGenerator (next) {
|
||||
return yield next()
|
||||
}
|
||||
|
||||
// convert.compose(mw, mw, mw)
|
||||
// convert.compose([mw, mw, mw])
|
||||
convert.compose = function (arr) {
|
||||
if (!Array.isArray(arr)) {
|
||||
arr = Array.from(arguments)
|
||||
}
|
||||
return compose(arr.map(convert))
|
||||
}
|
||||
|
||||
convert.back = function (mw) {
|
||||
if (typeof mw !== 'function') {
|
||||
throw new TypeError('middleware must be a function')
|
||||
}
|
||||
if (mw.constructor.name === 'GeneratorFunction') {
|
||||
// assume it's generator middleware
|
||||
return mw
|
||||
}
|
||||
const converted = function * (next) {
|
||||
let ctx = this
|
||||
let called = false
|
||||
// no need try...catch here, it's ok even `mw()` throw exception
|
||||
yield Promise.resolve(mw(ctx, function () {
|
||||
if (called) {
|
||||
// guard against multiple next() calls
|
||||
// https://github.com/koajs/compose/blob/4e3e96baf58b817d71bd44a8c0d78bb42623aa95/index.js#L36
|
||||
return Promise.reject(new Error('next() called multiple times'))
|
||||
}
|
||||
called = true
|
||||
return co.call(ctx, next)
|
||||
}))
|
||||
}
|
||||
converted._name = mw._name || mw.name
|
||||
return converted
|
||||
}
|
||||
50
node_modules/koa-convert/node_modules/koa-compose/History.md
generated
vendored
Normal file
50
node_modules/koa-convert/node_modules/koa-compose/History.md
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
3.2.1 / 2016-10-26
|
||||
==================
|
||||
|
||||
* revert add variadric support #65 - introduced an unintended breaking change
|
||||
|
||||
3.2.0 / 2016-10-25
|
||||
==================
|
||||
|
||||
* fix #60 infinite loop when calling next https://github.com/koajs/compose/pull/61
|
||||
* add variadric support https://github.com/koajs/compose/pull/65
|
||||
|
||||
3.1.0 / 2016-03-17
|
||||
==================
|
||||
|
||||
* add linting w/ standard
|
||||
* use `any-promise` so that the promise engine is configurable
|
||||
|
||||
3.0.0 / 2015-10-19
|
||||
==================
|
||||
|
||||
* change middleware signature to `async (ctx, next) => await next()` for `koa@2`.
|
||||
See https://github.com/koajs/compose/pull/27 for more information.
|
||||
|
||||
2.3.0 / 2014-05-01
|
||||
==================
|
||||
|
||||
* remove instrumentation
|
||||
|
||||
2.2.0 / 2014-01-22
|
||||
==================
|
||||
|
||||
* add `fn._name` for debugging
|
||||
|
||||
2.1.0 / 2013-12-22
|
||||
==================
|
||||
|
||||
* add debugging support
|
||||
* improve performance ~15%
|
||||
|
||||
2.0.1 / 2013-12-21
|
||||
==================
|
||||
|
||||
* update co to v3
|
||||
* use generator delegation
|
||||
|
||||
2.0.0 / 2013-11-07
|
||||
==================
|
||||
|
||||
* change middleware signature expected
|
||||
40
node_modules/koa-convert/node_modules/koa-compose/Readme.md
generated
vendored
Normal file
40
node_modules/koa-convert/node_modules/koa-compose/Readme.md
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
# koa-compose
|
||||
|
||||
[![NPM version][npm-image]][npm-url]
|
||||
[![Build status][travis-image]][travis-url]
|
||||
[![Test coverage][codecov-image]][codecov-url]
|
||||
[![Dependency Status][david-image]][david-url]
|
||||
[![License][license-image]][license-url]
|
||||
[![Downloads][downloads-image]][downloads-url]
|
||||
|
||||
Compose middleware.
|
||||
|
||||
## Installation
|
||||
|
||||
```js
|
||||
$ npm install koa-compose
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### compose([a, b, c, ...])
|
||||
|
||||
Compose the given middleware and return middleware.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
[npm-image]: https://img.shields.io/npm/v/koa-compose.svg?style=flat-square
|
||||
[npm-url]: https://npmjs.org/package/koa-compose
|
||||
[travis-image]: https://img.shields.io/travis/koajs/compose/next.svg?style=flat-square
|
||||
[travis-url]: https://travis-ci.org/koajs/compose
|
||||
[codecov-image]: https://img.shields.io/codecov/c/github/koajs/compose/next.svg?style=flat-square
|
||||
[codecov-url]: https://codecov.io/github/koajs/compose
|
||||
[david-image]: http://img.shields.io/david/koajs/compose.svg?style=flat-square
|
||||
[david-url]: https://david-dm.org/koajs/compose
|
||||
[license-image]: http://img.shields.io/npm/l/koa-compose.svg?style=flat-square
|
||||
[license-url]: LICENSE
|
||||
[downloads-image]: http://img.shields.io/npm/dm/koa-compose.svg?style=flat-square
|
||||
[downloads-url]: https://npmjs.org/package/koa-compose
|
||||
52
node_modules/koa-convert/node_modules/koa-compose/index.js
generated
vendored
Normal file
52
node_modules/koa-convert/node_modules/koa-compose/index.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
'use strict'
|
||||
|
||||
const Promise = require('any-promise')
|
||||
|
||||
/**
|
||||
* Expose compositor.
|
||||
*/
|
||||
|
||||
module.exports = compose
|
||||
|
||||
/**
|
||||
* Compose `middleware` returning
|
||||
* a fully valid middleware comprised
|
||||
* of all those which are passed.
|
||||
*
|
||||
* @param {Array} middleware
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function compose (middleware) {
|
||||
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!')
|
||||
for (const fn of middleware) {
|
||||
if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} context
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
return function (context, next) {
|
||||
// last called middleware #
|
||||
let index = -1
|
||||
return dispatch(0)
|
||||
function dispatch (i) {
|
||||
if (i <= index) return Promise.reject(new Error('next() called multiple times'))
|
||||
index = i
|
||||
let fn = middleware[i]
|
||||
if (i === middleware.length) fn = next
|
||||
if (!fn) return Promise.resolve()
|
||||
try {
|
||||
return Promise.resolve(fn(context, function next () {
|
||||
return dispatch(i + 1)
|
||||
}))
|
||||
} catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
72
node_modules/koa-convert/node_modules/koa-compose/package.json
generated
vendored
Normal file
72
node_modules/koa-convert/node_modules/koa-compose/package.json
generated
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"koa-compose@3.2.1",
|
||||
"J:\\Github\\CURD-TS"
|
||||
]
|
||||
],
|
||||
"_development": true,
|
||||
"_from": "koa-compose@3.2.1",
|
||||
"_id": "koa-compose@3.2.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-qFzLQLfZhtjlo0Wzoazo6rz1Tec=",
|
||||
"_location": "/koa-convert/koa-compose",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "koa-compose@3.2.1",
|
||||
"name": "koa-compose",
|
||||
"escapedName": "koa-compose",
|
||||
"rawSpec": "3.2.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "3.2.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/koa-convert"
|
||||
],
|
||||
"_resolved": "http://192.168.250.101:4873/koa-compose/-/koa-compose-3.2.1.tgz",
|
||||
"_spec": "3.2.1",
|
||||
"_where": "J:\\Github\\CURD-TS",
|
||||
"bugs": {
|
||||
"url": "https://github.com/koajs/compose/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"any-promise": "^1.1.0"
|
||||
},
|
||||
"description": "compose Koa middleware",
|
||||
"devDependencies": {
|
||||
"co": "^4.6.0",
|
||||
"istanbul": "^0.4.2",
|
||||
"matcha": "^0.7.0",
|
||||
"mocha": "^3.1.2",
|
||||
"should": "^2.0.0",
|
||||
"standard": "^8.4.0"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"homepage": "https://github.com/koajs/compose#readme",
|
||||
"keywords": [
|
||||
"koa",
|
||||
"middleware",
|
||||
"compose"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "koa-compose",
|
||||
"publishConfig": {
|
||||
"tag": "next"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/koajs/compose.git"
|
||||
},
|
||||
"scripts": {
|
||||
"bench": "matcha bench/bench.js",
|
||||
"lint": "standard index.js test/*.js",
|
||||
"test": "mocha --require should --reporter spec",
|
||||
"test-cov": "node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should",
|
||||
"test-travis": "node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should"
|
||||
},
|
||||
"version": "3.2.1"
|
||||
}
|
||||
72
node_modules/koa-convert/package.json
generated
vendored
Normal file
72
node_modules/koa-convert/package.json
generated
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"koa-convert@1.2.0",
|
||||
"J:\\Github\\CURD-TS"
|
||||
]
|
||||
],
|
||||
"_development": true,
|
||||
"_from": "koa-convert@1.2.0",
|
||||
"_id": "koa-convert@1.2.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-2kCHXfSd4FOQmNFwC1CCDOvNIdA=",
|
||||
"_location": "/koa-convert",
|
||||
"_phantomChildren": {
|
||||
"any-promise": "1.3.0"
|
||||
},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "koa-convert@1.2.0",
|
||||
"name": "koa-convert",
|
||||
"escapedName": "koa-convert",
|
||||
"rawSpec": "1.2.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "1.2.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/koa"
|
||||
],
|
||||
"_resolved": "http://192.168.250.101:4873/koa-convert/-/koa-convert-1.2.0.tgz",
|
||||
"_spec": "1.2.0",
|
||||
"_where": "J:\\Github\\CURD-TS",
|
||||
"author": {
|
||||
"name": "gyson",
|
||||
"email": "eilian.yunsong@gmail.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/gyson/koa-convert/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"co": "^4.6.0",
|
||||
"koa-compose": "^3.0.0"
|
||||
},
|
||||
"description": "convert koa legacy generator-based middleware to promise-based middleware",
|
||||
"devDependencies": {
|
||||
"koa": "^2.0.0-alpha.2",
|
||||
"koa-v1": "^1.0.0",
|
||||
"mocha": "^2.3.3",
|
||||
"standard": "^5.3.1",
|
||||
"supertest": "^1.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 4"
|
||||
},
|
||||
"homepage": "https://github.com/gyson/koa-convert#readme",
|
||||
"keywords": [
|
||||
"koa",
|
||||
"middleware",
|
||||
"convert"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "koa-convert",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/gyson/koa-convert.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "standard && mocha test.js"
|
||||
},
|
||||
"version": "1.2.0"
|
||||
}
|
||||
254
node_modules/koa-convert/test.js
generated
vendored
Normal file
254
node_modules/koa-convert/test.js
generated
vendored
Normal file
@@ -0,0 +1,254 @@
|
||||
/* global describe, it */
|
||||
|
||||
'use strict'
|
||||
|
||||
const co = require('co')
|
||||
const Koa = require('koa')
|
||||
const KoaV1 = require('koa-v1')
|
||||
const assert = require('assert')
|
||||
const convert = require('./index')
|
||||
const request = require('supertest')
|
||||
|
||||
describe('convert()', () => {
|
||||
it('should work', () => {
|
||||
let call = []
|
||||
let ctx = {}
|
||||
let mw = convert(function * (next) {
|
||||
assert.ok(ctx === this)
|
||||
call.push(1)
|
||||
})
|
||||
|
||||
return mw(ctx, function () {
|
||||
call.push(2)
|
||||
}).then(function () {
|
||||
assert.deepEqual(call, [1])
|
||||
})
|
||||
})
|
||||
|
||||
it('should inherit the original middleware name', () => {
|
||||
let mw = convert(function * testing (next) {})
|
||||
assert.strictEqual(mw._name, 'testing')
|
||||
})
|
||||
|
||||
it('should work with `yield next`', () => {
|
||||
let call = []
|
||||
let ctx = {}
|
||||
let mw = convert(function * (next) {
|
||||
assert.ok(ctx === this)
|
||||
call.push(1)
|
||||
yield next
|
||||
call.push(3)
|
||||
})
|
||||
|
||||
return mw(ctx, function () {
|
||||
call.push(2)
|
||||
return Promise.resolve()
|
||||
}).then(function () {
|
||||
assert.deepEqual(call, [1, 2, 3])
|
||||
})
|
||||
})
|
||||
|
||||
it('should work with `yield* next`', () => {
|
||||
let call = []
|
||||
let ctx = {}
|
||||
let mw = convert(function * (next) {
|
||||
assert.ok(ctx === this)
|
||||
call.push(1)
|
||||
yield* next
|
||||
call.push(3)
|
||||
})
|
||||
|
||||
return mw(ctx, function () {
|
||||
call.push(2)
|
||||
return Promise.resolve()
|
||||
}).then(function () {
|
||||
assert.deepEqual(call, [1, 2, 3])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('convert.compose()', () => {
|
||||
it('should work', () => {
|
||||
let call = []
|
||||
let context = {}
|
||||
let _context
|
||||
let mw = convert.compose([
|
||||
function * name (next) {
|
||||
call.push(1)
|
||||
yield next
|
||||
call.push(11)
|
||||
},
|
||||
(ctx, next) => {
|
||||
call.push(2)
|
||||
return next().then(() => {
|
||||
call.push(10)
|
||||
})
|
||||
},
|
||||
function * (next) {
|
||||
call.push(3)
|
||||
yield* next
|
||||
call.push(9)
|
||||
},
|
||||
co.wrap(function * (ctx, next) {
|
||||
call.push(4)
|
||||
yield next()
|
||||
call.push(8)
|
||||
}),
|
||||
function * (next) {
|
||||
try {
|
||||
call.push(5)
|
||||
yield next
|
||||
} catch (e) {
|
||||
call.push(7)
|
||||
}
|
||||
},
|
||||
(ctx, next) => {
|
||||
_context = ctx
|
||||
call.push(6)
|
||||
throw new Error()
|
||||
}
|
||||
])
|
||||
|
||||
return mw(context).then(() => {
|
||||
assert.equal(context, _context)
|
||||
assert.deepEqual(call, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
|
||||
})
|
||||
})
|
||||
|
||||
it('should work too', () => {
|
||||
let call = []
|
||||
let context = {}
|
||||
let _context
|
||||
let mw = convert.compose(
|
||||
(ctx, next) => {
|
||||
call.push(1)
|
||||
return next().catch(() => {
|
||||
call.push(4)
|
||||
})
|
||||
},
|
||||
function * (next) {
|
||||
call.push(2)
|
||||
yield next
|
||||
call.push(-1) // should not call this
|
||||
},
|
||||
function * (next) {
|
||||
call.push(3)
|
||||
yield* next
|
||||
call.push(-1) // should not call this
|
||||
},
|
||||
(ctx, next) => {
|
||||
_context = ctx
|
||||
return Promise.reject(new Error())
|
||||
}
|
||||
)
|
||||
|
||||
return mw(context).then(() => {
|
||||
assert.equal(context, _context)
|
||||
assert.deepEqual(call, [1, 2, 3, 4])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('convert.back()', () => {
|
||||
it('should work with koa 1', done => {
|
||||
let app = new KoaV1()
|
||||
|
||||
app.use(function * (next) {
|
||||
this.body = [1]
|
||||
yield next
|
||||
this.body.push(6)
|
||||
})
|
||||
|
||||
app.use(convert.back((ctx, next) => {
|
||||
ctx.body.push(2)
|
||||
return next().then(() => {
|
||||
ctx.body.push(5)
|
||||
})
|
||||
}))
|
||||
|
||||
app.use(convert.back(co.wrap(function * (ctx, next) {
|
||||
ctx.body.push(3)
|
||||
yield next()
|
||||
ctx.body.push(4)
|
||||
})))
|
||||
|
||||
request(app.callback())
|
||||
.get('/')
|
||||
.expect(200, [1, 2, 3, 4, 5, 6])
|
||||
.end(done)
|
||||
})
|
||||
|
||||
it('should guard multiple calls', done => {
|
||||
let app = new KoaV1()
|
||||
|
||||
app.use(function * (next) {
|
||||
try {
|
||||
this.body = [1]
|
||||
yield next
|
||||
} catch (e) {
|
||||
this.body.push(e.message)
|
||||
}
|
||||
})
|
||||
|
||||
app.use(convert.back(co.wrap(function * (ctx, next) {
|
||||
ctx.body.push(2)
|
||||
yield next()
|
||||
yield next() // this should throw new Error('next() called multiple times')
|
||||
})))
|
||||
|
||||
request(app.callback())
|
||||
.get('/')
|
||||
.expect(200, [1, 2, 'next() called multiple times'])
|
||||
.end(done)
|
||||
})
|
||||
|
||||
it('should inherit the original middleware name', () => {
|
||||
let mw = convert.back(function testing (ctx, next) {})
|
||||
assert.strictEqual(mw._name, 'testing')
|
||||
})
|
||||
})
|
||||
|
||||
describe('migration snippet', () => {
|
||||
let app = new Koa()
|
||||
|
||||
// snippet
|
||||
const _use = app.use
|
||||
app.use = x => _use.call(app, convert(x))
|
||||
// end
|
||||
|
||||
app.use((ctx, next) => {
|
||||
ctx.body = [1]
|
||||
return next().then(() => {
|
||||
ctx.body.push(9)
|
||||
})
|
||||
})
|
||||
|
||||
app.use(function * (next) {
|
||||
this.body.push(2)
|
||||
yield next
|
||||
this.body.push(8)
|
||||
})
|
||||
|
||||
app.use(function * (next) {
|
||||
this.body.push(3)
|
||||
yield* next
|
||||
this.body.push(7)
|
||||
})
|
||||
|
||||
app.use(co.wrap(function * (ctx, next) {
|
||||
ctx.body.push(4)
|
||||
yield next()
|
||||
ctx.body.push(6)
|
||||
}))
|
||||
|
||||
app.use(ctx => {
|
||||
ctx.body.push(5)
|
||||
})
|
||||
|
||||
it('should work', done => {
|
||||
request(app.callback())
|
||||
.get('/')
|
||||
.expect(200, [1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
.end(done)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user