0.2.2 • Published 1 year 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 devdocs
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
1 year ago
0.1.2
2 years ago
0.2.0
1 year ago
0.1.8
1 year ago
0.1.7
1 year ago
0.1.9
1 year ago
0.1.4
2 years ago
0.2.2
1 year ago
0.1.3
2 years ago
0.1.6
1 year ago
0.1.5
2 years ago
0.1.1
2 years ago
0.1.0
2 years ago
0.0.20
2 years ago
0.0.21
2 years ago
0.0.22
2 years ago
0.0.23
2 years ago
0.0.24
2 years ago
0.0.25
2 years ago
0.0.15
2 years ago
0.0.16
2 years ago
0.0.17
2 years ago
0.0.18
2 years ago
0.0.19
2 years ago
0.0.30
2 years ago
0.0.31
2 years ago
0.0.32
2 years ago
0.0.10
2 years ago
0.0.33
2 years ago
0.0.11
2 years ago
0.0.12
2 years ago
0.0.13
2 years ago
0.0.14
2 years ago
0.0.26
2 years ago
0.0.9
2 years ago
0.0.27
2 years ago
0.0.8
2 years ago
0.0.28
2 years ago
0.0.29
2 years ago
0.0.5
2 years ago
0.0.7
2 years ago
0.0.6
2 years ago
0.0.4
3 years ago
0.0.3
3 years ago
0.0.2
3 years ago
0.0.1
3 years ago