@sgedda/mockifyer-fetch 1.8.39 → 1.8.41

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.
Files changed (3) hide show
  1. package/dist/index.js +32 -16
  2. package/package.json +2 -2
  3. package/src/index.ts +43 -20
package/dist/index.js CHANGED
@@ -41,10 +41,23 @@ const fetch_client_1 = require("./clients/fetch-client");
41
41
  class MockifyerClass {
42
42
  /** Best-effort dashboard network log (skipped when traffic goes through `proxy.baseUrl`). */
43
43
  logNetworkEvent(partial, correlation) {
44
+ // Dashboard proxy hops are recorded in performDashboardProxyRequest (shared axios/fetch path).
44
45
  if (this.config.proxy?.baseUrl)
45
46
  return;
46
47
  const scenario = this.config.proxy?.scenario?.trim() ||
47
48
  (0, mockifyer_core_1.getCurrentScenario)(this.config.mockDataPath);
49
+ (0, mockifyer_core_1.recordInlineTraceHopFromExchange)({
50
+ method: partial.method,
51
+ url: partial.url,
52
+ status: partial.status,
53
+ source: partial.source,
54
+ transport: partial.transport ?? 'fetch',
55
+ requestId: correlation?.requestId ?? partial.requestId ?? null,
56
+ parentRequestId: correlation?.parentRequestId ?? partial.parentRequestId ?? null,
57
+ durationMs: partial.durationMs,
58
+ clientId: this.config.clientId ?? partial.clientId ?? null,
59
+ errorMessage: partial.errorMessage,
60
+ });
48
61
  (0, mockifyer_core_1.emitMockifyerNetworkEvent)({
49
62
  config: this.config,
50
63
  scenario,
@@ -691,13 +704,24 @@ class MockifyerClass {
691
704
  }
692
705
  const matchedMock = response.config.__mockifyer_matchedMock;
693
706
  if (matchedMock) {
707
+ const startTime = response.config.__mockifyer_startTime;
708
+ const durationMs = startTime ? Date.now() - startTime : undefined;
709
+ const reqUrl = response.config?.url || url;
710
+ const reqMethod = (response.config?.method || 'GET').toUpperCase();
711
+ // Parent hop first, then unwrap nested mockifyerTrace from downstream services.
712
+ this.logNetworkEvent({
713
+ method: reqMethod,
714
+ url: reqUrl,
715
+ source: 'upstream',
716
+ status: response.status,
717
+ durationMs,
718
+ }, this.readRequestCorrelation(response.config));
719
+ response.data = (0, mockifyer_core_1.unwrapAndMergeInlineTraceEnvelope)(response.data);
694
720
  const capturedResponse = {
695
721
  status: response.status,
696
722
  data: response.data,
697
723
  headers: response.headers || {},
698
724
  };
699
- const startTime = response.config.__mockifyer_startTime;
700
- const durationMs = startTime ? Date.now() - startTime : undefined;
701
725
  if ((0, mockifyer_core_1.resolveShouldPersistLiveCapture)(matchedMock.mockData, this.config)) {
702
726
  await this.persistMatchedMockAfterLiveCapture(matchedMock, capturedResponse, durationMs);
703
727
  }
@@ -705,22 +729,8 @@ class MockifyerClass {
705
729
  response.data = clientResponse.data;
706
730
  response.status = clientResponse.status;
707
731
  delete response.config.__mockifyer_matchedMock;
708
- const reqUrl = response.config?.url || url;
709
- const reqMethod = (response.config?.method || 'GET').toUpperCase();
710
- this.logNetworkEvent({
711
- method: reqMethod,
712
- url: reqUrl,
713
- source: 'upstream',
714
- status: response.status,
715
- durationMs,
716
- }, this.readRequestCorrelation(response.config));
717
732
  return response;
718
733
  }
719
- // Only save locally if recordMode is enabled AND we're not proxying upstream calls.
720
- // When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
721
- if (this.config.recordMode && !this.config.proxy?.baseUrl) {
722
- await this.saveResponse(response);
723
- }
724
734
  const reqUrl = response.config?.url || url;
725
735
  const reqMethod = (response.config?.method || 'GET').toUpperCase();
726
736
  this.logNetworkEvent({
@@ -732,6 +742,12 @@ class MockifyerClass {
732
742
  ? response.config.metadata.durationMs
733
743
  : undefined,
734
744
  }, this.readRequestCorrelation(response.config));
745
+ response.data = (0, mockifyer_core_1.unwrapAndMergeInlineTraceEnvelope)(response.data);
746
+ // Only save locally if recordMode is enabled AND we're not proxying upstream calls.
747
+ // When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
748
+ if (this.config.recordMode && !this.config.proxy?.baseUrl) {
749
+ await this.saveResponse(response);
750
+ }
735
751
  return response;
736
752
  }, async (error) => {
737
753
  const requestKey = error.config?.__mockifyer_requestKey;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sgedda/mockifyer-fetch",
3
- "version": "1.8.39",
3
+ "version": "1.8.41",
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.15"
52
+ "@sgedda/mockifyer-core": "^1.10.23"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/node": "^20.11.24",
package/src/index.ts CHANGED
@@ -68,6 +68,8 @@ import {
68
68
  type MockifyerActivationMode,
69
69
  emitMockifyerNetworkEvent,
70
70
  networkEventHashFromRequestKey,
71
+ recordInlineTraceHopFromExchange,
72
+ unwrapAndMergeInlineTraceEnvelope,
71
73
  resolveRecordResponses,
72
74
  applyOutboundRequestCorrelation,
73
75
  type RequestCorrelationContext,
@@ -116,10 +118,26 @@ class MockifyerClass {
116
118
  partial: Parameters<typeof emitMockifyerNetworkEvent>[0]['event'] & { transport?: 'fetch' },
117
119
  correlation?: RequestCorrelationContext
118
120
  ): void {
121
+ // Dashboard proxy hops are recorded in performDashboardProxyRequest (shared axios/fetch path).
119
122
  if (this.config.proxy?.baseUrl) return;
123
+
120
124
  const scenario =
121
125
  this.config.proxy?.scenario?.trim() ||
122
126
  getCurrentScenario(this.config.mockDataPath);
127
+
128
+ recordInlineTraceHopFromExchange({
129
+ method: partial.method,
130
+ url: partial.url,
131
+ status: partial.status,
132
+ source: partial.source,
133
+ transport: partial.transport ?? 'fetch',
134
+ requestId: correlation?.requestId ?? partial.requestId ?? null,
135
+ parentRequestId: correlation?.parentRequestId ?? partial.parentRequestId ?? null,
136
+ durationMs: partial.durationMs,
137
+ clientId: this.config.clientId ?? partial.clientId ?? null,
138
+ errorMessage: partial.errorMessage,
139
+ });
140
+
123
141
  emitMockifyerNetworkEvent({
124
142
  config: this.config,
125
143
  scenario,
@@ -884,13 +902,29 @@ class MockifyerClass {
884
902
 
885
903
  const matchedMock = (response.config as any).__mockifyer_matchedMock as CachedMockData | undefined;
886
904
  if (matchedMock) {
905
+ const startTime = (response.config as any).__mockifyer_startTime;
906
+ const durationMs = startTime ? Date.now() - startTime : undefined;
907
+
908
+ const reqUrl = response.config?.url || url;
909
+ const reqMethod = (response.config?.method || 'GET').toUpperCase();
910
+ // Parent hop first, then unwrap nested mockifyerTrace from downstream services.
911
+ this.logNetworkEvent(
912
+ {
913
+ method: reqMethod,
914
+ url: reqUrl,
915
+ source: 'upstream',
916
+ status: response.status,
917
+ durationMs,
918
+ },
919
+ this.readRequestCorrelation(response.config)
920
+ );
921
+ response.data = unwrapAndMergeInlineTraceEnvelope(response.data);
922
+
887
923
  const capturedResponse: StoredResponse = {
888
924
  status: response.status,
889
925
  data: response.data,
890
926
  headers: (response.headers as Record<string, string>) || {},
891
927
  };
892
- const startTime = (response.config as any).__mockifyer_startTime;
893
- const durationMs = startTime ? Date.now() - startTime : undefined;
894
928
 
895
929
  if (resolveShouldPersistLiveCapture(matchedMock.mockData, this.config)) {
896
930
  await this.persistMatchedMockAfterLiveCapture(matchedMock, capturedResponse, durationMs);
@@ -905,27 +939,9 @@ class MockifyerClass {
905
939
  response.status = clientResponse.status;
906
940
  delete (response.config as any).__mockifyer_matchedMock;
907
941
 
908
- const reqUrl = response.config?.url || url;
909
- const reqMethod = (response.config?.method || 'GET').toUpperCase();
910
- this.logNetworkEvent(
911
- {
912
- method: reqMethod,
913
- url: reqUrl,
914
- source: 'upstream',
915
- status: response.status,
916
- durationMs,
917
- },
918
- this.readRequestCorrelation(response.config)
919
- );
920
942
  return response;
921
943
  }
922
944
 
923
- // Only save locally if recordMode is enabled AND we're not proxying upstream calls.
924
- // When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
925
- if (this.config.recordMode && !this.config.proxy?.baseUrl) {
926
- await this.saveResponse(response);
927
- }
928
-
929
945
  const reqUrl = response.config?.url || url;
930
946
  const reqMethod = (response.config?.method || 'GET').toUpperCase();
931
947
  this.logNetworkEvent(
@@ -941,6 +957,13 @@ class MockifyerClass {
941
957
  },
942
958
  this.readRequestCorrelation(response.config)
943
959
  );
960
+ response.data = unwrapAndMergeInlineTraceEnvelope(response.data);
961
+
962
+ // Only save locally if recordMode is enabled AND we're not proxying upstream calls.
963
+ // When proxy is configured, recording should happen on the proxy (e.g. dashboard → Redis).
964
+ if (this.config.recordMode && !this.config.proxy?.baseUrl) {
965
+ await this.saveResponse(response);
966
+ }
944
967
 
945
968
  return response;
946
969
  },