1.0.4 • Published 2 years ago
vite-plugin-interceptor v1.0.4
vite-plugin-interceptor
English | 中文
A vite plugin based on service worker to intercept and modify requests in a development environment
the browser needs to support service worker
a chrome extension can be used in production environment. easy-interceptor
Usage
npm i -D vite-plugin-interceptor
// in vite.config.ts
import interceptor from 'vite-plugin-interceptor'
export default defineConfig({
plugins: [
// interceptor()
// or
interceptor({
input: 'src/index.ts', // default src/main.ts
mockDir: '__mock__', // default mock
}),
]
})
// new a ts or js file in __mock__ dir,must be use export default and set rules by plugin provide
export default function() {
window.__INTERCEPTOR_DEVTOOL__.set(
'1',
{
url: '**/tsconfig.json',
method: 'get',
response: '{"foo":"test"}',
// enable: false,
// delay: 5000,
}
)
}
API
__INTERCEPTOR_DEVTOOL__
prop | explain | type | description |
---|---|---|---|
get | get rules | ()=>Promise | |
set | set a rule | (id, rule) => void | |
clear | clear all rules | ()=>void |
InterceptorRule
prop | explain | type | description |
---|---|---|---|
url | match request url | string | 必填 |
response | response text | string | 必填 |
delay | delay | number | 0 |
enable | enable rule | boolean | true |
method | request method | get post put delete | |
responseHeaders | response headers | Record<string, string> | {} |
status | response status | number | 200 |
perhaps, it has a warn in vite2.0, you can use
as any as Plugin
to resolve it.about multi-page: the sw.js scope is
./
, so the plugin supports multi-page. just load the configured page to make the script active