Skip to content

Commit f842394

Browse files
Add nest-openapi tools (#688)
1 parent d96ee32 commit f842394

3 files changed

Lines changed: 182 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
name: '@nest-openapi/mock'
3+
description: Spec-driven mock server for NestJS that generates realistic mock responses from OpenAPI.
4+
categories:
5+
- mocking-tools
6+
link: https://nest-openapi.github.io/mock/
7+
languages:
8+
typescript: true
9+
nodejs: true
10+
repo: https://github.com/ts-oas/nest-openapi
11+
oasVersions:
12+
v2: false
13+
v3: true
14+
v3_1: true
15+
v3_2: false
16+
---
17+
18+
## Overview
19+
20+
`@nest-openapi/mock` enables spec-driven mocking for NestJS applications. It generates realistic mock responses from your OpenAPI specification, making it ideal for development, testing, and frontend development when backend APIs are not yet available.
21+
22+
## Features
23+
24+
- **Spec-driven mocking**: Generates mock responses directly from your OpenAPI specification
25+
- **Multiple strategies**: Examples, JSON Schema Faker, response recording/reply, primitive values, or passthrough
26+
- **Selective mocking**: Can mock all routes by default or specific routes only
27+
- **Drop-in integration**: Works seamlessly with existing NestJS controllers
28+
- **Flexible**: Per-route overrides and request header hints
29+
30+
## Usage
31+
32+
Install the package and configure it in your NestJS module:
33+
34+
```typescript
35+
import { OpenAPIMockModule } from '@nest-openapi/mock';
36+
import { Module } from '@nestjs/common';
37+
38+
import * as openApiSpec from './openapi.json';
39+
40+
@Module({
41+
imports: [
42+
OpenAPIMockModule.forRoot({
43+
specSource: { type: 'object', spec: openApiSpec },
44+
enable: process.env.NODE_ENV === 'development',
45+
mockByDefault: true, // Mock all routes by default, like a mock server
46+
}),
47+
],
48+
})
49+
export class AppModule {}
50+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
name: '@nest-openapi/serializer'
3+
description: High-performance response serialization for NestJS based on your OpenAPI spec using fast-json-stringify.
4+
categories:
5+
- misc
6+
link: https://nest-openapi.github.io/serializer/
7+
languages:
8+
typescript: true
9+
nodejs: true
10+
repo: https://github.com/ts-oas/nest-openapi
11+
oasVersions:
12+
v2: false
13+
v3: true
14+
v3_1: true
15+
v3_2: false
16+
---
17+
18+
## Overview
19+
20+
`@nest-openapi/serializer` provides high-performance response serialization for NestJS applications. It uses `fast-json-stringify` to automatically serialize responses according to your OpenAPI specification, ensuring output matches your API contract while maintaining optimal performance.
21+
22+
## Features
23+
24+
- **Spec-driven**: Automatically serializes responses based on your OpenAPI schema definitions
25+
- **Fast by default**: Uses [`fast-json-stringify`](https://github.com/fastify/fast-json-stringify) with caching and optional pre‑compilation.
26+
- **NestJS‑native**: Auto serializes with per‑route opt‑out and overrides.
27+
- **Drop-in integration**: Works seamlessly with existing NestJS controllers
28+
- **Platform agnostic**: Supports both Express and Fastify adapters
29+
30+
## Usage
31+
32+
Install the package and configure it in your NestJS module:
33+
34+
```typescript
35+
import { OpenAPISerializerModule } from '@nest-openapi/serializer';
36+
import { Module } from '@nestjs/common';
37+
38+
import * as openApiSpec from './openapi.json';
39+
40+
@Module({
41+
imports: [
42+
OpenAPISerializerModule.forRoot({
43+
specSource: { type: 'object', spec: openApiSpec },
44+
responseSerialization: {
45+
enable: true,
46+
skipErrorResponses: true,
47+
},
48+
}),
49+
],
50+
})
51+
export class AppModule {}
52+
```
53+
54+
You can disable serialization for specific routes using the `@Serialize` decorator:
55+
56+
```typescript
57+
import { Serialize } from '@nest-openapi/serializer';
58+
import { Body, Controller, Post } from '@nestjs/common';
59+
60+
@Controller('books')
61+
export class BooksController {
62+
@Post()
63+
@Serialize({ disable: true })
64+
create(@Body() dto: CreateBookDto): Book {
65+
return this.booksService.create(dto);
66+
}
67+
}
68+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
name: '@nest-openapi/validator'
3+
description: Automatic request/response validation for NestJS based on your OpenAPI spec using AJV.
4+
categories:
5+
- data-validators
6+
link: https://nest-openapi.github.io/validator/
7+
languages:
8+
typescript: true
9+
nodejs: true
10+
repo: https://github.com/ts-oas/nest-openapi
11+
oasVersions:
12+
v2: false
13+
v3: true
14+
v3_1: true
15+
v3_2: false
16+
---
17+
18+
## Overview
19+
20+
`@nest-openapi/validator` provides automatic, highly configurable request and response validation for NestJS applications using your OpenAPI specification as the single source of truth. It validates incoming requests and outgoing responses against your OpenAPI spec, ensuring API contracts are enforced at runtime.
21+
22+
## Features
23+
24+
- **Spec‑driven**: The OpenAPI spec is the contract; validation is generated from it
25+
- **Fast by Design**: [AJV under the hood](https://github.com/ajv-validator/ajv), with caching and optional pre-compilation
26+
- **NestJS‑native**: Auto validates with per‑route opt‑out and overrides
27+
- **Drop-in integration**: Works seamlessly with existing NestJS controllers
28+
- **Platform agnostic**: Supports both Express and Fastify adapters
29+
30+
## Usage
31+
32+
Install the package and configure it in your NestJS module:
33+
34+
```typescript
35+
import { OpenAPIValidatorModule } from '@nest-openapi/validator';
36+
import { Module } from '@nestjs/common';
37+
38+
import * as openApiSpec from './openapi.json';
39+
40+
@Module({
41+
imports: [
42+
OpenAPIValidatorModule.forRoot({
43+
specSource: { type: 'object', spec: openApiSpec },
44+
}),
45+
],
46+
})
47+
export class AppModule {}
48+
```
49+
50+
You can also use per-route decorators to customize validation behavior:
51+
52+
```typescript
53+
import { Validate } from '@nest-openapi/validator';
54+
import { Body, Controller, Post } from '@nestjs/common';
55+
56+
@Controller('books')
57+
export class BooksController {
58+
@Post()
59+
@Validate({ request: { query: false }, response: true })
60+
create(@Body() dto: CreateBookDto): Book {
61+
return this.booksService.create(dto);
62+
}
63+
}
64+
```

0 commit comments

Comments
 (0)