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

@aligames/maga-open

v1.0.0

Published

maga sdk for gameopen protocol

Readme

@aligames/maga-open

阿里游戏 API 网关 - Node.js SDK

NPM version build status Test coverage David deps Known Vulnerabilities NPM download

提供了 API 的请求封装、摘要签名、响应解释、消息监听等功能,使用 SDK 可以轻松完成 API 的调用,API 结果的获取。

安装依赖

仅支持 Node.js 6.x 以上版本。

npm i @aligames/maga-open --save

使用方法

作为消费者,发起请求

  • 通过 new Sdk.Client({ key, secret }) 进行初始化,需要提供 keysecret 配置。
  • 使用 yield client.request({ service, data, options }) 来封包,发送请求给服务端,以及解析服务端返回数据。
const Sdk = require('@aligames/maga-open');
const client = new Sdk.Client({
  key: '<your_app_key>',
  secret: '<your_app_secret>',
  // host: 'http://',
  // level: 'INFO',
  // timeout: 5000, // 超时时间,默认为 5000ms
  // required: true, // 遇到错误时,直接抛错还是返回错误对象,便于组合请求,默认为 true - 抛错
  // prefix: 'MAGA',
  // logfile: 'path/to/log/file',
  // logger: my_custom_logger,
});

const result = yield client.request({
  // API 名称,必须用 / 开头
  service: '/api/xxx',
  // 请求的数据体
  data: {
    uid: 'tz',
  },
});

// 返回的对象中,包含 { id, data, state, headers }
console.log(result.data);

并发组合请求:

const result = yield {
  api1: client.request({ service: '/api/xxx', data: { uid: 'tz' } }),
  api2: client.request({ service: '/api/yyy' }),
};

const data = {};
Object.keys(result).forEach(key => data[key] = result[key].data);

// 返回的对象中,包含 { api1: {}, api2: {} }
console.log(result.api1.data);

// 提取出所有 data
console.log(data);

错误处理:

try {
  const result = yield client.request({
    service: '/api/xxx',
    data: {
      uid: 'tz',
    },
  });
} catch (err) {
  // { message, code, data }
  console.error(err);
}

对特定请求配置:

const config = {
  // API 名称,必须用 / 开头
  service: '/api/xxx',
  // 请求的数据体
  data: {
    uid: 'tz',
  },
};

const result = yield client.request(config, {
  timeout: 100000,
  required: false,
});

// api1 是关键请求,失败了则整个失败,而 api2 请失败时,不会报错,此时 result.api2 是 error 对象
const combo = yield {
  api1: client.request({ service: '/api/xxx', data: { uid: 'tz' } }),
  api2: client.request({ service: '/api/yyy' }, { required: false }),
};

作为服务提供方,提供接口服务

  • 通过 new Sdk.Server({ keystore }) 进行初始化,需要提供 keystore 配置。
  • 使用 server.decode({ meta, payload }) 解析客户端传递过来的请求体,解析出错会抛错,请注意 try catch 处理。
  • 使用 server.response({ result, id, code, msg }) 对响应数据进行封包,返回 { meta, payload } 供开发者使用,前者用于配置 headers,后者是 buffer 直接返回给对端。
const Sdk = require('@aligames/maga-open');
const server = new Sdk.Server({
  // 允许访问的 app key 和 secret 值对
  keystore: {
    '<your_app_key>': '<your_app_secret>',
  },
  // level: 'INFO',
  // prefix: 'MAGA SERVER',
  // logfile: 'path/to/log/file',
  // logger: my_custom_logger,
});

// 获取客户端发送的请求对象,需传递 headers 和 req body buffer,进行解码,解析出错会抛错,请注意 `try catch` 处理。
const body = server.decode({ meta: ctx.headers, payload: rawBody });

// 进行业务逻辑处理,如查询数据库
const result = { uid: 'tz', from: 'server' };

try {
  // 对返回结果进行封包
  const { meta, payload } = server.response({ id: id || Date.now(), code, msg, result });

  // 返回 headers
  ctx.set(meta);

  // 返回请求体,buffer
  ctx.body = payload;

} catch (err) {
  // 需要自己处理报错后的返回
  ctx.status = err.code;
  ctx.body = err;
}

调试

我们给开发者提供了 cli 命令,用于本地发起请求。

$ maga-cli request --key=<key> --secret=<secret> <payload json string>
$ maga-cli encode --key=<key> --secret=<secret>  <payload json string>
$ maga-cli decode --key=<key> --secret=<secret>  <payload base64 string>

示例:

$ node ./node_modules/.bin/maga-cli --key=ngclient#2dcd --secret=wqjx0iXcRw2uEXdmjlruzw003 --host="http://localhost:7001" request '{"service":"/api/csbiz.account.findUserById?ver=1.0.0","data":{"uid":"tz"}}'

$ node bin/maga-cli.js --key=ngclient#2dcd --secret=wqjx0iXcRw2uEXdmjlruzw003 --host="http://100.84.254.233:7001" decode 'Rn+Cek0ATDXYJvkDxgiJ20+wRP4XdvFKcp4XXePyj+R83W9H+yct6LEIzrlP9cw6tohaF1a1AhcXnayIv+TfY18Kr7uJ8v9mdDBx1Efc3BUtDS3LJzW3BBhXBYeQ5C0B'

质量

我们的类库严格遵循 Semver 版本规则,故强烈建议开发者通过 ^ 的方式引入,即:

{
  "dependencies": {
    "@aligames/maga-open": "^1.0.0"
  }
}

单元测试覆盖率:

=============================== Coverage summary ===============================
Statements   : 100% ( 190/190 )
Branches     : 100% ( 80/80 )
Functions    : 100% ( 19/19 )
Lines        : 100% ( 188/188 )
================================================================================

服务端开发

参见示例: https://github.com/aliplay-team/maga-client-nodejs-open-example

推荐使用我们开源的『企业级的 Node.js Web 基础框架』 - eggjs

  • 官方地址:https://eggjs.org/
  • 介绍:https://zhuanlan.zhihu.com/p/25860846

反馈

  • 联系接口同学
  • 或提交 Issue