@sgedda/mockifyer-fetch 1.8.104 → 1.8.106
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 +46 -24
- package/dist/metro-sync-middleware.js +2 -56
- package/package.json +3 -3
- package/src/index.ts +55 -27
- package/src/metro-sync-middleware.ts +5 -56
package/dist/index.js
CHANGED
|
@@ -482,6 +482,24 @@ class MockifyerClass {
|
|
|
482
482
|
}
|
|
483
483
|
return undefined;
|
|
484
484
|
}
|
|
485
|
+
getAllJsonFiles(dir) {
|
|
486
|
+
if (!fs || !path || !fs.existsSync(dir))
|
|
487
|
+
return [];
|
|
488
|
+
const results = [];
|
|
489
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
490
|
+
const fullPath = path.join(dir, entry.name);
|
|
491
|
+
if (entry.isDirectory()) {
|
|
492
|
+
if ((0, mockifyer_core_1.isMockRecordingSidecarDir)(entry.name)) {
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
results.push(...this.getAllJsonFiles(fullPath));
|
|
496
|
+
}
|
|
497
|
+
else if (entry.name.endsWith('.json')) {
|
|
498
|
+
results.push(fullPath);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return results;
|
|
502
|
+
}
|
|
485
503
|
findBestMatchingMockFromFiles(request, options) {
|
|
486
504
|
const includePassthroughMocks = options?.includePassthroughMocks === true;
|
|
487
505
|
// Only use fs if available (Node.js environment)
|
|
@@ -501,27 +519,26 @@ class MockifyerClass {
|
|
|
501
519
|
mockifyer_core_2.logger.warn('[Mockifyer-Fetch] ⚠️ Scenario path does not exist:', scenarioPath);
|
|
502
520
|
return undefined;
|
|
503
521
|
}
|
|
504
|
-
const files =
|
|
505
|
-
.filter(file => file.endsWith('.json'));
|
|
522
|
+
const files = this.getAllJsonFiles(scenarioPath);
|
|
506
523
|
const requestKey = this.generateRequestKey(request);
|
|
507
524
|
let exactMatch;
|
|
508
525
|
let similarMatch;
|
|
509
|
-
for (const
|
|
526
|
+
for (const filePath of files) {
|
|
510
527
|
try {
|
|
511
|
-
const filePath = path.join(scenarioPath, file);
|
|
512
528
|
const fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
513
529
|
const mockData = JSON.parse(fileContent);
|
|
514
530
|
if (!mockData || !mockData.request || typeof mockData.request !== 'object') {
|
|
515
531
|
continue;
|
|
516
532
|
}
|
|
517
533
|
const mockKey = this.generateRequestKey(mockData.request);
|
|
534
|
+
const filename = path.relative(scenarioPath, filePath);
|
|
518
535
|
if (mockKey === requestKey) {
|
|
519
536
|
if ((0, mockifyer_core_1.mockShouldBeIncludedInRequestMatch)(mockData, {
|
|
520
537
|
includePassthroughMocks,
|
|
521
|
-
filename
|
|
538
|
+
filename,
|
|
522
539
|
scenarioPath,
|
|
523
540
|
})) {
|
|
524
|
-
exactMatch = { mockData, filename
|
|
541
|
+
exactMatch = { mockData, filename, filePath };
|
|
525
542
|
break;
|
|
526
543
|
}
|
|
527
544
|
continue;
|
|
@@ -591,10 +608,10 @@ class MockifyerClass {
|
|
|
591
608
|
}
|
|
592
609
|
if ((0, mockifyer_core_1.mockShouldBeIncludedInRequestMatch)(mockData, {
|
|
593
610
|
includePassthroughMocks,
|
|
594
|
-
filename
|
|
611
|
+
filename,
|
|
595
612
|
scenarioPath,
|
|
596
613
|
})) {
|
|
597
|
-
similarMatch = { mockData, filename
|
|
614
|
+
similarMatch = { mockData, filename, filePath };
|
|
598
615
|
}
|
|
599
616
|
}
|
|
600
617
|
}
|
|
@@ -605,7 +622,7 @@ class MockifyerClass {
|
|
|
605
622
|
}
|
|
606
623
|
}
|
|
607
624
|
catch (error) {
|
|
608
|
-
mockifyer_core_2.logger.warn(`[Mockifyer] Failed to load mock file ${
|
|
625
|
+
mockifyer_core_2.logger.warn(`[Mockifyer] Failed to load mock file ${filePath}:`, error);
|
|
609
626
|
}
|
|
610
627
|
}
|
|
611
628
|
return exactMatch || similarMatch;
|
|
@@ -696,12 +713,16 @@ class MockifyerClass {
|
|
|
696
713
|
if (hitScenarioPath) {
|
|
697
714
|
(0, mockifyer_core_1.ensureOverrideGroupRuntimeForScenarioPath)(hitScenarioPath, { clientId: this.config.clientId });
|
|
698
715
|
}
|
|
716
|
+
if (mockData.responseDelayMs && mockData.responseDelayMs > 0) {
|
|
717
|
+
await new Promise(resolve => setTimeout(resolve, mockData.responseDelayMs));
|
|
718
|
+
}
|
|
699
719
|
const mockResponseBody = this.prepareStoredResponseBody(mockData, filename, hitScenarioPath);
|
|
720
|
+
const effectiveStatus = mockData.statusOverride ?? mockData.response.status;
|
|
700
721
|
this.logNetworkEvent({
|
|
701
722
|
method: (request.method || 'GET').toUpperCase(),
|
|
702
723
|
url: this.resolveLoggedRequestUrl(config, request.url),
|
|
703
724
|
source: 'mock-hit',
|
|
704
|
-
status:
|
|
725
|
+
status: effectiveStatus,
|
|
705
726
|
requestHash: (0, mockifyer_core_1.networkEventHashFromRequestKey)(requestKey),
|
|
706
727
|
requestBody: request.data,
|
|
707
728
|
responseBody: mockResponseBody,
|
|
@@ -716,7 +737,7 @@ class MockifyerClass {
|
|
|
716
737
|
await this.warmPoolResponseCache(mockData);
|
|
717
738
|
const mockResponse = {
|
|
718
739
|
data: mockResponseBody,
|
|
719
|
-
status:
|
|
740
|
+
status: effectiveStatus,
|
|
720
741
|
statusText: 'OK',
|
|
721
742
|
headers: responseHeaders,
|
|
722
743
|
config: config
|
|
@@ -816,12 +837,14 @@ class MockifyerClass {
|
|
|
816
837
|
responseDataStr = String(response.data);
|
|
817
838
|
}
|
|
818
839
|
}
|
|
819
|
-
//
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
const
|
|
823
|
-
|
|
824
|
-
|
|
840
|
+
// Only Metro's own save rejection should skip recording. A business
|
|
841
|
+
// payload such as `{ success: false, error: "username taken" }` must
|
|
842
|
+
// still be logged, unwrapped, and saved.
|
|
843
|
+
const METRO_SAVE_REJECTION = 'Cannot save Mockifyer sync endpoint';
|
|
844
|
+
const hasRejectionMessage = Boolean(responseDataStr && responseDataStr.includes(METRO_SAVE_REJECTION));
|
|
845
|
+
const hasRejectionInObject = Boolean(responseDataObj &&
|
|
846
|
+
typeof responseDataObj.error === 'string' &&
|
|
847
|
+
responseDataObj.error.includes(METRO_SAVE_REJECTION));
|
|
825
848
|
if (hasRejectionMessage || hasRejectionInObject) {
|
|
826
849
|
return response;
|
|
827
850
|
}
|
|
@@ -1180,14 +1203,13 @@ class MockifyerClass {
|
|
|
1180
1203
|
mockifyer_core_2.logger.info(`[Mockifyer] Refreshed passthrough mock: ${currentScenario}/${existingMock.filename}`);
|
|
1181
1204
|
}
|
|
1182
1205
|
else {
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
const
|
|
1186
|
-
const
|
|
1187
|
-
|
|
1188
|
-
const filePath = path.join(scenarioPath, filename);
|
|
1206
|
+
// Same nested host/path layout as FilesystemProvider / Expo (Folders view).
|
|
1207
|
+
// getMockFilePath clamps over-long path segments to stay within NAME_MAX.
|
|
1208
|
+
const { dir, filename } = (0, mockifyer_core_1.getMockFilePath)(mockData, (0, mockifyer_core_1.formatDateStr)((0, mockifyer_core_1.getCurrentDate)()));
|
|
1209
|
+
const filePath = path.join(scenarioPath, dir, filename);
|
|
1210
|
+
fs.mkdirSync(path.join(scenarioPath, dir), { recursive: true });
|
|
1189
1211
|
fs.writeFileSync(filePath, JSON.stringify(mockData, null, 2));
|
|
1190
|
-
mockifyer_core_2.logger.info(`[Mockifyer] Saved new mock to file: ${currentScenario}/${filename}`);
|
|
1212
|
+
mockifyer_core_2.logger.info(`[Mockifyer] Saved new mock to file: ${currentScenario}/${dir ? `${dir}/` : ''}${filename}`);
|
|
1191
1213
|
}
|
|
1192
1214
|
if (this.config.generateTests?.enabled && this.testGenerator) {
|
|
1193
1215
|
await this.generateTestForMock(mockData);
|
|
@@ -254,60 +254,6 @@ function collectRequestBodyUtf8(req, onDone, maxBytes = MAX_METRO_POST_BODY_BYTE
|
|
|
254
254
|
});
|
|
255
255
|
req.on("error", (e) => finish(e, ""));
|
|
256
256
|
}
|
|
257
|
-
function getMockFilePathLocal(mockData, dateStr) {
|
|
258
|
-
const url = mockData.request.url || "";
|
|
259
|
-
const method = (mockData.request.method || "GET").toUpperCase();
|
|
260
|
-
let host = "unknown";
|
|
261
|
-
let pathSegments = [];
|
|
262
|
-
try {
|
|
263
|
-
const parsed = new URL(url);
|
|
264
|
-
host = parsed.hostname;
|
|
265
|
-
pathSegments = parsed.pathname.split("/").filter(Boolean);
|
|
266
|
-
}
|
|
267
|
-
catch {
|
|
268
|
-
return { dir: "unknown", filename: `${method}_${dateStr}.json` };
|
|
269
|
-
}
|
|
270
|
-
const hostSafe = host.replace(/[^a-zA-Z0-9.-]/g, "_");
|
|
271
|
-
const graphqlIdx = pathSegments.lastIndexOf("graphql");
|
|
272
|
-
const restIdx = pathSegments.indexOf("rest");
|
|
273
|
-
let type;
|
|
274
|
-
let remainingSegments;
|
|
275
|
-
if (graphqlIdx >= 0) {
|
|
276
|
-
type = "graphql";
|
|
277
|
-
remainingSegments = pathSegments.slice(graphqlIdx + 1);
|
|
278
|
-
}
|
|
279
|
-
else if (restIdx >= 0) {
|
|
280
|
-
type = "rest";
|
|
281
|
-
remainingSegments = pathSegments.slice(restIdx + 1);
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
type = "";
|
|
285
|
-
remainingSegments = pathSegments;
|
|
286
|
-
}
|
|
287
|
-
let identifier = "";
|
|
288
|
-
if (mockData.request.data) {
|
|
289
|
-
try {
|
|
290
|
-
const body = typeof mockData.request.data === "string"
|
|
291
|
-
? JSON.parse(mockData.request.data)
|
|
292
|
-
: mockData.request.data;
|
|
293
|
-
if (body.operationName)
|
|
294
|
-
identifier = body.operationName;
|
|
295
|
-
else if (body.path)
|
|
296
|
-
identifier = body.path;
|
|
297
|
-
else if (body.webAppName)
|
|
298
|
-
identifier = body.webAppName;
|
|
299
|
-
}
|
|
300
|
-
catch {
|
|
301
|
-
/* ignore */
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
identifier = identifier.replace(/[^a-zA-Z0-9_-]/g, "_").substring(0, 60);
|
|
305
|
-
const dir = [hostSafe, type, ...remainingSegments].filter(Boolean).join("/");
|
|
306
|
-
const filename = identifier
|
|
307
|
-
? `${method}_${identifier}_${dateStr}.json`
|
|
308
|
-
: `${method}_${dateStr}.json`;
|
|
309
|
-
return { dir, filename };
|
|
310
|
-
}
|
|
311
257
|
/**
|
|
312
258
|
* Scenario folder names the dashboard accepts (one path segment).
|
|
313
259
|
* `pool` is the fixture-pool directory and is not a scenario.
|
|
@@ -568,7 +514,7 @@ function saveMockToProjectFolder(mockData, projectRoot, mockDataPath, testConfig
|
|
|
568
514
|
.replace(/T/, "_")
|
|
569
515
|
.replace(/\..+/, "")
|
|
570
516
|
.replace(/:/g, "-");
|
|
571
|
-
const { dir, filename } =
|
|
517
|
+
const { dir, filename } = (0, mockifyer_core_1.getMockFilePath)(mockData, dateStr);
|
|
572
518
|
const fullDir = path.join(scenarioPath, dir);
|
|
573
519
|
const filePath = path.join(fullDir, filename);
|
|
574
520
|
// Check if file already exists - skip saving if it does
|
|
@@ -1750,7 +1696,7 @@ function createMockSyncMiddleware(options) {
|
|
|
1750
1696
|
}
|
|
1751
1697
|
const upserts = (0, mockifyer_core_2.parseDomainPathRules)(parsed.upserts ?? parsed.rules ?? {});
|
|
1752
1698
|
const existing = (0, mockifyer_core_2.readDomainPathRulesFile)(mockDataPath, scenarioName);
|
|
1753
|
-
const { rules, changed } = (0, mockifyer_core_2.
|
|
1699
|
+
const { rules, changed } = (0, mockifyer_core_2.mergeDiscoveredDomainPathUpserts)(existing, upserts);
|
|
1754
1700
|
if (changed) {
|
|
1755
1701
|
(0, mockifyer_core_2.writeDomainPathRulesFile)(mockDataPath, scenarioName, rules);
|
|
1756
1702
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.106",
|
|
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.190"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^20.11.24",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"@sgedda/mockifyer-axios": "*",
|
|
60
|
-
"axios": "^1.6.0 || ^1.7.0",
|
|
60
|
+
"axios": "^1.6.0 || ^1.7.0 || ^1.20.0",
|
|
61
61
|
"react-native-launch-arguments": ">=4.0.0"
|
|
62
62
|
},
|
|
63
63
|
"peerDependenciesMeta": {
|
package/src/index.ts
CHANGED
|
@@ -107,6 +107,10 @@ import {
|
|
|
107
107
|
type MockifyerRuntimeEnabledStorage,
|
|
108
108
|
outboundHeadersToRecord,
|
|
109
109
|
appendParamsToUrl,
|
|
110
|
+
getMockFilePath,
|
|
111
|
+
formatDateStr,
|
|
112
|
+
getCurrentDate,
|
|
113
|
+
isMockRecordingSidecarDir,
|
|
110
114
|
} from '@sgedda/mockifyer-core';
|
|
111
115
|
import { logger, setLogLevel } from '@sgedda/mockifyer-core';
|
|
112
116
|
import {
|
|
@@ -685,6 +689,23 @@ class MockifyerClass {
|
|
|
685
689
|
return undefined;
|
|
686
690
|
}
|
|
687
691
|
|
|
692
|
+
private getAllJsonFiles(dir: string): string[] {
|
|
693
|
+
if (!fs || !path || !fs.existsSync(dir)) return [];
|
|
694
|
+
const results: string[] = [];
|
|
695
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
696
|
+
const fullPath = path.join(dir, entry.name);
|
|
697
|
+
if (entry.isDirectory()) {
|
|
698
|
+
if (isMockRecordingSidecarDir(entry.name)) {
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
results.push(...this.getAllJsonFiles(fullPath));
|
|
702
|
+
} else if (entry.name.endsWith('.json')) {
|
|
703
|
+
results.push(fullPath);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return results;
|
|
707
|
+
}
|
|
708
|
+
|
|
688
709
|
private findBestMatchingMockFromFiles(
|
|
689
710
|
request: StoredRequest,
|
|
690
711
|
options?: { includePassthroughMocks?: boolean }
|
|
@@ -711,16 +732,14 @@ class MockifyerClass {
|
|
|
711
732
|
return undefined;
|
|
712
733
|
}
|
|
713
734
|
|
|
714
|
-
const files =
|
|
715
|
-
.filter(file => file.endsWith('.json'));
|
|
735
|
+
const files = this.getAllJsonFiles(scenarioPath);
|
|
716
736
|
|
|
717
737
|
const requestKey = this.generateRequestKey(request);
|
|
718
738
|
let exactMatch: CachedMockData | undefined;
|
|
719
739
|
let similarMatch: CachedMockData | undefined;
|
|
720
740
|
|
|
721
|
-
for (const
|
|
741
|
+
for (const filePath of files) {
|
|
722
742
|
try {
|
|
723
|
-
const filePath = path!.join(scenarioPath, file);
|
|
724
743
|
const fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
725
744
|
const mockData: MockData = JSON.parse(fileContent);
|
|
726
745
|
|
|
@@ -729,16 +748,17 @@ class MockifyerClass {
|
|
|
729
748
|
}
|
|
730
749
|
|
|
731
750
|
const mockKey = this.generateRequestKey(mockData.request);
|
|
751
|
+
const filename = path.relative(scenarioPath, filePath);
|
|
732
752
|
|
|
733
753
|
if (mockKey === requestKey) {
|
|
734
754
|
if (
|
|
735
755
|
mockShouldBeIncludedInRequestMatch(mockData, {
|
|
736
756
|
includePassthroughMocks,
|
|
737
|
-
filename
|
|
757
|
+
filename,
|
|
738
758
|
scenarioPath,
|
|
739
759
|
})
|
|
740
760
|
) {
|
|
741
|
-
exactMatch = { mockData, filename
|
|
761
|
+
exactMatch = { mockData, filename, filePath };
|
|
742
762
|
break;
|
|
743
763
|
}
|
|
744
764
|
continue;
|
|
@@ -820,11 +840,11 @@ class MockifyerClass {
|
|
|
820
840
|
if (
|
|
821
841
|
mockShouldBeIncludedInRequestMatch(mockData, {
|
|
822
842
|
includePassthroughMocks,
|
|
823
|
-
filename
|
|
843
|
+
filename,
|
|
824
844
|
scenarioPath,
|
|
825
845
|
})
|
|
826
846
|
) {
|
|
827
|
-
similarMatch = { mockData, filename
|
|
847
|
+
similarMatch = { mockData, filename, filePath };
|
|
828
848
|
}
|
|
829
849
|
}
|
|
830
850
|
} catch (e) {
|
|
@@ -833,7 +853,7 @@ class MockifyerClass {
|
|
|
833
853
|
}
|
|
834
854
|
}
|
|
835
855
|
} catch (error) {
|
|
836
|
-
logger.warn(`[Mockifyer] Failed to load mock file ${
|
|
856
|
+
logger.warn(`[Mockifyer] Failed to load mock file ${filePath}:`, error);
|
|
837
857
|
}
|
|
838
858
|
}
|
|
839
859
|
|
|
@@ -950,13 +970,19 @@ class MockifyerClass {
|
|
|
950
970
|
if (hitScenarioPath) {
|
|
951
971
|
ensureOverrideGroupRuntimeForScenarioPath(hitScenarioPath, { clientId: this.config.clientId });
|
|
952
972
|
}
|
|
973
|
+
|
|
974
|
+
if (mockData.responseDelayMs && mockData.responseDelayMs > 0) {
|
|
975
|
+
await new Promise(resolve => setTimeout(resolve, mockData.responseDelayMs));
|
|
976
|
+
}
|
|
977
|
+
|
|
953
978
|
const mockResponseBody = this.prepareStoredResponseBody(mockData, filename, hitScenarioPath);
|
|
979
|
+
const effectiveStatus = mockData.statusOverride ?? mockData.response.status;
|
|
954
980
|
this.logNetworkEvent(
|
|
955
981
|
{
|
|
956
982
|
method: (request.method || 'GET').toUpperCase(),
|
|
957
983
|
url: this.resolveLoggedRequestUrl(config, request.url),
|
|
958
984
|
source: 'mock-hit',
|
|
959
|
-
status:
|
|
985
|
+
status: effectiveStatus,
|
|
960
986
|
requestHash: networkEventHashFromRequestKey(requestKey),
|
|
961
987
|
requestBody: request.data,
|
|
962
988
|
responseBody: mockResponseBody,
|
|
@@ -976,7 +1002,7 @@ class MockifyerClass {
|
|
|
976
1002
|
|
|
977
1003
|
const mockResponse = {
|
|
978
1004
|
data: mockResponseBody,
|
|
979
|
-
status:
|
|
1005
|
+
status: effectiveStatus,
|
|
980
1006
|
statusText: 'OK',
|
|
981
1007
|
headers: responseHeaders,
|
|
982
1008
|
config: config as any
|
|
@@ -1091,16 +1117,17 @@ class MockifyerClass {
|
|
|
1091
1117
|
}
|
|
1092
1118
|
}
|
|
1093
1119
|
|
|
1094
|
-
//
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1120
|
+
// Only Metro's own save rejection should skip recording. A business
|
|
1121
|
+
// payload such as `{ success: false, error: "username taken" }` must
|
|
1122
|
+
// still be logged, unwrapped, and saved.
|
|
1123
|
+
const METRO_SAVE_REJECTION = 'Cannot save Mockifyer sync endpoint';
|
|
1124
|
+
const hasRejectionMessage = Boolean(
|
|
1125
|
+
responseDataStr && responseDataStr.includes(METRO_SAVE_REJECTION)
|
|
1098
1126
|
);
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
(responseDataObj.success === false && responseDataObj.error)
|
|
1127
|
+
const hasRejectionInObject = Boolean(
|
|
1128
|
+
responseDataObj &&
|
|
1129
|
+
typeof responseDataObj.error === 'string' &&
|
|
1130
|
+
responseDataObj.error.includes(METRO_SAVE_REJECTION)
|
|
1104
1131
|
);
|
|
1105
1132
|
|
|
1106
1133
|
if (hasRejectionMessage || hasRejectionInObject) {
|
|
@@ -1552,14 +1579,15 @@ class MockifyerClass {
|
|
|
1552
1579
|
`[Mockifyer] Refreshed passthrough mock: ${currentScenario}/${existingMock.filename}`
|
|
1553
1580
|
);
|
|
1554
1581
|
} else {
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
const
|
|
1558
|
-
const
|
|
1559
|
-
|
|
1560
|
-
const filePath = path.join(scenarioPath, filename);
|
|
1582
|
+
// Same nested host/path layout as FilesystemProvider / Expo (Folders view).
|
|
1583
|
+
// getMockFilePath clamps over-long path segments to stay within NAME_MAX.
|
|
1584
|
+
const { dir, filename } = getMockFilePath(mockData, formatDateStr(getCurrentDate()));
|
|
1585
|
+
const filePath = path.join(scenarioPath, dir, filename);
|
|
1586
|
+
fs.mkdirSync(path.join(scenarioPath, dir), { recursive: true });
|
|
1561
1587
|
fs.writeFileSync(filePath, JSON.stringify(mockData, null, 2));
|
|
1562
|
-
logger.info(
|
|
1588
|
+
logger.info(
|
|
1589
|
+
`[Mockifyer] Saved new mock to file: ${currentScenario}/${dir ? `${dir}/` : ''}${filename}`
|
|
1590
|
+
);
|
|
1563
1591
|
}
|
|
1564
1592
|
|
|
1565
1593
|
if (this.config.generateTests?.enabled && this.testGenerator) {
|
|
@@ -39,13 +39,14 @@ import {
|
|
|
39
39
|
MockData,
|
|
40
40
|
TestGenerator,
|
|
41
41
|
TestGenerationOptions,
|
|
42
|
+
getMockFilePath,
|
|
43
|
+
logger,
|
|
42
44
|
} from "@sgedda/mockifyer-core";
|
|
43
|
-
import { logger } from "@sgedda/mockifyer-core";
|
|
44
45
|
import {
|
|
45
46
|
POOL_ID_PATTERN,
|
|
46
47
|
loadPoolResponseItem,
|
|
47
48
|
type PoolResponseItem,
|
|
48
|
-
|
|
49
|
+
mergeDiscoveredDomainPathUpserts,
|
|
49
50
|
parseDomainPathRules,
|
|
50
51
|
readDomainPathRulesFile,
|
|
51
52
|
writeDomainPathRulesFile,
|
|
@@ -366,58 +367,6 @@ function collectRequestBodyUtf8(
|
|
|
366
367
|
req.on("error", (e: Error) => finish(e, ""));
|
|
367
368
|
}
|
|
368
369
|
|
|
369
|
-
function getMockFilePathLocal(
|
|
370
|
-
mockData: MockData,
|
|
371
|
-
dateStr: string,
|
|
372
|
-
): { dir: string; filename: string } {
|
|
373
|
-
const url = mockData.request.url || "";
|
|
374
|
-
const method = (mockData.request.method || "GET").toUpperCase();
|
|
375
|
-
let host = "unknown";
|
|
376
|
-
let pathSegments: string[] = [];
|
|
377
|
-
try {
|
|
378
|
-
const parsed = new URL(url);
|
|
379
|
-
host = parsed.hostname;
|
|
380
|
-
pathSegments = parsed.pathname.split("/").filter(Boolean);
|
|
381
|
-
} catch {
|
|
382
|
-
return { dir: "unknown", filename: `${method}_${dateStr}.json` };
|
|
383
|
-
}
|
|
384
|
-
const hostSafe = host.replace(/[^a-zA-Z0-9.-]/g, "_");
|
|
385
|
-
const graphqlIdx = pathSegments.lastIndexOf("graphql");
|
|
386
|
-
const restIdx = pathSegments.indexOf("rest");
|
|
387
|
-
let type: string;
|
|
388
|
-
let remainingSegments: string[];
|
|
389
|
-
if (graphqlIdx >= 0) {
|
|
390
|
-
type = "graphql";
|
|
391
|
-
remainingSegments = pathSegments.slice(graphqlIdx + 1);
|
|
392
|
-
} else if (restIdx >= 0) {
|
|
393
|
-
type = "rest";
|
|
394
|
-
remainingSegments = pathSegments.slice(restIdx + 1);
|
|
395
|
-
} else {
|
|
396
|
-
type = "";
|
|
397
|
-
remainingSegments = pathSegments;
|
|
398
|
-
}
|
|
399
|
-
let identifier = "";
|
|
400
|
-
if (mockData.request.data) {
|
|
401
|
-
try {
|
|
402
|
-
const body =
|
|
403
|
-
typeof mockData.request.data === "string"
|
|
404
|
-
? JSON.parse(mockData.request.data)
|
|
405
|
-
: mockData.request.data;
|
|
406
|
-
if (body.operationName) identifier = body.operationName;
|
|
407
|
-
else if (body.path) identifier = body.path;
|
|
408
|
-
else if (body.webAppName) identifier = body.webAppName;
|
|
409
|
-
} catch {
|
|
410
|
-
/* ignore */
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
identifier = identifier.replace(/[^a-zA-Z0-9_-]/g, "_").substring(0, 60);
|
|
414
|
-
const dir = [hostSafe, type, ...remainingSegments].filter(Boolean).join("/");
|
|
415
|
-
const filename = identifier
|
|
416
|
-
? `${method}_${identifier}_${dateStr}.json`
|
|
417
|
-
: `${method}_${dateStr}.json`;
|
|
418
|
-
return { dir, filename };
|
|
419
|
-
}
|
|
420
|
-
|
|
421
370
|
/**
|
|
422
371
|
* Scenario folder names the dashboard accepts (one path segment).
|
|
423
372
|
* `pool` is the fixture-pool directory and is not a scenario.
|
|
@@ -756,7 +705,7 @@ function saveMockToProjectFolder(
|
|
|
756
705
|
.replace(/T/, "_")
|
|
757
706
|
.replace(/\..+/, "")
|
|
758
707
|
.replace(/:/g, "-");
|
|
759
|
-
const { dir, filename } =
|
|
708
|
+
const { dir, filename } = getMockFilePath(mockData, dateStr);
|
|
760
709
|
const fullDir = path.join(scenarioPath, dir);
|
|
761
710
|
const filePath = path.join(fullDir, filename);
|
|
762
711
|
|
|
@@ -2237,7 +2186,7 @@ export function createMockSyncMiddleware(options?: MetroSyncMiddlewareOptions) {
|
|
|
2237
2186
|
parsed.upserts ?? parsed.rules ?? {},
|
|
2238
2187
|
);
|
|
2239
2188
|
const existing = readDomainPathRulesFile(mockDataPath, scenarioName);
|
|
2240
|
-
const { rules, changed } =
|
|
2189
|
+
const { rules, changed } = mergeDiscoveredDomainPathUpserts(
|
|
2241
2190
|
existing,
|
|
2242
2191
|
upserts,
|
|
2243
2192
|
);
|