@sgedda/mockifyer-fetch 1.8.106 → 1.8.108
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 +181 -0
- package/dist/clients/fetch-client.d.ts +7 -2
- package/dist/clients/fetch-client.js +47 -7
- package/dist/core-proxy.browser.d.ts +19 -0
- package/dist/core-proxy.browser.js +57 -0
- package/dist/index.browser.d.ts +53 -0
- package/dist/index.browser.js +111 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +41 -1564
- package/dist/mockifyer-class.d.ts +128 -0
- package/dist/mockifyer-class.js +1417 -0
- package/dist/node-capabilities.browser.d.ts +6 -0
- package/dist/node-capabilities.browser.js +110 -0
- package/dist/node-capabilities.d.ts +51 -0
- package/dist/node-capabilities.js +6 -0
- package/dist/node-capabilities.node.d.ts +6 -0
- package/dist/node-capabilities.node.js +31 -0
- package/dist/react-native.js +2 -1
- package/dist/utils/fetch-response-body.d.ts +18 -1
- package/dist/utils/fetch-response-body.js +18 -4
- package/package.json +13 -2
- package/src/clients/fetch-client.ts +62 -10
- package/src/core-proxy.browser.ts +62 -0
- package/src/index.browser.ts +171 -0
- package/src/index.ts +48 -1917
- package/src/mockifyer-class.ts +1830 -0
- package/src/node-capabilities.browser.ts +123 -0
- package/src/node-capabilities.node.ts +40 -0
- package/src/node-capabilities.ts +60 -0
- package/src/react-native.ts +2 -1
- package/src/utils/fetch-response-body.ts +38 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser entry for mockifyer-fetch.
|
|
3
|
+
* Avoids statically loading Node built-ins by not re-exporting the full main index barrel.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Re-export only essential types and browser-safe utilities from core
|
|
7
|
+
// DO NOT use `export * from '@sgedda/mockifyer-core'` - it pulls in Node-dependent modules
|
|
8
|
+
import type {
|
|
9
|
+
MockifyerConfig,
|
|
10
|
+
MockData,
|
|
11
|
+
StoredRequest,
|
|
12
|
+
StoredResponse,
|
|
13
|
+
HTTPClient,
|
|
14
|
+
HTTPResponse,
|
|
15
|
+
DatabaseProvider,
|
|
16
|
+
DatabaseProviderConfig,
|
|
17
|
+
} from '@sgedda/mockifyer-core';
|
|
18
|
+
|
|
19
|
+
export type {
|
|
20
|
+
MockifyerConfig,
|
|
21
|
+
MockData,
|
|
22
|
+
StoredRequest,
|
|
23
|
+
StoredResponse,
|
|
24
|
+
HTTPClient,
|
|
25
|
+
HTTPResponse,
|
|
26
|
+
DatabaseProvider,
|
|
27
|
+
DatabaseProviderConfig,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export {
|
|
31
|
+
MemoryProvider,
|
|
32
|
+
createProvider,
|
|
33
|
+
} from '@sgedda/mockifyer-core';
|
|
34
|
+
|
|
35
|
+
// Re-export browser-safe fetch utilities
|
|
36
|
+
export { FetchHTTPClient } from './clients/fetch-client';
|
|
37
|
+
import { bodyInitForFetchResponse } from './utils/fetch-response-body';
|
|
38
|
+
export { bodyInitForFetchResponse };
|
|
39
|
+
export { resolvePatchedFetchRequest } from './utils/resolve-patched-fetch-request';
|
|
40
|
+
|
|
41
|
+
export {
|
|
42
|
+
canUseDashboardRedisProxy,
|
|
43
|
+
canUseDashboardCentralProxy,
|
|
44
|
+
} from './utils/dashboard-redis-health';
|
|
45
|
+
|
|
46
|
+
// Import MockifyerClass and browser capabilities
|
|
47
|
+
import { MockifyerClass } from './mockifyer-class';
|
|
48
|
+
import { nodeCapabilities } from './node-capabilities.browser';
|
|
49
|
+
|
|
50
|
+
// MockifyerInstance interface
|
|
51
|
+
export interface MockifyerInstance extends HTTPClient {
|
|
52
|
+
reloadMockData: (syncFromProject?: boolean) => Promise<void>;
|
|
53
|
+
clearStaleCacheEntries: () => number;
|
|
54
|
+
clearAllMocks: () => Promise<void>;
|
|
55
|
+
setClientId: (lane: string) => void;
|
|
56
|
+
getClientId: () => string | undefined;
|
|
57
|
+
setRecordMode: (enabled: boolean) => void;
|
|
58
|
+
isRecordMode: () => boolean;
|
|
59
|
+
setRecordResponses: (enabled: boolean) => void;
|
|
60
|
+
isRecordResponses: () => boolean;
|
|
61
|
+
enableMockifyer: () => void;
|
|
62
|
+
disableMockifyer: () => void;
|
|
63
|
+
isMockifyerEnabled: () => boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Setup Mockifyer for browser/web with in-memory provider.
|
|
68
|
+
*
|
|
69
|
+
* **Browser-specific constraints:**
|
|
70
|
+
* - Only `memory` provider is supported (filesystem/redis/sqlite need Node.js)
|
|
71
|
+
* - Preload fixtures via `databaseProvider.options.fixtures: MockData[]`
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { setupMockifyer } from '@sgedda/mockifyer-fetch';
|
|
76
|
+
* import rates from './mock-data/api.frankfurter.dev/v1/latest.json';
|
|
77
|
+
*
|
|
78
|
+
* const mockifyer = setupMockifyer({
|
|
79
|
+
* mockDataPath: './mock-data',
|
|
80
|
+
* databaseProvider: {
|
|
81
|
+
* type: 'memory',
|
|
82
|
+
* options: {
|
|
83
|
+
* fixtures: [rates]
|
|
84
|
+
* }
|
|
85
|
+
* },
|
|
86
|
+
* recordMode: false,
|
|
87
|
+
* failOnMissingMock: true,
|
|
88
|
+
* useGlobalFetch: true
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
93
|
+
// Validate browser-safe config
|
|
94
|
+
if (config.databaseProvider && config.databaseProvider.type && config.databaseProvider.type !== 'memory') {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`[Mockifyer Browser] Provider '${config.databaseProvider.type}' requires Node.js (fs/path/crypto). ` +
|
|
97
|
+
`Use databaseProvider: { type: 'memory', options: { fixtures: [...] } } in browser builds.`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!config.databaseProvider) {
|
|
102
|
+
console.warn(
|
|
103
|
+
'[Mockifyer Browser] No databaseProvider configured. Falling back to memory provider. ' +
|
|
104
|
+
'Preload fixtures via databaseProvider: { type: "memory", options: { fixtures: [...] } }'
|
|
105
|
+
);
|
|
106
|
+
config.databaseProvider = { type: 'memory', options: {} };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Create with browser capabilities (no Node fs/path/crypto)
|
|
110
|
+
const mockifyer = new MockifyerClass(config, nodeCapabilities);
|
|
111
|
+
|
|
112
|
+
// Get HTTP client
|
|
113
|
+
const httpClient = mockifyer.getHTTPClient();
|
|
114
|
+
|
|
115
|
+
// Patch global fetch if useGlobalFetch is true
|
|
116
|
+
if (config.useGlobalFetch) {
|
|
117
|
+
const originalFetch = globalThis.fetch;
|
|
118
|
+
const mockifyerInstance = mockifyer;
|
|
119
|
+
|
|
120
|
+
// Store original fetch on the http client so it can be used for unmocked requests
|
|
121
|
+
(httpClient as any)._originalFetch = originalFetch;
|
|
122
|
+
|
|
123
|
+
globalThis.fetch = async function(input: string | Request | URL, init?: RequestInit): Promise<Response> {
|
|
124
|
+
if (!mockifyerInstance.isMockifyerEnabled()) {
|
|
125
|
+
return await originalFetch(input, init);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
|
|
129
|
+
const method = (init?.method || 'GET').toUpperCase();
|
|
130
|
+
|
|
131
|
+
// Perform the request through mockifyer
|
|
132
|
+
const response = await httpClient.request({
|
|
133
|
+
method,
|
|
134
|
+
url,
|
|
135
|
+
headers: init?.headers as Record<string, string> || {},
|
|
136
|
+
data: init?.body,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Convert mockifyer response to fetch Response (rebuild stream when present)
|
|
140
|
+
return new Response(
|
|
141
|
+
bodyInitForFetchResponse(response.status, response.data, {
|
|
142
|
+
stream: response.mockifyerStoredStream,
|
|
143
|
+
streamReplay: config.streamReplay,
|
|
144
|
+
}),
|
|
145
|
+
{
|
|
146
|
+
status: response.status,
|
|
147
|
+
statusText: response.statusText || '',
|
|
148
|
+
headers: new Headers(response.headers || {})
|
|
149
|
+
}
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Return httpClient extended with mockifyer methods
|
|
155
|
+
const extendedClient = httpClient as any;
|
|
156
|
+
|
|
157
|
+
extendedClient.reloadMockData = (syncFromProject?: boolean) => mockifyer.reloadMockData(syncFromProject);
|
|
158
|
+
extendedClient.clearStaleCacheEntries = () => mockifyer.clearStaleCacheEntries();
|
|
159
|
+
extendedClient.clearAllMocks = () => mockifyer.clearAllMocks();
|
|
160
|
+
extendedClient.setClientId = (lane: string) => mockifyer.setClientId(lane);
|
|
161
|
+
extendedClient.getClientId = () => mockifyer.getClientId();
|
|
162
|
+
extendedClient.setRecordMode = (enabled: boolean) => mockifyer.setRecordMode(enabled);
|
|
163
|
+
extendedClient.isRecordMode = () => mockifyer.isRecordMode();
|
|
164
|
+
extendedClient.setRecordResponses = (enabled: boolean) => mockifyer.setRecordResponses(enabled);
|
|
165
|
+
extendedClient.isRecordResponses = () => mockifyer.isRecordResponses();
|
|
166
|
+
extendedClient.enableMockifyer = () => mockifyer.enableMockifyer();
|
|
167
|
+
extendedClient.disableMockifyer = () => mockifyer.disableMockifyer();
|
|
168
|
+
extendedClient.isMockifyerEnabled = () => mockifyer.isMockifyerEnabled();
|
|
169
|
+
|
|
170
|
+
return extendedClient;
|
|
171
|
+
}
|