@dbs-portal/tool-mock
Version:
API mocking toolkit using MSW for DBS Portal development workflows
424 lines (423 loc) • 9.92 kB
JavaScript
import { b as f, c as w, g } from "./environment-DlK8CH_m.js";
import { setupWorker as v } from "msw/browser";
let i = null;
const d = {
enabled: !1,
mode: "disabled",
baseUrl: "",
delay: 0,
logging: !1,
handlers: [],
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0,
timeoutErrorRate: 0,
customErrors: []
},
serviceWorker: {
url: "/mockServiceWorker.js",
scope: "/",
updateViaCache: "none"
}
};
function c(e = {}) {
const r = f(), n = w(), t = {};
if (n.enabled !== void 0 && (t.enabled = n.enabled === "true"), n.mode && (t.mode = n.mode), n.baseUrl && (t.baseUrl = n.baseUrl), n.delay) {
const s = parseInt(n.delay, 10);
isNaN(s) || (t.delay = s);
}
return n.logging !== void 0 && (t.logging = n.logging === "true"), i = {
...d,
...r,
...t,
...e,
errorSimulation: {
...d.errorSimulation,
...r.errorSimulation,
...t.errorSimulation,
...e.errorSimulation
},
serviceWorker: {
...d.serviceWorker,
...t.serviceWorker,
...e.serviceWorker
}
}, i;
}
function l() {
return i || c();
}
function R(e) {
return i || (i = c()), i = {
...i,
...e,
errorSimulation: {
...i.errorSimulation,
...e.errorSimulation
},
serviceWorker: {
...i.serviceWorker,
...e.serviceWorker
}
}, i;
}
function M() {
return i = null, c();
}
function E(e) {
const r = [];
if (e.delay !== void 0)
if (Array.isArray(e.delay)) {
const [n, t] = e.delay;
(n < 0 || t < 0) && r.push("Delay values must be non-negative"), n > t && r.push("Minimum delay must be less than or equal to maximum delay");
} else typeof e.delay == "number" && e.delay < 0 && r.push("Delay must be non-negative");
if (e.errorSimulation) {
const { networkErrorRate: n, serverErrorRate: t, timeoutErrorRate: s } = e.errorSimulation;
n !== void 0 && (n < 0 || n > 1) && r.push("Network error rate must be between 0 and 1"), t !== void 0 && (t < 0 || t > 1) && r.push("Server error rate must be between 0 and 1"), s !== void 0 && (s < 0 || s > 1) && r.push("Timeout error rate must be between 0 and 1");
}
return e.baseUrl && !k(e.baseUrl) && r.push("Base URL must be a valid URL"), r;
}
function m(e) {
const r = e.config || {};
return e.handlers && e.handlers.length > 0 && (r.handlers = [
...r.handlers || [],
...e.handlers
]), c(r);
}
function C(e) {
switch (e) {
case "development":
return {
enabled: !0,
mode: "development",
logging: !0,
delay: [100, 300],
errorSimulation: {
networkErrorRate: 0.01,
serverErrorRate: 5e-3
}
};
case "testing":
return {
enabled: !0,
mode: "testing",
logging: !1,
delay: 0,
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0
}
};
case "storybook":
return {
enabled: !0,
mode: "storybook",
logging: !1,
delay: [50, 150],
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0
}
};
case "production":
default:
return {
enabled: !1,
mode: "disabled",
logging: !1,
delay: 0,
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0
}
};
}
}
function k(e) {
try {
return new URL(e), !0;
} catch {
return !1;
}
}
class h {
worker;
_isRunning = !1;
constructor(r) {
this.worker = v(...r);
}
async start(r) {
if (this._isRunning) {
console.warn("MSW browser worker is already running");
return;
}
try {
await this.worker.start({
onUnhandledRequest: "warn",
quiet: !1,
...r
}), this._isRunning = !0;
} catch (n) {
throw console.error("Failed to start MSW browser worker:", n), n;
}
}
stop() {
this._isRunning && (this.worker.stop(), this._isRunning = !1);
}
use(...r) {
this.worker.use(...r);
}
resetHandlers(...r) {
this.worker.resetHandlers(...r);
}
restoreHandlers() {
this.worker.restoreHandlers();
}
isRunning() {
return this._isRunning;
}
getWorker() {
return this.worker;
}
}
async function p(e, r) {
if (typeof window > "u")
throw new Error("setupBrowser can only be called in browser environment");
if (!("serviceWorker" in navigator))
throw new Error("Service Worker is not supported in this browser");
const n = new h(e);
return r.logging && console.log("MSW: Setting up browser worker with", e.length, "handlers"), n;
}
async function U(e = "public", r = "mockServiceWorker.js") {
if (typeof window > "u")
throw new Error("Service worker can only be initialized in browser environment");
await import("msw/browser");
const n = `/${r}`;
try {
if (!(await fetch(n, { method: "HEAD" })).ok) {
console.warn(
`MSW service worker not found at ${n}. Run 'npx msw init ${e}' to generate it.`
);
return;
}
} catch (t) {
console.warn("Failed to check MSW service worker:", t);
return;
}
}
async function H() {
if (typeof window > "u" || !("serviceWorker" in navigator))
return !1;
try {
return await navigator.serviceWorker.getRegistration() !== void 0;
} catch {
return !1;
}
}
async function I() {
if (typeof window > "u" || !("serviceWorker" in navigator))
return null;
try {
return await navigator.serviceWorker.getRegistration() || null;
} catch {
return null;
}
}
async function F() {
if (typeof window > "u" || !("serviceWorker" in navigator))
return !1;
try {
const e = await navigator.serviceWorker.getRegistration();
return e ? await e.unregister() : !0;
} catch {
return !1;
}
}
function D() {
return {
serviceWorker: {
url: "/mockServiceWorker.js",
scope: "/",
updateViaCache: "none"
}
};
}
function $() {
const e = [];
return typeof window > "u" && e.push("Not running in browser environment"), typeof navigator > "u" && e.push("Navigator not available"), "serviceWorker" in navigator || e.push("Service Worker not supported"), "fetch" in window || e.push("Fetch API not supported"), {
compatible: e.length === 0,
issues: e
};
}
let o = null, a = !1;
async function u(e = {}) {
if (a) {
console.warn("MSW is already set up");
return;
}
const r = m(e);
if (!r.enabled) {
r.logging && console.log("MSW: Mocking is disabled");
return;
}
const n = g();
r.logging && console.log(`MSW: Setting up in ${n.environment} environment (${r.mode} mode)`);
const t = [
...r.handlers || [],
...e.handlers || []
];
try {
if (n.environment === "browser")
o = await p(t, r), e.start !== !1 && await o.start({
onUnhandledRequest: r.mode === "development" ? "warn" : "bypass",
quiet: !r.logging
});
else if (n.environment === "node") {
const { setupNodeServer: s } = await import("./server-C3vfOwOG.js");
o = s(t, r), e.start !== !1 && await o.start({
onUnhandledRequest: r.mode === "testing" ? "error" : "warn"
});
} else
throw new Error(`Unsupported environment: ${n.environment}`);
a = !0, r.logging && console.log(`MSW: Successfully set up with ${t.length} handlers`);
} catch (s) {
throw console.error("MSW: Failed to setup:", s), s;
}
}
async function y() {
if (!a || !o)
return;
const e = l();
try {
o.stop(), o = null, a = !1, e.logging && console.log("MSW: Successfully torn down");
} catch (r) {
throw console.error("MSW: Failed to teardown:", r), r;
}
}
async function q(e = {}) {
await y(), await u(e);
}
function A(...e) {
if (!a || !o) {
console.warn("MSW: Cannot add handlers - MSW is not set up");
return;
}
o.use(...e), l().logging && console.log(`MSW: Added ${e.length} handlers`);
}
function B(...e) {
if (!a || !o) {
console.warn("MSW: Cannot reset handlers - MSW is not set up");
return;
}
o.resetHandlers(...e), l().logging && console.log("MSW: Reset handlers to original state");
}
function N() {
if (!a || !o) {
console.warn("MSW: Cannot restore handlers - MSW is not set up");
return;
}
o.restoreHandlers(), l().logging && console.log("MSW: Restored original handlers");
}
function _() {
return a && o !== null;
}
function S() {
return a && o !== null && o.isRunning();
}
function x() {
return o;
}
async function V(e = []) {
g().shouldMock && await u({
handlers: e,
start: !0
});
}
async function j(e = []) {
await u({
config: {
enabled: !0,
mode: "development",
logging: !0,
delay: [100, 300],
errorSimulation: {
networkErrorRate: 0.01,
serverErrorRate: 5e-3
}
},
handlers: e,
start: !0
});
}
async function z(e = []) {
await u({
config: {
enabled: !0,
mode: "testing",
logging: !1,
delay: 0,
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0
}
},
handlers: e,
start: !0
});
}
async function L(e = []) {
await u({
config: {
enabled: !0,
mode: "storybook",
logging: !1,
delay: [50, 150],
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0
}
},
handlers: e,
start: !0
});
}
function T() {
return {
isSetup: a,
isRunning: S(),
hasInstance: o !== null,
config: a ? l() : null,
environment: g()
};
}
export {
D as A,
$ as B,
A as a,
B as b,
N as c,
S as d,
V as e,
j as f,
x as g,
z as h,
_ as i,
L as j,
T as k,
l,
c as m,
M as n,
m as o,
C as p,
p as q,
q as r,
u as s,
y as t,
R as u,
E as v,
U as w,
H as x,
I as y,
F as z
};
//# sourceMappingURL=setup-oE-TArFC.js.map