vite-plugin-mock-dev-server 2.0.6 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/helper.d.mts CHANGED
@@ -1,3 +1,126 @@
1
- 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-DF6NPJx4.mjs";
2
- import { a as HeaderStream, i as defineMock, n as defineMockData, o as SSEMessage, r as createDefineMock, s as createSSEStream, t as MockData } from "./index-u_3ZZqGO.mjs";
1
+ 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";
2
+ import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "node:http";
3
+ import { Transform } from "node:stream";
4
+
5
+ //#region src/helper/createSSEStream.d.ts
6
+ interface SSEMessage {
7
+ data?: string | object;
8
+ comment?: string;
9
+ event?: string;
10
+ id?: string;
11
+ retry?: number;
12
+ }
13
+ interface WriteHeaders {
14
+ writeHead?: (statusCode: number, headers?: OutgoingHttpHeaders) => WriteHeaders;
15
+ flushHeaders?: () => void;
16
+ }
17
+ type HeaderStream = NodeJS.WritableStream & WriteHeaders;
18
+ /**
19
+ * Transforms "messages" to W3C event stream content.
20
+ * See https://html.spec.whatwg.org/multipage/server-sent-events.html
21
+ * A message is an object with one or more of the following properties:
22
+ * - data (String or object, which gets turned into JSON)
23
+ * - event
24
+ * - id
25
+ * - retry
26
+ * - comment
27
+ *
28
+ * If constructed with a HTTP Request, it will optimise the socket for streaming.
29
+ * If this stream is piped to an HTTP Response, it will set appropriate headers.
30
+ */
31
+ declare class SSEStream extends Transform {
32
+ constructor(req: IncomingMessage);
33
+ pipe<T extends HeaderStream>(destination: T, options?: {
34
+ end?: boolean;
35
+ }): T;
36
+ _transform(message: SSEMessage, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
37
+ write(message: SSEMessage, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
38
+ write(message: SSEMessage, cb?: (error: Error | null | undefined) => void): boolean;
39
+ destroy(error?: Error): this;
40
+ }
41
+ /**
42
+ * 创建一个 Server-sent events 写入流,用于支持模拟 EventSource
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { createSSEStream, defineMock } from 'vite-plugin-mock-dev-server'
47
+ *
48
+ * export default defineMock({
49
+ * url: '/api',
50
+ * response: (req, res) => {
51
+ * const sse = createSSEStream(req, res)
52
+ * sse.write({ event: 'message', data: { message: 'hello world' } })
53
+ * }
54
+ * })
55
+ * ```
56
+ */
57
+ declare function createSSEStream(req: IncomingMessage, res: ServerResponse): SSEStream;
58
+ //#endregion
59
+ //#region src/helper/defineMock.d.ts
60
+ /**
61
+ * mock config Type helper
62
+ *
63
+ * mock配置 类型帮助函数
64
+ * @param config see config docs:
65
+ * {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config en-US DOC} |
66
+ * {@link https://vite-plugin-mock-dev-server.netlify.app/zh/guide/mock-config zh-CN DOC}
67
+ *
68
+ * @example
69
+ * Mock Http Request
70
+ * ```ts
71
+ * export default defineMock({
72
+ * url: '/api/example',
73
+ * method: ['GET', 'POST'],
74
+ * body: { a: 1 },
75
+ * })
76
+ * ```
77
+ * ```ts
78
+ * export default defineMock({
79
+ * url: '/api/example',
80
+ * method: 'GET',
81
+ * body: ({ query }) => ({ a: 1, b: query.b }),
82
+ * })
83
+ * ```
84
+ * @example
85
+ * Mock WebSocket
86
+ * ```ts
87
+ * export default defineMock({
88
+ * url: '/socket.io',
89
+ * ws: true,
90
+ * setup(wss) {
91
+ * wss.on('connection', (ws) => {
92
+ * ws.on('message', (rawData) => console.log(rawData))
93
+ * ws.send('data')
94
+ * })
95
+ * },
96
+ * })
97
+ * ```
98
+ */
99
+ declare function defineMock(config: MockHttpItem): MockHttpItem;
100
+ declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
101
+ declare function defineMock(config: MockOptions): MockOptions;
102
+ /**
103
+ * Return a custom defineMock function to support preprocessing of mock config.
104
+ *
105
+ * 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
106
+ * @param transformer preprocessing function
107
+ * @example
108
+ * ```ts
109
+ * const definePostMock = createDefineMock((mock) => {
110
+ * mock.url = '/api/post/' + mock.url
111
+ * })
112
+ * export default definePostMock({
113
+ * url: 'list',
114
+ * body: [{ title: '1' }, { title: '2' }],
115
+ * })
116
+ * ```
117
+ */
118
+ declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
119
+ //#endregion
120
+ //#region src/helper/defineMockData.d.ts
121
+ type MockData<T = any> = readonly [() => T, (val: T | ((val: T) => T | void)) => void] & {
122
+ value: T;
123
+ };
124
+ declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
125
+ //#endregion
3
126
  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 };
package/dist/helper.mjs CHANGED
@@ -1,3 +1,4 @@
1
- import { i as createSSEStream, n as createDefineMock, r as defineMock, t as defineMockData } from "./helper-ChmPhNrY.mjs";
1
+ import{deepClone as e,deepEqual as t,isArray as n,isFunction as r}from"@pengzhanbo/utils";import{Transform as i}from"node:stream";var a=class extends i{constructor(e){super({objectMode:!0}),e.socket.setKeepAlive(!0),e.socket.setNoDelay(!0),e.socket.setTimeout(0)}pipe(e,t){return e.writeHead&&(e.writeHead(200,{"Content-Type":`text/event-stream; charset=utf-8`,"Transfer-Encoding":`identity`,"Cache-Control":`no-cache`,Connection:`keep-alive`}),e.flushHeaders?.()),e.write(`:ok
2
2
 
3
- export { createDefineMock, createSSEStream, defineMock, defineMockData };
3
+ `),super.pipe(e,t)}_transform(e,t,n){e.comment&&this.push(`: ${e.comment}\n`),e.event&&this.push(`event: ${e.event}\n`),e.id&&this.push(`id: ${e.id}\n`),e.retry&&this.push(`retry: ${e.retry}\n`),e.data&&this.push(o(e.data)),this.push(`
4
+ `),n()}write(e,...t){return super.write(e,...t)}destroy(e){return e&&this.write({event:`error`,data:e.message}),this.end(),this}};function o(e){return typeof e==`object`?o(JSON.stringify(e)):e.split(/\r\n|\r|\n/).map(e=>`data: ${e}\n`).join(``)}function s(e,t){let n=new a(e);return n.pipe(t),n}function c(e){return e}function l(e){return t=>(t=n(t)?t.map(t=>e(t)||t):e(t)||t,t)}const u=new Map,d=new WeakMap;var f=class{value;#e;#t;constructor(t){this.value=t,this.#e=e(t),this.#t=Date.now()}hotUpdate(n){Date.now()-this.#t<70||t(n,this.#e)||(this.value=n,this.#e=e(n),this.#t=Date.now())}};function p(e,t){let n=u.get(e);if(!n){let r=new f(t),i=u.get(e);i?n=i:(u.set(e,r),n=r)}if(n.hotUpdate(t),d.has(n))return d.get(n);let i=[()=>n.value,e=>{r(e)&&(e=e(n.value)??n.value),n.value=e}];return Object.defineProperty(i,`value`,{get(){return n.value},set(e){n.value=e}}),d.set(n,i),i}export{l as createDefineMock,s as createSSEStream,c as defineMock,p as defineMockData};
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- 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-DF6NPJx4.mjs";
2
- import { a as HeaderStream, i as defineMock, n as defineMockData, o as SSEMessage, r as createDefineMock, s as createSSEStream, t as MockData } from "./index-u_3ZZqGO.mjs";
3
- import { a as createLogger, c as processRawData, i as Logger, l as sortByValidator, n as CreateMockMiddlewareOptions, o as logLevels, r as createMockMiddleware, s as processMockData, t as mockWebSocket } from "./server-CvrNrvvb.mjs";
1
+ 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";
2
+ import { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from "./helper.mjs";
3
+ import { a as createLogger, c as processRawData, i as Logger, l as sortByValidator, n as CreateMockMiddlewareOptions, o as logLevels, r as createMockMiddleware, s as processMockData, t as mockWebSocket } from "./server-lwszq3fl.mjs";
4
4
  import { Plugin } from "vite";
5
5
 
6
6
  //#region src/plugin.d.ts