@sgedda/mockifyer-fetch 1.8.8 → 1.8.11

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.
@@ -6,6 +6,7 @@ export declare class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<an
6
6
  private proxyBaseUrl?;
7
7
  private proxyScenario?;
8
8
  private proxyRecordOnMiss;
9
+ private clientId?;
9
10
  constructor(config?: {
10
11
  baseUrl?: string;
11
12
  defaultHeaders?: Record<string, string>;
@@ -14,6 +15,7 @@ export declare class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<an
14
15
  scenario?: string;
15
16
  recordOnMiss?: boolean;
16
17
  };
18
+ clientId?: string;
17
19
  });
18
20
  protected performRequest<D = any>(config: HTTPRequestConfig<D>): Promise<HTTPResponse<any>>;
19
21
  protected getDefaultHeaders(): Record<string, string>;
@@ -10,6 +10,7 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
10
10
  this.proxyBaseUrl = config?.proxy?.baseUrl;
11
11
  this.proxyScenario = config?.proxy?.scenario;
12
12
  this.proxyRecordOnMiss = config?.proxy?.recordOnMiss ?? false;
13
+ this.clientId = config?.clientId;
13
14
  }
14
15
  async performRequest(config) {
15
16
  // Check if this is a mock response (set by Mockifyer interceptor)
@@ -56,10 +57,14 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
56
57
  const proxyUrl = new URL('/api/proxy', this.proxyBaseUrl).toString();
57
58
  const proxyResponse = await fetchFn(proxyUrl, {
58
59
  method: 'POST',
59
- headers: { 'content-type': 'application/json' },
60
+ headers: {
61
+ 'content-type': 'application/json',
62
+ ...(this.clientId ? { 'x-mockifyer-client-id': this.clientId } : {}),
63
+ },
60
64
  body: JSON.stringify({
61
65
  url,
62
66
  method: requestConfig.method,
67
+ clientId: this.clientId,
63
68
  headers: (() => {
64
69
  const out = {};
65
70
  headers.forEach((value, key) => {
package/dist/index.js CHANGED
@@ -82,6 +82,8 @@ class MockifyerClass {
82
82
  }
83
83
  // Store config BEFORE any modifications
84
84
  this.config = { ...config }; // Create a copy to avoid mutations
85
+ // Resolve client lane id (env/config/fallback); used for Redis per-client scenario overrides and proxy headers.
86
+ this.config.clientId = (0, mockifyer_core_1.resolveClientId)(this.config);
85
87
  // Initialize test generator if test generation is enabled
86
88
  if (config.generateTests?.enabled) {
87
89
  this.testGenerator = new mockifyer_core_1.TestGenerator();
@@ -93,6 +95,7 @@ class MockifyerClass {
93
95
  options: {
94
96
  ...config.databaseProvider.options,
95
97
  mockDataPath: config.databaseProvider.options?.mockDataPath ?? config.mockDataPath,
98
+ clientId: this.config.clientId,
96
99
  },
97
100
  };
98
101
  this.databaseProvider = (0, mockifyer_core_1.createProvider)(config.databaseProvider.type, providerConfig);
@@ -112,7 +115,8 @@ class MockifyerClass {
112
115
  this.httpClient = new fetch_client_1.FetchHTTPClient({
113
116
  baseUrl: config.baseUrl,
114
117
  defaultHeaders: config.defaultHeaders,
115
- proxy: config.proxy
118
+ proxy: config.proxy,
119
+ clientId: this.config.clientId,
116
120
  });
117
121
  if (!config.recordSameEndpoints) {
118
122
  this.loadMockData();
@@ -200,7 +204,7 @@ class MockifyerClass {
200
204
  console.warn('[Mockifyer-Fetch] ⚠️ Mock data path does not exist:', resolvedMockDataPath);
201
205
  return undefined;
202
206
  }
203
- const currentScenario = (0, mockifyer_core_1.getCurrentScenario)(resolvedMockDataPath);
207
+ const currentScenario = (0, mockifyer_core_1.getCurrentScenario)(resolvedMockDataPath, this.config.clientId);
204
208
  const scenarioPath = (0, mockifyer_core_1.getScenarioFolderPath)(resolvedMockDataPath, currentScenario);
205
209
  if (!fs.existsSync(scenarioPath)) {
206
210
  mockifyer_core_2.logger.warn('[Mockifyer-Fetch] ⚠️ Scenario path does not exist:', scenarioPath);
@@ -221,8 +225,11 @@ class MockifyerClass {
221
225
  }
222
226
  const mockKey = this.generateRequestKey(mockData.request);
223
227
  if (mockKey === requestKey) {
224
- exactMatch = { mockData, filename: file, filePath };
225
- break;
228
+ if (!(0, mockifyer_core_1.mockPassesThroughToRealApi)(mockData)) {
229
+ exactMatch = { mockData, filename: file, filePath };
230
+ break;
231
+ }
232
+ continue;
226
233
  }
227
234
  if (!exactMatch && this.config.useSimilarMatch && !similarMatch) {
228
235
  // Check if it's a GraphQL request - skip similar matching for GraphQL
@@ -287,7 +294,9 @@ class MockifyerClass {
287
294
  continue; // Required parameter differs, skip this mock
288
295
  }
289
296
  }
290
- similarMatch = { mockData, filename: file, filePath };
297
+ if (!(0, mockifyer_core_1.mockPassesThroughToRealApi)(mockData)) {
298
+ similarMatch = { mockData, filename: file, filePath };
299
+ }
291
300
  }
292
301
  }
293
302
  catch (e) {
@@ -605,7 +614,7 @@ class MockifyerClass {
605
614
  const domain = urlParts[0].replace(/\./g, '_');
606
615
  const urlPathPart = urlParts.slice(1).join('_') || 'root';
607
616
  const filename = `${timestamp}_${response.config.method?.toUpperCase() || 'GET'}_${domain}_${urlPathPart}.json`;
608
- const currentScenario = (0, mockifyer_core_1.getCurrentScenario)(this.config.mockDataPath);
617
+ const currentScenario = (0, mockifyer_core_1.getCurrentScenario)(this.config.mockDataPath, this.config.clientId);
609
618
  const scenarioPath = (0, mockifyer_core_1.getScenarioFolderPath)(this.config.mockDataPath, currentScenario);
610
619
  (0, mockifyer_core_1.ensureScenarioFolder)(this.config.mockDataPath, currentScenario);
611
620
  // Check request limit before saving (only if limit is set via env var)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sgedda/mockifyer-fetch",
3
- "version": "1.8.8",
3
+ "version": "1.8.11",
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",
@@ -46,7 +46,7 @@
46
46
  "author": "sgedda",
47
47
  "license": "MIT",
48
48
  "dependencies": {
49
- "@sgedda/mockifyer-core": "^1.8.8"
49
+ "@sgedda/mockifyer-core": "^1.8.11"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/node": "^20.11.24",
@@ -7,11 +7,13 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
7
7
  private proxyBaseUrl?: string;
8
8
  private proxyScenario?: string;
9
9
  private proxyRecordOnMiss: boolean;
10
+ private clientId?: string;
10
11
 
11
12
  constructor(config?: {
12
13
  baseUrl?: string;
13
14
  defaultHeaders?: Record<string, string>;
14
15
  proxy?: { baseUrl: string; scenario?: string; recordOnMiss?: boolean };
16
+ clientId?: string;
15
17
  }) {
16
18
  super();
17
19
  this.baseUrl = config?.baseUrl;
@@ -19,6 +21,7 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
19
21
  this.proxyBaseUrl = config?.proxy?.baseUrl;
20
22
  this.proxyScenario = config?.proxy?.scenario;
21
23
  this.proxyRecordOnMiss = config?.proxy?.recordOnMiss ?? false;
24
+ this.clientId = config?.clientId;
22
25
  }
23
26
 
24
27
  protected async performRequest<D = any>(config: HTTPRequestConfig<D>): Promise<HTTPResponse<any>> {
@@ -71,10 +74,14 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
71
74
  const proxyUrl = new URL('/api/proxy', this.proxyBaseUrl).toString();
72
75
  const proxyResponse = await fetchFn(proxyUrl, {
73
76
  method: 'POST',
74
- headers: { 'content-type': 'application/json' },
77
+ headers: {
78
+ 'content-type': 'application/json',
79
+ ...(this.clientId ? { 'x-mockifyer-client-id': this.clientId } : {}),
80
+ },
75
81
  body: JSON.stringify({
76
82
  url,
77
83
  method: requestConfig.method,
84
+ clientId: this.clientId,
78
85
  headers: (() => {
79
86
  const out: Record<string, string> = {};
80
87
  headers.forEach((value, key) => {
package/src/index.ts CHANGED
@@ -35,7 +35,9 @@ import {
35
35
  checkRequestLimit,
36
36
  prepareMockResponseBody,
37
37
  getCurrentDate,
38
- shouldExcludeUrl
38
+ shouldExcludeUrl,
39
+ mockPassesThroughToRealApi,
40
+ resolveClientId
39
41
  } from '@sgedda/mockifyer-core';
40
42
  import { logger, setLogLevel } from '@sgedda/mockifyer-core';
41
43
 
@@ -106,6 +108,9 @@ class MockifyerClass {
106
108
 
107
109
  // Store config BEFORE any modifications
108
110
  this.config = { ...config }; // Create a copy to avoid mutations
111
+
112
+ // Resolve client lane id (env/config/fallback); used for Redis per-client scenario overrides and proxy headers.
113
+ this.config.clientId = resolveClientId(this.config);
109
114
 
110
115
  // Initialize test generator if test generation is enabled
111
116
  if (config.generateTests?.enabled) {
@@ -120,6 +125,7 @@ class MockifyerClass {
120
125
  ...config.databaseProvider.options,
121
126
  mockDataPath:
122
127
  config.databaseProvider.options?.mockDataPath ?? config.mockDataPath,
128
+ clientId: this.config.clientId,
123
129
  },
124
130
  };
125
131
  this.databaseProvider = createProvider(config.databaseProvider.type, providerConfig);
@@ -139,7 +145,8 @@ class MockifyerClass {
139
145
  this.httpClient = new FetchHTTPClient({
140
146
  baseUrl: config.baseUrl,
141
147
  defaultHeaders: config.defaultHeaders,
142
- proxy: config.proxy
148
+ proxy: config.proxy,
149
+ clientId: this.config.clientId,
143
150
  });
144
151
 
145
152
  if(!config.recordSameEndpoints) {
@@ -245,7 +252,7 @@ class MockifyerClass {
245
252
  return undefined;
246
253
  }
247
254
 
248
- const currentScenario = getCurrentScenario(resolvedMockDataPath);
255
+ const currentScenario = getCurrentScenario(resolvedMockDataPath, this.config.clientId);
249
256
  const scenarioPath = getScenarioFolderPath(resolvedMockDataPath, currentScenario);
250
257
 
251
258
  if (!fs.existsSync(scenarioPath)) {
@@ -273,8 +280,11 @@ class MockifyerClass {
273
280
  const mockKey = this.generateRequestKey(mockData.request);
274
281
 
275
282
  if (mockKey === requestKey) {
276
- exactMatch = { mockData, filename: file, filePath };
277
- break;
283
+ if (!mockPassesThroughToRealApi(mockData)) {
284
+ exactMatch = { mockData, filename: file, filePath };
285
+ break;
286
+ }
287
+ continue;
278
288
  }
279
289
 
280
290
  if (!exactMatch && this.config.useSimilarMatch && !similarMatch) {
@@ -350,7 +360,9 @@ class MockifyerClass {
350
360
  }
351
361
  }
352
362
 
353
- similarMatch = { mockData, filename: file, filePath };
363
+ if (!mockPassesThroughToRealApi(mockData)) {
364
+ similarMatch = { mockData, filename: file, filePath };
365
+ }
354
366
  }
355
367
  } catch (e) {
356
368
  continue;
@@ -715,7 +727,7 @@ class MockifyerClass {
715
727
  const domain = urlParts[0].replace(/\./g, '_');
716
728
  const urlPathPart = urlParts.slice(1).join('_') || 'root';
717
729
  const filename = `${timestamp}_${response.config.method?.toUpperCase() || 'GET'}_${domain}_${urlPathPart}.json`;
718
- const currentScenario = getCurrentScenario(this.config.mockDataPath);
730
+ const currentScenario = getCurrentScenario(this.config.mockDataPath, this.config.clientId);
719
731
  const scenarioPath = getScenarioFolderPath(this.config.mockDataPath, currentScenario);
720
732
  ensureScenarioFolder(this.config.mockDataPath, currentScenario);
721
733