All files / src/view/pages/MainPage/DetailPage DetailPage.service.ts

0% Statements 0/42
0% Branches 0/1
0% Functions 0/1
0% Lines 0/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                                                                                                   
import HttpService from '@/core/services/http/http.service';
import { ICharacter, IEpisode, ILocation } from '@/shared/types/character';
import { catchError, from, map, Observable, of } from 'rxjs';
 
export const api = 'https://rickandmortyapi.com/api';
 
const http = new HttpService();
 
export const getCharacterById = (id: string): Observable<ICharacter> => {
  return from(http.get<ICharacter>(`${api}/character/${id}`, {})).pipe(
    map((response) => response ?? {}),
    catchError((error) => {
      console.error('Error fetching character', error);
      return of(error);
    })
  );
};
 
export const getEpisodesByUrls = (urls: string[]): Observable<IEpisode[]> => {
  const episodeIds = urls.map((url) => url.split('/').pop()).join(',');
  return from(http.get<IEpisode[]>(`${api}/episode/${episodeIds}`, {})).pipe(
    map((response) => (Array.isArray(response) ? response : [response])),
    catchError((error) => {
      console.error('Error fetching episode', error);
      return of(error);
    })
  );
};
 
export const getLocationByUrl = (url: string): Observable<ILocation> => {
  return from(http.get<ILocation>(url, {})).pipe(
    map((response) => response),
    catchError((error) => {
      console.error('Error fetching character location', error);
      return of(error);
    })
  );
};
 
export const getCharactersByUrls = (urls: string[]): Observable<ICharacter[]> => {
  const ids = urls.map((url) => url.split('/').pop()).join(',');
  return from(http.get<ICharacter[]>(`${api}/character/${ids}`, {})).pipe(
    map((response) => (Array.isArray(response) ? response : [response])),
    catchError((error) => {
      console.error('Error fetching characters', error);
      return of(error);
    })
  );
};