Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Alexander Savelyev <vordgi1@gmail.com>
Copyright (c) 2024 Alex Savelyev <dev@alexdln.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
51 changes: 35 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# @nimpl/middleware-chain
# @nimpl/proxy-chain

The package allows you to create a chain of native next.js middlewares without any modifications (_i.e., you can add any ready-made middleware to the chain_)
The package allows you to create a chain of native next.js proxies without any modifications (_i.e., you can add any ready-made proxy to the chain_)

```ts
// middleware.ts
import { chain } from "@nimpl/middleware-chain";
```ts filename="proxy.ts"
// proxy.ts
import { chain } from "@nimpl/proxy-chain";

export default chain([
[intlMiddleware, { exclude: /^\/private(\/.*)?$/ }],
authMiddleware,
[intlProxy, { exclude: /^\/private(\/.*)?$/ }],
authProxy,
(req, event) => {
event.waitUntil(
fetch("https://my-analytics-platform.com", {
Expand All @@ -18,29 +18,47 @@ export default chain([
);
return NextResponse.next();
},
customMiddleware,
customProxy,
]);
```

Visit https://nimpl.dev/docs/middleware-chain to view the full documentation.
Visit https://nimpl.dev/docs/proxy-chain to view the full documentation.

## Compatibility

The utility is fully compatible with **all versions of Next.js** that support middleware/proxy.

If you're using a version **below 16.0.0**, create a `middleware.ts` instead of `proxy.ts` file:

```ts filename="middleware.ts"
import { chain } from "@nimpl/proxy-chain";
import { NextRequest, NextResponse, NextFetchEvent } from "next/types";

export const middleware = chain<NextRequest, NextResponse, NextFetchEvent>([
// ...
]);
```

> [!TIP]
> Explicitly pass the types NextRequest, NextResponse, and NextFetchEvent as generics to the chain function to ensure proper type safety and autocompletion.

## Installation

**Using npm:**

```bash
npm i @nimpl/middleware-chain
npm i @nimpl/proxy-chain
```

**Using yarn:**

```bash
yarn add @nimpl/middleware-chain
yarn add @nimpl/proxy-chain
```

## Motivation

All existing solutions work through their own APIs - made under the style of express or in their own vision. They are useful, well implemented, and convenient. But only in cases where you can update every used middleware.
All existing solutions work through their own APIs - made under the style of express or in their own vision. They are useful, well implemented, and convenient. But only in cases where you can update every used proxy.

However, there are many situations where you need to add already prepared solutions. Usually, in the issues of these solutions, you can find “support to add a chain package A, working with chain package B”.

Expand All @@ -50,12 +68,13 @@ This is not Koa and not Express, this is a package for next.js, in its unique st

## Examples

- [Base example](https://github.com/vordgi/nimpl-middleware-chain/tree/main/examples/base).
- [next-auth + next-intl example](https://github.com/vordgi/nimpl-middleware-chain/tree/main/examples/auth-intl).
- [Base example](https://github.com/alexdln/nimpl-proxy-chain/tree/main/examples/base).
- [next-auth + next-intl example](https://github.com/alexdln/nimpl-proxy-chain/tree/main/examples/auth-intl).
- [next-auth5 + next-intl example](https://github.com/alexdln/nimpl-proxy-chain/tree/main/examples/auth5-intl).

## Development

Read about working on the package and making changes on the contribution page
Read about working on the package and making changes on the [contribution page](https://nimpl.dev/contribution)

## Additional

Expand All @@ -65,4 +84,4 @@ Create issues with wishes, ideas, difficulties, etc. All of them will definitely

## License

[MIT](https://github.com/vordgi/nimpl-middleware-chain/blob/main/LICENSE)
[MIT](https://github.com/alexdln/nimpl-proxy-chain/blob/main/LICENSE)
5 changes: 3 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended";
import tseslint from "typescript-eslint";

const ignores = ["**/node_modules/**", "**/dist/**", "**/*.js", "**/*.d.ts"];
const ignores = ["**/node_modules/**", "**/dist/**", "**/.next/**", "**/*.js", "**/*.d.ts"];

export default [
{
Expand All @@ -19,4 +19,5 @@ export default [
},
...tseslint.configs.recommended,
eslintPluginPrettierRecommended,
].map((r) => Object.assign(r, { ignores }));
{ ignores },
];
1 change: 1 addition & 0 deletions examples/auth-intl/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/.pnp
.pnp.js
.yarn/install-state.gz
pnpm-lock.yaml

# testing
/coverage
Expand Down
19 changes: 9 additions & 10 deletions examples/auth-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"start": "next start"
},
"dependencies": {
"@nimpl/middleware-chain": "0.5.2",
"next": "14.2.3",
"@nimpl/proxy-chain": "latest",
"next": "16.0.0",
"next-auth": "4.24.7",
"next-intl": "3.14.1",
"react": "^18",
"react-dom": "^18"
"react": "19.2.0",
"react-dom": "19.2.0"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"typescript": "^5"
"@types/node": "24.9.1",
"@types/react": "19.2.2",
"@types/react-dom": "19.2.2",
"typescript": "5.9.3"
}
}
Loading