@sgedda/mockifyer-fetch 1.8.71 → 1.8.85
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/index.d.ts +3 -0
- package/dist/index.js +93 -8
- package/dist/metro-atlas-key-handlers.d.ts +113 -0
- package/dist/metro-atlas-key-handlers.js +436 -0
- package/dist/metro-config.d.ts +3 -0
- package/dist/metro-config.js +31 -3
- package/dist/metro-sync-middleware.d.ts +19 -0
- package/dist/metro-sync-middleware.js +54 -11
- package/dist/react-native.d.ts +26 -2
- package/dist/react-native.js +39 -21
- package/package.json +2 -2
- package/src/index.ts +132 -11
- package/src/metro-atlas-key-handlers.ts +503 -0
- package/src/metro-config.ts +31 -3
- package/src/metro-sync-middleware.ts +92 -20
- package/src/react-native.ts +78 -24
package/dist/react-native.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { setupMockifyer } from './index';
|
|
8
8
|
import { HTTPClient } from '@sgedda/mockifyer-core';
|
|
9
|
-
import { type MockifyerRuntimeMode } from '@sgedda/mockifyer-core';
|
|
9
|
+
import { type MockifyerRuntimeMode, type MockifyerRuntimeEnabledStorage } from '@sgedda/mockifyer-core';
|
|
10
10
|
export { setupMockifyer } from './index';
|
|
11
11
|
export * from '@sgedda/mockifyer-core';
|
|
12
12
|
export interface MockifyerInstance extends HTTPClient {
|
|
@@ -15,6 +15,9 @@ export interface MockifyerInstance extends HTTPClient {
|
|
|
15
15
|
clearAllMocks: () => Promise<void>;
|
|
16
16
|
setClientId: (lane: string) => void;
|
|
17
17
|
getClientId: () => string | undefined;
|
|
18
|
+
enableMockifyer: () => void;
|
|
19
|
+
disableMockifyer: () => void;
|
|
20
|
+
isMockifyerEnabled: () => boolean;
|
|
18
21
|
}
|
|
19
22
|
/**
|
|
20
23
|
* Outcome of {@link setupMockifyerForReactNative}.
|
|
@@ -51,9 +54,27 @@ export interface ReactNativeMockifyerConfig {
|
|
|
51
54
|
bundledDataPath?: string;
|
|
52
55
|
/** Enable recording mode (development only) */
|
|
53
56
|
recordMode?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* When true, Mockifyer starts disabled (all requests bypass).
|
|
59
|
+
* Use `instance.enableMockifyer()` to turn it on via GUI.
|
|
60
|
+
*
|
|
61
|
+
* **Prefer `runtimeMode: 'manual'`** for clearer intent.
|
|
62
|
+
*
|
|
63
|
+
* Default: false (starts enabled).
|
|
64
|
+
*/
|
|
65
|
+
startDisabled?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Persist enable/disable across app restarts (AsyncStorage when `true`).
|
|
68
|
+
* With `runtimeMode: 'manual'`, first launch is off; after the user enables,
|
|
69
|
+
* the next launch stays on.
|
|
70
|
+
*/
|
|
71
|
+
persistRuntimeEnabled?: boolean | MockifyerRuntimeEnabledStorage;
|
|
54
72
|
/**
|
|
55
73
|
* When true, reads `scenario` from `react-native-launch-arguments` (optional peer).
|
|
56
74
|
* Highest priority over MOCKIFYER_SCENARIO, config.scenarios, Metro scenario sync, and scenario-config.json.
|
|
75
|
+
* When a launch `scenario` is present, Mockifyer also **starts enabled** (even with `runtimeMode: 'manual'`).
|
|
76
|
+
* Set to `false` to ignore launch scenario args entirely.
|
|
77
|
+
* Default: auto-detect when a non-empty launch `scenario` is present.
|
|
57
78
|
*/
|
|
58
79
|
useLaunchArgumentsScenario?: boolean;
|
|
59
80
|
/**
|
|
@@ -93,7 +114,10 @@ export interface ReactNativeMockifyerConfig {
|
|
|
93
114
|
launchArgumentClientIdKey?: string;
|
|
94
115
|
/**
|
|
95
116
|
* When Mockifyer may patch `fetch` at startup. Overrides **`MOCKIFYER_MODE`** env and `config.runtimeMode`.
|
|
96
|
-
* Prefer env **`MOCKIFYER_MODE`**: `off` | `on` | `launch_client`
|
|
117
|
+
* Prefer env **`MOCKIFYER_MODE`**: `off` | `on` | `launch_client` | `manual`.
|
|
118
|
+
*
|
|
119
|
+
* Use **`manual`** to patch but start disabled (enable via GUI toggle).
|
|
120
|
+
* Aliases: `e2e`, `maestro` → `launch_client`; `gui`, `toggle` → `manual`.
|
|
97
121
|
*/
|
|
98
122
|
runtimeMode?: MockifyerRuntimeMode;
|
|
99
123
|
}
|
package/dist/react-native.js
CHANGED
|
@@ -41,32 +41,26 @@ function isMockifyerReactNativeActive(result) {
|
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* Apply scenario from launch arguments and/or defaultScenario (highest priority in getCurrentScenario).
|
|
44
|
+
* @returns `true` when a native launch-argument `scenario` was applied (forces runtime toggle on).
|
|
44
45
|
*/
|
|
45
46
|
function applyReactNativeScenarioOptions(options) {
|
|
46
|
-
let
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const raw = LaunchArguments && typeof LaunchArguments.value === 'function' ? LaunchArguments.value() : undefined;
|
|
54
|
-
const scenario = raw?.scenario;
|
|
55
|
-
if (scenario !== undefined && scenario !== null && String(scenario).trim() !== '') {
|
|
56
|
-
(0, mockifyer_core_1.setScenarioLaunchOverride)(String(scenario).trim());
|
|
57
|
-
applied = true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
catch {
|
|
61
|
-
// Package not installed or native module unavailable
|
|
47
|
+
let appliedFromLaunchArgs = false;
|
|
48
|
+
// Auto-detect launch `scenario` when present (E2E). Opt out with useLaunchArgumentsScenario: false.
|
|
49
|
+
if (options.useLaunchArgumentsScenario !== false) {
|
|
50
|
+
const scenarioFromLaunch = (0, mockifyer_core_2.tryGetScenarioFromLaunchArguments)();
|
|
51
|
+
if (scenarioFromLaunch) {
|
|
52
|
+
(0, mockifyer_core_1.setScenarioLaunchOverride)(scenarioFromLaunch, { fromLaunchArguments: true });
|
|
53
|
+
appliedFromLaunchArgs = true;
|
|
62
54
|
}
|
|
63
55
|
}
|
|
64
|
-
if (!
|
|
56
|
+
if (!appliedFromLaunchArgs && options.defaultScenario !== undefined && options.defaultScenario !== null) {
|
|
65
57
|
const t = String(options.defaultScenario).trim();
|
|
66
58
|
if (t !== '') {
|
|
59
|
+
// App config default — not a native launch arg; do not force runtime toggle on.
|
|
67
60
|
(0, mockifyer_core_1.setScenarioLaunchOverride)(t);
|
|
68
61
|
}
|
|
69
62
|
}
|
|
63
|
+
return appliedFromLaunchArgs;
|
|
70
64
|
}
|
|
71
65
|
// Lazy load bundled data (only used in production builds)
|
|
72
66
|
let bundledMockData = null;
|
|
@@ -125,7 +119,7 @@ async function loadBundledMockData(bundledDataPath) {
|
|
|
125
119
|
* ```
|
|
126
120
|
*/
|
|
127
121
|
async function setupMockifyerForReactNative(options) {
|
|
128
|
-
const { isDev, mockDataPath = 'mock-data', bundledDataPath = './assets/mock-data', recordMode = false, config: userConfig = {}, proxyBaseUrl, proxyScenario, proxyRecordOnMiss, proxyRecordResponses, skipDashboardRedisHealthCheck, useLaunchArgumentsClientId = false, launchArgumentClientIdKey = mockifyer_core_2.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY, runtimeMode: runtimeModeOption, } = options;
|
|
122
|
+
const { isDev, mockDataPath = 'mock-data', bundledDataPath = './assets/mock-data', recordMode = false, startDisabled = false, persistRuntimeEnabled, config: userConfig = {}, proxyBaseUrl, proxyScenario, proxyRecordOnMiss, proxyRecordResponses, skipDashboardRedisHealthCheck, useLaunchArgumentsClientId = false, launchArgumentClientIdKey = mockifyer_core_2.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY, runtimeMode: runtimeModeOption, } = options;
|
|
129
123
|
const proxyShouldRecordOnMiss = proxyRecordOnMiss ?? recordMode;
|
|
130
124
|
const proxyShouldRecordResponses = (0, mockifyer_core_2.resolveRecordResponses)(proxyRecordResponses ?? userConfig.proxy?.recordResponses);
|
|
131
125
|
const launchClientIdKey = launchArgumentClientIdKey ?? mockifyer_core_2.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY;
|
|
@@ -134,9 +128,27 @@ async function setupMockifyerForReactNative(options) {
|
|
|
134
128
|
const resolvedRuntimeMode = (0, mockifyer_core_2.resolveMockifyerRuntimeMode)({
|
|
135
129
|
configMode: runtimeModeOption ?? userConfig.runtimeMode,
|
|
136
130
|
});
|
|
131
|
+
const persistOption = persistRuntimeEnabled ?? userConfig.persistRuntimeEnabled;
|
|
132
|
+
const runtimeEnabledStorage = (0, mockifyer_core_2.resolveRuntimeEnabledStorage)(persistOption);
|
|
133
|
+
let initialRuntimeEnabled = userConfig.initialRuntimeEnabled;
|
|
134
|
+
if (runtimeEnabledStorage && typeof initialRuntimeEnabled !== 'boolean') {
|
|
135
|
+
const saved = await (0, mockifyer_core_2.loadPersistedRuntimeEnabled)(runtimeEnabledStorage);
|
|
136
|
+
if (typeof saved === 'boolean') {
|
|
137
|
+
initialRuntimeEnabled = saved;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// Apply scenario launch override early so we know if E2E forced a scenario.
|
|
141
|
+
const launchScenarioApplied = applyReactNativeScenarioOptions(options);
|
|
142
|
+
if (launchScenarioApplied) {
|
|
143
|
+
// Launch `scenario` arg → start enabled (wins over persisted off for this session).
|
|
144
|
+
initialRuntimeEnabled = true;
|
|
145
|
+
mockifyer_core_2.logger.info('[Mockifyer] Launch argument scenario present — starting with Mockifyer ENABLED');
|
|
146
|
+
}
|
|
137
147
|
const mergedConfig = {
|
|
138
148
|
...userConfig,
|
|
139
149
|
runtimeMode: resolvedRuntimeMode,
|
|
150
|
+
...(persistOption !== undefined ? { persistRuntimeEnabled: persistOption } : {}),
|
|
151
|
+
...(typeof initialRuntimeEnabled === 'boolean' ? { initialRuntimeEnabled } : {}),
|
|
140
152
|
...(shouldApplyLaunchClientId
|
|
141
153
|
? {
|
|
142
154
|
useLaunchArgumentsClientId: true,
|
|
@@ -152,8 +164,13 @@ async function setupMockifyerForReactNative(options) {
|
|
|
152
164
|
}
|
|
153
165
|
return 'React Native dev · hybrid (device + Metro)';
|
|
154
166
|
};
|
|
155
|
-
|
|
156
|
-
|
|
167
|
+
// Patch fetch for on / manual; launch_client only when lane id is present.
|
|
168
|
+
// Launch `scenario` forces runtime toggle on after activation — it does not replace client id.
|
|
169
|
+
const isEnabled = (0, mockifyer_core_2.shouldActivateMockifyerForReactNative)({
|
|
170
|
+
runtimeMode: resolvedRuntimeMode,
|
|
171
|
+
hasLaunchClientId: Boolean(clientIdFromLaunchArgs),
|
|
172
|
+
hasLaunchScenario: launchScenarioApplied,
|
|
173
|
+
});
|
|
157
174
|
if (!isEnabled) {
|
|
158
175
|
(0, mockifyer_core_2.logMockifyerNotActivated)(resolvedRuntimeMode, {
|
|
159
176
|
launchClientIdKey,
|
|
@@ -161,7 +178,6 @@ async function setupMockifyerForReactNative(options) {
|
|
|
161
178
|
});
|
|
162
179
|
return { status: 'not_activated', instance: null };
|
|
163
180
|
}
|
|
164
|
-
applyReactNativeScenarioOptions(options);
|
|
165
181
|
if (isDev === true) {
|
|
166
182
|
const strictProxyEnabled = proxyBaseUrl
|
|
167
183
|
? skipDashboardRedisHealthCheck === true ||
|
|
@@ -210,6 +226,7 @@ async function setupMockifyerForReactNative(options) {
|
|
|
210
226
|
: databaseProviderConfig,
|
|
211
227
|
recordMode,
|
|
212
228
|
useGlobalFetch: true,
|
|
229
|
+
startDisabled,
|
|
213
230
|
proxy: strictProxyEnabled && proxyBaseUrl
|
|
214
231
|
? {
|
|
215
232
|
baseUrl: proxyBaseUrl,
|
|
@@ -271,6 +288,7 @@ async function setupMockifyerForReactNative(options) {
|
|
|
271
288
|
},
|
|
272
289
|
recordMode: false, // Can't record in production builds
|
|
273
290
|
useGlobalFetch: true,
|
|
291
|
+
startDisabled,
|
|
274
292
|
proxy: proxyBaseUrl
|
|
275
293
|
? {
|
|
276
294
|
baseUrl: proxyBaseUrl,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.85",
|
|
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.10.
|
|
52
|
+
"@sgedda/mockifyer-core": "^1.10.162"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^20.11.24",
|
package/src/index.ts
CHANGED
|
@@ -24,12 +24,14 @@ import {
|
|
|
24
24
|
generateRequestKey as generateRequestKeyUtil,
|
|
25
25
|
CachedMockData,
|
|
26
26
|
initializeDateManipulation,
|
|
27
|
+
scheduleRuntimeDateSyncFromConfig,
|
|
27
28
|
DatabaseProvider,
|
|
28
29
|
createProvider,
|
|
29
30
|
getCurrentScenario,
|
|
30
31
|
getScenarioFolderPath,
|
|
31
32
|
ensureScenarioFolder,
|
|
32
33
|
initializeScenario,
|
|
34
|
+
isScenarioLaunchFromNativeArguments,
|
|
33
35
|
TestGenerator,
|
|
34
36
|
TestGenerationOptions,
|
|
35
37
|
checkRequestLimit,
|
|
@@ -73,7 +75,8 @@ import {
|
|
|
73
75
|
networkEventHashFromRequestKey,
|
|
74
76
|
recordInlineTraceHopFromExchange,
|
|
75
77
|
unwrapAndMergeInlineTraceEnvelope,
|
|
76
|
-
|
|
78
|
+
unwrapInlineTraceEnvelopeEmittingNetworkEvents,
|
|
79
|
+
resolveNetworkLogIncludeTraceOptions,
|
|
77
80
|
getInlineTraceEnvelopeBusinessBody,
|
|
78
81
|
resolveRecordResponses,
|
|
79
82
|
applyOutboundRequestCorrelation,
|
|
@@ -90,6 +93,10 @@ import {
|
|
|
90
93
|
DomainPathRulesSession,
|
|
91
94
|
installNodeInboundRequestCorrelationCapture,
|
|
92
95
|
stripMockifyerTraceFromBody,
|
|
96
|
+
resolveInitialRuntimeEnabled,
|
|
97
|
+
resolveRuntimeEnabledStorage,
|
|
98
|
+
savePersistedRuntimeEnabled,
|
|
99
|
+
type MockifyerRuntimeEnabledStorage,
|
|
93
100
|
} from '@sgedda/mockifyer-core';
|
|
94
101
|
import { logger, setLogLevel } from '@sgedda/mockifyer-core';
|
|
95
102
|
import {
|
|
@@ -130,6 +137,10 @@ class MockifyerClass {
|
|
|
130
137
|
*/
|
|
131
138
|
private readonly poolResponseCache = new Map<string, PoolResponseItem>();
|
|
132
139
|
private readonly domainPathRules: DomainPathRulesSession;
|
|
140
|
+
/** Runtime toggle: when false, all Mockifyer logic is bypassed (no dashboard, Redis, proxy, or mock lookup). */
|
|
141
|
+
private runtimeEnabled: boolean;
|
|
142
|
+
/** Optional storage for persisting enable/disable across restarts. */
|
|
143
|
+
private readonly runtimeEnabledStorage?: MockifyerRuntimeEnabledStorage;
|
|
133
144
|
|
|
134
145
|
/** Session id for timeline hops — per-screen id from {@link setFlightRecorderRuntimeContext} when set. */
|
|
135
146
|
private getRuntimeSessionId(): string {
|
|
@@ -144,6 +155,36 @@ class MockifyerClass {
|
|
|
144
155
|
});
|
|
145
156
|
}
|
|
146
157
|
|
|
158
|
+
private unwrapResponseInlineTrace(response: {
|
|
159
|
+
data?: unknown;
|
|
160
|
+
config?: unknown;
|
|
161
|
+
}): void {
|
|
162
|
+
const correlation = this.readRequestCorrelation(response.config);
|
|
163
|
+
const parentRequestId = correlation?.requestId?.trim();
|
|
164
|
+
if (!parentRequestId) {
|
|
165
|
+
response.data = unwrapAndMergeInlineTraceEnvelope(response.data);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const scenario =
|
|
169
|
+
this.config.proxy?.scenario?.trim() ||
|
|
170
|
+
getCurrentScenario(this.config.mockDataPath);
|
|
171
|
+
response.data = unwrapInlineTraceEnvelopeEmittingNetworkEvents(response.data, {
|
|
172
|
+
parentRequestId,
|
|
173
|
+
config: this.config,
|
|
174
|
+
scenario,
|
|
175
|
+
clientId: this.config.clientId,
|
|
176
|
+
sessionId: this.getRuntimeSessionId(),
|
|
177
|
+
transport: this.usesDashboardProxy() ? 'proxy' : 'fetch',
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private applyOutboundCorrelation(config: unknown): RequestCorrelationContext {
|
|
182
|
+
return applyOutboundRequestCorrelation(
|
|
183
|
+
config as { headers?: unknown; url?: unknown; method?: unknown },
|
|
184
|
+
resolveNetworkLogIncludeTraceOptions(this.config)
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
147
188
|
/** Best-effort dashboard network log. */
|
|
148
189
|
private logNetworkEvent(
|
|
149
190
|
partial: Parameters<typeof emitMockifyerNetworkEvent>[0]['event'] & {
|
|
@@ -190,12 +231,8 @@ class MockifyerClass {
|
|
|
190
231
|
transport: eventPartial.transport ?? 'fetch',
|
|
191
232
|
requestId: correlation?.requestId ?? eventPartial.requestId,
|
|
192
233
|
parentRequestId: correlation?.parentRequestId ?? eventPartial.parentRequestId,
|
|
193
|
-
requestBodyPreview:
|
|
194
|
-
|
|
195
|
-
(requestBody !== undefined ? toNetworkLogBodyPreview(requestBody) : undefined),
|
|
196
|
-
responseBodyPreview:
|
|
197
|
-
eventPartial.responseBodyPreview ??
|
|
198
|
-
(responseBody !== undefined ? toNetworkLogBodyPreview(businessResponseBody) : undefined),
|
|
234
|
+
requestBodyPreview: eventPartial.requestBodyPreview,
|
|
235
|
+
responseBodyPreview: eventPartial.responseBodyPreview,
|
|
199
236
|
},
|
|
200
237
|
});
|
|
201
238
|
}
|
|
@@ -413,6 +450,41 @@ class MockifyerClass {
|
|
|
413
450
|
}
|
|
414
451
|
this.activationMode = resolveActivationMode(this.config);
|
|
415
452
|
this.domainPathRules = new DomainPathRulesSession({ config: this.config });
|
|
453
|
+
|
|
454
|
+
this.runtimeEnabledStorage = resolveRuntimeEnabledStorage(config.persistRuntimeEnabled);
|
|
455
|
+
if (config.persistRuntimeEnabled === true && !this.runtimeEnabledStorage) {
|
|
456
|
+
logger.warn(
|
|
457
|
+
'[Mockifyer-Fetch] persistRuntimeEnabled: true but no storage found. ' +
|
|
458
|
+
'Install @react-native-async-storage/async-storage (RN) or use a custom { getItem, setItem }.'
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
this.runtimeEnabled = resolveInitialRuntimeEnabled({
|
|
462
|
+
initialRuntimeEnabled: config.initialRuntimeEnabled,
|
|
463
|
+
startDisabled: config.startDisabled,
|
|
464
|
+
runtimeMode: config.runtimeMode,
|
|
465
|
+
launchScenarioPresent: isScenarioLaunchFromNativeArguments(),
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
if (!this.runtimeEnabled) {
|
|
469
|
+
const reason =
|
|
470
|
+
typeof config.initialRuntimeEnabled === 'boolean'
|
|
471
|
+
? 'persisted / initialRuntimeEnabled: false'
|
|
472
|
+
: config.runtimeMode === 'manual'
|
|
473
|
+
? 'runtimeMode: "manual"'
|
|
474
|
+
: 'startDisabled: true';
|
|
475
|
+
logger.info(
|
|
476
|
+
`[Mockifyer-Fetch] Starting with Mockifyer DISABLED (${reason}). Call enableMockifyer() to activate.`
|
|
477
|
+
);
|
|
478
|
+
} else if (isScenarioLaunchFromNativeArguments()) {
|
|
479
|
+
logger.info(
|
|
480
|
+
'[Mockifyer-Fetch] Starting with Mockifyer ENABLED (native launch scenario argument).'
|
|
481
|
+
);
|
|
482
|
+
} else if (typeof config.initialRuntimeEnabled === 'boolean' && config.initialRuntimeEnabled) {
|
|
483
|
+
logger.info(
|
|
484
|
+
'[Mockifyer-Fetch] Starting with Mockifyer ENABLED (restored from persisted preference).'
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
|
|
416
488
|
configureFlightRecorder(resolveFlightRecorderConfig(this.config));
|
|
417
489
|
configureAtlas(this.config, {
|
|
418
490
|
scenario: getCurrentScenario(this.config.mockDataPath, this.config.clientId),
|
|
@@ -743,6 +815,12 @@ class MockifyerClass {
|
|
|
743
815
|
|
|
744
816
|
private setupMockResponses(): void {
|
|
745
817
|
this.httpClient.interceptors.request.use(async (config: any) => {
|
|
818
|
+
// CRITICAL: Runtime toggle — if disabled, bypass all Mockifyer logic
|
|
819
|
+
if (!this.runtimeEnabled) {
|
|
820
|
+
(config as any).__mockifyer_bypass = true;
|
|
821
|
+
return config;
|
|
822
|
+
}
|
|
823
|
+
|
|
746
824
|
// CRITICAL: Completely bypass Mockifyer interception for sync endpoints
|
|
747
825
|
// This prevents any Mockifyer processing (mocking, saving, etc.) for these endpoints
|
|
748
826
|
const url = config.url || '';
|
|
@@ -763,7 +841,7 @@ class MockifyerClass {
|
|
|
763
841
|
useProxyLane: { proxyBaseUrl: this.config.proxy?.baseUrl, resolvedClientId: this.config.clientId },
|
|
764
842
|
})
|
|
765
843
|
) {
|
|
766
|
-
|
|
844
|
+
this.applyOutboundCorrelation(config);
|
|
767
845
|
(config as any).__mockifyer_bypass = true;
|
|
768
846
|
return config;
|
|
769
847
|
}
|
|
@@ -776,7 +854,7 @@ class MockifyerClass {
|
|
|
776
854
|
return config;
|
|
777
855
|
}
|
|
778
856
|
|
|
779
|
-
let correlation =
|
|
857
|
+
let correlation = this.applyOutboundCorrelation(config);
|
|
780
858
|
this.stashRequestCorrelation(config, correlation);
|
|
781
859
|
|
|
782
860
|
// Normalize empty params: treat {} the same as undefined for consistent matching
|
|
@@ -1067,7 +1145,7 @@ class MockifyerClass {
|
|
|
1067
1145
|
},
|
|
1068
1146
|
this.readRequestCorrelation(response.config)
|
|
1069
1147
|
);
|
|
1070
|
-
|
|
1148
|
+
this.unwrapResponseInlineTrace(response);
|
|
1071
1149
|
|
|
1072
1150
|
const capturedResponse: StoredResponse = {
|
|
1073
1151
|
status: response.status,
|
|
@@ -1123,7 +1201,7 @@ class MockifyerClass {
|
|
|
1123
1201
|
},
|
|
1124
1202
|
this.readRequestCorrelation(response.config)
|
|
1125
1203
|
);
|
|
1126
|
-
|
|
1204
|
+
this.unwrapResponseInlineTrace(response);
|
|
1127
1205
|
|
|
1128
1206
|
// Only save locally if recordMode is enabled AND we're not proxying upstream calls.
|
|
1129
1207
|
// When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
|
|
@@ -1677,6 +1755,42 @@ class MockifyerClass {
|
|
|
1677
1755
|
return this.config.clientId;
|
|
1678
1756
|
}
|
|
1679
1757
|
|
|
1758
|
+
/**
|
|
1759
|
+
* Enable Mockifyer at runtime. All subsequent requests will go through Mockifyer
|
|
1760
|
+
* (mock lookup, recording, dashboard/Redis proxy, etc.).
|
|
1761
|
+
* When `persistRuntimeEnabled` is set, the preference is saved for the next app launch.
|
|
1762
|
+
*/
|
|
1763
|
+
enableMockifyer(): void {
|
|
1764
|
+
this.runtimeEnabled = true;
|
|
1765
|
+
logger.info('[Mockifyer-Fetch] Mockifyer enabled at runtime');
|
|
1766
|
+
void this.persistRuntimeEnabledState(true);
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
/**
|
|
1770
|
+
* Disable Mockifyer at runtime. All subsequent requests will bypass Mockifyer completely
|
|
1771
|
+
* (no dashboard, Redis, proxy, mock lookup, or recording).
|
|
1772
|
+
* When `persistRuntimeEnabled` is set, the preference is saved for the next app launch.
|
|
1773
|
+
*/
|
|
1774
|
+
disableMockifyer(): void {
|
|
1775
|
+
this.runtimeEnabled = false;
|
|
1776
|
+
logger.info('[Mockifyer-Fetch] Mockifyer disabled at runtime - all requests will bypass');
|
|
1777
|
+
void this.persistRuntimeEnabledState(false);
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
/**
|
|
1781
|
+
* Check if Mockifyer is currently enabled at runtime.
|
|
1782
|
+
*/
|
|
1783
|
+
isMockifyerEnabled(): boolean {
|
|
1784
|
+
return this.runtimeEnabled;
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
private async persistRuntimeEnabledState(enabled: boolean): Promise<void> {
|
|
1788
|
+
if (!this.runtimeEnabledStorage) {
|
|
1789
|
+
return;
|
|
1790
|
+
}
|
|
1791
|
+
await savePersistedRuntimeEnabled(this.runtimeEnabledStorage, enabled);
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1680
1794
|
getHTTPClient(): HTTPClient {
|
|
1681
1795
|
return this.httpClient;
|
|
1682
1796
|
}
|
|
@@ -1723,6 +1837,9 @@ export interface MockifyerInstance extends HTTPClient {
|
|
|
1723
1837
|
clearAllMocks: () => Promise<void>;
|
|
1724
1838
|
setClientId: (lane: string) => void;
|
|
1725
1839
|
getClientId: () => string | undefined;
|
|
1840
|
+
enableMockifyer: () => void;
|
|
1841
|
+
disableMockifyer: () => void;
|
|
1842
|
+
isMockifyerEnabled: () => boolean;
|
|
1726
1843
|
}
|
|
1727
1844
|
|
|
1728
1845
|
export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
@@ -1761,6 +1878,7 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1761
1878
|
|
|
1762
1879
|
initializeDateManipulation(resolvedConfig);
|
|
1763
1880
|
initializeScenario(resolvedConfig);
|
|
1881
|
+
scheduleRuntimeDateSyncFromConfig(resolvedConfig);
|
|
1764
1882
|
|
|
1765
1883
|
|
|
1766
1884
|
const mockifyer = new MockifyerClass(resolvedConfig);
|
|
@@ -1896,6 +2014,9 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1896
2014
|
extendedClient.clearAllMocks = () => mockifyer.clearAllMocks();
|
|
1897
2015
|
extendedClient.setClientId = (lane: string) => mockifyer.setClientId(lane);
|
|
1898
2016
|
extendedClient.getClientId = () => mockifyer.getClientId();
|
|
2017
|
+
extendedClient.enableMockifyer = () => mockifyer.enableMockifyer();
|
|
2018
|
+
extendedClient.disableMockifyer = () => mockifyer.disableMockifyer();
|
|
2019
|
+
extendedClient.isMockifyerEnabled = () => mockifyer.isMockifyerEnabled();
|
|
1899
2020
|
|
|
1900
2021
|
registerMockifyerInstance(extendedClient);
|
|
1901
2022
|
|