UNPKG

rspack-plugin-mock

Version:
164 lines (163 loc) 6.34 kB
import { A as ResponseBody, C as HandleFunction, D as MockResponse, E as MockRequest, M as CookiesOption, N as GetCookieOption, O as NextFunction, P as SetCookieOption, S as ExtraRequest, T as Method, _ as RecordedMeta, a as MockWebsocketItem, b as RecordedRes, c as MockHttpItem, d as LogType, f as MockMatchPriority, g as RecordOptions, h as ServerBuildOption, i as MockOptions, j as SimpleHandleFunction, k as NextHandleFunction, l as BodyParserOptions, m as MockServerPluginOptions, n as HttpProxyPlugin, o as WebSocketSetupContext, p as MockMatchSpecialPriority, r as PathFilter, s as MockErrorConfig, t as FormidableFile, u as LogLevel, v as RecordedReq, w as Headers, x as ResolvedRecordOptions, y as RecordedRequest } from "./index-BMMGM-eD.js"; import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http"; import { Transform } from "node:stream"; //#region src/helpers/createSSEStream.d.ts interface SSEMessage { data?: string | object; comment?: string; event?: string; id?: string; retry?: number; } interface WriteHeaders { writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders; flushHeaders?: () => void; } type HeaderStream = NodeJS.WritableStream & WriteHeaders; /** * Transforms "messages" to W3C event stream content. * See https://html.spec.whatwg.org/multipage/server-sent-events.html * A message is an object with one or more of the following properties: * - data (String or object, which gets turned into JSON) * - event * - id * - retry * - comment * * If constructed with a HTTP Request, it will optimise the socket for streaming. * If this stream is piped to an HTTP Response, it will set appropriate headers. */ declare class SSEStream extends Transform { constructor(req: IncomingMessage); pipe<T extends HeaderStream>(destination: T, options?: { end?: boolean; }): T; _transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void; write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean; write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean; destroy(error?: Error): this; } /** * 创建一个 Server-sent events 写入流,用于支持模拟 EventSource * * @example * ```ts * import { createSSEStream, defineMock } from 'vite-plugin-mock-dev-server' * * export default defineMock({ * url: '/api', * response: (req, res) => { * const sse = createSSEStream(req, res) * sse.write({ event: 'message', data: { message: 'hello world' } }) * } * }) * ``` */ declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream; //#endregion //#region src/helpers/defineMock.d.ts /** * Mock config Type helper * * mock配置 类型帮助函数 * @param config see config docs: * {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config en-US DOC} | * {@link https://vite-plugin-mock-dev-server.netlify.app/zh/guide/mock-config zh-CN DOC} * * @example * Mock Http Request * ```ts * export default defineMock({ * url: '/api/example', * method: ['GET', 'POST'], * body: { a: 1 }, * }) * ``` * ```ts * export default defineMock({ * url: '/api/example', * method: 'GET', * body: ({ query }) => ({ a: 1, b: query.b }), * }) * ``` * @example * Mock WebSocket * ```ts * export default defineMock({ * url: '/socket.io', * ws: true, * setup(wss) { * wss.on('connection', (ws) => { * ws.on('message', (rawData) => console.log(rawData)) * ws.send('data') * }) * }, * }) * ``` */ declare function defineMock(config: MockHttpItem): MockHttpItem; declare function defineMock(config: MockWebsocketItem): MockWebsocketItem; declare function defineMock(config: MockOptions): MockOptions; /** * Return a custom defineMock function to support preprocessing of mock config. * * 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。 * @param transformer preprocessing function * @example * ```ts * const definePostMock = createDefineMock((mock) => { * mock.url = '/api/post/' + mock.url * }) * export default definePostMock({ * url: 'list', * body: [{ title: '1' }, { title: '2' }], * }) * ``` */ declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock; //#endregion //#region src/helpers/defineMockData.d.ts /** * Options for defineMockData * * defineMockData 的选项 */ interface DefineMockDataOptions { /** * Whether to persist the data value on HMR (Hot Module Replacement). * * 热更新时是否保持数据值 * * @default false */ persistOnHMR?: boolean; } /** * Mock data type with getter, setter, and value property * * 带有 getter、setter 和 value 属性的 Mock 数据类型 * * @template T - Type of mock data / Mock 数据的类型 */ type MockData<T = any> = readonly [() => T, (val: T | ((val: T) => T | void)) => void] & { /** * Current value * * 当前值 */ value: T; }; /** * Define mock data with memory-based sharing mechanism * * 定义带有基于内存的共享机制的 Mock 数据 * * @template T - Type of mock data / Mock 数据的类型 * @param key - Unique key for mock data / Mock 数据的唯一键 * @param initialData - Initial data value / 初始数据值 * @param options - Options / 选项 * @returns MockData object with getter, setter, and value property / 带有 getter、setter 和 value 属性的 MockData 对象 */ declare function defineMockData<T = any>(key: string, initialData: T, options?: DefineMockDataOptions): MockData<T>; //#endregion export { BodyParserOptions, CookiesOption, DefineMockDataOptions, ExtraRequest, FormidableFile, GetCookieOption, HandleFunction, HeaderStream, Headers, HttpProxyPlugin, LogLevel, LogType, Method, MockData, MockErrorConfig, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, NextFunction, NextHandleFunction, PathFilter, RecordOptions, RecordedMeta, RecordedReq, RecordedRequest, RecordedRes, ResolvedRecordOptions, ResponseBody, SSEMessage, ServerBuildOption, SetCookieOption, SimpleHandleFunction, WebSocketSetupContext, createDefineMock, createSSEStream, defineMock, defineMockData };