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

@nfly/router

v1.2.0

Published

路由模块, 面向切面编程, 依赖注入的形式开发node服务

Downloads

8

Readme

Install

pnpm add @nfly/router

Usage

Register it if you need.

# Controller
import {
  GET, Body, Service, Path, Query, Params, Before, After, Middleware,
} from '@nfly/router';
import { BaseController } from '@nfly/core';
import HomeService from '../../service/Home';

function Test(target: any, key: symbol|string, des: any) {
  const old = des.value;
  // eslint-disable-next-line no-param-reassign
  des.value = async function (...args: any) {
    console.log('func start');
    await old.apply(this, args);
    console.log('func end');
  };
}

/**
 * After Before用户装饰类 每个路由前或者后执行
 */
@Path('/user')
@After(async (ctx) => {
  console.log('after each methods');
})
@Before(async (ctx) => {
  console.log('before each methods');
  // ctx.body = '没有权限';
  // 如果return true; 会中断该下所有路由执行 否则正常执行
  // return true;
})
@Middleware(async (ctx, next) => {
  // class all methods use the middleware
  await next();
})
class Home extends BaseController {
  @Service(HomeService)
    homeServer!: HomeService;

  /**
   * api path [/getUserInfo]
   */
  @GET()
  async getUserInfo() {
    this.json({
      code: 0,
      message: '用户信息',
      data: {
        name: 'my name',
        age: 100,
      },
    });
  }

  /**
   * api path [/info]
   */
  @GET('/info')
  async renameUserInfo() {
    this.json({
      code: 0,
      message: '自定义路由',
      data: {
        name: 'my rename',
        age: 100,
      },
    });
  }

  /**
   * api path [/:id]
   */
  @Test
  @Middleware(async (ctx, next) => {
    console.log('this router use the middleware');
    await next();
  })
  @GET('/last/:id')
  async index(@Query('name') name: string, @Query() query: any, @Params('id') id: string) {
    const list = await this.homeServer.getName();
    this.success({
      name,
      id,
      query,
      list,
    });
  }
}

export default Home;
# Service
import { BaseService } from '@nfly/core';
import { Model } from '@nfly/orm';
import { Repository } from 'typeorm';

import { Photo } from '../../model/home';

class HomeService extends BaseService {
  @Model<Photo>(Photo)
    home!: Repository<Photo>;

  async getName() {
    const data = await this.home.find();
    return data;
  }
}

export default HomeService;

class Decorator

|Decorator|description| |-|-| |@Path(params: string)| class root path;| | @Middleware(params: Middleware) | koa middleware; | | @After(params: IContext) |on call after each method | | @before(params: IContext) |on call before each method |

method Decorator

|Decorator|description|other Decorators| |-|-|-| |@GET(params?: string)| router path;|@POST;@PUT;@DELETE;...| | @Middleware(params: Middleware) | koa middleware; || | @Query(args: string|string[]) | get fields from ctx |@Params;@Body;@Headers;@Cookies|

attribute Decorator

|Decorator|description| |-|-| |@Model| typeorm Model| | @Service | BaseService |

use @nfly/cli create demo.