@sgedda/mockifyer-fetch 1.8.21 → 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 +21 -0
- package/dist/clients/fetch-client.d.ts +4 -1
- package/dist/clients/fetch-client.js +23 -8
- package/dist/index.d.ts +2 -0
- package/dist/index.js +25 -0
- package/dist/react-native.d.ts +2 -0
- package/dist/react-native.js +2 -1
- package/dist/utils/join-proxy-dashboard-api-url.d.ts +11 -0
- package/dist/utils/join-proxy-dashboard-api-url.js +18 -0
- package/package.json +6 -3
- package/src/clients/fetch-client.ts +28 -9
- package/src/index.ts +33 -0
- package/src/react-native.ts +4 -1
- package/src/utils/join-proxy-dashboard-api-url.ts +15 -0
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>;
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FetchHTTPClient = void 0;
|
|
4
4
|
const mockifyer_core_1 = require("@sgedda/mockifyer-core");
|
|
5
5
|
const mockifyer_core_2 = require("@sgedda/mockifyer-core");
|
|
6
|
+
const join_proxy_dashboard_api_url_1 = require("../utils/join-proxy-dashboard-api-url");
|
|
6
7
|
class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
|
|
7
8
|
constructor(config) {
|
|
8
9
|
super();
|
|
@@ -11,9 +12,22 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
|
|
|
11
12
|
this.proxyBaseUrl = config?.proxy?.baseUrl;
|
|
12
13
|
this.proxyScenario = config?.proxy?.scenario;
|
|
13
14
|
this.proxyRecordOnMiss = config?.proxy?.recordOnMiss ?? false;
|
|
14
|
-
this.
|
|
15
|
+
this.getClientId = config?.getClientId;
|
|
16
|
+
this.clientIdSnapshot = config?.clientId;
|
|
15
17
|
this.deviceId = config?.deviceId;
|
|
16
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
|
+
}
|
|
17
31
|
async performRequest(config) {
|
|
18
32
|
// Check if this is a mock response (set by Mockifyer interceptor)
|
|
19
33
|
if (config.__mockResponse) {
|
|
@@ -54,20 +68,21 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
|
|
|
54
68
|
if (!this._originalFetch) {
|
|
55
69
|
console.warn('[FetchHTTPClient] _originalFetch not set! This may cause infinite loops if global fetch is patched.');
|
|
56
70
|
}
|
|
71
|
+
const lane = this.resolvedClientLane();
|
|
57
72
|
// Proxy mode (e.g. React Native → dashboard → Redis)
|
|
58
73
|
if (this.proxyBaseUrl) {
|
|
59
|
-
const proxyUrl =
|
|
74
|
+
const proxyUrl = (0, join_proxy_dashboard_api_url_1.joinProxyDashboardApiUrl)(this.proxyBaseUrl, 'api/proxy');
|
|
60
75
|
const proxyResponse = await fetchFn(proxyUrl, {
|
|
61
76
|
method: 'POST',
|
|
62
77
|
headers: {
|
|
63
78
|
'content-type': 'application/json',
|
|
64
|
-
...(
|
|
79
|
+
...(lane ? { [mockifyer_core_2.MOCKIFYER_CLIENT_ID_HEADER]: lane } : {}),
|
|
65
80
|
...(this.deviceId ? { [mockifyer_core_2.MOCKIFYER_DEVICE_ID_HEADER]: this.deviceId } : {}),
|
|
66
81
|
},
|
|
67
82
|
body: JSON.stringify({
|
|
68
83
|
url,
|
|
69
84
|
method: requestConfig.method,
|
|
70
|
-
clientId:
|
|
85
|
+
clientId: lane,
|
|
71
86
|
deviceId: this.deviceId,
|
|
72
87
|
headers: (() => {
|
|
73
88
|
const out = {};
|
|
@@ -91,9 +106,9 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
|
|
|
91
106
|
const hash = typeof payload?.hash === 'string' ? payload.hash : '';
|
|
92
107
|
if (source) {
|
|
93
108
|
const hashShort = hash ? `${hash.slice(0, 8)}…` : '';
|
|
94
|
-
const
|
|
109
|
+
const laneLabel = lane ? lane : '—';
|
|
95
110
|
const kind = source === 'redis' ? 'mock hit' : 'upstream';
|
|
96
|
-
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})`);
|
|
97
112
|
}
|
|
98
113
|
}
|
|
99
114
|
catch {
|
|
@@ -110,8 +125,8 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
|
|
|
110
125
|
config,
|
|
111
126
|
};
|
|
112
127
|
}
|
|
113
|
-
if (
|
|
114
|
-
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);
|
|
115
130
|
}
|
|
116
131
|
if (this.deviceId && String(this.deviceId).trim() && !(0, mockifyer_core_2.getOutboundMockifyerDeviceIdHeader)(headers)) {
|
|
117
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
|
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}.
|
package/dist/react-native.js
CHANGED
|
@@ -9,6 +9,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
exports.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY = exports.tryGetClientIdFromLaunchArguments = void 0;
|
|
10
10
|
exports.isMockifyerReactNativeActive = isMockifyerReactNativeActive;
|
|
11
11
|
exports.setupMockifyerForReactNative = setupMockifyerForReactNative;
|
|
12
|
+
const join_proxy_dashboard_api_url_1 = require("./utils/join-proxy-dashboard-api-url");
|
|
12
13
|
const index_1 = require("./index");
|
|
13
14
|
const mockifyer_core_1 = require("@sgedda/mockifyer-core");
|
|
14
15
|
const mockifyer_core_2 = require("@sgedda/mockifyer-core");
|
|
@@ -26,7 +27,7 @@ async function canUseStrictRedisProxy(proxyBaseUrl) {
|
|
|
26
27
|
try {
|
|
27
28
|
const controller = typeof AbortController !== 'undefined' ? new AbortController() : undefined;
|
|
28
29
|
const timeout = setTimeout(() => controller?.abort(), 800);
|
|
29
|
-
const url =
|
|
30
|
+
const url = (0, join_proxy_dashboard_api_url_1.joinProxyDashboardApiUrl)(proxyBaseUrl, 'api/health');
|
|
30
31
|
const res = await fetch(url, { signal: controller?.signal });
|
|
31
32
|
clearTimeout(timeout);
|
|
32
33
|
if (!res.ok)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds a full URL to the Mockifyer dashboard HTTP API under a given `proxyBaseUrl`.
|
|
3
|
+
*
|
|
4
|
+
* `proxyBaseUrl` may include a path prefix (e.g. `https://host/apim-prefix/mockifyer/`).
|
|
5
|
+
* Avoid `new URL('/api/...', proxyBaseUrl)` — a leading `/` on the second argument replaces the
|
|
6
|
+
* entire pathname of the base URL, so the gateway prefix is lost.
|
|
7
|
+
*
|
|
8
|
+
* @param proxyBaseUrl - Origin and optional path prefix (with or without trailing slash)
|
|
9
|
+
* @param apiSubPath - Path under that prefix without a leading slash (e.g. `api/health`, `api/proxy`)
|
|
10
|
+
*/
|
|
11
|
+
export declare function joinProxyDashboardApiUrl(proxyBaseUrl: string, apiSubPath: string): string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.joinProxyDashboardApiUrl = joinProxyDashboardApiUrl;
|
|
4
|
+
/**
|
|
5
|
+
* Builds a full URL to the Mockifyer dashboard HTTP API under a given `proxyBaseUrl`.
|
|
6
|
+
*
|
|
7
|
+
* `proxyBaseUrl` may include a path prefix (e.g. `https://host/apim-prefix/mockifyer/`).
|
|
8
|
+
* Avoid `new URL('/api/...', proxyBaseUrl)` — a leading `/` on the second argument replaces the
|
|
9
|
+
* entire pathname of the base URL, so the gateway prefix is lost.
|
|
10
|
+
*
|
|
11
|
+
* @param proxyBaseUrl - Origin and optional path prefix (with or without trailing slash)
|
|
12
|
+
* @param apiSubPath - Path under that prefix without a leading slash (e.g. `api/health`, `api/proxy`)
|
|
13
|
+
*/
|
|
14
|
+
function joinProxyDashboardApiUrl(proxyBaseUrl, apiSubPath) {
|
|
15
|
+
const normalizedBase = proxyBaseUrl.trim().replace(/\/+$/, '');
|
|
16
|
+
const normalizedPath = apiSubPath.trim().replace(/^\/+/, '');
|
|
17
|
+
return `${normalizedBase}/${normalizedPath}`;
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
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":
|
|
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.29"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|
|
52
55
|
"@types/node": "^20.11.24",
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
MOCKIFYER_CLIENT_ID_HEADER,
|
|
7
7
|
MOCKIFYER_DEVICE_ID_HEADER,
|
|
8
8
|
} from '@sgedda/mockifyer-core';
|
|
9
|
+
import { joinProxyDashboardApiUrl } from '../utils/join-proxy-dashboard-api-url';
|
|
9
10
|
|
|
10
11
|
export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
11
12
|
private baseUrl?: string;
|
|
@@ -13,7 +14,8 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
13
14
|
private proxyBaseUrl?: string;
|
|
14
15
|
private proxyScenario?: string;
|
|
15
16
|
private proxyRecordOnMiss: boolean;
|
|
16
|
-
private
|
|
17
|
+
private getClientId?: () => string | undefined;
|
|
18
|
+
private clientIdSnapshot?: string;
|
|
17
19
|
private deviceId?: string;
|
|
18
20
|
|
|
19
21
|
constructor(config?: {
|
|
@@ -21,6 +23,7 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
21
23
|
defaultHeaders?: Record<string, string>;
|
|
22
24
|
proxy?: { baseUrl: string; scenario?: string; recordOnMiss?: boolean };
|
|
23
25
|
clientId?: string;
|
|
26
|
+
getClientId?: () => string | undefined;
|
|
24
27
|
deviceId?: string;
|
|
25
28
|
}) {
|
|
26
29
|
super();
|
|
@@ -29,10 +32,24 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
29
32
|
this.proxyBaseUrl = config?.proxy?.baseUrl;
|
|
30
33
|
this.proxyScenario = config?.proxy?.scenario;
|
|
31
34
|
this.proxyRecordOnMiss = config?.proxy?.recordOnMiss ?? false;
|
|
32
|
-
this.
|
|
35
|
+
this.getClientId = config?.getClientId;
|
|
36
|
+
this.clientIdSnapshot = config?.clientId;
|
|
33
37
|
this.deviceId = config?.deviceId;
|
|
34
38
|
}
|
|
35
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
|
+
|
|
36
53
|
protected async performRequest<D = any>(config: HTTPRequestConfig<D>): Promise<HTTPResponse<any>> {
|
|
37
54
|
// Check if this is a mock response (set by Mockifyer interceptor)
|
|
38
55
|
if ((config as any).__mockResponse) {
|
|
@@ -78,20 +95,22 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
78
95
|
console.warn('[FetchHTTPClient] _originalFetch not set! This may cause infinite loops if global fetch is patched.');
|
|
79
96
|
}
|
|
80
97
|
|
|
98
|
+
const lane = this.resolvedClientLane();
|
|
99
|
+
|
|
81
100
|
// Proxy mode (e.g. React Native → dashboard → Redis)
|
|
82
101
|
if (this.proxyBaseUrl) {
|
|
83
|
-
const proxyUrl =
|
|
102
|
+
const proxyUrl = joinProxyDashboardApiUrl(this.proxyBaseUrl, 'api/proxy');
|
|
84
103
|
const proxyResponse = await fetchFn(proxyUrl, {
|
|
85
104
|
method: 'POST',
|
|
86
105
|
headers: {
|
|
87
106
|
'content-type': 'application/json',
|
|
88
|
-
...(
|
|
107
|
+
...(lane ? { [MOCKIFYER_CLIENT_ID_HEADER]: lane } : {}),
|
|
89
108
|
...(this.deviceId ? { [MOCKIFYER_DEVICE_ID_HEADER]: this.deviceId } : {}),
|
|
90
109
|
},
|
|
91
110
|
body: JSON.stringify({
|
|
92
111
|
url,
|
|
93
112
|
method: requestConfig.method,
|
|
94
|
-
clientId:
|
|
113
|
+
clientId: lane,
|
|
95
114
|
deviceId: this.deviceId,
|
|
96
115
|
headers: (() => {
|
|
97
116
|
const out: Record<string, string> = {};
|
|
@@ -115,12 +134,12 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
115
134
|
const hash = typeof payload?.hash === 'string' ? payload.hash : '';
|
|
116
135
|
if (source) {
|
|
117
136
|
const hashShort = hash ? `${hash.slice(0, 8)}…` : '';
|
|
118
|
-
const
|
|
137
|
+
const laneLabel = lane ? lane : '—';
|
|
119
138
|
const kind = source === 'redis' ? 'mock hit' : 'upstream';
|
|
120
139
|
console.log(
|
|
121
140
|
`[Mockifyer-Fetch] Proxy ${kind}: ${requestConfig.method} ${url} ${
|
|
122
141
|
hashShort ? `(hash=${hashShort}) ` : ''
|
|
123
|
-
}(lane=${
|
|
142
|
+
}(lane=${laneLabel})`
|
|
124
143
|
);
|
|
125
144
|
}
|
|
126
145
|
} catch {
|
|
@@ -139,8 +158,8 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
139
158
|
};
|
|
140
159
|
}
|
|
141
160
|
|
|
142
|
-
if (
|
|
143
|
-
headers.set(MOCKIFYER_CLIENT_ID_HEADER,
|
|
161
|
+
if (lane && !getOutboundMockifyerClientIdHeader(headers)) {
|
|
162
|
+
headers.set(MOCKIFYER_CLIENT_ID_HEADER, lane);
|
|
144
163
|
}
|
|
145
164
|
if (this.deviceId && String(this.deviceId).trim() && !getOutboundMockifyerDeviceIdHeader(headers)) {
|
|
146
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
|
}
|
package/src/react-native.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* conditional provider selection (FileSystem in dev, Memory in prod)
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { joinProxyDashboardApiUrl } from './utils/join-proxy-dashboard-api-url';
|
|
8
9
|
import { setupMockifyer } from './index';
|
|
9
10
|
import { MemoryProvider, ExpoFileSystemProvider, MockData, HTTPClient } from '@sgedda/mockifyer-core';
|
|
10
11
|
import {
|
|
@@ -20,6 +21,8 @@ export interface MockifyerInstance extends HTTPClient {
|
|
|
20
21
|
reloadMockData: (syncFromProject?: boolean) => Promise<void>;
|
|
21
22
|
clearStaleCacheEntries: () => number;
|
|
22
23
|
clearAllMocks: () => Promise<void>;
|
|
24
|
+
setClientId: (lane: string) => void;
|
|
25
|
+
getClientId: () => string | undefined;
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
/**
|
|
@@ -100,7 +103,7 @@ async function canUseStrictRedisProxy(proxyBaseUrl?: string): Promise<boolean> {
|
|
|
100
103
|
try {
|
|
101
104
|
const controller = typeof AbortController !== 'undefined' ? new AbortController() : undefined;
|
|
102
105
|
const timeout = setTimeout(() => controller?.abort(), 800);
|
|
103
|
-
const url =
|
|
106
|
+
const url = joinProxyDashboardApiUrl(proxyBaseUrl, 'api/health');
|
|
104
107
|
const res = await fetch(url, { signal: controller?.signal });
|
|
105
108
|
clearTimeout(timeout);
|
|
106
109
|
if (!res.ok) return false;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds a full URL to the Mockifyer dashboard HTTP API under a given `proxyBaseUrl`.
|
|
3
|
+
*
|
|
4
|
+
* `proxyBaseUrl` may include a path prefix (e.g. `https://host/apim-prefix/mockifyer/`).
|
|
5
|
+
* Avoid `new URL('/api/...', proxyBaseUrl)` — a leading `/` on the second argument replaces the
|
|
6
|
+
* entire pathname of the base URL, so the gateway prefix is lost.
|
|
7
|
+
*
|
|
8
|
+
* @param proxyBaseUrl - Origin and optional path prefix (with or without trailing slash)
|
|
9
|
+
* @param apiSubPath - Path under that prefix without a leading slash (e.g. `api/health`, `api/proxy`)
|
|
10
|
+
*/
|
|
11
|
+
export function joinProxyDashboardApiUrl(proxyBaseUrl: string, apiSubPath: string): string {
|
|
12
|
+
const normalizedBase = proxyBaseUrl.trim().replace(/\/+$/, '');
|
|
13
|
+
const normalizedPath = apiSubPath.trim().replace(/^\/+/, '');
|
|
14
|
+
return `${normalizedBase}/${normalizedPath}`;
|
|
15
|
+
}
|