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

@ai-request-guard/core

v1.0.1

Published

Frontend anti-corruption layer — isolate backend DTOs from view models with the adapter pattern

Readme

@ai-request-guard/core

前端防腐层核心运行时。提供 adapter 注册、mock 系统、schema 校验,以及开发模式下的真实请求拦截能力。

安装

pnpm add @ai-request-guard/core

基本用法

import AIRequestGuard from '@ai-request-guard/core'

// 注册 adapter
AIRequestGuard.register('user-detail', (raw) => {
  const r = raw as Record<string, unknown>
  const dept = (r.dept ?? {}) as Record<string, unknown>
  return {
    id: r.user_id as number,
    userName: (r.username as string) ?? '',
    mobile: (r.phone_no as string) ?? '',
    deptName: (dept.dept_name as string) ?? '未知部门',
    age: Number(r.age ?? 0),
    avatar: (r.avatar as string) ?? 'https://example.com/default.png',
    createTime: (r.create_time as string) ?? '',
  }
})

// 发起请求
const user = await AIRequestGuard({
  id: 'user-detail',
  request: () => fetch('/api/user/detail').then(r => r.json()),
  schema: { id: 0, userName: '', mobile: '', deptName: '', age: 0, avatar: '', createTime: '' },
})

Mock

// 全局切换到 mock 模式
AIRequestGuard.setMode('mock')

// 单接口 mock(静态数据)
const user = await AIRequestGuard({
  id: 'user-detail',
  request: () => fetch('/api/user/detail').then(r => r.json()),
  mode: 'mock',
  mockData: { user_id: 1, username: 'test', phone_no: '138xxxxxxxx', age: '28' },
})

// 工厂函数 mock
const user = await AIRequestGuard({
  id: 'user-detail',
  request: () => fetch('/api/user/detail').then(r => r.json()),
  mode: 'mock',
  mockData: (id) => ({ user_id: 0, username: `mock-${id}`, phone_no: '', age: '0' }),
})

真实请求拦截(配合 Vite 插件)

// 注册 URL 监听规则,GET 请求命中时自动上报 raw 数据
AIRequestGuard.watch('/api/user/detail', 'user-detail')
AIRequestGuard.watch(/\/api\/order\/\d+/, 'order-detail')

API 速览

| 方法 | 说明 | |---|---| | AIRequestGuard(options) | 发起请求并经 adapter 转换,返回 ViewModel | | AIRequestGuard.register(id, fn) | 注册 adapter | | AIRequestGuard.configure(config) | 更新全局配置(mode / dev)| | AIRequestGuard.setMode(mode) | 切换全局请求模式 | | AIRequestGuard.watch(pattern, id) | 注册 URL 拦截规则(dev only)| | AIRequestGuard.clearWatch() | 清空拦截规则(dev only)| | validateSchema(id, data, schema) | 手动执行 schema diff 校验 | | hasDiff(diff) | 判断 diff 结果是否有需关注的差异 | | getDiffRecords() | 获取内存中所有 diff 记录 | | clearDiffRecords() | 清空 diff 记录 |

生产构建

mock 代码、schema 校验、fetch 拦截在生产构建中通过 __DEV__ 标志 tree-shake,不进入线上产物,零额外开销。

配合 Rollup @rollup/plugin-replace 使用:

replace({ __DEV__: "false", preventAssignment: true })

License

MIT