import {Album, Photo, User} from "../types";

class Api {
  private BASE_URL = "https://jsonplaceholder.typicode.com";

  private get(path: string) {
    return fetch(`${this.BASE_URL}${path}`)
      .then(data => data.json())
      .catch((e) => console.error("Fetch failed with error", e));
  }

  getPhotos(): Promise<Photo[]> {
    return this.get("/photos");
  }

  getPhoto(id: number): Promise<Photo> {
    return this.get(`/photos/${id}`);
  }

  getAlbum(id: number): Promise<Album> {
    return this.get(`/albums/${id}`);
  }

  getUser(id: number): Promise<User> {
    return this.get(`/users/${id}`);
  }
}

const client = new Api();

export default client;
