Bun-Bakery is a web framework for Bun. It uses a file based router in style like svelte-kit. No need to define routes during runtime.

Overview

Reliability Rating Security Rating

Bun Bakery

Bun-Bakery is a web framework for Bun. It uses a file based router in style like svelte-kit. No need to define routes during runtime.

Quick Start

bun add @kapsonfire/bun-bakery

On your main script import Router from bun-bakery and define your pathes. i.e. main.ts

import {Router} from "@kapsonfire/bun-bakery"

new Router({
    assetsPath: import.meta.dir + '/assets/',
    routesPath: import.meta.dir + '/routes/'
})

After that run the server and open your browser http://localhost:3000

bun main.ts

Routing

Routes are added automatically when creating files inside your routesPath when exporting functions with the corresponding Method Names. Given example above create index.ts inside routes/ and export a GET function calling ctx.sendResponse().

import {Context} from "@kapsonfire/bun-bakery"

export async function GET(ctx: Context) {
    ctx.sendResponse(new Response('hello world!'));
}

Parameters

Routes can have parameters inside dirname and/or filename. Just put the parameter name inside brackets and it will be added to ctx.params. In example: given routes/user/[username].ts and open http://localhost:3000/user/kapsonfire

import {Context} from "@kapsonfire/bun-bakery"

export async function GET(ctx: Context) {
    ctx.sendResponse(new Response('hello '+ ctx.params.username +'!'));
}

will output hello kapsonfire!

Spread Paramaters

Routes can also have wildcard/spread paramaters. In example: given routes/users/[...usernames].ts and open http://localhost:3000/users/kapsonfire/jarred/tricked

import {Context} from "@kapsonfire/bun-bakery"

export async function GET(ctx: Context) {
    ctx.sendAsJson(JSON.stringify(ctx.params));
}

will output

{"usernames":["kapsonfire","jarred","tricked"]}

Handlers

Inside the context variable you can access the native bun Request object inside ctx.request. ctx.sendResponse expects a native bun Response object.

Middlewares

bun-bakery supports some life-cycles to add middleware

  • onRequest will be called before the router handles the request
  • onRoute will be called before the route function will be called
  • onResponse will be called after the route function finished
router.addMiddleware({
    onRequest: (ctx: Context) => { ctx.params.injected = "1"; console.log('onRequest', ctx) },
    onRoute: (ctx: Context) => console.log('onRoute', ctx),
    onResponse: (ctx: Context) => {
        ctx.response.headers.set('content-type', 'application/jsonx');
        console.log('onResponse', ctx)
    },
});
You might also like...

⚡️A minimalistic and sweet router for blazing fast bun

⚡️A minimalistic and sweet router for blazing fast bun

Melonpan is a simple and minimalistic web-router designed to work with Bun, keeping performance in mind. 🤔 Why Melonpan? no/minimal learning curve De

Jan 6, 2023

Use this app to track and prioritize the topics you need to study during your job search

Use this app to track and prioritize the topics you need to study during your job search

Interview Skills Grid Use this app to track and prioritize the topics you need to study during your job search. Try it for yourself! -- Interview Ski

Apr 13, 2022

Svultimate Svelte(Kit) Svutilities

/svu /svu is a collection of Svelte(Kit) utillities that make developing webapps even more easy and fun. Read the docs at http://svu.vercel.app Why Sv

Dec 16, 2022

This is a starter templete for svelte kit and maplibre.

This is a starter template for maplibre and svelte Clone this application by running git clone https://github.com/Thuhaa/svelte-maplibre-starter.git A

Nov 28, 2022

An adapter where you can define which function to run

Switch Functions An adapter where you can define which function to run Installation This is a Node.js module available through the npm registry. Befor

Jun 17, 2022

Define tool for JS/TS

JSDef A Define tool for js/ts Install npm i jsdefn HOW TO Create Defs import { JSDef } from 'jsdefn' const $ = JSDef({ "A": "apple" }) Get define

