fixes handlers not being compatible with deno handlers
This commit is contained in:
parent
05831b364b
commit
67ddb71fdd
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@bearmetal/router",
|
"name": "@bearmetal/router",
|
||||||
"description": "A simple router for Deno",
|
"description": "A simple router for Deno",
|
||||||
"version": "0.1.0",
|
"version": "0.1.1",
|
||||||
"stable": true,
|
"stable": true,
|
||||||
"repository": "https://github.com/emmaos/bearmetal",
|
"repository": "https://github.com/emmaos/bearmetal",
|
||||||
"files": [
|
"files": [
|
||||||
|
@ -49,7 +49,7 @@ describe("Router", () => {
|
|||||||
describe("Route Parameters", () => {
|
describe("Route Parameters", () => {
|
||||||
it("should handle route parameters", async () => {
|
it("should handle route parameters", async () => {
|
||||||
router.route("/users/:id")
|
router.route("/users/:id")
|
||||||
.get(async (ctx) => new Response(ctx.params.id));
|
.get(async (_, ctx) => new Response(ctx.params.id));
|
||||||
|
|
||||||
const req = new Request("http://localhost/users/123", {
|
const req = new Request("http://localhost/users/123", {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@ -61,7 +61,7 @@ describe("Router", () => {
|
|||||||
|
|
||||||
it("should handle multiple route parameters", async () => {
|
it("should handle multiple route parameters", async () => {
|
||||||
router.route("/users/:userId/posts/:postId")
|
router.route("/users/:userId/posts/:postId")
|
||||||
.get(async (ctx) => {
|
.get(async (_, ctx) => {
|
||||||
return new Response(JSON.stringify(ctx.params));
|
return new Response(JSON.stringify(ctx.params));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -182,7 +182,7 @@ describe("Router", () => {
|
|||||||
it("should handle nested routes with parameters", async () => {
|
it("should handle nested routes with parameters", async () => {
|
||||||
const apiRouter = new Router();
|
const apiRouter = new Router();
|
||||||
apiRouter.route("/users/:id")
|
apiRouter.route("/users/:id")
|
||||||
.get(async (ctx) => new Response(ctx.params.id));
|
.get(async (_, ctx) => new Response(ctx.params.id));
|
||||||
|
|
||||||
router.use("/api/:version", apiRouter);
|
router.use("/api/:version", apiRouter);
|
||||||
|
|
||||||
@ -225,7 +225,7 @@ describe("Router", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.route("/test")
|
router.route("/test")
|
||||||
.get(async (ctx) => {
|
.get(async (_, ctx) => {
|
||||||
return new Response(ctx.state.test as string);
|
return new Response(ctx.state.test as string);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ interface RouterContext {
|
|||||||
request: Request;
|
request: Request;
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handler = (ctx: RouterContext) => Promise<Response> | Response;
|
type Handler = (req: Request, ctx: RouterContext) => Promise<Response> | Response;
|
||||||
type Middleware = (ctx: RouterContext, next: () => Promise<Response>) => Promise<Response>;
|
type Middleware = (ctx: RouterContext, next: () => Promise<Response>) => Promise<Response>;
|
||||||
|
|
||||||
interface RouteConfig {
|
interface RouteConfig {
|
||||||
@ -247,7 +247,7 @@ export class Router {
|
|||||||
return await middleware.handler(middlewareCtx, executeMiddleware);
|
return await middleware.handler(middlewareCtx, executeMiddleware);
|
||||||
}
|
}
|
||||||
// Final handler gets the accumulated parameters
|
// Final handler gets the accumulated parameters
|
||||||
return await handler(baseCtx);
|
return await handler(req, baseCtx);
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user