| 84 | const originalFetch = globalThis.fetch; |
| 85 | const loopback = new URL(server.issuerUrl); |
| 86 | const patched: typeof fetch = async (input, init) => { |
| 87 | const requestUrl = |
| 88 | typeof input === "string" ? input : input instanceof URL ? input.href : input.url; |
| 89 | const target = new URL(requestUrl); |
| 90 | if (target.hostname === loopback.hostname) { |
| 91 | return originalFetch(input as Parameters<typeof fetch>[0], init); |
| 92 | } |
| 93 | if (target.pathname === "/token") { |
| 94 | const bodyText = |
| 95 | init?.body instanceof URLSearchParams |
| 96 | ? init.body.toString() |
| 97 | : typeof init?.body === "string" |
| 98 | ? init.body |
| 99 | : input instanceof Request |
| 100 | ? await input.clone().text() |
| 101 | : ""; |
| 102 | record.push({ |
| 103 | host: target.hostname, |
| 104 | grantType: new URLSearchParams(bodyText).get("grant_type"), |
| 105 | }); |
| 106 | } |
| 107 | const rerouted = new URL(loopback.origin); |
| 108 | rerouted.pathname = target.pathname; |
| 109 | rerouted.search = target.search; |
| 110 | return input instanceof Request |
| 111 | ? originalFetch(new Request(rerouted.href, input)) |
| 112 | : originalFetch(rerouted.href, init); |
| 113 | }; |
| 114 | // oxlint-disable-next-line executor/no-raw-fetch -- test boundary: install the doubled fetch (see above). |
| 115 | globalThis.fetch = patched; |
| 116 | return () => { |