msw-fetch-mock 0.3.0 → 0.3.2

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.
Files changed (46) hide show
  1. package/README.md +72 -6
  2. package/dist/browser.cjs +43 -0
  3. package/dist/browser.d.cts +15 -0
  4. package/dist/browser.d.ts +15 -9
  5. package/dist/browser.js +42 -7
  6. package/dist/chunk-3RAWYKAG.js +551 -0
  7. package/dist/chunk-IOGQ34QL.js +27 -0
  8. package/dist/chunk-N6B7UP6B.cjs +551 -0
  9. package/dist/chunk-PJU5FUNI.cjs +58 -0
  10. package/dist/chunk-PUVZ7LB4.js +58 -0
  11. package/dist/chunk-QKIKWNFZ.cjs +27 -0
  12. package/dist/fetch-mock-DhiqmHdF.d.cts +199 -0
  13. package/dist/fetch-mock-DhiqmHdF.d.ts +199 -0
  14. package/dist/index.cjs +19 -0
  15. package/dist/index.d.cts +2 -0
  16. package/dist/index.d.ts +2 -3
  17. package/dist/index.js +19 -1
  18. package/dist/legacy.cjs +71 -0
  19. package/dist/legacy.d.cts +38 -0
  20. package/dist/legacy.d.ts +38 -0
  21. package/dist/legacy.js +71 -0
  22. package/dist/node.cjs +19 -0
  23. package/dist/node.d.cts +18 -0
  24. package/dist/node.d.ts +17 -10
  25. package/dist/node.js +19 -12
  26. package/docs/api.md +121 -13
  27. package/docs/msw-v1-legacy.md +94 -0
  28. package/package.json +34 -10
  29. package/dist/browser-adapter.d.ts +0 -10
  30. package/dist/browser-adapter.d.ts.map +0 -1
  31. package/dist/browser-adapter.js +0 -20
  32. package/dist/browser.d.ts.map +0 -1
  33. package/dist/fetch-mock.d.ts +0 -31
  34. package/dist/fetch-mock.d.ts.map +0 -1
  35. package/dist/fetch-mock.js +0 -347
  36. package/dist/index.d.ts.map +0 -1
  37. package/dist/mock-call-history.d.ts +0 -65
  38. package/dist/mock-call-history.d.ts.map +0 -1
  39. package/dist/mock-call-history.js +0 -140
  40. package/dist/node-adapter.d.ts +0 -11
  41. package/dist/node-adapter.d.ts.map +0 -1
  42. package/dist/node-adapter.js +0 -34
  43. package/dist/node.d.ts.map +0 -1
  44. package/dist/types.d.ts +0 -74
  45. package/dist/types.d.ts.map +0 -1
  46. package/dist/types.js +0 -1
