23 lines
1.0 KiB
TypeScript
23 lines
1.0 KiB
TypeScript
import { fakeApi, makeUser } from './fakeApi';
|
|
import { mobileFixtures } from './mobileFixtures';
|
|
|
|
/** Подменяет fetch на фейковый API. Включается только при сборке с VITE_E2E=1. */
|
|
export function installE2EFetch() {
|
|
const api = fakeApi([
|
|
makeUser('admin', 'admin', { must_change_credentials: true }),
|
|
makeUser('alice', 'user'),
|
|
makeUser('mobile-admin', 'admin', { reviews_uploads: true }),
|
|
makeUser('moderator', 'moderator', { reviews_uploads: true }),
|
|
]);
|
|
const orig = window.fetch.bind(window);
|
|
window.fetch = async (input, init) => {
|
|
const req = new Request(input, init);
|
|
const path = new URL(req.url, window.location.origin).pathname;
|
|
if (req.method === 'GET' && path in mobileFixtures) {
|
|
return new Response(JSON.stringify(mobileFixtures[path]), { headers: { 'Content-Type': 'application/json' } });
|
|
}
|
|
if (new URL(req.url, window.location.origin).pathname.startsWith('/api/')) return api.fetch(req);
|
|
return orig(input, init);
|
|
};
|
|
}
|