UNPKG

@dbs-portal/tool-mock

Version:

API mocking toolkit using MSW for DBS Portal development workflows

93 lines (92 loc) 2.22 kB
import { setupServer as i } from "msw/node"; class t { server; _isRunning = !1; constructor(e) { this.server = i(...e); } async start(e) { if (this._isRunning) { console.warn("MSW server is already running"); return; } try { this.server.listen({ onUnhandledRequest: "warn", ...e }), this._isRunning = !0; } catch (n) { throw console.error("Failed to start MSW server:", n), n; } } stop() { this._isRunning && (this.server.close(), this._isRunning = !1); } use(...e) { this.server.use(...e); } resetHandlers(...e) { this.server.resetHandlers(...e); } restoreHandlers() { this.server.restoreHandlers(); } isRunning() { return this._isRunning; } getServer() { return this.server; } } function l(r, e) { if (typeof process > "u") throw new Error("setupServer can only be called in Node.js environment"); const n = new t(r); return e.logging && console.log("MSW: Setting up Node.js server with", r.length, "handlers"), n; } function a() { return { // Node.js doesn't need service worker configuration }; } function c() { const r = []; if (typeof process > "u" && r.push("Not running in Node.js environment"), typeof global > "u" && r.push("Global object not available"), typeof process < "u" && process.versions?.node) { const e = process.versions.node; parseInt(e?.split(".")[0] || "0", 10) < 16 && r.push(`Node.js version ${e} is not supported. Minimum version is 16.`); } return { compatible: r.length === 0, issues: r }; } function o(r = []) { return new t(r); } function d(r = []) { const e = o(r); return { server: e, start: () => e.start({ onUnhandledRequest: "error" }), stop: () => e.stop(), reset: () => e.resetHandlers(), use: (...n) => e.use(...n) }; } let s = null; function f(r = []) { return s || (s = o(r)), s; } function p() { s && (s.stop(), s = null); } export { c as checkNodeCompatibility, d as createTestServer, f as getGlobalNodeTestServer, a as getNodeDefaults, p as resetGlobalTestServer, l as setupNodeServer, o as setupTestServer }; //# sourceMappingURL=server-C3vfOwOG.js.map