1.0.6 • Published 5 months ago

webster-router v1.0.6

Weekly downloads
-
License
-
Repository
-
Last release
5 months ago

Webster Router

An easy-to-use, file-based router to Elysia

Usage

Register Plugin

To use Webster in your Elysia app, register it on your Elysia instance.

import { Elysia } from "elysia";
import { webster } from "webster-router";

const app = new Elysia()
  .use(
    webster({
      dir: "./src/my-routes", // uses src/routes by default
    })
  )
  .listen(3000);

Creating a route

// routes/index.ts
export const get = {
  handler: () => "Hello, Webster!",
};

Routes are defined as variables whose names are HTTP verbs. This means that one route file can handle multiple methods

export const get = {
  handler: () => "Hello, Webster!",
};

export const delete = {
  handler: () => "Bye, Webster. :(",
};

Each route is passed an Elysia Context

export const get = {
  handler: (context) => context.path,
};

To define a schema, add a schema property to a method object, and define the schema the same way you would in a normal Elysia app

import { t } from 'elysia'

export const get = {
  schema: {
    query: t.Object({
      name: t.String()
    })
  }
  handler: (context) => context.path,
};

Dynamic Routes

To create a dynamic route, create a folder with the :<parameter-name> format. For example, if we want to create a path that takes in an id parameter, we'd create a folder called :id. Elysia will put the id value in context.params

export const get = {
  handler: ({ params }) => {
    return params.id;
  },
};

Now, if we visit /123, we will see '123' returned back to us.

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago