@sgedda/mockifyer-fetch 1.8.40 → 1.8.42
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 +51 -31
- package/package.json +2 -2
- package/src/index.ts +69 -36
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,
|
|
@@ -704,13 +715,26 @@ class MockifyerClass {
|
|
|
704
715
|
}
|
|
705
716
|
const matchedMock = response.config.__mockifyer_matchedMock;
|
|
706
717
|
if (matchedMock) {
|
|
718
|
+
const startTime = response.config.__mockifyer_startTime;
|
|
719
|
+
const durationMs = startTime ? Date.now() - startTime : undefined;
|
|
720
|
+
const reqUrl = response.config?.url || url;
|
|
721
|
+
const reqMethod = (response.config?.method || 'GET').toUpperCase();
|
|
722
|
+
// Parent hop first, then unwrap nested mockifyerTrace from downstream services.
|
|
723
|
+
this.logNetworkEvent({
|
|
724
|
+
method: reqMethod,
|
|
725
|
+
url: reqUrl,
|
|
726
|
+
source: 'upstream',
|
|
727
|
+
status: response.status,
|
|
728
|
+
durationMs,
|
|
729
|
+
requestBody: response.config?.data,
|
|
730
|
+
responseBody: response.data,
|
|
731
|
+
}, this.readRequestCorrelation(response.config));
|
|
732
|
+
response.data = (0, mockifyer_core_1.unwrapAndMergeInlineTraceEnvelope)(response.data);
|
|
707
733
|
const capturedResponse = {
|
|
708
734
|
status: response.status,
|
|
709
735
|
data: response.data,
|
|
710
736
|
headers: response.headers || {},
|
|
711
737
|
};
|
|
712
|
-
const startTime = response.config.__mockifyer_startTime;
|
|
713
|
-
const durationMs = startTime ? Date.now() - startTime : undefined;
|
|
714
738
|
if ((0, mockifyer_core_1.resolveShouldPersistLiveCapture)(matchedMock.mockData, this.config)) {
|
|
715
739
|
await this.persistMatchedMockAfterLiveCapture(matchedMock, capturedResponse, durationMs);
|
|
716
740
|
}
|
|
@@ -718,22 +742,8 @@ class MockifyerClass {
|
|
|
718
742
|
response.data = clientResponse.data;
|
|
719
743
|
response.status = clientResponse.status;
|
|
720
744
|
delete response.config.__mockifyer_matchedMock;
|
|
721
|
-
const reqUrl = response.config?.url || url;
|
|
722
|
-
const reqMethod = (response.config?.method || 'GET').toUpperCase();
|
|
723
|
-
this.logNetworkEvent({
|
|
724
|
-
method: reqMethod,
|
|
725
|
-
url: reqUrl,
|
|
726
|
-
source: 'upstream',
|
|
727
|
-
status: response.status,
|
|
728
|
-
durationMs,
|
|
729
|
-
}, this.readRequestCorrelation(response.config));
|
|
730
745
|
return response;
|
|
731
746
|
}
|
|
732
|
-
// Only save locally if recordMode is enabled AND we're not proxying upstream calls.
|
|
733
|
-
// When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
|
|
734
|
-
if (this.config.recordMode && !this.config.proxy?.baseUrl) {
|
|
735
|
-
await this.saveResponse(response);
|
|
736
|
-
}
|
|
737
747
|
const reqUrl = response.config?.url || url;
|
|
738
748
|
const reqMethod = (response.config?.method || 'GET').toUpperCase();
|
|
739
749
|
this.logNetworkEvent({
|
|
@@ -744,7 +754,15 @@ class MockifyerClass {
|
|
|
744
754
|
durationMs: typeof response.config?.metadata?.durationMs === 'number'
|
|
745
755
|
? response.config.metadata.durationMs
|
|
746
756
|
: undefined,
|
|
757
|
+
requestBody: response.config?.data,
|
|
758
|
+
responseBody: response.data,
|
|
747
759
|
}, this.readRequestCorrelation(response.config));
|
|
760
|
+
response.data = (0, mockifyer_core_1.unwrapAndMergeInlineTraceEnvelope)(response.data);
|
|
761
|
+
// Only save locally if recordMode is enabled AND we're not proxying upstream calls.
|
|
762
|
+
// When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
|
|
763
|
+
if (this.config.recordMode && !this.config.proxy?.baseUrl) {
|
|
764
|
+
await this.saveResponse(response);
|
|
765
|
+
}
|
|
748
766
|
return response;
|
|
749
767
|
}, async (error) => {
|
|
750
768
|
const requestKey = error.config?.__mockifyer_requestKey;
|
|
@@ -759,6 +777,8 @@ class MockifyerClass {
|
|
|
759
777
|
source: 'error',
|
|
760
778
|
status: error.response?.status,
|
|
761
779
|
errorMessage: error?.message ?? String(error),
|
|
780
|
+
requestBody: error.config?.data,
|
|
781
|
+
responseBody: error.response?.data,
|
|
762
782
|
}, this.readRequestCorrelation(error.config));
|
|
763
783
|
}
|
|
764
784
|
throw error;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.42",
|
|
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.23"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^20.11.24",
|
package/src/index.ts
CHANGED
|
@@ -69,6 +69,9 @@ import {
|
|
|
69
69
|
emitMockifyerNetworkEvent,
|
|
70
70
|
networkEventHashFromRequestKey,
|
|
71
71
|
recordInlineTraceHopFromExchange,
|
|
72
|
+
unwrapAndMergeInlineTraceEnvelope,
|
|
73
|
+
toNetworkLogBodyPreview,
|
|
74
|
+
getInlineTraceEnvelopeBusinessBody,
|
|
72
75
|
resolveRecordResponses,
|
|
73
76
|
applyOutboundRequestCorrelation,
|
|
74
77
|
type RequestCorrelationContext,
|
|
@@ -114,7 +117,12 @@ class MockifyerClass {
|
|
|
114
117
|
|
|
115
118
|
/** Best-effort dashboard network log (skipped when traffic goes through `proxy.baseUrl`). */
|
|
116
119
|
private logNetworkEvent(
|
|
117
|
-
partial: Parameters<typeof emitMockifyerNetworkEvent>[0]['event'] & {
|
|
120
|
+
partial: Parameters<typeof emitMockifyerNetworkEvent>[0]['event'] & {
|
|
121
|
+
transport?: 'fetch';
|
|
122
|
+
/** Raw bodies for inline-trace / network-log previews (stripped before emit). */
|
|
123
|
+
requestBody?: unknown;
|
|
124
|
+
responseBody?: unknown;
|
|
125
|
+
},
|
|
118
126
|
correlation?: RequestCorrelationContext
|
|
119
127
|
): void {
|
|
120
128
|
// Dashboard proxy hops are recorded in performDashboardProxyRequest (shared axios/fetch path).
|
|
@@ -124,17 +132,22 @@ class MockifyerClass {
|
|
|
124
132
|
this.config.proxy?.scenario?.trim() ||
|
|
125
133
|
getCurrentScenario(this.config.mockDataPath);
|
|
126
134
|
|
|
135
|
+
const { requestBody, responseBody, ...eventPartial } = partial;
|
|
136
|
+
const businessResponseBody = getInlineTraceEnvelopeBusinessBody(responseBody);
|
|
137
|
+
|
|
127
138
|
recordInlineTraceHopFromExchange({
|
|
128
|
-
method:
|
|
129
|
-
url:
|
|
130
|
-
status:
|
|
131
|
-
source:
|
|
132
|
-
transport:
|
|
133
|
-
requestId: correlation?.requestId ??
|
|
134
|
-
parentRequestId: correlation?.parentRequestId ??
|
|
135
|
-
durationMs:
|
|
136
|
-
clientId: this.config.clientId ??
|
|
137
|
-
errorMessage:
|
|
139
|
+
method: eventPartial.method,
|
|
140
|
+
url: eventPartial.url,
|
|
141
|
+
status: eventPartial.status,
|
|
142
|
+
source: eventPartial.source,
|
|
143
|
+
transport: eventPartial.transport ?? 'fetch',
|
|
144
|
+
requestId: correlation?.requestId ?? eventPartial.requestId ?? null,
|
|
145
|
+
parentRequestId: correlation?.parentRequestId ?? eventPartial.parentRequestId ?? null,
|
|
146
|
+
durationMs: eventPartial.durationMs,
|
|
147
|
+
clientId: this.config.clientId ?? eventPartial.clientId ?? null,
|
|
148
|
+
errorMessage: eventPartial.errorMessage,
|
|
149
|
+
requestBody,
|
|
150
|
+
responseBody,
|
|
138
151
|
});
|
|
139
152
|
|
|
140
153
|
emitMockifyerNetworkEvent({
|
|
@@ -142,10 +155,16 @@ class MockifyerClass {
|
|
|
142
155
|
scenario,
|
|
143
156
|
clientId: this.config.clientId,
|
|
144
157
|
event: {
|
|
145
|
-
...
|
|
146
|
-
transport:
|
|
147
|
-
requestId: correlation?.requestId ??
|
|
148
|
-
parentRequestId: correlation?.parentRequestId ??
|
|
158
|
+
...eventPartial,
|
|
159
|
+
transport: eventPartial.transport ?? 'fetch',
|
|
160
|
+
requestId: correlation?.requestId ?? eventPartial.requestId,
|
|
161
|
+
parentRequestId: correlation?.parentRequestId ?? eventPartial.parentRequestId,
|
|
162
|
+
requestBodyPreview:
|
|
163
|
+
eventPartial.requestBodyPreview ??
|
|
164
|
+
(requestBody !== undefined ? toNetworkLogBodyPreview(requestBody) : undefined),
|
|
165
|
+
responseBodyPreview:
|
|
166
|
+
eventPartial.responseBodyPreview ??
|
|
167
|
+
(responseBody !== undefined ? toNetworkLogBodyPreview(businessResponseBody) : undefined),
|
|
149
168
|
},
|
|
150
169
|
});
|
|
151
170
|
}
|
|
@@ -724,6 +743,7 @@ class MockifyerClass {
|
|
|
724
743
|
`[Mockifyer-Fetch] Mock hit: ${request.method} ${request.url} → ${filename}` +
|
|
725
744
|
(filePath ? ` (${filePath})` : '')
|
|
726
745
|
);
|
|
746
|
+
const mockResponseBody = this.prepareStoredResponseBody(mockData);
|
|
727
747
|
this.logNetworkEvent(
|
|
728
748
|
{
|
|
729
749
|
method: (request.method || 'GET').toUpperCase(),
|
|
@@ -731,6 +751,8 @@ class MockifyerClass {
|
|
|
731
751
|
source: 'mock-hit',
|
|
732
752
|
status: mockData.response.status,
|
|
733
753
|
requestHash: networkEventHashFromRequestKey(requestKey),
|
|
754
|
+
requestBody: request.data,
|
|
755
|
+
responseBody: mockResponseBody,
|
|
734
756
|
},
|
|
735
757
|
correlation
|
|
736
758
|
);
|
|
@@ -745,7 +767,7 @@ class MockifyerClass {
|
|
|
745
767
|
await this.warmPoolResponseCache(mockData);
|
|
746
768
|
|
|
747
769
|
const mockResponse = {
|
|
748
|
-
data:
|
|
770
|
+
data: mockResponseBody,
|
|
749
771
|
status: mockData.response.status,
|
|
750
772
|
statusText: 'OK',
|
|
751
773
|
headers: responseHeaders,
|
|
@@ -901,13 +923,31 @@ class MockifyerClass {
|
|
|
901
923
|
|
|
902
924
|
const matchedMock = (response.config as any).__mockifyer_matchedMock as CachedMockData | undefined;
|
|
903
925
|
if (matchedMock) {
|
|
926
|
+
const startTime = (response.config as any).__mockifyer_startTime;
|
|
927
|
+
const durationMs = startTime ? Date.now() - startTime : undefined;
|
|
928
|
+
|
|
929
|
+
const reqUrl = response.config?.url || url;
|
|
930
|
+
const reqMethod = (response.config?.method || 'GET').toUpperCase();
|
|
931
|
+
// Parent hop first, then unwrap nested mockifyerTrace from downstream services.
|
|
932
|
+
this.logNetworkEvent(
|
|
933
|
+
{
|
|
934
|
+
method: reqMethod,
|
|
935
|
+
url: reqUrl,
|
|
936
|
+
source: 'upstream',
|
|
937
|
+
status: response.status,
|
|
938
|
+
durationMs,
|
|
939
|
+
requestBody: response.config?.data,
|
|
940
|
+
responseBody: response.data,
|
|
941
|
+
},
|
|
942
|
+
this.readRequestCorrelation(response.config)
|
|
943
|
+
);
|
|
944
|
+
response.data = unwrapAndMergeInlineTraceEnvelope(response.data);
|
|
945
|
+
|
|
904
946
|
const capturedResponse: StoredResponse = {
|
|
905
947
|
status: response.status,
|
|
906
948
|
data: response.data,
|
|
907
949
|
headers: (response.headers as Record<string, string>) || {},
|
|
908
950
|
};
|
|
909
|
-
const startTime = (response.config as any).__mockifyer_startTime;
|
|
910
|
-
const durationMs = startTime ? Date.now() - startTime : undefined;
|
|
911
951
|
|
|
912
952
|
if (resolveShouldPersistLiveCapture(matchedMock.mockData, this.config)) {
|
|
913
953
|
await this.persistMatchedMockAfterLiveCapture(matchedMock, capturedResponse, durationMs);
|
|
@@ -922,27 +962,9 @@ class MockifyerClass {
|
|
|
922
962
|
response.status = clientResponse.status;
|
|
923
963
|
delete (response.config as any).__mockifyer_matchedMock;
|
|
924
964
|
|
|
925
|
-
const reqUrl = response.config?.url || url;
|
|
926
|
-
const reqMethod = (response.config?.method || 'GET').toUpperCase();
|
|
927
|
-
this.logNetworkEvent(
|
|
928
|
-
{
|
|
929
|
-
method: reqMethod,
|
|
930
|
-
url: reqUrl,
|
|
931
|
-
source: 'upstream',
|
|
932
|
-
status: response.status,
|
|
933
|
-
durationMs,
|
|
934
|
-
},
|
|
935
|
-
this.readRequestCorrelation(response.config)
|
|
936
|
-
);
|
|
937
965
|
return response;
|
|
938
966
|
}
|
|
939
967
|
|
|
940
|
-
// Only save locally if recordMode is enabled AND we're not proxying upstream calls.
|
|
941
|
-
// When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
|
|
942
|
-
if (this.config.recordMode && !this.config.proxy?.baseUrl) {
|
|
943
|
-
await this.saveResponse(response);
|
|
944
|
-
}
|
|
945
|
-
|
|
946
968
|
const reqUrl = response.config?.url || url;
|
|
947
969
|
const reqMethod = (response.config?.method || 'GET').toUpperCase();
|
|
948
970
|
this.logNetworkEvent(
|
|
@@ -955,9 +977,18 @@ class MockifyerClass {
|
|
|
955
977
|
typeof response.config?.metadata?.durationMs === 'number'
|
|
956
978
|
? response.config.metadata.durationMs
|
|
957
979
|
: undefined,
|
|
980
|
+
requestBody: response.config?.data,
|
|
981
|
+
responseBody: response.data,
|
|
958
982
|
},
|
|
959
983
|
this.readRequestCorrelation(response.config)
|
|
960
984
|
);
|
|
985
|
+
response.data = unwrapAndMergeInlineTraceEnvelope(response.data);
|
|
986
|
+
|
|
987
|
+
// Only save locally if recordMode is enabled AND we're not proxying upstream calls.
|
|
988
|
+
// When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
|
|
989
|
+
if (this.config.recordMode && !this.config.proxy?.baseUrl) {
|
|
990
|
+
await this.saveResponse(response);
|
|
991
|
+
}
|
|
961
992
|
|
|
962
993
|
return response;
|
|
963
994
|
},
|
|
@@ -975,6 +1006,8 @@ class MockifyerClass {
|
|
|
975
1006
|
source: 'error',
|
|
976
1007
|
status: error.response?.status,
|
|
977
1008
|
errorMessage: error?.message ?? String(error),
|
|
1009
|
+
requestBody: error.config?.data,
|
|
1010
|
+
responseBody: error.response?.data,
|
|
978
1011
|
},
|
|
979
1012
|
this.readRequestCorrelation(error.config)
|
|
980
1013
|
);
|