# astro-router

> At the time of writing, Astro SSR does not yet support Middleware. This project is just a simple router that adds middleware to your Astro SSR projects, as well as some utilities like getting params, query params, headers, etc.

Latest version **0.0.1** (published 2022-04-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install astro-router
pnpm add astro-router
yarn add astro-router
bun add astro-router
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2022-04-14 |
| First published | 2022-04-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | passle |

## Links

- npm: https://www.npmjs.com/package/astro-router
- npm.io page: https://npm.io/package/astro-router

## Recent versions

- 0.0.1 (latest) — 2022-04-14

## README

# astro-router

At the time of writing, Astro SSR does not yet support Middleware. This project is just a simple router that adds middleware to your Astro SSR projects, as well as some utilities like getting params, query params, headers, etc.

## Installation

```
npm i -S astro-router
```

## Usage

Import the `router`:
```js
import { router } from 'astro-router';
```

Then export your `get`, `post`, etc:
`/foo/[...all]/index.js`:
```js
export const get = router({
  routes: [
    {
      path: '/foo',
      response: () => new Response(null, {status: 200})
    }
  ]
});
```

## Examples

`/sales/[...all]/index.js`:
```js
import { router } from 'astro-router';
import { auth, logger } from './middleware.js';
import { User, Order } from './db.js';

export const get = router({
  routes: [
    {
      path: '/sales/:user/:order',
      middleware: [logger, auth],
      response({params}) {
        const user = await User.findOne({id: params.user});
        const order = await Order.findOne({id: params.order});

        return new Response(null, {status: 200});
      }
    }
  ]
})
```

`Request: /users/1234?foo=bar`:
```js
export const get = router({
  path: '/users/:id',
  middleware: [logger, auth],
  response: ({params, query}) => {
    console.log(params.id) // '1234'
    console.log(query.foo) // 'bar'
  }
});
```

## Api

### Router

```js
export const get = router({
  /** Routes */
  routes: [
    {
      /** The path to match, supports 'express'-style route params */
      path: '/users/:id',
      middleware: [
        ({
          /** Astro's original `request` object */
          request,
          /** Any query params as object */
          query,
          /** Route params as object */
          params,
          /** Headers as object */
          headers
          /** Url object */
          url,
          /** Next middleware to invoke */
          next
        }) => {
          next();
        }
      ],
      response: ({
        /** Astro's original `request` object */
        request,
        /** Any query params as object */
        query,
        /** Route params as object */
        params,
        /** Headers as object */
        headers
        /** Url object */
        url,
      }) => {
        return new Response(null, {status: 200})
      }
    }
  ],
  /** 
   * Custom fallback in case no routes match 
   * defaults to a 404 response
   */
  fallback: () => new Response(null, {status: 404}),
})
```

---
_Source: https://npm.io/package/astro-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
