Preview release rc-2

We have released @adonisjs/[email protected] on October 23rd, 2020 and it contains following additions, changes and bug fixes.

Highlights

  • Support for in-memory compilition of Typescript. In other words, the development workflow of AdonisJS will no longer write the compiled Javascript to the disk and uses in memory compilition. It has become possible because of the @adonisjs/require-ts module.
  • Ability to validate environment variables. Learn more about it here.
  • Introducing @adonisjs/repl

Http Server

  • addition: Adding request.param and request.params methods as an alternative to read the route params. 06c0f0ebc

    Route.get('users/:id', ({ request, params }) => {
      console.log(params.id)
    
      // Same as the above
      console.log(request.param('id'))
    })
    
  • improvement: Simplify creation of HTTP context instance using HttpContext.create method. This is useful when you are trying to unit test a piece of code that needs the HTTP context. For example: Unit testing a middleware. 75fcbc6b0

    class SomeMiddleware {
      public async handle(ctx) {}
    }
    
    // inside some test file
    import HttpContext from '@ioc:Adonis/Core/HttpContext'
    
    const ctx = HttpContext.create('users/:id', { id: 1 })
    await new SomeMiddleware().handle(ctx)
    
  • improvement: POTENTIONAL BREAKING CHANGE The response.redirect().withQs() method now merges the query strings together when called multiple times. Earlier, it used to overwrite the previous values leading to difficulties when trying to append to the current request query string. For example:

    // REQUEST URL: /users?page=1&limit=10
    
    // EARLIER OUTPUT: /users?sort=popular
    response.redirect().withQs().withQs('sort', 'popular')
    
    // CURRENT OUTPUT: /users?page=1&limit=10&sort=popular
    response.redirect().withQs().withQs('sort', 'popular')
    

    If you want to clean up existing query string, then you can make use of the newly added cleanQs method. 545e606ff8

Env

The @adonisjs/env package has received a bunch of improvements including the support for validating environment variables.

  • addition: Support for validating environment variables. Begin by creating env.ts file inside the project root. 15985122.

    env.ts
    import Env from '@ioc:Adonis/Core/Env'
    
    Env.rules({
      APP_KEY: Env.schema.string(),
      HOST: Env.schema.string({ format: 'host' }),
    })
    
  • improvement: Make existence of .env file optional. The validations should take care of ensuring that all required environment variables exists. 6e3f2851.

  • remove: Depreciate Env.getOrFail method and hence validations should handle the existence of a variable. f555e8a8

Validator

  • improvement: Cache validation schema using LRU cache. The maximum limit is hardcoded to cache a total of 100 schemas (should be enough for majority of applications). f19a6312e3
  • fix: Bug fix for array.anyMembers. The method was never implemented but was documented. 866cb452
  • feat: Expose the validator config to customize certain aspects of the validation. You can define the following object inside the config/app.ts file. 12d116f254

    export const validator = {
      bail: true, // stop after first validation error (default to "false")
      existsStrict: true, // only consider undefined and null as missing values (default to "false")
      reporter: async () => {
        return (await import('@ioc:Adonis/Core/Validator')).validator.reporters.api
      }, // define a custom reporter for all validations
    }
    
  • feat: Allow customizing the negotiator function to decide the error reporter when using request.validate method. This will allow 3rd party packages like InertiaJS to use a custom function for handling validation failures. Here's the default implementation of the negotiator function.

    class MyProvider {
      public boot() {
        const { validator } = this.app.container.use('Adonis/Core/Validator')
        validator.negotiator((request) => {
          // return an error reporter to be used
        })
      }
    }
    

Application

  • addition: Add methods to setup the AdonisJS application. Earlier ignitor was responsible for doing this, but we have moved the core the Application class. This is useful for developing packages, as you can quickly setup an AdonisJS application and test your package against it. 814500b2
  • addition: Introduce additional repl environment. After this, AdonisJS has a total of four environments, i.e: web, console, test, repl.

Logger

  • addition: Add keywords to format the timestamps for the log messages. 95122405. The keywords are iso, unix and epoch and can be used inside the config/app.ts file as follows:

    export const logger = {
      enabled: true,
      timestamp: 'iso'
    }
    

    Alternatively, you can define a custom function as well.

    timestamp: () => `,"time":${Date.now()}`