|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- export { default as Form } from './Form'
-
- // for noscript form submissions
- export const forActionRoute = (fn) => async (req, res) => {
- const { redirect, body, status, title, } = await fn(req, res)
- if (redirect) {
- res.redirect(redirect.url.toString())
- return
- }
-
- res.status(status)
- res.statusMessage = title
- res.json(body.data)
- }
-
- // for fetching data for SSR of pages
- export const forGetServerSideProps = (fn) => async (ctx) => {
- const { redirect, body, status, } = await fn(ctx)
- if (redirect) {
- return {
- redirect: {
- destination: redirect.url.toString(),
- permanent: Boolean(redirect.permanent),
- },
- props: body.data,
- }
- }
-
- if (status === 404) {
- return {
- notFound: true,
- props: body.data,
- }
- }
-
- return {
- props: body.data,
- }
- }
-
- // for handling of AJAX (fetch) requests from clients
- export const forResourceRoute = (fn) => async (req, res) => {
- const { body, status, title, } = await fn(req) || {
- status: 405,
- }
- res.status(status)
- if (title) {
- res.statusMessage = title
- }
- if (body) {
- res.json(body)
- return
- }
- res.end('')
- }
-
- // for form submit handlers
- export const forClientSideState = ({ router, ...state }) => (fn) => async (e) => {
- const { redirect, status, } = await fn(state)
- if (redirect) {
- if (redirect.permanent) {
- router.replace(redirect.url)
- } else {
- router.push(redirect.url)
- }
- return
- }
-
- if (status > 299) {
- // noop
- return
- }
- }
-
|