
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
@snack-kit/lib
Advanced tools
企业级通用工具库,提供 HTTP 客户端、调试面板、通用工具函数等模块,全栈 TypeScript,支持 ESM / CJS / UMD 三种格式。
npm install @snack-kit/lib
| 入口 | 说明 |
|---|---|
@snack-kit/lib | 聚合入口,导出全部模块 |
@snack-kit/lib/http | HTTP 客户端、Context 路由映射 |
@snack-kit/lib/debugger | 开发调试面板 |
@snack-kit/lib/utils | 通用工具函数 |
import { Get, Post, Context } from '@snack-kit/lib/http'
// 加载路由映射(生产环境无需传参,自动使用当前 Origin)
await Context.load()
// 发起请求
const { data, error } = await Get<{ name: string }>('/api/user/1', { ctx: 'user-svc' })
if (error) console.error(error.message)
else console.log(data?.name)
所有方法均支持两种调用形式:
// 位置参数形式
Get(url, config?)
Post(url, data?, config?)
Put(url, data?, config?)
Patch(url, data?, config?)
Del(url, config?)
// 配置对象形式(method 已预置,无需传入)
Get({ url, ...config })
Post({ url, data, ...config })
示例:
import { Get, Post, Del } from '@snack-kit/lib/http'
// GET
const { data } = await Get('/api/users', { ctx: 'user-svc' })
// POST - 位置参数
await Post('/api/user', { name: 'Alice' }, { ctx: 'user-svc' })
// POST - 配置对象
await Post({ url: '/api/user', data: { name: 'Alice' }, ctx: 'user-svc' })
// DELETE
await Del('/api/user/1')
import { Context } from '@snack-kit/lib/http'
// 生产环境:无参调用,自动以当前页面 Origin 为网关请求 /ngw/context
await Context.load()
// 指定网关地址
await Context.load('http://172.16.32.155:20000')
// 本地注入(测试 / Mock 场景)
await Context.load({ 'user-svc': 'http://user.api.com' })
Context.get('user-svc') // 'http://user.api.com'
Context.info // { version, domain, client, lang }
Context.loaded // true
import { Get, Cancel, CancelAll } from '@snack-kit/lib/http'
let cancelId = ''
Get('/api/list', { onCancelId: (id) => { cancelId = id } })
Cancel(cancelId) // 取消单个请求
CancelAll() // 取消所有进行中的请求
http 模块同时导出了原始 axios 实例及其常用类型,无需单独安装 axios:
import { axios } from '@snack-kit/lib/http'
import type { AxiosRequestConfig, AxiosResponse, AxiosInstance, AxiosError } from '@snack-kit/lib/http'
// 直接使用 axios 实例
const res = await axios.get('https://api.example.com/data')
在开发环境挂载调试面板,支持切换网关、选择目标服务,并自动加载对应的路由映射。
import { Debugger } from '@snack-kit/lib/debugger'
// 仅在非生产环境加载
if (import.meta.env.DEV) {
// 配置对象形式(可设置 timeout 等选项)
await Debugger.init({
gateways: [
'http://dev-gateway.example.com',
'http://test-gateway.example.com',
],
timeout: 5000,
})
// 数组简写形式(等价写法)
await Debugger.init([
'http://dev-gateway.example.com',
'http://test-gateway.example.com',
])
}
// 生产环境正常调用,Debugger 未激活时自动以 Origin 加载
await Context.load()
调用
Debugger.init()后,Context.load()无参调用会自动跳过,由调试面板接管加载时机。
import { IsNumber, IsString, IsArray, IsObject, IsNull, IsEqual } from '@snack-kit/lib/utils'
IsNumber('42') // true
IsObject([]) // false(数组不算对象)
IsNull(undefined) // true
IsEqual({ a: 1 }, { a: 1 }) // true
import { IsEmail, IsPhone, IsUrl, IsIpv4, REGEX } from '@snack-kit/lib/utils'
IsEmail('user@example.com') // true
IsPhone('13812345678') // true
IsUrl('https://example.com') // true
REGEX.color.test('#fff') // true
import { Unique, UniqueByKey, Minus } from '@snack-kit/lib/utils'
Unique([1, 2, 1, 3]) // [1, 2, 3]
UniqueByKey([{ id: 1 }, { id: 1 }, { id: 2 }], 'id') // [{ id: 1 }, { id: 2 }]
Minus([1, 2, 3], [2, 3]) // [1]
import { DeepClone, CleanObject, Pick, Omit } from '@snack-kit/lib/utils'
DeepClone({ a: { b: 1 } }) // 深拷贝新对象
CleanObject({ a: 1, b: null, c: undefined }) // { a: 1 }
Pick({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { a: 1, c: 3 }
Omit({ a: 1, b: 2, c: 3 }, ['b']) // { a: 1, c: 3 }
import { Debounce, Throttle, Delay } from '@snack-kit/lib/utils'
const handleInput = Debounce((val: string) => search(val), 500)
const handleScroll = Throttle(() => updatePosition(), 100)
await Delay(1000) // 等待 1 秒
import { UUID, GetURLParam, GetURLParams, ObjectToQuery, QueryToObject } from '@snack-kit/lib/utils'
UUID() // 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
GetURLParam('page', 'https://example.com?page=2') // '2'
GetURLParams('https://example.com?a=1&b=2') // { a: '1', b: '2' }
ObjectToQuery({ page: 1, size: 10 }) // '?page=1&size=10'
QueryToObject('?a=1&b=2') // { a: '1', b: '2' }
import { FormatDate, GetDateOffset, GetDayRange } from '@snack-kit/lib/utils'
FormatDate(new Date(), 'yyyy-MM-dd') // '2026-03-02'
GetDateOffset(new Date(), -7, 'yyyy-MM-dd') // 7 天前的日期
GetDayRange(new Date()) // { start: 1740844800000, end: 1740931199999 }
| 命令 | 说明 |
|---|---|
npm run build | 构建生产包(ESM + CJS + UMD + 类型声明) |
npm run build:watch | 监听模式构建 |
npm run test | 运行单元测试 |
npm run test:coverage | 运行测试并生成覆盖率报告 |
npm run docs | 生成 API 文档 |
npm run lint | TypeScript 类型检查 |
npm run release | 构建并发布到 npm |
执行 npm run docs 后在 docs/api/index.html 查看完整 API 文档。
MIT
Ws(url | config, event) 创建连接,WsClose(id) 关闭指定连接,WsCloseAll() 关闭所有连接WsRequestConfig、WsEventName、WsEventCallback<T>Ws 支持 ctx 自动从 Context 获取 host 拼接地址,自动将 http(s):// 转换为 ws(s)://Ws message 事件自动尝试 JSON 解析,支持泛型 <T> 类型推导CopyToClipboard(str): Promise<boolean>,优先使用 navigator.clipboard.writeText(),自动降级为 execCommandREGEXPRULES 完整正则规则集,覆盖数字/英文/中文、身份证、邮箱、电话、URL、IP、时间、颜色等分类Debugger.init() 新增数组简写入参形式,直接传入网关 URL 数组等价于 { gateways: [...] }Context 内部使用独立 axios 实例,隔离外部拦截器对上下文加载请求的污染Debugger 内部使用独立 axios 实例,隔离外部拦截器对调试面板请求的污染utils 模块,包含以下子模块(详见 0.2.0 条目)HttpMethodConfig 类型、原始 axios 导出(详见 0.4.0 条目)Get / Post / Put / Patch / Del 新增配置对象入参形式,method 由方法预置无需重复传入HttpMethodConfig(Omit<HttpRequestConfig, 'method'>)axios 实例及 AxiosRequestConfig / AxiosResponse / AxiosInstance / AxiosError 类型,无需单独安装 axiosContext.load() 参数改为可选:
Origin 为网关请求 /ngw/contextDEBUGGER_ACTIVE_KEY 常量,供 Debugger 模块写入全局标志位Debugger.init() 执行时向 globalThis 写入激活标志位DEBUGGER_ACTIVE_KEYutils 模块,包含以下子模块:
detect:类型检测(IsNumber / IsString / IsArray / IsObject / IsNull / IsEqual 等)validate:输入校验与常用正则(IsEmail / IsPhone / IsUrl / IsIpv4 / REGEX 等)array:数组工具(Unique / UniqueByKey / Minus)object:对象工具(DeepClone / CleanObject / Pick / Omit)func:函数工具(Debounce / Throttle / Delay)string:字符串工具(UUID / GetURLParam / GetURLParams / ObjectToQuery / QueryToObject)time:时间工具(FormatDate / GetDateOffset / GetDayRange)tsup.config.ts 与 package.json 新增 utils 独立打包入口与导出字段Request / Get / Post / Put / Patch / DelContext / Origin / CtxCancel / CancelAllDebugger.init()FAQs
Enterprise-grade utility library
We found that @snack-kit/lib demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.