Skip to content

New features and some breaking changes

Compare
Choose a tag to compare
@thetutlage thetutlage released this 12 Feb 09:52

Breaking changes

Following is the list of breaking changes. The transition from preview to the main version is our best bet to come up with all the breaking changes

route.makeUrl

The route.makeUrl was overloaded with too many options. We tried improving the API and also introduce a new URL builder API.

For the most common use case, there are no breaking changes. But a few, if you are generating URLs for a specific domain

Following examples will continue to work

// Will continue to work
Route.makeUrl('PostsController.show', { id: 1 })
Route.makeUrl('PostsController.show', {
  id: 1,
  qs: { published: true }
})

// Will continue to work
Route.makeUrl('PostsController.show', { params: { id: 1 } })
Route.makeUrl('PostsController.show', {
  params: { id: 1 },
  qs: { published: true }
})

Following will not work

Route.makeUrl(
  'PostsController.show',
  { id: 1 },
- 'blog.adonisjs.com'
)

Route.makeUrl(
  'PostsController.show',
  { id: 1 },
+ {
+  domain: 'blog.adonisjs.com'
+ })

Also prefixDomain and domainParams have been removed and you should instead use the prefixUrl option. Following is the new ideal API

// Pass params as an array
Route.makeUrl('/posts/:id', [1])

// Pass params as an object
Route.makeUrl('/posts/:id', { id: 1 })

// Options as 3rd argument
Route.makeUrl(
  '/posts/:id',
  { id: 1 },
  { qs: { published: true } }
)

Route.makeSignedUrl

The same changes are made to the makeSignedUrl method as well

Deprecate

The request.get and request.post methods have been deprecated in favor of request.qs and request.body. They were badly named at the first place.

- request.get()
+ request.qs()

- request.post()
+ request.body()

New features

Route param cast function

Along side the route matchers, you can also define a function to cast the parameter value. For example:

Route
  .get('users/:id', 'UsersController.show')
  .where('id', {
    match: /^[0-9]+$/,
    cast: (id) => Number(id)
  })

In the above example, the cast method converts the string representation of the number to an actual Number. We also ship with a couple of commonly use matchers too

Route
  .get('users/:id', 'UsersController.show')
-  .where('id', {
-    match: /^[0-9]+$/,
-    cast: (id) => Number(id)
-  })
+  .where('id', Route.matchers.number())

Add route.redirect method

Along side the route.render method, we now also have shortcut methods to redirect the request.

Route.get('guides/:doc', 'GuidesController.show')
Route.on('docs/:doc').redirect('GuidesController.show')

To redirect to an exact URL, you can make use of the redirectToPath method.

Route.on('blog/:slug').redirectToPath('https://medium.com/my-blog')

Commits

Check the following commits for bug fixes

  • chore(deps): bump ini from 1.3.5 to 1.3.8 (#36) fa09eea
  • feat: add brisk route redirect methods 2af36d6
  • fix: remove etag when streaming response fails 5a362f6
  • fix: response.send now sends 304 status code when etag cache is fresh 4668dfd
  • refactor: change signature of response.abortUnless 6ab60ee
  • refactor: the API for making urls from routes 5c7e39c
  • test: write test for an edge case param matcher test ac2480a
  • feat: add shortcut methods for defining route param matchers 29dd0d0
  • feat: add support for defining route casters 3324daf
  • improvement: deprecate request.get and request.post in favor of request.qs and request.body c40bbe3
  • fix: response.redirect forward referrer query string 6d716c1
  • style: reformat source files 94df36a
  • chore: remove npm-audit in favor of synk a06d736
  • chore: update dependencies b4c8bc8
  • fix(router): adding support for HEAD HTTP method for all GET routes back to v5 (#32) 05c00a2
  • fix: upgrade ms from 2.1.2 to 2.1.3 (#29) ea55c2a
  • chore(deps): bump ini from 1.3.5 to 1.3.8 (#28) 396a353
  • chore(deps): bump highlight.js from 10.4.0 to 10.4.1 (#26) c78cf09

v4.0.9...v5.0.0