@sgedda/mockifyer-fetch 1.8.37 → 1.8.39
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/core-proxy.d.ts +1 -1
- package/dist/core-proxy.js +9 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +119 -3
- package/dist/metro-sync-middleware.d.ts +2 -1
- package/dist/metro-sync-middleware.js +52 -1
- package/dist/react-native.d.ts +2 -0
- package/dist/react-native.js +20 -1
- package/package.json +10 -2
- package/src/core-proxy.ts +9 -0
- package/src/index.ts +165 -7
- package/src/metro-sync-middleware.ts +58 -1
- package/src/react-native.ts +5 -0
package/dist/core-proxy.d.ts
CHANGED
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export { applyProxyRecordOnMissEnv } from '@sgedda/mockifyer-core/utils/apply-proxy-record-on-miss-env';
|
|
5
5
|
export { mirrorProxyRecordingToClient } from '@sgedda/mockifyer-core/utils/mirror-proxy-recording-to-client';
|
|
6
|
-
export { initMockifyerForDashboardProxy, initMockifyerForLocalFilesystem, type InitMockifyerForDashboardProxyOptions, type InitMockifyerForLocalFilesystemOptions, } from '@sgedda/mockifyer-core/utils/init-mockifyer-presets';
|
|
6
|
+
export { initMockifyerForDashboardProxy, initMockifyerForLocalFilesystem, initMockifyerForDashboardProxyClients, initMockifyerForLocalFilesystemClients, resolveClientInitFlags, needsSiblingClientSetup, syncDualMockifyerControls, pickPrimaryDualMockifyerInstance, loadAxiosSetupMockifyer, loadFetchSetupMockifyer, type InitMockifyerForDashboardProxyOptions, type InitMockifyerForLocalFilesystemOptions, type DualMockifyerControlSurface, } from '@sgedda/mockifyer-core/utils/init-mockifyer-presets';
|
|
7
7
|
export { performDashboardProxyRequest } from '@sgedda/mockifyer-core/utils/perform-dashboard-proxy-request';
|
package/dist/core-proxy.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.performDashboardProxyRequest = exports.initMockifyerForLocalFilesystem = exports.initMockifyerForDashboardProxy = exports.mirrorProxyRecordingToClient = exports.applyProxyRecordOnMissEnv = void 0;
|
|
3
|
+
exports.performDashboardProxyRequest = exports.loadFetchSetupMockifyer = exports.loadAxiosSetupMockifyer = exports.pickPrimaryDualMockifyerInstance = exports.syncDualMockifyerControls = exports.needsSiblingClientSetup = exports.resolveClientInitFlags = exports.initMockifyerForLocalFilesystemClients = exports.initMockifyerForDashboardProxyClients = exports.initMockifyerForLocalFilesystem = exports.initMockifyerForDashboardProxy = exports.mirrorProxyRecordingToClient = exports.applyProxyRecordOnMissEnv = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Proxy/dashboard helpers from core via subpath exports (RN-safe; avoids barrel resolution).
|
|
6
6
|
*/
|
|
@@ -11,5 +11,13 @@ Object.defineProperty(exports, "mirrorProxyRecordingToClient", { enumerable: tru
|
|
|
11
11
|
var init_mockifyer_presets_1 = require("@sgedda/mockifyer-core/utils/init-mockifyer-presets");
|
|
12
12
|
Object.defineProperty(exports, "initMockifyerForDashboardProxy", { enumerable: true, get: function () { return init_mockifyer_presets_1.initMockifyerForDashboardProxy; } });
|
|
13
13
|
Object.defineProperty(exports, "initMockifyerForLocalFilesystem", { enumerable: true, get: function () { return init_mockifyer_presets_1.initMockifyerForLocalFilesystem; } });
|
|
14
|
+
Object.defineProperty(exports, "initMockifyerForDashboardProxyClients", { enumerable: true, get: function () { return init_mockifyer_presets_1.initMockifyerForDashboardProxyClients; } });
|
|
15
|
+
Object.defineProperty(exports, "initMockifyerForLocalFilesystemClients", { enumerable: true, get: function () { return init_mockifyer_presets_1.initMockifyerForLocalFilesystemClients; } });
|
|
16
|
+
Object.defineProperty(exports, "resolveClientInitFlags", { enumerable: true, get: function () { return init_mockifyer_presets_1.resolveClientInitFlags; } });
|
|
17
|
+
Object.defineProperty(exports, "needsSiblingClientSetup", { enumerable: true, get: function () { return init_mockifyer_presets_1.needsSiblingClientSetup; } });
|
|
18
|
+
Object.defineProperty(exports, "syncDualMockifyerControls", { enumerable: true, get: function () { return init_mockifyer_presets_1.syncDualMockifyerControls; } });
|
|
19
|
+
Object.defineProperty(exports, "pickPrimaryDualMockifyerInstance", { enumerable: true, get: function () { return init_mockifyer_presets_1.pickPrimaryDualMockifyerInstance; } });
|
|
20
|
+
Object.defineProperty(exports, "loadAxiosSetupMockifyer", { enumerable: true, get: function () { return init_mockifyer_presets_1.loadAxiosSetupMockifyer; } });
|
|
21
|
+
Object.defineProperty(exports, "loadFetchSetupMockifyer", { enumerable: true, get: function () { return init_mockifyer_presets_1.loadFetchSetupMockifyer; } });
|
|
14
22
|
var perform_dashboard_proxy_request_1 = require("@sgedda/mockifyer-core/utils/perform-dashboard-proxy-request");
|
|
15
23
|
Object.defineProperty(exports, "performDashboardProxyRequest", { enumerable: true, get: function () { return perform_dashboard_proxy_request_1.performDashboardProxyRequest; } });
|
package/dist/index.d.ts
CHANGED
|
@@ -11,11 +11,14 @@ export declare function setupMockifyer(config: MockifyerConfig): MockifyerInstan
|
|
|
11
11
|
export type { InitMockifyerForDashboardProxyOptions, InitMockifyerForLocalFilesystemOptions };
|
|
12
12
|
/**
|
|
13
13
|
* Preset: dashboard central store proxy when health check passes; otherwise filesystem mocks.
|
|
14
|
+
* Set both `useGlobalFetch` and `useGlobalAxios` to patch fetch and axios in one call
|
|
15
|
+
* (requires `@sgedda/mockifyer-axios` when axios is enabled).
|
|
14
16
|
* For full control, use {@link setupMockifyer} directly.
|
|
15
17
|
*/
|
|
16
18
|
export declare function initMockifyerForDashboardProxy(options: InitMockifyerForDashboardProxyOptions): Promise<MockifyerInstance>;
|
|
17
19
|
/**
|
|
18
20
|
* Preset: local filesystem mocks. No dashboard proxy.
|
|
21
|
+
* Set both `useGlobalFetch` and `useGlobalAxios` to patch both clients in one call.
|
|
19
22
|
*/
|
|
20
23
|
export declare function initMockifyerForLocalFilesystem(options?: InitMockifyerForLocalFilesystemOptions): MockifyerInstance;
|
|
21
24
|
/** Shared with {@link setupMockifyerForReactNative} strict-proxy branching. */
|
package/dist/index.js
CHANGED
|
@@ -70,6 +70,86 @@ class MockifyerClass {
|
|
|
70
70
|
const parentRequestId = config.__mockifyer_parentRequestId;
|
|
71
71
|
return parentRequestId ? { requestId, parentRequestId } : { requestId };
|
|
72
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Serve stored mock body with optional `$pool` resolution from the local fixture pool.
|
|
75
|
+
* Always passes `loadPoolResponse` — including React Native where Node `fs`/`path` are
|
|
76
|
+
* missing or Metro-stubbed — so `$pool` refs resolve via disk and/or {@link poolResponseCache}.
|
|
77
|
+
*/
|
|
78
|
+
prepareStoredResponseBody(mockData) {
|
|
79
|
+
const joinPath = path && typeof path.join === 'function' ? path.join.bind(path) : null;
|
|
80
|
+
return (0, mockifyer_core_1.prepareMockResponseBody)(mockData, mockifyer_core_1.getCurrentDate, {
|
|
81
|
+
loadPoolResponse: (0, mockifyer_core_1.createServeTimePoolResponseLoader)({
|
|
82
|
+
mockDataPath: this.config.mockDataPath,
|
|
83
|
+
nodeFs: (0, mockifyer_core_1.isUsableNodeLikePoolFs)(fs) ? fs : null,
|
|
84
|
+
joinPath,
|
|
85
|
+
cache: this.poolResponseCache,
|
|
86
|
+
}),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Ensure `$pool` fixtures are available in {@link poolResponseCache} when Node fs
|
|
91
|
+
* cannot load them (React Native). Tries database provider, then Metro.
|
|
92
|
+
*/
|
|
93
|
+
async warmPoolResponseCache(mockData) {
|
|
94
|
+
if (!(0, mockifyer_core_1.arePoolRefsEnabled)() || !(0, mockifyer_core_1.containsPoolRefs)(mockData.response?.data)) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if ((0, mockifyer_core_1.isUsableNodeLikePoolFs)(fs)) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const ids = (0, mockifyer_core_1.collectPoolRefIds)(mockData.response.data);
|
|
101
|
+
for (const id of ids) {
|
|
102
|
+
if (this.poolResponseCache.has(id)) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const fromProvider = await this.loadPoolResponseFromProvider(id);
|
|
106
|
+
if (fromProvider) {
|
|
107
|
+
this.poolResponseCache.set(id, fromProvider);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const fromMetro = await this.loadPoolResponseFromMetro(id);
|
|
111
|
+
if (fromMetro) {
|
|
112
|
+
this.poolResponseCache.set(id, fromMetro);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async loadPoolResponseFromProvider(id) {
|
|
117
|
+
const provider = this.databaseProvider;
|
|
118
|
+
if (!provider || typeof provider.loadPoolResponseItem !== 'function') {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
return (await provider.loadPoolResponseItem(id)) ?? undefined;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
mockifyer_core_2.logger.warn(`[Mockifyer-Fetch] Failed to load pool response "${id}" from provider:`, error);
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async loadPoolResponseFromMetro(id) {
|
|
130
|
+
const metroPort = this.config.databaseProvider?.options?.metroPort ?? 8081;
|
|
131
|
+
const originalFetch = global.__mockifyer_original_fetch ||
|
|
132
|
+
(typeof fetch !== 'undefined' ? fetch : undefined);
|
|
133
|
+
if (!originalFetch) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const url = `http://localhost:${metroPort}/mockifyer-pool-response?id=${encodeURIComponent(id)}`;
|
|
138
|
+
const response = await originalFetch(url);
|
|
139
|
+
if (!response.ok) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
const payload = (await response.json());
|
|
143
|
+
if (payload.success && payload.item?.responseItemId && payload.item.response) {
|
|
144
|
+
return payload.item;
|
|
145
|
+
}
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
mockifyer_core_2.logger.warn(`[Mockifyer-Fetch] Failed to load pool response "${id}" from Metro:`, error);
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
73
153
|
constructor(config) {
|
|
74
154
|
this.processingRequests = new Set();
|
|
75
155
|
this.savingResponses = new Set();
|
|
@@ -78,6 +158,11 @@ class MockifyerClass {
|
|
|
78
158
|
this.currentSessionId = null;
|
|
79
159
|
this.sessionStartTime = 0;
|
|
80
160
|
this.SESSION_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
|
|
161
|
+
/**
|
|
162
|
+
* Promoted pool responses for serve-time `$pool` resolve when Node `fs` is
|
|
163
|
+
* unavailable (React Native / Metro stubs). Seeded via {@link warmPoolResponseCache}.
|
|
164
|
+
*/
|
|
165
|
+
this.poolResponseCache = new Map();
|
|
81
166
|
// Validate database provider - filesystem, expo-filesystem, hybrid, and memory are supported
|
|
82
167
|
if (config.databaseProvider && config.databaseProvider.type) {
|
|
83
168
|
const supportedTypes = ['filesystem', 'expo-filesystem', 'hybrid', 'memory', 'redis', 'sqlite'];
|
|
@@ -473,8 +558,9 @@ class MockifyerClass {
|
|
|
473
558
|
'x-mockifyer-filename': filename,
|
|
474
559
|
'x-mockifyer-filepath': filePath
|
|
475
560
|
};
|
|
561
|
+
await this.warmPoolResponseCache(mockData);
|
|
476
562
|
const mockResponse = {
|
|
477
|
-
data:
|
|
563
|
+
data: this.prepareStoredResponseBody(mockData),
|
|
478
564
|
status: mockData.response.status,
|
|
479
565
|
statusText: 'OK',
|
|
480
566
|
headers: responseHeaders,
|
|
@@ -1256,6 +1342,7 @@ function setupMockifyer(config) {
|
|
|
1256
1342
|
throw error;
|
|
1257
1343
|
}
|
|
1258
1344
|
};
|
|
1345
|
+
mockifyer_core_2.logger.info('[Mockifyer] ✅ Patched global fetch (useGlobalFetch: true)');
|
|
1259
1346
|
}
|
|
1260
1347
|
const extendedClient = httpClient;
|
|
1261
1348
|
extendedClient.reloadMockData = async (syncFromProject) => await mockifyer.reloadMockData(syncFromProject);
|
|
@@ -1268,16 +1355,45 @@ function setupMockifyer(config) {
|
|
|
1268
1355
|
}
|
|
1269
1356
|
/**
|
|
1270
1357
|
* Preset: dashboard central store proxy when health check passes; otherwise filesystem mocks.
|
|
1358
|
+
* Set both `useGlobalFetch` and `useGlobalAxios` to patch fetch and axios in one call
|
|
1359
|
+
* (requires `@sgedda/mockifyer-axios` when axios is enabled).
|
|
1271
1360
|
* For full control, use {@link setupMockifyer} directly.
|
|
1272
1361
|
*/
|
|
1273
1362
|
async function initMockifyerForDashboardProxy(options) {
|
|
1274
|
-
|
|
1363
|
+
const flags = (0, core_proxy_1.resolveClientInitFlags)(options, {
|
|
1364
|
+
useGlobalFetch: true,
|
|
1365
|
+
useGlobalAxios: false,
|
|
1366
|
+
});
|
|
1367
|
+
if (!(0, core_proxy_1.needsSiblingClientSetup)('fetch', flags)) {
|
|
1368
|
+
return (0, core_proxy_1.initMockifyerForDashboardProxy)({ ...options, useGlobalFetch: flags.useFetch, useGlobalAxios: false }, setupMockifyer);
|
|
1369
|
+
}
|
|
1370
|
+
const result = await (0, core_proxy_1.initMockifyerForDashboardProxyClients)(options, {
|
|
1371
|
+
...(flags.useFetch ? { fetch: setupMockifyer } : {}),
|
|
1372
|
+
...(flags.useAxios
|
|
1373
|
+
? { axios: (0, core_proxy_1.loadAxiosSetupMockifyer)() }
|
|
1374
|
+
: {}),
|
|
1375
|
+
}, flags);
|
|
1376
|
+
return (0, core_proxy_1.pickPrimaryDualMockifyerInstance)('fetch', flags, result);
|
|
1275
1377
|
}
|
|
1276
1378
|
/**
|
|
1277
1379
|
* Preset: local filesystem mocks. No dashboard proxy.
|
|
1380
|
+
* Set both `useGlobalFetch` and `useGlobalAxios` to patch both clients in one call.
|
|
1278
1381
|
*/
|
|
1279
1382
|
function initMockifyerForLocalFilesystem(options = {}) {
|
|
1280
|
-
|
|
1383
|
+
const flags = (0, core_proxy_1.resolveClientInitFlags)(options, {
|
|
1384
|
+
useGlobalFetch: true,
|
|
1385
|
+
useGlobalAxios: false,
|
|
1386
|
+
});
|
|
1387
|
+
if (!(0, core_proxy_1.needsSiblingClientSetup)('fetch', flags)) {
|
|
1388
|
+
return (0, core_proxy_1.initMockifyerForLocalFilesystem)({ ...options, useGlobalFetch: flags.useFetch, useGlobalAxios: false }, setupMockifyer);
|
|
1389
|
+
}
|
|
1390
|
+
const result = (0, core_proxy_1.initMockifyerForLocalFilesystemClients)(options, {
|
|
1391
|
+
...(flags.useFetch ? { fetch: setupMockifyer } : {}),
|
|
1392
|
+
...(flags.useAxios
|
|
1393
|
+
? { axios: (0, core_proxy_1.loadAxiosSetupMockifyer)() }
|
|
1394
|
+
: {}),
|
|
1395
|
+
}, flags);
|
|
1396
|
+
return (0, core_proxy_1.pickPrimaryDualMockifyerInstance)('fetch', flags, result);
|
|
1281
1397
|
}
|
|
1282
1398
|
/** Shared with {@link setupMockifyerForReactNative} strict-proxy branching. */
|
|
1283
1399
|
var dashboard_redis_health_1 = require("./utils/dashboard-redis-health");
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* 1. POST /mockifyer-save - Direct save endpoint (used by Hybrid Provider for instant sync)
|
|
6
6
|
* 2. GET /mockifyer-sync-to-device-manifest + /mockifyer-sync-to-device-file - Project → app (HybridProvider; avoids huge single JSON)
|
|
7
7
|
* 3. GET /mockifyer-sync-to-device - Legacy: all files in one response (may fail on large scenarios)
|
|
8
|
-
* 4. GET /mockifyer-
|
|
8
|
+
* 4. GET /mockifyer-pool-response?id= - Load a promoted pool response for RN `$pool` resolve
|
|
9
|
+
* 5. GET /mockifyer-sync - Legacy: iOS simulator mock-data → project folder
|
|
9
10
|
*
|
|
10
11
|
* The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
|
|
11
12
|
* Legacy polling-based sync is still available for backward compatibility.
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
* 1. POST /mockifyer-save - Direct save endpoint (used by Hybrid Provider for instant sync)
|
|
7
7
|
* 2. GET /mockifyer-sync-to-device-manifest + /mockifyer-sync-to-device-file - Project → app (HybridProvider; avoids huge single JSON)
|
|
8
8
|
* 3. GET /mockifyer-sync-to-device - Legacy: all files in one response (may fail on large scenarios)
|
|
9
|
-
* 4. GET /mockifyer-
|
|
9
|
+
* 4. GET /mockifyer-pool-response?id= - Load a promoted pool response for RN `$pool` resolve
|
|
10
|
+
* 5. GET /mockifyer-sync - Legacy: iOS simulator mock-data → project folder
|
|
10
11
|
*
|
|
11
12
|
* The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
|
|
12
13
|
* Legacy polling-based sync is still available for backward compatibility.
|
|
@@ -54,6 +55,7 @@ exports.syncFromIOSSimulator = syncFromIOSSimulator;
|
|
|
54
55
|
const fs = __importStar(require("fs"));
|
|
55
56
|
const path = __importStar(require("path"));
|
|
56
57
|
const mockifyer_core_1 = require("@sgedda/mockifyer-core");
|
|
58
|
+
const mockifyer_core_2 = require("@sgedda/mockifyer-core");
|
|
57
59
|
const DEFAULT_SCENARIO = 'default';
|
|
58
60
|
let autoSyncInterval = null;
|
|
59
61
|
function getMockFilePathLocal(mockData, dateStr) {
|
|
@@ -563,6 +565,34 @@ function buildSyncToDevicePayload(mockDataPath) {
|
|
|
563
565
|
return { success: false, error: error.message };
|
|
564
566
|
}
|
|
565
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* One pool response fixture for GET /mockifyer-pool-response?id=
|
|
570
|
+
* Used by React Native serve-time `$pool` resolution when Node fs is unavailable.
|
|
571
|
+
*/
|
|
572
|
+
function buildPoolResponsePayload(mockDataPath, rawId) {
|
|
573
|
+
const id = String(rawId || '').trim();
|
|
574
|
+
if (!id || !mockifyer_core_2.POOL_ID_PATTERN.test(id)) {
|
|
575
|
+
return { success: false, error: 'Invalid pool response id' };
|
|
576
|
+
}
|
|
577
|
+
try {
|
|
578
|
+
const item = (0, mockifyer_core_2.loadPoolResponseItem)(mockDataPath, id, {
|
|
579
|
+
joinPath: (...parts) => path.join(...parts),
|
|
580
|
+
existsSync: (p) => fs.existsSync(p),
|
|
581
|
+
readFileSync: (p, encoding) => fs.readFileSync(p, encoding),
|
|
582
|
+
writeFileSync: () => {
|
|
583
|
+
throw new Error('pool loader is read-only');
|
|
584
|
+
},
|
|
585
|
+
mkdirSync: () => undefined,
|
|
586
|
+
});
|
|
587
|
+
if (!item) {
|
|
588
|
+
return { success: false, error: 'Not found' };
|
|
589
|
+
}
|
|
590
|
+
return { success: true, item };
|
|
591
|
+
}
|
|
592
|
+
catch (error) {
|
|
593
|
+
return { success: false, error: error.message };
|
|
594
|
+
}
|
|
595
|
+
}
|
|
566
596
|
/**
|
|
567
597
|
* Normalize URL pathname from Node/Metro `req.url` so `/mockifyer-sync-to-device/` matches `/mockifyer-sync-to-device`.
|
|
568
598
|
* Without this, trailing slashes fall through to Expo Web's SPA and Expo Router treats them as AppDun routes.
|
|
@@ -639,6 +669,27 @@ function createMockSyncMiddleware(options) {
|
|
|
639
669
|
res.end(JSON.stringify(payload));
|
|
640
670
|
return;
|
|
641
671
|
}
|
|
672
|
+
// Promoted pool response for RN `$pool` serve-time resolve
|
|
673
|
+
if (url === '/mockifyer-pool-response' && req.method === 'GET') {
|
|
674
|
+
const fullUrl = req.url || '';
|
|
675
|
+
const qIndex = fullUrl.indexOf('?');
|
|
676
|
+
const query = qIndex >= 0 ? fullUrl.slice(qIndex + 1) : '';
|
|
677
|
+
const params = new URLSearchParams(query);
|
|
678
|
+
const idParam = params.get('id') || '';
|
|
679
|
+
const payload = buildPoolResponsePayload(mockDataPath, idParam);
|
|
680
|
+
res.setHeader('Content-Type', 'application/json');
|
|
681
|
+
if (payload.success) {
|
|
682
|
+
res.statusCode = 200;
|
|
683
|
+
}
|
|
684
|
+
else if (payload.error === 'Not found') {
|
|
685
|
+
res.statusCode = 404;
|
|
686
|
+
}
|
|
687
|
+
else {
|
|
688
|
+
res.statusCode = 400;
|
|
689
|
+
}
|
|
690
|
+
res.end(JSON.stringify(payload));
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
642
693
|
// Single file for HybridProvider (avoids multi‑MB single response)
|
|
643
694
|
if (url === '/mockifyer-sync-to-device-file' && req.method === 'GET') {
|
|
644
695
|
const fullUrl = req.url || '';
|
package/dist/react-native.d.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
import { setupMockifyer } from './index';
|
|
8
8
|
import { HTTPClient } from '@sgedda/mockifyer-core';
|
|
9
9
|
import { type MockifyerRuntimeMode } from '@sgedda/mockifyer-core';
|
|
10
|
+
export { setupMockifyer } from './index';
|
|
11
|
+
export * from '@sgedda/mockifyer-core';
|
|
10
12
|
export interface MockifyerInstance extends HTTPClient {
|
|
11
13
|
reloadMockData: (syncFromProject?: boolean) => Promise<void>;
|
|
12
14
|
clearStaleCacheEntries: () => number;
|
package/dist/react-native.js
CHANGED
|
@@ -5,8 +5,22 @@
|
|
|
5
5
|
* Simplified setup for React Native applications with automatic
|
|
6
6
|
* conditional provider selection (FileSystem in dev, Memory in prod)
|
|
7
7
|
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
20
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
|
+
};
|
|
8
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.clearMockifyerClientIdRuntime = exports.registerMockifyerInstance = exports.setClientId = exports.getClientId = exports.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY = exports.tryGetClientIdFromLaunchArguments = void 0;
|
|
23
|
+
exports.clearMockifyerClientIdRuntime = exports.registerMockifyerInstance = exports.setClientId = exports.getClientId = exports.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY = exports.tryGetClientIdFromLaunchArguments = exports.setupMockifyer = void 0;
|
|
10
24
|
exports.isMockifyerReactNativeActive = isMockifyerReactNativeActive;
|
|
11
25
|
exports.setupMockifyerForReactNative = setupMockifyerForReactNative;
|
|
12
26
|
exports.initMockifyerForReactNativeDashboard = initMockifyerForReactNativeDashboard;
|
|
@@ -14,6 +28,11 @@ const dashboard_redis_health_1 = require("./utils/dashboard-redis-health");
|
|
|
14
28
|
const index_1 = require("./index");
|
|
15
29
|
const mockifyer_core_1 = require("@sgedda/mockifyer-core");
|
|
16
30
|
const mockifyer_core_2 = require("@sgedda/mockifyer-core");
|
|
31
|
+
// React Native package-root resolution points at this entry. Keep the root
|
|
32
|
+
// public API available for apps that import from '@sgedda/mockifyer-fetch'.
|
|
33
|
+
var index_2 = require("./index");
|
|
34
|
+
Object.defineProperty(exports, "setupMockifyer", { enumerable: true, get: function () { return index_2.setupMockifyer; } });
|
|
35
|
+
__exportStar(require("@sgedda/mockifyer-core"), exports);
|
|
17
36
|
/**
|
|
18
37
|
* Type guard: `true` when {@link setupMockifyerForReactNative} patched `fetch` and returned an instance.
|
|
19
38
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.39",
|
|
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,16 +49,24 @@
|
|
|
49
49
|
},
|
|
50
50
|
"license": "MIT",
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@sgedda/mockifyer-core": "^1.10.
|
|
52
|
+
"@sgedda/mockifyer-core": "^1.10.15"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^20.11.24",
|
|
56
56
|
"typescript": "^5.3.3"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
+
"@sgedda/mockifyer-axios": "*",
|
|
60
|
+
"axios": "^1.6.0 || ^1.7.0",
|
|
59
61
|
"react-native-launch-arguments": ">=4.0.0"
|
|
60
62
|
},
|
|
61
63
|
"peerDependenciesMeta": {
|
|
64
|
+
"@sgedda/mockifyer-axios": {
|
|
65
|
+
"optional": true
|
|
66
|
+
},
|
|
67
|
+
"axios": {
|
|
68
|
+
"optional": true
|
|
69
|
+
},
|
|
62
70
|
"react-native-launch-arguments": {
|
|
63
71
|
"optional": true
|
|
64
72
|
}
|
package/src/core-proxy.ts
CHANGED
|
@@ -6,7 +6,16 @@ export { mirrorProxyRecordingToClient } from '@sgedda/mockifyer-core/utils/mirro
|
|
|
6
6
|
export {
|
|
7
7
|
initMockifyerForDashboardProxy,
|
|
8
8
|
initMockifyerForLocalFilesystem,
|
|
9
|
+
initMockifyerForDashboardProxyClients,
|
|
10
|
+
initMockifyerForLocalFilesystemClients,
|
|
11
|
+
resolveClientInitFlags,
|
|
12
|
+
needsSiblingClientSetup,
|
|
13
|
+
syncDualMockifyerControls,
|
|
14
|
+
pickPrimaryDualMockifyerInstance,
|
|
15
|
+
loadAxiosSetupMockifyer,
|
|
16
|
+
loadFetchSetupMockifyer,
|
|
9
17
|
type InitMockifyerForDashboardProxyOptions,
|
|
10
18
|
type InitMockifyerForLocalFilesystemOptions,
|
|
19
|
+
type DualMockifyerControlSurface,
|
|
11
20
|
} from '@sgedda/mockifyer-core/utils/init-mockifyer-presets';
|
|
12
21
|
export { performDashboardProxyRequest } from '@sgedda/mockifyer-core/utils/perform-dashboard-proxy-request';
|
package/src/index.ts
CHANGED
|
@@ -35,6 +35,12 @@ import {
|
|
|
35
35
|
checkRequestLimit,
|
|
36
36
|
prepareMockResponseBody,
|
|
37
37
|
getCurrentDate,
|
|
38
|
+
createServeTimePoolResponseLoader,
|
|
39
|
+
collectPoolRefIds,
|
|
40
|
+
arePoolRefsEnabled,
|
|
41
|
+
containsPoolRefs,
|
|
42
|
+
isUsableNodeLikePoolFs,
|
|
43
|
+
type PoolResponseItem,
|
|
38
44
|
shouldBypassMockifyerForUrl,
|
|
39
45
|
resolveRecordingExclusions,
|
|
40
46
|
shouldExcludeRecording,
|
|
@@ -73,6 +79,12 @@ import {
|
|
|
73
79
|
mirrorProxyRecordingToClient,
|
|
74
80
|
initMockifyerForDashboardProxy as coreInitMockifyerForDashboardProxy,
|
|
75
81
|
initMockifyerForLocalFilesystem as coreInitMockifyerForLocalFilesystem,
|
|
82
|
+
initMockifyerForDashboardProxyClients,
|
|
83
|
+
initMockifyerForLocalFilesystemClients,
|
|
84
|
+
resolveClientInitFlags,
|
|
85
|
+
needsSiblingClientSetup,
|
|
86
|
+
pickPrimaryDualMockifyerInstance,
|
|
87
|
+
loadAxiosSetupMockifyer,
|
|
76
88
|
type InitMockifyerForDashboardProxyOptions,
|
|
77
89
|
type InitMockifyerForLocalFilesystemOptions,
|
|
78
90
|
} from './core-proxy';
|
|
@@ -93,6 +105,11 @@ class MockifyerClass {
|
|
|
93
105
|
private readonly SESSION_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
|
|
94
106
|
private testGenerator?: TestGenerator;
|
|
95
107
|
private readonly activationMode: MockifyerActivationMode;
|
|
108
|
+
/**
|
|
109
|
+
* Promoted pool responses for serve-time `$pool` resolve when Node `fs` is
|
|
110
|
+
* unavailable (React Native / Metro stubs). Seeded via {@link warmPoolResponseCache}.
|
|
111
|
+
*/
|
|
112
|
+
private readonly poolResponseCache = new Map<string, PoolResponseItem>();
|
|
96
113
|
|
|
97
114
|
/** Best-effort dashboard network log (skipped when traffic goes through `proxy.baseUrl`). */
|
|
98
115
|
private logNetworkEvent(
|
|
@@ -130,6 +147,100 @@ class MockifyerClass {
|
|
|
130
147
|
return parentRequestId ? { requestId, parentRequestId } : { requestId };
|
|
131
148
|
}
|
|
132
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Serve stored mock body with optional `$pool` resolution from the local fixture pool.
|
|
152
|
+
* Always passes `loadPoolResponse` — including React Native where Node `fs`/`path` are
|
|
153
|
+
* missing or Metro-stubbed — so `$pool` refs resolve via disk and/or {@link poolResponseCache}.
|
|
154
|
+
*/
|
|
155
|
+
private prepareStoredResponseBody(mockData: MockData): unknown {
|
|
156
|
+
const joinPath =
|
|
157
|
+
path && typeof path.join === 'function' ? path.join.bind(path) : null;
|
|
158
|
+
return prepareMockResponseBody(mockData, getCurrentDate, {
|
|
159
|
+
loadPoolResponse: createServeTimePoolResponseLoader({
|
|
160
|
+
mockDataPath: this.config.mockDataPath,
|
|
161
|
+
nodeFs: isUsableNodeLikePoolFs(fs) ? fs : null,
|
|
162
|
+
joinPath,
|
|
163
|
+
cache: this.poolResponseCache,
|
|
164
|
+
}),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Ensure `$pool` fixtures are available in {@link poolResponseCache} when Node fs
|
|
170
|
+
* cannot load them (React Native). Tries database provider, then Metro.
|
|
171
|
+
*/
|
|
172
|
+
private async warmPoolResponseCache(mockData: MockData): Promise<void> {
|
|
173
|
+
if (!arePoolRefsEnabled() || !containsPoolRefs(mockData.response?.data)) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (isUsableNodeLikePoolFs(fs)) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const ids = collectPoolRefIds(mockData.response.data);
|
|
181
|
+
for (const id of ids) {
|
|
182
|
+
if (this.poolResponseCache.has(id)) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const fromProvider = await this.loadPoolResponseFromProvider(id);
|
|
186
|
+
if (fromProvider) {
|
|
187
|
+
this.poolResponseCache.set(id, fromProvider);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const fromMetro = await this.loadPoolResponseFromMetro(id);
|
|
191
|
+
if (fromMetro) {
|
|
192
|
+
this.poolResponseCache.set(id, fromMetro);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private async loadPoolResponseFromProvider(
|
|
198
|
+
id: string
|
|
199
|
+
): Promise<PoolResponseItem | undefined> {
|
|
200
|
+
const provider = this.databaseProvider as
|
|
201
|
+
| { loadPoolResponseItem?: (poolId: string) => Promise<PoolResponseItem | undefined> | PoolResponseItem | undefined }
|
|
202
|
+
| undefined;
|
|
203
|
+
if (!provider || typeof provider.loadPoolResponseItem !== 'function') {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
return (await provider.loadPoolResponseItem(id)) ?? undefined;
|
|
208
|
+
} catch (error) {
|
|
209
|
+
logger.warn(`[Mockifyer-Fetch] Failed to load pool response "${id}" from provider:`, error);
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private async loadPoolResponseFromMetro(
|
|
215
|
+
id: string
|
|
216
|
+
): Promise<PoolResponseItem | undefined> {
|
|
217
|
+
const metroPort = this.config.databaseProvider?.options?.metroPort ?? 8081;
|
|
218
|
+
const originalFetch =
|
|
219
|
+
(global as { __mockifyer_original_fetch?: typeof fetch }).__mockifyer_original_fetch ||
|
|
220
|
+
(typeof fetch !== 'undefined' ? fetch : undefined);
|
|
221
|
+
if (!originalFetch) {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
const url = `http://localhost:${metroPort}/mockifyer-pool-response?id=${encodeURIComponent(id)}`;
|
|
226
|
+
const response = await originalFetch(url);
|
|
227
|
+
if (!response.ok) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
const payload = (await response.json()) as {
|
|
231
|
+
success?: boolean;
|
|
232
|
+
item?: PoolResponseItem;
|
|
233
|
+
};
|
|
234
|
+
if (payload.success && payload.item?.responseItemId && payload.item.response) {
|
|
235
|
+
return payload.item;
|
|
236
|
+
}
|
|
237
|
+
return undefined;
|
|
238
|
+
} catch (error) {
|
|
239
|
+
logger.warn(`[Mockifyer-Fetch] Failed to load pool response "${id}" from Metro:`, error);
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
133
244
|
constructor(config: MockifyerConfig) {
|
|
134
245
|
// Validate database provider - filesystem, expo-filesystem, hybrid, and memory are supported
|
|
135
246
|
if (config.databaseProvider && config.databaseProvider.type) {
|
|
@@ -614,8 +725,10 @@ class MockifyerClass {
|
|
|
614
725
|
'x-mockifyer-filepath': filePath
|
|
615
726
|
};
|
|
616
727
|
|
|
728
|
+
await this.warmPoolResponseCache(mockData);
|
|
729
|
+
|
|
617
730
|
const mockResponse = {
|
|
618
|
-
data:
|
|
731
|
+
data: this.prepareStoredResponseBody(mockData),
|
|
619
732
|
status: mockData.response.status,
|
|
620
733
|
statusText: 'OK',
|
|
621
734
|
headers: responseHeaders,
|
|
@@ -1548,6 +1661,8 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1548
1661
|
throw error;
|
|
1549
1662
|
}
|
|
1550
1663
|
} as typeof fetch;
|
|
1664
|
+
|
|
1665
|
+
logger.info('[Mockifyer] ✅ Patched global fetch (useGlobalFetch: true)');
|
|
1551
1666
|
}
|
|
1552
1667
|
|
|
1553
1668
|
const extendedClient = httpClient as MockifyerInstance;
|
|
@@ -1567,27 +1682,70 @@ export type { InitMockifyerForDashboardProxyOptions, InitMockifyerForLocalFilesy
|
|
|
1567
1682
|
|
|
1568
1683
|
/**
|
|
1569
1684
|
* Preset: dashboard central store proxy when health check passes; otherwise filesystem mocks.
|
|
1685
|
+
* Set both `useGlobalFetch` and `useGlobalAxios` to patch fetch and axios in one call
|
|
1686
|
+
* (requires `@sgedda/mockifyer-axios` when axios is enabled).
|
|
1570
1687
|
* For full control, use {@link setupMockifyer} directly.
|
|
1571
1688
|
*/
|
|
1572
1689
|
export async function initMockifyerForDashboardProxy(
|
|
1573
1690
|
options: InitMockifyerForDashboardProxyOptions
|
|
1574
1691
|
): Promise<MockifyerInstance> {
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1692
|
+
const flags = resolveClientInitFlags(options, {
|
|
1693
|
+
useGlobalFetch: true,
|
|
1694
|
+
useGlobalAxios: false,
|
|
1695
|
+
});
|
|
1696
|
+
|
|
1697
|
+
if (!needsSiblingClientSetup('fetch', flags)) {
|
|
1698
|
+
return coreInitMockifyerForDashboardProxy(
|
|
1699
|
+
{ ...options, useGlobalFetch: flags.useFetch, useGlobalAxios: false },
|
|
1700
|
+
setupMockifyer
|
|
1701
|
+
);
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
const result = await initMockifyerForDashboardProxyClients(
|
|
1705
|
+
options,
|
|
1706
|
+
{
|
|
1707
|
+
...(flags.useFetch ? { fetch: setupMockifyer } : {}),
|
|
1708
|
+
...(flags.useAxios
|
|
1709
|
+
? { axios: loadAxiosSetupMockifyer() as typeof setupMockifyer }
|
|
1710
|
+
: {}),
|
|
1711
|
+
},
|
|
1712
|
+
flags
|
|
1578
1713
|
);
|
|
1714
|
+
|
|
1715
|
+
return pickPrimaryDualMockifyerInstance('fetch', flags, result);
|
|
1579
1716
|
}
|
|
1580
1717
|
|
|
1581
1718
|
/**
|
|
1582
1719
|
* Preset: local filesystem mocks. No dashboard proxy.
|
|
1720
|
+
* Set both `useGlobalFetch` and `useGlobalAxios` to patch both clients in one call.
|
|
1583
1721
|
*/
|
|
1584
1722
|
export function initMockifyerForLocalFilesystem(
|
|
1585
1723
|
options: InitMockifyerForLocalFilesystemOptions = {}
|
|
1586
1724
|
): MockifyerInstance {
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1725
|
+
const flags = resolveClientInitFlags(options, {
|
|
1726
|
+
useGlobalFetch: true,
|
|
1727
|
+
useGlobalAxios: false,
|
|
1728
|
+
});
|
|
1729
|
+
|
|
1730
|
+
if (!needsSiblingClientSetup('fetch', flags)) {
|
|
1731
|
+
return coreInitMockifyerForLocalFilesystem(
|
|
1732
|
+
{ ...options, useGlobalFetch: flags.useFetch, useGlobalAxios: false },
|
|
1733
|
+
setupMockifyer
|
|
1734
|
+
);
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
const result = initMockifyerForLocalFilesystemClients(
|
|
1738
|
+
options,
|
|
1739
|
+
{
|
|
1740
|
+
...(flags.useFetch ? { fetch: setupMockifyer } : {}),
|
|
1741
|
+
...(flags.useAxios
|
|
1742
|
+
? { axios: loadAxiosSetupMockifyer() as typeof setupMockifyer }
|
|
1743
|
+
: {}),
|
|
1744
|
+
},
|
|
1745
|
+
flags
|
|
1590
1746
|
);
|
|
1747
|
+
|
|
1748
|
+
return pickPrimaryDualMockifyerInstance('fetch', flags, result);
|
|
1591
1749
|
}
|
|
1592
1750
|
|
|
1593
1751
|
/** Shared with {@link setupMockifyerForReactNative} strict-proxy branching. */
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* 1. POST /mockifyer-save - Direct save endpoint (used by Hybrid Provider for instant sync)
|
|
6
6
|
* 2. GET /mockifyer-sync-to-device-manifest + /mockifyer-sync-to-device-file - Project → app (HybridProvider; avoids huge single JSON)
|
|
7
7
|
* 3. GET /mockifyer-sync-to-device - Legacy: all files in one response (may fail on large scenarios)
|
|
8
|
-
* 4. GET /mockifyer-
|
|
8
|
+
* 4. GET /mockifyer-pool-response?id= - Load a promoted pool response for RN `$pool` resolve
|
|
9
|
+
* 5. GET /mockifyer-sync - Legacy: iOS simulator mock-data → project folder
|
|
9
10
|
*
|
|
10
11
|
* The Hybrid Provider (recommended) uses POST /mockifyer-save for instant file sync.
|
|
11
12
|
* Legacy polling-based sync is still available for backward compatibility.
|
|
@@ -18,6 +19,11 @@ import * as path from 'path';
|
|
|
18
19
|
import { execSync } from 'child_process';
|
|
19
20
|
import { MockData, TestGenerator, TestGenerationOptions } from '@sgedda/mockifyer-core';
|
|
20
21
|
import { logger } from '@sgedda/mockifyer-core';
|
|
22
|
+
import {
|
|
23
|
+
POOL_ID_PATTERN,
|
|
24
|
+
loadPoolResponseItem,
|
|
25
|
+
type PoolResponseItem,
|
|
26
|
+
} from '@sgedda/mockifyer-core';
|
|
21
27
|
|
|
22
28
|
export interface MetroSyncMiddlewareOptions {
|
|
23
29
|
/** Project root directory (default: process.cwd()) */
|
|
@@ -644,6 +650,37 @@ function buildSyncToDevicePayload(mockDataPath: string): {
|
|
|
644
650
|
}
|
|
645
651
|
}
|
|
646
652
|
|
|
653
|
+
/**
|
|
654
|
+
* One pool response fixture for GET /mockifyer-pool-response?id=
|
|
655
|
+
* Used by React Native serve-time `$pool` resolution when Node fs is unavailable.
|
|
656
|
+
*/
|
|
657
|
+
function buildPoolResponsePayload(
|
|
658
|
+
mockDataPath: string,
|
|
659
|
+
rawId: string
|
|
660
|
+
): { success: boolean; item?: PoolResponseItem; error?: string } {
|
|
661
|
+
const id = String(rawId || '').trim();
|
|
662
|
+
if (!id || !POOL_ID_PATTERN.test(id)) {
|
|
663
|
+
return { success: false, error: 'Invalid pool response id' };
|
|
664
|
+
}
|
|
665
|
+
try {
|
|
666
|
+
const item = loadPoolResponseItem(mockDataPath, id, {
|
|
667
|
+
joinPath: (...parts) => path.join(...parts),
|
|
668
|
+
existsSync: (p) => fs.existsSync(p),
|
|
669
|
+
readFileSync: (p, encoding) => fs.readFileSync(p, encoding),
|
|
670
|
+
writeFileSync: () => {
|
|
671
|
+
throw new Error('pool loader is read-only');
|
|
672
|
+
},
|
|
673
|
+
mkdirSync: () => undefined,
|
|
674
|
+
});
|
|
675
|
+
if (!item) {
|
|
676
|
+
return { success: false, error: 'Not found' };
|
|
677
|
+
}
|
|
678
|
+
return { success: true, item };
|
|
679
|
+
} catch (error) {
|
|
680
|
+
return { success: false, error: (error as Error).message };
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
647
684
|
/**
|
|
648
685
|
* Normalize URL pathname from Node/Metro `req.url` so `/mockifyer-sync-to-device/` matches `/mockifyer-sync-to-device`.
|
|
649
686
|
* Without this, trailing slashes fall through to Expo Web's SPA and Expo Router treats them as AppDun routes.
|
|
@@ -738,6 +775,26 @@ export function createMockSyncMiddleware(options?: MetroSyncMiddlewareOptions) {
|
|
|
738
775
|
return;
|
|
739
776
|
}
|
|
740
777
|
|
|
778
|
+
// Promoted pool response for RN `$pool` serve-time resolve
|
|
779
|
+
if (url === '/mockifyer-pool-response' && req.method === 'GET') {
|
|
780
|
+
const fullUrl = req.url || '';
|
|
781
|
+
const qIndex = fullUrl.indexOf('?');
|
|
782
|
+
const query = qIndex >= 0 ? fullUrl.slice(qIndex + 1) : '';
|
|
783
|
+
const params = new URLSearchParams(query);
|
|
784
|
+
const idParam = params.get('id') || '';
|
|
785
|
+
const payload = buildPoolResponsePayload(mockDataPath, idParam);
|
|
786
|
+
res.setHeader('Content-Type', 'application/json');
|
|
787
|
+
if (payload.success) {
|
|
788
|
+
res.statusCode = 200;
|
|
789
|
+
} else if (payload.error === 'Not found') {
|
|
790
|
+
res.statusCode = 404;
|
|
791
|
+
} else {
|
|
792
|
+
res.statusCode = 400;
|
|
793
|
+
}
|
|
794
|
+
res.end(JSON.stringify(payload));
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
|
|
741
798
|
// Single file for HybridProvider (avoids multi‑MB single response)
|
|
742
799
|
if (url === '/mockifyer-sync-to-device-file' && req.method === 'GET') {
|
|
743
800
|
const fullUrl = req.url || '';
|
package/src/react-native.ts
CHANGED
|
@@ -19,6 +19,11 @@ import {
|
|
|
19
19
|
type MockifyerRuntimeMode,
|
|
20
20
|
} from '@sgedda/mockifyer-core';
|
|
21
21
|
|
|
22
|
+
// React Native package-root resolution points at this entry. Keep the root
|
|
23
|
+
// public API available for apps that import from '@sgedda/mockifyer-fetch'.
|
|
24
|
+
export { setupMockifyer } from './index';
|
|
25
|
+
export * from '@sgedda/mockifyer-core';
|
|
26
|
+
|
|
22
27
|
// Re-export MockifyerInstance type to avoid circular dependency
|
|
23
28
|
export interface MockifyerInstance extends HTTPClient {
|
|
24
29
|
reloadMockData: (syncFromProject?: boolean) => Promise<void>;
|