|
1 | 1 | # API Mocker |
2 | | - |
3 | 2 | A lightweight TypeScript library for mocking API endpoints during frontend development. Perfect for developing frontend applications before the backend is ready, or for testing different API scenarios. |
4 | 3 |
|
5 | 4 | ## Features |
@@ -98,20 +97,46 @@ export function setupMocks() { |
98 | 97 |
|
99 | 98 | **For React Applications:** |
100 | 99 |
|
101 | | -Create `src/mocks/index.js`: |
| 100 | +> 💡 **Environment variable tips:** Create React App (and other Webpack-based builds) exposes values through `process.env`, while Vite exposes them via `import.meta.env`. Use the snippet that matches your toolchain so the browser build never tries to access an undefined `process` object. |
| 101 | +
|
| 102 | +Create `src/mocks/index.js` (Create React App / Webpack): |
| 103 | + |
| 104 | +```typescript |
| 105 | +import { setupMocks, apiMocker } from './api-mocks'; |
| 106 | + |
| 107 | +const env = typeof process !== 'undefined' && process?.env ? process.env : {}; |
| 108 | + |
| 109 | +// Only enable mocks in development |
| 110 | +const isDevelopment = env.NODE_ENV === 'development'; |
| 111 | +const useMocks = env.REACT_APP_USE_MOCKS === 'true' || isDevelopment; |
| 112 | + |
| 113 | +if (useMocks) { |
| 114 | + console.log('🎭 API Mocking enabled'); |
| 115 | + setupMocks(); |
| 116 | +} else { |
| 117 | + console.log('🌐 Using real API'); |
| 118 | + apiMocker.disable(); |
| 119 | +} |
| 120 | + |
| 121 | +export { apiMocker }; |
| 122 | +``` |
| 123 | + |
| 124 | +Or for Vite-powered React projects: |
102 | 125 |
|
103 | 126 | ```typescript |
104 | 127 | import { setupMocks, apiMocker } from './api-mocks'; |
105 | 128 |
|
| 129 | +const env = import.meta.env; |
| 130 | + |
106 | 131 | // Only enable mocks in development |
107 | | -const isDevelopment = process.env.NODE_ENV === 'development'; |
108 | | -const useMocks = process.env.REACT_APP_USE_MOCKS === 'true' || isDevelopment; |
| 132 | +const isDevelopment = env.MODE === 'development' || env.DEV; |
| 133 | +const useMocks = env.VITE_USE_MOCKS === 'true' || isDevelopment; |
109 | 134 |
|
110 | 135 | if (useMocks) { |
111 | | - console.log('API Mocking enabled'); |
| 136 | + console.log('🎭 API Mocking enabled'); |
112 | 137 | setupMocks(); |
113 | 138 | } else { |
114 | | - console.log('Using real API'); |
| 139 | + console.log('🌐 Using real API'); |
115 | 140 | apiMocker.disable(); |
116 | 141 | } |
117 | 142 |
|
@@ -144,10 +169,10 @@ export function initializeMocks() { |
144 | 169 | const useMocks = import.meta.env.VITE_USE_MOCKS === 'true' || isDevelopment; |
145 | 170 |
|
146 | 171 | if (useMocks) { |
147 | | - console.log('API Mocking enabled'); |
| 172 | + console.log('🎭 API Mocking enabled'); |
148 | 173 | setupMocks(); |
149 | 174 | } else { |
150 | | - console.log('Using real API'); |
| 175 | + console.log('🌐 Using real API'); |
151 | 176 | apiMocker.disable(); |
152 | 177 | } |
153 | 178 | } |
@@ -519,41 +544,83 @@ VITE_API_URL=https://api.yourapp.com |
519 | 544 | ### Conditional Mock Setup |
520 | 545 |
|
521 | 546 | ```typescript |
522 | | -// src/mocks/index.js |
| 547 | +// src/mocks/index.js (Create React App / Webpack) |
523 | 548 | import { setupMocks, setupStatefulMocks, apiMocker } from './api-mocks'; |
524 | 549 |
|
| 550 | +const env = typeof process !== 'undefined' && process?.env ? process.env : {}; |
| 551 | + |
525 | 552 | const config = { |
526 | 553 | // Enable mocks in development or when explicitly requested |
527 | | - enabled: process.env.NODE_ENV === 'development' || |
528 | | - process.env.REACT_APP_USE_MOCKS === 'true', |
529 | | - |
| 554 | + enabled: env.NODE_ENV === 'development' || env.REACT_APP_USE_MOCKS === 'true', |
| 555 | + |
530 | 556 | // Use stateful mocks for more realistic testing |
531 | | - stateful: process.env.REACT_APP_STATEFUL_MOCKS === 'true', |
532 | | - |
| 557 | + stateful: env.REACT_APP_STATEFUL_MOCKS === 'true', |
| 558 | + |
533 | 559 | // Add delays to simulate real network conditions |
534 | | - realistic: process.env.REACT_APP_REALISTIC_DELAYS === 'true' |
| 560 | + realistic: env.REACT_APP_REALISTIC_DELAYS === 'true' |
535 | 561 | }; |
536 | 562 |
|
537 | 563 | if (config.enabled) { |
538 | | - console.log('Initializing API mocks...'); |
539 | | - |
| 564 | + console.log('🎭 Initializing API mocks...'); |
| 565 | + |
540 | 566 | // Configure the mocker |
541 | 567 | apiMocker.updateConfig({ |
542 | | - baseUrl: process.env.REACT_APP_API_URL, |
| 568 | + baseUrl: env.REACT_APP_API_URL, |
543 | 569 | logRequests: true, |
544 | 570 | globalDelay: config.realistic ? 500 : 0 |
545 | 571 | }); |
546 | | - |
| 572 | + |
547 | 573 | // Setup mocks |
548 | 574 | if (config.stateful) { |
549 | 575 | setupStatefulMocks(); |
550 | 576 | } else { |
551 | 577 | setupMocks(); |
552 | 578 | } |
553 | | - |
554 | | - console.log('API mocks ready'); |
| 579 | + |
| 580 | + console.log('✅ API mocks ready'); |
555 | 581 | } else { |
556 | | - console.log('Using real API endpoints'); |
| 582 | + console.log('🌐 Using real API endpoints'); |
| 583 | + apiMocker.disable(); |
| 584 | +} |
| 585 | +``` |
| 586 | + |
| 587 | +```typescript |
| 588 | +// src/mocks/index.js (Vite) |
| 589 | +import { setupMocks, setupStatefulMocks, apiMocker } from './api-mocks'; |
| 590 | + |
| 591 | +const env = import.meta.env; |
| 592 | + |
| 593 | +const config = { |
| 594 | + // Enable mocks in development or when explicitly requested |
| 595 | + enabled: env.DEV || env.MODE === 'development' || env.VITE_USE_MOCKS === 'true', |
| 596 | + |
| 597 | + // Use stateful mocks for more realistic testing |
| 598 | + stateful: env.VITE_STATEFUL_MOCKS === 'true', |
| 599 | + |
| 600 | + // Add delays to simulate real network conditions |
| 601 | + realistic: env.VITE_REALISTIC_DELAYS === 'true' |
| 602 | +}; |
| 603 | + |
| 604 | +if (config.enabled) { |
| 605 | + console.log('🎭 Initializing API mocks...'); |
| 606 | + |
| 607 | + // Configure the mocker |
| 608 | + apiMocker.updateConfig({ |
| 609 | + baseUrl: env.VITE_API_URL, |
| 610 | + logRequests: true, |
| 611 | + globalDelay: config.realistic ? 500 : 0 |
| 612 | + }); |
| 613 | + |
| 614 | + // Setup mocks |
| 615 | + if (config.stateful) { |
| 616 | + setupStatefulMocks(); |
| 617 | + } else { |
| 618 | + setupMocks(); |
| 619 | + } |
| 620 | + |
| 621 | + console.log('✅ API mocks ready'); |
| 622 | +} else { |
| 623 | + console.log('🌐 Using real API endpoints'); |
557 | 624 | apiMocker.disable(); |
558 | 625 | } |
559 | 626 | ``` |
@@ -583,11 +650,36 @@ apiMocker.remove('POST', '/users'); |
583 | 650 | ### 3. Environment-based Control |
584 | 651 |
|
585 | 652 | ```typescript |
586 | | -// Use environment variables to control which endpoints are mocked |
| 653 | +// Use environment variables to control which endpoints are mocked (CRA / Webpack) |
| 654 | +const env = typeof process !== 'undefined' && process?.env ? process.env : {}; |
| 655 | + |
| 656 | +const mockConfig = { |
| 657 | + users: env.REACT_APP_MOCK_USERS !== 'false', |
| 658 | + products: env.REACT_APP_MOCK_PRODUCTS !== 'false', |
| 659 | + orders: env.REACT_APP_MOCK_ORDERS !== 'false' |
| 660 | +}; |
| 661 | + |
| 662 | +if (mockConfig.users) { |
| 663 | + setupUserMocks(); |
| 664 | +} |
| 665 | + |
| 666 | +if (mockConfig.products) { |
| 667 | + setupProductMocks(); |
| 668 | +} |
| 669 | + |
| 670 | +if (mockConfig.orders) { |
| 671 | + setupOrderMocks(); |
| 672 | +} |
| 673 | +``` |
| 674 | + |
| 675 | +```typescript |
| 676 | +// Use environment variables to control which endpoints are mocked (Vite) |
| 677 | +const env = import.meta.env; |
| 678 | + |
587 | 679 | const mockConfig = { |
588 | | - users: process.env.REACT_APP_MOCK_USERS !== 'false', |
589 | | - products: process.env.REACT_APP_MOCK_PRODUCTS !== 'false', |
590 | | - orders: process.env.REACT_APP_MOCK_ORDERS !== 'false' |
| 680 | + users: env.VITE_MOCK_USERS !== 'false', |
| 681 | + products: env.VITE_MOCK_PRODUCTS !== 'false', |
| 682 | + orders: env.VITE_MOCK_ORDERS !== 'false' |
591 | 683 | }; |
592 | 684 |
|
593 | 685 | if (mockConfig.users) { |
|
0 commit comments