0.2.3 • Published 1 month ago

elysia-autoload v0.2.3

Weekly downloads
-
License
-
Repository
-
Last release
1 month ago

elysia-autoload

Plugin for Elysia which autoload all routes in directory and code-generate types for Eden

Currently, Eden types generation is broken!!

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(autoload()).listen(3000);

export type ElysiaApp = typeof app;

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
    └── 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

Options

KeyTypeDefaultDescription
pattern?string"**\/*.{ts,tsx,js,jsx,mjs,cjs}"Glob patterns
dir?string"./routes"The folder where routes are located
prefix?stringPrefix for routes
types?boolean | Types OptionsfalseOptions to configure type code-generation. if boolean - enables/disables generation
schema?FunctionHandler for providing routes guard schema

Types Options

KeyTypeDefaultDescription
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?booleanfalseUse 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(
        autoload({
            types: {
                output: "./routes.ts",
                typeName: "Routes",
            }, // or pass true for use default params
        })
    )
    .listen(3000);

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

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

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

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

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

console.log(data);

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

Usage of schema handler

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

const app = new Elysia()
    .use(
        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);
0.2.3

1 month ago

0.2.2

1 month ago

0.2.1

1 month ago

0.2.0

1 month ago

0.1.8

2 months ago

0.1.9

2 months ago

0.1.7

2 months ago

0.1.6

2 months ago

0.1.4

4 months ago

0.1.3

4 months ago

0.1.5

4 months ago

0.1.2

4 months ago

0.1.1

4 months ago

0.1.0

4 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago