msw-fetch-mock 0.3.0 → 0.3.1
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/README.md +72 -6
- package/dist/browser.cjs +43 -0
- package/dist/browser.d.cts +15 -0
- package/dist/browser.d.ts +15 -9
- package/dist/browser.js +42 -7
- package/dist/chunk-43CWIVXO.cjs +540 -0
- package/dist/chunk-A56K5DPD.cjs +58 -0
- package/dist/chunk-ATXIIOZA.js +540 -0
- package/dist/chunk-D26SJTTI.js +27 -0
- package/dist/chunk-HYRDB2FH.cjs +27 -0
- package/dist/chunk-KSDUQENI.js +58 -0
- package/dist/fetch-mock-BNke4Oik.d.cts +199 -0
- package/dist/fetch-mock-BNke4Oik.d.ts +199 -0
- package/dist/index.cjs +19 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +19 -1
- package/dist/legacy.cjs +71 -0
- package/dist/legacy.d.cts +38 -0
- package/dist/legacy.d.ts +38 -0
- package/dist/legacy.js +71 -0
- package/dist/node.cjs +19 -0
- package/dist/node.d.cts +18 -0
- package/dist/node.d.ts +17 -10
- package/dist/node.js +19 -12
- package/docs/api.md +121 -13
- package/docs/msw-v1-legacy.md +94 -0
- package/package.json +34 -10
- package/dist/browser-adapter.d.ts +0 -10
- package/dist/browser-adapter.d.ts.map +0 -1
- package/dist/browser-adapter.js +0 -20
- package/dist/browser.d.ts.map +0 -1
- package/dist/fetch-mock.d.ts +0 -31
- package/dist/fetch-mock.d.ts.map +0 -1
- package/dist/fetch-mock.js +0 -347
- package/dist/index.d.ts.map +0 -1
- package/dist/mock-call-history.d.ts +0 -65
- package/dist/mock-call-history.d.ts.map +0 -1
- package/dist/mock-call-history.js +0 -140
- package/dist/node-adapter.d.ts +0 -11
- package/dist/node-adapter.d.ts.map +0 -1
- package/dist/node-adapter.js +0 -34
- package/dist/node.d.ts.map +0 -1
- package/dist/types.d.ts +0 -74
- package/dist/types.d.ts.map +0 -1
- 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 matchOrigin;
|
|
192
|
+
private matchPathForOrigin;
|
|
193
|
+
private matchAndConsume;
|
|
194
|
+
private registerHandler;
|
|
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 matchOrigin;
|
|
192
|
+
private matchPathForOrigin;
|
|
193
|
+
private matchAndConsume;
|
|
194
|
+
private registerHandler;
|
|
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 _chunkA56K5DPDcjs = require('./chunk-A56K5DPD.cjs');
|
|
6
|
+
require('./chunk-HYRDB2FH.cjs');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
var _chunk43CWIVXOcjs = require('./chunk-43CWIVXO.cjs');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
exports.FetchMock = _chunk43CWIVXOcjs.FetchMock; exports.MockCallHistory = _chunk43CWIVXOcjs.MockCallHistory; exports.MockCallHistoryLog = _chunk43CWIVXOcjs.MockCallHistoryLog; exports.NodeMswAdapter = _chunkA56K5DPDcjs.NodeMswAdapter; exports.createFetchMock = _chunkA56K5DPDcjs.createFetchMock; exports.fetchMock = _chunkA56K5DPDcjs.fetchMock;
|
package/dist/index.d.cts
ADDED
|
@@ -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-BNke4Oik.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export
|
|
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-BNke4Oik.js';
|
package/dist/index.js
CHANGED
|
@@ -1 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
|
+
NodeMswAdapter,
|
|
3
|
+
createFetchMock,
|
|
4
|
+
fetchMock
|
|
5
|
+
} from "./chunk-KSDUQENI.js";
|
|
6
|
+
import "./chunk-D26SJTTI.js";
|
|
7
|
+
import {
|
|
8
|
+
FetchMock,
|
|
9
|
+
MockCallHistory,
|
|
10
|
+
MockCallHistoryLog
|
|
11
|
+
} from "./chunk-ATXIIOZA.js";
|
|
12
|
+
export {
|
|
13
|
+
FetchMock,
|
|
14
|
+
MockCallHistory,
|
|
15
|
+
MockCallHistoryLog,
|
|
16
|
+
NodeMswAdapter,
|
|
17
|
+
createFetchMock,
|
|
18
|
+
fetchMock
|
|
19
|
+
};
|
package/dist/legacy.cjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunk43CWIVXOcjs = require('./chunk-43CWIVXO.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
|
+
_chunk43CWIVXOcjs.FetchMock._handlerFactory = createLegacyHandlerFactory(rest);
|
|
60
|
+
if (server) {
|
|
61
|
+
return new (0, _chunk43CWIVXOcjs.FetchMock)(server);
|
|
62
|
+
}
|
|
63
|
+
return new (0, _chunk43CWIVXOcjs.FetchMock)();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
exports.FetchMock = _chunk43CWIVXOcjs.FetchMock; exports.MockCallHistory = _chunk43CWIVXOcjs.MockCallHistory; exports.MockCallHistoryLog = _chunk43CWIVXOcjs.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-BNke4Oik.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-BNke4Oik.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 };
|
package/dist/legacy.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { h as HttpMethod, H as HandlerFactory, i as SetupServerLike, F as FetchMock } from './fetch-mock-BNke4Oik.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-BNke4Oik.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-ATXIIOZA.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
|
+
};
|