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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ys-helper

v1.0.5

Published

**此插件包更名为ys-helper,具备的功能如下**

Readme

此插件包更名为ys-helper,具备的功能如下

  1. 接口助手
    • 支持数据格式统一
    • 支持中间件
  2. 异常助手

#调用方式


var ysHelper = require('ys-helper'),
    api1 = ysHelper.apiInit('v1'),
    api2 = ysHelper.apiInit('v2'),
    api3 = ysHelper.apiInit('v3'),
    api4 = ysHelper.apiInit('v4'),
    errors = ysHelper.errors;

ap1.post({
    action: '....',
    param1: '....',
    param2: '....',
}).then(function(data){
    console.log(data)
}).catch(errors.ApiError, function(err){
    //可以通过异常类型处理错误
    console.log(err);
}).catch(function(err){
    console.log(err);
});

#中间件定义

var Promise = require('bluebird');
var TestMiddleWare = (function() {
  function TestMiddleWare() {}

  //定义接口在请求开始前的处理
  TestMiddleWare.prototype.start = function(request) {
    console.log('request start');
    console.log(request.params);
    return Promise.resolve();
  };
  
  //定义接口在数据已处理完成准备请求前的处理
  TestMiddleWare.prototype.afterMakeRequest = function(request) {
    console.log(request.params);
    return Promise.resolve();
  };

  //定义接口请求结束后的处理
  TestMiddleWare.prototype.end = function(request) {
    console.log('request end');
    return Promise.resolve();
  };
  
  //定义接口请求异常时的处理
  TestMiddleWare.prototype.error = function(request, err) {
    console.log('request error', err);
    return Promise.resolve();
  };

  return TestMiddleWare;

})();

#中间件使用

var middlewares = [
    [TestMiddleWare]
]
var api = ysHelper.apiInit(version, middlewares);

#为何需要异常助手

  1. 分流错误处理,每个控制器的编写再也不需要去考虑我这个异常该怎么处理,那个错误该怎么返回
  2. 保持promise格式的优雅,有异常有错误都是直接到catch中,只要一路写then即可

#异常助手说明

  1. errors.HttpError -- 接口http访问异常
    • message -- 错误信息
    • version -- api版本
    • action -- 接口名称
  2. errors.ApiError -- 接口信息异常
    • message -- 错误信息
    • version -- api版本
    • action -- 接口名称
  3. errors.TipsError -- 告知程序此异常可通知于用户
    • message -- 错误信息

#异常助手使用栗子

###后端代码

app.post('/getData', function(req, res, next){
    api.post({
        action: 'xxxxx',
        params: 'xxxxx'
    }).then(function(data){
        if(!data.status){
            return Promise.reject(new errors.TipsError(data.msg));
        }
        return api.post({
            action: 'xxxxx',
            params: 'xxxxx'
        });
    }).then(function(data){
        res.json({status: true, data: data});
    }).catch(next);
});

app.use(function(err, req, res, next){
    if(err instanceof errors.TipsError){
        res.json({status: false, errors: 1, tips: err.message});
    }else if(err instanceof otherError){
        res.json({status: false, errors: 2})
    }else{
        res.status(500).render('error');
    }
});

###前端全局代码

$(document).ajaxError(function(){
    alert('系统繁忙,请稍后重试');
}).ajaxSuccess(function(event, xhr){
    var data = JSON.parse(xhr.responseText);
    if(!data.status){
        switch(data.errors){
            case 1:
                alert(data.tips);
                break;
            case 2:
                alert(....);
                break;
            default:
                ....
        }
    }
});

###前端页面代码

$.post('/getData', {param: 'xxxx'}, function(data){
    if(!data.status) return; //此处直接return,交由全局处理自动处理
    //此处处理正常逻辑,开发人员对于异常/错误提示的处理可以集中在全局,从此精力可完全集中于正常的逻辑处理
}, 'json');

#接口开发说明 接口最基础的类是api/Tools.coffee,下一层是api/httpHelper.https.coffee,最终api接口继承httpHelper的Request类

Tools.coffee是用来集中api接口处理数据时所需的各种方法

httpHelper.coffee是用来程序最终访问http接口的处理,其中make_request是在请求前处理要发送的数据信息,dispose_data用来处理请求后最终返回的数据格式

每个api版本继承了Request后,通过重写make_request和dispose_data来处理不同版本间的差异

接口约定的返回格式统一为


{
    status: 'bool', 
    raw: '接口返回的原始数据', 
    data: '开发者可以直接拿来处理的数据', 
    msg: 'status为false的信息'
}