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

sunnier

v0.3.5

Published

A lightweight inversion of control container for Node.js apps powered by TypeScript and Koa runtime

Downloads

6

Readme

sunnier

⚙A lightweight inversion of control container for Node.js apps powered by TypeScript and Koa runtime.

Support:

  • Typescript ✅
  • IOC ✅
  • HTTP Server ✅
  • HTTP Decorator Router ✅
  • AOP Route Plugins✅
  • Global Plugins ✅
  • Params Decorator ✅
  • Shield app.ts, add config mode ✅
  • more Decorator 🤩

Quick Start

Install

npm install sunnier typescript --save

step0

write tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "removeComments": true,
    "preserveConstEnums": true,
    "target": "es2018",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "declaration": true,
    "outDir": "./lib",
    "declarationDir": "./dts",
    "noEmitOnError": true
  },
  "include": ["src/**/*"]
}

step1

define constants

//  src/app/constants/index.ts
export const USER = Symbol.for('USER')

step2

create a model

//  src/app/models/User.ts
export namespace Model {
  export class UserModel {
    id: Number
    name: String
    age: Number
    sex: Number //0 男 1 女
  }
}

step3

create a injectable service

//  src/src/service/User.ts
import { Injectable } from 'sunnier'
import { USER } from '../constants'
import { Model } from '../models'
import { UserInterface } from '../interface'
@Injectable(USER)
export default class User implements UserInterface {
  private users: Array<Model.UserModel>
  constructor() {
    this.users = [
      {
        id: 0,
        name: '张三',
        age: 18,
        sex: 0
      },
      {
        id: 1,
        name: '李四',
        age: 19,
        sex: 1
      },
      {
        id: 2,
        name: '王二',
        age: 20,
        sex: 1
      },
      {
        id: 3,
        name: '麻子',
        age: 21,
        sex: 0
      }
    ]
  }
  getUser(id: number): Model.UserModel {
    return this.users.find(user => user.id === id)
  }
}

step4

create a controller and inject User service

//  src/app/controllers/UserController.ts
import { BaseController, Controller, Get, Inject, Params } from 'sunnier'
import { USER } from '../constants'

@Controller()
export default class UserController extends BaseController {
  @Inject(USER)
  private user

  @Get('/getUser')
  public async getUser(@Params(['query']) query) {
    const data = await this.user.getUser(Number(query.id))
    let result
    if (data) {
      result = {
        code: 200,
        message: 'success',
        data
      }
    } else {
      result = {
        code: 0,
        message: `id为${query.id}的用户未找到`,
        data: null
      }
    }
    this.ctx.body = result
  }
  @Post('/postTest')
  async postTest() {
    this.ctx.body = {
      method: 'post'
    }
  }
  @Put('/putTest')
  async putTest() {
    this.ctx.body = {
      method: 'put'
    }
  }
  @Delete('deleteTest')
  async deleteTest() {
    this.ctx.body = {
      method: 'delete'
    }
  }
  @All('allTest')
  async allTest() {
    this.ctx.body = {
      method: 'all'
    }
  }
}

step5

write config file

// src/config/index.ts
export default () => {
  let options = {
    port: 9000,
    host: '127.0.0.1'
    plugins: [],
  }

  return options
}

step6

startup server

//package.json
{
  "scripts": {
    "start": "sunnier-bin --dir=./src --ts",
    "prod": "sunnier-bin --dir=./dist"
  }
}

npm run start

open 127.0.0.1:9000/getUser?id=1 in browser

{
  "code": 200,
  "message": "success",
  "data": {
    "id": 1,
    "name": "李四",
    "age": 19,
    "sex": 1
  }
}

step7

golbal middleware

async function text(ctx, next) {
  console.log('middleware')
  next()
}
module.exports = {
  plugins: [text],
  port: 9000,
  loadPath: {
    controller: './dist/controllers',
    service: './dist/service'
  }
}

step8

aop based on route,

import { Controller, Inject, Get, BaseController } from 'sunnier'
import { TEST_CONTROLLER } from './constants'

async function before(ctx, next) {
  console.log('before')
  await next()
}
async function after(ctx, next) {
  console.log('after')
  await next()
}

export class UserController extends BaseController {
  @Inject(USER)
  private user
  constructor() {}
  @Get('/getUser', {
    before: [before],
    after: [after]
  })
  async getUser() {
    const result = this.user.getUser(1)
    this.ctx.body = {
      code: 200,
      noerr: '',
      data: {
        result
      }
    }
    await this.next()
  }
}

Params Decorator

@Headers

get request headers

async getUest(@Headers(['host']) host) {
  console.log(host) //127.0.0.1
  const result = this.user.getUser(1)
  this.ctx.body = {
    code: 200,
    noerr: '',
    data: {
      result
    }
  }
  await this.next()
}

@Req

get http request

async getUser(@Req() req) {
  console.log(req)
}

@Res

get http response

async getUser(@Res() res) {
  console.log(res)
}

@Cookie

get cookie

async getUser(@Cookie() cookie) {
  console.log(cookie)
}

@Params

get http request parameters, include:body、params、query.

async getUser(@Params() args) {
  console.log(args.body)
  console.log(args.params)
  console.log(args.query)
}