npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pyi-swagger

v1.0.3

Published

pyi swagger

Downloads

4

Readme

pyi

Quick Start

npm i pyi-swagger --save

app use

import { SwaggerInjectService, Swagger } from 'pyi-swagger';

SwaggerInjectService.register();

import "reflect-metadata";
import Koa from 'koa';
import { useKoaServer } from "routing-controllers";
import { UserController } from "./controllers/user.controller";

const koa = new Koa();

Swagger.build('/swagger', koa);

const app: Koa = useKoaServer(koa, {
    cors: true,
    controllers: [UserController]
});

app.listen(3001);

controller

import {
    request,
    summary,
    body,
    tags,
    path,
    description
} from 'pyi-swagger';
import { Get, Delete, Controller, Ctx } from 'routing-controllers';

const tag = tags(['User']);

const userSchema = {
    name: { type: 'string', required: true },
    password: { type: 'string', required: true }
};

@Controller()
export class UserController {

    @Get('/user/register')
    @request('POST', '/user/register')
    @summary('register user')
    @description('example of api')
    @tag
    @body(userSchema)
    public async register(ctx) {
        const { name } = ctx.validatedBody;
        const user = { name };
        ctx.body = { user };
    }

    @Get('/user/login')
    @request('post', '/user/login')
    @summary('user login, password is 123456')
    @tag
    @body(userSchema)
    public async login(ctx) {
        const { name, password } = ctx.validatedBody;
        if (password !== '123456') throw new Error('wrong password');
        const user = { name };
        ctx.body = { user };
    }

    @Get('/user')
    @request('get', '/user')
    @summary('user list')
    @tag
    public async getAll(ctx) {
        const users = [{ name: 'foo' }, { name: 'bar' }];
        ctx.body = { users };
    }

    @Get('/user/:id')
    @request('get', '/user/:id')
    @summary('get user by id')
    @tag
    @path({ id: { type: 'string', required: true } })
    public async getOne(ctx) {
        const { id } = ctx.validatedParams;
        console.log('id:', id);
        const user = { name: 'foo' };
        ctx.body = { user };
    }

    @Delete('/user/:id/:ids')
    @request('DELETE', '/user/:id/:ids')
    @summary('delete user by id')
    @tag
    @path({ id: { type: 'string', required: true }, ids: { type: 'string', required: true } })
    public async deleteOne(@Ctx() ctx) {
        // console.log(ctx.validatedParams)
        // const { id } = ctx.validatedParams;
        // console.log('id:', id);
        // ctx.body = { msg: 'success' };
        return 111;
    }
}

注意事项

SwaggerInjectService.register(); // 一定要在 controller 加载之前

Swagger.build('/swagger', app); // 将 swagger service 中间件 use 到 app

修饰器参考

koa-swagger-decorator [https://github.com/Cody2333/koa-swagger-decorator.git]

jsdoc 风格

// 参考 swagger-ui-koa [https://github.com/Darmikon/swagger-ui-koa.git]

SwaggerInjectService.register();

const options = {
    swaggerDefinition: {
      info: {
        title: 'API', // Title (required)
        version: '2.0.0', // Version (required)
      },
    },
    apis: [
      './src/module/swagger/swagger.yaml',
      './src/routes/*.js', // Path to the API docs from root
      './src/module/swagger/parameters.yaml'
    ],
};
Swagger.build('/swagger', app, options);