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

cromo

v1.1.0

Published

A tiny, small & simple web framework for Bun

Downloads

108

Readme

🎴 Cromo

A tiny, fast & simple file-based router server for Bun 🧄

NPM version install size NPM Downloads npm Build status

Table of Contents

Server set up

To get started with Cromo, you need to download the package using Bun:

bun add cromo

Next, import and initialize Cromo in your code:

import { Cromo } from 'cromo'

const cromo = new Cromo({
  dir: './src/api'  // default: './api'
  port: 5000        // default: Bun.env.PORT || 3000
})

Router usage

Cromo utilizes a file-based router system. By default, the router folder is named api, but you can change it by passing the dir option during initialization:

// File structure example
├── api
│   ├── hello
│   │   ├── [world]
│   │   │   └── index.ts
│   └── user
│       ├── [name].ts
│       └── index.ts
└── index.ts

Handlers usage

Inside the router files, you can write HTTP method handlers:

// api/hello/[world]/index.ts
import type { CromoContext, CromoHandler } from 'cromo'

// handler for GET method
export const GET: CromoHandler = (context) => {
  const { world } = context.params
  return Response.redirect(`https://google.com/search?q=${world}`)
}

// default handler is called if there is no specific handler for the method
export default (context: CromoContext): Response => {
  return Response.json(null, 404)
}

Middlewares

There are three ways to add middlewares in Cromo:

  1. Using setMiddleware: Simply call setMiddleware to add an array of middlewares to the server.
// index.ts
cromo.setMiddleware([
  (context, next) => {
    console.log("middlewares called")
    return next(context)
  },
  ...
])
  1. Declaring middlewares const: Declare an array of middlewares in the router files as middlewares const.
// api/user/index.ts
import type { CromoHandler, CromoMiddleware } from 'cromo'

export const GET: CromoHandler = ({ responseInit }) => {
  return Response.json({ name: 'John' }, responseInit)
}

export const middlewares: CromoMiddleware[] = [
  ({ responseInit }, next) => {
    responseInit.headers = {
      'Access-Control-Allow-Origin': '*'
    }
    return next(context)
  }
]
  1. Declaring [METHOD]_middlewares const: Declare an array of middlewares in the router files as [METHOD]_middlewares const to apply middlewares to a specific method.
// api/user/[name].ts
import type { CromoHandler, CromoMiddleware } from 'cromo'

export const POST: CromoHandler = ({ params, responseInit }) => {
  const { name } = params
  return Response.json({ name }, responseInit)
}

export const POST_middlewares: CromoMiddleware[] = [
  ({ responseInit }, next) => {
    responseInit.headers = {
      'Access-Control-Allow-Origin': '*'
    }
    return next(context)
  }
]

Start the server

By default, Cromo will listen to port Bun.env.PORT, and if it is not set, it will listen to port 3000. However, you can change it by passing the port option during initialization.

Here is an example of starting the server:

cromo.start(port => {
  console.log(`Listening on port ${port}`)
})

Context object

In Cromo, we use the context object to pass data between middlewares and handlers. It contains basic Web API objects like Request, URL, ResponseInit, and some useful properties like matchedRoute, body, params, query.

import { MatchedRoute } from 'bun'

export interface CromoContext {
  // request objects
  request: Request
  url: URL
  matchedRoute: MatchedRoute
  body: unknown
  params: Record<string, string>
  query: Record<string, string>
  // response objects
  responseInit: ResponseInit
}