Apr 18, 2022

This is a place to define better practices in code.

Better Practices Categories Angular General Programming GitHub Pages JavaScript Naming Conventions React Influence This guide is heavily influenced by

Sep 3, 2022

The best Nodejs price kit you need when working with cryptocurrencies with multiple providers support

Cryptocurrency Price Kit STAGE: RFC The best Nodejs price kit you need when working with cryptocurrencies with multiple providers support. Goal To pro

Sep 7, 2022

Zero runtime type-safe CSS in the same file as components

macaron comptime-css is now called macaron! macaron is a zero-runtime and type-safe CSS-in-JS library made with performance in mind Powered by vanilla

Jan 4, 2023
Comments
  • Cache assets files info to faster response

    Cache assets files info to faster response

    https://github.com/Kapsonfire-DE/bun-bakery/blob/9837704a7c8aebcacbb0b25e4e391490360816bf/lib/Router.ts#L184

    See a Sirv library. https://github.com/gornostay25/svelte-adapter-bun/blob/master/src/sirv.js

    enhancement 
    opened by gornostay25 0
  • missing spread parameters in routes

    missing spread parameters in routes

    Add the ability to add spread parameters

    File: /routes/users/[...usernames].ts URL: http://localhost/users/kapsonfire/jarred/tricked

    Results into:

    ctx.params = {
       username: ['kapsonfire', 'jarred', 'tricked']
    }
    
    enhancement 
    opened by Kapsonfire-DE 0
  • Creating a Website for Bun Bakery

    Creating a Website for Bun Bakery

    I'm working on another bun project and I found that you can get more contributors with a website. I built a really simple website and documentation generation tool if you want to add it to your project. It's under the MIT License- I'm just sending it around the bun community so it gains some more traction. If you want to check it out, it's right here. It's all integrated with Github Actions so all you have to do is add the code to GitHub actions and then you're all set. If you don't want to, just let me know and close this issue- good luck with your project!

    Also, just a tip, add a bunch of issues you need help with and label them "good first issue" because then people will see your project from places like goodfirstissues.com.

    opened by William-McGonagle 0
Owner
Dennis Dudek
Dennis Dudek
⚡️ A fast, minimalist web framework for the Bun JavaScript runtime

?? Bao.js A fast, minimalist web framework for the Bun JavaScript runtime. ⚡️ Bao.js is 3.7x faster than Express.js and has similar syntax for an easy

Matt Reid 746 Dec 26, 2022
A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast.

RESPRESS A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, ma

Yousef Wadi 9 Aug 29, 2022
Fast, Bun-powered, and Bun-only(for now) Web API framework with full Typescript support.

Zarf Fast, Bun-powered, and Bun-only(for now) Web API framework with full Typescript support. Quickstart Starting with Zarf is as simple as instantiat

Zarf Framework 65 Dec 28, 2022
A Svelte parser that compiles to Mitosis JSON, allowing you to write Svelte components once and compile to every framework.

Sveltosis Still in development A Svelte parser that compiles to Mitosis JSON, allowing you to write Svelte components once and compile to every framew

sveltosis 70 Nov 24, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
🦆 lightning fast duckdb bindings for bun runtime

@evan/duckdb lightning fast duckdb bindings for bun runtime Install bun add @evan/duckdb Features ?? batteries included ?? jit optimized bindings ?? 4

evan 29 Oct 20, 2022
TypeScript type definitions for Bun's JavaScript runtime APIs

Bun TypeScript type definitions These are the type definitions for Bun's JavaScript runtime APIs. Installation Install the bun-types npm package: # ya

Oven 73 Dec 16, 2022
A blazingly fast Bun.js filesystem router, with an unpleasantly smooth experience!

Oily A blazingly fast Bun.js filesystem router, with an unpleasantly smooth experience! Installation · Usage · Examples · Discord Installation Once yo

Aries 22 Dec 19, 2022