api.ts 819 Bytes
import type { HomeData } from '../types/home';
import { MOCK_FIXTURES, createMockHomeData } from './data';

let currentFixture: keyof typeof MOCK_FIXTURES = 'normal';

export function setMockFixture(fixture: keyof typeof MOCK_FIXTURES): void {
  currentFixture = fixture;
}

export function resetMockFixture(): void {
  currentFixture = 'normal';
}

export async function fetchHomeData(): Promise<HomeData> {
  await new Promise((resolve) => setTimeout(resolve, 100));

  const fixture = MOCK_FIXTURES[currentFixture];
  if (!fixture) {
    throw new Error(`Unknown fixture: ${currentFixture}`);
  }

  return fixture;
}

export async function fetchHomeDataWithError(): Promise<HomeData> {
  await new Promise((resolve) => setTimeout(resolve, 100));
  throw new Error('Network error');
}

export { createMockHomeData };