Skip to content

Commit

Permalink
fix: only share errors and flash messages when it's an inertia request
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Feb 28, 2024
1 parent 6bdf025 commit e7b11bf
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions inertia-sails/private/inertia-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@ const getPartialData = require('./get-partial-data')
const resolveValidationErrors = require('./resolve-validation-errors')
function inertia(sails, { hook, sharedProps, sharedViewData, rootView }) {
return function inertiaMiddleware(req, res, next) {
/**
* Flash messages stored in the session.
* @typedef {Object} FlashMessages
* @property {Array} message - Flash message(s).
* @property {Array} error - Error message(s).
* @property {Array} success - Success message(s).
*/
const flash = {
message: req.flash('message'),
error: req.flash('error'),
success: req.flash('success')
}
hook.share('flash', flash) // Share the flash object as props
/**
* Validation errors stored in the session, resolved and formatted for Inertia.js.
* @type {Object}
*/
const validationErrors = resolveValidationErrors(req)
req.flash('errors', validationErrors) // Flash the validation error so we can share it later
if (isInertiaRequest(req)) {
/**
* Flash messages stored in the session.
* @typedef {Object} FlashMessages
* @property {Array} message - Flash message(s).
* @property {Array} error - Error message(s).
* @property {Array} success - Success message(s).
*/
const flash = {
message: req.flash('message'),
error: req.flash('error'),
success: req.flash('success')
}
hook.share('flash', flash) // Share the flash object as props
/**
* Validation errors stored in the session, resolved and formatted for Inertia.js.
* @type {Object}
*/
const validationErrors = resolveValidationErrors(req)
req.flash('errors', validationErrors) // Flash the validation error so we can share it later

hook.share('errors', req.flash('errors')[0] || {}) // Share validation errors as props
hook.share('errors', req.flash('errors')[0] || {}) // Share validation errors as props
}

hook.render = function (component, props = {}, viewData = {}) {
const allProps = {
Expand Down

0 comments on commit e7b11bf

Please sign in to comment.