0.2.2 • Published 5 months ago
next-compose-middlewares v0.2.2
next-compose-middlewares
- Using koa style middlewares inside nextjs
- Unified request/response context(express api) across Page and Route/Action
- SetCookie/clearCookie both inside Page and Route/Action
- Easily access request/response context between components inside Page and functions inside Route/Action
demo
pnpm i
npm run dev
docs
Usage
nextjs middleware
src/middleware.ts
import { createMiddleware } from 'next-compose-middlewares/middleware';
export const middleware = createMiddleware();
export const config = {
matcher: '/((?!_next|favicon.ico|sitemap.xml|robots.txt).*)',
};
extends type
declare module 'next-compose-middlewares' {
interface NextContext {
user: string;
}
}
page
src/app/page.tsx
import React from 'react';
import { withPageMiddlewares, getNextContext } from 'next-compose-middlewares';
export default withPageMiddlewares([
async (context, next) => {
context.user = 'test';
await next();
}])(
async () => {
const { user } = getNextContext();
return (
<>
<p>{user}</p>
</>
);
},
);
action
src/action/getUser.ts
import { withActionMiddlewares, getNextContext } from 'next-compose-middlewares';
export default withActionMiddlewares([
async (context, next) => {
context.user = 'test';
await next();
}])(
async () => {
const { user } = getNextContext();
return user;
},
);
route
src/app/get/route.ts
import { withRouteMiddlewares,getNextContext } from 'next-compose-middlewares';
export const GET = withRouteMiddlewares([
async (context, next) => {
context.user = 'test';
await next();
}])(
async () => {
const { user, res } = getNextContext();
res.json({ user });
},
);
nginx
location /rewrite {
proxy_set_header X-Forwarded-URI $request_uri;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:3000/dynamic;
proxy_http_version 1.1;
# Disable buffering for streaming support
proxy_buffering off;
proxy_set_header X-Accel-Buffering no;
}
0.2.1
6 months ago
0.1.2
8 months ago
0.2.0
6 months ago
0.1.8
6 months ago
0.1.7
6 months ago
0.1.9
6 months ago
0.1.4
8 months ago
0.2.2
5 months ago
0.1.3
8 months ago
0.1.6
7 months ago
0.1.5
8 months ago
0.1.1
8 months ago
0.1.0
9 months ago
0.0.20
1 year ago
0.0.21
1 year ago
0.0.22
1 year ago
0.0.23
1 year ago
0.0.24
1 year ago
0.0.25
12 months ago
0.0.15
1 year ago
0.0.16
1 year ago
0.0.17
1 year ago
0.0.18
1 year ago
0.0.19
1 year ago
0.0.30
11 months ago
0.0.31
11 months ago
0.0.32
11 months ago
0.0.10
1 year ago
0.0.33
11 months ago
0.0.11
1 year ago
0.0.12
1 year ago
0.0.13
1 year ago
0.0.14
1 year ago
0.0.26
12 months ago
0.0.9
1 year ago
0.0.27
12 months ago
0.0.8
1 year ago
0.0.28
11 months ago
0.0.29
11 months ago
0.0.5
1 year ago
0.0.7
1 year ago
0.0.6
1 year ago
0.0.4
2 years ago
0.0.3
2 years ago
0.0.2
2 years ago
0.0.1
2 years ago