Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Feb 29, 2024
2 parents 17b8247 + ab35904 commit ec3bea5
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 135 deletions.
30 changes: 13 additions & 17 deletions inertia-sails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion inertia-sails/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inertia-sails",
"version": "0.1.9",
"version": "0.2.0",
"description": "The Sails adapter for Inertia.",
"main": "index.js",
"sails": {
Expand Down
90 changes: 5 additions & 85 deletions inertia-sails/private/inertia-middleware.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "boring-stack",
"version": "0.1.3",
"version": "0.2.0",
"private": "true",
"description": "The Boring JavaScript Stack 🥱 - an opinionated project starter for fullstack JavaScript",
"scripts": {
Expand Down
13 changes: 9 additions & 4 deletions templates/mellow-vue/api/controllers/auth/view-check-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module.exports = {
description: 'Display "Verify email" page.',

exits: {
success: {}
success: {
responseType: 'inertia'
}
},

fn: async function () {
Expand All @@ -14,8 +16,11 @@ module.exports = {
} else {
message = `We sent an email verification link to ${this.req.session.userEmail}`
}
return sails.inertia.render('check-email', {
message
})
return {
page: 'check-email',
props: {
message
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module.exports = {
description: 'Display "Forgot password" page.',

exits: {
success: {}
success: {
responseType: 'inertia'
}
},

fn: async function () {
return sails.inertia.render('forgot-password')
return { page: 'forgot-password' }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module.exports = {
description: 'Display "Link expired" page.',

exits: {
success: {}
success: {
responseType: 'inertia'
}
},

fn: async function () {
return sails.inertia.render('link-expired')
return { page: 'link-expired' }
}
}
6 changes: 4 additions & 2 deletions templates/mellow-vue/api/controllers/auth/view-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module.exports = {
description: 'Display "Login" page.',

exits: {
success: {}
success: {
responseType: 'inertia'
}
},

fn: async function () {
return sails.inertia.render('login')
return { page: 'login' }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module.exports = {
}
},
exits: {
success: {},
success: {
responseType: 'inertia'
},
invalidOrExpiredToken: {
responseType: 'expired',
description: 'The provided token is expired, invalid, or already used up.'
Expand All @@ -26,6 +28,6 @@ module.exports = {
if (!user || user.passwordResetTokenExpiresAt <= Date.now()) {
throw 'invalidOrExpiredToken'
}
return sails.inertia.render('reset-password', { token })
return { page: 'reset-password', props: { token } }
}
}
6 changes: 4 additions & 2 deletions templates/mellow-vue/api/controllers/auth/view-signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module.exports = {

description: 'Display "Signup" page.',
exits: {
success: {}
success: {
responseType: 'inertia'
}
},

fn: async function () {
return sails.inertia.render('signup')
return { page: 'signup' }
}
}
17 changes: 11 additions & 6 deletions templates/mellow-vue/api/controllers/auth/view-success.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ module.exports = {
}
},
exits: {
success: {}
success: {
responseType: 'inertia'
}
},

fn: async function ({ operation }) {
Expand All @@ -24,10 +26,13 @@ module.exports = {
message = 'Password has been successful reset'
pageHeading = 'Password reset successful'
}
return sails.inertia.render('success', {
pageTitle,
pageHeading,
message
})
return {
page: 'success',
props: {
pageTitle,
pageHeading,
message
}
}
}
}
8 changes: 6 additions & 2 deletions templates/mellow-vue/api/controllers/home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ module.exports = {

inputs: {},

exits: {},
exits: {
success: {
responseType: 'inertia'
}
},

fn: async function () {
return sails.inertia.render('index')
return { page: 'index' }
}
}
6 changes: 4 additions & 2 deletions templates/mellow-vue/api/controllers/user/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ module.exports = {
inputs: {},

exits: {
success: {}
success: {
responseType: 'inertiaRedirect'
}
},

fn: async function () {
sails.inertia.flushShared('loggedInUser')
delete this.req.session.userId
return sails.inertia.location('/')
return '/'
}
}
6 changes: 4 additions & 2 deletions templates/mellow-vue/api/controllers/user/view-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module.exports = {
description: 'Display "Profile" page.',

exits: {
success: {}
success: {
responseType: 'inertia'
}
},

fn: async function () {
return sails.inertia.render('profile')
return { page: 'profile' }
}
}
Loading

0 comments on commit ec3bea5

Please sign in to comment.