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 🙏

© 2025 – Pkg Stats / Ryan Hefner

gsc-frontend-sdk

v0.0.82

Published

这是GSC前端SDK,提供与后端服务通信的工具和APIs。

Readme

GSC Frontend SDK

这是GSC前端SDK,提供与后端服务通信的工具和APIs。

功能特性

  • 基于Fetch API的HTTP客户端(替代Axios)
  • RPC通信
  • 应用程序上下文管理
  • 安全通信支持

安装

npm install gsc-frontend-sdk

使用示例

import { RpcClient } from 'gsc-frontend-sdk';

// 创建一个RPC客户端实例
const client = RpcClient.New('user-service')
  .setPath('User', 'getInfo')
  .setHeader('appID', 'my-app');

// 获取数据
client.get('user-123')
  .then(response => {
    console.log('用户信息:', response);
  })
  .catch(error => {
    console.error('获取数据失败:', error);
  });

开发

安装依赖

npm install

测试

项目使用Jest进行单元测试。可以运行以下命令执行测试:

# 运行所有测试
npm test

# 监视模式
npm run test:watch

# 生成测试覆盖率报告
npm run test:coverage

测试覆盖了以下关键模块:

  • fetchClient: HTTP客户端,基于原生Fetch API实现
  • RpcClient: RPC通信客户端
  • Application: 应用程序上下文管理

测试策略

我们采用了两种测试策略:

  1. 功能测试:直接测试模块功能,如fetchClient模块的方法存在性和基本行为
  2. 模拟测试:对于依赖复杂的模块,我们采用模拟类进行隔离测试,确保核心功能正确

这种组合策略既能测试关键模块的真实行为,又能确保测试的稳定性和可靠性。

测试示例

以下是一些测试示例供参考:

fetchClient测试(直接功能测试)

/**
 * fetchClient 模块的单元测试
 */
import fetchClient from '../../../gfw/util/fetchClient.js';

describe('fetchClient', () => {
  test('应该提供所有标准HTTP方法', () => {
    expect(typeof fetchClient.get).toBe('function');
    expect(typeof fetchClient.post).toBe('function');
    expect(typeof fetchClient.put).toBe('function');
    expect(typeof fetchClient.patch).toBe('function');
    expect(typeof fetchClient.delete).toBe('function');
  });
  
  test('create方法应该返回包含HTTP方法的实例', () => {
    const instance = fetchClient.create();
    
    expect(typeof instance.get).toBe('function');
    expect(typeof instance.post).toBe('function');
    expect(typeof instance.put).toBe('function');
    expect(typeof instance.patch).toBe('function');
    expect(typeof instance.delete).toBe('function');
  });
});

RpcClient测试(模拟测试)

/**
 * RpcClient 模块的单元测试
 */
describe('RpcClient 基本功能测试', () => {
  // 创建一个模拟的RpcClient类用于测试
  class MockRpcClient {
    constructor(service) {
      this.service = service;
      this.class = '';
      this.action = '';
    }
    
    static New(service) {
      return new MockRpcClient(service);
    }
    
    setPath(className, actionName) {
      this.class = className;
      this.action = actionName;
      return this;
    }
  }
  
  test('工厂方法应该创建RpcClient实例', () => {
    const client = MockRpcClient.New('test-service');
    expect(client instanceof MockRpcClient).toBe(true);
    expect(client.service).toBe('test-service');
  });
  
  test('setPath方法应支持链式调用', () => {
    const client = MockRpcClient.New('test-service');
    const result = client.setPath('TestClass', 'testMethod');
    
    expect(result).toBe(client);
    expect(client.class).toBe('TestClass');
    expect(client.action).toBe('testMethod');
  });
});

主要改进

  • 移除Axios依赖:使用原生Fetch API实现HTTP客户端,减轻了包大小并改善了性能
  • 模块化设计:清晰的代码结构和接口设计,方便扩展和维护
  • ES模块支持:完全基于ES模块,更好地支持现代JavaScript开发
  • 测试框架:添加了Jest测试框架和基础测试用例,提高代码质量
  • 依赖更新:更新了所有依赖到最新版本,提高安全性

技术栈

  • ES模块(使用type: "module"
  • Fetch API(原生支持Promise和现代浏览器)
  • Jest(测试框架)
  • Babel(用于测试环境的代码转换)

贡献

欢迎提交问题和Pull Request!