@sgedda/mockifyer-fetch 1.7.2 → 1.8.2

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.
@@ -1,45 +0,0 @@
1
- /**
2
- * React Native/Expo Helper Functions
3
- *
4
- * Simplified setup for React Native applications with automatic
5
- * conditional provider selection (FileSystem in dev, Memory in prod)
6
- */
7
- import { setupMockifyer } from './index';
8
- import { HTTPClient } from '@sgedda/mockifyer-core';
9
- export interface MockifyerInstance extends HTTPClient {
10
- reloadMockData: () => void;
11
- clearStaleCacheEntries: () => number;
12
- clearAllMocks: () => Promise<void>;
13
- }
14
- export interface ReactNativeMockifyerConfig {
15
- /** Whether we're in development mode (determines which provider to use) */
16
- isDev: boolean;
17
- /** Path to mock data directory (used in development) */
18
- mockDataPath?: string;
19
- /** Path to bundled mock data file (used in production) */
20
- bundledDataPath?: string;
21
- /** Enable recording mode (development only) */
22
- recordMode?: boolean;
23
- /** Additional Mockifyer config options */
24
- config?: Partial<Parameters<typeof setupMockifyer>[0]>;
25
- }
26
- /**
27
- * Setup Mockifyer for React Native/Expo with automatic conditional provider selection
28
- *
29
- * - Development (isDev === true): Uses Expo FileSystem provider - can record mocks
30
- * - Production (isDev === false): Uses Memory provider with bundled TypeScript file
31
- *
32
- * @example
33
- * ```typescript
34
- * import { setupMockifyerForReactNative } from '@sgedda/mockifyer-fetch/react-native';
35
- *
36
- * await setupMockifyerForReactNative({
37
- * isDev: __DEV__, // Pass React Native's __DEV__ variable
38
- * mockDataPath: 'mock-data',
39
- * bundledDataPath: './assets/mock-data',
40
- * recordMode: process.env.MOCKIFYER_RECORD === 'true',
41
- * });
42
- * ```
43
- */
44
- export declare function setupMockifyerForReactNative(options: ReactNativeMockifyerConfig): Promise<MockifyerInstance | null>;
45
- //# sourceMappingURL=react-native.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-native.d.ts","sourceRoot":"","sources":["react-native.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAoD,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAKtG,MAAM,WAAW,iBAAkB,SAAQ,UAAU;IACnD,cAAc,EAAE,MAAM,IAAI,CAAC;IAC3B,sBAAsB,EAAE,MAAM,MAAM,CAAC;IACrC,aAAa,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,0BAA0B;IACzC,2EAA2E;IAC3E,KAAK,EAAE,OAAO,CAAC;IACf,wDAAwD;IACxD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,0DAA0D;IAC1D,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,+CAA+C;IAC/C,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,0CAA0C;IAC1C,MAAM,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACxD;AAyCD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,0BAA0B,GAClC,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CA4FnC"}
@@ -1,141 +0,0 @@
1
- "use strict";
2
- /**
3
- * React Native/Expo Helper Functions
4
- *
5
- * Simplified setup for React Native applications with automatic
6
- * conditional provider selection (FileSystem in dev, Memory in prod)
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.setupMockifyerForReactNative = setupMockifyerForReactNative;
10
- const index_1 = require("./index");
11
- const mockifyer_core_1 = require("@sgedda/mockifyer-core");
12
- const mockifyer_core_2 = require("@sgedda/mockifyer-core");
13
- // Lazy load bundled data (only used in production builds)
14
- let bundledMockData = null;
15
- /**
16
- * Load bundled mock data from a TypeScript/JavaScript module
17
- *
18
- * Note: For React Native/Metro, we need to handle dynamic imports differently
19
- * Metro doesn't support dynamic require() with variables, so we use a workaround
20
- */
21
- async function loadBundledMockData(bundledDataPath) {
22
- if (bundledMockData) {
23
- return bundledMockData;
24
- }
25
- try {
26
- // Metro bundler doesn't support dynamic require() with variables
27
- // We use eval with a fully constructed string to avoid Metro's static analysis
28
- // This bypasses Metro's static analysis completely
29
- const r = 'r';
30
- const e = 'e';
31
- const q = 'q';
32
- const u = 'u';
33
- const i = 'i';
34
- const req = r + e + q + u + i + r + e;
35
- // eslint-disable-next-line no-eval
36
- const module = eval(`${req}(${JSON.stringify(bundledDataPath)})`);
37
- const data = Array.isArray(module.mockData)
38
- ? module.mockData
39
- : [module.mockData];
40
- bundledMockData = data;
41
- return data;
42
- }
43
- catch (error) {
44
- mockifyer_core_2.logger.warn('[Mockifyer] Could not load bundled mock data:', error);
45
- return [];
46
- }
47
- }
48
- /**
49
- * Setup Mockifyer for React Native/Expo with automatic conditional provider selection
50
- *
51
- * - Development (isDev === true): Uses Expo FileSystem provider - can record mocks
52
- * - Production (isDev === false): Uses Memory provider with bundled TypeScript file
53
- *
54
- * @example
55
- * ```typescript
56
- * import { setupMockifyerForReactNative } from '@sgedda/mockifyer-fetch/react-native';
57
- *
58
- * await setupMockifyerForReactNative({
59
- * isDev: __DEV__, // Pass React Native's __DEV__ variable
60
- * mockDataPath: 'mock-data',
61
- * bundledDataPath: './assets/mock-data',
62
- * recordMode: process.env.MOCKIFYER_RECORD === 'true',
63
- * });
64
- * ```
65
- */
66
- async function setupMockifyerForReactNative(options) {
67
- const { isDev, mockDataPath = 'mock-data', bundledDataPath = './assets/mock-data', recordMode = false, config = {}, } = options;
68
- // Check if Mockifyer is enabled
69
- const isEnabled = process.env.MOCKIFYER_ENABLED === 'true' || isDev;
70
- if (!isEnabled) {
71
- mockifyer_core_2.logger.info('[Mockifyer] Disabled');
72
- return null;
73
- }
74
- if (isDev === true) {
75
- // DEVELOPMENT MODE (React Native app running in dev)
76
- // Use Hybrid provider - saves to both device AND project folder simultaneously
77
- // Files are immediately available in project folder (no polling needed)
78
- // Note: The provider's initialize() is async and will be handled by the provider methods
79
- const metroPort = process.env.METRO_PORT ? parseInt(process.env.METRO_PORT, 10) : 8081;
80
- // Merge database provider config from options if provided
81
- const baseDatabaseProvider = {
82
- type: 'hybrid',
83
- path: mockDataPath,
84
- options: {
85
- metroPort,
86
- },
87
- };
88
- // Merge with config.databaseProvider if provided
89
- const databaseProviderConfig = config.databaseProvider
90
- ? {
91
- ...baseDatabaseProvider,
92
- ...config.databaseProvider,
93
- options: {
94
- ...baseDatabaseProvider.options,
95
- ...(config.databaseProvider.options || {}),
96
- metroPort, // Always ensure metroPort is set
97
- },
98
- }
99
- : baseDatabaseProvider;
100
- const instance = (0, index_1.setupMockifyer)({
101
- mockDataPath,
102
- databaseProvider: databaseProviderConfig,
103
- recordMode,
104
- useGlobalFetch: true,
105
- ...config,
106
- });
107
- mockifyer_core_2.logger.info('[Mockifyer] Development mode: Using Hybrid provider (device + project folder)');
108
- mockifyer_core_2.logger.info(`[Mockifyer] Metro endpoint: http://localhost:${metroPort}/mockifyer-save`);
109
- if (recordMode) {
110
- mockifyer_core_2.logger.info('[Mockifyer] Recording mode enabled - new API responses will be saved');
111
- }
112
- return instance;
113
- }
114
- else {
115
- // PRODUCTION BUILD MODE
116
- // Use Memory provider with bundled TypeScript file
117
- const provider = new mockifyer_core_1.MemoryProvider({});
118
- provider.initialize();
119
- // Load bundled mock data
120
- const mockDataArray = await loadBundledMockData(bundledDataPath);
121
- if (mockDataArray.length === 0) {
122
- mockifyer_core_2.logger.warn('[Mockifyer] No bundled mock data found. Make sure to run the build script first.');
123
- return null;
124
- }
125
- // Pre-load all mocks into memory
126
- for (const mockData of mockDataArray) {
127
- provider.save(mockData);
128
- }
129
- const instance = (0, index_1.setupMockifyer)({
130
- mockDataPath: './mock-data', // Not used with memory provider
131
- databaseProvider: {
132
- type: 'memory',
133
- },
134
- recordMode: false, // Can't record in production builds
135
- useGlobalFetch: true,
136
- ...config,
137
- });
138
- mockifyer_core_2.logger.info(`[Mockifyer] Production mode: Loaded ${mockDataArray.length} mocks from bundle`);
139
- return instance;
140
- }
141
- }