Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middlewares Pattern #4

Closed
Abiti-936 opened this issue Sep 7, 2022 · 5 comments
Closed

Middlewares Pattern #4

Abiti-936 opened this issue Sep 7, 2022 · 5 comments

Comments

@Abiti-936
Copy link

Abiti-936 commented Sep 7, 2022

I liked these Routing Library & I am now using it with My Supabase Edge Functions, & it's just a great library.

One, thing that is cool to have is middlewares pattern, to Add CORS Header, Check for Authorization Headers & more... before & after Handling Routes.

For Example, The Pattern can be like these :-

import { createRouter } from "https://deno.land/x/http_router/mod.ts";
import { serve } from "https://deno.land/std/http/mod.ts";
import cors from 'https://deno.land/x/edge_cors/src/cors.ts';

const router = createRouter({
  "/*": async (request, { next }) => {
    const response = await next(); // invokes the next route

    return cors(request, response); // set CORS Headers to the response
  },
  "/hello": (request, _context) => {
    return new Response.json({ hello: "world" });
  }
});

await serve(router);

I think these can make a whole lot of use cases possible. Thanks.

@TomokiMiyauci
Copy link
Collaborator

@Abiti-936
We are currently looking into supporting nested routes.

One idea is to compose a createRouter.
For example, we aim to be able to do the following:

import cors from "https://deno.land/x/edge_cors/src/cors.ts";
import { createRouter } from "https://deno.land/x/http_router/mod.ts";

const api = createRouter({
  "/hello": () => new Response("hello"),
});
const router = createRouter({
  "/api": cors(api),
});
function log(handler: (req: Request) => Promise<Response> | Response) {
  return async (req: Request) => {
    const res = await handler(req);
    console.log({
      req, res,
    });
    return res;
  };
}
await Deno.serve(log(router));

If the router is self-recursive, it is divisible and most expressive and beautiful.

However, it requires some ingenuity in implementation because of the need to perform URL pattern matching.


The middleware pattern is difficult to formulate a standard API.

In fact, middleware was discussed in Deno, but is not currently implemented for the same reason. denoland/std#1295

Also, createRouter aims to be a stateless function.
Therefore, we do not plan to add methods such as use.

Any thoughts on these?

@Abiti-936
Copy link
Author

Abiti-936 commented Sep 8, 2022

I mean, I don't expect you to add the use method, but you can add middlewares pattern like /*, /api/*, or something like that. Personally I don't like the use method that we can find in Oak & similar frameworks, but I like the Netlify Edge Functions Middlewares Pattern.

Also the Upper Implementation is Just Really Good.

@Abiti-936
Copy link
Author

Abiti-936 commented Sep 8, 2022

I have tried the Upper log example by changing it cors & it's working.

import { createRouter } from "https://deno.land/x/http_router/mod.ts"; 
import edgeCors from "https://deno.land/x/edge_cors@0.2.1/src/cors.ts"; 
import { options } from "./corsOptions.ts";

const router = createRouter({ 
  "/hello": () => new Response("hello")
}); 

export function cors(handler(requestRequest) => Promise<Response> | Response) { 
   return async (requestRequest) => { 
     const response = await handler(request); 
  
     return edgeCors(request, response, options); 
   }; 
 }

await Deno.serve(cors(router));

Thanks.

@TomokiMiyauci
Copy link
Collaborator

@Abiti-936
I have released v1.2.

A number of important features have been added, including nested route and base path.

https://github.com/httpland/http-router/releases/tag/1.2.0

@Abiti-936
Copy link
Author

That's Great. I will take a look at the new release. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants