@sgedda/mockifyer-fetch 1.8.41 → 1.8.43
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/index.js +38 -17
- package/package.json +2 -2
- package/src/index.ts +52 -18
package/dist/index.js
CHANGED
|
@@ -46,27 +46,35 @@ class MockifyerClass {
|
|
|
46
46
|
return;
|
|
47
47
|
const scenario = this.config.proxy?.scenario?.trim() ||
|
|
48
48
|
(0, mockifyer_core_1.getCurrentScenario)(this.config.mockDataPath);
|
|
49
|
+
const { requestBody, responseBody, ...eventPartial } = partial;
|
|
50
|
+
const businessResponseBody = (0, mockifyer_core_1.getInlineTraceEnvelopeBusinessBody)(responseBody);
|
|
49
51
|
(0, mockifyer_core_1.recordInlineTraceHopFromExchange)({
|
|
50
|
-
method:
|
|
51
|
-
url:
|
|
52
|
-
status:
|
|
53
|
-
source:
|
|
54
|
-
transport:
|
|
55
|
-
requestId: correlation?.requestId ??
|
|
56
|
-
parentRequestId: correlation?.parentRequestId ??
|
|
57
|
-
durationMs:
|
|
58
|
-
clientId: this.config.clientId ??
|
|
59
|
-
errorMessage:
|
|
52
|
+
method: eventPartial.method,
|
|
53
|
+
url: eventPartial.url,
|
|
54
|
+
status: eventPartial.status,
|
|
55
|
+
source: eventPartial.source,
|
|
56
|
+
transport: eventPartial.transport ?? 'fetch',
|
|
57
|
+
requestId: correlation?.requestId ?? eventPartial.requestId ?? null,
|
|
58
|
+
parentRequestId: correlation?.parentRequestId ?? eventPartial.parentRequestId ?? null,
|
|
59
|
+
durationMs: eventPartial.durationMs,
|
|
60
|
+
clientId: this.config.clientId ?? eventPartial.clientId ?? null,
|
|
61
|
+
errorMessage: eventPartial.errorMessage,
|
|
62
|
+
requestBody,
|
|
63
|
+
responseBody,
|
|
60
64
|
});
|
|
61
65
|
(0, mockifyer_core_1.emitMockifyerNetworkEvent)({
|
|
62
66
|
config: this.config,
|
|
63
67
|
scenario,
|
|
64
68
|
clientId: this.config.clientId,
|
|
65
69
|
event: {
|
|
66
|
-
...
|
|
67
|
-
transport:
|
|
68
|
-
requestId: correlation?.requestId ??
|
|
69
|
-
parentRequestId: correlation?.parentRequestId ??
|
|
70
|
+
...eventPartial,
|
|
71
|
+
transport: eventPartial.transport ?? 'fetch',
|
|
72
|
+
requestId: correlation?.requestId ?? eventPartial.requestId,
|
|
73
|
+
parentRequestId: correlation?.parentRequestId ?? eventPartial.parentRequestId,
|
|
74
|
+
requestBodyPreview: eventPartial.requestBodyPreview ??
|
|
75
|
+
(requestBody !== undefined ? (0, mockifyer_core_1.toNetworkLogBodyPreview)(requestBody) : undefined),
|
|
76
|
+
responseBodyPreview: eventPartial.responseBodyPreview ??
|
|
77
|
+
(responseBody !== undefined ? (0, mockifyer_core_1.toNetworkLogBodyPreview)(businessResponseBody) : undefined),
|
|
70
78
|
},
|
|
71
79
|
});
|
|
72
80
|
}
|
|
@@ -557,12 +565,15 @@ class MockifyerClass {
|
|
|
557
565
|
if ((0, mockifyer_core_1.mockShouldServeStoredBody)(mockData)) {
|
|
558
566
|
mockifyer_core_2.logger.info(`[Mockifyer-Fetch] Mock hit: ${request.method} ${request.url} → ${filename}` +
|
|
559
567
|
(filePath ? ` (${filePath})` : ''));
|
|
568
|
+
const mockResponseBody = this.prepareStoredResponseBody(mockData);
|
|
560
569
|
this.logNetworkEvent({
|
|
561
570
|
method: (request.method || 'GET').toUpperCase(),
|
|
562
571
|
url: request.url,
|
|
563
572
|
source: 'mock-hit',
|
|
564
573
|
status: mockData.response.status,
|
|
565
574
|
requestHash: (0, mockifyer_core_1.networkEventHashFromRequestKey)(requestKey),
|
|
575
|
+
requestBody: request.data,
|
|
576
|
+
responseBody: mockResponseBody,
|
|
566
577
|
}, correlation);
|
|
567
578
|
const responseHeaders = {
|
|
568
579
|
...mockData.response.headers,
|
|
@@ -573,7 +584,7 @@ class MockifyerClass {
|
|
|
573
584
|
};
|
|
574
585
|
await this.warmPoolResponseCache(mockData);
|
|
575
586
|
const mockResponse = {
|
|
576
|
-
data:
|
|
587
|
+
data: mockResponseBody,
|
|
577
588
|
status: mockData.response.status,
|
|
578
589
|
statusText: 'OK',
|
|
579
590
|
headers: responseHeaders,
|
|
@@ -715,6 +726,8 @@ class MockifyerClass {
|
|
|
715
726
|
source: 'upstream',
|
|
716
727
|
status: response.status,
|
|
717
728
|
durationMs,
|
|
729
|
+
requestBody: response.config?.data,
|
|
730
|
+
responseBody: response.data,
|
|
718
731
|
}, this.readRequestCorrelation(response.config));
|
|
719
732
|
response.data = (0, mockifyer_core_1.unwrapAndMergeInlineTraceEnvelope)(response.data);
|
|
720
733
|
const capturedResponse = {
|
|
@@ -741,6 +754,8 @@ class MockifyerClass {
|
|
|
741
754
|
durationMs: typeof response.config?.metadata?.durationMs === 'number'
|
|
742
755
|
? response.config.metadata.durationMs
|
|
743
756
|
: undefined,
|
|
757
|
+
requestBody: response.config?.data,
|
|
758
|
+
responseBody: response.data,
|
|
744
759
|
}, this.readRequestCorrelation(response.config));
|
|
745
760
|
response.data = (0, mockifyer_core_1.unwrapAndMergeInlineTraceEnvelope)(response.data);
|
|
746
761
|
// Only save locally if recordMode is enabled AND we're not proxying upstream calls.
|
|
@@ -762,6 +777,8 @@ class MockifyerClass {
|
|
|
762
777
|
source: 'error',
|
|
763
778
|
status: error.response?.status,
|
|
764
779
|
errorMessage: error?.message ?? String(error),
|
|
780
|
+
requestBody: error.config?.data,
|
|
781
|
+
responseBody: error.response?.data,
|
|
765
782
|
}, this.readRequestCorrelation(error.config));
|
|
766
783
|
}
|
|
767
784
|
throw error;
|
|
@@ -1276,8 +1293,12 @@ function setupMockifyer(config) {
|
|
|
1276
1293
|
const method = init?.method || 'GET';
|
|
1277
1294
|
const headers = init?.headers || {};
|
|
1278
1295
|
const body = init?.body;
|
|
1279
|
-
// Skip Mockifyer sync endpoints and Resend API
|
|
1280
|
-
if (url.includes('/mockifyer-save') ||
|
|
1296
|
+
// Skip Mockifyer sync endpoints, dashboard `/api/proxy` plumbing, and Resend API
|
|
1297
|
+
if (url.includes('/mockifyer-save') ||
|
|
1298
|
+
url.includes('/mockifyer-clear') ||
|
|
1299
|
+
url.includes('/mockifyer-sync') ||
|
|
1300
|
+
url.includes('api.resend.com') ||
|
|
1301
|
+
(0, mockifyer_core_1.isMockifyerDashboardProxyApiUrl)(url)) {
|
|
1281
1302
|
return await originalFetchForPatched(input, init);
|
|
1282
1303
|
}
|
|
1283
1304
|
const simpleRequestKey = `${method}:${url}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.43",
|
|
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",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
},
|
|
50
50
|
"license": "MIT",
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@sgedda/mockifyer-core": "^1.10.
|
|
52
|
+
"@sgedda/mockifyer-core": "^1.10.25"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^20.11.24",
|
package/src/index.ts
CHANGED
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
isUsableNodeLikePoolFs,
|
|
43
43
|
type PoolResponseItem,
|
|
44
44
|
shouldBypassMockifyerForUrl,
|
|
45
|
+
isMockifyerDashboardProxyApiUrl,
|
|
45
46
|
resolveRecordingExclusions,
|
|
46
47
|
shouldExcludeRecording,
|
|
47
48
|
mockPassesThroughToRealApi,
|
|
@@ -70,6 +71,8 @@ import {
|
|
|
70
71
|
networkEventHashFromRequestKey,
|
|
71
72
|
recordInlineTraceHopFromExchange,
|
|
72
73
|
unwrapAndMergeInlineTraceEnvelope,
|
|
74
|
+
toNetworkLogBodyPreview,
|
|
75
|
+
getInlineTraceEnvelopeBusinessBody,
|
|
73
76
|
resolveRecordResponses,
|
|
74
77
|
applyOutboundRequestCorrelation,
|
|
75
78
|
type RequestCorrelationContext,
|
|
@@ -115,7 +118,12 @@ class MockifyerClass {
|
|
|
115
118
|
|
|
116
119
|
/** Best-effort dashboard network log (skipped when traffic goes through `proxy.baseUrl`). */
|
|
117
120
|
private logNetworkEvent(
|
|
118
|
-
partial: Parameters<typeof emitMockifyerNetworkEvent>[0]['event'] & {
|
|
121
|
+
partial: Parameters<typeof emitMockifyerNetworkEvent>[0]['event'] & {
|
|
122
|
+
transport?: 'fetch';
|
|
123
|
+
/** Raw bodies for inline-trace / network-log previews (stripped before emit). */
|
|
124
|
+
requestBody?: unknown;
|
|
125
|
+
responseBody?: unknown;
|
|
126
|
+
},
|
|
119
127
|
correlation?: RequestCorrelationContext
|
|
120
128
|
): void {
|
|
121
129
|
// Dashboard proxy hops are recorded in performDashboardProxyRequest (shared axios/fetch path).
|
|
@@ -125,17 +133,22 @@ class MockifyerClass {
|
|
|
125
133
|
this.config.proxy?.scenario?.trim() ||
|
|
126
134
|
getCurrentScenario(this.config.mockDataPath);
|
|
127
135
|
|
|
136
|
+
const { requestBody, responseBody, ...eventPartial } = partial;
|
|
137
|
+
const businessResponseBody = getInlineTraceEnvelopeBusinessBody(responseBody);
|
|
138
|
+
|
|
128
139
|
recordInlineTraceHopFromExchange({
|
|
129
|
-
method:
|
|
130
|
-
url:
|
|
131
|
-
status:
|
|
132
|
-
source:
|
|
133
|
-
transport:
|
|
134
|
-
requestId: correlation?.requestId ??
|
|
135
|
-
parentRequestId: correlation?.parentRequestId ??
|
|
136
|
-
durationMs:
|
|
137
|
-
clientId: this.config.clientId ??
|
|
138
|
-
errorMessage:
|
|
140
|
+
method: eventPartial.method,
|
|
141
|
+
url: eventPartial.url,
|
|
142
|
+
status: eventPartial.status,
|
|
143
|
+
source: eventPartial.source,
|
|
144
|
+
transport: eventPartial.transport ?? 'fetch',
|
|
145
|
+
requestId: correlation?.requestId ?? eventPartial.requestId ?? null,
|
|
146
|
+
parentRequestId: correlation?.parentRequestId ?? eventPartial.parentRequestId ?? null,
|
|
147
|
+
durationMs: eventPartial.durationMs,
|
|
148
|
+
clientId: this.config.clientId ?? eventPartial.clientId ?? null,
|
|
149
|
+
errorMessage: eventPartial.errorMessage,
|
|
150
|
+
requestBody,
|
|
151
|
+
responseBody,
|
|
139
152
|
});
|
|
140
153
|
|
|
141
154
|
emitMockifyerNetworkEvent({
|
|
@@ -143,10 +156,16 @@ class MockifyerClass {
|
|
|
143
156
|
scenario,
|
|
144
157
|
clientId: this.config.clientId,
|
|
145
158
|
event: {
|
|
146
|
-
...
|
|
147
|
-
transport:
|
|
148
|
-
requestId: correlation?.requestId ??
|
|
149
|
-
parentRequestId: correlation?.parentRequestId ??
|
|
159
|
+
...eventPartial,
|
|
160
|
+
transport: eventPartial.transport ?? 'fetch',
|
|
161
|
+
requestId: correlation?.requestId ?? eventPartial.requestId,
|
|
162
|
+
parentRequestId: correlation?.parentRequestId ?? eventPartial.parentRequestId,
|
|
163
|
+
requestBodyPreview:
|
|
164
|
+
eventPartial.requestBodyPreview ??
|
|
165
|
+
(requestBody !== undefined ? toNetworkLogBodyPreview(requestBody) : undefined),
|
|
166
|
+
responseBodyPreview:
|
|
167
|
+
eventPartial.responseBodyPreview ??
|
|
168
|
+
(responseBody !== undefined ? toNetworkLogBodyPreview(businessResponseBody) : undefined),
|
|
150
169
|
},
|
|
151
170
|
});
|
|
152
171
|
}
|
|
@@ -725,6 +744,7 @@ class MockifyerClass {
|
|
|
725
744
|
`[Mockifyer-Fetch] Mock hit: ${request.method} ${request.url} → ${filename}` +
|
|
726
745
|
(filePath ? ` (${filePath})` : '')
|
|
727
746
|
);
|
|
747
|
+
const mockResponseBody = this.prepareStoredResponseBody(mockData);
|
|
728
748
|
this.logNetworkEvent(
|
|
729
749
|
{
|
|
730
750
|
method: (request.method || 'GET').toUpperCase(),
|
|
@@ -732,6 +752,8 @@ class MockifyerClass {
|
|
|
732
752
|
source: 'mock-hit',
|
|
733
753
|
status: mockData.response.status,
|
|
734
754
|
requestHash: networkEventHashFromRequestKey(requestKey),
|
|
755
|
+
requestBody: request.data,
|
|
756
|
+
responseBody: mockResponseBody,
|
|
735
757
|
},
|
|
736
758
|
correlation
|
|
737
759
|
);
|
|
@@ -746,7 +768,7 @@ class MockifyerClass {
|
|
|
746
768
|
await this.warmPoolResponseCache(mockData);
|
|
747
769
|
|
|
748
770
|
const mockResponse = {
|
|
749
|
-
data:
|
|
771
|
+
data: mockResponseBody,
|
|
750
772
|
status: mockData.response.status,
|
|
751
773
|
statusText: 'OK',
|
|
752
774
|
headers: responseHeaders,
|
|
@@ -915,6 +937,8 @@ class MockifyerClass {
|
|
|
915
937
|
source: 'upstream',
|
|
916
938
|
status: response.status,
|
|
917
939
|
durationMs,
|
|
940
|
+
requestBody: response.config?.data,
|
|
941
|
+
responseBody: response.data,
|
|
918
942
|
},
|
|
919
943
|
this.readRequestCorrelation(response.config)
|
|
920
944
|
);
|
|
@@ -954,6 +978,8 @@ class MockifyerClass {
|
|
|
954
978
|
typeof response.config?.metadata?.durationMs === 'number'
|
|
955
979
|
? response.config.metadata.durationMs
|
|
956
980
|
: undefined,
|
|
981
|
+
requestBody: response.config?.data,
|
|
982
|
+
responseBody: response.data,
|
|
957
983
|
},
|
|
958
984
|
this.readRequestCorrelation(response.config)
|
|
959
985
|
);
|
|
@@ -981,6 +1007,8 @@ class MockifyerClass {
|
|
|
981
1007
|
source: 'error',
|
|
982
1008
|
status: error.response?.status,
|
|
983
1009
|
errorMessage: error?.message ?? String(error),
|
|
1010
|
+
requestBody: error.config?.data,
|
|
1011
|
+
responseBody: error.response?.data,
|
|
984
1012
|
},
|
|
985
1013
|
this.readRequestCorrelation(error.config)
|
|
986
1014
|
);
|
|
@@ -1596,8 +1624,14 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1596
1624
|
const headers = init?.headers || {};
|
|
1597
1625
|
const body = init?.body;
|
|
1598
1626
|
|
|
1599
|
-
// Skip Mockifyer sync endpoints and Resend API
|
|
1600
|
-
if (
|
|
1627
|
+
// Skip Mockifyer sync endpoints, dashboard `/api/proxy` plumbing, and Resend API
|
|
1628
|
+
if (
|
|
1629
|
+
url.includes('/mockifyer-save') ||
|
|
1630
|
+
url.includes('/mockifyer-clear') ||
|
|
1631
|
+
url.includes('/mockifyer-sync') ||
|
|
1632
|
+
url.includes('api.resend.com') ||
|
|
1633
|
+
isMockifyerDashboardProxyApiUrl(url)
|
|
1634
|
+
) {
|
|
1601
1635
|
return await originalFetchForPatched(input, init);
|
|
1602
1636
|
}
|
|
1603
1637
|
|