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

myapp1998

v3.4.6

Published

A nodejs framework for building concise and decorative applications written by typescript

Downloads

1

Readme

souljs

A nodejs web framework, 对koa简单包装,装饰风格,内置开发常用中间件,使用typescript编写

快速开始

git clone https://github.com/test/souljs-starter.git

npm install && npm run start

Node.js >= 8.0.0 required.

创建应用实例

import { createApplication } from 'souljs';
import * as router from './router';

async function main() {
  const app = await createApplication(__dirname, Object.keys(router).map(k => router[k]));

  app.listen(8080);
}

main();

路由处理并返回数据

@Controller('/user')
export default class User {

  @Post('/chname')
  changeName() {
    return ‘hello world’;
  }
}

通过@Render返回视图

@Controller('/user')
@Use(Auth())
export default class User {

  @Get()
  @Render('user')
  index() {
    return { content: 'hi' };
  }
}

请求参数验证

@Controller('/user')
@Role(SYS_ROLE.admin)
export default class User {

  @Post('/chname')
  @QuerySchame({
    username: joi.string().required(),
    password: joi.string().required(),
  })
  changeName(@Body() body: any, @Query() query: any) {
    return ResultUtils.ok(body);
  }
}

接口描述

接口文档默认访问地址: /swagger-ui/index.html

@Controller('/user')
@Description('用户信息')
export default class User {

  @Post('/hi')
  @Description('test')
  test() {}
}

计划任务 @CronJob

使用修饰器@CronJob执行计划任务

@Controller()
@Description('用户信息')
export default class User {

  @CronJob('* * * * * *', { onlyRunMaster: false }) // 多进程下是否只在master进程执行 default: true
  cron() {
    console.log('计划任务执行了!');
  }
}

API

createApplication(root, controllers, options): Application

  • 参数

    • { string } root - 项目路径
    • { string | controller[] } controllers - 控制器的目录位置,使用globs匹配,或者是控制器类的数组
    • { ApplicationOptions } options - 参考如下ApplicationOptions, 值false不启用功能
      interface ApplicationOptions {
        staticAssets?: { root: string; prefix?: string } | boolean; // default: disable https://github.com/koajs/static
        swagger?: { url: string; prefix?: string } | boolean; // swagger-ui
        bodyparser?: Bodyparser.Options | boolean; // https://github.com/koajs/bodyparser
        cors?: object | boolean; // https://github.com/koajs/cors
        hbs?: { viewPath?: string } | boolean; // default: disable https://github.com/koajs/koa-hbs
        helmet?: object | boolean; // https://github.com/venables/koa-helmet
      }
  • 返回值:Application

Application

实例方法

  • use(mid: Koa.Middleware)

  • listen(port: number)

  • getKoaInstance(): Koa

  • getHttpServer(): http.Server

http请求装饰器

内部使用koa-router中间件,提供对应的方法的装饰器

  • @Controller(string|void)

  • @POST(string|void)

  • @Get(string|void)

@Render(view: string) 模板渲染

使用Joi验证请求参数

  • BodySchame(schame: joi.AnySchema)

  • QuerySchame(schame: joi.AnySchema)

控制器处理方法参数注入

@Post('/test')
test(@Body() Body: any, @Query() query: any) {}
  

自定义装饰器

import { Use, Description } from 'souljs';
export default function Role(...roles: string[]) {
  const role = Use(async (ctx: Koa.Context, next: () => void) => {
    const sql = `
      SELECT R.code FROM role R
      LEFT JOIN user_roles UR ON UR.role_id = R.id
      WHERE UR.user_id = ?
    `;
    const userRoles = await db.query(sql, [signData.id]);
    if (roles.some(r => userRoles.find(ur => ur.code === r))) {
      await next();
    } else {
      throw new Error('no authorization!');
    }
  });

  const description = Description(`【${roles.join()}】`);

  return (target: any, propertyKey?: string) => {
    role(target, propertyKey);
    description(target, propertyKey);
  };
}  

// 使用装饰器
@Controller('/system/role')
@Role(SYS_ROLE.admin)
export class RoleController {
  @Get('/list')
  @Description('角色列表')
  async list() { }
}

提供如下修饰器

  • @Ctx() - ctx

  • @Request() - ctx.request

  • @Response() - ctx.Response

  • @Query(string|void) - ctx.request.query[string] | ctx.request.query

  • @Body(string|void) - ctx.request.body[string] | ctx.request.body

  • @ApplicationInstance() - 当前应用实例

  • @KoaInstance() - 当前koa实例