https://github.com/robinbuschmann/afraid-swagger
Generates swagger documentation from afraid middlewares
https://github.com/robinbuschmann/afraid-swagger
Last synced: over 1 year ago
JSON representation
Generates swagger documentation from afraid middlewares
- Host: GitHub
- URL: https://github.com/robinbuschmann/afraid-swagger
- Owner: RobinBuschmann
- Created: 2018-08-09T18:19:45.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-08T16:28:59.000Z (over 7 years ago)
- Last Synced: 2025-03-01T11:20:06.986Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 127 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/afraid-swagger)
[](https://travis-ci.org/RobinBuschmann/afraid-swagger)
# afraid-swagger
> You already described all you routes with afraid? Fear the redundancy? Let's get a swagger documentation with nearly no effort!
## Installation
```bash
npm install afraid --save --no-optional
npm install afraid-swagger --save
```
## Usage
```typescript
import {query, f, fail} from 'afraid';
import {swagger, responseBody} from 'afraid-swagger';
import * as express from 'express';
const app = express();
app.use('/api-docs', swagger({version: '1.0', title: 'API Docs'})); // 🎉
app.get('/users', [
query(
f('limit').int(),
f('offset').int(),
f('filters').string().array().opt(),
),
responseBody(
f('id').int(),
f('name').string(),
),
fail,
], (req, res, next) => {
// ...
});
```
## Using classes for validation and transformation (optional)
#### Installation
Omitting `--no-optional` will install required packages `class-transformer` and `reflect-metadata` automatically
```
npm install afraid --save
```
#### Configuration
The following flags in `tsconfig.json`:
```json
{
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
```
#### Usage
```typescript
import {body, Field, IsInt, fail} from 'afraid';
import {swagger, responseBody} from 'afraid-swagger';
import * as express from 'express';
const app = express();
class CreateUserDTO {
@Field name: string;
@IsInt() @Field age: number;
}
class UserDTO {
@Field id: number;
@Field name: string;
@IsInt() @Field age: number;
}
app.use('/api-docs', swagger({version: '2.0', title: 'API Docs'})); // 🎉
app.post('/users', [
body(CreateUserDTO),
responseBody(UserDTO),
fail,
], (req, res, next) => {
// ...
});
```