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

mm_ret

v1.4.6

Published

这是超级美眉http请求结果输出类函数模块,用于输出json-rpc2.0标准结果

Downloads

68

Readme

mm_ret

npm version License

一个用于生成符合 JSON-RPC 2.0 标准的响应和请求的 Node.js 模块。

特性

  • JSON-RPC 2.0 标准兼容 - 完全符合 JSON-RPC 2.0 规范
  • 多种响应类型 - 支持对象、列表、布尔值等响应格式
  • 错误码管理 - 内置标准错误码和自定义错误码
  • 请求模板 - 支持请求参数模板功能
  • 轻量级 - 无依赖,体积小巧
  • TypeScript 支持 - 提供完整的类型定义

安装

npm install mm_ret

快速开始

基本使用

const { Ret, Req, Error } = require('mm_ret');

// 生成成功响应
const successResponse = Ret.body('操作成功');
console.log(successResponse);
// 输出: {"result":"操作成功"}

// 生成错误响应
const errorResponse = Ret.error(10000, '参数错误');
console.log(errorResponse);
// 输出: {"error":{"code":10000,"message":"参数错误"}}

// 生成请求
const req = new Req('user');
const requestBody = req.send('getUser', { id: 123 });
console.log(requestBody);
// 输出: {"jsonrpc":"2.0","method":"user.getUser","params":{"id":123},"id":"..."}

API 文档

Ret 类 - 响应生成器

生成符合 JSON-RPC 2.0 标准的响应对象。

方法

body(result, error, id)

生成基础的 JSON-RPC 响应体。

参数:

  • result {any} - 返回结果
  • error {Object} - 错误信息(可选)
  • id {String} - 请求ID(可选)

返回值: {Object} JSON-RPC 响应对象

error(code, message, id)

生成错误响应。

参数:

  • code {Number} - 错误码
  • message {String} - 错误信息
  • id {String} - 请求ID(可选)

返回值: {Object} 包含错误信息的响应对象

list(list, count, id)

生成列表类型的响应。

参数:

  • list {Array} - 数据列表
  • count {Number} - 总数量(可选)
  • id {String} - 请求ID(可选)

返回值: {Object} 包含列表数据的响应对象

obj(obj, id)

生成对象类型的响应。

参数:

  • obj {Object} - 返回的对象
  • id {String} - 请求ID(可选)

返回值: {Object} 包含对象数据的响应对象

bl(bl, tip, id)

生成布尔类型的响应。

参数:

  • bl {Boolean} - 布尔值
  • tip {String} - 提示信息(可选)
  • id {String} - 请求ID(可选)

返回值: {Object} 包含布尔值和提示信息的响应对象

Req 类 - 请求生成器

生成符合 JSON-RPC 2.0 标准的请求对象。

构造函数

new Req(scope = 'sys')

参数:

  • scope {String} - 作用域,默认为"sys"

方法

send(method, params)

生成 JSON-RPC 请求。

参数:

  • method {String} - 方法名
  • params {Object} - 参数对象

返回值: {Object} 标准的 JSON-RPC 请求对象

Error 类 - 错误码管理

管理 JSON-RPC 标准错误码和自定义错误码。

方法

get(keyword)

根据关键词或错误码获取对应的错误信息。

参数:

  • keyword {String|Number} - 关键词或错误码

返回值: {Object} 错误信息对象,包含 code 和 message

使用示例

响应示例

const { Ret } = require('mm_ret');

// 成功响应
const success = Ret.body({ message: '操作成功' });
// {"result":{"message":"操作成功"}}

// 错误响应
const error = Ret.error(400, '参数验证失败');
// {"error":{"code":400,"message":"参数验证失败"}}

// 列表响应
const list = Ret.list([{ id: 1, name: '用户1' }, { id: 2, name: '用户2' }], 2);
// {"result":{"list":[{...}],"count":2}}

// 对象响应
const obj = Ret.obj({ id: 1, name: '张三', age: 25 });
// {"result":{"obj":{...}}}

// 布尔响应
const bool = Ret.bl(true, '操作成功');
// {"result":{"bl":true,"tip":"操作成功"}}

请求示例

const { Req } = require('mm_ret');

// 基本请求
const req = new Req('user');
const request = req.send('getProfile', { userId: 123 });
// {"method":"user.getProfile","params":{"userId":123},"id":"..."}

// 使用模板
const reqWithTemplate = new Req('message');
reqWithTemplate.tpl.message = {
    to_user: '',
    from_user: '',
    content: ''
};
const templateRequest = reqWithTemplate.send('message', { 
    content: 'Hello!',
    media: { title: '重要通知' }
});
// 自动合并模板字段

错误码参考

标准 JSON-RPC 错误码

| 错误码 | 说明 | |--------|------| | -32700 | 格式错误 | | -32701 | 编码不支持 | | -32702 | 无效字符 | | -32600 | 无效请求 | | -32601 | 方法不存在 | | -32602 | 无效参数 | | -32603 | 内部错误 | | -32500 | 应用错误 | | -32400 | 系统错误 | | -32300 | 传输错误 |

自定义错误码

| 错误码 | 说明 | |--------|------| | 10000 | 业务逻辑错误 | | 30000 | 身份验证失败 | | 40000 | 数据库执行错误 | | 50000 | 服务端执行错误 | | 70000 | 参数不正确 |

测试

npm test

许可证

ISC License

贡献

欢迎提交 Issue 和 Pull Request!

更新日志

查看 GitHub Releases 获取详细更新信息。