@r0hitsharma/http-client-msw 0.12.0-rohit-fork-ci.1

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.
@@ -0,0 +1,108 @@
1
+ import type { StartOptions } from 'msw/browser';
2
+
3
+ import { resolveWorkerScriptUrl } from './base-url.js';
4
+
5
+ /**
6
+ * The browser entry's decisions, factored out of `browser.ts` so they are
7
+ * testable without a service worker: a vitest run cannot register one, but it
8
+ * can assert the options that would be passed and the start-once behaviour.
9
+ */
10
+
11
+ export type MockWorkerOptions = {
12
+ /**
13
+ * The app's **public base path** — `import.meta.env.BASE_URL` under Vite — used
14
+ * to locate the worker script for a subpath deployment. This is not the API
15
+ * base from `createMockApi`: the script is served by the app, the API is
16
+ * whatever the app talks to.
17
+ *
18
+ * @default '/'
19
+ */
20
+ baseUrl?: string;
21
+ /**
22
+ * What msw does with a request no handler matched.
23
+ *
24
+ * Defaults to `'bypass'`, unlike msw's own `'warn'`: an app running against
25
+ * mocks still loads its own documents, modules, images, and fonts, and warning
26
+ * on every one of them buries the requests a developer is actually looking at.
27
+ * Use `'warn'` while filling gaps in a mock set.
28
+ */
29
+ onUnhandledRequest?: StartOptions['onUnhandledRequest'];
30
+ /**
31
+ * Suppress msw's per-request console logging. Defaults to `true` — the request
32
+ * log is long enough to hide application logs, and the network panel already
33
+ * shows the same traffic.
34
+ */
35
+ quiet?: boolean;
36
+ /** Escape hatch merged last into msw's own `worker.start()` options. */
37
+ start?: StartOptions;
38
+ };
39
+
40
+ export function buildWorkerStartOptions(
41
+ options: MockWorkerOptions = {},
42
+ ): StartOptions {
43
+ const { baseUrl, onUnhandledRequest, quiet, start } = options;
44
+
45
+ return {
46
+ ...start,
47
+ // Resolved after the spread rather than defaulted before it: an options
48
+ // object forwarded with explicit `undefined` values would otherwise put
49
+ // those keys back and hand msw its own defaults instead of ours. `start`
50
+ // still wins over the named options where it sets a value.
51
+ onUnhandledRequest:
52
+ start?.onUnhandledRequest ?? onUnhandledRequest ?? 'bypass',
53
+ quiet: start?.quiet ?? quiet ?? true,
54
+ serviceWorker: {
55
+ ...start?.serviceWorker,
56
+ // The same hole one level down: a `serviceWorker` object populated from
57
+ // another options bag carries an explicit `url: undefined`, which spread
58
+ // over the resolved path would erase it and send msw to its own default
59
+ // location — the wrong one for a subpath deployment.
60
+ url: start?.serviceWorker?.url ?? resolveWorkerScriptUrl(baseUrl),
61
+ },
62
+ };
63
+ }
64
+
65
+ export type IdempotentStart = {
66
+ /** Runs the wrapped function at most once, until `invalidate()`. */
67
+ start: () => Promise<void>;
68
+ /**
69
+ * Forgets a completed start so the next `start()` runs again. Whatever the
70
+ * start registered has to be torn down first — a memo pointing at a stopped
71
+ * worker resolves immediately and intercepts nothing.
72
+ */
73
+ invalidate: () => void;
74
+ };
75
+
76
+ /**
77
+ * Wraps a start function so concurrent and repeated calls share one
78
+ * registration. An app entry, a hot reload, and a Playwright fixture can all
79
+ * call `start()`; registering the worker again mid-session would reset its
80
+ * handler list and drop any runtime overrides a test had installed.
81
+ *
82
+ * A rejected start invalidates itself, so a caller can retry after fixing
83
+ * whatever failed (a missing `mockServiceWorker.js`, most often).
84
+ */
85
+ export function createIdempotentStart(
86
+ start: () => Promise<void>,
87
+ ): IdempotentStart {
88
+ let pending: Promise<void> | undefined;
89
+
90
+ const invalidate = (): void => {
91
+ pending = undefined;
92
+ };
93
+
94
+ return {
95
+ start: () => {
96
+ // Called through an async wrapper so a synchronous throw from `start`
97
+ // rejects the returned promise instead of escaping as an exception the
98
+ // caller has no way to `.catch`.
99
+ pending ??= (async () => await start())().catch((error: unknown) => {
100
+ invalidate();
101
+ throw error;
102
+ });
103
+
104
+ return pending;
105
+ },
106
+ invalidate,
107
+ };
108
+ }