Skip to content

Commit b42c537

Browse files
authored
fix(#161): use() accepts array
1 parent 89196b1 commit b42c537

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ declare module "next-connect" {
3333
interface NextConnect<Req, Res> {
3434
(req: Req, res: Res): Promise<void>;
3535

36+
use<ReqExt = {}, ResExt = {}>(
37+
handlers: Middleware<Req & ReqExt, Res & ResExt>[]
38+
): this;
3639
use<ReqExt = {}, ResExt = {}>(
3740
...handlers: Middleware<Req & ReqExt, Res & ResExt>[]
3841
): this;

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export default function factory({
3535
return nc;
3636
}
3737
nc.use = function use(base, ...fns) {
38-
if (typeof base === "function") return this.use("/", base, ...fns);
38+
if (Array.isArray(base)) return this.use("/", ...base);
39+
if (Array.isArray(base) || typeof base === "function") return this.use("/", base, ...fns);
3940
if (typeof base === "string" && base !== "/") {
4041
let slashAdded = false;
4142
fns.unshift((req, _, next) => {

test/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,24 @@ describe("use()", () => {
294294
await request(app).get("/some/path").expect("no");
295295
});
296296

297+
it("match path by array of RegExp and base", async () => {
298+
const handler = nc();
299+
handler.use([new RegExp("/this|/that"), '/the/other/'], (req, res, next) => {
300+
req.ok = "ok";
301+
next();
302+
});
303+
handler.get((req, res) => {
304+
res.end(req.ok || "no");
305+
});
306+
const app = createServer(handler);
307+
await request(app).get("/this/that/these/those").expect("ok");
308+
await request(app).get("/this").expect("ok");
309+
await request(app).get("/that/this/these/those").expect("ok");
310+
await request(app).get("/that").expect("ok");
311+
await request(app).get("/some/path").expect("no");
312+
await request(app).get("/the/other/path").expect("ok");
313+
});
314+
297315
it("mount subapp", () => {
298316
const handler2 = nc();
299317
handler2.use((req, res, next) => {

0 commit comments

Comments
 (0)