-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create v2 Migration document #920
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e1ec6c3
Give v2 migration documentation its own document. Incorporate docs fr…
JeffRMoore dafce23
Fix mis-capitalization of Koa
JeffRMoore 9196915
Remove unnecessary Dependency section
JeffRMoore 32fafb1
Hint at koa-convert enabled compatibility
JeffRMoore 307a1e9
Add section on constructing with new
JeffRMoore 57147f9
Merge branch 'master' into migration-docs
JeffRMoore 7e5554f
Clarify es6 constructors are used
JeffRMoore b23097c
Fix varying capitalization
JeffRMoore 7b0b8d2
Restore mistakenly removed Dependency changes section
JeffRMoore 242cf4e
v1.x should not receive feature updates
JeffRMoore e9983eb
Add next() to signature, add missing backticks
JeffRMoore 1ea3068
Merge branch 'master' into migration-docs
JeffRMoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Migrating from Koa v1.x to v2.x | ||
|
||
## New middleware signature | ||
|
||
Koa v2 introduces a new signature for middleware. | ||
|
||
**Old signature middleware (v1.x) support will be removed in v3** | ||
|
||
The new middleware signature is: | ||
|
||
```js | ||
// uses async arrow functions | ||
app.use(async (ctx, next) => { | ||
try { | ||
await next() // next is now a function | ||
} catch (err) { | ||
ctx.body = { message: err.message } | ||
ctx.status = err.status || 500 | ||
} | ||
}) | ||
|
||
app.use(async ctx => { | ||
const user = await User.getById(this.session.userid) // await instead of yield | ||
ctx.body = user // ctx instead of this | ||
}) | ||
``` | ||
|
||
You don't have to use asynchronous functions - you just have to pass a function that returns a promise. | ||
A regular function that returns a promise works too! | ||
|
||
The signature has changed to pass `Context` via an explicit parameter, `ctx` above, instead of via | ||
`this`. The context passing change makes koa more compatible with es6 arrow functions, which capture `this`. | ||
|
||
## Using v1.x Middleware in v2.x | ||
|
||
Koa v2.x will try to convert legacy signature, generator middleware on `app.use`, using [koa-convert](https://github.com/koajs/convert). | ||
It is however recommended that you choose to migrate all v1.x middleware as soon as possible. | ||
|
||
```js | ||
// Koa will convert | ||
app.use(function *(next) { | ||
const start = new Date(); | ||
yield next; | ||
const ms = new Date() - start; | ||
console.log(`${this.method} ${this.url} - ${ms}ms`); | ||
}); | ||
``` | ||
|
||
You could do it manually as well, in which case Koa will not convert. | ||
|
||
```js | ||
const convert = require('koa-convert'); | ||
|
||
app.use(convert(function *(next) { | ||
const start = new Date(); | ||
yield next; | ||
const ms = new Date() - start; | ||
console.log(`${this.method} ${this.url} - ${ms}ms`); | ||
})); | ||
``` | ||
|
||
## Upgrading middleware | ||
|
||
You will have to convert your generators to async functions with the new middleware signature: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would show There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Fixed. |
||
|
||
```js | ||
app.use(async ctx => { | ||
const user = await Users.getById(this.session.user_id); | ||
await next(); | ||
ctx.body = { message: 'some message' }; | ||
}) | ||
``` | ||
|
||
Upgrading your middleware may require some work. One migration path is to update them one-by-one. | ||
|
||
1. Wrap all your current middleware in `koa-convert` | ||
2. Test | ||
3. `npm outdated` to see which koa middleware is outdated | ||
4. Update one outdated middleware, remove using `koa-convert` | ||
5. Test | ||
6. Repeat steps 3-5 until you're done | ||
|
||
|
||
## Updating your code | ||
|
||
You should start refactoring your code now to ease migrating to Koa v2: | ||
|
||
- Return promises everywhere! | ||
- Do not use `yield*` | ||
- Do not use `yield {}` or `yield []`. | ||
- Convert `yield []` into `yield Promise.all([])` | ||
- Convert `yield {}` into `yield Bluebird.props({})` | ||
|
||
You could also refactor your logic outside of Koa middleware functions. Create functions like | ||
`function* someLogic(ctx) {}` and call it in your middleware as | ||
`const result = yield someLogic(this)`. | ||
Not using `this` will help migrations to the new middleware signature, which does not use `this`. | ||
|
||
## Application object constructor requires new | ||
|
||
In v1.x, the Application constructor function could be called directly, without `new` to | ||
instantiate an instance of an application. For example: | ||
|
||
```js | ||
var koa = require('koa'); | ||
var app = module.exports = koa(); | ||
``` | ||
|
||
v2.x uses es6 classes which require the `new` keyword to be used. | ||
|
||
```js | ||
var koa = require('koa'); | ||
var app = module.exports = new koa(); | ||
``` | ||
|
||
## ENV specific logging behavior removed | ||
|
||
An explicit check for the `test` environment was removed from error handling. | ||
|
||
## Dependency changes | ||
|
||
- [co](https://github.com/tj/co) is no longer bundled with Koa. Require or import it directly. | ||
- [composition](https://github.com/thenables/composition) is no longer used and deprecated. | ||
|
||
## v1.x support | ||
|
||
The v1.x branch is still supported but should not receive feature updates. Except for this migration | ||
guide, documentation will target the latest version. | ||
|
||
## Help out | ||
|
||
If you encounter migration related issues not covered by this migration guide, please consider | ||
submitting a documentation pull request. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we should just remove this section as it would only confuse people
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was weak, but didn't know if the unbundling of co mattered. Seems not. Removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait! I completely misread this. My mistake. I thought you were referring to the dependency section in the migration page which is where I moved the "no longer supplied text" to.
@jonathanong When you refer to "this section" are you referring to the "GeneratorFunction" section?
Should I add back the dependency section I just removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about it, I agree. The overview document should focus on the recommended way, which I think now is async and leave the other two formats for deeper documentation. I'd like to submit that as a separate PR, though, so as to not block the merging of this migration guide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and restored the Dependency changes section I mistakenly removed. I don't know if its meaningful, I'm not actually a v1.x user, I was attracted by the v2.x release.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, I meant the
GeneratorFunction
section. we shouldn't be recommending people to use generator functions, and if they do, it should be the v1.x version that should be migrated to async functions. this is a generation function with(ctx, next)
arguments, which is a mix of v1.x and v2.x APIs and is awkward