()
| 51 | } |
| 52 | |
| 53 | export function patchFetchForBasePath() { |
| 54 | if (window.__FR_FETCH_BASEPATCH__) return; |
| 55 | window.__FR_FETCH_BASEPATCH__ = true; |
| 56 | |
| 57 | const nativeFetch = window.fetch.bind(window); |
| 58 | window.fetch = (input, init) => { |
| 59 | try { |
| 60 | const base = getBasePath(); |
| 61 | if (base) { |
| 62 | if (typeof input === 'string') { |
| 63 | if (input.startsWith('/api/') && !input.startsWith(base + '/api/')) { |
| 64 | return nativeFetch(base + input, init); |
| 65 | } |
| 66 | } else if (input && typeof input.url === 'string') { |
| 67 | const u = new URL(input.url, window.location.origin); |
| 68 | if (u.origin === window.location.origin && u.pathname.startsWith('/api/') && !u.pathname.startsWith(base + '/api/')) { |
| 69 | const rewritten = new URL(base + u.pathname + u.search + u.hash, window.location.origin); |
| 70 | const req = new Request(rewritten.toString(), input); |
| 71 | return nativeFetch(req, init); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } catch (e) { |
| 76 | // fall through |
| 77 | } |
| 78 | return nativeFetch(input, init); |
| 79 | }; |
| 80 | } |
| 81 |
no test coverage detected