Skip to content

refactor: compose shared Wecom client and add subpath exports - #5

Open
witjs wants to merge 16 commits into
nextfrom
refactor/compose-client-and-subpath-exports
Open

refactor: compose shared Wecom client and add subpath exports#5
witjs wants to merge 16 commits into
nextfrom
refactor/compose-client-and-subpath-exports

Conversation

@witjs

@witjs witjs commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Summary

Architecture refactor for the WeCom SDK client layer:

Before

  • Business modules (User, Message, Agent, …) inherited Wecom
  • Sharing transport/token across modules relied on Wecom.setGlobal() or constructing many clients with duplicated config
  • Package only exported the root barrel (no first-class subpath imports)

After

  • WecomModule composition: modules take ClientSource = Partial<WecomConfig> | Wecom and share one Wecom instance
  • RequestKernel: shared request/retry/error policy used by Wecom and unauthenticated clients like Webhook
  • createClient / createScope: preferred factories for shared clients and multi-tenant scopes (no process-wide globals)
  • Suite.corp / corpWecom / createCorpClient and Hardware.device / deviceWecom / createDeviceClient for typed corp/device helpers
  • package.json exports: wecom/user, wecom/message, wecom/client, … plus matching tsdown entries

Compatibility

  • new User(config) / new Message(config) still work
  • Modules also accept an existing Wecom instance
  • Message supports default agentId (override per send / payload)
  • Wecom.setGlobal() remains but is deprecated in favor of createClient / createScope

Examples

import { createClient } from 'wecom';
// or: import { User } from 'wecom/user';

const client = createClient({
  corpId: process.env.CORPID!,
  corpSecret: process.env.TEST_SECRET!,
  agentId: Number(process.env.TEST_AGENT_ID),
});
await client.user.get('alice');
await client.message.send({ touser: 'alice', msgtype: 'text', text: { content: 'hi' } });
const corp = suite.corp({ authCorpId, permanentCode });
const message = new Message(corp);
// or: suite.createCorpClient({ authCorpId, permanentCode, agentId })

Test plan

  • pnpm install && pnpm typecheck
  • pnpm test (unit + contract + types; mock fetch)
  • Spot-check createClient sharing (single token refresh across modules)
  • Spot-check new User(config) / new Message(wecom) compatibility
  • Spot-check subpath import import { User } from 'wecom/user' after pnpm build
  • Docs pages for config / identities / suite still render

Notes

Pushed via GitHub MCP (create_branch + push_files / create_or_update_file) because shell had no usable GH_TOKEN for git push. Branch tip may include multiple incremental commits rather than the single local 4823fd7 commit; content targets the local refactor tree.

A few guide/API doc pages and large test files (identity / modules / endpoints) may still need a follow-up sync if CI shows drift vs local /workspace/wecom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant