createDefineMock(transformer)
Create a custom defineMock function for preprocessing Mock configurations.
Usage
ts
import path from 'node:path'
import { createDefineMock } from 'vite-plugin-mock-dev-server'
const definePostMock = createDefineMock((mock) => {
mock.url = path.join('/api/post', mock.url)
})
export default definePostMock({
url: 'list', // The path will be concatenated as /api/post/list
body: {
list: []
}
})Parameters
transformer
- Type:
(mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void - Required: Yes
Preprocessing function that receives a Mock configuration object. You can modify and return it, or return a new configuration object.
Use Cases
- Unified path prefix: Avoid repeating the same prefix in each configuration
- Unified response headers: Add unified response headers to all Mocks
- Unified delay: Add unified response delay to all Mocks
More Details
See API Reference - createDefineMock for complete type definitions and more examples.
