Skip to content

配置

所有模块都继承 Wecom,构造函数接受同一份 WecomConfigAgent / AgentMenu 额外要求 agentId

ts
import { User } from 'wecom';

const user = new User({
  corpId: process.env.CORPID!,
  corpSecret: process.env.DIRECTORY_SECRET!,
  timeout: 10_000,
  retryTimes: 2,
});

字段

参数类型必填默认值说明
corpIdstring自建时是企业 ID
corpSecretstring自建时是应用 Secret
tokenProviderTokenProvider外部换票;传入后不再要求 corpSecret
tokenParamTokenParamaccess_token自动附加的 query 名
baseURLstringhttps://qyapi.weixin.qq.com/cgi-bin/接口前缀,会自动补 /
retryTimesnumber3可恢复错误的额外重试次数,允许 0
timeoutnumber30000单次请求超时,毫秒
headersRecord<string, string>{}额外请求头
fetchtypeof fetch全局 fetch自定义传输,便于测试或代理
tokenStoreTokenStore内存缓存可替换的 Token 存储
loggerWecomLoggerdebug / info / warn / error
signalAbortSignal全局取消信号

校验规则:

  • 自建应用强制 corpIdcorpSecret;传入 tokenProvider 时改为用外部换票
  • retryTimes 必须是大于等于 0 的有限数字
  • timeout 必须是大于 0 的有限数字

全局配置

Wecom.setGlobal() 仍然可用,但已标记为 deprecated。优先在每个客户端上显式传配置。

ts
import { Wecom, User } from 'wecom';

Wecom.setGlobal({
  corpId: process.env.CORPID!,
  corpSecret: process.env.TEST_SECRET!,
});

const user = new User();

实例配置会覆盖全局配置;headers 会浅合并。

自定义 fetch

需要走代理、记录流量或写单测时,传入自己的 fetch

ts
const wecom = new Wecom({
  corpId,
  corpSecret,
  fetch: async (input, init) => {
    const url = new URL(String(input));
    url.hostname = 'proxy.example.com';
    return fetch(url, init);
  },
});

请求级选项

request() 不再接受 Axios 配置,改用:

ts
await wecom.request({
  url: '/user/get',
  method: 'GET',
  params: { userid: 'alice' },
  data: undefined,
  headers: { 'X-Debug': '1' },
  timeout: 5000,
  signal,
  skipAuth: false,
  responseType: 'json',
});

下载素材时把 responseType 设为 'arrayBuffer'。HTTP 206 视为成功。

基于 MIT 协议发布