1.0.0 • Published 3 years ago

nextware v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

💛 You can help the author become a full-time open-source maintainer by sponsoring him on GitHub.


nextware

npm version

Install

npm i nextware

Usage

import { nextware, Middleware } from 'nextware'

type Props = {
  user: {
    id: number
    name: string
  }
}

const requireAuth: Middleware<Props> = async (ctx) => {
  const user = await db.user.find(ctx.params.username)
  if (!user) {
    return ctx.redirect(`/login`)
  }
  ctx.setProp('user', user)
}

export const getServerSideProps = (ctx) => {
  return nextware<Props>().use(requireAuth).run(ctx)
}

export default function Page({ user }: Props) {
  return <div>{user.name}</div>
}

Middleware

The middleware function receives a single argument ctx which is an object containing following properties:

  • req: http.IncomingRequest
  • res: http.ServerResponse
  • params: Route params.
  • getProp(key): Get a page prop.
  • setProp(key, value): Set a page prop.
  • redirect(url): Redirect to a url.

To break the middleware chain, you can return false in the middleware function.

License

MIT © EGOIST