@sgedda/mockifyer-fetch 1.8.22 → 1.8.25
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 +21 -0
- package/dist/clients/fetch-client.d.ts +4 -1
- package/dist/clients/fetch-client.js +21 -7
- package/dist/index.d.ts +2 -0
- package/dist/index.js +26 -0
- package/dist/react-native.d.ts +3 -1
- package/dist/react-native.js +5 -1
- package/package.json +6 -3
- package/src/clients/fetch-client.ts +26 -8
- package/src/index.ts +37 -1
- package/src/react-native.ts +10 -1
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
|
|
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.
|
|
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
|
-
...(
|
|
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:
|
|
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
|
|
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=${
|
|
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 (
|
|
115
|
-
headers.set(mockifyer_core_2.MOCKIFYER_CLIENT_ID_HEADER,
|
|
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,9 @@ 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();
|
|
1027
|
+
(0, mockifyer_core_1.registerMockifyerInstance)(extendedClient);
|
|
1002
1028
|
return extendedClient;
|
|
1003
1029
|
}
|
|
1004
1030
|
// Re-export types from core
|
package/dist/react-native.d.ts
CHANGED
|
@@ -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}.
|
|
@@ -95,4 +97,4 @@ export interface ReactNativeMockifyerConfig {
|
|
|
95
97
|
* ```
|
|
96
98
|
*/
|
|
97
99
|
export declare function setupMockifyerForReactNative(options: ReactNativeMockifyerConfig): Promise<SetupMockifyerForReactNativeResult>;
|
|
98
|
-
export { tryGetClientIdFromLaunchArguments, MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY } from '@sgedda/mockifyer-core';
|
|
100
|
+
export { tryGetClientIdFromLaunchArguments, MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY, getClientId, setClientId, registerMockifyerInstance, clearMockifyerClientIdRuntime, } from '@sgedda/mockifyer-core';
|
package/dist/react-native.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* conditional provider selection (FileSystem in dev, Memory in prod)
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY = exports.tryGetClientIdFromLaunchArguments = void 0;
|
|
9
|
+
exports.clearMockifyerClientIdRuntime = exports.registerMockifyerInstance = exports.setClientId = exports.getClientId = exports.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY = exports.tryGetClientIdFromLaunchArguments = void 0;
|
|
10
10
|
exports.isMockifyerReactNativeActive = isMockifyerReactNativeActive;
|
|
11
11
|
exports.setupMockifyerForReactNative = setupMockifyerForReactNative;
|
|
12
12
|
const join_proxy_dashboard_api_url_1 = require("./utils/join-proxy-dashboard-api-url");
|
|
@@ -223,3 +223,7 @@ async function setupMockifyerForReactNative(options) {
|
|
|
223
223
|
var mockifyer_core_3 = require("@sgedda/mockifyer-core");
|
|
224
224
|
Object.defineProperty(exports, "tryGetClientIdFromLaunchArguments", { enumerable: true, get: function () { return mockifyer_core_3.tryGetClientIdFromLaunchArguments; } });
|
|
225
225
|
Object.defineProperty(exports, "MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY", { enumerable: true, get: function () { return mockifyer_core_3.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY; } });
|
|
226
|
+
Object.defineProperty(exports, "getClientId", { enumerable: true, get: function () { return mockifyer_core_3.getClientId; } });
|
|
227
|
+
Object.defineProperty(exports, "setClientId", { enumerable: true, get: function () { return mockifyer_core_3.setClientId; } });
|
|
228
|
+
Object.defineProperty(exports, "registerMockifyerInstance", { enumerable: true, get: function () { return mockifyer_core_3.registerMockifyerInstance; } });
|
|
229
|
+
Object.defineProperty(exports, "clearMockifyerClientIdRuntime", { enumerable: true, get: function () { return mockifyer_core_3.clearMockifyerClientIdRuntime; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.25",
|
|
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":
|
|
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.
|
|
52
|
+
"@sgedda/mockifyer-core": "^1.8.30"
|
|
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
|
|
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.
|
|
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
|
-
...(
|
|
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:
|
|
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
|
|
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=${
|
|
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 (
|
|
144
|
-
headers.set(MOCKIFYER_CLIENT_ID_HEADER,
|
|
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
|
@@ -38,10 +38,12 @@ import {
|
|
|
38
38
|
shouldExcludeUrl,
|
|
39
39
|
mockPassesThroughToRealApi,
|
|
40
40
|
resolveClientId,
|
|
41
|
+
registerMockifyerInstance,
|
|
41
42
|
tryGetClientIdFromLaunchArguments,
|
|
42
43
|
MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY,
|
|
43
44
|
resolveActivationMode,
|
|
44
45
|
shouldApplyMockifyer,
|
|
46
|
+
isExplicitProxyScenarioContext,
|
|
45
47
|
type MockifyerActivationMode,
|
|
46
48
|
} from '@sgedda/mockifyer-core';
|
|
47
49
|
import { logger, setLogLevel } from '@sgedda/mockifyer-core';
|
|
@@ -151,6 +153,7 @@ class MockifyerClass {
|
|
|
151
153
|
mockDataPath:
|
|
152
154
|
config.databaseProvider.options?.mockDataPath ?? config.mockDataPath,
|
|
153
155
|
clientId: this.config.clientId,
|
|
156
|
+
getClientId: () => this.config.clientId,
|
|
154
157
|
},
|
|
155
158
|
};
|
|
156
159
|
this.databaseProvider = createProvider(config.databaseProvider.type, providerConfig);
|
|
@@ -172,6 +175,7 @@ class MockifyerClass {
|
|
|
172
175
|
defaultHeaders: config.defaultHeaders,
|
|
173
176
|
proxy: config.proxy,
|
|
174
177
|
clientId: this.config.clientId,
|
|
178
|
+
getClientId: () => this.config.clientId,
|
|
175
179
|
deviceId: (this.config as any).deviceId,
|
|
176
180
|
});
|
|
177
181
|
|
|
@@ -435,6 +439,14 @@ class MockifyerClass {
|
|
|
435
439
|
return config;
|
|
436
440
|
}
|
|
437
441
|
|
|
442
|
+
if (!isExplicitProxyScenarioContext(this.config)) {
|
|
443
|
+
logger.warn(
|
|
444
|
+
'[Mockifyer-Fetch] Strict proxy scenario: set clientId (lane) or proxy.scenario; passthrough HTTP for this request'
|
|
445
|
+
);
|
|
446
|
+
(config as any).__mockifyer_bypass = true;
|
|
447
|
+
return config;
|
|
448
|
+
}
|
|
449
|
+
|
|
438
450
|
// Normalize empty params: treat {} the same as undefined for consistent matching
|
|
439
451
|
const rawParams = config.params || {};
|
|
440
452
|
const anonymizedQueryParams = this.anonymizeQueryParams(rawParams);
|
|
@@ -980,6 +992,24 @@ class MockifyerClass {
|
|
|
980
992
|
return anonymized;
|
|
981
993
|
}
|
|
982
994
|
|
|
995
|
+
/**
|
|
996
|
+
* Updates the logical client lane for subsequent requests (headers, proxy envelope, scenario paths, Redis).
|
|
997
|
+
* Does not re-read native launch arguments or environment variables.
|
|
998
|
+
*/
|
|
999
|
+
setClientId(lane: string): void {
|
|
1000
|
+
const t = String(lane).trim();
|
|
1001
|
+
if (!t) {
|
|
1002
|
+
throw new Error('[Mockifyer-Fetch] setClientId requires a non-empty string');
|
|
1003
|
+
}
|
|
1004
|
+
this.config.clientId = t;
|
|
1005
|
+
logger.info(`[Mockifyer-Fetch] clientId set to: ${t}`);
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
/** Current logical lane id (same value used for isolation). */
|
|
1009
|
+
getClientId(): string | undefined {
|
|
1010
|
+
return this.config.clientId;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
983
1013
|
getHTTPClient(): HTTPClient {
|
|
984
1014
|
return this.httpClient;
|
|
985
1015
|
}
|
|
@@ -1020,6 +1050,8 @@ export interface MockifyerInstance extends HTTPClient {
|
|
|
1020
1050
|
reloadMockData: (syncFromProject?: boolean) => Promise<void>;
|
|
1021
1051
|
clearStaleCacheEntries: () => number;
|
|
1022
1052
|
clearAllMocks: () => Promise<void>;
|
|
1053
|
+
setClientId: (lane: string) => void;
|
|
1054
|
+
getClientId: () => string | undefined;
|
|
1023
1055
|
}
|
|
1024
1056
|
|
|
1025
1057
|
export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
@@ -1178,7 +1210,11 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1178
1210
|
await mockifyer.reloadMockData(syncFromProject);
|
|
1179
1211
|
extendedClient.clearStaleCacheEntries = () => mockifyer.clearStaleCacheEntries();
|
|
1180
1212
|
extendedClient.clearAllMocks = () => mockifyer.clearAllMocks();
|
|
1181
|
-
|
|
1213
|
+
extendedClient.setClientId = (lane: string) => mockifyer.setClientId(lane);
|
|
1214
|
+
extendedClient.getClientId = () => mockifyer.getClientId();
|
|
1215
|
+
|
|
1216
|
+
registerMockifyerInstance(extendedClient);
|
|
1217
|
+
|
|
1182
1218
|
return extendedClient;
|
|
1183
1219
|
}
|
|
1184
1220
|
|
package/src/react-native.ts
CHANGED
|
@@ -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
|
/**
|
|
@@ -337,5 +339,12 @@ export async function setupMockifyerForReactNative(
|
|
|
337
339
|
}
|
|
338
340
|
}
|
|
339
341
|
|
|
340
|
-
export {
|
|
342
|
+
export {
|
|
343
|
+
tryGetClientIdFromLaunchArguments,
|
|
344
|
+
MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY,
|
|
345
|
+
getClientId,
|
|
346
|
+
setClientId,
|
|
347
|
+
registerMockifyerInstance,
|
|
348
|
+
clearMockifyerClientIdRuntime,
|
|
349
|
+
} from '@sgedda/mockifyer-core';
|
|
341
350
|
|