@sgedda/mockifyer-fetch 1.7.0 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +27 -174
- package/dist/metro-config.d.ts +89 -8
- package/dist/metro-config.js +126 -8
- package/dist/metro-sync-middleware.d.ts +4 -2
- package/dist/metro-sync-middleware.js +292 -36
- package/dist/react-native.d.ts +1 -1
- package/dist/react-native.js +16 -7
- package/package.json +8 -3
- package/src/index.ts +33 -179
- package/src/metro-config.ts +153 -9
- package/src/metro-sync-middleware.ts +319 -41
- package/src/react-native.ts +19 -8
package/src/metro-config.ts
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This helper configures Metro to stub Node.js built-in modules that aren't
|
|
5
5
|
* available in React Native, allowing Mockifyer to bundle successfully.
|
|
6
|
+
* Optionally sets up sync middleware for Hybrid Provider.
|
|
6
7
|
*
|
|
7
|
-
* @example
|
|
8
|
+
* @example Basic usage (fs stubbing only):
|
|
8
9
|
* ```javascript
|
|
9
10
|
* const { getDefaultConfig } = require('expo/metro-config');
|
|
10
11
|
* const { configureMetroForMockifyer } = require('@sgedda/mockifyer-fetch/metro-config');
|
|
@@ -12,8 +13,41 @@
|
|
|
12
13
|
* const config = getDefaultConfig(__dirname);
|
|
13
14
|
* module.exports = configureMetroForMockifyer(config);
|
|
14
15
|
* ```
|
|
16
|
+
*
|
|
17
|
+
* @example With sync middleware (for Hybrid Provider):
|
|
18
|
+
* ```javascript
|
|
19
|
+
* const { getDefaultConfig } = require('expo/metro-config');
|
|
20
|
+
* const { configureMetroForMockifyer } = require('@sgedda/mockifyer-fetch/metro-config');
|
|
21
|
+
*
|
|
22
|
+
* const config = getDefaultConfig(__dirname);
|
|
23
|
+
* module.exports = configureMetroForMockifyer(config, {
|
|
24
|
+
* syncMiddleware: {
|
|
25
|
+
* projectRoot: __dirname,
|
|
26
|
+
* mockDataPath: './mock-data',
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example With auto-sync (for ExpoFileSystem Provider - syncs files from device to project folder):
|
|
32
|
+
* ```javascript
|
|
33
|
+
* const { getDefaultConfig } = require('expo/metro-config');
|
|
34
|
+
* const { configureMetroForMockifyer } = require('@sgedda/mockifyer-fetch/metro-config');
|
|
35
|
+
*
|
|
36
|
+
* const config = getDefaultConfig(__dirname);
|
|
37
|
+
* module.exports = configureMetroForMockifyer(config, {
|
|
38
|
+
* autoSync: {
|
|
39
|
+
* enabled: true,
|
|
40
|
+
* intervalMs: 5000,
|
|
41
|
+
* projectRoot: __dirname,
|
|
42
|
+
* mockDataPath: './mock-data',
|
|
43
|
+
* },
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
15
46
|
*/
|
|
16
47
|
|
|
48
|
+
import type { MetroSyncMiddlewareOptions } from './metro-sync-middleware';
|
|
49
|
+
import { logger } from '@sgedda/mockifyer-core';
|
|
50
|
+
|
|
17
51
|
// Metro config types (simplified - Metro doesn't export types)
|
|
18
52
|
export interface MetroConfig {
|
|
19
53
|
resolver?: {
|
|
@@ -40,18 +74,38 @@ export interface MetroConfig {
|
|
|
40
74
|
*/
|
|
41
75
|
const NODE_BUILTINS = ['fs', 'path', 'assert', 'util'] as const;
|
|
42
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Options for configuring Metro for Mockifyer
|
|
79
|
+
*/
|
|
80
|
+
export interface ConfigureMetroOptions {
|
|
81
|
+
/** Sync middleware options - if provided, sets up Hybrid Provider sync */
|
|
82
|
+
syncMiddleware?: MetroSyncMiddlewareOptions;
|
|
83
|
+
/** Auto-sync options - Required when using ExpoFileSystem Provider to sync files from device to project folder for code search. Not needed with Hybrid Provider (uses instant HTTP sync). */
|
|
84
|
+
autoSync?: {
|
|
85
|
+
/** Enable auto-sync (default: false). Set to true if using ExpoFileSystem Provider and want files in project folder for code search */
|
|
86
|
+
enabled?: boolean;
|
|
87
|
+
/** Sync interval in milliseconds (default: 5000) */
|
|
88
|
+
intervalMs?: number;
|
|
89
|
+
/** Project root directory (default: process.cwd()) */
|
|
90
|
+
projectRoot?: string;
|
|
91
|
+
/** Mock data path (default: 'mock-data') */
|
|
92
|
+
mockDataPath?: string;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
43
96
|
/**
|
|
44
97
|
* Configure Metro bundler for Mockifyer
|
|
45
98
|
*
|
|
46
|
-
* This function
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
99
|
+
* This function:
|
|
100
|
+
* 1. Adds resolver configuration to stub Node.js built-in modules (fs, path, assert, util)
|
|
101
|
+
* 2. Optionally sets up sync middleware for Hybrid Provider (if syncMiddleware options provided)
|
|
102
|
+
* 3. Optionally sets up auto-sync polling (if autoSync options provided - needed for ExpoFileSystem Provider, not needed with Hybrid Provider)
|
|
50
103
|
*
|
|
51
104
|
* @param config - Existing Metro config (e.g., from getDefaultConfig)
|
|
52
|
-
* @
|
|
105
|
+
* @param options - Optional configuration including sync middleware setup
|
|
106
|
+
* @returns Metro config with Mockifyer configuration merged in
|
|
53
107
|
*
|
|
54
|
-
* @example
|
|
108
|
+
* @example Basic usage (fs stubbing only):
|
|
55
109
|
* ```javascript
|
|
56
110
|
* const { getDefaultConfig } = require('expo/metro-config');
|
|
57
111
|
* const { configureMetroForMockifyer } = require('@sgedda/mockifyer-fetch/metro-config');
|
|
@@ -59,8 +113,41 @@ const NODE_BUILTINS = ['fs', 'path', 'assert', 'util'] as const;
|
|
|
59
113
|
* const config = getDefaultConfig(__dirname);
|
|
60
114
|
* module.exports = configureMetroForMockifyer(config);
|
|
61
115
|
* ```
|
|
116
|
+
*
|
|
117
|
+
* @example With sync middleware (for Hybrid Provider):
|
|
118
|
+
* ```javascript
|
|
119
|
+
* const { getDefaultConfig } = require('expo/metro-config');
|
|
120
|
+
* const { configureMetroForMockifyer } = require('@sgedda/mockifyer-fetch/metro-config');
|
|
121
|
+
*
|
|
122
|
+
* const config = getDefaultConfig(__dirname);
|
|
123
|
+
* module.exports = configureMetroForMockifyer(config, {
|
|
124
|
+
* syncMiddleware: {
|
|
125
|
+
* projectRoot: __dirname,
|
|
126
|
+
* mockDataPath: './mock-data',
|
|
127
|
+
* },
|
|
128
|
+
* });
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @example With auto-sync (for ExpoFileSystem Provider - syncs files from device to project folder):
|
|
132
|
+
* ```javascript
|
|
133
|
+
* const { getDefaultConfig } = require('expo/metro-config');
|
|
134
|
+
* const { configureMetroForMockifyer } = require('@sgedda/mockifyer-fetch/metro-config');
|
|
135
|
+
*
|
|
136
|
+
* const config = getDefaultConfig(__dirname);
|
|
137
|
+
* module.exports = configureMetroForMockifyer(config, {
|
|
138
|
+
* autoSync: {
|
|
139
|
+
* enabled: true,
|
|
140
|
+
* intervalMs: 5000,
|
|
141
|
+
* projectRoot: __dirname,
|
|
142
|
+
* mockDataPath: './mock-data',
|
|
143
|
+
* },
|
|
144
|
+
* });
|
|
145
|
+
* ```
|
|
62
146
|
*/
|
|
63
|
-
export function configureMetroForMockifyer(
|
|
147
|
+
export function configureMetroForMockifyer(
|
|
148
|
+
config: MetroConfig,
|
|
149
|
+
options?: ConfigureMetroOptions
|
|
150
|
+
): MetroConfig {
|
|
64
151
|
// Get path to empty module stub
|
|
65
152
|
// Resolve using the package export path, which points to metro-polyfills/empty-module.js
|
|
66
153
|
let emptyModulePath: string;
|
|
@@ -103,6 +190,64 @@ export function configureMetroForMockifyer(config: MetroConfig): MetroConfig {
|
|
|
103
190
|
return context.resolveRequest(context, moduleName, platform);
|
|
104
191
|
};
|
|
105
192
|
|
|
193
|
+
// Set up sync middleware if options provided
|
|
194
|
+
if (options?.syncMiddleware) {
|
|
195
|
+
try {
|
|
196
|
+
const { createMockSyncMiddleware } = require('./metro-sync-middleware');
|
|
197
|
+
const syncMiddleware = createMockSyncMiddleware({
|
|
198
|
+
projectRoot: options.syncMiddleware.projectRoot || process.cwd(),
|
|
199
|
+
mockDataPath: options.syncMiddleware.mockDataPath || 'mock-data',
|
|
200
|
+
testGeneration: options.syncMiddleware.testGeneration,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Add middleware to server config
|
|
204
|
+
// Expo Metro uses enhanceMiddleware, but also supports middleware array
|
|
205
|
+
config.server = config.server || {};
|
|
206
|
+
|
|
207
|
+
// Use enhanceMiddleware if available (Expo Metro standard)
|
|
208
|
+
// enhanceMiddleware receives existing middleware and returns new middleware
|
|
209
|
+
const existingEnhance = config.server.enhanceMiddleware;
|
|
210
|
+
config.server.enhanceMiddleware = (middleware: any) => {
|
|
211
|
+
// Return a middleware function that runs our syncMiddleware first,
|
|
212
|
+
// then calls the existing enhanced middleware
|
|
213
|
+
return (req: any, res: any, next: any) => {
|
|
214
|
+
// Try our middleware first (it calls next() if not handling the request)
|
|
215
|
+
syncMiddleware(req, res, (err?: any) => {
|
|
216
|
+
if (err) {
|
|
217
|
+
return next(err);
|
|
218
|
+
}
|
|
219
|
+
// If our middleware didn't handle it, call the existing middleware
|
|
220
|
+
if (!res.headersSent) {
|
|
221
|
+
return middleware(req, res, next);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
} catch (e) {
|
|
227
|
+
logger.warn('[Metro] Could not set up Mockifyer sync middleware:', e);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Set up auto-sync (polling-based sync from iOS Simulator to project folder) if options provided
|
|
232
|
+
// Required when using ExpoFileSystem Provider to sync files from device to project folder for code search
|
|
233
|
+
// Not needed with Hybrid Provider (uses instant HTTP sync via syncMiddleware)
|
|
234
|
+
if (options?.autoSync?.enabled) {
|
|
235
|
+
try {
|
|
236
|
+
const { startAutoSync } = require('./metro-sync-middleware');
|
|
237
|
+
const intervalMs = options.autoSync.intervalMs || 5000;
|
|
238
|
+
const projectRoot = options.autoSync.projectRoot || process.cwd();
|
|
239
|
+
const mockDataPath = options.autoSync.mockDataPath || 'mock-data';
|
|
240
|
+
|
|
241
|
+
startAutoSync(intervalMs, {
|
|
242
|
+
projectRoot,
|
|
243
|
+
mockDataPath,
|
|
244
|
+
});
|
|
245
|
+
console.log(`[Metro] ✅ Auto-sync enabled: polling every ${intervalMs}ms`);
|
|
246
|
+
} catch (e) {
|
|
247
|
+
logger.warn('[Metro] Could not set up Mockifyer auto-sync:', e);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
106
251
|
// Merge config with new resolver
|
|
107
252
|
return {
|
|
108
253
|
...config,
|
|
@@ -112,4 +257,3 @@ export function configureMetroForMockifyer(config: MetroConfig): MetroConfig {
|
|
|
112
257
|
},
|
|
113
258
|
};
|
|
114
259
|
}
|
|
115
|
-
|