@sgedda/mockifyer-fetch 1.8.22 → 1.8.24

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sebastian Gedda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -6,7 +6,8 @@ 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
+ private getClientId?;
10
+ private clientIdSnapshot?;
10
11
  private deviceId?;
11
12
  constructor(config?: {
12
13
  baseUrl?: string;
@@ -17,8 +18,10 @@ export declare class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<an
17
18
  recordOnMiss?: boolean;
18
19
  };
19
20
  clientId?: string;
21
+ getClientId?: () => string | undefined;
20
22
  deviceId?: string;
21
23
  });
24
+ private resolvedClientLane;
22
25
  protected performRequest<D = any>(config: HTTPRequestConfig<D>): Promise<HTTPResponse<any>>;
23
26
  protected getDefaultHeaders(): Record<string, string>;
24
27
  protected getDefaultConfig(): Record<string, any>;
@@ -12,9 +12,22 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
12
12
  this.proxyBaseUrl = config?.proxy?.baseUrl;
13
13
  this.proxyScenario = config?.proxy?.scenario;
14
14
  this.proxyRecordOnMiss = config?.proxy?.recordOnMiss ?? false;
15
- this.clientId = config?.clientId;
15
+ this.getClientId = config?.getClientId;
16
+ this.clientIdSnapshot = config?.clientId;
16
17
  this.deviceId = config?.deviceId;
17
18
  }
