UNPKG

rspack-plugin-mock

Version:
123 lines (118 loc) 4.2 kB
import { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.cjs'; export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, c as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.cjs'; import { OutgoingHttpHeaders, IncomingMessage, ServerResponse } from 'node:http'; import { Transform } from 'node:stream'; import 'co-body'; import 'cookies'; import 'cors'; import 'formidable'; import 'node:buffer'; import 'ws'; /** * mock config Type helper * * mock配置 类型帮助函数 * @param config see config docs: * {@link https://vite-plugin-mock-dev-server.netlify.app/en/guide/mock-config en-US DOC} | * {@link https://vite-plugin-mock-dev-server.netlify.app/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; type MockData<T = any> = readonly [ /** * getter */ () => T, /** * setter */ (val: T | ((val: T) => T | void)) => void ] & { value: T; }; declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>; 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; } declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream; export { type HeaderStream, type MockData, MockHttpItem, MockOptions, MockWebsocketItem, type SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData };