@sgedda/mockifyer-fetch 1.8.106 → 1.8.107
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/README.md +155 -0
- package/dist/clients/fetch-client.d.ts +1 -1
- package/dist/clients/fetch-client.js +4 -2
- package/dist/core-proxy.browser.d.ts +19 -0
- package/dist/core-proxy.browser.js +57 -0
- package/dist/index.browser.d.ts +52 -0
- package/dist/index.browser.js +107 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +32 -1562
- package/dist/mockifyer-class.d.ts +123 -0
- package/dist/mockifyer-class.js +1306 -0
- package/dist/node-capabilities.browser.d.ts +6 -0
- package/dist/node-capabilities.browser.js +110 -0
- package/dist/node-capabilities.d.ts +51 -0
- package/dist/node-capabilities.js +6 -0
- package/dist/node-capabilities.node.d.ts +6 -0
- package/dist/node-capabilities.node.js +31 -0
- package/dist/react-native.js +2 -1
- package/package.json +13 -2
- package/src/clients/fetch-client.ts +6 -3
- package/src/core-proxy.browser.ts +62 -0
- package/src/index.browser.ts +166 -0
- package/src/index.ts +32 -1911
- package/src/mockifyer-class.ts +1700 -0
- package/src/node-capabilities.browser.ts +123 -0
- package/src/node-capabilities.node.ts +40 -0
- package/src/node-capabilities.ts +60 -0
- package/src/react-native.ts +2 -1
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Browser implementation of NodeCapabilities.
|
|
4
|
+
* Uses Web Crypto API and provides no-ops for fs-based features.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.nodeCapabilities = void 0;
|
|
8
|
+
// Pure-JS SHA-256 implementation (from crypto-digest.ts)
|
|
9
|
+
function pureSha256Hex(input) {
|
|
10
|
+
const utf8 = unescape(encodeURIComponent(input));
|
|
11
|
+
const bytes = [];
|
|
12
|
+
for (let i = 0; i < utf8.length; i += 1) {
|
|
13
|
+
bytes.push(utf8.charCodeAt(i));
|
|
14
|
+
}
|
|
15
|
+
const K = [
|
|
16
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
17
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
18
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
19
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
20
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
21
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
22
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
23
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
24
|
+
];
|
|
25
|
+
const rotr = (n, x) => (x >>> n) | (x << (32 - n));
|
|
26
|
+
const ch = (x, y, z) => (x & y) ^ (~x & z);
|
|
27
|
+
const maj = (x, y, z) => (x & y) ^ (x & z) ^ (y & z);
|
|
28
|
+
const sigma0 = (x) => rotr(2, x) ^ rotr(13, x) ^ rotr(22, x);
|
|
29
|
+
const sigma1 = (x) => rotr(6, x) ^ rotr(11, x) ^ rotr(25, x);
|
|
30
|
+
const gamma0 = (x) => rotr(7, x) ^ rotr(18, x) ^ (x >>> 3);
|
|
31
|
+
const gamma1 = (x) => rotr(17, x) ^ rotr(19, x) ^ (x >>> 10);
|
|
32
|
+
// Padding
|
|
33
|
+
const len = bytes.length;
|
|
34
|
+
const bitLen = len * 8;
|
|
35
|
+
bytes.push(0x80);
|
|
36
|
+
while ((bytes.length % 64) !== 56) {
|
|
37
|
+
bytes.push(0);
|
|
38
|
+
}
|
|
39
|
+
for (let i = 7; i >= 0; i -= 1) {
|
|
40
|
+
bytes.push((bitLen >>> (i * 8)) & 0xff);
|
|
41
|
+
}
|
|
42
|
+
// Initial hash
|
|
43
|
+
let H = [
|
|
44
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
45
|
+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
46
|
+
];
|
|
47
|
+
// Process blocks
|
|
48
|
+
for (let offset = 0; offset < bytes.length; offset += 64) {
|
|
49
|
+
const W = [];
|
|
50
|
+
for (let t = 0; t < 16; t += 1) {
|
|
51
|
+
W[t] =
|
|
52
|
+
(bytes[offset + t * 4] << 24) |
|
|
53
|
+
(bytes[offset + t * 4 + 1] << 16) |
|
|
54
|
+
(bytes[offset + t * 4 + 2] << 8) |
|
|
55
|
+
bytes[offset + t * 4 + 3];
|
|
56
|
+
}
|
|
57
|
+
for (let t = 16; t < 64; t += 1) {
|
|
58
|
+
W[t] = (gamma1(W[t - 2]) + W[t - 7] + gamma0(W[t - 15]) + W[t - 16]) >>> 0;
|
|
59
|
+
}
|
|
60
|
+
let [a, b, c, d, e, f, g, h] = H;
|
|
61
|
+
for (let t = 0; t < 64; t += 1) {
|
|
62
|
+
const T1 = (h + sigma1(e) + ch(e, f, g) + K[t] + W[t]) >>> 0;
|
|
63
|
+
const T2 = (sigma0(a) + maj(a, b, c)) >>> 0;
|
|
64
|
+
h = g;
|
|
65
|
+
g = f;
|
|
66
|
+
f = e;
|
|
67
|
+
e = (d + T1) >>> 0;
|
|
68
|
+
d = c;
|
|
69
|
+
c = b;
|
|
70
|
+
b = a;
|
|
71
|
+
a = (T1 + T2) >>> 0;
|
|
72
|
+
}
|
|
73
|
+
H = [
|
|
74
|
+
(H[0] + a) >>> 0,
|
|
75
|
+
(H[1] + b) >>> 0,
|
|
76
|
+
(H[2] + c) >>> 0,
|
|
77
|
+
(H[3] + d) >>> 0,
|
|
78
|
+
(H[4] + e) >>> 0,
|
|
79
|
+
(H[5] + f) >>> 0,
|
|
80
|
+
(H[6] + g) >>> 0,
|
|
81
|
+
(H[7] + h) >>> 0,
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
return H.map((n) => n.toString(16).padStart(8, '0')).join('');
|
|
85
|
+
}
|
|
86
|
+
const browserCapabilities = {
|
|
87
|
+
digest(input) {
|
|
88
|
+
// Use pure-JS implementation (Web Crypto is async, we need sync)
|
|
89
|
+
return pureSha256Hex(input);
|
|
90
|
+
},
|
|
91
|
+
getCurrentScenario(_mockDataPath, _clientId) {
|
|
92
|
+
// Browser: use env var only, or default
|
|
93
|
+
return (typeof process !== 'undefined' && process.env?.MOCKIFYER_SCENARIO) || 'default';
|
|
94
|
+
},
|
|
95
|
+
getScenarioFolderPath(mockDataPath, _scenario) {
|
|
96
|
+
// Browser: return mock path as-is (no filesystem resolution)
|
|
97
|
+
return mockDataPath;
|
|
98
|
+
},
|
|
99
|
+
ensureScenarioFolder(_mockDataPath, _scenario) {
|
|
100
|
+
// Browser: no-op (no filesystem)
|
|
101
|
+
},
|
|
102
|
+
// Optional features not supported in browser
|
|
103
|
+
mirrorProxyRecording: undefined,
|
|
104
|
+
spillBodyToDisk: undefined,
|
|
105
|
+
readSpilledBody: undefined,
|
|
106
|
+
applyRequestCorrelation: undefined,
|
|
107
|
+
generateAtlasHtml: undefined,
|
|
108
|
+
};
|
|
109
|
+
// Export as nodeCapabilities for consistency (package.json maps .node to .browser)
|
|
110
|
+
exports.nodeCapabilities = browserCapabilities;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform-specific capabilities for Mockifyer.
|
|
3
|
+
* Node entry provides full implementations; browser/RN provide limited or no-op versions.
|
|
4
|
+
*/
|
|
5
|
+
export interface NodeCapabilities {
|
|
6
|
+
/**
|
|
7
|
+
* SHA-256 digest for cache keys and request hashing.
|
|
8
|
+
* Node: crypto.createHash. Browser: Web Crypto or pure-JS fallback.
|
|
9
|
+
*/
|
|
10
|
+
digest(input: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Get current scenario name from filesystem or env.
|
|
13
|
+
* Node: reads from disk/env. Browser: returns default or env-only.
|
|
14
|
+
*/
|
|
15
|
+
getCurrentScenario(mockDataPath: string, clientId?: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Get filesystem path for scenario folder.
|
|
18
|
+
* Node: resolves paths. Browser: returns mock path (no fs operations).
|
|
19
|
+
*/
|
|
20
|
+
getScenarioFolderPath(mockDataPath: string, scenario: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Ensure scenario directory exists.
|
|
23
|
+
* Node: creates directory. Browser: no-op.
|
|
24
|
+
*/
|
|
25
|
+
ensureScenarioFolder(mockDataPath: string, scenario: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* Mirror proxy recording to client filesystem.
|
|
28
|
+
* Node: writes to disk. Browser: no-op or error.
|
|
29
|
+
*/
|
|
30
|
+
mirrorProxyRecording?(mockData: any, scenario: string, hash: string): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Spill large response bodies to disk.
|
|
33
|
+
* Node: writes file. Browser: keeps in memory or errors.
|
|
34
|
+
*/
|
|
35
|
+
spillBodyToDisk?(body: unknown, spillPath: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Read spilled body from disk.
|
|
38
|
+
* Node: reads file. Browser: returns undefined.
|
|
39
|
+
*/
|
|
40
|
+
readSpilledBody?(spillPath: string): unknown;
|
|
41
|
+
/**
|
|
42
|
+
* HTTP request correlation (for distributed tracing).
|
|
43
|
+
* Node: uses http/https modules. Browser: header-only or no-op.
|
|
44
|
+
*/
|
|
45
|
+
applyRequestCorrelation?(request: any, context: any): void;
|
|
46
|
+
/**
|
|
47
|
+
* Generate Atlas HTML documentation.
|
|
48
|
+
* Node: full HTML generation. Browser: not supported.
|
|
49
|
+
*/
|
|
50
|
+
generateAtlasHtml?(data: any): string | undefined;
|
|
51
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Node.js implementation of NodeCapabilities.
|
|
4
|
+
* Imports real utilities from mockifyer-core that use fs/path/crypto.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.nodeCapabilities = void 0;
|
|
8
|
+
const mockifyer_core_1 = require("@sgedda/mockifyer-core");
|
|
9
|
+
// Import crypto digest directly from source (works in both Jest and production)
|
|
10
|
+
const cryptoDigest = require('@sgedda/mockifyer-core/utils/crypto-digest');
|
|
11
|
+
const sha256Hex = cryptoDigest.sha256Hex;
|
|
12
|
+
exports.nodeCapabilities = {
|
|
13
|
+
digest(input) {
|
|
14
|
+
return sha256Hex(input);
|
|
15
|
+
},
|
|
16
|
+
getCurrentScenario(mockDataPath, clientId) {
|
|
17
|
+
return (0, mockifyer_core_1.getCurrentScenario)(mockDataPath, clientId);
|
|
18
|
+
},
|
|
19
|
+
getScenarioFolderPath(mockDataPath, scenario) {
|
|
20
|
+
return (0, mockifyer_core_1.getScenarioFolderPath)(mockDataPath, scenario);
|
|
21
|
+
},
|
|
22
|
+
ensureScenarioFolder(mockDataPath, scenario) {
|
|
23
|
+
(0, mockifyer_core_1.ensureScenarioFolder)(mockDataPath, scenario);
|
|
24
|
+
},
|
|
25
|
+
// Optional capabilities - lazily loaded when needed
|
|
26
|
+
mirrorProxyRecording: undefined, // Loaded on demand
|
|
27
|
+
spillBodyToDisk: undefined,
|
|
28
|
+
readSpilledBody: undefined,
|
|
29
|
+
applyRequestCorrelation: undefined,
|
|
30
|
+
generateAtlasHtml: undefined,
|
|
31
|
+
};
|
package/dist/react-native.js
CHANGED
|
@@ -121,7 +121,8 @@ async function loadBundledMockData(bundledDataPath) {
|
|
|
121
121
|
async function setupMockifyerForReactNative(options) {
|
|
122
122
|
const { isDev, mockDataPath = 'mock-data', bundledDataPath = './assets/mock-data', recordMode = false, startDisabled = false, persistRuntimeEnabled, config: userConfig = {}, proxyBaseUrl, proxyScenario, proxyRecordOnMiss, proxyRecordResponses, skipDashboardRedisHealthCheck, useLaunchArgumentsClientId = false, launchArgumentClientIdKey = mockifyer_core_2.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY, runtimeMode: runtimeModeOption, } = options;
|
|
123
123
|
const proxyShouldRecordOnMiss = proxyRecordOnMiss ?? recordMode;
|
|
124
|
-
const
|
|
124
|
+
const recordResponsesConfig = proxyRecordResponses ?? userConfig.proxy?.recordResponses;
|
|
125
|
+
const proxyShouldRecordResponses = (0, mockifyer_core_2.resolveRecordResponses)(typeof recordResponsesConfig === 'boolean' ? recordResponsesConfig : false);
|
|
125
126
|
const launchClientIdKey = launchArgumentClientIdKey ?? mockifyer_core_2.MOCKIFYER_LAUNCH_ARGUMENT_CLIENT_ID_KEY;
|
|
126
127
|
const clientIdFromLaunchArgs = (0, mockifyer_core_2.tryGetClientIdFromLaunchArguments)(launchClientIdKey);
|
|
127
128
|
const shouldApplyLaunchClientId = useLaunchArgumentsClientId || Boolean(clientIdFromLaunchArgs);
|
package/package.json
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgedda/mockifyer-fetch",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.107",
|
|
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",
|
|
7
7
|
"react-native": "dist/react-native.js",
|
|
8
|
+
"browser": {
|
|
9
|
+
"./dist/index.js": "./dist/index.browser.js",
|
|
10
|
+
"./dist/core-proxy.js": "./dist/core-proxy.browser.js",
|
|
11
|
+
"./dist/node-capabilities.js": "./dist/node-capabilities.browser.js",
|
|
12
|
+
"./dist/node-capabilities.node.js": "./dist/node-capabilities.browser.js",
|
|
13
|
+
"./src/index.ts": "./src/index.browser.ts",
|
|
14
|
+
"./src/node-capabilities.ts": "./src/node-capabilities.browser.ts",
|
|
15
|
+
"./src/node-capabilities.node.ts": "./src/node-capabilities.browser.ts",
|
|
16
|
+
"./src/core-proxy.ts": "./src/core-proxy.browser.ts"
|
|
17
|
+
},
|
|
8
18
|
"exports": {
|
|
9
19
|
".": {
|
|
20
|
+
"browser": "./dist/index.browser.js",
|
|
10
21
|
"react-native": "./dist/react-native.js",
|
|
11
22
|
"types": "./dist/index.d.ts",
|
|
12
23
|
"default": "./dist/index.js"
|
|
@@ -49,7 +60,7 @@
|
|
|
49
60
|
},
|
|
50
61
|
"license": "MIT",
|
|
51
62
|
"dependencies": {
|
|
52
|
-
"@sgedda/mockifyer-core": "^1.10.
|
|
63
|
+
"@sgedda/mockifyer-core": "^1.10.192"
|
|
53
64
|
},
|
|
54
65
|
"devDependencies": {
|
|
55
66
|
"@types/node": "^20.11.24",
|
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
MOCKIFYER_DEVICE_ID_HEADER,
|
|
11
11
|
resolveMockifyerTraceFromHeaders,
|
|
12
12
|
} from '@sgedda/mockifyer-core';
|
|
13
|
-
import { performDashboardProxyRequest } from '../core-proxy';
|
|
14
13
|
|
|
15
14
|
function headerContentType(headers: Headers): string | undefined {
|
|
16
15
|
const contentType = headers.get('content-type');
|
|
@@ -74,7 +73,7 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
74
73
|
private proxyScenario?: string;
|
|
75
74
|
/** When set, included as `record` on `/api/proxy`. When unset, dashboard per-scenario default applies. */
|
|
76
75
|
private proxyRecordOnMiss?: boolean;
|
|
77
|
-
private proxyRecordResponses: boolean
|
|
76
|
+
private proxyRecordResponses: boolean | Array<string | RegExp>;
|
|
78
77
|
private getClientId?: () => string | undefined;
|
|
79
78
|
private getStrictLaneScenario?: () => boolean;
|
|
80
79
|
private getUpstreamTlsInsecure?: () => boolean;
|
|
@@ -89,7 +88,7 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
89
88
|
baseUrl: string;
|
|
90
89
|
scenario?: string;
|
|
91
90
|
recordOnMiss?: boolean;
|
|
92
|
-
recordResponses?: boolean
|
|
91
|
+
recordResponses?: boolean | Array<string | RegExp>;
|
|
93
92
|
strictLaneScenario?: boolean;
|
|
94
93
|
mirrorRecordedMocksToClient?: boolean;
|
|
95
94
|
};
|
|
@@ -107,6 +106,7 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
107
106
|
this.proxyBaseUrl = config?.proxy?.baseUrl;
|
|
108
107
|
this.proxyScenario = config?.proxy?.scenario;
|
|
109
108
|
this.proxyRecordOnMiss = config?.proxy?.recordOnMiss;
|
|
109
|
+
// Pass recordResponses through (boolean or array) - envelope handles URL matching
|
|
110
110
|
this.proxyRecordResponses = config?.proxy?.recordResponses ?? false;
|
|
111
111
|
this.getClientId = config?.getClientId;
|
|
112
112
|
this.getStrictLaneScenario = config?.getStrictLaneScenario;
|
|
@@ -194,6 +194,9 @@ export class FetchHTTPClient extends BaseHTTPClient<any, HTTPResponse<any>> {
|
|
|
194
194
|
headers.forEach((value, key) => {
|
|
195
195
|
headerRecord[key] = value;
|
|
196
196
|
});
|
|
197
|
+
|
|
198
|
+
// Dashboard proxy now works in both Node and browser (browser uses fetch, Node uses http)
|
|
199
|
+
const { performDashboardProxyRequest } = require('../core-proxy');
|
|
197
200
|
return performDashboardProxyRequest({
|
|
198
201
|
proxyBaseUrl: this.proxyBaseUrl,
|
|
199
202
|
url,
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser implementation for core-proxy - exports browser-compatible dashboard proxy via fetch
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function applyProxyRecordOnMissEnv(config: any): any {
|
|
6
|
+
return config;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function mirrorProxyRecordingToClient(): void {
|
|
10
|
+
// Browser: no-op
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Export browser implementation of dashboard proxy (uses fetch instead of Node http)
|
|
14
|
+
export { performDashboardProxyRequest } from '@sgedda/mockifyer-core/utils/perform-dashboard-proxy-request';
|
|
15
|
+
|
|
16
|
+
export function initMockifyerForDashboardProxy(): Promise<never> {
|
|
17
|
+
return Promise.reject(new Error(
|
|
18
|
+
'[Mockifyer Browser] Use setupMockifyer from browser entry instead of initMockifyerForDashboardProxy'
|
|
19
|
+
));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function initMockifyerForLocalFilesystem(): never {
|
|
23
|
+
throw new Error('[Mockifyer Browser] Use setupMockifyer from browser entry');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function initMockifyerForDashboardProxyClients(): Promise<never> {
|
|
27
|
+
return Promise.reject(new Error(
|
|
28
|
+
'[Mockifyer Browser] Use setupMockifyer from browser entry instead of initMockifyerForDashboardProxyClients'
|
|
29
|
+
));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function initMockifyerForLocalFilesystemClients(): never {
|
|
33
|
+
throw new Error('[Mockifyer Browser] Use setupMockifyer from browser entry');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function resolveClientInitFlags(): any {
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function needsSiblingClientSetup(): boolean {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function syncDualMockifyerControls(): void {
|
|
45
|
+
// Browser: no-op
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function pickPrimaryDualMockifyerInstance(_transport: string, _flags: any, result: any): any {
|
|
49
|
+
return result.fetch || result.axios;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function loadAxiosSetupMockifyer(): null {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function loadFetchSetupMockifyer(): null {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type InitMockifyerForDashboardProxyOptions = any;
|
|
61
|
+
export type InitMockifyerForLocalFilesystemOptions = any;
|
|
62
|
+
export type DualMockifyerControlSurface = any;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser entry for mockifyer-fetch.
|
|
3
|
+
* Avoids statically loading Node built-ins by not re-exporting the full main index barrel.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Re-export only essential types and browser-safe utilities from core
|
|
7
|
+
// DO NOT use `export * from '@sgedda/mockifyer-core'` - it pulls in Node-dependent modules
|
|
8
|
+
import type {
|
|
9
|
+
MockifyerConfig,
|
|
10
|
+
MockData,
|
|
11
|
+
StoredRequest,
|
|
12
|
+
StoredResponse,
|
|
13
|
+
HTTPClient,
|
|
14
|
+
HTTPResponse,
|
|
15
|
+
DatabaseProvider,
|
|
16
|
+
DatabaseProviderConfig,
|
|
17
|
+
} from '@sgedda/mockifyer-core';
|
|
18
|
+
|
|
19
|
+
export type {
|
|
20
|
+
MockifyerConfig,
|
|
21
|
+
MockData,
|
|
22
|
+
StoredRequest,
|
|
23
|
+
StoredResponse,
|
|
24
|
+
HTTPClient,
|
|
25
|
+
HTTPResponse,
|
|
26
|
+
DatabaseProvider,
|
|
27
|
+
DatabaseProviderConfig,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export {
|
|
31
|
+
MemoryProvider,
|
|
32
|
+
createProvider,
|
|
33
|
+
} from '@sgedda/mockifyer-core';
|
|
34
|
+
|
|
35
|
+
// Re-export browser-safe fetch utilities
|
|
36
|
+
export { FetchHTTPClient } from './clients/fetch-client';
|
|
37
|
+
export { bodyInitForFetchResponse } from './utils/fetch-response-body';
|
|
38
|
+
export { resolvePatchedFetchRequest } from './utils/resolve-patched-fetch-request';
|
|
39
|
+
|
|
40
|
+
export {
|
|
41
|
+
canUseDashboardRedisProxy,
|
|
42
|
+
canUseDashboardCentralProxy,
|
|
43
|
+
} from './utils/dashboard-redis-health';
|
|
44
|
+
|
|
45
|
+
// Import MockifyerClass and browser capabilities
|
|
46
|
+
import { MockifyerClass } from './mockifyer-class';
|
|
47
|
+
import { nodeCapabilities } from './node-capabilities.browser';
|
|
48
|
+
|
|
49
|
+
// MockifyerInstance interface
|
|
50
|
+
export interface MockifyerInstance extends HTTPClient {
|
|
51
|
+
reloadMockData: (syncFromProject?: boolean) => Promise<void>;
|
|
52
|
+
clearStaleCacheEntries: () => number;
|
|
53
|
+
clearAllMocks: () => Promise<void>;
|
|
54
|
+
setClientId: (lane: string) => void;
|
|
55
|
+
getClientId: () => string | undefined;
|
|
56
|
+
setRecordMode: (enabled: boolean) => void;
|
|
57
|
+
isRecordMode: () => boolean;
|
|
58
|
+
setRecordResponses: (enabled: boolean) => void;
|
|
59
|
+
isRecordResponses: () => boolean;
|
|
60
|
+
enableMockifyer: () => void;
|
|
61
|
+
disableMockifyer: () => void;
|
|
62
|
+
isMockifyerEnabled: () => boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Setup Mockifyer for browser/web with in-memory provider.
|
|
67
|
+
*
|
|
68
|
+
* **Browser-specific constraints:**
|
|
69
|
+
* - Only `memory` provider is supported (filesystem/redis/sqlite need Node.js)
|
|
70
|
+
* - Preload fixtures via `databaseProvider.options.fixtures: MockData[]`
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { setupMockifyer } from '@sgedda/mockifyer-fetch';
|
|
75
|
+
* import rates from './mock-data/api.frankfurter.dev/v1/latest.json';
|
|
76
|
+
*
|
|
77
|
+
* const mockifyer = setupMockifyer({
|
|
78
|
+
* mockDataPath: './mock-data',
|
|
79
|
+
* databaseProvider: {
|
|
80
|
+
* type: 'memory',
|
|
81
|
+
* options: {
|
|
82
|
+
* fixtures: [rates]
|
|
83
|
+
* }
|
|
84
|
+
* },
|
|
85
|
+
* recordMode: false,
|
|
86
|
+
* failOnMissingMock: true,
|
|
87
|
+
* useGlobalFetch: true
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
92
|
+
// Validate browser-safe config
|
|
93
|
+
if (config.databaseProvider && config.databaseProvider.type && config.databaseProvider.type !== 'memory') {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`[Mockifyer Browser] Provider '${config.databaseProvider.type}' requires Node.js (fs/path/crypto). ` +
|
|
96
|
+
`Use databaseProvider: { type: 'memory', options: { fixtures: [...] } } in browser builds.`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!config.databaseProvider) {
|
|
101
|
+
console.warn(
|
|
102
|
+
'[Mockifyer Browser] No databaseProvider configured. Falling back to memory provider. ' +
|
|
103
|
+
'Preload fixtures via databaseProvider: { type: "memory", options: { fixtures: [...] } }'
|
|
104
|
+
);
|
|
105
|
+
config.databaseProvider = { type: 'memory', options: {} };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Create with browser capabilities (no Node fs/path/crypto)
|
|
109
|
+
const mockifyer = new MockifyerClass(config, nodeCapabilities);
|
|
110
|
+
|
|
111
|
+
// Get HTTP client
|
|
112
|
+
const httpClient = mockifyer.getHTTPClient();
|
|
113
|
+
|
|
114
|
+
// Patch global fetch if useGlobalFetch is true
|
|
115
|
+
if (config.useGlobalFetch) {
|
|
116
|
+
const originalFetch = globalThis.fetch;
|
|
117
|
+
const mockifyerInstance = mockifyer;
|
|
118
|
+
|
|
119
|
+
// Store original fetch on the http client so it can be used for unmocked requests
|
|
120
|
+
(httpClient as any)._originalFetch = originalFetch;
|
|
121
|
+
|
|
122
|
+
globalThis.fetch = async function(input: string | Request | URL, init?: RequestInit): Promise<Response> {
|
|
123
|
+
if (!mockifyerInstance.isMockifyerEnabled()) {
|
|
124
|
+
return await originalFetch(input, init);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
|
|
128
|
+
const method = (init?.method || 'GET').toUpperCase();
|
|
129
|
+
|
|
130
|
+
// Perform the request through mockifyer
|
|
131
|
+
const response = await httpClient.request({
|
|
132
|
+
method,
|
|
133
|
+
url,
|
|
134
|
+
headers: init?.headers as Record<string, string> || {},
|
|
135
|
+
data: init?.body,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Convert mockifyer response to fetch Response
|
|
139
|
+
return new Response(
|
|
140
|
+
typeof response.data === 'string' ? response.data : JSON.stringify(response.data),
|
|
141
|
+
{
|
|
142
|
+
status: response.status,
|
|
143
|
+
headers: new Headers(response.headers || {})
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Return httpClient extended with mockifyer methods
|
|
150
|
+
const extendedClient = httpClient as any;
|
|
151
|
+
|
|
152
|
+
extendedClient.reloadMockData = (syncFromProject?: boolean) => mockifyer.reloadMockData(syncFromProject);
|
|
153
|
+
extendedClient.clearStaleCacheEntries = () => mockifyer.clearStaleCacheEntries();
|
|
154
|
+
extendedClient.clearAllMocks = () => mockifyer.clearAllMocks();
|
|
155
|
+
extendedClient.setClientId = (lane: string) => mockifyer.setClientId(lane);
|
|
156
|
+
extendedClient.getClientId = () => mockifyer.getClientId();
|
|
157
|
+
extendedClient.setRecordMode = (enabled: boolean) => mockifyer.setRecordMode(enabled);
|
|
158
|
+
extendedClient.isRecordMode = () => mockifyer.isRecordMode();
|
|
159
|
+
extendedClient.setRecordResponses = (enabled: boolean) => mockifyer.setRecordResponses(enabled);
|
|
160
|
+
extendedClient.isRecordResponses = () => mockifyer.isRecordResponses();
|
|
161
|
+
extendedClient.enableMockifyer = () => mockifyer.enableMockifyer();
|
|
162
|
+
extendedClient.disableMockifyer = () => mockifyer.disableMockifyer();
|
|
163
|
+
extendedClient.isMockifyerEnabled = () => mockifyer.isMockifyerEnabled();
|
|
164
|
+
|
|
165
|
+
return extendedClient;
|
|
166
|
+
}
|