next-api-composer
v1.1.1
Published
A helper utility for composing Next.js API routes with per-method implementations
Downloads
2,870
Readme
Next API Composer
Your implementations of API Routes for your Next.js applications will be much easier with this library.
You have to code the API Routes like:
// api/example.ts
export default async (req, res) => {
if (req.method === 'GET') {
// do something
res.json({ foo: 'bar' });
return;
}
res.setHeader('Allow', 'GET');
if (req.method === 'OPTIONS') {
res.status(204).send(null);
} else {
res.status(405).send(null);
}
};
With this library:
// api/example.ts
export default nextApi({
GET: async (req, res) => {
// do something
res.json({ foo: 'bar' });
},
});
Installation
npm i next-api-composer
Supported Methods
GET
POST
PUT
PATCH
DELETE
HTTP Error Handling
- This library includes various HTTP Error types such as
HTTP400BadRequest
, which extendsRouteError
. RouteError
has a methodsend(res: Sendable)
.- If any errors extending
RouteError
is thrown in the handler, they'll be handled with calling thesend
method of the error. - If any errors other than
RouteError
is thrown in the handler, the handler will emit HTTP 500 error. - For details, see
src/index.ts
.
// api/example.ts
export default nextApi({
GET: async (req, res) => {
// Given, the function authenticate will return false
// if the request does not have a valid token in its HTTP header.
if (!authenticate(req)) {
throw new HTTP401Unauthorized();
}
// do something
res.json({ foo: 'bar' });
},
});