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

@xdmp-open/sdk-core

v0.1.6

Published

XDMP platform SDK core: request engine contracts and response types

Downloads

1,321

Readme

@xdmp-open/sdk-core

XDMP 平台 SDK 核心:基于 axios 的请求引擎,统一拆包 CommonResponse、单飞 token 刷新,以及可注入的适配器契约(token provider / notifier / storage)。不硬依赖 Vue、Element Plus 或 DOM —— 副作用一律由宿主注入。

兼容性

0.x 对齐 XDMP 平台 2.6.x。破坏性变更升 major。

安装

pnpm add @xdmp-open/sdk-core

用法

import { createSdkClient } from '@xdmp-open/sdk-core'

const client = createSdkClient({
  baseURL: '/',                    // 网关地址
  tokenProvider: {
    // 返回完整 Authorization 头,例如 `Bearer xxx`
    getAccessToken: () => session.accessToken,
    getRefreshToken: () => session.refreshToken,
    setTokens: (accessToken, refreshToken, tokenType, expiresIn) => {
      session.accessToken = `${tokenType ?? 'Bearer'} ${accessToken}`
      session.refreshToken = refreshToken
      // 刷新响应里若有 expiresIn(秒),原样写入
      if (expiresIn != null) session.expiresAt = Date.now() + expiresIn * 1000
    },
    clear: () => { session.accessToken = null; session.refreshToken = null },
    // 可选:令牌临期时主动刷新
    shouldRefresh: () => session.expiresAt - Date.now() < 5 * 60 * 1000,
  },
  notifier: {                      // 可选,通常接到 ElMessage
    error: (msg) => ElMessage.error(msg),
    warning: (msg) => ElMessage.warning(msg),
  },
  onUnauthorized: () => router.push('/login'),  // 可选
  // tokenRefreshPath: '/api/passport/login/refresh',  // 默认值
  // messages: { serverError: '...' },               // 覆盖内置文案
})

// 所有方法返回拆包后的载荷(CommonResponse.data);失败以 SdkError 拒绝
// { code, message, reqId?, httpStatus? }
const user = await client.get<UserVO>('/api/system/users/user/query/admin')
const created = await client.post<UserVO>('/api/system/users/user/add', form)
await client.upload('/api/system/binaries/binary/add', formData)
const { data, headers } = await client.download('/api/system/departs/export')

// 匿名接口(登录、验证码)跳过 Authorization 头:
await client.post('/api/passport/login', payload, { skipAuth: true })

行为要点:

  • 拆包:HTTP 2xx 且 code === '2000000' 时 resolve data;其它业务码以 SdkError reject。
  • Token 刷新:鉴权请求遇到 HTTP 401/424 或业务码 4010001 时触发一次共享刷新(并发请求挂起,带新 token 重放);刷新失败则清 token,且 onUnauthorized 只调一次。skipAuth 匿名请求(登录、验证码)不走刷新,直接提示后端文案。
  • 错误分级:403/400/404 → notifier.warning,5xx / 网络 / 超时 → notifier.error;文案默认中文,可通过 config.messages 覆盖。单次请求 silent: true 可关掉提示。
  • Blob 错误体会解析(走 blob.text(),浏览器与 Node 18+ 可用),下载失败仍能露出后端 message。

配合 @xdmp-open/sdk-admin

import { createSdkClient } from '@xdmp-open/sdk-core'
import { createAdminClient } from '@xdmp-open/sdk-admin'

const client = createSdkClient({ /* ... */ })
const admin = createAdminClient(client)
const page = await admin.system.user.page({ pageNo: 1, pageSize: 20, form: {} })

测试

测试通过 axiosInstance 注入 axios-mock-adapter:

const instance = axios.create()
const mock = new MockAdapter(instance)
const client = createSdkClient({ baseURL: '/', tokenProvider, axiosInstance: instance })