Skip to content
On this page

类型格式

Commonjs

使用 Commonjs 格式编写 mock 配置文件

js
/**
 * @type {import('vite-plugin-mock-dev-server').MockHttpItem}
 */
module.exports = {
  url: '/api/common-js',
  body: {
    message: 'Write mock configuration using a CommonJs file.',
  },
}
/**
 * @type {import('vite-plugin-mock-dev-server').MockHttpItem}
 */
module.exports = {
  url: '/api/common-js',
  body: {
    message: 'Write mock configuration using a CommonJs file.',
  },
}

ESModule

使用 ESModule 编写 mock 文件

js
import { defineMock } from 'vite-plugin-mock-dev-server'

export default defineMock({
  url: '/api/javascript',
  body: {
    message: 'Write mock configuration using a js file.',
  },
})
import { defineMock } from 'vite-plugin-mock-dev-server'

export default defineMock({
  url: '/api/javascript',
  body: {
    message: 'Write mock configuration using a js file.',
  },
})
ts
import { defineMock } from 'vite-plugin-mock-dev-server'

export default defineMock([
  {
    url: '/api/typescript',
    body: {
      message: 'Write mock configuration using a typescript file.',
    },
  },
])
import { defineMock } from 'vite-plugin-mock-dev-server'

export default defineMock([
  {
    url: '/api/typescript',
    body: {
      message: 'Write mock configuration using a typescript file.',
    },
  },
])
js
/**
 * @type {import('vite-plugin-mock-dev-server').MockOptions}
 */
export default [
  {
    url: 'api/post/1',
    body: {}
  },
  {
    url: 'api/post/2',
    body: {}
  }
]
/**
 * @type {import('vite-plugin-mock-dev-server').MockOptions}
 */
export default [
  {
    url: 'api/post/1',
    body: {}
  },
  {
    url: 'api/post/2',
    body: {}
  }
]
ts
import { defineMock } from 'vite-plugin-mock-dev-server'

export default defineMock([
  {
    url: 'api/post/1',
    body: {}
  },
  {
    url: 'api/post/2',
    body: {}
  }
])
import { defineMock } from 'vite-plugin-mock-dev-server'

export default defineMock([
  {
    url: 'api/post/1',
    body: {}
  },
  {
    url: 'api/post/2',
    body: {}
  }
])

JSON

使用 JSON/JSON5 编写 mock 文件

json
{
  "url": "/api/json",
  "headers": {
    "X-Custom-Header": "your custom header"
  },
  "body": {
    "message": "Write mock configuration using a json file."
  }
}
{
  "url": "/api/json",
  "headers": {
    "X-Custom-Header": "your custom header"
  },
  "body": {
    "message": "Write mock configuration using a json file."
  }
}
json5
{
  url: "/api/json5",
  headers: {
    "X-Custom-Header": "your custom header"
  },
  body: {
    message: "Write mock configuration using a json5 file."
  },
}
{
  url: "/api/json5",
  headers: {
    "X-Custom-Header": "your custom header"
  },
  body: {
    message: "Write mock configuration using a json5 file."
  },
}

Released under the MIT License.