@sgedda/mockifyer-fetch 1.8.32 → 1.8.34
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/clients/fetch-client.js +21 -94
- package/dist/core-proxy.d.ts +7 -0
- package/dist/core-proxy.js +15 -0
- package/dist/index.d.ts +4 -50
- package/dist/index.js +21 -189
- package/dist/metro-config.js +20 -0
- package/dist/utils/dashboard-redis-health.d.ts +1 -19
- package/dist/utils/dashboard-redis-health.js +4 -47
- package/dist/utils/join-proxy-dashboard-api-url.d.ts +1 -11
- package/dist/utils/join-proxy-dashboard-api-url.js +3 -16
- package/package.json +2 -2
- package/src/clients/fetch-client.ts +21 -136
- package/src/core-proxy.ts +12 -0
- package/src/index.ts +31 -265
- package/src/metro-config.ts +24 -0
- package/src/utils/dashboard-redis-health.ts +5 -58
- package/src/utils/join-proxy-dashboard-api-url.ts +1 -15
- package/tsconfig.json +5 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.34",
|
|
4
4
|
"description": "Mockifyer for Fetch - API mocking and logging utility for storing and replaying API responses",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
},
|
|
50
50
|
"license": "MIT",
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@sgedda/mockifyer-core": "^1.
|
|
52
|
+
"@sgedda/mockifyer-core": "^1.10.2"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^20.11.24",
|
|
@@ -8,57 +8,8 @@ import {
|
|
|
8
8
|
getOutboundMockifyerDeviceIdHeader,
|
|
9
9
|
MOCKIFYER_CLIENT_ID_HEADER,
|
|
10
10
|
MOCKIFYER_DEVICE_ID_HEADER,
|
|
11
|
-
MOCKIFYER_PARENT_REQUEST_ID_HEADER,
|
|
12
|
-
MOCKIFYER_REQUEST_ID_HEADER,
|
|
13
11
|
} from '@sgedda/mockifyer-core';
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
function buildProxyEnvelope(params: {
|
|
17
|
-
url: string;
|
|
18
|
-
method: string;
|
|
19
|
-
lane: string | undefined;
|
|
20
|
-
deviceId: string | undefined;
|
|
21
|
-
requestId: string | undefined;
|
|
22
|
-
parentRequestId: string | undefined;
|
|
23
|
-
headers: Record<string, string>;
|
|
24
|
-
body: unknown;
|
|
25
|
-
scenario: string | undefined;
|
|
26
|
-
recordOnMiss: boolean | undefined;
|
|
27
|
-
recordResponses: boolean;
|
|
28
|
-
strictLaneScenario: boolean;
|
|
29
|
-
}): Record<string, unknown> {
|
|
30
|
-
const {
|
|
31
|
-
url,
|
|
32
|
-
method,
|
|
33
|
-
lane,
|
|
34
|
-
deviceId,
|
|
35
|
-
requestId,
|
|
36
|
-
parentRequestId,
|
|
37
|
-
headers,
|
|
38
|
-
body,
|
|
39
|
-
scenario,
|
|
40
|
-
recordOnMiss,
|
|
41
|
-
recordResponses,
|
|
42
|
-
strictLaneScenario,
|
|
43
|
-
} = params;
|
|
44
|
-
const envelope: Record<string, unknown> = {
|
|
45
|
-
url,
|
|
46
|
-
method,
|
|
47
|
-
clientId: lane,
|
|
48
|
-
deviceId,
|
|
49
|
-
requestId,
|
|
50
|
-
parentRequestId,
|
|
51
|
-
headers,
|
|
52
|
-
body,
|
|
53
|
-
scenario,
|
|
54
|
-
recordResponses,
|
|
55
|
-
strictLaneScenario,
|
|
56
|
-
};
|
|
57
|
-
if (typeof recordOnMiss === 'boolean') {
|
|
58
|
-
envelope.record = recordOnMiss;
|
|
59
|
-
}
|
|
60
|
-
return envelope;
|
|
61
|
-
}
|
|
12
|
+
import { performDashboardProxyRequest } from '../core-proxy';
|
|
62
13
|
|
|
63
14
|
export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
64
15
|
private baseUrl?: string;
|
|
@@ -166,7 +117,6 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
166
117
|
};
|
|
167
118
|
|
|
168
119
|
// Use original fetch if available (when global fetch is patched), otherwise use global fetch
|
|
169
|
-
// CRITICAL: Always use _originalFetch if available to prevent infinite loops
|
|
170
120
|
const fetchFn = (this as any)._originalFetch || fetch;
|
|
171
121
|
if (!(this as any)._originalFetch) {
|
|
172
122
|
console.warn('[FetchHTTPClient] _originalFetch not set! This may cause infinite loops if global fetch is patched.');
|
|
@@ -180,92 +130,29 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
180
130
|
|
|
181
131
|
const explicitProxyContext = this.getExplicitProxyScenarioContext?.() ?? true;
|
|
182
132
|
|
|
183
|
-
// Proxy mode (e.g. React Native → dashboard → Redis)
|
|
184
133
|
if (this.proxyBaseUrl && !bypassMockifyer && explicitProxyContext) {
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
headers: {
|
|
189
|
-
'content-type': 'application/json',
|
|
190
|
-
...(lane ? { [MOCKIFYER_CLIENT_ID_HEADER]: lane } : {}),
|
|
191
|
-
...(this.deviceId ? { [MOCKIFYER_DEVICE_ID_HEADER]: this.deviceId } : {}),
|
|
192
|
-
...(hopRequestId ? { [MOCKIFYER_REQUEST_ID_HEADER]: hopRequestId } : {}),
|
|
193
|
-
...(hopParentRequestId ? { [MOCKIFYER_PARENT_REQUEST_ID_HEADER]: hopParentRequestId } : {}),
|
|
194
|
-
},
|
|
195
|
-
body: JSON.stringify(
|
|
196
|
-
buildProxyEnvelope({
|
|
197
|
-
url,
|
|
198
|
-
method: String(requestConfig.method || 'GET'),
|
|
199
|
-
lane,
|
|
200
|
-
deviceId: this.deviceId,
|
|
201
|
-
requestId: hopRequestId,
|
|
202
|
-
parentRequestId: hopParentRequestId,
|
|
203
|
-
headers: (() => {
|
|
204
|
-
const out: Record<string, string> = {};
|
|
205
|
-
headers.forEach((value, key) => {
|
|
206
|
-
out[key] = value;
|
|
207
|
-
});
|
|
208
|
-
return out;
|
|
209
|
-
})(),
|
|
210
|
-
body: config.data ?? null,
|
|
211
|
-
scenario: this.proxyScenario,
|
|
212
|
-
recordOnMiss: this.proxyRecordOnMiss,
|
|
213
|
-
recordResponses: this.proxyRecordResponses,
|
|
214
|
-
strictLaneScenario: this.getStrictLaneScenario?.() ?? true,
|
|
215
|
-
})
|
|
216
|
-
),
|
|
134
|
+
const headerRecord: Record<string, string> = {};
|
|
135
|
+
headers.forEach((value, key) => {
|
|
136
|
+
headerRecord[key] = value;
|
|
217
137
|
});
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
hashShort ? `(hash=${hashShort}) ` : ''
|
|
233
|
-
}(lane=${laneLabel})`
|
|
234
|
-
);
|
|
235
|
-
}
|
|
236
|
-
} catch {
|
|
237
|
-
// ignore logging failures
|
|
238
|
-
}
|
|
239
|
-
const data = payload?.response?.data;
|
|
240
|
-
const status = payload?.response?.status ?? 200;
|
|
241
|
-
const responseHeaders: Record<string, string> = payload?.response?.headers ?? {};
|
|
242
|
-
|
|
243
|
-
const scenarioResolution = payload?.scenarioResolution as
|
|
244
|
-
| { scenario?: string | null }
|
|
245
|
-
| undefined;
|
|
246
|
-
const scenarioName =
|
|
247
|
-
typeof scenarioResolution?.scenario === 'string' && scenarioResolution.scenario.trim()
|
|
248
|
-
? scenarioResolution.scenario.trim()
|
|
249
|
-
: undefined;
|
|
250
|
-
|
|
251
|
-
const mockifyerProxyRecording =
|
|
252
|
-
payload?.recordedToStore === true && payload?.storedMock
|
|
253
|
-
? {
|
|
254
|
-
recordedToStore: true as const,
|
|
255
|
-
storedMock: payload.storedMock,
|
|
256
|
-
hash: typeof payload.hash === 'string' ? payload.hash : undefined,
|
|
257
|
-
scenarioName,
|
|
258
|
-
}
|
|
259
|
-
: undefined;
|
|
260
|
-
|
|
261
|
-
return {
|
|
262
|
-
data,
|
|
263
|
-
status,
|
|
264
|
-
statusText: String(status),
|
|
265
|
-
headers: responseHeaders,
|
|
138
|
+
return performDashboardProxyRequest({
|
|
139
|
+
proxyBaseUrl: this.proxyBaseUrl,
|
|
140
|
+
url,
|
|
141
|
+
method: String(requestConfig.method || 'GET'),
|
|
142
|
+
headers: headerRecord,
|
|
143
|
+
body: config.data ?? null,
|
|
144
|
+
lane,
|
|
145
|
+
deviceId: this.deviceId,
|
|
146
|
+
requestId: hopRequestId,
|
|
147
|
+
parentRequestId: hopParentRequestId,
|
|
148
|
+
scenario: this.proxyScenario,
|
|
149
|
+
recordOnMiss: this.proxyRecordOnMiss,
|
|
150
|
+
recordResponses: this.proxyRecordResponses,
|
|
151
|
+
strictLaneScenario: this.getStrictLaneScenario?.() ?? true,
|
|
266
152
|
config,
|
|
267
|
-
|
|
268
|
-
|
|
153
|
+
fetchFn,
|
|
154
|
+
logTag: 'Mockifyer-Fetch',
|
|
155
|
+
});
|
|
269
156
|
}
|
|
270
157
|
|
|
271
158
|
if (!bypassMockifyer && lane && !getOutboundMockifyerClientIdHeader(headers)) {
|
|
@@ -292,7 +179,6 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
292
179
|
}
|
|
293
180
|
}
|
|
294
181
|
|
|
295
|
-
// Convert response Headers to Record<string, string>
|
|
296
182
|
const responseHeaders: Record<string, string> = {};
|
|
297
183
|
response.headers.forEach((value: string, key: string) => {
|
|
298
184
|
responseHeaders[key] = value;
|
|
@@ -317,4 +203,3 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
317
203
|
};
|
|
318
204
|
}
|
|
319
205
|
}
|
|
320
|
-
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proxy/dashboard helpers from core via subpath exports (RN-safe; avoids barrel resolution).
|
|
3
|
+
*/
|
|
4
|
+
export { applyProxyRecordOnMissEnv } from '@sgedda/mockifyer-core/utils/apply-proxy-record-on-miss-env';
|
|
5
|
+
export { mirrorProxyRecordingToClient } from '@sgedda/mockifyer-core/utils/mirror-proxy-recording-to-client';
|
|
6
|
+
export {
|
|
7
|
+
initMockifyerForDashboardProxy,
|
|
8
|
+
initMockifyerForLocalFilesystem,
|
|
9
|
+
type InitMockifyerForDashboardProxyOptions,
|
|
10
|
+
type InitMockifyerForLocalFilesystemOptions,
|
|
11
|
+
} from '@sgedda/mockifyer-core/utils/init-mockifyer-presets';
|
|
12
|
+
export { performDashboardProxyRequest } from '@sgedda/mockifyer-core/utils/perform-dashboard-proxy-request';
|
package/src/index.ts
CHANGED
|
@@ -56,7 +56,6 @@ import {
|
|
|
56
56
|
resolveActivationMode,
|
|
57
57
|
shouldApplyMockifyer,
|
|
58
58
|
isExplicitProxyScenarioContext,
|
|
59
|
-
parseProxyRecordOnMissEnv,
|
|
60
59
|
newRecordingUsesAlwaysUseRealApi,
|
|
61
60
|
shouldBlockLocalMockRecording,
|
|
62
61
|
resolveStrictScenarioResolution,
|
|
@@ -69,34 +68,16 @@ import {
|
|
|
69
68
|
installNodeInboundRequestCorrelationCapture,
|
|
70
69
|
} from '@sgedda/mockifyer-core';
|
|
71
70
|
import { logger, setLogLevel } from '@sgedda/mockifyer-core';
|
|
71
|
+
import {
|
|
72
|
+
applyProxyRecordOnMissEnv,
|
|
73
|
+
mirrorProxyRecordingToClient,
|
|
74
|
+
initMockifyerForDashboardProxy as coreInitMockifyerForDashboardProxy,
|
|
75
|
+
initMockifyerForLocalFilesystem as coreInitMockifyerForLocalFilesystem,
|
|
76
|
+
type InitMockifyerForDashboardProxyOptions,
|
|
77
|
+
type InitMockifyerForLocalFilesystemOptions,
|
|
78
|
+
} from './core-proxy';
|
|
72
79
|
|
|
73
80
|
import { FetchHTTPClient } from './clients/fetch-client';
|
|
74
|
-
import { canUseDashboardRedisProxy } from './utils/dashboard-redis-health';
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* When `proxy.baseUrl` is set and `proxy.recordOnMiss` is omitted, apply `MOCKIFYER_PROXY_RECORD_ON_MISS` if set.
|
|
78
|
-
*/
|
|
79
|
-
function applyProxyRecordOnMissEnv(config: MockifyerConfig): MockifyerConfig {
|
|
80
|
-
const proxy = config.proxy;
|
|
81
|
-
const baseUrl = proxy?.baseUrl?.trim();
|
|
82
|
-
if (!proxy || !baseUrl || proxy.recordOnMiss !== undefined) {
|
|
83
|
-
return config;
|
|
84
|
-
}
|
|
85
|
-
const parsed = parseProxyRecordOnMissEnv();
|
|
86
|
-
if (parsed === undefined) {
|
|
87
|
-
return config;
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
...config,
|
|
91
|
-
proxy: {
|
|
92
|
-
baseUrl: proxy.baseUrl,
|
|
93
|
-
scenario: proxy.scenario,
|
|
94
|
-
recordOnMiss: parsed,
|
|
95
|
-
strictLaneScenario: proxy.strictLaneScenario,
|
|
96
|
-
mirrorRecordedMocksToClient: proxy.mirrorRecordedMocksToClient,
|
|
97
|
-
},
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
81
|
|
|
101
82
|
class MockifyerClass {
|
|
102
83
|
private config: MockifyerConfig;
|
|
@@ -877,57 +858,23 @@ class MockifyerClass {
|
|
|
877
858
|
}
|
|
878
859
|
const hash = rec.hash?.trim();
|
|
879
860
|
const scenarioName = rec.scenarioName?.trim();
|
|
880
|
-
if (!hash || !scenarioName) {
|
|
881
|
-
logger.warn('[Mockifyer-Fetch] Proxy client mirror skipped: missing hash or scenario from proxy');
|
|
882
|
-
return;
|
|
883
|
-
}
|
|
884
861
|
const mockData = rec.storedMock as MockData;
|
|
885
|
-
const url = mockData?.request?.url || '';
|
|
886
|
-
if (shouldExcludeUrl(url, this.config.excludedUrls)) {
|
|
887
|
-
return;
|
|
888
|
-
}
|
|
889
|
-
const recExclusions = resolveRecordingExclusions(this.config);
|
|
890
|
-
if (shouldExcludeRecording(url, recExclusions, this.config.baseUrl)) {
|
|
891
|
-
return;
|
|
892
|
-
}
|
|
893
|
-
|
|
894
|
-
await this.databaseProviderInitPromise;
|
|
895
|
-
|
|
896
862
|
const providerType = this.config.databaseProvider?.type;
|
|
897
|
-
if (this.databaseProvider && providerType !== 'memory') {
|
|
898
|
-
try {
|
|
899
|
-
const out = this.databaseProvider.save(mockData, {
|
|
900
|
-
relativePath: `redis/${hash}.json`,
|
|
901
|
-
scenario: scenarioName,
|
|
902
|
-
});
|
|
903
|
-
if (out instanceof Promise) {
|
|
904
|
-
await out;
|
|
905
|
-
}
|
|
906
|
-
logger.info(`[Mockifyer-Fetch] Mirrored proxy recording (${providerType}): ${scenarioName}/redis/${hash}.json`);
|
|
907
|
-
if (providerType === 'hybrid') {
|
|
908
|
-
await this.tryMirrorProxyRecordingViaMetro(mockData, scenarioName, hash);
|
|
909
|
-
}
|
|
910
|
-
} catch (e) {
|
|
911
|
-
logger.error('[Mockifyer-Fetch] Proxy client mirror (provider) failed:', e);
|
|
912
|
-
}
|
|
913
|
-
return;
|
|
914
|
-
}
|
|
915
863
|
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
} catch (e) {
|
|
925
|
-
logger.error('[Mockifyer-Fetch] Proxy client mirror (Node fs) failed:', e);
|
|
926
|
-
}
|
|
927
|
-
return;
|
|
928
|
-
}
|
|
864
|
+
await mirrorProxyRecordingToClient({
|
|
865
|
+
config: this.config,
|
|
866
|
+
mockDataPath: this.config.mockDataPath,
|
|
867
|
+
recording: rec,
|
|
868
|
+
databaseProvider: this.databaseProvider,
|
|
869
|
+
databaseProviderInitPromise: this.databaseProviderInitPromise,
|
|
870
|
+
logPrefix: 'Mockifyer-Fetch',
|
|
871
|
+
});
|
|
929
872
|
|
|
930
|
-
|
|
873
|
+
if (providerType === 'hybrid' && hash && scenarioName && mockData) {
|
|
874
|
+
await this.tryMirrorProxyRecordingViaMetro(mockData, scenarioName, hash);
|
|
875
|
+
} else if (!fs && hash && scenarioName && mockData) {
|
|
876
|
+
await this.tryMirrorProxyRecordingViaMetro(mockData, scenarioName, hash);
|
|
877
|
+
}
|
|
931
878
|
}
|
|
932
879
|
|
|
933
880
|
private async tryMirrorProxyRecordingViaMetro(
|
|
@@ -1609,212 +1556,31 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1609
1556
|
return extendedClient;
|
|
1610
1557
|
}
|
|
1611
1558
|
|
|
1612
|
-
|
|
1613
|
-
function omitProxyFromPartialConfig(config: Partial<MockifyerConfig>): Partial<MockifyerConfig> {
|
|
1614
|
-
const { proxy: _drop, ...rest } = config;
|
|
1615
|
-
void _drop;
|
|
1616
|
-
return rest;
|
|
1617
|
-
}
|
|
1618
|
-
|
|
1619
|
-
/** Options for {@link initMockifyerForDashboardProxy} — dashboard + `/api/proxy` (run `mockifyer-dashboard --provider redis`). */
|
|
1620
|
-
export interface InitMockifyerForDashboardProxyOptions {
|
|
1621
|
-
/** mockifyer-dashboard origin (e.g. `http://localhost:3002`). Not the Redis URL. */
|
|
1622
|
-
dashboardBaseUrl: string;
|
|
1623
|
-
/** Mock root for scenario file fallbacks etc. Defaults `MOCKIFYER_PATH` env or `./mock-data`. */
|
|
1624
|
-
mockDataPath?: string;
|
|
1625
|
-
/** Client lane id (`X-Mockifyer-Client-Id` / proxy envelope). Defaults `MOCKIFYER_CLIENT_ID` env. */
|
|
1626
|
-
clientId?: string;
|
|
1627
|
-
deviceId?: string;
|
|
1628
|
-
scenario?: string;
|
|
1629
|
-
/**
|
|
1630
|
-
* When **`true`**, each `/api/proxy` request sends **`"record": true`**. When **`false`**, sends **`"record": false`**.
|
|
1631
|
-
* When **omitted**, the **`record`** field is omitted so the **dashboard per-scenario** toggle applies; **`MOCKIFYER_PROXY_RECORD_ON_MISS`**
|
|
1632
|
-
* and **`MOCKIFYER_RECORD=true`** (last) still set an explicit client flag when you want env-driven recording without code.
|
|
1633
|
-
*/
|
|
1634
|
-
recordOnMiss?: boolean;
|
|
1635
|
-
/**
|
|
1636
|
-
* When false, dashboard proxy stores request-only stubs on cache miss.
|
|
1637
|
-
* Defaults via {@link resolveRecordResponses} (`MOCKIFYER_RECORD_RESPONSES` env, else `false`).
|
|
1638
|
-
*/
|
|
1639
|
-
recordResponses?: boolean;
|
|
1640
|
-
strictLaneScenario?: boolean;
|
|
1641
|
-
useGlobalFetch?: boolean;
|
|
1642
|
-
/** Use a local provider for mock hits before the proxy (default in-memory only) when **`/api/proxy`** is active. */
|
|
1643
|
-
databaseProvider?: MockifyerConfig['databaseProvider'];
|
|
1644
|
-
/** Additional `MockifyerConfig` fields — applied first, then preset fields win on `mockDataPath`, `proxy`, etc. */
|
|
1645
|
-
config?: Partial<MockifyerConfig>;
|
|
1646
|
-
/**
|
|
1647
|
-
* When **`true`**, skips **`GET …/api/health`** and always sets **`proxy`**.
|
|
1648
|
-
* Default **`false`**: if Redis is not healthy (or unreachable), omit **`proxy`** and use filesystem mocks + **`recordMode`** disk saves — aligns with **`setupMockifyerForReactNative`** dev fallback.
|
|
1649
|
-
*/
|
|
1650
|
-
skipDashboardRedisHealthCheck?: boolean;
|
|
1651
|
-
/**
|
|
1652
|
-
* When the proxy records a response to Redis, also write the same mock under `mockDataPath` on this machine
|
|
1653
|
-
* (or via Metro when using strict RN + in-memory). Env: `MOCKIFYER_PROXY_MIRROR_TO_CLIENT`.
|
|
1654
|
-
*/
|
|
1655
|
-
mirrorRecordedMocksToClient?: boolean;
|
|
1656
|
-
}
|
|
1559
|
+
export type { InitMockifyerForDashboardProxyOptions, InitMockifyerForLocalFilesystemOptions };
|
|
1657
1560
|
|
|
1658
1561
|
/**
|
|
1659
|
-
* Preset:
|
|
1660
|
-
* Run **`mockifyer-dashboard --provider redis`** for the dashboard hop; use **`skipDashboardRedisHealthCheck`** to bypass the probe.
|
|
1661
|
-
*
|
|
1562
|
+
* Preset: dashboard central store proxy when health check passes; otherwise filesystem mocks.
|
|
1662
1563
|
* For full control, use {@link setupMockifyer} directly.
|
|
1663
1564
|
*/
|
|
1664
1565
|
export async function initMockifyerForDashboardProxy(
|
|
1665
1566
|
options: InitMockifyerForDashboardProxyOptions
|
|
1666
1567
|
): Promise<MockifyerInstance> {
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
throw new Error('initMockifyerForDashboardProxy: dashboardBaseUrl is required');
|
|
1671
|
-
}
|
|
1672
|
-
|
|
1673
|
-
const mockDataPath =
|
|
1674
|
-
options.mockDataPath ??
|
|
1675
|
-
extra.mockDataPath ??
|
|
1676
|
-
(typeof process !== 'undefined' && process.env?.MOCKIFYER_PATH
|
|
1677
|
-
? process.env.MOCKIFYER_PATH
|
|
1678
|
-
: './mock-data');
|
|
1679
|
-
|
|
1680
|
-
const envRecord =
|
|
1681
|
-
typeof process !== 'undefined' && process.env?.MOCKIFYER_RECORD === 'true';
|
|
1682
|
-
|
|
1683
|
-
const recordOnMiss =
|
|
1684
|
-
options.recordOnMiss ??
|
|
1685
|
-
extra.proxy?.recordOnMiss ??
|
|
1686
|
-
parseProxyRecordOnMissEnv() ??
|
|
1687
|
-
(envRecord ? true : undefined);
|
|
1688
|
-
|
|
1689
|
-
const recordResponses = resolveRecordResponses(
|
|
1690
|
-
options.recordResponses ?? extra.proxy?.recordResponses
|
|
1568
|
+
return coreInitMockifyerForDashboardProxy(
|
|
1569
|
+
{ ...options, useGlobalFetch: options.useGlobalFetch ?? options.config?.useGlobalFetch ?? true },
|
|
1570
|
+
setupMockifyer
|
|
1691
1571
|
);
|
|
1692
|
-
|
|
1693
|
-
const useRedisProxy =
|
|
1694
|
-
options.skipDashboardRedisHealthCheck === true ||
|
|
1695
|
-
(await canUseDashboardRedisProxy(dashboardBaseUrl));
|
|
1696
|
-
|
|
1697
|
-
if (!useRedisProxy) {
|
|
1698
|
-
const strictProxyOnly = resolveStrictScenarioResolution({
|
|
1699
|
-
strictScenarioResolution:
|
|
1700
|
-
options.config?.strictScenarioResolution ?? extra.strictScenarioResolution,
|
|
1701
|
-
});
|
|
1702
|
-
logger.warn(
|
|
1703
|
-
`[Mockifyer] initMockifyerForDashboardProxy: "${dashboardBaseUrl}" did not report healthy Redis ` +
|
|
1704
|
-
(strictProxyOnly
|
|
1705
|
-
? '(strict proxy-only — local recording disabled). '
|
|
1706
|
-
: '(unreachable or non-Redis provider). Falling back to filesystem mocks without proxy. ') +
|
|
1707
|
-
'Set skipDashboardRedisHealthCheck: true to force proxy anyway.'
|
|
1708
|
-
);
|
|
1709
|
-
const stripped = omitProxyFromPartialConfig(extra);
|
|
1710
|
-
const fallbackDb = options.databaseProvider ?? extra.databaseProvider;
|
|
1711
|
-
const mergedInitLogFs: MockifyerConfig['initLog'] = {
|
|
1712
|
-
...stripped.initLog,
|
|
1713
|
-
headline:
|
|
1714
|
-
stripped.initLog?.headline ??
|
|
1715
|
-
(strictProxyOnly
|
|
1716
|
-
? '[Mockifyer preset] Node · strict proxy-only (dashboard Redis health check failed)'
|
|
1717
|
-
: '[Mockifyer preset] Node · filesystem (dashboard Redis health check failed)'),
|
|
1718
|
-
};
|
|
1719
|
-
return setupMockifyer({
|
|
1720
|
-
...stripped,
|
|
1721
|
-
mockDataPath,
|
|
1722
|
-
...(fallbackDb !== undefined ? { databaseProvider: fallbackDb } : {}),
|
|
1723
|
-
...(strictProxyOnly ? { intendedProxyBaseUrl: dashboardBaseUrl.trim() } : {}),
|
|
1724
|
-
useGlobalFetch: options.useGlobalFetch ?? extra.useGlobalFetch ?? true,
|
|
1725
|
-
clientId: options.clientId ?? extra.clientId,
|
|
1726
|
-
deviceId: options.deviceId ?? extra.deviceId,
|
|
1727
|
-
initLog: mergedInitLogFs,
|
|
1728
|
-
});
|
|
1729
|
-
}
|
|
1730
|
-
|
|
1731
|
-
const upstreamProxy = extra.proxy as MockifyerConfig['proxy'] | undefined;
|
|
1732
|
-
const mergedProxy = {
|
|
1733
|
-
...upstreamProxy,
|
|
1734
|
-
baseUrl: upstreamProxy?.baseUrl ?? dashboardBaseUrl,
|
|
1735
|
-
scenario:
|
|
1736
|
-
options.scenario ??
|
|
1737
|
-
upstreamProxy?.scenario ??
|
|
1738
|
-
(typeof process !== 'undefined' && process.env?.MOCKIFYER_SCENARIO?.trim()
|
|
1739
|
-
? process.env.MOCKIFYER_SCENARIO.trim()
|
|
1740
|
-
: undefined),
|
|
1741
|
-
...(typeof recordOnMiss === 'boolean' ? { recordOnMiss } : {}),
|
|
1742
|
-
recordResponses,
|
|
1743
|
-
} as NonNullable<MockifyerConfig['proxy']>;
|
|
1744
|
-
if (
|
|
1745
|
-
options.strictLaneScenario !== undefined ||
|
|
1746
|
-
upstreamProxy?.strictLaneScenario !== undefined
|
|
1747
|
-
) {
|
|
1748
|
-
mergedProxy.strictLaneScenario =
|
|
1749
|
-
options.strictLaneScenario ?? upstreamProxy?.strictLaneScenario;
|
|
1750
|
-
}
|
|
1751
|
-
|
|
1752
|
-
const envMirrorRaw =
|
|
1753
|
-
typeof process !== 'undefined'
|
|
1754
|
-
? String(process.env?.MOCKIFYER_PROXY_MIRROR_TO_CLIENT || '').trim().toLowerCase()
|
|
1755
|
-
: '';
|
|
1756
|
-
const envMirror =
|
|
1757
|
-
envMirrorRaw === '1' || envMirrorRaw === 'true' || envMirrorRaw === 'yes' || envMirrorRaw === 'on';
|
|
1758
|
-
const mirrorRecordedMocksToClient =
|
|
1759
|
-
options.mirrorRecordedMocksToClient ??
|
|
1760
|
-
extra.proxy?.mirrorRecordedMocksToClient ??
|
|
1761
|
-
(envMirror ? true : undefined);
|
|
1762
|
-
if (mirrorRecordedMocksToClient !== undefined) {
|
|
1763
|
-
mergedProxy.mirrorRecordedMocksToClient = mirrorRecordedMocksToClient;
|
|
1764
|
-
}
|
|
1765
|
-
|
|
1766
|
-
const mergedInitLogProxy: MockifyerConfig['initLog'] = {
|
|
1767
|
-
...extra.initLog,
|
|
1768
|
-
headline: extra.initLog?.headline ?? '[Mockifyer preset] Node · dashboard Redis proxy',
|
|
1769
|
-
};
|
|
1770
|
-
|
|
1771
|
-
const strictScenarioResolution =
|
|
1772
|
-
extra.strictScenarioResolution ??
|
|
1773
|
-
options.config?.strictScenarioResolution ??
|
|
1774
|
-
true;
|
|
1775
|
-
|
|
1776
|
-
return setupMockifyer({
|
|
1777
|
-
...extra,
|
|
1778
|
-
mockDataPath,
|
|
1779
|
-
strictScenarioResolution,
|
|
1780
|
-
databaseProvider: options.databaseProvider ?? extra.databaseProvider ?? { type: 'memory' },
|
|
1781
|
-
useGlobalFetch: options.useGlobalFetch ?? extra.useGlobalFetch ?? true,
|
|
1782
|
-
clientId: options.clientId ?? extra.clientId,
|
|
1783
|
-
deviceId: options.deviceId ?? extra.deviceId,
|
|
1784
|
-
proxy: mergedProxy,
|
|
1785
|
-
initLog: mergedInitLogProxy,
|
|
1786
|
-
});
|
|
1787
|
-
}
|
|
1788
|
-
|
|
1789
|
-
export interface InitMockifyerForLocalFilesystemOptions {
|
|
1790
|
-
mockDataPath?: string;
|
|
1791
|
-
useGlobalFetch?: boolean;
|
|
1792
|
-
recordMode?: boolean;
|
|
1793
|
-
/** Applied first; preset fills `mockDataPath` / `useGlobalFetch` when omitted. */
|
|
1794
|
-
config?: Partial<MockifyerConfig>;
|
|
1795
1572
|
}
|
|
1796
1573
|
|
|
1797
1574
|
/**
|
|
1798
|
-
* Preset: local
|
|
1799
|
-
* (default `./mock-data` or `MOCKIFYER_PATH`). No dashboard proxy.
|
|
1575
|
+
* Preset: local filesystem mocks. No dashboard proxy.
|
|
1800
1576
|
*/
|
|
1801
1577
|
export function initMockifyerForLocalFilesystem(
|
|
1802
1578
|
options: InitMockifyerForLocalFilesystemOptions = {}
|
|
1803
1579
|
): MockifyerInstance {
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
(typeof process !== 'undefined' && process.env?.MOCKIFYER_PATH
|
|
1809
|
-
? process.env.MOCKIFYER_PATH
|
|
1810
|
-
: './mock-data');
|
|
1811
|
-
|
|
1812
|
-
return setupMockifyer({
|
|
1813
|
-
...extra,
|
|
1814
|
-
mockDataPath,
|
|
1815
|
-
useGlobalFetch: options.useGlobalFetch ?? extra.useGlobalFetch ?? true,
|
|
1816
|
-
recordMode: options.recordMode ?? extra.recordMode ?? false,
|
|
1817
|
-
});
|
|
1580
|
+
return coreInitMockifyerForLocalFilesystem(
|
|
1581
|
+
{ ...options, useGlobalFetch: options.useGlobalFetch ?? options.config?.useGlobalFetch ?? true },
|
|
1582
|
+
setupMockifyer
|
|
1583
|
+
);
|
|
1818
1584
|
}
|
|
1819
1585
|
|
|
1820
1586
|
/** Shared with {@link setupMockifyerForReactNative} strict-proxy branching. */
|
package/src/metro-config.ts
CHANGED
|
@@ -97,6 +97,25 @@ function isMockifyerStubbedModule(moduleName: string): boolean {
|
|
|
97
97
|
);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
/** Metro often resolves `@sgedda/mockifyer-core` to `main` instead of the RN entry. */
|
|
101
|
+
function resolveMockifyerCoreReactNativeEntry(
|
|
102
|
+
moduleName: string,
|
|
103
|
+
platform: string | null
|
|
104
|
+
): { filePath: string; type: 'sourceFile' } | null {
|
|
105
|
+
if (moduleName !== '@sgedda/mockifyer-core') {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
if (platform === 'web') {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const filePath = require.resolve('@sgedda/mockifyer-core/dist/index.react-native.js');
|
|
113
|
+
return { filePath, type: 'sourceFile' };
|
|
114
|
+
} catch {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
100
119
|
/**
|
|
101
120
|
* Options for configuring Metro for Mockifyer
|
|
102
121
|
*/
|
|
@@ -201,6 +220,11 @@ export function configureMetroForMockifyer(
|
|
|
201
220
|
};
|
|
202
221
|
}
|
|
203
222
|
|
|
223
|
+
const mockifyerCoreRn = resolveMockifyerCoreReactNativeEntry(moduleName, platform);
|
|
224
|
+
if (mockifyerCoreRn) {
|
|
225
|
+
return mockifyerCoreRn;
|
|
226
|
+
}
|
|
227
|
+
|
|
204
228
|
// Call existing resolver if present
|
|
205
229
|
if (existingResolveRequest) {
|
|
206
230
|
const result = existingResolveRequest(context, moduleName, platform);
|
|
@@ -1,58 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export interface DashboardHealthPayload {
|
|
7
|
-
status?: string;
|
|
8
|
-
provider?: string;
|
|
9
|
-
redisOk?: boolean | null;
|
|
10
|
-
sqliteOk?: boolean | null;
|
|
11
|
-
/** When true, dashboard central store (redis or sqlite) is ready for `/api/proxy`. */
|
|
12
|
-
centralStoreOk?: boolean | null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function centralStoreHealthy(data: DashboardHealthPayload): boolean {
|
|
16
|
-
if (data.centralStoreOk === true) {
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
if (data.provider === 'redis' && data.redisOk === true) {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
if (data.provider === 'sqlite' && data.sqliteOk === true) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* True when mockifyer-dashboard responds to **`GET /api/health`** with a healthy central store
|
|
30
|
-
* (**`provider === 'redis'`** and **`redisOk`**, or **`provider === 'sqlite'`** and **`sqliteOk`**,
|
|
31
|
-
* or **`centralStoreOk === true`**). Same gate as React Native strict proxy / **`initMockifyerForDashboardProxy`**.
|
|
32
|
-
*/
|
|
33
|
-
export async function canUseDashboardCentralProxy(proxyBaseUrl: string | undefined): Promise<boolean> {
|
|
34
|
-
if (!proxyBaseUrl?.trim()) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
try {
|
|
38
|
-
const controller = typeof AbortController !== 'undefined' ? new AbortController() : undefined;
|
|
39
|
-
const timeout = setTimeout(() => controller?.abort(), HEALTH_TIMEOUT_MS);
|
|
40
|
-
const url = joinProxyDashboardApiUrl(proxyBaseUrl.trim(), 'api/health');
|
|
41
|
-
const res = await fetch(url, { signal: controller?.signal });
|
|
42
|
-
clearTimeout(timeout);
|
|
43
|
-
if (!res.ok) {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
const data = (await res.json()) as DashboardHealthPayload;
|
|
47
|
-
return centralStoreHealthy(data);
|
|
48
|
-
} catch {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @deprecated Use {@link canUseDashboardCentralProxy}. Kept for existing imports.
|
|
55
|
-
*/
|
|
56
|
-
export async function canUseDashboardRedisProxy(proxyBaseUrl: string | undefined): Promise<boolean> {
|
|
57
|
-
return canUseDashboardCentralProxy(proxyBaseUrl);
|
|
58
|
-
}
|
|
1
|
+
export {
|
|
2
|
+
canUseDashboardCentralProxy,
|
|
3
|
+
canUseDashboardRedisProxy,
|
|
4
|
+
type DashboardHealthPayload,
|
|
5
|
+
} from '@sgedda/mockifyer-core/utils/dashboard-central-proxy-health';
|