Skip to content

Commit

Permalink
fix(#161): use() accepts array
Browse files Browse the repository at this point in the history
  • Loading branch information
jimisaacs authored Apr 10, 2022
1 parent 89196b1 commit b42c537
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ declare module "next-connect" {
interface NextConnect<Req, Res> {
(req: Req, res: Res): Promise<void>;

use<ReqExt = {}, ResExt = {}>(
handlers: Middleware<Req & ReqExt, Res & ResExt>[]
): this;
use<ReqExt = {}, ResExt = {}>(
...handlers: Middleware<Req & ReqExt, Res & ResExt>[]
): this;
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default function factory({
return nc;
}
nc.use = function use(base, ...fns) {
if (typeof base === "function") return this.use("/", base, ...fns);
if (Array.isArray(base)) return this.use("/", ...base);
if (Array.isArray(base) || typeof base === "function") return this.use("/", base, ...fns);
if (typeof base === "string" && base !== "/") {
let slashAdded = false;
fns.unshift((req, _, next) => {
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,24 @@ describe("use()", () => {
await request(app).get("/some/path").expect("no");
});

it("match path by array of RegExp and base", async () => {
const handler = nc();
handler.use([new RegExp("/this|/that"), '/the/other/'], (req, res, next) => {
req.ok = "ok";
next();
});
handler.get((req, res) => {
res.end(req.ok || "no");
});
const app = createServer(handler);
await request(app).get("/this/that/these/those").expect("ok");
await request(app).get("/this").expect("ok");
await request(app).get("/that/this/these/those").expect("ok");
await request(app).get("/that").expect("ok");
await request(app).get("/some/path").expect("no");
await request(app).get("/the/other/path").expect("ok");
});

it("mount subapp", () => {
const handler2 = nc();
handler2.use((req, res, next) => {
Expand Down

0 comments on commit b42c537

Please sign in to comment.