New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hono-autoloader

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hono-autoloader

Plugin for [Hono](https://hono.dev/) that autoloads all routes in a directory.

  • 0.3.0
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

hono-autoloader

Plugin for Hono that autoloads all routes in a directory.

Inspired by elysia-autoload.

Installation

yarn add hono-autoloader

Usage

Register the Plugin

import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import { autoloadRoutes } from 'hono-autoloader'

const port = +(process.env.PORT || 3000)

const app = await autoloadRoutes(new Hono(), {
  // Pattern to scan route files
  pattern: '**/*.ts',
  // Prefix to add to routes
  prefix: '/api',
  // Source directory of route files: use "relative" path
  routesDir: './src/api'
})

serve(
  {
    fetch: app.fetch,
    port
  },
  () => console.log(`Server running at http://localhost:${port}`)
)

Create a Route

// /routes/index.ts
import type { Context } from 'hono'

export default (c: Context) => {
  return c.text('Hello World!')
}

Directory Structure

Guide on how hono-autoloader matches 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

KeyTypeDefaultDescription
failGlob?booleantrueThrows an error if no matches are found
importKey?stringdefaultThe key (name) of the exported function of route files
pattern?string**/*.{ts,tsx,js,jsx,mjs,cjs}Glob patterns
prefix?string Prefix to be added to each route
routesDir?string./routesThe folder where routes are located (use a relative path)
skipImportErrors?booleanfalseThrows an error if there is an import error of a route file

Transpile

For Vite + Node.js and similar use cases, where .ts or .tsx files aren't transpiled, install esbuild.

Note:

Bun internally transpiles every file it executes (both .js and .ts) → Read more

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 03 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc