@saber2pr/next-with-axios

0.0.6 • Public • Published

@saber2pr/next-with-axios

axios plugin for nextjs.

yarn add @saber2pr/next-with-axios
import createAxiosMonad, { fmap } from '@saber2pr/next-with-axios'

const AxiosMonad = createAxiosMonad(async (handler, ctx) => {
  const result: GetServerSidePropsResult<any> = {
    props: {},
  }
  try {
    result.props = await handler(axios, ctx)
  } catch (error) {
    result.props = error
  } finally {
    return result
  }
})

const withRedirect = fmap((withAxios) => (handler, ctx) => {
  const url = ctx.req.url
  if (url === '/index') {
    ctx.res.writeHead(302, {
      Location: '/',
    })
    ctx.res.end()
  }
  return withAxios(handler)(ctx)
})(AxiosMonad)

Author: saber2pr

/@saber2pr/next-with-axios/

    Package Sidebar

    Install

    npm i @saber2pr/next-with-axios

    Weekly Downloads

    1

    Version

    0.0.6

    License

    MIT

    Unpacked Size

    2.53 kB

    Total Files

    6

    Last publish

    Collaborators

    • saber2pr