Preview. Beacon ships inside Flare, which has no public release yet.
Start a project
flare init my-api -t api
cd my-api
flare src/main.ts # run in development
flare build # one bundled file for productionLike NestJS, Beacon needs "experimentalDecorators": true in tsconfig.json.
A first controller
import { Beacon, Body, Controller, Get, Injectable, Module, Param, Post, MinLength } from '@flare/beacon';
class CreateCat {
@MinLength(2) name: string;
age: number;
}
@Injectable()
class CatsService {
private cats: CreateCat[] = [];
add(cat: CreateCat) { this.cats.push(cat); return cat; }
find(id: number) { return this.cats[id]; }
}
@Controller('cats')
class CatsController {
constructor(private cats: CatsService) {}
@Get(':id')
find(@Param('id') id: number) { return this.cats.find(id); }
@Post()
create(@Body() body: CreateCat) { return this.cats.add(body); }
}
@Module({ controllers: [CatsController], providers: [CatsService] })
class AppModule {}
const app = await Beacon.create(AppModule);
await app.listen(3000);id: number is checked and converted from the URL without a pipe, and CreateCat is validated from its types and rules. Unknown properties are dropped.
How it works
- Compile. Before your code runs, Flare reads the TypeScript types: constructor parameter types become dependency-injection tokens, and parameter and property types become validation rules. No
reflect-metadataat run time. - Generate. At start-up Beacon writes one straight-line handler per route, one validator per DTO class and a router of nested
switchstatements. - Fall back. Routes the generator doesn't cover (middleware,
@Res(), server-sent events, redirects, request-scoped controllers, wildcards) run through general handlers.
The framework adds about 0.04 µs per request over a hand-written handler.
What's supported
| Area | Supported |
|---|---|
| Modules | @Module, @Global, dynamic modules (forRoot/register), re-exports, forwardRef |
| Providers | classes, useValue, useFactory, useExisting, @Inject, @Optional, ModuleRef; default, transient and request scopes |
| Routes | @Get/@Post/@Put/@Patch/@Delete/@All, @HttpCode, @Header, @Redirect, @Sse, global prefix, URI versioning |
| Parameters | @Param, @Query, @Body, @Headers, @Req, @Res, @Ip, custom decorators |
| Enhancers | guards, interceptors, pipes (ValidationPipe, ParseIntPipe, …), exception filters, middleware |
| Config | ConfigModule.forRoot, ConfigService, .env files |
| OpenAPI | generated from types; @ApiTags, @ApiProperty, Swagger UI via useOpenAPI(app, { path }) |
| Real-time | server-sent events; WebSocket gateways (@WebSocketGateway, @SubscribeMessage) |
| HTTP | HTTP/1.1; HTTPS and HTTP/2 via httpsOptions; HTTP/3 with http3: true |
| Testing | Test.createTestingModule, app.getHttpServer() for supertest |
Running a NestJS app
Imports from @nestjs/common, core, testing, config, swagger, mapped-types and platform-express/-fastify resolve to Beacon, so an existing NestJS app runs with flare src/main.ts. Not mapped yet: socket.io gateways, multipart uploads, microservices, GraphQL and CQRS.
Switches
| Variable | Effect |
|---|---|
BEACON_DEBUG=1 | log the generated routes |
BEACON_COMPILE=0 | no code generation (general handlers only) |
FLARE_NEST=node | don't map @nestjs/* to Beacon |