vite-plugin-mock-dev-server
Version:
Vite Plugin for API mock dev server.
126 lines (125 loc) • 4.68 kB
text/typescript
import { _ as WebSocketSetupContext, a as LogType, c as MockMatchPriority, d as MockRequest, f as MockResponse, g as ServerBuildOption, h as ResponseBody, i as LogLevel, l as MockMatchSpecialPriority, m as MockWebsocketItem, n as ExtraRequest, o as Method, p as MockServerPluginOptions, r as FormidableFile, s as MockHttpItem, t as BodyParserOptions, u as MockOptions } from "./types-C8ZwTU-4.mjs";
import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http";
import { Transform } from "node:stream";
//#region src/helper/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/helper/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/helper/defineMockData.d.ts
type MockData<T = any> = readonly [() => T, (val: T | ((val: T) => T | void)) => void] & {
value: T;
};
declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
//#endregion
export { BodyParserOptions, ExtraRequest, FormidableFile, HeaderStream, LogLevel, LogType, Method, MockData, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, SSEMessage, ServerBuildOption, WebSocketSetupContext, createDefineMock, createSSEStream, defineMock, defineMockData };