Socket
Socket
Sign inDemoInstall

afraid-swagger

Package Overview
Dependencies
80
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    afraid-swagger

Generates swagger documentation from afraid middlewares


Version published
Weekly downloads
9
Maintainers
1
Install size
12.1 MB
Created
Weekly downloads
 

Readme

Source

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

FAQs

Last updated on 08 Jan 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc