0.0.3 • Published 2 years ago

@lvindotexe/router v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Astro router

A simple middlebare based astro router, this router can be used as middleware or act as endpoints

Usage

Add fallback routes to your endpoints

//src/[...all].ts
import type { APIRoute } from "astro";

export const GET:APIRoute = () => new Response(null,{status:404})
export const POST:APIRoute = () => new Response(null,{status:404})

Update Astro config to support dynamic endpoints

//astro.config.mjs
import { defineConfig } from 'astro/config';

can be used as endpoints or as middleware

//middleware.ts
const count = 0
const router = new Router().get(
  "/hello",
  () => new Response(JSON.stringify({ message: "world" })),
).post("/count", () => {
  count + 1;
  return new Response(JSON.stringify({ count }));
});

const mainRouter = new Router().use("/", router).get(
  "/",
  () => new Response(JSON.stringify({ message: "world" })),
);

export const onRequest: MiddlewareResponseHandler = (conext, next) =>
  router.build()(conext, next);
0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago