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

elysia-autoload

v1.4.0

Published

Plugin for Elysia which autoload all routes in directory and code-generate types for Eden with Bun.build support

Downloads

741

Readme

elysia-autoload

Plugin for Elysia which autoload all routes in directory and code-generate types for Eden with Bun.build support!

Installation

Start new project with create-elysiajs

bun create elysiajs <directory-name>

and select Autoload in plugins

Manual

bun install elysia-autoload

Usage

Register the plugin

import { Elysia } from "elysia";
import { autoload } from "elysia-autoload";

const app = new Elysia().use(await autoload()).listen(3000);

export type ElysiaApp = typeof app;

[!IMPORTANT] We strictly recommend use await when registering plugin

Read more about Lazy-load plugins

Create route

// routes/index.ts
import type { ElysiaApp } from "app";

export default (app: ElysiaApp) => app.get("/", { hello: "world" });

Directory structure

Guide how elysia-autoload match routes

├── app.ts
├── routes
    ├── index.ts // index routes
    ├── posts
        ├── index.ts
        └── [id].ts // dynamic params
    ├── likes
        └── [...].ts // wildcard
    ├── domains
        ├── @[...] // wildcard with @ prefix
            └──index.ts
    ├── frontend
        └──index.tsx // usage of tsx extension
    ├── events
        └──(post).ts // post and get will not be in the link
        └──(get).ts
    └── users.ts
└── package.json
  • /routes/index.ts → /
  • /routes/posts/index.ts → /posts
  • /routes/posts/[id].ts → /posts/:id
  • /routes/users.ts → /users
  • /routes/likes/[...].ts → /likes/*
  • /routes/domains/@[...]/index.ts → /domains/@*
  • /routes/frontend/index.tsx → /frontend
  • /routes/events/(post).ts → /events
  • /routes/events/(get).ts → /events

Options

| Key | Type | Default | Description | | -------- | ------------------------------------------ | ---------------------------------- | ----------------------------------------------------------------------------------- | | pattern? | string | "**/*.{ts,tsx,js,jsx,mjs,cjs}" | Glob patterns | | dir? | string | "./routes" | The folder where routes are located | | prefix? | string | | Prefix for routes | | types? | boolean | Types Options | false | Options to configure type code-generation. if boolean - enables/disables generation | | schema? | Function | | Handler for providing routes guard schema |

Types Options

| Key | Type | Default | Description | | ---------- | ------------------ | ------------------- | --------------------------------------------------------------------------------------- | | output? | string | string[] | "./routes-types.ts" | Type code-generation output. It can be an array | | typeName? | string | "Routes" | Name for code-generated global type for Eden | | useExport? | boolean | false | Use export instead of global type |

Usage of types code-generation for Eden

// app.ts
import { Elysia } from "elysia";
import { autoload } from "elysia-autoload";

const app = new Elysia()
    .use(
        await autoload({
            types: {
                output: "./routes.ts",
                typeName: "Routes",
            }, // or pass true for use default params
        })
    )
    .listen(3000);

export type ElysiaApp = typeof app;
// client.ts

import { treaty } from "@elysiajs/eden";

// Routes are a global type so you don't need to import it.

const app = treaty<Routes>("http://localhost:3002");

const { data } = await app.test["some-path-param"].get({
    query: {
        key: 2,
    },
});

console.log(data);

routes.ts will be:

// @filename: routes.ts

import type { ElysiaWithBaseUrl } from "elysia-autoload";
import type Route0 from "./routes/index";
import type Route1 from "./routes/test/[some]/index";

declare global {
    export type Routes = ElysiaWithBaseUrl<"/api", typeof Route0> &
        ElysiaWithBaseUrl<"/api/test/:some", typeof Route1>;
}

Example of app with types code-generation you can see in example

Bun build usage

You can use this plugin with Bun.build, thanks to esbuild-plugin-autoload!

// @filename: build.ts
import { autoload } from "esbuild-plugin-autoload"; // default import also supported

await Bun.build({
    entrypoints: ["src/index.ts"],
    target: "bun",
    outdir: "out",
    plugins: [autoload()],
}).then(console.log);

Then, build it with bun build.ts and run with bun out/index.ts.

Bun compile usage

You can bundle and then compile it into a single executable binary file

import { autoload } from "esbuild-plugin-autoload"; // default import also supported

await Bun.build({
    entrypoints: ["src/index.ts"],
    target: "bun",
    outdir: "out",
    plugins: [autoload()],
}).then(console.log);

await Bun.$`bun build --compile out/index.js`;

[!WARNING] You cannot use it in bun build --compile mode without extra step (Feature issue)

Read more

Usage of schema handler

import swagger from "@elysiajs/swagger";
import Elysia from "elysia";
import { autoload } from "elysia-autoload";

const app = new Elysia()
    .use(
        await autoload({
            schema: ({ path, url }) => {
                const tag = url.split("/").at(1)!;

                return {
                    beforeHandle: ({ request }) => {
                        console.log(request.url);
                    },
                    detail: {
                        description: `Route autoloaded from ${path}`,
                        tags: [tag],
                    },
                };
            },
        })
    )
    .use(swagger());

export type ElysiaApp = typeof app;

app.listen(3001, console.log);