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

@micl/net

v0.0.3

Published

这是一个基于 **Axios / 微信小程序 request** 封装的通用请求工具,提供:

Readme

@micl/net 请求工具库

这是一个基于 Axios / 微信小程序 request 封装的通用请求工具,提供:

  • Axios REST 请求统一封装
  • 微信小程序(WX)请求封装
  • 请求别名(Alias)机制
  • 自动注入 TransId / Header
  • 可中断(Abort)的 Promise
  • 统一后端返回结构 ServerResult<T>

适用于 Web / Node / 微信小程序 项目。


📦 模块导出

export { AxiosRest } from './rest';
export { WXRest } from './rest.wx';
export default PromiseWithAbort;
export type { ServerResult };

🧱 后端通用返回结构

export interface ServerResult<T> {
  code?: number;
  success?: boolean;
  message?: string;
  msgId?: string;
  data?: T;
  status?: {
    code?: number;
    desc?: string;
  };
  timestamp?: number;
}

推荐后端统一使用该结构,便于前端泛型约束。


⛔ PromiseWithAbort

一个可中断的 Promise,基于 Axios CancelToken 实现。

class PromiseWithAbort<T> extends Promise<T> {
  source?: CancelTokenSource;
  abort(message?: string): void;
}

使用示例

const req = AxiosRest.get<User>('/api/user');

setTimeout(() => {
  req.abort('用户取消请求');
}, 1000);

🌐 AxiosRest(Web / Node)

获取实例

import { AxiosRest } from '@micl/net';

AxiosRest 为单例对象。


基础配置

AxiosRest.config({
  logger: console,
  axios: {
    baseURL: '/api',
    timeout: 10000
  },
  useAlias: true,
  useTransId: true,
  onRequest: (config) => {
    console.log('request', config);
  },
  onResponse: (res) => {
    console.log('response', res);
  }
});

配置项说明

| 参数 | 说明 | |----|----| | logger | 日志对象(console / 自定义) | | axios | Axios 原生配置 | | useAlias | 是否启用别名 | | useTransId | 是否自动注入 TransId | | onRequest | 请求拦截回调 | | onResponse | 响应拦截回调 | | bodyConfig | body 处理配置或函数 |


Alias 别名配置

AxiosRest.alias({
  '@user': {
    url: '/user/info',
    headers: {
      token: 'xxx'
    }
  }
});

使用:

AxiosRest.get<User>('getUser');

发起请求

request

AxiosRest.request<User>({
  url: '/user',
  method: 'GET'
});

get

AxiosRest.get<User>('/user', { id: 1 });

post

AxiosRest.post<User>('/user', {
  name: 'Tom'
});

终止所有请求

AxiosRest.abortAll();

适用于路由切换、页面销毁等场景。


📱 WXRest(微信小程序)

引入

import { WXRest } from '@micl/net';

基础配置

WXRest.config({
  context: wx,
  appId: 'your-app-id',
  headers: {
    token: 'xxx'
  },
  noHttpCache: true,
  logger: console
});

Alias 配置

WXRest.alias({
  login: {
    url: '/login'
  }
});

发起请求

GET

WXRest.get<User>('/user', { id: 1 });

POST

WXRest.post<User>('/user', {
  name: 'Tom'
});

返回值统一为:

Promise<ServerResult<T>>

请求去重 & 缓存

WXRest 内部维护请求缓存:

  • 相同 URL + 参数的请求可自动复用
  • 避免短时间内重复请求

适合小程序页面频繁触发请求的场景。


🔧 工具函数

generateUUID()

生成唯一 UUID。

import { generateUUID } from '@micl/net';

const id = generateUUID();

✅ 设计目标

  • 强类型(TypeScript 泛型)
  • 可中断请求
  • 多端统一 API(Web / WX)
  • 统一后端返回结构
  • 易于扩展与维护

📄 License

MIT