@unispechq/unispec-nestjs
v0.1.3
Published
NestJS adapter for UniSpec (module, decorators, and /unispec.json endpoint).
Readme
@unispechq/unispec-nestjs
UniSpec adapter for NestJS applications.
- Automatically collects REST routes from controllers.
- Detects the GraphQL schema and operations when using
@nestjs/graphql. - Detects WebSocket gateways and events when using
@nestjs/websockets. - Exposes the UniSpec document at
/unispec.jsonvia a dedicated module.
Installation
pnpm add @unispechq/unispec-nestjs @nestjs/common @nestjs/core reflect-metadata
# Optional for GraphQL
pnpm add @nestjs/graphql @apollo/server graphql
# Optional for WebSocket
pnpm add @nestjs/websocketsQuick start (REST + GraphQL + WebSocket)
import { Module, Controller, Get } from "@nestjs/common";
import { GraphQLModule } from "@nestjs/graphql";
import { Query, Resolver } from "@nestjs/graphql";
import { WebSocketGateway, SubscribeMessage } from "@nestjs/websockets";
import { UniSpecModule } from "@unispechq/unispec-nestjs";
@Controller()
class AppController {
@Get("/ping")
getPing() {
return { ok: true };
}
}
@Resolver()
class AppResolver {
@Query(() => String, { name: "hello" })
hello(): string {
return "Hello from NestJS GraphQL!";
}
}
@WebSocketGateway()
class AppGateway {
@SubscribeMessage("chatMessage")
handleChatMessage() {
return "ok";
}
}
@Module({
imports: [
GraphQLModule.forRoot({ autoSchemaFile: true }),
UniSpecModule.forRoot({
service: {
id: "nestjs-service",
name: "NestJS Service",
version: "1.0.0"
}
})
],
controllers: [AppController],
providers: [AppResolver, AppGateway]
})
export class AppModule {}// main.ts
import "reflect-metadata";
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
console.log("REST: http://localhost:3000/ping");
console.log("GraphQL: http://localhost:3000/graphql");
console.log("UniSpec doc: http://localhost:3000/unispec.json");
}
bootstrap();What goes into UniSpec
When /unispec.json is requested, the module returns a UniSpec document built from NestJS metadata:
service.protocols.rest.paths— controllers/routes.service.protocols.graphql— SDL and a basic list of GraphQL operations.service.protocols.websocket— channels/events from@WebSocketGateway/@SubscribeMessagegateways.
See a more complete working example in the examples/nestjs-basic directory.
