-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(inertia-sails): remove location and render method implementations * feat(inertia-sails): remove unneeded methos and have sharedProps as a property in sails.inertia * feat(mellow-vue): add inertia and inertiaRedirect responses * feat(mellow-vue): replace calls to sails.inertia.render with inertia custom response and exit * feat(mellow-vue): replace call to sails.inertia.location with the inertiaRedirect custom response
- Loading branch information
1 parent
60a3721
commit cbf1d46
Showing
12 changed files
with
146 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// @ts-nocheck | ||
const { encode } = require('querystring') | ||
module.exports = function inertia(data) { | ||
const req = this.req | ||
const res = this.res | ||
const sails = req._sails | ||
|
||
const sharedProps = sails.inertia.sharedProps | ||
const sharedViewData = sails.inertia.sharedViewData | ||
const rootView = sails.config.inertia.rootView | ||
|
||
const allProps = { | ||
...sharedProps, | ||
...data.props | ||
} | ||
|
||
const allViewData = { | ||
...sharedViewData, | ||
...data.viewData | ||
} | ||
|
||
let url = req.url || req.originalUrl | ||
const assetVersion = sails.config.inertia.version | ||
const currentVersion = | ||
typeof assetVersion === 'function' ? assetVersion() : assetVersion | ||
|
||
const page = { | ||
component: data.page, | ||
version: currentVersion, | ||
props: allProps, | ||
url | ||
} | ||
|
||
// Implements inertia partial reload. See https://inertiajs.com/partial-reload | ||
if ( | ||
req.get(inertiaHeaders.PARTIAL_DATA) && | ||
req.get(inertiaHeaders.PARTIAL_COMPONENT) === page.component | ||
) { | ||
const only = req.get(inertiaHeaders.PARTIAL_DATA).split(',') | ||
page.props = only.length ? getPartialData(data.props, only) : page.props | ||
} | ||
|
||
const queryParams = req.query | ||
if (req.method === 'GET' && Object.keys(queryParams).length) { | ||
// Keep original request query params | ||
url += `?${encode(queryParams)}` | ||
} | ||
|
||
if (req.get(inertiaHeaders.INERTIA)) { | ||
res.set(inertiaHeaders.INERTIA, true) | ||
res.set('Vary', 'Accept') | ||
return res.json(page) | ||
} else { | ||
// Implements full page reload | ||
return res.view(rootView, { | ||
page, | ||
viewData: allViewData | ||
}) | ||
} | ||
} | ||
|
||
function getPartialData(props, only = []) { | ||
return Object.assign({}, ...only.map((key) => ({ [key]: props[key] }))) | ||
} | ||
|
||
const inertiaHeaders = { | ||
INERTIA: 'X-Inertia', | ||
VERSION: 'X-Inertia-Version', | ||
PARTIAL_DATA: 'X-Inertia-Partial-Data', | ||
PARTIAL_COMPONENT: 'X-Inertia-Partial-Component', | ||
LOCATION: 'X-Inertia-Location' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @ts-nocheck | ||
|
||
const inertiaHeaders = { | ||
INERTIA: 'X-Inertia', | ||
LOCATION: 'X-Inertia-Location' | ||
} | ||
|
||
module.exports = function inertiaRedirect(url) { | ||
const req = this.req | ||
const res = this.res | ||
|
||
if (req.get(inertiaHeaders.INERTIA)) { | ||
res.set(inertiaHeaders.LOCATION, url) | ||
} | ||
|
||
return res.redirect( | ||
['PUT', 'PATCH', 'DELETE'].includes(req.method) ? 303 : 409, | ||
url | ||
) | ||
} |