afraid-swagger
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-alpha.7 • Public • Published

NPM Build Status

afraid-swagger

You already described all you routes with afraid? Fear the redundancy? Let's get a swagger documentation with nearly no effort!

Installation

npm install afraid --save --no-optional
npm install afraid-swagger --save

Usage

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:

{
  "experimentalDecorators": true,
  "emitDecoratorMetadata": true
}

Usage

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) => {
    // ...
});

Readme

Keywords

none

Package Sidebar

Install

npm i afraid-swagger

Weekly Downloads

0

Version

1.0.0-alpha.7

License

ISC

Unpacked Size

26 kB

Total Files

38

Last publish

Collaborators

  • robinbuschmann