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 dfd24aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
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
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function factory({
return nc;
}
nc.use = function use(base, ...fns) {
if (Array.isArray(base)) return this.use("/", ...base);
if (typeof base === "function") return this.use("/", base, ...fns);
if (typeof base === "string" && base !== "/") {
let slashAdded = false;
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ describe("use()", () => {
await request(app).get("/some/path").expect("no");
});

it("use accepts an array of middleware", async () => {
const handler = nc();
handler.use([(req, res, next) => {
req.ok1 = "ok1";
next();
},(req, res, next) => {
req.ok2 = "ok2";
next();
}]);
handler.get((req, res) => {
res.end(`${req.ok1}-${req.ok2}` || "no");
});
const app = createServer(handler);
await request(app).get("/some/path").expect("ok1-ok2");
});

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

0 comments on commit dfd24aa

Please sign in to comment.