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

dorsale

v0.1.4

Published

Dorsale is a Bun-based TypeScript backend framework

Downloads

14

Readme

dorsale

Dorsale is a Bun-based backend TypeScript framework. It is built on top of the Bun server and wint for routing. Dorsale uses decorators to define elements of your application.
It is a work in progress, and is not ready for production use. If you benchmark it, please let me know the results!

Installation

bun add dorsale

Getting started

Bootstrapping

The setup is intentionally minimalistic. Bootstrapping a Dorsale application is as simple as:

import { dorsale } from 'dorsale';

dorsale({ port: 8080 });

This will start a server on port 8080. You can then add controllers, services, and repositories to your application.

Full application example

A full example could look like this:

src
├── index.ts
├── user.ts
├── userFinder.ts
├── userManager.ts
├── userController.ts
// index.ts
import { dorsale } from 'dorsale';

dorsale({ port: 8080 });
// user.ts
export interface User {
  id: string;
  email: string;
  password: string;
}
// userFinder.ts
import { User } from "./user";

export interface UserFinder {
  findAllUsers(): Promise<User[]>;

  findUserById(id: string): Promise<User | undefined>;

  findUserByEmail(email: string): Promise<User | undefined>;
}
// userManager.ts
import { UserEditor } from "./userEditor";
import { UserFinder } from "./userFinder";
import { User } from "./user";
import { Component } from "dorsale";

@Component
export class UserManager implements UserFinder {
  users: User[] // = [... some users];

  findAllUsers(): Promise<User[]> {
    return Promise.resolve(this.users);
  }

  findUserByEmail(email: string): Promise<User | undefined> {
    const user = this.users.find((u) => u.email === email);
    return Promise.resolve(user);
  }

  findUserById(id: string): Promise<User | undefined> {
    const user = this.users.find((u) => u.id === id);
    return Promise.resolve(user);
  }
}
// userController.ts
import { Controller, Get } from "dorsale";
import type { UserFinder } from "./userFinder";

@Controller()
export class UserController {
  constructor(private readonly userFinder: UserFinder) {
  }

  @Get("/")
  getAll() {
    return this.userFinder.findAllUsers();
  }

  @Get("/:id")
  getById(id: string) {
    return this.userFinder.findUserById(id);
  }
}

You just have to run the file containing the dorsale call, and you're good to go! The other components will be automatically discovered and injected. This reduces the amount of boilerplate code you have to write.
⚠️ Important: you must import interfaces as type in the controller file, this is a limitation of Bun. (see https://github.com/oven-sh/bun/issues/8618)

By default, Dorsale will look for components in the src folder, but you can change this by passing a rootDir option to the dorsale call.

dorsale({ port: 8080, rootDir: "myFolder/relative/to/the/current/file" });

Controllers

Controller classes

Controllers are classes that define the routes exposed by your application. They are decorated with @Controller().

import { Controller } from "dorsale";

@Controller()
export class UserController {
    // ... your routes
}

You can also specify a prefix for all the routes defined in a controller by passing it as an argument to the decorator.

@Controller("/users")
export class UserController {
    // ... your routes
}

Routes

Routes are defined by decorating methods with @Get, @Post, @Put, @Patch, or @Delete.

import { Controller, Get } from "dorsale";

@Controller()
export class UserController {
    @Get("/hello")
    getHello() {
        return "Hello world!";
    }
}

In the example above, a GET route will be exposed at /hello, and will return the string "Hello world!".

Route parameters

You can define route parameters by adding a colon (:) before the parameter name in the route path.

import { Controller, Get } from "dorsale";

@Controller()
export class UserController {
    @Get("/users/:id")
    getUserById(id: string) {
        // ...
    }
}

Query parameters

Query parameters are defined by adding a @Query decorator to the parameter.

import { Controller, Get, Query } from "dorsale";

@Controller()
export class UserController {
    @Get("/users") // e.g. GET /users?page=1&limit=10
    getUsers(@Query page: number, @Query limit: number) {
        // ...
    }
}

If some query parameters are not provided in the request, they will be undefined.

Body

You can access the body of the request by adding a @Body decorator to a parameter.

import { Controller, Post, Body } from "dorsale";

@Controller()
export class UserController {
    @Post("/users")
    createUser(@Body user: User) {
        // ...
    }
}

Testing

Coming soon!

Contributing

Contributions are welcome! Feel free to open an issue or a PR if you have any suggestions or bug reports. Please follow the GitHub flow when contributing (see here for more information). Thanks for your interest in Dorsale!