Discord bot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.ts 1.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. export { default as Form } from './Form'
  2. // for noscript form submissions
  3. export const forActionRoute = (fn) => async (req, res) => {
  4. const { redirect, body, status, title, } = await fn(req, res)
  5. if (redirect) {
  6. res.redirect(redirect.url.toString())
  7. return
  8. }
  9. res.status(status)
  10. res.statusMessage = title
  11. res.json(body.data)
  12. }
  13. // for fetching data for SSR of pages
  14. export const forGetServerSideProps = (fn) => async (ctx) => {
  15. const { redirect, body, status, } = await fn(ctx)
  16. if (redirect) {
  17. return {
  18. redirect: {
  19. destination: redirect.url.toString(),
  20. permanent: Boolean(redirect.permanent),
  21. },
  22. props: body.data,
  23. }
  24. }
  25. if (status === 404) {
  26. return {
  27. notFound: true,
  28. props: body.data,
  29. }
  30. }
  31. return {
  32. props: body.data,
  33. }
  34. }
  35. // for handling of AJAX (fetch) requests from clients
  36. export const forResourceRoute = (fn) => async (req, res) => {
  37. const { body, status, title, } = await fn(req) || {
  38. status: 405,
  39. }
  40. res.status(status)
  41. if (title) {
  42. res.statusMessage = title
  43. }
  44. if (body) {
  45. res.json(body)
  46. return
  47. }
  48. res.end('')
  49. }
  50. // for form submit handlers
  51. export const forClientSideState = ({ router, ...state }) => (fn) => async (e) => {
  52. const { redirect, status, } = await fn(state)
  53. if (redirect) {
  54. if (redirect.permanent) {
  55. router.replace(redirect.url)
  56. } else {
  57. router.push(redirect.url)
  58. }
  59. return
  60. }
  61. if (status > 299) {
  62. // noop
  63. return
  64. }
  65. }