https://github.com/marcus-rise/next-api-interceptor
Nextjs api interceptor
https://github.com/marcus-rise/next-api-interceptor
api interceptor nextjs serverless
Last synced: about 1 year ago
JSON representation
Nextjs api interceptor
- Host: GitHub
- URL: https://github.com/marcus-rise/next-api-interceptor
- Owner: Marcus-Rise
- Created: 2022-08-24T12:46:50.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-08-24T13:18:10.000Z (over 3 years ago)
- Last Synced: 2025-03-17T17:29:48.155Z (about 1 year ago)
- Topics: api, interceptor, nextjs, serverless
- Language: TypeScript
- Homepage:
- Size: 63.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Next api interceptor
Interceptor for serverless nextjs api
## Install
using `npm`
```bash
npm install @marcus-rise/next-api-interceptor
```
or using `yarn`
```bash
yarn add @marcus-rise/next-api-interceptor
```
## Usage
You can create several interceptors and apply them in train
Interceptor is a wrapper function, that runs before a wrapped function
```tsx
// src/interceptor/method-handlers.interceptor.ts
import type {Interceptor} from "@marcus-rise/next-api-interceptor";
import type {NextApiHandler} from "next";
enum RequestMethod {
GET = "GET",
POST = "POST",
}
type MethodHandler = {
method: RequestMethod | string;
handler: Handler;
};
const withMethodHandlers =
(...allowedHandlers: Array): Interceptor =>
(handler: NextApiHandler = () => {
}) =>
async (req, res) => {
const allowedHandler = allowedHandlers.find((h) => h.method === req.method)?.handler;
if (!allowedHandler) {
res.status(405).end();
} else {
await allowedHandler(req, res);
}
return handler(req, res);
};
export {withMethodHandlers};
```
```ts
// pages/api/test.ts
import type {NextApiHandler} from "next";
import {withMethodHandlers} from "../../src/interceptor/method-handlers.interceptor.ts";
const TestHandler: NextApiHandler = (req, response) => {
return response.status(res.status).json({res: "hellow"});
};
export default withMethodHandlers({
method: "GET",
handler: TestHandler,
})();
// or
export default applyInterceptors(
TestHandler,
anotherInterceptor,
withMethodHandlers({
method: "GET",
handler: TestHandler,
}),
yetAnotherOneInterceptor,
);
```
The execution order of example described above is: anotherInterceptor, withMethodHandlers,
yetAnotherOneInterceptor, TestHandler