@@ -0,0 +1,199 @@
1
+ interface MockCallHistoryLogData {
2
+ body: string | null;
3
+ method: string;
4
+ headers: Record<string, string>;
5
+ fullUrl: string;
6
+ origin: string;
7
+ path: string;
8
+ searchParams: Record<string, string>;
9
+ protocol: string;
10
+ host: string;
11
+ port: string;
12
+ hash: string;
13
+ }
14
+ declare class MockCallHistoryLog {
15
+ readonly body: string | null;
16
+ readonly method: string;
17
+ readonly headers: Record<string, string>;
18
+ readonly fullUrl: string;
19
+ readonly origin: string;
20
+ readonly path: string;
21
+ readonly searchParams: Record<string, string>;
22
+ readonly protocol: string;
23
+ readonly host: string;
24
+ readonly port: string;
25
+ readonly hash: string;
26
+ constructor(data: MockCallHistoryLogData);
27
+ json(): unknown;
28
+ toMap(): Map<string, string | null | Record<string, string>>;
29
+ toString(): string;
30
+ }
31
+ interface CallHistoryFilterCriteria {
32
+ method?: string;
33
+ path?: string;
34
+ origin?: string;
35
+ protocol?: string;
36
+ host?: string;
37
+ port?: string;
38
+ hash?: string;
39
+ fullUrl?: string;
40
+ }
41
+ declare class MockCallHistory {
42
+ private logs;
43
+ get length(): number;
44
+ record(data: MockCallHistoryLogData): void;
45
+ called(criteria?: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp): boolean;
46
+ calls(): MockCallHistoryLog[];
47
+ firstCall(criteria?: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp): MockCallHistoryLog | undefined;
48
+ lastCall(criteria?: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp): MockCallHistoryLog | undefined;
49
+ nthCall(n: number, criteria?: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp): MockCallHistoryLog | undefined;
50
+ clear(): void;
51
+ [Symbol.iterator](): Iterator<MockCallHistoryLog>;
52
+ filterCalls(criteria: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp, options?: {
53
+ operator?: 'AND' | 'OR';
54
+ }): MockCallHistoryLog[];
55
+ private filterBy;
56
+ filterCallsByMethod(filter: string | RegExp): MockCallHistoryLog[];
57
+ filterCallsByPath(filter: string | RegExp): MockCallHistoryLog[];
58
+ filterCallsByOrigin(filter: string | RegExp): MockCallHistoryLog[];
59
+ filterCallsByProtocol(filter: string | RegExp): MockCallHistoryLog[];
60
+ filterCallsByHost(filter: string | RegExp): MockCallHistoryLog[];
61
+ filterCallsByPort(filter: string | RegExp): MockCallHistoryLog[];
62
+ filterCallsByHash(filter: string | RegExp): MockCallHistoryLog[];
63
+ filterCallsByFullUrl(filter: string | RegExp): MockCallHistoryLog[];
64
+ }
65
+
66
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
67
+ type PathMatcher = string | RegExp | ((path: string) => boolean);
68
+ type HeaderValueMatcher = string | RegExp | ((value: string) => boolean);
69
+ type BodyMatcher = string | RegExp | ((body: string) => boolean);
70
+ interface InterceptOptions {
71
+ path: PathMatcher;
72
+ method?: HttpMethod;
73
+ headers?: Record<string, HeaderValueMatcher>;
74
+ body?: BodyMatcher;
75
+ query?: Record<string, string>;
76
+ }
77
+ interface ReplyOptions {
78
+ headers?: Record<string, string>;
79
+ }
80
+ type ReplyCallback = (req: {
81
+ body: string | null;
82
+ }) => unknown | Promise<unknown>;
83
+ interface SingleReplyResult {
84
+ statusCode: number;
85
+ data: unknown;
86
+ responseOptions?: ReplyOptions;
87
+ }
88
+ type SingleReplyCallback = (req: {
89
+ body: string | null;
90
+ }) => SingleReplyResult | Promise<SingleReplyResult>;
91
+ interface MockReplyChain {
92
+ times(n: number): void;
93
+ persist(): void;
94
+ delay(ms: number): void;
95
+ replyContentLength(): void;
96
+ }
97
+ interface MockInterceptor {
98
+ reply(status: number, body?: unknown, options?: ReplyOptions): MockReplyChain;
99
+ reply(status: number, callback: ReplyCallback): MockReplyChain;
100
+ reply(callback: SingleReplyCallback): MockReplyChain;
101
+ replyWithError(error?: Error): MockReplyChain;
102
+ }
103
+ interface MockPool {
104
+ intercept(options: InterceptOptions): MockInterceptor;
105
+ }
106
+ interface PendingInterceptor {
107
+ origin: string;
108
+ path: string;
109
+ method: string;
110
+ consumed: boolean;
111
+ times: number;
112
+ timesInvoked: number;
113
+ persist: boolean;
114
+ }
115
+ type PrintAPI = {
116
+ warning(): void;
117
+ error(): void;
118
+ };
119
+ type OnUnhandledRequestCallback = (request: Request, print: PrintAPI) => void;
120
+ type OnUnhandledRequest = 'bypass' | 'warn' | 'error' | OnUnhandledRequestCallback;
121
+ interface ActivateOptions {
122
+ onUnhandledRequest?: OnUnhandledRequest;
123
+ }
124
+ interface ResolvedActivateOptions {
125
+ onUnhandledRequest: OnUnhandledRequestCallback;
126
+ }
127
+ /** Structural type to avoid cross-package nominal type mismatch on MSW's private fields */
128
+ interface SetupServerLike {
129
+ use(...handlers: Array<unknown>): void;
130
+ resetHandlers(...handlers: Array<unknown>): void;
131
+ listen(options?: Record<string, unknown>): void;
132
+ close(): void;
133
+ }
134
+ /** Structural type for MSW's setupWorker return type */
135
+ interface SetupWorkerLike {
136
+ use(...handlers: Array<unknown>): void;
137
+ resetHandlers(...handlers: Array<unknown>): void;
138
+ start(options?: Record<string, unknown>): Promise<void>;
139
+ stop(): void;
140
+ }
141
+ /** Environment-agnostic adapter interface for MSW server/worker */
142
+ interface MswAdapter {
143
+ use(...handlers: Array<unknown>): void;
144
+ resetHandlers(...handlers: Array<unknown>): void;
145
+ activate(options: ResolvedActivateOptions): void | Promise<void>;
146
+ deactivate(): void;
147
+ }
148
+ /** Pluggable factory for MSW-version-specific handler and response creation */
149
+ interface HandlerFactory {
150
+ createHandler(method: HttpMethod, urlPattern: string | RegExp, handlerFn: (request: Request) => Promise<Response | undefined>): unknown;
151
+ buildResponse(status: number, body: unknown, headers?: Headers): Response;
152
+ buildErrorResponse(): Response;
153
+ }
154
+
155
+ declare class FetchMock {
156
+ /** @internal */
157
+ static _defaultAdapterFactory?: () => MswAdapter;
158
+ /** @internal */
159
+ static _handlerFactory?: HandlerFactory;
160
+ private get handlerFactory();
161
+ private buildResponse;
162
+ private readonly _calls;
163
+ private adapter;
164
+ private interceptors;
165
+ private netConnectAllowed;
166
+ private mswHandlers;
167
+ private _defaultReplyHeaders;
168
+ private _callHistoryEnabled;
169
+ get calls(): MockCallHistory;
170
+ constructor(input?: SetupServerLike | SetupWorkerLike | MswAdapter);
171
+ activate(options?: ActivateOptions): Promise<void>;
172
+ disableNetConnect(): void;
173
+ enableNetConnect(matcher?: string | RegExp | ((host: string) => boolean)): void;
174
+ private isNetConnectAllowed;
175
+ /**
176
+ * Remove consumed MSW handlers so future requests to those URLs
177
+ * go through MSW's onUnhandledRequest instead of silently passing through.
178
+ */
179
+ private syncMswHandlers;
180
+ getCallHistory(): MockCallHistory;
181
+ clearCallHistory(): void;
182
+ clearAllCallHistory(): void;
183
+ defaultReplyHeaders(headers: Record<string, string>): void;
184
+ enableCallHistory(): void;
185
+ disableCallHistory(): void;
186
+ deactivate(): void;
187
+ reset(): void;
188
+ assertNoPendingInterceptors(): void;
189
+ pendingInterceptors(): PendingInterceptor[];
190
+ private resolveUrlPattern;
191
+ private matchOriginAndPath;
192
+ private matchAndConsume;
193
+ private registerHandler;
194
+ private createMatchingHandler;
195
+ private buildChain;
196
+ get(origin: string | RegExp | ((origin: string) => boolean)): MockPool;
197
+ }
198
+
199
+ export { type ActivateOptions as A, type CallHistoryFilterCriteria as C, FetchMock as F, type HandlerFactory as H, type InterceptOptions as I, type MswAdapter as M, type OnUnhandledRequest as O, type PendingInterceptor as P, type ResolvedActivateOptions as R, type SetupWorkerLike as S, MockCallHistory as a, MockCallHistoryLog as b, type MockCallHistoryLogData as c, type MockInterceptor as d, type MockPool as e, type MockReplyChain as f, type ReplyOptions as g, type HttpMethod as h, type SetupServerLike as i, type ReplyCallback as j, type SingleReplyCallback as k, type SingleReplyResult as l };
@@ -0,0 +1,199 @@
1
+ interface MockCallHistoryLogData {
2
+ body: string | null;
3
+ method: string;
4
+ headers: Record<string, string>;
5
+ fullUrl: string;
6
+ origin: string;
7
+ path: string;
8
+ searchParams: Record<string, string>;
9
+ protocol: string;
10
+ host: string;
11
+ port: string;
12
+ hash: string;
13
+ }
14
+ declare class MockCallHistoryLog {
15
+ readonly body: string | null;
16
+ readonly method: string;
17
+ readonly headers: Record<string, string>;
18
+ readonly fullUrl: string;
19
+ readonly origin: string;
20
+ readonly path: string;
21
+ readonly searchParams: Record<string, string>;
22
+ readonly protocol: string;
23
+ readonly host: string;
24
+ readonly port: string;
25
+ readonly hash: string;
26
+ constructor(data: MockCallHistoryLogData);
27
+ json(): unknown;
28
+ toMap(): Map<string, string | null | Record<string, string>>;
29
+ toString(): string;
30
+ }
31
+ interface CallHistoryFilterCriteria {
32
+ method?: string;
33
+ path?: string;
34
+ origin?: string;
35
+ protocol?: string;
36
+ host?: string;
37
+ port?: string;
38
+ hash?: string;
39
+ fullUrl?: string;
40
+ }
41
+ declare class MockCallHistory {
42
+ private logs;
43
+ get length(): number;
44
+ record(data: MockCallHistoryLogData): void;
45
+ called(criteria?: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp): boolean;
46
+ calls(): MockCallHistoryLog[];
47
+ firstCall(criteria?: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp): MockCallHistoryLog | undefined;
48
+ lastCall(criteria?: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp): MockCallHistoryLog | undefined;
49
+ nthCall(n: number, criteria?: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp): MockCallHistoryLog | undefined;
50
+ clear(): void;
51
+ [Symbol.iterator](): Iterator<MockCallHistoryLog>;
52
+ filterCalls(criteria: ((log: MockCallHistoryLog) => boolean) | CallHistoryFilterCriteria | RegExp, options?: {
53
+ operator?: 'AND' | 'OR';
54
+ }): MockCallHistoryLog[];
55
+ private filterBy;
56
+ filterCallsByMethod(filter: string | RegExp): MockCallHistoryLog[];
57
+ filterCallsByPath(filter: string | RegExp): MockCallHistoryLog[];
58
+ filterCallsByOrigin(filter: string | RegExp): MockCallHistoryLog[];
59
+ filterCallsByProtocol(filter: string | RegExp): MockCallHistoryLog[];
60
+ filterCallsByHost(filter: string | RegExp): MockCallHistoryLog[];
61
+ filterCallsByPort(filter: string | RegExp): MockCallHistoryLog[];
62
+ filterCallsByHash(filter: string | RegExp): MockCallHistoryLog[];
63
+ filterCallsByFullUrl(filter: string | RegExp): MockCallHistoryLog[];
64
+ }
65
+
66
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
67
+ type PathMatcher = string | RegExp | ((path: string) => boolean);
68
+ type HeaderValueMatcher = string | RegExp | ((value: string) => boolean);
69
+ type BodyMatcher = string | RegExp | ((body: string) => boolean);
70
+ interface InterceptOptions {
71
+ path: PathMatcher;
72
+ method?: HttpMethod;
73
+ headers?: Record<string, HeaderValueMatcher>;
74
+ body?: BodyMatcher;
75
+ query?: Record<string, string>;
76
+ }
77
+ interface ReplyOptions {
78
+ headers?: Record<string, string>;
79
+ }
80
+ type ReplyCallback = (req: {
81
+ body: string | null;
82
+ }) => unknown | Promise<unknown>;
83
+ interface SingleReplyResult {
84
+ statusCode: number;
85
+ data: unknown;
86
+ responseOptions?: ReplyOptions;
87
+ }
88
+ type SingleReplyCallback = (req: {
89
+ body: string | null;
90
+ }) => SingleReplyResult | Promise<SingleReplyResult>;
91
+ interface MockReplyChain {
92
+ times(n: number): void;
93
+ persist(): void;
94
+ delay(ms: number): void;
95
+ replyContentLength(): void;
96
+ }
97
+ interface MockInterceptor {
98
+ reply(status: number, body?: unknown, options?: ReplyOptions): MockReplyChain;
99
+ reply(status: number, callback: ReplyCallback): MockReplyChain;
100
+ reply(callback: SingleReplyCallback): MockReplyChain;
101
+ replyWithError(error?: Error): MockReplyChain;
102
+ }
103
+ interface MockPool {
104
+ intercept(options: InterceptOptions): MockInterceptor;
105
+ }
106
+ interface PendingInterceptor {
107
+ origin: string;
108
+ path: string;
109
+ method: string;
110
+ consumed: boolean;
111
+ times: number;
112
+ timesInvoked: number;
113
+ persist: boolean;
114
+ }
115
+ type PrintAPI = {
116
+ warning(): void;
117
+ error(): void;
118
+ };
119
+ type OnUnhandledRequestCallback = (request: Request, print: PrintAPI) => void;
120
+ type OnUnhandledRequest = 'bypass' | 'warn' | 'error' | OnUnhandledRequestCallback;
121
+ interface ActivateOptions {
122
+ onUnhandledRequest?: OnUnhandledRequest;
123
+ }
124
+ interface ResolvedActivateOptions {
125
+ onUnhandledRequest: OnUnhandledRequestCallback;
126
+ }
127
+ /** Structural type to avoid cross-package nominal type mismatch on MSW's private fields */
128
+ interface SetupServerLike {
129
+ use(...handlers: Array<unknown>): void;
130
+ resetHandlers(...handlers: Array<unknown>): void;
131
+ listen(options?: Record<string, unknown>): void;
132
+ close(): void;
133
+ }
134
+ /** Structural type for MSW's setupWorker return type */
135
+ interface SetupWorkerLike {
136
+ use(...handlers: Array<unknown>): void;
137
+ resetHandlers(...handlers: Array<unknown>): void;
138
+ start(options?: Record<string, unknown>): Promise<void>;
139
+ stop(): void;
140
+ }
141
+ /** Environment-agnostic adapter interface for MSW server/worker */
142
+ interface MswAdapter {
143
+ use(...handlers: Array<unknown>): void;
144
+ resetHandlers(...handlers: Array<unknown>): void;
145
+ activate(options: ResolvedActivateOptions): void | Promise<void>;
146
+ deactivate(): void;
147
+ }
148
+ /** Pluggable factory for MSW-version-specific handler and response creation */
149
+ interface HandlerFactory {
150
+ createHandler(method: HttpMethod, urlPattern: string | RegExp, handlerFn: (request: Request) => Promise<Response | undefined>): unknown;
151
+ buildResponse(status: number, body: unknown, headers?: Headers): Response;
152
+ buildErrorResponse(): Response;
153
+ }
154
+
155
+ declare class FetchMock {
156
+ /** @internal */
157
+ static _defaultAdapterFactory?: () => MswAdapter;
158
+ /** @internal */
159
+ static _handlerFactory?: HandlerFactory;
160
+ private get handlerFactory();
161
+ private buildResponse;
162
+ private readonly _calls;
163
+ private adapter;
164
+ private interceptors;
165
+ private netConnectAllowed;
166
+ private mswHandlers;
167
+ private _defaultReplyHeaders;
168
+ private _callHistoryEnabled;
169
+ get calls(): MockCallHistory;
170
+ constructor(input?: SetupServerLike | SetupWorkerLike | MswAdapter);
171
+ activate(options?: ActivateOptions): Promise<void>;
172
+ disableNetConnect(): void;
173
+ enableNetConnect(matcher?: string | RegExp | ((host: string) => boolean)): void;
174
+ private isNetConnectAllowed;
175
+ /**
176
+ * Remove consumed MSW handlers so future requests to those URLs
177
+ * go through MSW's onUnhandledRequest instead of silently passing through.
178
+ */
179
+ private syncMswHandlers;
180
+ getCallHistory(): MockCallHistory;
181
+ clearCallHistory(): void;
182
+ clearAllCallHistory(): void;
183
+ defaultReplyHeaders(headers: Record<string, string>): void;
184
+ enableCallHistory(): void;
185
+ disableCallHistory(): void;
186
+ deactivate(): void;
187
+ reset(): void;
188
+ assertNoPendingInterceptors(): void;
189
+ pendingInterceptors(): PendingInterceptor[];
190
+ private resolveUrlPattern;
191
+ private matchOriginAndPath;
192
+ private matchAndConsume;
193
+ private registerHandler;
194
+ private createMatchingHandler;
195
+ private buildChain;
196
+ get(origin: string | RegExp | ((origin: string) => boolean)): MockPool;
197
+ }
198
+
199
+ export { type ActivateOptions as A, type CallHistoryFilterCriteria as C, FetchMock as F, type HandlerFactory as H, type InterceptOptions as I, type MswAdapter as M, type OnUnhandledRequest as O, type PendingInterceptor as P, type ResolvedActivateOptions as R, type SetupWorkerLike as S, MockCallHistory as a, MockCallHistoryLog as b, type MockCallHistoryLogData as c, type MockInterceptor as d, type MockPool as e, type MockReplyChain as f, type ReplyOptions as g, type HttpMethod as h, type SetupServerLike as i, type ReplyCallback as j, type SingleReplyCallback as k, type SingleReplyResult as l };
package/dist/index.cjs ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+ var _chunkPJU5FUNIcjs = require('./chunk-PJU5FUNI.cjs');
6
+ require('./chunk-QKIKWNFZ.cjs');
7
+
8
+
9
+
10
+
11
+ var _chunkN6B7UP6Bcjs = require('./chunk-N6B7UP6B.cjs');
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+ exports.FetchMock = _chunkN6B7UP6Bcjs.FetchMock; exports.MockCallHistory = _chunkN6B7UP6Bcjs.MockCallHistory; exports.MockCallHistoryLog = _chunkN6B7UP6Bcjs.MockCallHistoryLog; exports.NodeMswAdapter = _chunkPJU5FUNIcjs.NodeMswAdapter; exports.createFetchMock = _chunkPJU5FUNIcjs.createFetchMock; exports.fetchMock = _chunkPJU5FUNIcjs.fetchMock;
@@ -0,0 +1,2 @@
1
+ export { NodeMswAdapter, createFetchMock, fetchMock } from './node.cjs';
2
+ export { A as ActivateOptions, C as CallHistoryFilterCriteria, F as FetchMock, H as HandlerFactory, I as InterceptOptions, a as MockCallHistory, b as MockCallHistoryLog, c as MockCallHistoryLogData, d as MockInterceptor, e as MockPool, f as MockReplyChain, M as MswAdapter, O as OnUnhandledRequest, P as PendingInterceptor, j as ReplyCallback, g as ReplyOptions, i as SetupServerLike, k as SingleReplyCallback, l as SingleReplyResult } from './fetch-mock-DhiqmHdF.cjs';
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- export { FetchMock, NodeMswAdapter, createFetchMock, fetchMock, MockCallHistory, MockCallHistoryLog, } from './node';
2
- export type { ActivateOptions, OnUnhandledRequest, InterceptOptions, MockPool, MockInterceptor, MockReplyChain, ReplyOptions, PendingInterceptor, MswAdapter, SetupServerLike, MockCallHistoryLogData, CallHistoryFilterCriteria, } from './node';
3
- //# sourceMappingURL=index.d.ts.map
1
+ export { NodeMswAdapter, createFetchMock, fetchMock } from './node.js';
2
+ export { A as ActivateOptions, C as CallHistoryFilterCriteria, F as FetchMock, H as HandlerFactory, I as InterceptOptions, a as MockCallHistory, b as MockCallHistoryLog, c as MockCallHistoryLogData, d as MockInterceptor, e as MockPool, f as MockReplyChain, M as MswAdapter, O as OnUnhandledRequest, P as PendingInterceptor, j as ReplyCallback, g as ReplyOptions, i as SetupServerLike, k as SingleReplyCallback, l as SingleReplyResult } from './fetch-mock-DhiqmHdF.js';
package/dist/index.js CHANGED
@@ -1 +1,19 @@
1
- export { FetchMock, NodeMswAdapter, createFetchMock, fetchMock, MockCallHistory, MockCallHistoryLog, } from './node';
1
+ import {
2
+ NodeMswAdapter,
3
+ createFetchMock,
4
+ fetchMock
5
+ } from "./chunk-PUVZ7LB4.js";
6
+ import "./chunk-IOGQ34QL.js";
7
+ import {
8
+ FetchMock,
9
+ MockCallHistory,
10
+ MockCallHistoryLog
11
+ } from "./chunk-3RAWYKAG.js";
12
+ export {
13
+ FetchMock,
14
+ MockCallHistory,
15
+ MockCallHistoryLog,
16
+ NodeMswAdapter,
17
+ createFetchMock,
18
+ fetchMock
19
+ };
@@ -0,0 +1,71 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+ var _chunkN6B7UP6Bcjs = require('./chunk-N6B7UP6B.cjs');
6
+
7
+ // src/legacy-handler-factory.ts
8
+ function createLegacyHandlerFactory(rest) {
9
+ const methods = {
10
+ GET: rest.get,
11
+ POST: rest.post,
12
+ PUT: rest.put,
13
+ DELETE: rest.delete,
14
+ PATCH: rest.patch
15
+ };
16
+ return {
17
+ createHandler(method, urlPattern, handlerFn) {
18
+ return methods[method](urlPattern, async (req, res, ctx) => {
19
+ const headers = new Headers(req.headers.all());
20
+ const hasBody = !["GET", "HEAD"].includes(req.method);
21
+ const body = hasBody && req.body != null ? typeof req.body === "string" ? req.body : JSON.stringify(req.body) : void 0;
22
+ const request = new Request(req.url.toString(), {
23
+ method: req.method,
24
+ headers,
25
+ body
26
+ });
27
+ const response = await handlerFn(request);
28
+ if (!response) return void 0;
29
+ if (response.type === "error") {
30
+ return res.networkError("Failed to fetch");
31
+ }
32
+ const transformers = [ctx.status(response.status)];
33
+ response.headers.forEach((value, key) => {
34
+ transformers.push(ctx.set(key, value));
35
+ });
36
+ const text = await response.text();
37
+ if (text) {
38
+ transformers.push(ctx.body(text));
39
+ }
40
+ return res(...transformers);
41
+ });
42
+ },
43
+ buildResponse(status, body, headers) {
44
+ if (body === null || body === void 0) {
45
+ return new Response(null, { status, headers });
46
+ }
47
+ const responseHeaders = new Headers(headers);
48
+ responseHeaders.set("content-type", "application/json");
49
+ return new Response(JSON.stringify(body), { status, headers: responseHeaders });
50
+ },
51
+ buildErrorResponse() {
52
+ return Response.error();
53
+ }
54
+ };
55
+ }
56
+
57
+ // src/legacy.ts
58
+ function createFetchMock(rest, server) {
59
+ _chunkN6B7UP6Bcjs.FetchMock._handlerFactory = createLegacyHandlerFactory(rest);
60
+ if (server) {
61
+ return new (0, _chunkN6B7UP6Bcjs.FetchMock)(server);
62
+ }
63
+ return new (0, _chunkN6B7UP6Bcjs.FetchMock)();
64
+ }
65
+
66
+
67
+
68
+
69
+
70
+
71
+ exports.FetchMock = _chunkN6B7UP6Bcjs.FetchMock; exports.MockCallHistory = _chunkN6B7UP6Bcjs.MockCallHistory; exports.MockCallHistoryLog = _chunkN6B7UP6Bcjs.MockCallHistoryLog; exports.createFetchMock = createFetchMock; exports.createLegacyHandlerFactory = createLegacyHandlerFactory;
@@ -0,0 +1,38 @@
1
+ import { h as HttpMethod, H as HandlerFactory, i as SetupServerLike, F as FetchMock } from './fetch-mock-DhiqmHdF.cjs';
2
+ export { A as ActivateOptions, C as CallHistoryFilterCriteria, I as InterceptOptions, a as MockCallHistory, b as MockCallHistoryLog, c as MockCallHistoryLogData, d as MockInterceptor, e as MockPool, f as MockReplyChain, M as MswAdapter, O as OnUnhandledRequest, P as PendingInterceptor, g as ReplyOptions } from './fetch-mock-DhiqmHdF.cjs';
3
+
4
+ /** Duck-typed interface for MSW v1's `rest` API methods */
5
+ type LegacyRestMethod = (url: string | RegExp, resolver: (req: any, res: any, ctx: any) => any) => unknown;
6
+ /** Duck-typed interface for MSW v1's `rest` namespace */
7
+ type LegacyRestApi = Record<Lowercase<HttpMethod>, LegacyRestMethod>;
8
+ /**
9
+ * Creates a HandlerFactory for MSW v1 (legacy) API.
10
+ *
11
+ * MSW v1 uses `rest.get(url, (req, res, ctx) => res(ctx.json(data)))` style.
12
+ * This factory bridges the standard Request/Response interface used by FetchMock
13
+ * to the v1 resolver callback format.
14
+ *
15
+ * @param rest - The `rest` object from `msw` v1 (e.g., `import { rest } from 'msw'`)
16
+ */
17
+ declare function createLegacyHandlerFactory(rest: LegacyRestApi): HandlerFactory;
18
+
19
+ /**
20
+ * Create a FetchMock instance for MSW v1 (legacy) environments.
21
+ *
22
+ * Usage with MSW v1:
23
+ * ```ts
24
+ * import { rest } from 'msw';
25
+ * import { setupServer } from 'msw/node';
26
+ * import { createFetchMock } from 'msw-fetch-mock/legacy';
27
+ *
28
+ * const server = setupServer();
29
+ * const fetchMock = createFetchMock(rest, server);
30
+ * ```
31
+ *
32
+ * @param rest - The `rest` object from MSW v1 (`import { rest } from 'msw'`)
33
+ * @param server - Optional MSW v1 setupServer instance. If omitted, you must pass a
34
+ * server or adapter to `new FetchMock(server)` yourself.
35
+ */
36
+ declare function createFetchMock(rest: LegacyRestApi, server?: SetupServerLike): FetchMock;
37
+
38
+ export { FetchMock, HandlerFactory, type LegacyRestApi, SetupServerLike, createFetchMock, createLegacyHandlerFactory };
@@ -0,0 +1,38 @@
1
+ import { h as HttpMethod, H as HandlerFactory, i as SetupServerLike, F as FetchMock } from './fetch-mock-DhiqmHdF.js';
2
+ export { A as ActivateOptions, C as CallHistoryFilterCriteria, I as InterceptOptions, a as MockCallHistory, b as MockCallHistoryLog, c as MockCallHistoryLogData, d as MockInterceptor, e as MockPool, f as MockReplyChain, M as MswAdapter, O as OnUnhandledRequest, P as PendingInterceptor, g as ReplyOptions } from './fetch-mock-DhiqmHdF.js';
3
+
4
+ /** Duck-typed interface for MSW v1's `rest` API methods */
5
+ type LegacyRestMethod = (url: string | RegExp, resolver: (req: any, res: any, ctx: any) => any) => unknown;
6
+ /** Duck-typed interface for MSW v1's `rest` namespace */
7
+ type LegacyRestApi = Record<Lowercase<HttpMethod>, LegacyRestMethod>;
8
+ /**
9
+ * Creates a HandlerFactory for MSW v1 (legacy) API.
10
+ *
11
+ * MSW v1 uses `rest.get(url, (req, res, ctx) => res(ctx.json(data)))` style.
12
+ * This factory bridges the standard Request/Response interface used by FetchMock
13
+ * to the v1 resolver callback format.
14
+ *
15
+ * @param rest - The `rest` object from `msw` v1 (e.g., `import { rest } from 'msw'`)
16
+ */
17
+ declare function createLegacyHandlerFactory(rest: LegacyRestApi): HandlerFactory;
18
+
19
+ /**
20
+ * Create a FetchMock instance for MSW v1 (legacy) environments.
21
+ *
22
+ * Usage with MSW v1:
23
+ * ```ts
24
+ * import { rest } from 'msw';
25
+ * import { setupServer } from 'msw/node';
26
+ * import { createFetchMock } from 'msw-fetch-mock/legacy';
27
+ *
28
+ * const server = setupServer();
29
+ * const fetchMock = createFetchMock(rest, server);
30
+ * ```
31
+ *
32
+ * @param rest - The `rest` object from MSW v1 (`import { rest } from 'msw'`)
33
+ * @param server - Optional MSW v1 setupServer instance. If omitted, you must pass a
34
+ * server or adapter to `new FetchMock(server)` yourself.
35
+ */
36
+ declare function createFetchMock(rest: LegacyRestApi, server?: SetupServerLike): FetchMock;
37
+
38
+ export { FetchMock, HandlerFactory, type LegacyRestApi, SetupServerLike, createFetchMock, createLegacyHandlerFactory };
package/dist/legacy.js ADDED
@@ -0,0 +1,71 @@
1
+ import {
2
+ FetchMock,
3
+ MockCallHistory,
4
+ MockCallHistoryLog
5
+ } from "./chunk-3RAWYKAG.js";
6
+
7
+ // src/legacy-handler-factory.ts
8
+ function createLegacyHandlerFactory(rest) {
9
+ const methods = {
10
+ GET: rest.get,
11
+ POST: rest.post,
12
+ PUT: rest.put,
13
+ DELETE: rest.delete,
14
+ PATCH: rest.patch
15
+ };
16
+ return {
17
+ createHandler(method, urlPattern, handlerFn) {
18
+ return methods[method](urlPattern, async (req, res, ctx) => {
19
+ const headers = new Headers(req.headers.all());
20
+ const hasBody = !["GET", "HEAD"].includes(req.method);
21
+ const body = hasBody && req.body != null ? typeof req.body === "string" ? req.body : JSON.stringify(req.body) : void 0;
22
+ const request = new Request(req.url.toString(), {
23
+ method: req.method,
24
+ headers,
25
+ body
26
+ });
27
+ const response = await handlerFn(request);
28
+ if (!response) return void 0;
29
+ if (response.type === "error") {
30
+ return res.networkError("Failed to fetch");
31
+ }
32
+ const transformers = [ctx.status(response.status)];
33
+ response.headers.forEach((value, key) => {
34
+ transformers.push(ctx.set(key, value));
35
+ });
36
+ const text = await response.text();
37
+ if (text) {
38
+ transformers.push(ctx.body(text));
39
+ }
40
+ return res(...transformers);
41
+ });
42
+ },
43
+ buildResponse(status, body, headers) {
44
+ if (body === null || body === void 0) {
45
+ return new Response(null, { status, headers });
46
+ }
47
+ const responseHeaders = new Headers(headers);
48
+ responseHeaders.set("content-type", "application/json");
49
+ return new Response(JSON.stringify(body), { status, headers: responseHeaders });
50
+ },
51
+ buildErrorResponse() {
52
+ return Response.error();
53
+ }
54
+ };
55
+ }
56
+
57
+ // src/legacy.ts
58
+ function createFetchMock(rest, server) {
59
+ FetchMock._handlerFactory = createLegacyHandlerFactory(rest);
60
+ if (server) {
61
+ return new FetchMock(server);
62
+ }
63
+ return new FetchMock();
64
+ }
65
+ export {
66
+ FetchMock,
67
+ MockCallHistory,
68
+ MockCallHistoryLog,
69
+ createFetchMock,
70
+ createLegacyHandlerFactory
71
+ };