@sgedda/mockifyer-fetch 1.8.13 → 1.8.16
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/dist/clients/fetch-client.d.ts +2 -0
- package/dist/clients/fetch-client.js +16 -0
- package/dist/index.js +1 -0
- package/package.json +2 -2
- package/src/clients/fetch-client.ts +21 -0
- package/src/index.ts +1 -0
|
@@ -7,6 +7,7 @@ export declare class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<an
|
|
|
7
7
|
private proxyScenario?;
|
|
8
8
|
private proxyRecordOnMiss;
|
|
9
9
|
private clientId?;
|
|
10
|
+
private deviceId?;
|
|
10
11
|
constructor(config?: {
|
|
11
12
|
baseUrl?: string;
|
|
12
13
|
defaultHeaders?: Record<string, string>;
|
|
@@ -16,6 +17,7 @@ export declare class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<an
|
|
|
16
17
|
recordOnMiss?: boolean;
|
|
17
18
|
};
|
|
18
19
|
clientId?: string;
|
|
20
|
+
deviceId?: string;
|
|
19
21
|
});
|
|
20
22
|
protected performRequest<D = any>(config: HTTPRequestConfig<D>): Promise<HTTPResponse<any>>;
|
|
21
23
|
protected getDefaultHeaders(): Record<string, string>;
|
|
@@ -11,6 +11,7 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
|
|
|
11
11
|
this.proxyScenario = config?.proxy?.scenario;
|
|
12
12
|
this.proxyRecordOnMiss = config?.proxy?.recordOnMiss ?? false;
|
|
13
13
|
this.clientId = config?.clientId;
|
|
14
|
+
this.deviceId = config?.deviceId;
|
|
14
15
|
}
|
|
15
16
|
async performRequest(config) {
|
|
16
17
|
// Check if this is a mock response (set by Mockifyer interceptor)
|
|
@@ -60,11 +61,13 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
|
|
|
60
61
|
headers: {
|
|
61
62
|
'content-type': 'application/json',
|
|
62
63
|
...(this.clientId ? { 'x-mockifyer-client-id': this.clientId } : {}),
|
|
64
|
+
...(this.deviceId ? { 'x-mockifyer-device-id': this.deviceId } : {}),
|
|
63
65
|
},
|
|
64
66
|
body: JSON.stringify({
|
|
65
67
|
url,
|
|
66
68
|
method: requestConfig.method,
|
|
67
69
|
clientId: this.clientId,
|
|
70
|
+
deviceId: this.deviceId,
|
|
68
71
|
headers: (() => {
|
|
69
72
|
const out = {};
|
|
70
73
|
headers.forEach((value, key) => {
|
|
@@ -82,6 +85,19 @@ class FetchHTTPClient extends mockifyer_core_1.BaseHTTPClient {
|
|
|
82
85
|
throw new Error(`[FetchHTTPClient] Proxy error: ${proxyResponse.status} ${txt}`);
|
|
83
86
|
}
|
|
84
87
|
const payload = await proxyResponse.json();
|
|
88
|
+
try {
|
|
89
|
+
const source = String(payload?.source || '');
|
|
90
|
+
const hash = typeof payload?.hash === 'string' ? payload.hash : '';
|
|
91
|
+
if (source) {
|
|
92
|
+
const hashShort = hash ? `${hash.slice(0, 8)}…` : '';
|
|
93
|
+
const lane = this.clientId ? this.clientId : '—';
|
|
94
|
+
const kind = source === 'redis' ? 'mock hit' : 'upstream';
|
|
95
|
+
console.log(`[Mockifyer-Fetch] Proxy ${kind}: ${requestConfig.method} ${url} ${hashShort ? `(hash=${hashShort}) ` : ''}(lane=${lane})`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// ignore logging failures
|
|
100
|
+
}
|
|
85
101
|
const data = payload?.response?.data;
|
|
86
102
|
const status = payload?.response?.status ?? 200;
|
|
87
103
|
const responseHeaders = payload?.response?.headers ?? {};
|
package/dist/index.js
CHANGED
|
@@ -84,6 +84,7 @@ class MockifyerClass {
|
|
|
84
84
|
this.config = { ...config }; // Create a copy to avoid mutations
|
|
85
85
|
// Resolve client lane id (env/config/fallback); used for Redis per-client scenario overrides and proxy headers.
|
|
86
86
|
this.config.clientId = (0, mockifyer_core_1.resolveClientId)(this.config);
|
|
87
|
+
mockifyer_core_2.logger.info(`[Mockifyer-Fetch] clientId: ${this.config.clientId}`);
|
|
87
88
|
// Initialize test generator if test generation is enabled
|
|
88
89
|
if (config.generateTests?.enabled) {
|
|
89
90
|
this.testGenerator = new mockifyer_core_1.TestGenerator();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.16",
|
|
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.
|
|
49
|
+
"@sgedda/mockifyer-core": "^1.8.22"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/node": "^20.11.24",
|
|
@@ -8,12 +8,14 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
8
8
|
private proxyScenario?: string;
|
|
9
9
|
private proxyRecordOnMiss: boolean;
|
|
10
10
|
private clientId?: string;
|
|
11
|
+
private deviceId?: string;
|
|
11
12
|
|
|
12
13
|
constructor(config?: {
|
|
13
14
|
baseUrl?: string;
|
|
14
15
|
defaultHeaders?: Record<string, string>;
|
|
15
16
|
proxy?: { baseUrl: string; scenario?: string; recordOnMiss?: boolean };
|
|
16
17
|
clientId?: string;
|
|
18
|
+
deviceId?: string;
|
|
17
19
|
}) {
|
|
18
20
|
super();
|
|
19
21
|
this.baseUrl = config?.baseUrl;
|
|
@@ -22,6 +24,7 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
22
24
|
this.proxyScenario = config?.proxy?.scenario;
|
|
23
25
|
this.proxyRecordOnMiss = config?.proxy?.recordOnMiss ?? false;
|
|
24
26
|
this.clientId = config?.clientId;
|
|
27
|
+
this.deviceId = config?.deviceId;
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
protected async performRequest<D = any>(config: HTTPRequestConfig<D>): Promise<HTTPResponse<any>> {
|
|
@@ -77,11 +80,13 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
77
80
|
headers: {
|
|
78
81
|
'content-type': 'application/json',
|
|
79
82
|
...(this.clientId ? { 'x-mockifyer-client-id': this.clientId } : {}),
|
|
83
|
+
...(this.deviceId ? { 'x-mockifyer-device-id': this.deviceId } : {}),
|
|
80
84
|
},
|
|
81
85
|
body: JSON.stringify({
|
|
82
86
|
url,
|
|
83
87
|
method: requestConfig.method,
|
|
84
88
|
clientId: this.clientId,
|
|
89
|
+
deviceId: this.deviceId,
|
|
85
90
|
headers: (() => {
|
|
86
91
|
const out: Record<string, string> = {};
|
|
87
92
|
headers.forEach((value, key) => {
|
|
@@ -99,6 +104,22 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
99
104
|
throw new Error(`[FetchHTTPClient] Proxy error: ${proxyResponse.status} ${txt}`);
|
|
100
105
|
}
|
|
101
106
|
const payload = await proxyResponse.json();
|
|
107
|
+
try {
|
|
108
|
+
const source = String(payload?.source || '');
|
|
109
|
+
const hash = typeof payload?.hash === 'string' ? payload.hash : '';
|
|
110
|
+
if (source) {
|
|
111
|
+
const hashShort = hash ? `${hash.slice(0, 8)}…` : '';
|
|
112
|
+
const lane = this.clientId ? this.clientId : '—';
|
|
113
|
+
const kind = source === 'redis' ? 'mock hit' : 'upstream';
|
|
114
|
+
console.log(
|
|
115
|
+
`[Mockifyer-Fetch] Proxy ${kind}: ${requestConfig.method} ${url} ${
|
|
116
|
+
hashShort ? `(hash=${hashShort}) ` : ''
|
|
117
|
+
}(lane=${lane})`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
} catch {
|
|
121
|
+
// ignore logging failures
|
|
122
|
+
}
|
|
102
123
|
const data = payload?.response?.data;
|
|
103
124
|
const status = payload?.response?.status ?? 200;
|
|
104
125
|
const responseHeaders: Record<string, string> = payload?.response?.headers ?? {};
|
package/src/index.ts
CHANGED
|
@@ -111,6 +111,7 @@ class MockifyerClass {
|
|
|
111
111
|
|
|
112
112
|
// Resolve client lane id (env/config/fallback); used for Redis per-client scenario overrides and proxy headers.
|
|
113
113
|
this.config.clientId = resolveClientId(this.config);
|
|
114
|
+
logger.info(`[Mockifyer-Fetch] clientId: ${this.config.clientId}`);
|
|
114
115
|
|
|
115
116
|
// Initialize test generator if test generation is enabled
|
|
116
117
|
if (config.generateTests?.enabled) {
|