@danielgl/steampunk
v1.0.0
Published
A high-performance, DX-focused web framework for Bun, inspired by ASP.NET Core.
Maintainers
Readme
Features
- Dependency Injection: Powerful
ServiceCollectionandServiceProviderout of the box (Singleton,Scoped,Transient). - Builder Pattern: Familiar
WebApplication.createBuilder()approach. - Decorators: Beautiful Controller and Routing decorators (
@Controller(),@Get(),@Post(),@FromRoute(), etc). - Extremely Fast: Built natively on top of
Bun.serve().
Installation
bun add @danielgl/steampunk reflect-metadataEnsure your tsconfig.json has experimentalDecorators and emitDecoratorMetadata enabled:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}Quick Start
1. Create a Service
import { Injectable } from "@danielgl/steampunk";
export interface User {
id: string;
name: string;
}
@Injectable()
export class UsersService {
private users: User[] = [{ id: "1", name: "Alice" }];
public getAll() {
return this.users;
}
public getById(id: string) {
return this.users.find((u) => u.id === id);
}
}2. Create a Controller
import { Controller, Get, FromRoute, HttpResult } from "@danielgl/steampunk";
import { UsersService } from "./users-service";
import type { User } from "./users-service";
@Controller("users")
export class UsersController {
// Services are automatically injected!
constructor(private usersService: UsersService) {}
@Get()
public getAllUsers(): HttpResult {
const users = this.usersService.getAll();
return HttpResult.ok(users);
}
@Get(":id")
public getUserById(@FromRoute("id") id: string): HttpResult {
const user = this.usersService.getById(id);
if (!user) {
return HttpResult.notFound("User not found");
}
return HttpResult.ok(user);
}
}3. Bootstrap your App
import { WebApplication } from "@danielgl/steampunk";
import { UsersService } from "./users-service";
import { UsersController } from "./users-controller";
const builder = WebApplication.createBuilder();
// 1. Register DI Services
builder.services.addSingleton(UsersService);
const app = builder.build();
// 2. Add Middlewares
app.use(async (context, next) => {
console.log(`[${context.method}] ${context.path}`);
return next();
});
// 3. Map Controllers
app.mapControllers([UsersController]);
### 4. Run!
```bash
bun run main.tsResponse Helpers (HttpResult)
Steampunk provides a powerful HttpResult class to simplify returning HTTP responses.
// Return 200 OK with an object (auto-serialized to JSON)
return HttpResult.ok({ id: 1, name: "John" });
// Return 201 Created with a Location header
return HttpResult.created(newUser, `/users/${newUser.id}`);
// Return 400 Bad Request with a message
return HttpResult.badRequest("Invalid input");
// Return 404 Not Found
return HttpResult.notFound("User not found");All methods support flexible payloads: string, object, or array.
Authentication & Authorization
Protect your routes easily using the @Authorize() decorator.
@Authorize() // Multi-level: Class or Method
@Controller("admin")
export class AdminController {
@Get("secret")
public getSecret() {
return HttpResult.ok("shhh!");
}
}Development
Available scripts for contributing or local development:
bun run lint: Run ESLint checks.bun run format: Format code with Prettier.bun run test: Run unit tests with Bun.
Pre-commit hooks are enforced via Husky to ensure code quality!
Made with ❤️ focusing on Developer Experience.
