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

uws-autoload

v0.4.0

Published

Plugin for [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js) that autoloads all routes in a directory.

Downloads

579

Readme

uws-autoload

Plugin for uWebSockets.js that autoloads all routes in a directory.

Inspired by elysia-autoload.

Installation

yarn add uws-autoload

Usage

Register the Plugin

import { App } from 'uWebSockets.js'
import { autoloadRoutes } from 'uws-autoload'

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

const app = await autoloadRoutes(App(), {
  // 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'
})

app.listen(port, (listenSocket) => {
  if (listenSocket) {
    console.log(`Server running at http://localhost:${port}`)
  } else {
    console.log(`Failed to listen to port ${port}`)
  }
})

Create a Route

// /routes/index.ts
import type { RecognizedString, TemplatedApp } from 'uWebSockets.js'

export default (pattern: RecognizedString, app: TemplatedApp) => app.get(pattern, (res, req) => {
  res.end('Hello World!')
})

Directory Structure

Guide on how uws-autoload 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

| Key | Type | Default | Description | | ----------------- | ------- | ------------------------------ | ----------------------------------------------------------------- | | failGlob? | boolean | true | Throws an error if no matches are found | | importKey? | string | default | The 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 | ./routes | The folder where routes are located (use a relative path) | | skipImportErrors? | boolean | false | Throws 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.