Skip to content

Saber2pr/next-with-axios

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@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)
})

const withAxios = withRedirect(AxiosMonad)

export const getServerSideProps = withAxios<Props>(async ({ get }, ctx) => {
  const id = ctx.query.id
  const apiRes = await get('xxx', {
    params: { id },
  })
  return {
    data: apiRes.data,
  }
})

Author: saber2pr

About

axios plugin for nextjs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published