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

koa-api-plus

v1.0.12

Published

基于Typescript + KOA 开发的 Restful APi 接口

Downloads

177

Readme

基于 Typescript + KOA 开发的 Restful APi 接口

测试网站

测试网站 api.xygeng.cn

特点


  • ts 开发
  • 支持装饰器和 控制器 创建接口
  • 具有日志记录功能

安装


  • 克隆代码
  • 安装依赖
yarn
  • 开发运行项目
yarn run dev
  • 打包命令
yarn run build
  • 生产环境
yarn add koa-api-plus

控制器(重点)


存放控制器的目录,防止路由重复,文件名将默认为路由前缀。路由规则:/文件夹名/类装饰器(文件名,index文件为斜杠)/方法装饰器(方法名),支持不断嵌套

控制器文件命名规则:index.controller.ts

装饰器

  • Controller()装饰器,在类前面使用:@Controller(路由前缀),默认文件名就是前缀

    @Controller('user')

  • GET/POST,请求方法,在类方法前面使用:@函数(路由)

    @GET('/login') 对应的路由就是 /user/login

使用方法

// index.js
import { Controller, Get, Post } from 'koa-api-plus';
@Controller()
export default class IndexController {
    @GET('/test')
    async hello() {
        return {
            title: 'haha'
        };
    }
}
// [Get] /test => {}

// user/index.controller.ts
import { Controller, Get, Post } from 'koa-api-plus';
@Controller()
export default class IndexController {
    @POST('/test')
    async hello() {
        return {
            title: 'haha'
        };
    }
}

// 【Post】 /user/test =>{}

详细用例

文件:完整查看例子源码

hello-world 例子

//
import Api, { Controller, Get, Logger, Param, Post, ApiLogger } from '../../src';

type User = {
    id: number;
    name: {
        firstName: string;
        lastname: string;
    };
};

@Controller()
export class GetController {
    @Get('/get')
    // 获取query参数
    public get(@Param.Query<User>('id') id: number) {
        if (id) {
            return {
                id
            };
        }
        return null;
    }
}

@Controller('/post')
export class PostController {
    // 定义日志
    @Logger()
    private logger!: ApiLogger;
    @Post('/')
    // 获取body参数
    public post(@Param.Body<User>('id') id: number, @Param.Body<User>('name.firstName') firstName: string) {
        this.logger.log('post测试:', { id, firstName });
        if (id && firstName) {
            return {
                id,
                firstName
            };
        }
        return null;
    }
}

// 创建实例
const api = new Api();

api.on('log', (...args) => {
    console.log('[log]', ...args);
});
api.on('start', () => {
    console.log('[start]');
});

api.on('error', (e) => {
    console.error('[error]', e);
});

// 启动
api.start();

实例函数

// 事件监听
app.on();
// koa默认中间件
app.use();

// 默认启动服务
app.start();

打包流程

1、自动加载控制器

直接打包即可