19
+ resolvedClientLane() {
20
+ if (this.getClientId) {
21
+ const v = this.getClientId();
22
+ if (v != null && String(v).trim() !== '') {
23
+ return String(v).trim();
24
+ }
25
+ }
26
+ if (this.clientIdSnapshot != null && String(this.clientIdSnapshot).trim() !== '') {
27
+ return String(this.clientIdSnapshot).trim();
28
+ }
29
+ return undefined;
30
+ }
18
31
  async performRequest(config) {
19
32
  // Check if this is a mock response (set by Mockifyer interceptor)
20
33
  if (config.__mockResponse) {
@@ -55,6 +68,7 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
55
68
  if (!this._originalFetch) {
56
69
  console.warn('[FetchHTTPClient] _originalFetch not set! This may cause infinite loops if global fetch is patched.');
57
70
  }
71
+ const lane = this.resolvedClientLane();
58
72
  // Proxy mode (e.g. React Native → dashboard → Redis)
59
73
  if (this.proxyBaseUrl) {
60
74
  const proxyUrl = (0, join_proxy_dashboard_api_url_1.joinProxyDashboardApiUrl)(this.proxyBaseUrl, 'api/proxy');
@@ -62,13 +76,13 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
62
76
  method: 'POST',
63
77
  headers: {
64
78
  'content-type': 'application/json',
65
- ...(this.clientId ? { [mockifyer_core_2.MOCKIFYER_CLIENT_ID_HEADER]: this.clientId } : {}),
79
+ ...(lane ? { [mockifyer_core_2.MOCKIFYER_CLIENT_ID_HEADER]: lane } : {}),
66
80
  ...(this.deviceId ? { [mockifyer_core_2.MOCKIFYER_DEVICE_ID_HEADER]: this.deviceId } : {}),
67
81
  },
68
82
  body: JSON.stringify({
69
83
  url,
70
84
  method: requestConfig.method,
71
- clientId: this.clientId,
85
+ clientId: lane,
72
86
  deviceId: this.deviceId,
73
87
  headers: (() => {
74
88
  const out = {};
@@ -92,9 +106,9 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
92
106
  const hash = typeof payload?.hash === 'string' ? payload.hash : '';
93
107
  if (source) {
94
108
  const hashShort = hash ? `${hash.slice(0, 8)}…` : '';
95
- const lane = this.clientId ? this.clientId : '—';
109
+ const laneLabel = lane ? lane : '—';
96
110
  const kind = source === 'redis' ? 'mock hit' : 'upstream';
97
- console.log(`[Mockifyer-Fetch] Proxy ${kind}: ${requestConfig.method} ${url} ${hashShort ? `(hash=${hashShort}) ` : ''}(lane=${lane})`);
111
+ console.log(`[Mockifyer-Fetch] Proxy ${kind}: ${requestConfig.method} ${url} ${hashShort ? `(hash=${hashShort}) ` : ''}(lane=${laneLabel})`);
98
112
  }
99
113
  }
100
114
  catch {
@@ -111,8 +125,8 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
111
125
  config,
112
126
  };
113
127
  }
114
- if (this.clientId && String(this.clientId).trim() && !(0, mockifyer_core_2.getOutboundMockifyerClientIdHeader)(headers)) {
115
- headers.set(mockifyer_core_2.MOCKIFYER_CLIENT_ID_HEADER, String(this.clientId).trim());
128
+ if (lane && !(0, mockifyer_core_2.getOutboundMockifyerClientIdHeader)(headers)) {
129
+ headers.set(mockifyer_core_2.MOCKIFYER_CLIENT_ID_HEADER, lane);
116
130
  }
117
131
  if (this.deviceId && String(this.deviceId).trim() && !(0, mockifyer_core_2.getOutboundMockifyerDeviceIdHeader)(headers)) {
118
132
  headers.set(mockifyer_core_2.MOCKIFYER_DEVICE_ID_HEADER, String(this.deviceId).trim());
package/dist/index.d.ts CHANGED
@@ -3,6 +3,8 @@ export interface MockifyerInstance extends HTTPClient {
3
3
  reloadMockData: (syncFromProject?: boolean) => Promise<void>;
4
4
  clearStaleCacheEntries: () => number;
5
5
  clearAllMocks: () => Promise<void>;
6
+ setClientId: (lane: string) => void;
7
+ getClientId: () => string | undefined;
6
8
  }
7
9
  export declare function setupMockifyer(config: MockifyerConfig): MockifyerInstance;
8
10
  export * from '@sgedda/mockifyer-core';
package/dist/index.js CHANGED
@@ -114,6 +114,7 @@ class MockifyerClass {
114
114
  ...config.databaseProvider.options,
115
115
  mockDataPath: config.databaseProvider.options?.mockDataPath ?? config.mockDataPath,
116
116
  clientId: this.config.clientId,
117
+ getClientId: () => this.config.clientId,
117
118
  },
118
119
  };
119
120
  this.databaseProvider = (0, mockifyer_core_1.createProvider)(config.databaseProvider.type, providerConfig);
@@ -135,6 +136,7 @@ class MockifyerClass {
135
136
  defaultHeaders: config.defaultHeaders,
136
137
  proxy: config.proxy,
137
138
  clientId: this.config.clientId,
139
+ getClientId: () => this.config.clientId,
138
140
  deviceId: this.config.deviceId,
139
141
  });
140
142
  if (!config.recordSameEndpoints) {
@@ -357,6 +359,11 @@ class MockifyerClass {
357
359
  config.__mockifyer_bypass = true;
358
360
  return config;
359
361
  }
362
+ if (!(0, mockifyer_core_1.isExplicitProxyScenarioContext)(this.config)) {
363
+ mockifyer_core_2.logger.warn('[Mockifyer-Fetch] Strict proxy scenario: set clientId (lane) or proxy.scenario; passthrough HTTP for this request');
364
+ config.__mockifyer_bypass = true;
365
+ return config;
366
+ }
360
367
  // Normalize empty params: treat {} the same as undefined for consistent matching
361
368
  const rawParams = config.params || {};
362
369
  const anonymizedQueryParams = this.anonymizeQueryParams(rawParams);
@@ -824,6 +831,22 @@ class MockifyerClass {
824
831
  });
825
832
  return anonymized;
826
833
  }
834
+ /**
835
+ * Updates the logical client lane for subsequent requests (headers, proxy envelope, scenario paths, Redis).
836
+ * Does not re-read native launch arguments or environment variables.
837
+ */
838
+ setClientId(lane) {
839
+ const t = String(lane).trim();
840
+ if (!t) {
841
+ throw new Error('[Mockifyer-Fetch] setClientId requires a non-empty string');
842
+ }
843
+ this.config.clientId = t;
844
+ mockifyer_core_2.logger.info(`[Mockifyer-Fetch] clientId set to: ${t}`);
845
+ }
846
+ /** Current logical lane id (same value used for isolation). */
847
+ getClientId() {
848
+ return this.config.clientId;
849
+ }
827
850
  getHTTPClient() {
828
851
  return this.httpClient;
829
852
  }
@@ -999,6 +1022,8 @@ function setupMockifyer(config) {
999
1022
  extendedClient.reloadMockData = async (syncFromProject) => await mockifyer.reloadMockData(syncFromProject);
1000
1023
  extendedClient.clearStaleCacheEntries = () => mockifyer.clearStaleCacheEntries();
1001
1024
  extendedClient.clearAllMocks = () => mockifyer.clearAllMocks();
1025
+ extendedClient.setClientId = (lane) => mockifyer.setClientId(lane);
1026
+ extendedClient.getClientId = () => mockifyer.getClientId();
1002
1027
  return extendedClient;
1003
1028
  }
1004
1029
  // Re-export types from core
@@ -11,6 +11,8 @@ export interface MockifyerInstance extends HTTPClient {
11
11
  reloadMockData: (syncFromProject?: boolean) => Promise<void>;
12
12
  clearStaleCacheEntries: () => number;
13
13
  clearAllMocks: () => Promise<void>;
14
+ setClientId: (lane: string) => void;
15
+ getClientId: () => string | undefined;
14
16
  }
15
17
  /**
16
18
  * Outcome of {@link setupMockifyerForReactNative}.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sgedda/mockifyer-fetch",
3
- "version": "1.8.22",
3
+ "version": "1.8.24",
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",
@@ -43,10 +43,13 @@
43
43
  "intercept",
44
44
  "fetch"
45
45
  ],
46
- "author": "sgedda",
46
+ "author": {
47
+ "name": "Sebastian Gedda",
48
+ "url": "https://github.com/sgedda"
49
+ },
47
50
  "license": "MIT",
48
51
  "dependencies": {
49
- "@sgedda/mockifyer-core": "^1.8.27"
52
+ "@sgedda/mockifyer-core": "^1.8.29"
50
53
  },
51
54
  "devDependencies": {
52
55
  "@types/node": "^20.11.24",
@@ -14,7 +14,8 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
14
14
  private proxyBaseUrl?: string;
15
15
  private proxyScenario?: string;
16
16
  private proxyRecordOnMiss: boolean;
17
- private clientId?: string;
17
+ private getClientId?: () => string | undefined;
18
+ private clientIdSnapshot?: string;
18
19
  private deviceId?: string;
19
20
 
20
21
  constructor(config?: {
@@ -22,6 +23,7 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
22
23
  defaultHeaders?: Record<string, string>;
23
24
  proxy?: { baseUrl: string; scenario?: string; recordOnMiss?: boolean };
24
25
  clientId?: string;
26
+ getClientId?: () => string | undefined;
25
27
  deviceId?: string;
26
28
  }) {
27
29
  super();
@@ -30,10 +32,24 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
30
32
  this.proxyBaseUrl = config?.proxy?.baseUrl;
31
33
  this.proxyScenario = config?.proxy?.scenario;
32
34
  this.proxyRecordOnMiss = config?.proxy?.recordOnMiss ?? false;
33
- this.clientId = config?.clientId;
35
+ this.getClientId = config?.getClientId;
36
+ this.clientIdSnapshot = config?.clientId;
34
37
  this.deviceId = config?.deviceId;
35
38
  }
36
39
 
40
+ private resolvedClientLane(): string | undefined {
41
+ if (this.getClientId) {
42
+ const v = this.getClientId();
43
+ if (v != null && String(v).trim() !== '') {
44
+ return String(v).trim();
45
+ }
46
+ }
47
+ if (this.clientIdSnapshot != null && String(this.clientIdSnapshot).trim() !== '') {
48
+ return String(this.clientIdSnapshot).trim();
49
+ }
50
+ return undefined;
51
+ }
52
+
37
53
  protected async performRequest<D = any>(config: HTTPRequestConfig<D>): Promise<HTTPResponse<any>> {
38
54
  // Check if this is a mock response (set by Mockifyer interceptor)
39
55
  if ((config as any).__mockResponse) {
@@ -79,6 +95,8 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
79
95
  console.warn('[FetchHTTPClient] _originalFetch not set! This may cause infinite loops if global fetch is patched.');
80
96
  }
81
97
 
98
+ const lane = this.resolvedClientLane();
99
+
82
100
  // Proxy mode (e.g. React Native → dashboard → Redis)
83
101
  if (this.proxyBaseUrl) {
84
102
  const proxyUrl = joinProxyDashboardApiUrl(this.proxyBaseUrl, 'api/proxy');
@@ -86,13 +104,13 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
86
104
  method: 'POST',
87
105
  headers: {
88
106
  'content-type': 'application/json',
89
- ...(this.clientId ? { [MOCKIFYER_CLIENT_ID_HEADER]: this.clientId } : {}),
107
+ ...(lane ? { [MOCKIFYER_CLIENT_ID_HEADER]: lane } : {}),
90
108
  ...(this.deviceId ? { [MOCKIFYER_DEVICE_ID_HEADER]: this.deviceId } : {}),
91
109
  },
92
110
  body: JSON.stringify({
93
111
  url,
94
112
  method: requestConfig.method,
95
- clientId: this.clientId,
113
+ clientId: lane,
96
114
  deviceId: this.deviceId,
97
115
  headers: (() => {
98
116
  const out: Record<string, string> = {};
@@ -116,12 +134,12 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
116
134
  const hash = typeof payload?.hash === 'string' ? payload.hash : '';
117
135
  if (source) {
118
136
  const hashShort = hash ? `${hash.slice(0, 8)}…` : '';
119
- const lane = this.clientId ? this.clientId : '—';
137
+ const laneLabel = lane ? lane : '—';
120
138
  const kind = source === 'redis' ? 'mock hit' : 'upstream';
121
139
  console.log(
122
140
  `[Mockifyer-Fetch] Proxy ${kind}: ${requestConfig.method} ${url} ${
123
141
  hashShort ? `(hash=${hashShort}) ` : ''
124
- }(lane=${lane})`
142
+ }(lane=${laneLabel})`
125
143
  );
126
144
  }
127
145
  } catch {
@@ -140,8 +158,8 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
140
158
  };
141
159
  }
142
160
 
143
- if (this.clientId && String(this.clientId).trim() && !getOutboundMockifyerClientIdHeader(headers)) {
144
- headers.set(MOCKIFYER_CLIENT_ID_HEADER, String(this.clientId).trim());
161
+ if (lane && !getOutboundMockifyerClientIdHeader(headers)) {
162
+ headers.set(MOCKIFYER_CLIENT_ID_HEADER, lane);
145
163
  }
146
164
  if (this.deviceId && String(this.deviceId).trim() && !getOutboundMockifyerDeviceIdHeader(headers)) {
147
165
  headers.set(MOCKIFYER_DEVICE_ID_HEADER, String(this.deviceId).trim());
package/src/index.ts CHANGED
@@ -42,6 +42,7 @@ import {
42
42
  MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY,
43
43
  resolveActivationMode,
44
44
  shouldApplyMockifyer,
45
+ isExplicitProxyScenarioContext,
45
46
  type MockifyerActivationMode,
46
47
  } from '@sgedda/mockifyer-core';
47
48
  import { logger, setLogLevel } from '@sgedda/mockifyer-core';
@@ -151,6 +152,7 @@ class MockifyerClass {
151
152
  mockDataPath:
152
153
  config.databaseProvider.options?.mockDataPath ?? config.mockDataPath,
153
154
  clientId: this.config.clientId,
155
+ getClientId: () => this.config.clientId,
154
156
  },
155
157
  };
156
158
  this.databaseProvider = createProvider(config.databaseProvider.type, providerConfig);
@@ -172,6 +174,7 @@ class MockifyerClass {
172
174
  defaultHeaders: config.defaultHeaders,
173
175
  proxy: config.proxy,
174
176
  clientId: this.config.clientId,
177
+ getClientId: () => this.config.clientId,
175
178
  deviceId: (this.config as any).deviceId,
176
179
  });
177
180
 
@@ -435,6 +438,14 @@ class MockifyerClass {
435
438
  return config;
436
439
  }
437
440
 
441
+ if (!isExplicitProxyScenarioContext(this.config)) {
442
+ logger.warn(
443
+ '[Mockifyer-Fetch] Strict proxy scenario: set clientId (lane) or proxy.scenario; passthrough HTTP for this request'
444
+ );
445
+ (config as any).__mockifyer_bypass = true;
446
+ return config;
447
+ }
448
+
438
449
  // Normalize empty params: treat {} the same as undefined for consistent matching
439
450
  const rawParams = config.params || {};
440
451
  const anonymizedQueryParams = this.anonymizeQueryParams(rawParams);
@@ -980,6 +991,24 @@ class MockifyerClass {
980
991
  return anonymized;
981
992
  }
982
993
 
994
+ /**
995
+ * Updates the logical client lane for subsequent requests (headers, proxy envelope, scenario paths, Redis).
996
+ * Does not re-read native launch arguments or environment variables.
997
+ */
998
+ setClientId(lane: string): void {
999
+ const t = String(lane).trim();
1000
+ if (!t) {
1001
+ throw new Error('[Mockifyer-Fetch] setClientId requires a non-empty string');
1002
+ }
1003
+ this.config.clientId = t;
1004
+ logger.info(`[Mockifyer-Fetch] clientId set to: ${t}`);
1005
+ }
1006
+
1007
+ /** Current logical lane id (same value used for isolation). */
1008
+ getClientId(): string | undefined {
1009
+ return this.config.clientId;
1010
+ }
1011
+
983
1012
  getHTTPClient(): HTTPClient {
984
1013
  return this.httpClient;
985
1014
  }
@@ -1020,6 +1049,8 @@ export interface MockifyerInstance extends HTTPClient {
1020
1049
  reloadMockData: (syncFromProject?: boolean) => Promise<void>;
1021
1050
  clearStaleCacheEntries: () => number;
1022
1051
  clearAllMocks: () => Promise<void>;
1052
+ setClientId: (lane: string) => void;
1053
+ getClientId: () => string | undefined;
1023
1054
  }
1024
1055
 
1025
1056
  export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
@@ -1178,6 +1209,8 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
1178
1209
  await mockifyer.reloadMockData(syncFromProject);
1179
1210
  extendedClient.clearStaleCacheEntries = () => mockifyer.clearStaleCacheEntries();
1180
1211
  extendedClient.clearAllMocks = () => mockifyer.clearAllMocks();
1212
+ extendedClient.setClientId = (lane: string) => mockifyer.setClientId(lane);
1213
+ extendedClient.getClientId = () => mockifyer.getClientId();
1181
1214
 
1182
1215
  return extendedClient;
1183
1216
  }
@@ -21,6 +21,8 @@ export interface MockifyerInstance extends HTTPClient {
21
21
  reloadMockData: (syncFromProject?: boolean) => Promise<void>;
22
22
  clearStaleCacheEntries: () => number;
23
23
  clearAllMocks: () => Promise<void>;
24
+ setClientId: (lane: string) => void;
25
+ getClientId: () => string | undefined;
24
26
  }
25
27
 
26
28
  /**