
Research
/Security News
Popular Tinycolor npm Package Compromised in Supply Chain Attack Affecting 40+ Packages
Malicious update to @ctrl/tinycolor on npm is part of a supply-chain attack hitting 40+ packages across maintainers
ext-xhr-interceptor
Advanced tools
一个强大的 XMLHttpRequest 拦截器,支持请求和响应的拦截、修改和监听。使用 Proxy 技术实现,无需修改现有代码即可拦截所有 XHR 请求。
npm install ext-xhr-interceptor
或者使用 yarn:
yarn add ext-xhr-interceptor
你也可以通过 script 标签直接在浏览器中使用:
<script src="https://unpkg.com/ext-xhr-interceptor@latest/dist/index.browser.js"></script>
或者下载到本地使用:
<script src="./path/to/index.browser.js"></script>
import { interceptXHR } from 'ext-xhr-interceptor';
// 拦截所有包含 '/api' 的请求
interceptXHR({
targetUrl: '/api',
onRequest: (config) => {
console.log('拦截到请求:', config);
},
onResponse: (response) => {
console.log('拦截到响应:', response);
}
});
<script src="https://unpkg.com/ext-xhr-interceptor@latest/dist/index.browser.js"></script>
<script>
// 通过全局变量 XHRInterceptor 访问
XHRInterceptor.interceptXHR({
targetUrl: '/api',
onRequest: (config) => {
console.log('拦截到请求:', config);
},
onResponse: (response) => {
console.log('拦截到响应:', response);
}
});
// 或者使用解构赋值
const { interceptXHR } = XHRInterceptor;
interceptXHR({
targetUrl: '/api',
onRequest: (config) => {
console.log('拦截到请求:', config);
}
});
</script>
import { interceptXHR } from 'ext-xhr-interceptor';
interceptXHR({
// 支持多种匹配方式
targetUrl: ['/api', /\.json$/, 'https://example.com'],
// 请求前处理
beforeRequest: (config) => {
// 修改请求头
config.headers['X-Custom-Header'] = 'value';
// 修改请求体
if (config.body) {
config.body.extraField = 'extra value';
}
return config;
},
// 响应后处理
afterResponse: (response) => {
// 修改响应数据
if (response.status === 200) {
response.response.extraField = 'response value';
}
return response;
},
// 请求监听
onRequest: (config) => {
console.log('请求配置:', {
method: config.method,
url: config.url,
headers: config.headers,
body: config.body,
query: config.query
});
},
// 响应监听
onResponse: (response) => {
console.log('响应数据:', {
status: response.status,
headers: response.headers,
response: response.response,
matchedPattern: response.matchedPattern
});
},
// 错误处理
onError: (error, phase) => {
console.error(`${phase}阶段发生错误:`, error);
}
});
interceptXHR(options: XHRInterceptorOptions)
options.targetUrl
: string | RegExp | Array<string | RegExp>
- 目标 URL 匹配模式options.beforeRequest?
: (config: RequestConfig) => RequestConfig | Promise<RequestConfig>
- 请求前处理函数options.afterResponse?
: (response: ResponseConfig) => ResponseConfig | Promise<ResponseConfig>
- 响应后处理函数options.onRequest?
: (config: RequestConfig) => void
- 请求监听函数options.onResponse?
: (response: ResponseConfig) => void
- 响应监听函数options.onError?
: (error: Error, phase: 'request' | 'response') => void
- 错误处理函数interface RequestConfig {
method: any;
url: string;
query?: Record<string, string>;
headers: Record<string, string>;
body: any;
withCredentials?: boolean;
}
interface ResponseConfig<T = any> {
status: number;
headers: Record<string, string>;
response: T;
matchedPattern: string | RegExp;
requestConfig: RequestConfig;
}
interceptXHR({
targetUrl: '/api',
onRequest: (config) => {
console.log('API 请求:', config);
},
onResponse: (response) => {
console.log('API 响应:', response);
}
});
interceptXHR({
targetUrl: '/api/auth',
beforeRequest: (config) => {
// 自动添加认证头
config.headers['Authorization'] = `Bearer ${getToken()}`;
return config;
}
});
interceptXHR({
targetUrl: /\.json$/,
afterResponse: (response) => {
// 统一处理响应数据
if (response.status === 200 && response.response.code === 0) {
return response;
} else {
throw new Error(response.response.message || '请求失败');
}
}
});
interceptXHR({
targetUrl: ['/api', '/v1'],
onError: (error, phase) => {
// 上报错误到监控系统
reportError({
type: 'xhr_error',
phase,
error: error.message,
timestamp: Date.now()
});
}
});
# 安装依赖
npm install
# 开发模式
npm run dev
# 构建
npm run build
# 清理构建文件
npm run clean
MIT License
欢迎提交 Issue 和 Pull Request!
FAQs
一个强大的 XMLHttpRequest 拦截器,支持请求和响应的拦截、修改和监听
The npm package ext-xhr-interceptor receives a total of 15 weekly downloads. As such, ext-xhr-interceptor popularity was classified as not popular.
We found that ext-xhr-interceptor 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.
Research
/Security News
Malicious update to @ctrl/tinycolor on npm is part of a supply-chain attack hitting 40+ packages across maintainers
Security News
pnpm's new minimumReleaseAge setting delays package updates to prevent supply chain attacks, with other tools like Taze and NCU following suit.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.