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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@n2js/elysia-module

v0.1.0

Published

Type-preserving modular routing adapter for Elysia with Eden-ready module organization.

Readme

@n2js/elysia-module

简体中文 | English

@n2js/elysia-module 是面向 Elysia 的模块化路由适配包。它适合想把 Elysia routes 拆成独立模块,同时完整保留 route tuple、Eden RPC 和 handler 上下文类型推断的项目。defineElysiaModule() 会把多段 Elysia 路由合并成只读 router artifact,并挂到 @n2js/container 的模块实例上,让 Elysia 模块化路由、依赖解析和类型推断一起工作。

当前包提供 ESM-only 构建产物,运行入口为 ./dist/index.js,类型声明入口为 ./dist/index.d.ts

为什么适合 Elysia 模块化

  • 保留路由类型推断: 合并 route tuple 后仍保留 Eden RPC 类型,不把 router 降级成宽泛的运行时对象。
  • 按模块组织路由: 每个模块可以拥有自己的 routes、Provider、imports、features 和 lifecycle。
  • handler 内可解析依赖: handler 上下文中的 module 指向当前模块实例,业务 route 可以直接解析本模块可见的 Provider。
  • 子模块自动组合: 使用 mount() 组合子模块时,会按 mount path 合并子模块 router。
  • 搜索友好定位: 如果你在找 Elysia module、Elysia modular routing 或 Elysia route type inference,这个包就是为这类场景准备的。

安装

npm install @n2js/elysia-module @n2js/container elysia

@n2js/containerelysia 是 peer dependencies。请在使用侧安装它们,并尽量保持单实例,避免模块 token 与 router 类型来自不同依赖副本。若需要 Eden RPC client,请额外安装 @elysia/eden

快速开始

import { Container } from '@n2js/container';
import { Elysia } from 'elysia';
import {
  defineElysiaModule,
  type TElysiaModuleSingleton,
} from '@n2js/elysia-module';

class GreetingService {
  getMessage(): string {
    return 'Hello from DI';
  }
}

const greetingRoute = new Elysia<'', TElysiaModuleSingleton>().get(
  '/greeting',
  async ({ module }) => {
    const greetingService = await module.resolve(GreetingService);

    return greetingService?.getMessage() ?? 'missing';
  },
);

const ElysiaExampleModule = defineElysiaModule({
  displayName: 'ElysiaExampleModule',
  routes: [greetingRoute] as const,
  providers: [GreetingService],
});

const container = new Container(ElysiaExampleModule);
const response = await container.root.router.handle(
  new Request('http://localhost/greeting'),
);

console.log(await response.text());

核心能力

  • defineElysiaModule():定义带 Elysia router artifact 的模块,并保留 route tuple 类型推断。
  • mergeElysiaRoutes():合并 route tuple 和 mounted child routers。
  • TElysiaModuleSingleton:为 Elysia handler 增加 module 上下文类型。
  • TMergedElysiaRouter:保留 route tuple 推导出的 Eden RPC 类型。
  • mount() 子模块时,会按 mount path 自动组合子模块 router。

使用说明

defineElysiaModule() 接收与 defineModule() 相同的 Provider、imports、features 和 lifecycle 选项,同时增加 routes。返回的模块可以直接传给 new Container(),并通过 container.root.router.handle() 处理请求。

业务 route 中的 module 是当前模块实例。你可以在 handler 中调用 resolve()resolveOwn()resolveWithScope(),也可以读取模块 Feature 附加的只读 artifact。

类型导出

| 导出 | 说明 | | --- | --- | | defineElysiaModule() | 定义 Elysia 模块并生成合并后的 router。 | | mergeElysiaRoutes() | 独立合并 Elysia route tuple。 | | TElysiaModuleSingleton | Elysia singleton 类型,包含当前模块实例。 | | TElysiaModuleOptions | defineElysiaModule() 的选项类型。 | | TMergedElysiaRouter | 合并后 router 的类型工具。 |

许可证

MIT