npm.io
1.0.2 • Published 4 years ago

next-methods

Licence
MIT
Version
1.0.2
Deps
0
Size
6 kB
Vulns
0
Weekly
0

Split next.js handlers by http methods

Installation

npm i next-methods --save

Usage

Usage:

import { withMethodHandler } from 'next-methods';

export default withMethodHandler({
  'PUT': handlePut,
  'GET': handleGet,
);

Example combining with other wrappers:

export default withApiAuthRequired(
  withMethodHandler({
    'PUT': handlePut,
    'GET': handleGet,
  })
);

By default it will return 404 for unhandled methods but you can override the behavior as follows:

export default withMethodHandler({
  'PUT': handlePut,
  'GET': handleGet,
  'other': (req, res) => res.status(400).end()
);

License

MIT 2021 - José F. Romaniello