From 60a372182a2c7e7799f054a9ba09682187f6d3ae Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Thu, 29 Feb 2024 11:52:02 +0100 Subject: [PATCH] [feat]: use responses and exits (#87) * feat(inertia-sails): remove location and render method implementations * feat(inertia-sails): remove unneeded methos and have sharedProps as a property in sails.inertia --- inertia-sails/index.js | 30 +++---- inertia-sails/private/inertia-middleware.js | 90 ++------------------- 2 files changed, 18 insertions(+), 102 deletions(-) diff --git a/inertia-sails/index.js b/inertia-sails/index.js index 35e6dfc7..63e51db4 100644 --- a/inertia-sails/index.js +++ b/inertia-sails/index.js @@ -7,9 +7,6 @@ const inertia = require('./private/inertia-middleware') module.exports = function defineInertiaHook(sails) { let hook - let sharedProps = {} - let sharedViewData = {} - let rootView = 'app' const routesToBindInertiaTo = [ 'GET r|^((?![^?]*\\/[^?\\/]+\\.[^?\\/]+(\\?.*)?).)*$|', // (^^Leave out assets) @@ -22,37 +19,36 @@ module.exports = function defineInertiaHook(sails) { return { defaults: { inertia: { + rootView: 'app', version: 1 } }, initialize: async function () { hook = this sails.inertia = hook - + sails.inertia.sharedProps = {} + sails.inertia.sharedViewData = {} sails.on('router:before', function routerBefore() { routesToBindInertiaTo.forEach(function iterator(routeAddress) { - sails.router.bind( - routeAddress, - inertia(sails, { hook, sharedProps, sharedViewData, rootView }) - ) + sails.router.bind(routeAddress, inertia(hook)) }) }) }, - share: (key, value = null) => (sharedProps[key] = value), + share: (key, value = null) => (sails.inertia.sharedProps[key] = value), - getShared: (key = null) => sharedProps[key] ?? sharedProps, + getShared: (key = null) => + sails.inertia.sharedProps[key] ?? sails.inertia.sharedProps, flushShared: (key) => { - key ? delete sharedProps[key] : (sharedProps = {}) + key + ? delete sails.inertia.sharedProps[key] + : (sails.inertia.sharedProps = {}) }, - viewData: (key, value) => (sharedViewData[key] = value), - - getViewData: (key) => sharedViewData[key] ?? sharedViewData, - - setRootView: (newRootView) => (rootView = newRootView), + viewData: (key, value) => (sails.inertia.sharedViewData[key] = value), - getRootView: () => rootView + getViewData: (key) => + sails.inertia.sharedViewData[key] ?? sails.inertia.sharedViewData } } diff --git a/inertia-sails/private/inertia-middleware.js b/inertia-sails/private/inertia-middleware.js index 17e48b28..d2cd9c70 100644 --- a/inertia-sails/private/inertia-middleware.js +++ b/inertia-sails/private/inertia-middleware.js @@ -1,100 +1,20 @@ -const { encode } = require('querystring') const isInertiaRequest = require('./is-inertia-request') -const { - INERTIA, - PARTIAL_DATA, - PARTIAL_COMPONENT -} = require('./inertia-headers') - -const getPartialData = require('./get-partial-data') const resolveValidationErrors = require('./resolve-validation-errors') -function inertia(sails, { hook, sharedProps, sharedViewData, rootView }) { +function inertia(hook) { return function inertiaMiddleware(req, res, next) { 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.render = function (component, props = {}, viewData = {}) { - const allProps = { - ...sharedProps, - ...props - } - - const allViewData = { - ...sharedViewData, - ...viewData - } - - let url = req.url || req.originalUrl - const assetVersion = sails.config.inertia.version - const currentVersion = - typeof assetVersion == 'function' ? assetVersion() : assetVersion - - const page = { - component, - version: currentVersion, - props: allProps, - url - } + hook.share('flash', flash) - // Implements inertia partial reload. See https://inertiajs.com/partial-reload - if (req.get(PARTIAL_DATA) && req.get(PARTIAL_COMPONENT) === component) { - const only = req.get(PARTIAL_DATA).split(',') - page.props = only.length ? getPartialData(props, only) : page.props - } - - const queryParams = req.query - if (req.method == 'GET' && Object.keys(queryParams).length) { - // Keep original request query params - url += `?${encode(queryParams)}` - } + const validationErrors = resolveValidationErrors(req) + req.flash('errors', validationErrors) - if (isInertiaRequest(req)) { - res.set(INERTIA, true) - res.set('Vary', 'Accept') - return res.json(page) - } else { - // Implements full page reload - return sails.hooks.views.render(rootView, { - page, - viewData: allViewData - }) - } - } - /** - * Handle 303 and external redirects - * see https://inertiajs.com/redirects#303-response-code - * @param {string} url - The URL to redirect to. - */ - hook.location = function (url) { - if (isInertiaRequest(req)) { - res.set('X-Inertia-Location', url) - } - return res.redirect( - ['PUT', 'PATCH', 'DELETE'].includes(req.method) ? 303 : 409, - url - ) + hook.share('errors', req.flash('errors')[0] || {}) } return next()