npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hambergerpls/elysia-filesystemrouter

v0.1.3

Published

An experimental filesystem router plugin for [Elysia](https://github.com/elysiajs/elysia). Major breaking change may be introduced. Not recommended for production use.

Downloads

4

Readme

elysia-filesystemrouter

An experimental filesystem router plugin for Elysia. Major breaking change may be introduced. Not recommended for production use.

Installation

bun install @hambergerpls/elysia-filesystemrouter

Usage

  1. Create a router directory.
/.gitignore <-- Can add *.g.ts to ignore generated files
/src
  /router
    /index.ts <-- /
    /users 
        /index.ts <-- /users
        /[id] 
            /index.ts <-- /users/:id
            /posts
                /index.ts <-- /users/:id/posts
    /posts
        /[catch-all].ts <-- /posts/*
...

/package.json
/tsconfig.json
  1. Instantiate an Elysia instance with the plugin.
// src/main.ts
import { Elysia } from "elysia";
import { fileSystemRouter } from "elysia-filesystemrouter";

// We export app so that we can use it in our handlers for autocompletions and type checking
export const app = new Elysia()
                .decorate('hi', () => 'hi') // Add decorators, top-level hooks, etc.

// Must be separated to initialize app first
app.use(await fileSystemRouter({
    dir: "./src/router"
    }))
    .listen(3000)
  1. Inside index.ts/[wildcard].ts files, export desired handlers.
// src/router/index.ts
import { app } from "src/main.ts";
import { Handler } from "./index.g"; // Handler will be generated by the plugin after running bun --watch src/main.ts

// Creates a handler for all methods
export const onRequest = Handler(app, async ({ path, params }) => `Hello from ${path} with method ${request.method}`)

// Creates a handler for GET method with local hooks
export const onRequestGet = Handler(app, async ({ path, request }) => `Hello from ${path} with method ${request.method}`, {
    afterHandle(context) {
        return 'Hi from local afterHandle'
    }
})

// Creates a handler for POST method with validation
export const onRequestPost = Handler(app, async ({ query: { id } }) => id.toString(), {
    body: t.Object({
        username: t.String(),
        password: t.String()
    }),
    query: t.Object({
        id: t.String()
    }),
    response: {
        200: t.String()
    }
})

// Creates a handler for PUT method with access to decorators
export const onRequestPut = Handler(app, async ({ path, request, hi }) => hi())

// Creates a handler for PATCH method
export const onRequestPatch = Handler(app, async ({ path, request }) => `Hello from ${path} with method ${request.method}`)

// Creates a handler for DELETE method
export const onRequestDelete = Handler(app, async ({ path, request }) => `Hello from ${path} with method ${request.method}`)
  1. Run the server.
bun --watch src/main.ts
  1. Send a request to the server.
GET http://localhost:3000/ # Hello from / with method GET
  1. ???
  2. Profit

How it works

The plugin generates a handler function for each endpoint to allow autocompletion and type checking in a file that ends with .g.ts in their respective routes. The end goal is to allow developers to create endpoints using file based routing without sacrificing type safety and autocompletion and other features of Elysia.