adonis-fullstack-app/start/kernel.js
Romain Lanz 8e46d02ed1
feat: add NoTimestamp trait + ConvertEmptyStringsToNull middleware (#30)
Hey! 👋 

This PR add some middleware and traits by default.

**Middleware**:

- `AllowGuestOnly` throws an exception if you are authenticated.
- `ConvertEmptyStringsToNull` converts empty strings in the request body to `null`

**Trait**:

- `NoTimestamp` removes the need for `created_at` and `updated_at` fields.
- `FormatDate` formats the date following a convention.

**FormatDate**

The convention is:
- Ends with `_at` => DateTime
- Ends with `_on` => Date

You can change the format you use in your UI with two getters.

```js
class MyModel extends Model {
  static get formatDate () { return 'DD.MM.YYYY' }
  static get formatDateTime () { return 'DD.MM.YYYY HH:mm' }
}
```

You can also directly change default values inside the Trait since it is a user Trait and not defined in the core.

<hr>

If this is merged, I'll provide the same PR to the `API` blueprint.
2019-01-02 10:10:56 +01:00

64 lines
1.6 KiB
JavaScript

'use strict'
/** @type {import('@adonisjs/framework/src/Server')} */
const Server = use('Server')
/*
|--------------------------------------------------------------------------
| Global Middleware
|--------------------------------------------------------------------------
|
| Global middleware are executed on each http request only when the routes
| match.
|
*/
const globalMiddleware = [
'Adonis/Middleware/BodyParser',
'Adonis/Middleware/Session',
'Adonis/Middleware/Shield',
'Adonis/Middleware/AuthInit',
'App/Middleware/ConvertEmptyStringsToNull',
]
/*
|--------------------------------------------------------------------------
| Named Middleware
|--------------------------------------------------------------------------
|
| Named middleware is key/value object to conditionally add middleware on
| specific routes or group of routes.
|
| // define
| {
| auth: 'Adonis/Middleware/Auth'
| }
|
| // use
| Route.get().middleware('auth')
|
*/
const namedMiddleware = {
auth: 'Adonis/Middleware/Auth',
guest: 'Adonis/Middleware/AllowGuestOnly'
}
/*
|--------------------------------------------------------------------------
| Server Middleware
|--------------------------------------------------------------------------
|
| Server level middleware are executed even when route for a given URL is
| not registered. Features like `static assets` and `cors` needs better
| control over request lifecycle.
|
*/
const serverMiddleware = [
'Adonis/Middleware/Static',
'Adonis/Middleware/Cors'
]
Server
.registerGlobal(globalMiddleware)
.registerNamed(namedMiddleware)
.use(serverMiddleware)