Skip to content

Commit 8763655

Browse files
committed
update readme
1 parent 9bd10a2 commit 8763655

File tree

1 file changed

+118
-26
lines changed

1 file changed

+118
-26
lines changed

README.md

Lines changed: 118 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# API Mocker
2-
32
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.
43

54
## Features
@@ -98,20 +97,46 @@ export function setupMocks() {
9897

9998
**For React Applications:**
10099

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:
102125

103126
```typescript
104127
import { setupMocks, apiMocker } from './api-mocks';
105128

129+
const env = import.meta.env;
130+
106131
// 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;
109134

110135
if (useMocks) {
111-
console.log('API Mocking enabled');
136+
console.log('🎭 API Mocking enabled');
112137
setupMocks();
113138
} else {
114-
console.log('Using real API');
139+
console.log('🌐 Using real API');
115140
apiMocker.disable();
116141
}
117142

@@ -144,10 +169,10 @@ export function initializeMocks() {
144169
const useMocks = import.meta.env.VITE_USE_MOCKS === 'true' || isDevelopment;
145170

146171
if (useMocks) {
147-
console.log('API Mocking enabled');
172+
console.log('🎭 API Mocking enabled');
148173
setupMocks();
149174
} else {
150-
console.log('Using real API');
175+
console.log('🌐 Using real API');
151176
apiMocker.disable();
152177
}
153178
}
@@ -519,41 +544,83 @@ VITE_API_URL=https://api.yourapp.com
519544
### Conditional Mock Setup
520545

521546
```typescript
522-
// src/mocks/index.js
547+
// src/mocks/index.js (Create React App / Webpack)
523548
import { setupMocks, setupStatefulMocks, apiMocker } from './api-mocks';
524549

550+
const env = typeof process !== 'undefined' && process?.env ? process.env : {};
551+
525552
const config = {
526553
// 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+
530556
// 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+
533559
// Add delays to simulate real network conditions
534-
realistic: process.env.REACT_APP_REALISTIC_DELAYS === 'true'
560+
realistic: env.REACT_APP_REALISTIC_DELAYS === 'true'
535561
};
536562

537563
if (config.enabled) {
538-
console.log('Initializing API mocks...');
539-
564+
console.log('🎭 Initializing API mocks...');
565+
540566
// Configure the mocker
541567
apiMocker.updateConfig({
542-
baseUrl: process.env.REACT_APP_API_URL,
568+
baseUrl: env.REACT_APP_API_URL,
543569
logRequests: true,
544570
globalDelay: config.realistic ? 500 : 0
545571
});
546-
572+
547573
// Setup mocks
548574
if (config.stateful) {
549575
setupStatefulMocks();
550576
} else {
551577
setupMocks();
552578
}
553-
554-
console.log('API mocks ready');
579+
580+
console.log('API mocks ready');
555581
} 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');
557624
apiMocker.disable();
558625
}
559626
```
@@ -583,11 +650,36 @@ apiMocker.remove('POST', '/users');
583650
### 3. Environment-based Control
584651

585652
```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+
587679
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'
591683
};
592684

593685
if (mockConfig.users) {

0 commit comments

Comments
 (0)